Zoom in and out an image on mouse click using MVVM - c#

Here in this application, I've used buttons to zoom in and out an image using MVVM in WPF. On clicking zoom in button, the height and width are increased by a particular amount and vice versa for the zoom out. but I want to achieve the same using a mouse double click. Is there any way to achieve the same using MVVM?
private ICommand zoomOutCommand;
public ICommand ZoomOutCommand {
get {
if (zoomOutCommand == null) {
zoomOutCommand = new RelayCommand < object > (ZoomOutExecute, OutReturnBool, false);
}
return zoomOutCommand;
}
}
private ICommand zoomInCommand;
public ICommand ZoomInCommand {
get {
if (zoomInCommand == null) {
zoomInCommand = new RelayCommand < object > (ZoomInExecute, InReturnBool, false);
}
return zoomInCommand;
}
}
private void ZoomInExecute(object obj) {
// Scale += stepScale;
Height *= 1.2;
Width *= 1.2;
}
private bool InReturnBool(object obj) {
if (Height > 6 * iniWidth) return false;
else return true;
}
private bool OutReturnBool(object obj) {
if (Height < 0.1 * iniHeight) return false;
else return true;
}
private void ZoomOutExecute(object obj) {
Height *= 0.8;
Width *= 0.8;
}
private const double iniWidth = 500;
private double width = iniWidth;
public double Width {
get {
return width;
}
set {
width = value;
NotifyPropertyChanged("Width");
}
}
private const double iniHeight = 500;
private double height = iniHeight;
public double Height {
get {
return height;
}
set {
height = value;
NotifyPropertyChanged("Height");
}
}

For a double click you can bind the command this way
<Button>
<Button.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding YourCommand}" />
</Button.InputBindings>
</Button>

Related

Creating and comparing objects based on user input

I'm pretty new to C# and I have a task to solve.
I need to create a console app that asks the user to make 2 figures(either rectangle or hexagon again selected by the user input) to enter their properties and compare which one is bigger. For some reason I get not all code paths return a value in SelectAndCreateFigure even after using the else statement returning null.
This is my code:
figure class:
namespace PU_task
{
public class Figure
{
public virtual double Area()
{
return 0;
}
}
}
hexagon class:
using System;
namespace PU_task
{
public class Hexagon :Figure
{
private double side;
public double Side { get => side; set => side = value; }
public Hexagon()
{
}
public override double Area()
{
double area = 3 * Math.Sqrt(3 * Math.Pow(side, 2)) / 2;
return area;
}
public Figure CreateFigure(double side)
{
Hexagon hexagon = new Hexagon
{
Side = side
};
return hexagon;
}
}
}
rectangle class:
namespace PU_task
{
public class Rectangle : Figure
{
private double length;
private double width;
public double Length { get => length; set => length = value; }
public double Width { get => width; set => width = value; }
public Rectangle()
{
}
public override double Area()
{
double area = length * width;
return area;
}
public Figure CreateFigure(double width,double length)
{
Rectangle rectangle = new Rectangle
{
Width = width,
Length = length
};
return rectangle;
}
}
}
The code in my main file:
using System;
namespace PU_task
{
internal class Program
{
static void Main(string[] args)
{
Figure figure1 = new Figure();
figure1.SelectAndCreateFigure();
Figure figure2 = new Figure();
figure2.SelectAndCreateFigure();
SmallerArea(figure1, figure2);
}
public Figure SelectAndCreateFigure(Figure figure)
{
Console.WriteLine("Select figure type:");
string input = Console.ReadLine().ToLower();
if (input == "rectangle")
{
Console.WriteLine("Enter length:");
double length = double.Parse(Console.ReadLine());
Console.WriteLine("Enter width:");
double width = double.Parse(Console.ReadLine());
Rectangle rectangle = new Rectangle();
rectangle.CreateFigure(width, length);
return rectangle;
}
else if (input == "hexagon")
{
Console.WriteLine("Enter the the side length:");
double side = double.Parse(Console.ReadLine());
Hexagon hexagon = new Hexagon();
hexagon.CreateFigure(side);
}
else return null;
}
public void SmallerArea(Figure figure1, Figure figure2)
{
if (figure1.Area() > figure2.Area())
{
Console.WriteLine(figure1.Area());
}
else
{
Console.WriteLine(figure2.Area());
}
}
}
}
You have an issue with initializing your objects with the correct data. Each class needs a constructor to fill in the data. Also C# supports properties, that can either store values (like the side of the hexagon), or that can return calculated values (like the area of the figure).
A basic skeleton for the above functionality is shown below:
public abstract class Figure
{
public abstract double Area { get; }
}
public class Rectangle : Figure
{
public Rectangle(double length, double width)
{
Length = length;
Width = width;
}
public double Length { get; }
public double Width { get; }
public override double Area => Length * Width;
}
public class Hexagon : Figure
{
public Hexagon(double side)
{
Side = side;
}
public double Side { get; }
public override double Area => 3 * Math.Sqrt(3 * Math.Pow(Side, 2)) / 2;
}
class Program
{
static void Main(string[] args)
{
Figure rect = new Rectangle(12.0, 5.5);
Figure hex = new Hexagon(8.0);
if (rect.Area > hex.Area)
{
Console.WriteLine("Rectangle is bigger");
}
else
{
Console.WriteLine("Hexagon is bigger");
}
}
}
It is up to you to design the required functionality based on the class hierarchy above.
try this
static void Main(string[] args)
{
double hexagonArea=0;
double rectangleArea=0;
CreateFigures(ref hexagonArea, ref rectangleArea);
Console.WriteLine("rectangle area - "+rectangleArea.ToString());
Console.WriteLine("hexagon area - "+hexagonArea.ToString());
if (rectangleArea > hexagonArea)
Console.WriteLinge ("rectangle area is bigger!");
else Console.WriteLinge ("hexagon area is bigger!");
}
public void CreateFigures(ref double hexagonArea, ref double rectangleArea)
{
for (int i = 0; i < 2; i++)
{
Console.WriteLine("Select figure type:");
string input = Console.ReadLine();
if (input == "rectangle")
{
Console.WriteLine("Enter length:");
double length = double.Parse(Console.ReadLine());
Console.WriteLine("Enter width:");
double width = double.Parse(Console.ReadLine());
rectangleArea= CreateRectangle(width, length);
}
else if (input == "hexagon")
{
Console.WriteLine("Enter width:");
double side = double.Parse(Console.ReadLine());
hexagonArea = CreateHexagon(side);
}
}
}
public void CreateRectangle(double length,double width)
{
Rectangle rectangle = new Rectangle
{
Width = width,
Length = length
};
return rectangle.Area();
}
public double CreateHexagon(double side)
{
Hexagon hexagon = new Hexagon
{
Side = side
};
return hexagon.Area();
}
The reason you getting 'not all code paths return a value' error in SelectAndCreateFigure is because in your else if statement you not returning figure. this will solve that particular error.
public Figure SelectAndCreateFigure(Figure figure)
{
Console.WriteLine("Select figure type:");
string input = Console.ReadLine().ToLower();
if (input == "rectangle")
{
Console.WriteLine("Enter length:");
double length = double.Parse(Console.ReadLine());
Console.WriteLine("Enter width:");
double width = double.Parse(Console.ReadLine());
Rectangle rectangle = new Rectangle();
rectangle.CreateFigure(width, length);
return rectangle;
}
else if (input == "hexagon")
{
Console.WriteLine("Enter the the side length:");
double side = double.Parse(Console.ReadLine());
Hexagon hexagon = new Hexagon();
hexagon.CreateFigure(side);
return hexagon;
}
else return null;
}

Select a line on window br clicking on it

I want to make the If condition in the isAT to get the position of the line on the screen.Which mean when the mouse on the line and I click on the line I am able to select it.What I need is to able to select the Line when I click on it by the mouse.Thanks for help in advance.
public class MyLine : Shape
{
private float _endY, _endX;
public MyLine() : this(Color.Orange, 0 , 0, 0, 0)
{
}
public MyLine(Color clr, float starX, float starY, float endX, float endY ) : base(clr)
{
X = starX;
Y = starY;
this.endX = endX;
this.endY = endY;
}
public float endX
{
get { return _endX; }
set { _endX = value; }
}
public float endY
{
get { return _endY; }
set { _endY = value; }
}
public override bool IsAT(Point2D pt)
{
if (pt.X > X && pt.Y < Y && pt.X > endX && pt.Y > endY)
return true;
else
return false;
}
}

Snake. Roll back the coordinates of the snake to impact

I can’t realize the possibility of continuing the game after a collision. The snake should stop and start moving after clicking on one of the arrow buttons.
In a collision, a window appears about the loss, I need to continue the game.
I press the button and the following happens:
I don’t understand how I can save the coordinates of the snake just before the collision.
In the moveTimer_Tick method, all elements move, i.e. new coordinates have already appeared at the head and body, then there is a check for collisions with the wall and body. If they are found, a window appears about the loss.
New snake coordinates are not displayed. But after clicking the "Continue" button, an update occurs and the snake climbs to the border.
The question is: how can I save the coordinates of the snake, before the collision, and after continuing to start with them.
namespace Snake{
public partial class MainWindow : Window
{
//The field on which the snake lives
Entity field;
// snake head
Head head;
// whole snake
List<PositionedEntity> snake;
// apple
Apple apple;
//number of points
int score;
// Is movement paused
bool paused;
//time
DispatcherTimer moveTimer;
//constructor form
public MainWindow()
{
InitializeComponent();
snake = new List<PositionedEntity>();
//create field 600x600pixels
field = new Entity(600, 600, "pack://application:,,,/Resources/snake.png");
//create a timer that runs every 300 ms
moveTimer = new DispatcherTimer();
moveTimer.Interval = new TimeSpan(0, 0, 0, 0, 300);
moveTimer.Tick += new EventHandler(moveTimer_Tick);
}
//redraw screen method
private void UpdateField()
{
//update the position of the elements of the snake
foreach (var p in snake)
{
Canvas.SetTop(p.image, p.y);
Canvas.SetLeft(p.image, p.x);
}
//update the position of apple
Canvas.SetTop(apple.image, apple.y);
Canvas.SetLeft(apple.image, apple.x);
//points update
lblScore.Content = String.Format("{0}000", score);
}
//timer tick handler. All movement takes place here.
void moveTimer_Tick(object sender, EventArgs e)
{
// Do not update if movement is paused
if(paused) {
return;
}
//in the reverse order we move all the elements of the snake
foreach (var p in Enumerable.Reverse(snake))
{
p.move();
}
//we check that the head of the snake did not crash into the body
foreach (var p in snake.Where(x => x != head))
{
if (p.x == head.x && p.y == head.y)
{
//we lose
moveTimer.Stop();
GameOver.Visibility = Visibility.Visible;
btnRestart.Visibility = Visibility.Visible;
tbScore.Text = String.Format("SCORE: {0}000", score);
return;
}
}
//check that the head of the snake did not go out of the field
if (head.x < 40 || head.x >= 540 || head.y < 40 || head.y >= 540)
{
//we lose
moveTimer.Stop();
GameOver.Visibility = Visibility.Visible;
btnRestart.Visibility = Visibility.Visible;
tbScore.Text = String.Format("SCORE: {0}000", score);
return;
}
//check that the head of the snake crashed into an apple
if (head.x == apple.x && head.y == apple.y)
{
//increase the score
score++;
//move the apple to a new place
apple.move();
var part = new BodyPart(snake.Last());
canvas1.Children.Add(part.image);
snake.Add(part);
}
UpdateField();
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
// Unpause movement when any key is pressed
if(paused) {
paused = false;
}
switch (e.Key)
{
case Key.Up:
head.direction = Head.Direction.UP;
break;
case Key.Down:
head.direction = Head.Direction.DOWN;
break;
case Key.Left:
head.direction = Head.Direction.LEFT;
break;
case Key.Right:
head.direction = Head.Direction.RIGHT;
break;
}
}
// "Start"
private void button1_Click(object sender, RoutedEventArgs e)
{
btnStart.Visibility = Visibility.Hidden;
btnRestart.Visibility = Visibility.Hidden;
tBNotEnoughPoints.Visibility = Visibility.Hidden;
score = 0;
snake.Clear();
canvas1.Children.Clear();
// "Game Over"
GameOver.Visibility = Visibility.Hidden;
canvas1.Children.Add(field.image);
apple = new Apple(snake);
canvas1.Children.Add(apple.image);
head = new Head();
snake.Add(head);
canvas1.Children.Add(head.image);
moveTimer.Start();
UpdateField();
}
private void btnContinue_Click(object sender, RoutedEventArgs e)
{
if (score >= 2)
{
GameOver.Visibility = Visibility.Hidden;
btnRestart.Visibility = Visibility.Hidden;
score -= 2;
// Pause movement
paused = true;
moveTimer.Start();
UpdateField();
}
else
{
tBNotEnoughPoints.Visibility = Visibility.Visible;
}
}
public class Entity
{
protected int m_width;
protected int m_height;
Image m_image;
public Entity(int w, int h, string image)
{
m_width = w;
m_height = h;
m_image = new Image();
m_image.Source = (new ImageSourceConverter()).ConvertFromString(image) as ImageSource;
m_image.Width = w;
m_image.Height = h;
}
public Image image
{
get
{
return m_image;
}
}
}
public class PositionedEntity : Entity
{
protected int m_x;
protected int m_y;
public PositionedEntity(int x, int y, int w, int h, string image)
: base(w, h, image)
{
m_x = x;
m_y = y;
}
public virtual void move() { }
public int x
{
get
{
return m_x;
}
set
{
m_x = value;
}
}
public int y
{
get
{
return m_y;
}
set
{
m_y = value;
}
}
}
public class Apple : PositionedEntity
{
List<PositionedEntity> m_snake;
public Apple(List<PositionedEntity> s)
: base(0, 0, 40, 40, "pack://application:,,,/Resources/fruit.png")
{
m_snake = s;
move();
}
public override void move()
{
Random rand = new Random();
do
{
x = rand.Next(13) * 40 + 40 ;
y = rand.Next(13) * 40 + 40 ;
bool overlap = false;
foreach (var p in m_snake)
{
if (p.x == x && p.y == y)
{
overlap = true;
break;
}
}
if (!overlap)
break;
} while (true);
}
}
public class Head : PositionedEntity
{
public enum Direction
{
RIGHT, DOWN, LEFT, UP, NONE
};
Direction m_direction;
public Direction direction {
set
{
m_direction = value;
RotateTransform rotateTransform = new RotateTransform(90 * (int)value);
image.RenderTransform = rotateTransform;
}
}
public Head()
: base(280, 280, 40, 40, "pack://application:,,,/Resources/head.png")
{
image.RenderTransformOrigin = new Point(0.5, 0.5);
m_direction = Direction.NONE;
}
public override void move()
{
switch (m_direction)
{
case Direction.DOWN:
y += 40;
break;
case Direction.UP:
y -= 40;
break;
case Direction.LEFT:
x -= 40;
break;
case Direction.RIGHT:
x += 40;
break;
}
}
}
public class BodyPart : PositionedEntity
{
PositionedEntity m_next;
public BodyPart(PositionedEntity next)
: base(next.x, next.y, 40, 40, "pack://application:,,,/Resources/body.png")
{
m_next = next;
}
public override void move()
{
x = m_next.x;
y = m_next.y;
}
}
}
}
There is something to say about the design of your code, but if you don't care and you want a fast (and ugly) solution you can modify your PositionEntity in order to store old coordinates:
public class PositionedEntity : Entity
{
protected int m_x;
protected int m_y;
protected int m_oldX;
protected int m_oldY;
public PositionedEntity(int x, int y, int w, int h, string image)
: base(w, h, image)
{
m_x = x;
m_y = y;
m_oldX = x;
m_oldY = y;
}
public virtual void move() { }
public virtual void RestorePrevious()
{
m_x = m_oldX;
m_y = m_oldY;
}
public int x
{
get
{
return m_x;
}
set
{
m_oldX = m_x;
m_x = value;
}
}
public int y
{
get
{
return m_y;
}
set
{
m_oldY = m_y;
m_y = value;
}
}
}
When you have a collision you should call the RestorePrevious() on the head and on all the rest of the snake

Check if text fits inside textview

I have a problem. I need to adjust the font size of a text, so it fits inside the TextView. But how can I get the size of the TextView and the size of the Text to compare with each other?
If android api is above 8 , you can do that easily in axml:
<TextView
android:id="#+id/text"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="80dp" //code one
android:autoSizeMinTextSize="2dp" // code two
android:autoSizeTextType="uniform" // code three
android:background="#android:color/holo_red_light"
android:gravity="center"
android:maxLines="1" // be sure not return line
android:text="Here is auto size text"
android:textColor="#android:color/white"
android:textSize="80dp" />
else you can refer to the follow code to custom a TextView:
using Android.Content;
using Android.Runtime;
using Android.Widget;
using Android.Util;
using Android.Text;
using Java.Lang;
namespace My.Text
{
public class AutoResizeTextView : TextView
{
public const float MIN_TEXT_SIZE = 20;
public interface OnTextResizeListener
{
void OnTextResize(TextView textView, float oldSize, float newSize);
}
private const string mEllipsis = "...";
private OnTextResizeListener mTextResizeListener;
private bool mNeedsResize = false;
private float mTextSize;
private float mMaxTextSize = 0;
private float mMinTextSize = MIN_TEXT_SIZE;
private float mSpacingMult = 1.0f;
private float mSpacingAdd = 0.0f;
public bool AddEllipsis { get; set; } = true;
public AutoResizeTextView(Context context) : this(context, null) { }
public AutoResizeTextView(Context context, IAttributeSet attrs) : this(context, attrs, 0) { }
public AutoResizeTextView(Context context, IAttributeSet attrs, int defStyle): base(context, attrs, defStyle)
{
mTextSize = TextSize;
}
protected override void OnTextChanged(ICharSequence text, int start, int lengthBefore, int lengthAfter)
{
base.OnTextChanged(text, start, lengthBefore, lengthAfter);
mNeedsResize = true;
ResetTextSize();
}
protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
{
base.OnSizeChanged(w, h, oldw, oldh);
if (w != oldw || h != oldh)
mNeedsResize = true;
}
public void SetOnResizeListener(OnTextResizeListener listener)
{
mTextResizeListener = listener;
}
public override void SetTextSize([GeneratedEnum] ComplexUnitType unit, float size)
{
base.SetTextSize(unit, size);
mTextSize = TextSize;
}
public override void SetLineSpacing(float add, float mult)
{
base.SetLineSpacing(add, mult);
mSpacingMult = mult;
mSpacingAdd = add;
}
public void SetMaxTextSize(float maxTextSize)
{
mMaxTextSize = maxTextSize;
RequestLayout();
Invalidate();
}
public float GetMaxTextSize()
{
return mMaxTextSize;
}
public void SetMinTextSize(float minTextSize)
{
mMinTextSize = minTextSize;
RequestLayout();
Invalidate();
}
public float GetMinTextSize()
{
return mMinTextSize;
}
public void ResetTextSize()
{
if(mTextSize > 0)
{
base.SetTextSize(ComplexUnitType.Px, mTextSize);
mMaxTextSize = mTextSize;
}
}
protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
{
if(changed || mNeedsResize)
{
int widthLimit = (right - left) - CompoundPaddingLeft - CompoundPaddingRight;
int heightLimit = (bottom - top) - CompoundPaddingBottom - CompoundPaddingTop;
ResizeText(widthLimit, heightLimit);
}
base.OnLayout(changed, left, top, right, bottom);
base.OnLayout(changed, left, top, right, bottom);
}
public void ResizeText()
{
int heightLimit = Height - PaddingBottom - PaddingTop;
int widthLimit = Width - PaddingLeft - PaddingRight;
ResizeText(widthLimit, heightLimit);
}
public void ResizeText(int width, int height)
{
var text = TextFormatted;
if (text == null || text.Length() == 0 || height <= 0 || width <= 0 || mTextSize == 0)
return;
if (TransformationMethod != null)
text = TransformationMethod.GetTransformationFormatted(TextFormatted, this);
TextPaint textPaint = Paint;
float oldTextSize = textPaint.TextSize;
float targetTextSize = mMaxTextSize > 0 ? System.Math.Min(mTextSize, mMaxTextSize) : mTextSize;
int textHeight = GetTextHeight(text, textPaint, width, targetTextSize);
while(textHeight > height && targetTextSize > mMinTextSize)
{
targetTextSize = System.Math.Max(targetTextSize - 2, mMinTextSize);
textHeight = GetTextHeight(text, textPaint, width, targetTextSize);
}
if(AddEllipsis && targetTextSize == mMinTextSize && textHeight > height)
{
TextPaint paint = new TextPaint(textPaint);
StaticLayout layout = new StaticLayout(text, paint, width, Layout.Alignment.AlignNormal, mSpacingMult, mSpacingAdd, false);
if(layout.LineCount > 0)
{
int lastLine = layout.GetLineForVertical(height) - 1;
if (lastLine < 0)
SetText("", BufferType.Normal);
else
{
int start = layout.GetLineStart(lastLine);
int end = layout.GetLineEnd(lastLine);
float lineWidth = layout.GetLineWidth(lastLine);
float ellipseWidth = textPaint.MeasureText(mEllipsis);
while (width < lineWidth + ellipseWidth)
lineWidth = textPaint.MeasureText(text.SubSequence(start, --end + 1).ToString());
SetText(text.SubSequence(0, end) + mEllipsis, BufferType.Normal);
}
}
}
SetTextSize(ComplexUnitType.Px, targetTextSize);
SetLineSpacing(mSpacingAdd, mSpacingMult);
mTextResizeListener?.OnTextResize(this, oldTextSize, targetTextSize);
mNeedsResize = false;
}
private int GetTextHeight(ICharSequence source, TextPaint paint, int width, float textSize)
{
TextPaint paintCopy = new TextPaint(paint);
paintCopy.TextSize = textSize;
StaticLayout layout = new StaticLayout(source, paintCopy, width, Layout.Alignment.AlignNormal, mSpacingMult, mSpacingAdd, false);
return layout.Height;
}
}
}

C# Create a Rectangle class that holds width and height.

This is for C#
Create a Rectangle class that holds width and height. Provide a constructor that accepts width and height. The Rectangle class contains three methods, to calculate the perimeter, to calculate the area, and to check whether it is square or not respectively.
So far I got this done..
public double width;
public double height;
public Rectangle(double w, double h)
{
width = w;
height = h;
}
public double perimeter()
{
return 2 * (width + height);
}
public double area()
{
return width * height;
}
public boolean isSquare()
{
if (width == height)
{
return true;
}
else
{
return false;
}
}
}
}
I get an error for this line
}
public boolean isSquare()
{
What is the problem?
You should use bool instead of boolean. Try this code:
public bool isSquare()
{
if (width == height)
{
return true;
}
else
{
return false;
}
}
Hope it helps!

Categories