-
Notifications
You must be signed in to change notification settings - Fork 2
/
guiwidgetlist.monkey
144 lines (118 loc) · 3.77 KB
/
guiwidgetlist.monkey
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
Import horizon.utilimage
Import horizon.guiwidgetimagebutton
Import horizon.guiwidget
Import horizon.guiscrollbarvertical
Class GuiWidgetList Extends GuiWidget
Const PADDING:Int = 10
Field entryHeight:Int
Field hovered:Object
Field selected:Object
Field entries:List<Object>
Field offsetY:Int
Field font:BitmapFont
Field scrollbar:GuiScrollbarVertical
Method New()
selected = Null
hovered = Null
rect.w = 500
rect.h = 340
rect.x = 100
rect.y = 100
entries = New List<Object>
entryHeight = TextHeight() + PADDING
scrollbar = New GuiScrollbarVertical
AddChild(scrollbar)
End Method
Method UpdateScrollbar()
scrollbar.rect.x = rect.x + rect.w - scrollbar.rect.w
scrollbar.rect.y = rect.y
scrollbar.SetWidgetHeight(rect.h)
scrollbar.minValue = 0
scrollbar.currValue = scrollbar.minValue
scrollbar.maxValue = Max(0, Int(GetListHeight() - GetDisplayHeight())) / GetDisplayHeight()
scrollbar.UpdateScrollbarButtons()
If (GetDisplayHeight() >= GetListHeight())
scrollbar.Hide()
Else
scrollbar.Show()
End If
End Method
Method AddEntry(s:Object)
entries.AddLast(s)
End Method
Method ClearEntries()
entries.Clear()
End Method
Method Update()
offsetY = GetDisplayHeight() * scrollbar.GetValue()
If (InputControllerMouse.GetInstance().GetMouseWheel() <> 0 And widgetState = HOVER)
scrollbar.MoveBar(-InputControllerMouse.GetInstance().GetMouseWheel() * scrollbar.GetBarHeight() / 150)
OnMouseMove(0,0)
End If
End Method
Method Render()
UpdateScrollbar()
Local y:Float = rect.y - offsetY + PADDING / 2
Local w:Float = rect.w
If (scrollbar.visible) Then w -= scrollbar.rect.w
'' SetViewport(rect.x, rect.y, w, rect.h)
SetColor(0, 0, 0)
SetAlpha(0.4)
DrawRect(rect.x, rect.y, w, rect.h)
SetAlpha(1)
SetColor(255, 255, 255)
Local bx:Int, by:Int, bw:Int, bh:Int
entryHeight = TextHeight() + PADDING
Local key:Int = 0
For Local obj:Object = EachIn entries
Local value:String = "String(obj)"
bx = rect.x
by = y - PADDING / 2
bw = w
bh = entryHeight
If (selected = obj)
SetAlpha(0.5)
DrawRect bx, by, bw, bh
Else If (hovered = obj)
SetAlpha(0.25)
DrawRect bx, by, bw, bh
End If
SetAlpha(1)
font.DrawText(value, bx + PADDING, by + PADDING / 2)
y += entryHeight
key += 1
Next
'' SetViewport(0, 0, VirtualResolutionWidth(), VirtualResolutionHeight())
End Method
Method GetDisplayHeight:Float()
Return rect.h
End Method
Method GetListHeight:Float()
Return entries.Count() * entryHeight
End Method
Method OnMouseOver()
Super.OnMouseOver()
OnMouseMove(0, 0)
End Method
Method OnMouseMove(dx:Int, dy:Int)
Super.OnMouseMove(dx, dy)
Local hoveredId:Int = (InputControllerMouse.GetInstance().GetY() + offsetY - rect.y) / entryHeight
If (entries.Count() > hoveredId And hoveredId >= 0)
Local c% = 0
For Local e:Object = Eachin entries
c+=1
If (c = hoveredId) Then hovered = e
Next
Else
hovered = Null
End If
End Method
Method OnMouseClick()
Super.OnMouseClick()
If hovered Then selected = hovered
End Method
Method OnMouseOut()
Super.OnMouseOut()
hovered = Null
End Method
End