-
Notifications
You must be signed in to change notification settings - Fork 6
/
dialog.lua
164 lines (130 loc) · 3.34 KB
/
dialog.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
160
161
162
163
164
--
-- Dialog object that serves as base to implement other dialogs.
-- @copyright Jefferson Gonzalez
-- @license MIT
--
local core = require "core"
local style = require "core.style"
local Widget = require "libraries.widget"
local Button = require "libraries.widget.button"
local Label = require "libraries.widget.label"
---@class widget.dialog : widget
---@overload fun(title?:string):widget.dialog
---@field protected title widget.label
---@field protected close widget.button
---@field protected panel widget
local Dialog = Widget:extend()
---Constructor
---@param title string
function Dialog:new(title)
Dialog.super.new(self)
self.type_name = "widget.dialog"
self.draggable = true
self.scrollable = false
-- minimum width and height
self.size.mx = 400
self.size.my = 150
self.title = Label(self, "")
self.close = Button(self, "")
self.close:set_icon("X")
self.close.border.width = 0
self.close:toggle_background(false)
self.close.padding.x = 4
self.close.padding.y = 0
self.panel = Widget(self)
self.panel.border.width = 0
self.panel.scrollable = true
local this = self
function self.close:on_click()
this:on_close()
this:hide()
end
self:set_title(title or "")
end
---Returns the widget where you can add child widgets to this dialog.
---@return widget
function Dialog:get_panel()
return self.panel
end
---Change the dialog title.
---@param text string|widget.styledtext
function Dialog:set_title(text)
self.title:set_label(text)
end
---Calculate the dialog size, centers it relative to screen and shows it.
function Dialog:show()
Dialog.super.show(self)
self:update()
self:centered()
end
---Called when the user clicks the close button of the dialog.
function Dialog:on_close()
self:hide()
end
function Dialog:update()
if not Dialog.super.update(self) then return false end
local width = math.max(
self.title:get_width() + (style.padding.x * 3) + self.close:get_width(),
self.size.mx,
self.size.x
)
local height = math.max(
self.title:get_height() + (style.padding.y * 3),
self.size.my,
self.size.y
)
self:set_size(width, height)
self.title:set_position(
style.padding.x / 2,
style.padding.y / 2
)
self.close:set_position(
self.size.x - self.close.size.x - (style.padding.x / 2),
style.padding.y / 2
)
self.panel:set_position(
0,
self.title:get_bottom() + (style.padding.y / 2)
)
self.panel:set_size(
self.size.x,
self.size.y - self.title.size.y - style.padding.y
)
return true
end
---We overwrite default draw function to draw the title background.
function Dialog:draw()
if not self:is_visible() then return false end
Dialog.super.draw(self)
self:draw_border()
if self.background_color then
self:draw_background(self.background_color)
else
self:draw_background(
self.parent and style.background or style.background2
)
end
if #self.childs > 0 then
core.push_clip_rect(
self.position.x,
self.position.y,
self.size.x,
self.size.y
)
end
-- draw the title background
renderer.draw_rect(
self.position.x,
self.position.y,
self.size.x, self.title:get_height() + style.padding.y,
style.selection
)
for i=#self.childs, 1, -1 do
self.childs[i]:draw()
end
if #self.childs > 0 then
core.pop_clip_rect()
end
return true
end
return Dialog