Merge branch 'fixes' into main/gingo-test
[ryzomcore.git] / ryzom / common / data_common / r2 / r2_logic_ui.lua
bloba1430e3039f05dbd6269270e71d871f5e3caca50
1 r2.logicUI =
3 MenuName = "ui:interface:r2ed_triggers_menu",
4 Menu = nil,
7 function r2.logicUI:openLogicMenu(caller)
9 launchContextMenuInGame(self.MenuName)
10 if self.Menu == nil or self.Menu.isNil then
11 self.Menu = getUI(self.MenuName)
12 end
14 local menu = self.Menu
16 menu:setParentPos(caller)
17 menu:setPosRef("BL TL")
19 local root = menu:getRootMenu()
20 if (caller.y_real - root.h_real < 0) then
21 menu.y = root.h_real + caller.h_real
22 else
23 menu.y = 0
24 end
25 menu:setMinW(85)
26 menu:updateCoords()
27 end
29 function r2.logicUI:getEltUIInstId(eltUI)
30 return eltUI.Env.InstanceId
31 end
33 function r2.logicUI:setEltUIInstId(eltUI, id)
34 eltUI.Env.InstanceId = id
35 end
37 function r2.logicUI:setSequUIInstId(sequUI, id)
38 sequUI.Env.InstanceId = id
39 end
41 function r2.logicUI:getSequUIInstId(sequUI)
42 return sequUI.Env.InstanceId
43 end
45 ------- NEW ELEMENT UI ---------------------------------------------
46 function r2.logicUI:newElementUI(classUI, newInst, withOrder)
48 local templateParams = classUI.eltTemplateParams
50 --not necessary current sequenceUI
51 local sequenceUI = classUI:updatedSequUI()
52 assert(sequenceUI)
54 local listElements = sequenceUI:find("elements_list")
55 assert(listElements)
57 for i=0, listElements.childrenNb-1 do
58 local elt = listElements:getChild(i)
59 local instId = self:getEltUIInstId(elt)
60 if instId==newInst.InstanceId then return end
61 end
63 classUI.elementsIdCounter = classUI.elementsIdCounter+1
65 local elementId = "elt"..classUI.elementsIdCounter
67 -- create new element
68 local newElement = createGroupInstance("element_template", listElements.id,
69 {id=elementId, posref="TL TL", x="0", y="0", sizeref="w", hardtext="",
70 select_elt=templateParams.selectElt, open_elt_editor=templateParams.openEltEditor,
71 max_min_elt=templateParams.maxMinElt, remove_elt=templateParams.removeElt, open_chat=templateParams.openChat,
72 col_over=templateParams.colOver, col_pushed=templateParams.colPushed, multi_max_line=templateParams.multiMaxLine})
73 assert(newElement)
75 -- add element to list
76 local eltIndex = -1
77 if withOrder then
78 local comps = newInst.Parent
79 for i=0, comps.Size-1 do
80 local comp = comps[i]
81 if comp.InstanceId == newInst.InstanceId then
82 eltIndex = i
83 break
84 end
85 end
86 local eltEditor = listElements:find("edit_element")
87 assert(eltEditor)
88 local indexEltEditor = listElements:getElementIndex(eltEditor)
89 if indexEltEditor<= eltIndex then eltIndex=eltIndex+1 end
90 else
91 eltIndex = listElements.childrenNb
92 end
94 listElements:addChildAtIndex(newElement, eltIndex)
95 listElements.parent:updateCoords()
97 -- active global minimize / maximize button
98 if classUI.maxAndMin then
100 classUI:maximizeMinimizeElement(newElement)
102 if listElements.childrenNb == 2 then
103 local maxElts = sequenceUI:find("maximize_elements")
104 assert(maxElts)
105 maxElts.active = true
109 -- scroll goes down to new element
110 local scrollBar = sequenceUI:find("scroll_objects")
111 assert(scrollBar)
112 scrollBar.trackPos = 0
114 -- target sroll text
115 local scroll = newElement:find("scroll_bar_text")
116 assert(scroll)
118 local scrollText = newElement:find("scroll_text_gr")
119 assert(scrollText)
120 scroll:setTarget(scrollText.id)
122 self:setEltUIInstId(newElement, newInst.InstanceId)
124 -- update next elements title
125 if eltIndex<listElements.childrenNb-1 then
126 for i=eltIndex+1, listElements.childrenNb-1 do
127 local elt = listElements:getChild(i)
128 if r2.logicUI:getEltUIInstId(elt) then
129 classUI:updateElementTitle(elt)
134 if classUI.ownCreatedInstances[newInst.InstanceId] == true
135 or r2.logicComponents.undoRedoInstances[newInst.InstanceId]==true then
137 local select = newElement:find("select")
138 assert(select)
139 select.pushed = true
140 classUI:selectElement(select, self:getSequUIInstId(sequenceUI))
141 classUI.ownCreatedInstances[newInst.InstanceId] = nil
142 r2.logicComponents.undoRedoInstances[newInst.InstanceId] = nil
145 classUI:updateElementUI(newElement)
148 ------ REMOVE ELEMENT UI -------------------------------------------
149 function r2.logicUI:removeElementUI(classUI, removedEltUI)
151 local sequenceUI = classUI:currentSequUI()
152 assert(sequenceUI)
154 local listElements = sequenceUI:find("elements_list")
155 assert(listElements)
157 -- update follow elements number
158 local removedIndex = listElements:getElementIndex(removedEltUI)
159 for i = removedIndex+1, (listElements.childrenNb-1) do
160 local element = listElements:getChild(i)
161 assert(element)
163 -- if this element is not the element editor, update its title
164 if self:getEltUIInstId(element) then
165 classUI:updateElementTitle(element)
169 -- delete element and update coordinates of elements list
170 if removedEltUI == classUI:currentEltUI() then
172 classUI:setCurrentEltUIId(nil)
174 -- inactive element editor
175 local eltEditor = listElements:find("edit_element")
176 assert(eltEditor)
177 eltEditor.active = false
179 -- disactive up and down element buttons
180 local orderGr = sequenceUI:find("order_group")
181 assert(orderGr)
182 orderGr.active = false
185 listElements:delChild(removedEltUI)
186 listElements.parent:invalidateCoords()
188 -- if any elements in list, disactive global minimize / maximize button
189 if listElements.childrenNb == 1 then
190 local minElts = sequenceUI:find("minimize_elements")
191 assert(minElts)
193 local maxElts = sequenceUI:find("maximize_elements")
194 assert(maxElts)
196 minElts.active = false
197 maxElts.active = false
201 ------ UPDATE ELEMENT UI -------------------------------------------
202 function r2.logicUI:updateElementUI(classUI, elementUI)
204 local instance = r2:getInstanceFromId(self:getEltUIInstId(elementUI))
205 assert(instance)
207 classUI:updateElementTitle(elementUI)
209 if instance.InstanceId == classUI:currentEltInstId() then
210 classUI:updateElementEditor()
215 ------ DOWN/UP ELEMENT UI -------------------------------------------
216 function r2.logicUI:downUpElementUI(classUI, elementUI, instance)
218 local listElements = elementUI.parent
219 assert(listElements)
221 local eltEditor = listElements:find("edit_element")
222 assert(eltEditor)
224 local index = listElements:getElementIndex(elementUI)
225 local indexEditor = listElements:getElementIndex(eltEditor)
227 local indexInstance = -1
228 for i=0, instance.Parent.Size-1 do
229 if instance.Parent[i]==instance then
230 indexInstance = i
231 break
235 if index>=0 and indexEditor>=0 and indexInstance>=0 then
237 local finalIndex = indexInstance
239 if indexEditor-1 <= indexInstance then finalIndex=finalIndex+1 end
241 local selectedEltUI = classUI:currentEltUI()
242 local isSelected = (selectedEltUI==elementUI)
244 local loop = math.abs(finalIndex-index)
245 if loop>0 then
247 -- replace element UI
248 for i=1, loop do
249 if finalIndex < index then
250 listElements:upChild(elementUI)
251 else
252 listElements:downChild(elementUI)
256 -- replace element editor
257 if isSelected then
258 for i=1, loop do
259 if finalIndex < index then
260 listElements:upChild(eltEditor)
261 else
262 listElements:downChild(eltEditor)
267 local firstIndex = math.min(index, finalIndex)
268 local lastIndex = math.min(listElements.childrenNb-1, math.max(index, finalIndex)+1)
269 for i=firstIndex, lastIndex do
270 local eltUI = listElements:getChild(i)
271 if eltUI~=eltEditor then
272 classUI:updateElementTitle(eltUI)
276 if selectedEltUI~=nil then
277 selectedEltUI.active=false
278 classUI:updateElementEditor()
284 ------ MAX/MIN ELEMENT UI -------------------------------------------
285 function r2.logicUI:maximizeMinimizeElement(element, allMinimize)
287 if element == nil then
288 element = getUICaller().parent.parent.parent.parent.parent.parent
290 assert(element)
292 local eltText = element:find("element_text")
293 assert(eltText)
295 local allMin, allMax = true, true
296 if allMinimize ~= nil then
297 allMin = allMinimize
298 allMax = not allMin
301 local maxButton = element:find("maximize_element")
302 assert(maxButton)
303 local minButton = element:find("minimize_element")
304 assert(minButton)
306 -- maximize
307 if allMax and eltText.active==false then
308 eltText.active = true
309 maxButton.active = false
310 minButton.active = true
312 -- minimize
313 elseif allMin and eltText.active==true then
314 eltText.active = false
315 maxButton.active = true
316 minButton.active = false
320 ------ MAX/MIN ELEMENTS UI -------------------------------------------
321 function r2.logicUI:maximizeMinimizeElements(classUI)
323 local sequenceUI = classUI:currentSequUI()
324 assert(sequenceUI)
326 local elements = sequenceUI:find("elements_list")
327 assert(elements)
329 elements.Env.Minimize = not elements.Env.Minimize
331 for i = 0, elements.childrenNb-1 do
332 local element = elements:getChild(i)
333 assert(element)
335 -- if element is not the element editor
336 if self:getEltUIInstId(element) and element~=classUI:currentEltUI() then
337 classUI:maximizeMinimizeElement(element, elements.Env.Minimize)
341 local minElts = sequenceUI:find("minimize_elements")
342 assert(minElts)
343 local maxElts = sequenceUI:find("maximize_elements")
344 assert(maxElts)
346 if elements.Env.Minimize == true then
347 minElts.active = false
348 maxElts.active = true
349 else
350 minElts.active = true
351 maxElts.active = false
356 ------ CLOSE EDITOR ------------------------------------------
357 function r2.logicUI:closeEditor(classUI)
359 -- reset current dialog and current chat
360 classUI:setCurrentEltUIId(nil)
364 ------- UTILS ------------------------------------------------
366 function r2.logicUI:findElementUIFromInstance(classUI, instance)
368 local sequenceUI = classUI:currentSequUI()
369 assert(sequenceUI)
371 local eltsList = sequenceUI:find("elements_list")
372 assert(eltsList)
374 for i=0,eltsList.childrenNb-1 do
375 local element = eltsList:getChild(i)
376 if r2.logicUI:getEltUIInstId(element) == instance.InstanceId then
377 return element
381 return nil