-
Notifications
You must be signed in to change notification settings - Fork 25
/
Custom Widgets.lua
159 lines (133 loc) · 4.9 KB
/
Custom Widgets.lua
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
----------------------------------------------------------------------
-- This example shows how to create custom widgets using the canvas.
---------------------------------------------------------------------
local dlg = Dialog("Custom Widgets")
local mouse = {position = Point(0, 0), leftClick = false}
local focusedWidget = nil
local customButton = {
bounds = Rectangle(5, 5, 80, 40),
state = {
normal = {part = "button_normal", color = "button_normal_text"},
hot = {part = "button_hot", color = "button_hot_text"},
selected = {part = "button_selected", color = "button_selected_text"},
focused = {part = "button_focused", color = "button_normal_text"}
},
text = "Custom Button",
onclick = function() print("Clicked <Custom Button>") end
}
local doubleButtonLeft = {
bounds = Rectangle(5, 50, 60, 20),
state = {
normal = {
part = "drop_down_button_left_normal",
color = "button_normal_text"
},
hot = {part = "drop_down_button_left_hot", color = "button_hot_text"},
selected = {
part = "drop_down_button_left_selected",
color = "button_selected_text"
},
focused = {
part = "drop_down_button_left_focused",
color = "button_normal_text"
}
},
text = "Search",
onclick = function() print("Clicked <Search Button Left>") end
}
local doubleButtonRight = {
bounds = Rectangle(65, 50, 20, 20),
state = {
normal = {
part = "drop_down_button_right_normal",
color = "button_normal_text"
},
hot = {part = "drop_down_button_right_hot", color = "button_hot_text"},
selected = {
part = "drop_down_button_right_selected",
color = "button_selected_text"
},
focused = {
part = "drop_down_button_right_focused",
color = "button_normal_text"
}
},
icon = "tool_zoom",
onclick = function() print("Clicked <Search Button Right>") end
}
local sunkenMagicWand = {
bounds = Rectangle(90, 5, 65, 65),
state = {
normal = {part = "sunken_normal", color = "button_normal_text"},
selected = {part = "sunken_focused", color = "button_normal_text"},
focused = {part = "sunken_focused", color = "button_normal_text"}
},
icon = "tool_magic_wand",
onclick = function() print("Clicked <Sunken Magic Wand>") end
}
local customWidgets = {
customButton, doubleButtonLeft, doubleButtonRight, sunkenMagicWand
}
dlg:canvas{
id = "canvas",
width = 160,
height = 75,
onpaint = function(ev)
local ctx = ev.context
-- Draw each custom widget
for _, widget in ipairs(customWidgets) do
local state = widget.state.normal
if widget == focusedWidget then
state = widget.state.focused
end
local isMouseOver = widget.bounds:contains(mouse.position)
if isMouseOver then
state = widget.state.hot or state
if mouse.leftClick then
state = widget.state.selected
end
end
ctx:drawThemeRect(state.part, widget.bounds)
local center = Point(widget.bounds.x + widget.bounds.width / 2,
widget.bounds.y + widget.bounds.height / 2)
if widget.icon then
-- Assuming default icon size of 16x16 pixels
local size = Rectangle(0, 0, 16, 16)
ctx:drawThemeImage(widget.icon, center.x - size.width / 2,
center.y - size.height / 2)
elseif widget.text then
local size = ctx:measureText(widget.text)
ctx.color = app.theme.color[state.color]
ctx:fillText(widget.text, center.x - size.width / 2,
center.y - size.height / 2)
end
end
end,
onmousemove = function(ev)
-- Update the mouse position
mouse.position = Point(ev.x, ev.y)
dlg:repaint()
end,
onmousedown = function(ev)
-- Update information about left mouse button being pressed
mouse.leftClick = ev.button == MouseButton.LEFT
dlg:repaint()
end,
onmouseup = function(ev)
-- When releasing left mouse button over a widget, call `onclick` method
if mouse.leftClick then
for _, widget in ipairs(customWidgets) do
local isMouseOver = widget.bounds:contains(mouse.position)
if isMouseOver then
widget.onclick()
-- Last clicked widget has focus on it
focusedWidget = widget
end
end
end
-- Update information about left mouse button being released
mouse.leftClick = false
dlg:repaint()
end
}
dlg:show{wait = false}