-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.cs
75 lines (65 loc) · 2.39 KB
/
View.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;
namespace Conways_Game_of_Life
{
/// <summary>
/// Class for the view of the game which is moveable and has zoom functions.
/// </summary>
public class View
{
public Matrix InverseMatrix { get; private set; }
public float Zoom { get; private set; } = 1;
public Matrix Matrix { get; private set; }
public int posX { get; set; }
public int posY { get; set; }
public float PositionX { get; set; }
public float PositionY { get; set; }
private MouseState oldMouse;
/// <summary>
/// Main update loop for the View class.
/// </summary>
/// <param name="time"></param>
public void Update(GameTime time)
{
var mouse = Mouse.GetState();
// Gets the value of which direction the mouse is scrolling
int scrollValue = mouse.ScrollWheelValue - oldMouse.ScrollWheelValue;
// Checks the scroll value and either zooms in or zooms out depending on the input.
if (scrollValue < 0)
{
Zoom /= 2;
if (Zoom < 0.2f)
{
Zoom = 0.2f;
}
}
if (scrollValue > 0)
{
Zoom *= 2;
if (Zoom > 16)
{
Zoom = 16;
}
}
if (mouse.MiddleButton == ButtonState.Pressed)
{
// Finds the difference in position from the old mouse location and the new
posX = mouse.Position.X - oldMouse.Position.X;
posY = mouse.Position.Y - oldMouse.Position.Y;
// Moves the Matrix position X,Y values
PositionX += posX * 60 / (Zoom * 0.5f) * (float)time.ElapsedGameTime.TotalSeconds;
PositionY += posY * 60 / (Zoom * 0.5f) * (float)time.ElapsedGameTime.TotalSeconds;
}
// Makes the Matrix movement with a zoom scale
Matrix = Matrix.CreateTranslation(PositionX, PositionY, 0) * Matrix.CreateScale(Zoom);
InverseMatrix = Matrix.Invert(Matrix);
oldMouse = mouse;
}
}
}