-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponent.cs
103 lines (93 loc) · 2.54 KB
/
Component.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace Slip
{
public class Component
{
public Component parent = null;
public List<Component> children = new List<Component>();
public Vector2 position = Vector2.Zero;
public Vector2 alignment = Vector2.Zero;
public Vector2 size = Vector2.Zero;
public Vector2 border = Vector2.Zero;
public Vector2 scaleToParent = Vector2.Zero;
public delegate void MouseEvent(Main main);
public event MouseEvent OnClick;
public Vector2 Position
{
get
{
if (parent == null)
{
return position;
}
else
{
return parent.CanvasPosition + Helper.PointwiseMult(parent.CanvasSize - Size, alignment) + position;
}
}
}
public Vector2 Size
{
get
{
if (parent == null)
{
return size;
}
else
{
return Helper.PointwiseMult(parent.CanvasSize, scaleToParent) + size;
}
}
}
public Vector2 CanvasPosition
{
get
{
return Position + border;
}
}
public Vector2 CanvasSize
{
get
{
return Size - 2f * border;
}
}
public virtual void Initialize(Main main)
{
}
public virtual void Update(Main main)
{
if (Main.leftMouseClick && ContainsPoint(Main.mousePos) && OnClick != null)
{
OnClick(main);
}
foreach (Component child in children)
{
child.Update(main);
}
}
public virtual void Draw(Main main)
{
foreach (Component child in children)
{
child.Draw(main);
}
}
public void AddComponent(Component child)
{
child.parent = this;
children.Add(child);
child.Initialize(Main.instance);
}
public bool ContainsPoint(Point point)
{
Vector2 pos = Position;
Vector2 area = Size;
return point.X >= pos.X && point.X < pos.X + area.X && point.Y >= pos.Y && point.Y < pos.Y + area.Y;
}
}
}