Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / common / data_common / r2 / r2_ui_palette.lua
blob16e28b0dda4bd31e4b8a2b2fe2bfcdcdf7897fc7
1 ----------------
2 -- PALETTE UI --
3 ----------------
5 ------------------------------------------------------------------------------------------------------------
6 -- handle right click on palette item
7 function r2:onPaletteRightClick(paletteNode)
8 -- store palette path for futur display
9 self.tmpPaletteNode = paletteNode
10 end
12 ------------------------------------------------------------------------------------------------------------
13 function r2:onShowPaletteLuaTable(maxDepth)
14 if (self.tmpPaletteNode == nil) then return end
15 runCommand("luaObject", self.tmpPaletteNode, maxDepth)
16 end
20 ------------------------------------------------------------------------------------------------------------
21 function r2:buildPaletteUI()
23 --debugInfo(colorTag(0, 255, 127) .."Begin building palette UI")
24 local tree = getUI(r2.Palette.UIPath)
25 local botTree = getUI(r2.Palette.BotObjectsUIPath)
26 if tree==nil or botTree==nil then return end
28 local shortElementId = 0
30 r2.Palette.ShortIdToElement = {}
32 local function buildBranch(luaBranch, branchStrId, depth, openDepth)
33 local newNode = SNode()
34 newNode.Opened = depth < openDepth
35 newNode.Id = "branch"
36 newNode.Text = i18n.get(branchStrId)
37 for k, v in pairs(luaBranch) do
38 if (k ~= "instances") then
40 if type(v)=="table" and v.Display~=false then
42 -- this is a sub branch
43 newNode:addChild(buildBranch(v, "uiR2ED" .. k, depth + 1, openDepth))
44 end
45 else
46 for instKey, instValue in pairs(v) do
47 -- the value is a table containing a list of instances
48 local subNode = SNode()
49 if instValue.DirectName ~= nil then
50 subNode.Text = instValue.DirectName
51 else
52 subNode.Text = i18n.get(instValue.Translation)
53 end
55 r2.PaletteIdToTranslation[instValue.Id] = subNode.Text:toUtf8()
56 r2.PaletteIdToGroupTranslation[instValue.Id] = newNode.Text:toUtf8()
57 r2.PaletteIdToType[instValue.Id] = v
59 subNode.Id = tostring(shortElementId)
60 local paletteElement = r2.getPaletteElement(instValue.Id)
61 if (paletteElement == nil)
62 then
63 debugInfo("invalid nil Palette: " .. instValue.Id)
64 else
65 r2.Palette.ShortIdToElement[tostring(shortElementId)] = paletteElement
66 shortElementId = shortElementId + 1
67 subNode.Opened = false
68 subNode.AHName = "r2ed_create_entity"
69 subNode.AHParams = "PaletteId=" .. instValue.Id
70 --subNode.AHNameRight = "lua"
71 --subNode.AHParamsRight = "r2ed:onPaletteRightClick(" .. instValue.Id .. ")"
72 local ringAccess = r2.getPropertyValue(paletteElement, "RingAccess")
73 local insertNode = true
74 if ringAccess then
75 insertNode = r2.RingAccess.testAccess(ringAccess)
76 end
77 if insertNode then
78 newNode:addChild(subNode)
79 end
80 end
81 end
82 end
83 end
84 return newNode
85 end
86 -- create root node & call function
87 local rootNode = buildBranch(r2.Palette.Entries, r2.Palette.StrId, 0, 2)
88 rootNode:sort()
89 tree:setRootNode(rootNode)
91 local botRootNode = buildBranch(r2.Palette.BotEntries, r2.Palette.StrId, 0, 1)
92 botRootNode:sort()
93 botTree:setRootNode(botRootNode)
95 local paletteWindow = tree:getEnclosingContainer()
96 if paletteWindow then
97 -- paletteWindow.active = true
98 -- paletteWindow:updateCoords()
99 local selection = paletteWindow:find("entity_selection")
100 local enclosing = paletteWindow:find("entity_enclosing")
101 local delta = 6
102 enclosing.h = - selection.h_real - delta
103 enclosing.y = - selection.h_real - delta
104 paletteWindow:invalidateCoords()
107 --debugInfo(colorTag(0, 255, 127) .. "Palette UI built")
108 r2:setupPaletteAccessibleContent()
109 --r2:setupDefaultCustomBBoxes()
112 ---------------------------------------------------------------------------------------------------------
113 -- Setup the content that is visible in the palette depending on the chosen ecosystem and level
114 function r2:setupPaletteAccessibleContent()
116 local levelMin, levelMax, ecosystem = r2:getPaletteAccessibilityFactors()
118 local function setupBranch(branch)
119 local show = false
120 if branch.Id == "branch" then
121 for index = 0, branch:getNumChildren() - 1 do
122 local showChild = setupBranch(branch:getChild(index))
123 if showChild then
124 show = true
127 else
128 -- this is a leaf
129 local paletteNode = r2.Palette.ShortIdToElement[branch.Id]
130 --assert(paletteNode)
131 if paletteNode then
132 local currLevel = defaulting(paletteNode.Level, 1)
133 local currEcosystem = defaulting(paletteNode.Ecosystem, "")
134 -- tmp : ignore level & ecosystem for objects
135 if string.match(paletteNode.SheetClient, "object.*") then
136 show = true
137 elseif currLevel >= levelMin and currLevel <= levelMax and (currEcosystem == "" or string.match(currEcosystem, ecosystem)) then
138 show = true
139 else
140 --debugInfo(currEcosystem)
143 end
144 -- if show then
145 -- branch.Color = CRGBA(255, 255, 255)
146 -- else
147 -- branch.Color = CRGBA(255, 0, 0)
148 --end
149 branch.Show = show
150 return show
151 end
153 if ecosystem and levelMin and levelMax then
154 local tree = getUI(r2.Palette.UIPath)
155 setupBranch(tree:getRootNode())
156 tree:forceRebuild()
160 function r2:getPaletteAccessibilityFactors()
162 local tree = getUI(r2.Palette.UIPath)
163 if tree == nil then return end
164 local levelRange = tree:getEnclosingContainer():find("level").selection + 1 -- TMP : tha 'all' field was removed ...
165 local levelMin
166 local levelMax
167 if levelRange == 0 then
168 levelMin = 1
169 levelMax = 250
170 else
171 levelMin = (levelRange - 1) * 50 + 1
172 levelMax = levelMin + 49
173 end
174 local ecosystemTable =
176 ".*", "Desert", "Forest", "Jungle", "Lacustre", "PrimeRoots", "Goo"
178 local ecosystem= ecosystemTable[tree:getEnclosingContainer():find("ecosystem").selection + 2] -- TMP : added 2 instead of 1 because the 'all' field has been removed
180 return levelMin, levelMax, ecosystem
183 function r2:createRoad()
185 r2:setCurrentTool('R2::CToolDrawPrim', { Look = r2.PrimRender.RoadCreateLook,
186 InvalidLook = r2.PrimRender.RoadCreateInvalidLook,
187 Type="Road", ForceShowPrims=true })
190 function r2:createRegion()
192 r2:setCurrentTool('R2::CToolDrawPrim',
194 Look = r2.PrimRender.RegionCreateLook,
195 InvalidLook = r2.PrimRender.RegionCreateInvalidLook,
196 CanCloseLook = r2.PrimRender.RegionCreateCanCloseLook,
197 Type = "Region",
198 SelectInstance = true,
199 ForceShowPrims=true
204 ---------------------------------------------------------------------------------------------------------
205 -- TMP for demo : assign default custom bbox to mobs
206 --function r2:setupDefaultCustomBBoxes()
207 -- for id, node in pairs(r2.Palette.ShortIdToElement) do
208 -- if string.match(node.Base, "palette.entities.creatures.*") then
209 -- local box =
210 -- {
211 -- Enabled = true,
212 -- XMin = -0.5,
213 -- XMax = 0.5,
214 -- YMin = -0.5,
215 -- YMax = 0.5,
216 -- ZMin = 0,
217 -- ZMax = 2,
218 -- }
219 -- r2:setEntityCustomSelectBox(node.SheetClient, box)
220 -- end
221 -- end
222 --end