Merge branch 'fixes' into main/gingo-test
[ryzomcore.git] / ryzom / common / data_common / r2 / r2_ui_misc.lua
blob310e648b89735a1175e17eaa21bb810e16e37cea
1 -- misc ui helper functions
3 ------------------------------------------------------------------------------------------------------------
4 -- backup window coordinates
5 function r2:backupWndCoords(wnd)
6 if wnd == nil then
7 return nil
8 end
9 local coords = { x = wnd.x, y = wnd.y, w = wnd.w, h = wnd.h }
10 return coords
11 end
13 ------------------------------------------------------------------------------------------------------------
14 -- restore window coordinates
15 function r2:restoreWndCoords(wnd, coords)
16 if wnd == nil or coords == nil then
17 debugInfo("Not restauring coordinates, wnd = " .. tostring(wnd) .. ", coords = " .. tostring(coords))
18 return
19 end
20 wnd.x = coords.x
21 wnd.y = coords.y
22 wnd.w = coords.w
23 wnd.h = coords.h
24 wnd:invalidateCoords()
25 end
27 ------------------------------------------------------------------------------------------------------------
28 -- clean content of a tree node
29 function r2:cleanTreeNode(tree, nodeId)
30 local node = tree:getRootNode():getNodeFromId(nodeId)
31 if not node or node.isNil then
32 debugInfo("r2:cleanTreeNode : Can't find node " .. nodeId)
33 return
34 end
35 while node:getNumChildren() ~= 0 do
36 node:deleteChild(node:getChild(0))
37 end
38 end
40 ------------------------------------------------------------------------------------------------------------
41 -- clean content of a tree node
42 function r2:cleanTreeRootNode(tree)
43 local node = tree:getRootNode()
44 if not node or node.isNil then
45 debugInfo("r2:cleanTreeNode : Can't find node " .. nodeId)
46 return
47 end
48 while node:getNumChildren() ~= 0 do
49 node:deleteChild(node:getChild(0))
50 end
51 end
53 ------------------------------------------------------------------------------------------------------------
54 -- make an instance and its sons blink
55 -- TODO nico : put elsewhere (not really ui, but 3D view)
56 function r2:blink(instance)
57 if type(instance) == "userdata" then
58 local vd = instance.DisplayerVisual
59 if vd ~= nil then
60 vd:blink()
61 end
62 forEach(instance, function (k, v) self:blink(v) end)
63 -- TODO : pairs do not called the redefined 'next' function
64 --for k, v in pairs(instance) do
65 -- self:blink(v)
66 --end
67 end
68 end
70 ------------------------------------------------------------------------------------------------------------
71 -- clear the content of a menu
72 function r2:clearMenu(menu)
73 local numLine = menu:getNumLine()
74 for k = 1, numLine do
75 menu:removeLine(0)
76 end
77 end
79 ------------------------------------------------------------------------------------------------------------
80 -- add a menu entry with an icon on the left
81 function r2:addMenuLine(menu, ucText, ah, ahParams, id, iconName, size)
82 menu:addLine(ucText, ah, ahParams, id)
83 if iconName ~= "" and iconName ~= nil then
84 local menuButton = createGroupInstance("r2_menu_button", "", { bitmap = iconName, size = tostring(size)})
85 if menuButton then
86 menu:setUserGroupLeft(menu:getNumLine() - 1, menuButton)
87 end
88 end
89 end
91 ------------------------------------------------------------------------------------------------------------
92 -- enclose a ui xml script with the necessary header and ending
93 function r2:encloseXmlScript(script)
94 return [[ <interface_config>
95 <root id="interface" x="0" y="0" w="800" h="600" active="true" /> ]] .. script .. [[</interface_config>]]
96 end
98 ------------------------------------------------------------------------------------------------------------
99 -- create interface for inspecting a lua table
100 -- TODO place this elsewhere because useful for other stuffs
101 function oldInspect(object, maxdepth, alreadySeen)
103 if (object == nil) then
104 debugWarning("Cannot inspect a nil value")
105 return
108 if alreadySeen == nil then
109 alreadySeen = {}
112 if (maxdepth == nil) then maxdepth = 1 end
115 local x = 0
116 local y = 0
117 local w = 150
118 local h = 150
120 -- backup position
121 prevGroup = getUI("ui:interface:lua_inspector")
122 if not (prevGroup == nil) then
123 x = prevGroup.x
124 y = prevGroup.y
125 w = prevGroup.w
126 h = prevGroup.h
130 -- window header
131 local script =
133 <interface_config>
134 <root id="interface" x="0" y="0" w="800" h="600" active="true" />
135 <group type="container" id="old_lua_inspector" w="150" title="uiLuaInspector" global_color="false" line_at_bottom="false"
136 movable="true" active="true" opened="true" openable="false" resizer="true" header_color="UI:SAVE:WIN:COLORS:OPT"
137 pop_min_w="150" pop_min_h="150" pop_max_w="800" pop_max_h="600"
139 <group id="content" x="0" y="0" sizeref="wh" w="0" h="0" posref="TL TL" >
140 <group id="sbtree" posref="TL TL" sizeref="wh" x="0" w="-14" y="-8" h="-16" >
141 <!-- group of entity instances -->
142 <group id="tree_list" type="tree" posref="TL TL" x="14" y="0" col_over="255 255 255 48" col_select="255 255 255 80"
143 max_sizeparent="parent" max_sizeref="wh" max_w="-10" max_h="0">
146 local id = 0
148 -- generate a new ID for a tree node
149 local function newId() id = id + 1; return tostring(id) end
151 -- return xml code to open a new tree node with the given content
152 local function nodeOpen(content, color, opened)
153 return [[<node id="]] .. newId() .. [[" name="]] .. content .. [[" color="]] .. color .. [[" global_color="false" opened="]] .. tostring(opened) .. [[" fontsize="15" y_decal="-1" >]] .. "\n"
156 -- return xml code to close a tree node
157 local function nodeClose()
158 return "</node>\n"
161 -- color for each type
162 local typeColor =
164 number = "255 255 255 255",
165 string = "0 255 255 255",
166 boolean = "255 255 0 255",
167 thread = "128 128 128 255",
168 table = "255 128 32 255",
169 userdata = "255 128 64 255"
172 typeColor["function"] = "255 0 255 255"
175 -- return xml code for a leaf node
176 local function node(content, val)
177 --debugInfo(colorTag(255, 255, 0) .. "node")
178 local color = typeColor[type(val)]
179 if (color == nil) then color = "127 255 127 0" end
180 local result = [[<node id="]] .. newId() .. [[" name="]] .. content .. [[" global_color="false" color="]] .. color .. [[" opened="true" fontsize="15" y_decal="-1" />]] .. "\n"
181 -- debugInfo(result)
182 return result
186 local function objectStr(Name, value)
187 --return tostring(Name).. " (" .. type(value) .. ")" .. " = " .. tostring(value)
188 return tostring(Name) .. " = " .. tostring(value)
192 -- for each type, gives the code that generate the tree for the inspector
193 local typeTable =
195 number = function(key, val) return node(objectStr(key, val), val) end,
196 string = function(key, val) return node(objectStr(key, val), val) end,
197 boolean = function(key, val) return node(objectStr(key, val), val) end,
198 thread = function(key, val) return node(objectStr(key, "thread"), val) end,
201 -- 'function' is a declared keyword, so must declare it this way
202 typeTable["function"] = function(key, val) return node(objectStr(key, "function"), val) end
204 -- recursive code to generate the code for a table
205 typeTable.table = function(key, val, depth)
206 if (alreadySeen[val] == true) then -- avoid cyclic graph
207 return node("!CYCLE!", "0 255 0 255")
209 alreadySeen[val] = true
210 -- create a temp table sorted by type
211 local sortedTable = {}
212 local index = 1
213 forEach(val, function (k, v) sortedTable[index] = { key = k, value = v }; index = index + 1 end)
214 local function comp(val1, val2)
215 -- sort by type, then by key
216 if not (type(val1.value) == type(val2.value)) then return type(val1.value) < type(val2.value) end
217 return tostring(val1.key) < tostring(val2.key)
219 table.sort(sortedTable, comp)
220 --debugInfo("VAL")
221 --luaObject(val)
222 --debugInfo("SORTED")
223 --luaObject(sortedTable)
224 local content = nodeOpen(tostring(key) .. "(key type = " .. type(key) .. ")", typeColor[type(val)], depth < maxdepth);
225 local function tableElement(key, value)
226 if (typeTable[type(value.value)] == nil) then
227 content = content .. node("?")
228 return
230 -- add node for a field of the table
231 content = content .. typeTable[type(value.value)](value.key, value.value, depth + 1)
233 forEach(sortedTable, tableElement)
234 return content .. nodeClose()
237 typeTable.userdata = typeTable.table
240 -- generate the tree
241 script = script .. typeTable[type(object)]("#object#", object, 0)
245 -- window end
246 script = script ..
247 [[</group>
248 <ctrl style='skin_scroll' id='scroll_bar' align='T' target='tree_list' />
249 </group>
250 </group>
251 </group>
253 <tree node="old_lua_inspector">
254 </tree>
255 </interface_config>
256 ]] .. "\n"
258 --for w in string.gfind(script, "node(.*)") do
259 -- debugInfo(w)
260 --end
261 --debugInfo(script)
263 if true then
264 parseInterfaceFromString(script)
266 -- restore position
267 newGroup = getUI("ui:interface:old_lua_inspector")
268 if not (newGroup == nil) then
269 newGroup.x = x
270 newGroup.y = y
271 newGroup.w = w
272 newGroup.h = h
276 updateAllLocalisedElements()
281 ------------------------------------------------------------------------------------------------------------
282 -- inspect current instance content
283 function r2:inspectCurrInstance()
284 debugInfo("Inspect current instance")
285 local instanceTable = r2:getSelectedInstance()
286 if (instanceTable == nil) then
287 debugWarning("Can't found instance")
288 --runCommand("luaObject","r2.instances")
289 return;
291 inspect(instanceTable, 4, { instanceTable.parent })