1 -- misc ui helper functions
3 ------------------------------------------------------------------------------------------------------------
4 -- backup window coordinates
5 function r2
:backupWndCoords(wnd
)
9 local coords
= { x
= wnd
.x
, y
= wnd
.y
, w
= wnd
.w
, h
= wnd
.h
}
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
))
24 wnd
:invalidateCoords()
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
)
35 while node
:getNumChildren() ~= 0 do
36 node
:deleteChild(node
:getChild(0))
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
)
48 while node
:getNumChildren() ~= 0 do
49 node
:deleteChild(node
:getChild(0))
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
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
70 ------------------------------------------------------------------------------------------------------------
71 -- clear the content of a menu
72 function r2
:clearMenu(menu
)
73 local numLine
= menu
:getNumLine()
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
)})
86 menu
:setUserGroupLeft(menu
:getNumLine() - 1, menuButton
)
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>]]
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")
108 if alreadySeen
== nil then
112 if (maxdepth
== nil) then maxdepth
= 1 end
121 prevGroup
= getUI("ui:interface:lua_inspector")
122 if not (prevGroup
== nil) then
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">
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()
161 -- color for each type
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"
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
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
= {}
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
)
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("?")
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
241 script
= script
.. typeTable
[type(object
)]("#object#", object
, 0)
248 <ctrl style='skin_scroll' id='scroll_bar' align='T' target='tree_list' />
253 <tree node="old_lua_inspector">
258 --for w in string.gfind(script, "node(.*)") do
264 parseInterfaceFromString(script
)
267 newGroup
= getUI("ui:interface:old_lua_inspector")
268 if not (newGroup
== nil) then
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")
291 inspect(instanceTable
, 4, { instanceTable
.parent
})