Merge branch 'fixes' into main/gingo-test
[ryzomcore.git] / ryzom / common / data_common / r2 / r2_logic_comp.lua
blob85a37a47e216e5e0b0f3665ba4e1402c11b61257
1 r2.logicComponents = {}
3 r2.logicComponents.logicEditors = {
4 r2.activities,
5 r2.dialogs,
6 r2.events,
9 r2.logicComponents.undoRedoInstances = {}
12 ----- INIT ALL EDITORS ----------------------------------------------------
13 function r2.logicComponents:initLogicEditors()
15 for k, uiClass in pairs(self.logicEditors) do
16 uiClass:initEditor()
17 end
18 end
20 ------ SELECT ELEMENT --------------------------------------------
21 function r2.logicComponents:selectElement(classUI, selectedButtonElt)
23 if selectedButtonElt == nil then
24 selectedButtonElt = getUICaller()
25 end
26 assert(selectedButtonElt)
28 local sequenceUI = classUI:currentSequUI()
29 assert(sequenceUI)
31 local upDown = sequenceUI:find("order_group")
32 assert(upDown)
34 -- new selected element
35 if selectedButtonElt.pushed == true then
37 if classUI:currentEltUIId() then
39 local lastSelectedElement = classUI:currentEltUI()
40 assert(lastSelectedElement)
42 local lastEltsList = lastSelectedElement.parent
43 local editElt = lastEltsList:find("edit_element")
44 assert(editElt)
46 if classUI:currentEltUIId() == selectedButtonElt.parent.parent.parent.id then
47 return
48 end
50 lastSelectedElement.active = true
51 lastSelectedElement:find("select").pushed = false
53 editElt.active = false
54 end
56 classUI:setCurrentEltUIId(selectedButtonElt.parent.parent.parent.id)
58 local selectedElement = selectedButtonElt.parent.parent.parent
59 assert(selectedElement)
61 -- update element editor position in list
62 local eltsList = sequenceUI:find("elements_list")
63 assert(eltsList)
64 local editElt = eltsList:find("edit_element")
65 assert(editElt)
67 local indexSelectedElt = eltsList:getElementIndex(selectedElement)
68 local indexEltEditor = eltsList:getElementIndex(editElt)
70 if indexEltEditor<indexSelectedElt then
71 for i=indexEltEditor, indexSelectedElt-1 do
72 eltsList:downChild(editElt)
73 end
74 else
75 for i=indexSelectedElt, indexEltEditor-2 do
76 eltsList:upChild(editElt)
77 end
78 end
80 editElt.active = true
81 selectedElement.active = false
82 classUI:updateElementEditor()
84 upDown.active = (classUI.elementOrder==true and eltsList.childrenNb>2)
86 -- cancel current selection
87 else
89 local lastSelectedElement = classUI:currentEltUI()
90 assert(lastSelectedElement)
92 upDown.active = false
94 local lastEltsList = lastSelectedElement.parent
95 local editElt = lastEltsList:find("edit_element")
96 assert(editElt)
98 editElt.active = false
99 lastSelectedElement.active = true
101 classUI:setCurrentEltUIId(nil)
105 ------ SELECT SEQUENCE --------------------------------------
106 function r2.logicComponents:selectSequence(classUI, sequenceUI)
108 local sequenceUI = classUI:currentSequUI()
109 assert(sequenceUI)
111 -- select first chat of dialog
112 local eltsList = sequenceUI:find("elements_list")
113 assert(eltsList)
114 -- if eltsList.childrenNb > 1 then
116 -- local firstElt = eltsList:getChild(0)
117 -- element editor
118 -- if r2.logicUI:getEltUIInstId(firstElt) == nil then
119 -- firstElt = eltsList:getChild(1)
120 -- end
122 -- local selectedButton = firstElt:find("select")
123 -- assert(selectedButton)
125 -- selectedButton.pushed = true
126 -- classUI:selectElement(selectedButton)
127 -- end
129 -- deselect old selected element
130 if classUI:currentEltUIId() then
132 local lastSelectedElement = classUI:currentEltUI()
133 assert(lastSelectedElement)
135 local lastEltsList = lastSelectedElement.parent
136 local editElt = lastEltsList:find("edit_element")
137 assert(editElt)
139 lastSelectedElement.active = true
140 lastSelectedElement:find("select").pushed = false
142 editElt.active = false
144 classUI:setCurrentEltUIId(nil)
147 eltsList.Env.Minimize = true
150 ------ CREATE EDITOR -----------------------------------------------
151 function r2.logicComponents:createElementEditor(classUI)
153 -- not necessary current sequenceUI
154 local sequenceUI = classUI:updatedSequUI()
155 assert(sequenceUI)
157 -- create element editor
158 local elementsList = sequenceUI:find("elements_list")
159 assert(elementsList)
161 local newEditorElt = createGroupInstance(classUI.elementEditorTemplate, elementsList.id, {id="edit_element", active="false"})
162 assert(newEditorElt)
163 elementsList:addChild(newEditorElt)
164 elementsList.parent:updateCoords()
166 newEditorElt.active = false
168 return newEditorElt
171 ----- CLOSE ELEMENT EDITOR ---------------------------------------------
172 function r2.logicComponents:closeElementEditor(classUI)
174 local selectedEltUI = classUI:currentEltUI()
175 if selectedEltUI then
176 local selectedEltButton = selectedEltUI:find("select")
177 assert(selectedEltButton)
179 selectedEltButton.pushed = false
180 classUI:selectElement(selectedEltButton)
184 ------ NEW ELEMENT INST ------------------------------------------
185 function r2.logicComponents:newElementInst(classUI)
188 ------ UPDATE ELEMENT TITLE -------------------------------------------
189 function r2.logicComponents:updateElementTitle(classUI, eltUI, showPartIndex)
191 local eltInst = r2:getInstanceFromId(r2.logicUI:getEltUIInstId(eltUI))
192 assert(eltInst)
194 -- part index
195 local partIndex = ""
197 if showPartIndex then
198 local index = self:searchElementIndex(eltInst)
199 partIndex = classUI.elementInitialName.." "..index.." : "
202 local eltName = eltInst:getName()
204 -- title
205 local title = eltUI:find("title")
206 assert(title)
207 local uc_title = ucstring()
208 uc_title:fromUtf8(partIndex..eltName)
209 title.uc_hardtext_format = uc_title
212 ------ REMOVE ELEMENT INST ----------------------------------------
213 function r2.logicComponents:removeElementInst(classUI)
215 --r2.requestNewAction(i18n.get("uiR2EDRemoveLogicElementAction"))
217 local toErasedInstId = classUI:currentEltInstId()
218 assert(toErasedInstId)
220 -- request erase node
221 if toErasedInstId and r2:getInstanceFromId(toErasedInstId) then
222 r2.requestEraseNode(toErasedInstId, "", -1)
226 ------ UP ELEMENT INST -------------------------------------------
227 function r2.logicComponents:upElementInst(classUI)
229 r2.requestNewAction(i18n.get("uiR2EDMoveLogicElementUpAction"))
231 local sequenceUI = classUI:currentSequUI()
232 assert(sequenceUI)
234 local listElements = sequenceUI:find("elements_list")
235 assert(listElements)
237 local selectedElement = classUI:currentEltUI()
238 assert(selectedElement)
240 local index = listElements:getElementIndex(selectedElement)
242 if index>0 then
243 local sequenceId = classUI:currentSequInstId()
244 assert(sequenceId)
246 r2.requestMoveNode(sequenceId, "Components", index,
247 sequenceId, "Components", index-1)
251 ------ DOWN ELEMENT INST -----------------------------------------
252 function r2.logicComponents:downElementInst(classUI)
254 r2.requestNewAction(i18n.get("uiR2EDMoveLogicElementDownAction"))
256 local sequenceUI = classUI:currentSequUI()
257 assert(sequenceUI)
259 local listElements = sequenceUI:find("elements_list")
260 assert(listElements)
262 local selectedElement = classUI:currentEltUI()
263 assert(selectedElement)
265 local index = listElements:getElementIndex(selectedElement)
267 local sequenceId = classUI:currentSequInstId()
268 assert(sequenceId)
269 if index < r2:getInstanceFromId(sequenceId).Components.Size-1 then
270 r2.requestMoveNode(sequenceId, "Components", index,
271 sequenceId, "Components", index+1)
275 ---- TRANSLATE SECONDS IN HOURS, MINUTES AND ECONDS
276 function r2.logicComponents:calculHourMinSec(totalSecNb)
278 local minSecNb, hourNb = totalSecNb, 0
279 while minSecNb > 3599 do
280 hourNb = hourNb+1
281 minSecNb = minSecNb - 3600
284 local minNb, secNb = 0, minSecNb
285 while secNb > 59 do
286 minNb = minNb+1
287 secNb = secNb - 60
290 return hourNb, minNb, secNb
294 --- SEARCH INDEX OF INSTANCE IN COMPONENT TABLE
295 function r2.logicComponents:searchElementIndex(instance, fun)
297 local components = instance.Parent
298 local decalSequ = 0
300 for i=0, components.Size-1 do
301 local comp = components[i]
302 if fun and not fun(comp) then
303 decalSequ = decalSequ+1
304 elseif comp.User.Deleted == true then
305 decalSequ = decalSequ+1
307 if comp.InstanceId == instance.InstanceId then
308 return i+1-decalSequ
312 return -1