uiminiwindowcontainer.lua

main 213 lines · 5.3 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1-- @docclass
2UIMiniWindowContainer = extends(UIWidget, "UIMiniWindowContainer")
3
4function UIMiniWindowContainer.create()
5 local container = UIMiniWindowContainer.internalCreate()
6 container.scheduledWidgets = {}
7 container:setFocusable(false)
8 container:setPhantom(true)
9 return container
10end
11
12-- TODO: connect to window onResize event
13-- TODO: try to resize another widget?
14-- TODO: try to find another panel?
15function UIMiniWindowContainer:fitAll(noRemoveChild)
16 if not self:isVisible() then
17 return
18 end
19
20 if not noRemoveChild then
21 local children = self:getChildren()
22 if #children > 0 then
23 noRemoveChild = children[#children]
24 else
25 return
26 end
27 end
28
29 local sumHeight = 0
30 local children = self:getChildren()
31 for i=1,#children do
32 if children[i]:isVisible() then
33 sumHeight = sumHeight + children[i]:getHeight()
34 end
35 end
36
37 local selfHeight = self:getHeight() - (self:getPaddingTop() + self:getPaddingBottom())
38 if sumHeight <= selfHeight then
39 return
40 end
41
42 local removeChildren = {}
43
44 -- try to resize noRemoveChild
45 local maximumHeight = selfHeight - (sumHeight - noRemoveChild:getHeight())
46 if noRemoveChild:isResizeable() and noRemoveChild:getMinimumHeight() <= maximumHeight then
47 sumHeight = sumHeight - noRemoveChild:getHeight() + maximumHeight
48 addEvent(function() noRemoveChild:setHeight(maximumHeight) end)
49 end
50
51 -- try to remove no-save widget
52 for i=#children,1,-1 do
53 if sumHeight <= selfHeight then
54 break
55 end
56
57 local child = children[i]
58 if child ~= noRemoveChild and not child.save then
59 local childHeight = child:getHeight()
60 sumHeight = sumHeight - childHeight
61 table.insert(removeChildren, child)
62 end
63 end
64
65 -- try to remove save widget, not forceOpen
66 for i=#children,1,-1 do
67 if sumHeight <= selfHeight then
68 break
69 end
70
71 local child = children[i]
72 if child ~= noRemoveChild and child:isVisible() and not child.forceOpen then
73 local childHeight = child:getHeight()
74 sumHeight = sumHeight - childHeight
75 table.insert(removeChildren, child)
76 end
77 end
78
79 -- try to remove save widget
80 for i=#children,1,-1 do
81 if sumHeight <= selfHeight then
82 break
83 end
84
85 local child = children[i]
86 if child ~= noRemoveChild and child:isVisible() then
87 local childHeight = child:getHeight() - 50
88 sumHeight = sumHeight - childHeight
89 table.insert(removeChildren, child)
90 end
91 end
92
93 -- close widgets
94 for i=1,#removeChildren do
95 if removeChildren[i].forceOpen then
96 removeChildren[i]:minimize(true)
97 else
98 removeChildren[i]:close()
99 end
100 end
101end
102
103function UIMiniWindowContainer:onDrop(widget, mousePos)
104 if widget.UIMiniWindowContainer then
105 local oldParent = widget:getParent()
106 if oldParent == self then
107 return true
108 end
109
110 if oldParent then
111 oldParent:removeChild(widget)
112 end
113
114 if widget.movedWidget then
115 local index = self:getChildIndex(widget.movedWidget)
116 self:insertChild(index + widget.movedIndex, widget)
117 else
118 self:addChild(widget)
119 end
120
121 self:fitAll(widget)
122 return true
123 end
124end
125
126function UIMiniWindowContainer:moveTo(newPanel)
127 if not newPanel or newPanel == self then
128 return
129 end
130 local children = self:getChildByIndex(1)
131 while children do
132 newPanel:addChild(children)
133 children = self:getChildByIndex(1)
134 end
135 newPanel:fitAll()
136end
137
138function UIMiniWindowContainer:swapInsert(widget, index)
139 local oldParent = widget:getParent()
140 local oldIndex = self:getChildIndex(widget)
141
142 if oldParent == self and oldIndex ~= index then
143 local oldWidget = self:getChildByIndex(index)
144 if oldWidget then
145 self:removeChild(oldWidget)
146 self:insertChild(oldIndex, oldWidget)
147 end
148 self:removeChild(widget)
149 self:insertChild(index, widget)
150 end
151end
152
153function UIMiniWindowContainer:scheduleInsert(widget, index)
154 if index - 1 > self:getChildCount() then
155 if self.scheduledWidgets[index] then
156 pdebug('replacing scheduled widget id ' .. widget:getId())
157 end
158 self.scheduledWidgets[index] = widget
159 else
160 local oldParent = widget:getParent()
161 if oldParent ~= self then
162 if oldParent then
163 oldParent:removeChild(widget)
164 end
165 self:insertChild(index, widget)
166
167 while true do
168 local placed = false
169 for nIndex,nWidget in pairs(self.scheduledWidgets) do
170 if nIndex - 1 <= self:getChildCount() then
171 self:insertChild(nIndex, nWidget)
172 self.scheduledWidgets[nIndex] = nil
173 placed = true
174 break
175 end
176 end
177 if not placed then break end
178 end
179
180 end
181 end
182end
183
184function UIMiniWindowContainer:order()
185 local children = self:getChildren()
186 for i=1,#children do
187 if not children[i].miniLoaded then return end
188 end
189
190 for i=1,#children do
191 if children[i].miniIndex then
192 self:swapInsert(children[i], children[i].miniIndex)
193 elseif children[i].autoOpen then
194 self:swapInsert(children[i], children[i].autoOpen)
195 end
196 end
197end
198
199function UIMiniWindowContainer:saveChildren()
200 local children = self:getChildren()
201 local ignoreIndex = 0
202 for i=1,#children do
203 if children[i].save then
204 children[i]:saveParentIndex(self:getId(), i - ignoreIndex)
205 else
206 ignoreIndex = ignoreIndex + 1
207 end
208 end
209end
210
211function UIMiniWindowContainer:onGeometryChange()
212 self:fitAll()
213end
Top