-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTitleBar.vb
205 lines (184 loc) · 9.12 KB
/
TitleBar.vb
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
Imports System.Threading.Tasks
Imports GeonBit.UI
Imports GeonBit.UI.Entities
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Namespace Extensions
'TODO : Use a frame extender for the windows.forms.form frame instead, this is too dependent on game fps and available cpu time
''' <summary>
''' <para>Custom TitleBar.</para>
''' <para>Allows for moving the window, closing, and minimizing.</para>
''' </summary>
Public Class TitleBar
Inherits DrawableGameComponent
''' <summary>Retract if False, otherwise Extract if True.</summary>
Private ExtractingOrRetracting As Boolean
Private TriggerZone As Panel
Public _TitleBar As Panel
''' <summary>Uses its IsVisible to determine if underlying controls in other parts of the program should halt.</summary>
Public _Exit As Panel
Private TitleBar_MovementStepExtract As Single
Private TitleBar_MovementStepRetract As Single
Private TitleBar_MovementDone As Boolean = True
Private Backwards As Single
Private Size As Vector2
Private MousePosition_Last As Point
Private MousePosition_Start As Point
Private IsMoving As Boolean
Public Property Button_Menu As Button
Public Property Button_Close As Button
Public Property Button_Minimize As Button
Public Sub New(Game As Game)
MyBase.New(Game)
Dim GameBounds = Game.GraphicsDevice.Viewport.Bounds.Size.ToVector2
Size = New Vector2(GameBounds.X, GameBounds.Y * 0.045F)
TitleBar_MovementStepExtract = ((Size.Y / YOURGAME.GetService(Of Settings.Settings).Defaults.Game.UserFPS) * 1.0F)
TitleBar_MovementStepRetract = ((Size.Y / YOURGAME.GetService(Of Settings.Settings).Defaults.Game.UserFPS) * 0.4F)
_TitleBar = New Panel(Size, anchor:=Anchor.TopLeft, offset:=New Vector2(0, -Size.Y))
With _TitleBar
.FillColor = YOURGAME.GetService(Of Globals).GiveItem(Of Brush)("CA09").Color
.OnMouseDown = Sub() TitleBar_OnMouseDown()
.OnMouseReleased = Sub() TitleBar_OnMouseReleased()
.OnMouseLeave = Sub() TriggerZone_OnMouseLeave()
End With
TriggerZone = New Panel(Size, anchor:=Anchor.TopLeft)
With TriggerZone
.FillColor = Nothing
.OnMouseEnter = Sub() TriggerZone_OnMouseEnter()
.OnMouseLeave = Sub() TriggerZone_OnMouseLeave()
.WhileMouseHover = Sub() TriggerZone_WhileMouseHover()
End With
_Exit = New Panel(GameBounds, anchor:=Anchor.TopLeft)
With _Exit
.FillColor = YOURGAME.GetService(Of Globals).GiveItem(Of Brush)("CA03").Color
.AddChild(New Label(YOURGAME.GetService(Of Globals).Interpreter.GiveString("GA16"), Anchor.Center, offset:=New Vector2(0, (-GameBounds.Y * 0.1F))))
Dim OkCancel_Size = (GameBounds * 0.1F)
Dim OkCancel_Offset = ((OkCancel_Size.X / 2) + (OkCancel_Size.X * 0.1F))
.AddChild(New Button(YOURGAME.GetService(Of Globals).Interpreter.GiveString("GA17"), Anchor.Center, OkCancel_Size, New Vector2(-OkCancel_Offset, 0)) With {.OnClick = Sub() Button_Ok_OnClick()})
.AddChild(New Button(YOURGAME.GetService(Of Globals).Interpreter.GiveString("GA15"), Anchor.Center, OkCancel_Size, New Vector2(OkCancel_Offset, 0)) With {.OnClick = Sub() Button_Cancel_OnClick()})
.Visible = False
End With
Dim Button_Size = New Vector2((Size.X * 0.03F), Size.Y)
Button_Menu = New Button(Anchor:=Anchor.TopLeft, Size:=Button_Size)
With Button_Menu
.AddChild(New Image(YOURGAME.Instance.Content.Load(Of Texture2D)(YOURGAME.GUIRoot & YOURGAME.Instance.Theme & "/textures/icons/" & "Logo - 45x45"), Button_Size, anchor:=Anchor.TopLeft) With {.ClickThrough = True})
.OnClick = Sub() Button_Menu_OnClick()
End With
_TitleBar.AddChild(Button_Menu)
Button_Close = New Button("Ⓧ", Anchor.TopRight, Button_Size)
With Button_Close
.ButtonParagraph.AlignToCenter = True
.OnClick = Sub() Button_Close_OnClick()
End With
_TitleBar.AddChild(Button_Close)
Button_Minimize = New Button("▬", Anchor.TopRight, Button_Size, New Vector2(+Button_Size.X, 0))
With Button_Minimize
.OnClick = Sub() Button_Minimize_OnClick()
End With
_TitleBar.AddChild(Button_Minimize)
UserInterface.Active.AddEntity(TriggerZone)
UserInterface.Active.AddEntity(_TitleBar)
UserInterface.Active.AddEntity(_Exit)
End Sub
Private Sub Button_Menu_OnClick()
YOURGAME.GetComponent(Of GUI).Options.Open()
End Sub
Private Sub Button_Close_OnClick()
_Exit.Visible = True
End Sub
Private Sub Button_Ok_OnClick()
YOURGAME.Instance.Exit()
End Sub
Private Sub Button_Cancel_OnClick()
_Exit.Visible = False
End Sub
Private Sub Button_Minimize_OnClick()
YOURGAME.Minimize()
End Sub
Public Overrides Sub Update(GameTime As GameTime)
If (Not TitleBar_MovementDone) Then
If ExtractingOrRetracting Then
Extracting()
Else
Retracting()
End If
End If
If IsMoving Then
If (Input.Mouse.GetState().LeftButton = Input.ButtonState.Pressed) Then
Window_Move()
_TitleBar.SetOffset(Vector2.Zero)
TitleBar_MovementDone = True
Else
IsMoving = False
End If
End If
End Sub
Private Sub Extracting()
_TitleBar.SetOffset(New Vector2(0, (_TitleBar.GetRelativeOffset.Y + TitleBar_MovementStepExtract)))
If (_TitleBar.GetRelativeOffset.Y >= Size.Y) Then
_TitleBar.SetOffset(New Vector2(0, Size.Y))
TitleBar_MovementDone = True
End If
End Sub
Private Sub Retracting()
'Workaround because adding negative values doesn't work as intended on Vector2
Backwards -= TitleBar_MovementStepRetract
_TitleBar.SetOffset(New Vector2(0, (_TitleBar.GetRelativeOffset.Y + Backwards)))
If (_TitleBar.GetRelativeOffset.Y <= -Size.Y) Then
_TitleBar.SetOffset(New Vector2(0, -Size.Y))
TitleBar_MovementDone = True
Backwards = 0
End If
End Sub
Private Sub TriggerZone_WhileMouseHover()
If (_TitleBar.GetRelativeOffset.Y <> Size.Y) Then
TriggerZone_OnMouseEnter()
End If
End Sub
Private Sub TriggerZone_OnMouseEnter()
TitleBar_MovementDone = False
ExtractingOrRetracting = True
End Sub
Private Async Function DelayTask(Time As Double) As Task
Await Task.Delay(System.TimeSpan.FromSeconds(Time))
End Function
Private Async Sub TriggerZone_OnMouseLeave()
'Allow a margin of time to pass before trying to retract
Await DelayTask(1.0)
'If cursor has returned, don't retract
If (Not _TitleBar.IsMouseOver) Then
TitleBar_MovementDone = False
ExtractingOrRetracting = False
End If
End Sub
Private Sub TitleBar_OnMouseDown()
MousePosition_Last = New Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y)
MousePosition_Start = Input.Mouse.GetState.Position
IsMoving = True
End Sub
Private Sub TitleBar_OnMouseReleased()
IsMoving = False
End Sub
''' <summary>
''' Mouse movement is monitored on the OS level, to minimize the influence of the update rate on window movement.
''' </summary>
Private Sub Window_Move()
Dim CurrentMousePosition = New Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y)
If (MousePosition_Last <> CurrentMousePosition) Then
Dim RelativeMousePosition = Input.Mouse.GetState.Position
Dim Movement As New Point
If (RelativeMousePosition.X < MousePosition_Start.X) Then
Movement.X = (RelativeMousePosition.X - MousePosition_Start.X)
Else
Movement.X = System.Math.Abs(MousePosition_Start.X - RelativeMousePosition.X)
End If
If (RelativeMousePosition.Y < MousePosition_Start.Y) Then
Movement.Y = (RelativeMousePosition.Y - MousePosition_Start.Y)
Else
Movement.Y = System.Math.Abs(MousePosition_Start.Y - RelativeMousePosition.Y)
End If
YOURGAME.Instance.Window.Position += Movement
End If
End Sub
End Class
End Namespace