uiminiwindow.lua

main 445 lines · 11.5 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1-- @docclass
2UIMiniWindow = extends(UIWindow, "UIMiniWindow")
3
4function UIMiniWindow.create()
5 local miniwindow = UIMiniWindow.internalCreate()
6 miniwindow.UIMiniWindowContainer = true
7 return miniwindow
8end
9
10function UIMiniWindow:open(dontSave)
11 self:setVisible(true)
12
13 if not dontSave then
14 self:setSettings({closed = false})
15 end
16
17 signalcall(self.onOpen, self)
18end
19
20function UIMiniWindow:close(dontSave)
21 if not self:isExplicitlyVisible() then return end
22 if self.forceOpen then return end
23 self:setVisible(false)
24
25 if not dontSave then
26 self:setSettings({closed = true})
27 end
28
29 signalcall(self.onClose, self)
30end
31
32function UIMiniWindow:minimize(dontSave)
33 self:setOn(true)
34 self:getChildById('contentsPanel'):hide()
35 self:getChildById('miniwindowScrollBar'):hide()
36 self:getChildById('bottomResizeBorder'):hide()
37 if self.minimizeButton then
38 self.minimizeButton:setOn(true)
39 end
40 self.maximizedHeight = self:getHeight()
41 self:setHeight(self.minimizedHeight)
42
43 if not dontSave then
44 self:setSettings({minimized = true})
45 end
46
47 signalcall(self.onMinimize, self)
48end
49
50function UIMiniWindow:maximize(dontSave)
51 self:setOn(false)
52 self:getChildById('contentsPanel'):show()
53 self:getChildById('miniwindowScrollBar'):show()
54 self:getChildById('bottomResizeBorder'):show()
55 if self.minimizeButton then
56 self.minimizeButton:setOn(false)
57 end
58 self:setHeight(self:getSettings('height') or self.maximizedHeight)
59
60 if not dontSave then
61 self:setSettings({minimized = false})
62 end
63
64 local parent = self:getParent()
65 if parent and parent:getClassName() == 'UIMiniWindowContainer' then
66 parent:fitAll(self)
67 end
68
69 signalcall(self.onMaximize, self)
70end
71
72function UIMiniWindow:lock(dontSave)
73 local lockButton = self:getChildById('lockButton')
74 if lockButton then
75 lockButton:setOn(true)
76 end
77 self:setDraggable(false)
78 if not dontsave then
79 self:setSettings({locked = true})
80 end
81
82 signalcall(self.onLockChange, self)
83end
84
85function UIMiniWindow:unlock(dontSave)
86 local lockButton = self:getChildById('lockButton')
87 if lockButton then
88 lockButton:setOn(false)
89 end
90 self:setDraggable(true)
91 if not dontsave then
92 self:setSettings({locked = false})
93 end
94 signalcall(self.onLockChange, self)
95end
96
97function UIMiniWindow:setup()
98 self:getChildById('closeButton').onClick =
99 function()
100 self:close()
101 end
102 if self.forceOpen then
103 if self.closeButton then
104 self.closeButton:hide()
105 end
106 end
107
108 if(self.minimizeButton) then
109 self.minimizeButton.onClick =
110 function()
111 if self:isOn() then
112 self:maximize()
113 else
114 self:minimize()
115 end
116 end
117 end
118
119 local lockButton = self:getChildById('lockButton')
120 if lockButton then
121 lockButton.onClick =
122 function ()
123 if self:isDraggable() then
124 self:lock()
125 else
126 self:unlock()
127 end
128 end
129 end
130
131 self:getChildById('miniwindowTopBar').onDoubleClick =
132 function()
133 if self:isOn() then
134 self:maximize()
135 else
136 self:minimize()
137 end
138 end
139
140 local oldParent = self:getParent()
141
142 local settings = g_settings.getNode('MiniWindows')
143 if settings then
144 local selfSettings = settings[self:getId()]
145 if selfSettings then
146 if selfSettings.parentId then
147 local parent = rootWidget:recursiveGetChildById(selfSettings.parentId)
148 if parent then
149 if parent:getClassName() == 'UIMiniWindowContainer' and selfSettings.index and parent:isOn() then
150 self.miniIndex = selfSettings.index
151 parent:scheduleInsert(self, selfSettings.index)
152 elseif selfSettings.position then
153 self:setParent(parent, true)
154 self:setPosition(topoint(selfSettings.position))
155 end
156 end
157 end
158
159 if selfSettings.minimized then
160 self:minimize(true)
161 else
162 if selfSettings.height and self:isResizeable() then
163 self:setHeight(selfSettings.height)
164 elseif selfSettings.height and not self:isResizeable() then
165 self:eraseSettings({height = true})
166 end
167 end
168
169 if selfSettings.closed and not self.forceOpen then
170 self:close(true)
171 end
172
173 if selfSettings.locked then
174 self:lock(true)
175 end
176 else
177 if not self.forceOpen and self.autoOpen ~= nil and (self.autoOpen == 0 or self.autoOpen == false) then
178 self:close(true)
179 end
180 end
181 end
182
183 local newParent = self:getParent()
184
185 self.miniLoaded = true
186
187 if self.save then
188 if oldParent and oldParent:getClassName() == 'UIMiniWindowContainer' then
189 addEvent(function() oldParent:order() end)
190 end
191 if newParent and newParent:getClassName() == 'UIMiniWindowContainer' and newParent ~= oldParent then
192 addEvent(function() newParent:order() end)
193 end
194 end
195
196 self:fitOnParent()
197end
198
199function UIMiniWindow:onVisibilityChange(visible)
200 self:fitOnParent()
201end
202
203function UIMiniWindow:onDragEnter(mousePos)
204 local parent = self:getParent()
205 if not parent then return false end
206
207 if parent:getClassName() == 'UIMiniWindowContainer' then
208 local containerParent = parent:getParent():getParent()
209 parent:removeChild(self)
210 containerParent:addChild(self)
211 parent:saveChildren()
212 end
213
214 local oldPos = self:getPosition()
215 self.movingReference = { x = mousePos.x - oldPos.x, y = mousePos.y - oldPos.y }
216 self:setPosition(oldPos)
217 self.free = true
218 return true
219end
220
221function UIMiniWindow:onDragLeave(droppedWidget, mousePos)
222 if self.movedWidget then
223 self.setMovedChildMargin(self.movedOldMargin or 0)
224 self.movedWidget = nil
225 self.setMovedChildMargin = nil
226 self.movedOldMargin = nil
227 self.movedIndex = nil
228 end
229
230 UIWindow:onDragLeave(self, droppedWidget, mousePos)
231 self:saveParent(self:getParent())
232end
233
234function UIMiniWindow:onDragMove(mousePos, mouseMoved)
235 local oldMousePosY = mousePos.y - mouseMoved.y
236 local children = rootWidget:recursiveGetChildrenByMarginPos(mousePos)
237 local overAnyWidget = false
238 for i=1,#children do
239 local child = children[i]
240 if child:getParent():getClassName() == 'UIMiniWindowContainer' then
241 overAnyWidget = true
242
243 local childCenterY = child:getY() + child:getHeight() / 2
244 if child == self.movedWidget and mousePos.y < childCenterY and oldMousePosY < childCenterY then
245 break
246 end
247
248 if self.movedWidget then
249 self.setMovedChildMargin(self.movedOldMargin or 0)
250 self.setMovedChildMargin = nil
251 end
252
253 if mousePos.y < childCenterY then
254 self.movedOldMargin = child:getMarginTop()
255 self.setMovedChildMargin = function(v) child:setMarginTop(v) end
256 self.movedIndex = 0
257 else
258 self.movedOldMargin = child:getMarginBottom()
259 self.setMovedChildMargin = function(v) child:setMarginBottom(v) end
260 self.movedIndex = 1
261 end
262
263 self.movedWidget = child
264 self.setMovedChildMargin(self:getHeight())
265 break
266 end
267 end
268
269 if not overAnyWidget and self.movedWidget then
270 self.setMovedChildMargin(self.movedOldMargin or 0)
271 self.movedWidget = nil
272 end
273
274 return UIWindow.onDragMove(self, mousePos, mouseMoved)
275end
276
277function UIMiniWindow:onMousePress()
278 local parent = self:getParent()
279 if not parent then return false end
280 if parent:getClassName() ~= 'UIMiniWindowContainer' then
281 self:raise()
282 return true
283 end
284end
285
286function UIMiniWindow:onFocusChange(focused)
287 if not focused then return end
288 local parent = self:getParent()
289 if parent and parent:getClassName() ~= 'UIMiniWindowContainer' then
290 self:raise()
291 end
292end
293
294function UIMiniWindow:onHeightChange(height)
295 if not self:isOn() then
296 self:setSettings({height = height})
297 end
298 self:fitOnParent()
299end
300
301function UIMiniWindow:getSettings(name)
302 if not self.save then return nil end
303 local settings = g_settings.getNode('MiniWindows')
304 if settings then
305 local selfSettings = settings[self:getId()]
306 if selfSettings then
307 return selfSettings[name]
308 end
309 end
310 return nil
311end
312
313function UIMiniWindow:setSettings(data)
314 if not self.save then return end
315
316 local settings = g_settings.getNode('MiniWindows')
317 if not settings then
318 settings = {}
319 end
320
321 local id = self:getId()
322 if not settings[id] then
323 settings[id] = {}
324 end
325
326 for key,value in pairs(data) do
327 settings[id][key] = value
328 end
329
330 g_settings.setNode('MiniWindows', settings)
331end
332
333function UIMiniWindow:eraseSettings(data)
334 if not self.save then return end
335
336 local settings = g_settings.getNode('MiniWindows')
337 if not settings then
338 settings = {}
339 end
340
341 local id = self:getId()
342 if not settings[id] then
343 settings[id] = {}
344 end
345
346 for key,value in pairs(data) do
347 settings[id][key] = nil
348 end
349
350 g_settings.setNode('MiniWindows', settings)
351end
352
353function UIMiniWindow:saveParent(parent)
354 local parent = self:getParent()
355 if parent then
356 if parent:getClassName() == 'UIMiniWindowContainer' then
357 parent:saveChildren()
358 else
359 self:saveParentPosition(parent:getId(), self:getPosition())
360 end
361 end
362end
363
364function UIMiniWindow:saveParentPosition(parentId, position)
365 local selfSettings = {}
366 selfSettings.parentId = parentId
367 selfSettings.position = pointtostring(position)
368 self:setSettings(selfSettings)
369end
370
371function UIMiniWindow:saveParentIndex(parentId, index)
372 local selfSettings = {}
373 selfSettings.parentId = parentId
374 selfSettings.index = index
375 self:setSettings(selfSettings)
376 self.miniIndex = index
377end
378
379function UIMiniWindow:disableResize()
380 self:getChildById('bottomResizeBorder'):disable()
381end
382
383function UIMiniWindow:enableResize()
384 self:getChildById('bottomResizeBorder'):enable()
385end
386
387function UIMiniWindow:fitOnParent()
388 local parent = self:getParent()
389 if self:isVisible() and parent and parent:getClassName() == 'UIMiniWindowContainer' then
390 parent:fitAll(self)
391 end
392end
393
394function UIMiniWindow:setParent(parent, dontsave)
395 UIWidget.setParent(self, parent)
396 if not dontsave then
397 self:saveParent(parent)
398 end
399 self:fitOnParent()
400end
401
402function UIMiniWindow:setHeight(height)
403 UIWidget.setHeight(self, height)
404 signalcall(self.onHeightChange, self, height)
405end
406
407function UIMiniWindow:setContentHeight(height)
408 local contentsPanel = self:getChildById('contentsPanel')
409 local minHeight = contentsPanel:getMarginTop() + contentsPanel:getMarginBottom() + contentsPanel:getPaddingTop() + contentsPanel:getPaddingBottom()
410
411 local resizeBorder = self:getChildById('bottomResizeBorder')
412 resizeBorder:setParentSize(minHeight + height)
413end
414
415function UIMiniWindow:setContentMinimumHeight(height)
416 local contentsPanel = self:getChildById('contentsPanel')
417 local minHeight = contentsPanel:getMarginTop() + contentsPanel:getMarginBottom() + contentsPanel:getPaddingTop() + contentsPanel:getPaddingBottom()
418
419 local resizeBorder = self:getChildById('bottomResizeBorder')
420 resizeBorder:setMinimum(minHeight + height)
421end
422
423function UIMiniWindow:setContentMaximumHeight(height)
424 local contentsPanel = self:getChildById('contentsPanel')
425 local minHeight = contentsPanel:getMarginTop() + contentsPanel:getMarginBottom() + contentsPanel:getPaddingTop() + contentsPanel:getPaddingBottom()
426
427 local resizeBorder = self:getChildById('bottomResizeBorder')
428 resizeBorder:setMaximum(minHeight + height)
429end
430
431function UIMiniWindow:getMinimumHeight()
432 local resizeBorder = self:getChildById('bottomResizeBorder')
433 return resizeBorder:getMinimum()
434end
435
436function UIMiniWindow:getMaximumHeight()
437 local resizeBorder = self:getChildById('bottomResizeBorder')
438 return resizeBorder:getMaximum()
439end
440
441function UIMiniWindow:isResizeable()
442 local resizeBorder = self:getChildById('bottomResizeBorder')
443 return resizeBorder:isExplicitlyVisible() and resizeBorder:isEnabled()
444end
445
Top