Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/data/gui/scripts/GUITools.lua @ 7689

Last change on this file since 7689 was 7689, checked in by dafrick, 13 years ago

Merging menu branch to trunk.

  • Property svn:eol-style set to native
File size: 11.6 KB
Line 
1-- Returns a new menu sheet
2-- See MenuSheet.new for details about the parameters
3function createMenuSheet(name, bHidePrevious, tShowCursor, tUseKeyboard, bBlockJoyStick)
4    local sheet = require("MenuSheet").new(name, bHidePrevious, tShowCursor, tUseKeyboard, bBlockJoyStick)
5    _G[sheet.name] = sheet -- Global access required because of the event handlers
6    return sheet
7end
8
9-- Returns a new HUD sheet
10function createHUDSheet(name)
11    local sheet = require("HUDSheet").new(name)
12    _G[sheet.name] = sheet -- Global access required because of the event handlers
13    return sheet
14end
15
16function openDecisionPopup( text, callbackPtr )
17    showMenuSheet("DecisionPopup", false, true)
18    DecisionPopup.setCallback(callbackPtr)
19    DecisionPopup.setText(text)
20end
21
22function openInfoPopup(text, functionPtr, closeButton, arguments)
23    showMenuSheet("InfoPopup", false, true)
24    InfoPopup.execute(functionPtr, arguments)
25    InfoPopup.setText(text)
26    InfoPopup.setCloseButton(closeButton)
27end
28
29function getMinTextSize(window)
30    local size = {}
31
32    local lookAndFeel = CEGUI.WidgetLookManager:getSingleton():getWidgetLook(window:getLookNFeel())
33    local height = window:getFont():getLineSpacing() + window:getUnclippedPixelRect():getHeight() - lookAndFeel:getNamedArea("WithFrameTextRenderArea"):getArea():getPixelRect(window):getHeight()
34    local width =  window:getFont():getTextExtent(window:getText()) + window:getUnclippedPixelRect():getWidth() - lookAndFeel:getNamedArea("WithFrameTextRenderArea"):getArea():getPixelRect(window):getWidth()
35
36    table.insert(size, height)
37    table.insert(size, width)
38    return size
39end
40
41function getScrollingStepSize(window)
42    local height = window:getUnclippedPixelRect():getHeight()
43    local maxHeight = CEGUI.System:getSingleton():getGUISheet():getUnclippedPixelRect():getHeight()
44    local ratio = height/maxHeight
45    return 0.008*ratio/0.3204
46end
47
48function getStaticTextWindowHeight(window)
49    local lookAndFeel = CEGUI.WidgetLookManager:getSingleton():getWidgetLook(window:getLookNFeel())
50    local formattedArea = lookAndFeel:getNamedArea("WithFrameTextRenderArea"):getArea():getPixelRect(window)
51    local frameHeight = window:getUnclippedPixelRect():getHeight() - formattedArea:getHeight()
52    local lines = window:getFont():getFormattedLineCount(window:getText(), formattedArea, CEGUI.WordWrapLeftAligned)
53    local height = lines * window:getFont():getLineSpacing() + frameHeight
54    return height
55end
56
57--function to iterate through a menu sheet by using arrowkeys
58
59--@arguments:
60--  list: 2-dimensional table, arguments are items that contain a button and its function
61--        !!note: each button can only be in the list once!!
62--  code: code of any key on the keyboard
63--  P: menusheet
64--  n: number of rows of the buttontable
65--  m: number of colums of the buttontable
66
67function buttonIteratorHelper(list, code, P, n, m)
68
69    --after a key (down,up,left,right) is pressed the index of the current button has to be updated   
70
71    --key down
72    if code == "208" then
73        if P.index < 0 then     -- initial status
74            P.index = 0
75            P.oldindex = -1
76        else
77            P.oldindex = P.index
78            P.index = (P.index + m) % (m*n)     --modulo operation works as a "wrap around" in the button menu
79                                               
80            while list[P.index+1] == nil do     
81                P.oldindex = P.index
82                P.index = (P.index + m) % (m*n)
83            end
84        end
85
86    --key up
87    elseif code == "200" then
88        if P.index < 0 then
89            P.index = 0
90            P.oldindex = -1
91        elseif(P.index == 0) then
92            P.oldindex = P.index
93            P.index = m*n-m
94
95            while list[P.index+1] == nil do
96                P.oldindex = P.index
97                P.index = (P.index-m)%(m*n)
98            end
99        else
100            P.oldindex = P.index
101            P.index = (P.index -m) % (m*n)
102
103            while list[P.index+1] == nil do
104                P.oldindex = P.index
105                P.index = (P.index-m)%(m*n)
106            end
107        end
108
109    --key right
110    elseif code == "205" then
111        if P.index < 0 then
112            P.index = 0
113            P.oldindex = -1
114        elseif (P.index+1) % m == 0 then     -- we are at the right-end of a row
115            P.oldindex = P.index
116            P.index = P.index + 1 -m
117
118            while list[P.index+1] == nil do
119                P.oldindex = P.index
120                P.index = P.index + 1
121            end
122        else
123            P.oldindex = P.index
124            P.index = P.index + 1
125
126            while list[P.index+1] == nil do
127                if (P.index+1) % m == 0 then     -- we are at the right-end of a row
128                    P.oldindex = P.index
129                    P.index = P.index + 1-m
130
131                else   
132                    P.oldindex = P.index
133                    P.index = P.index + 1
134                end
135            end
136        end   
137
138    --key left
139    elseif code == "203" then
140        if P.index < 0 then
141            P.index = 0
142            P.oldindex = -1
143        elseif P.index % m == 0 then         -- we are at the left-end of a row
144            P.oldindex = P.index
145            P.index = P.index +m-1
146
147            while list[P.index+1] == nil do
148                P.oldindex = P.index
149                P.index = P.index -1
150            end
151        else
152            P.oldindex = P.index
153            P.index = P.index -1
154
155            while list[P.index+1] == nil do
156                if P.index % m == 0 then     -- we are at the left-end of a row
157                    P.oldindex = P.index
158                    P.index = P.index -1 + m
159                else               
160                    P.oldindex = P.index
161                    P.index = P.index -1
162                end
163            end   
164        end
165    end
166       
167    --to update the new current button
168    if (code == "208" or code == "200" or code == "203" or code == "205") and P.oldindex~= P.index then
169
170        local system = CEGUI.System:getSingleton()
171        local window = winMgr:getWindow("orxonox/MainMenuBackground")
172
173        local item = list[P.index+1]
174        local child = item["button"] 
175
176        --teste ob der Button nicht schon gehighlightet ist
177        if child:getProperty("NormalImageRightEdge") == "set:TaharezGreenLook image:ButtonRightHighlight" then
178            --nop
179        else
180            child:setProperty("NormalImageRightEdge", string.sub(child:getProperty("NormalImageRightEdge"),1,-7) .. "Highlight")
181            child:setProperty("NormalImageLeftEdge", string.sub(child:getProperty("NormalImageLeftEdge"),1,-7) .. "Highlight")
182            child:setProperty("NormalImageBackground", string.sub(child:getProperty("NormalImageBackground"),1,-7) .. "Highlight")
183            if P.oldindex >= 0 then
184                if list[P.oldindex+1] ~= nil then
185                    local item = list[P.oldindex+1]
186                    local oldChild = item["button"]
187                    oldChild:setProperty("NormalImageRightEdge", string.sub(oldChild:getProperty("NormalImageRightEdge"),1,-10) .. "Normal")
188                    oldChild:setProperty("NormalImageLeftEdge", string.sub(oldChild:getProperty("NormalImageLeftEdge"),1,-10) .. "Normal")
189                    oldChild:setProperty("NormalImageBackground", string.sub(oldChild:getProperty("NormalImageBackground"),1,-10) .. "Normal")
190                end
191            end
192        end
193
194        --for every highlighted button check if index is on its position. If not, set imageproperty on "normal"
195        local i = 1
196        while i < (n*m) do
197            if i == P.index +1 then 
198                i = i+1
199            else
200                if list[i] ~= nil then 
201                local item = list[i]
202                local child = item["button"]
203                    if child:getProperty("NormalImageRightEdge") == "set:TaharezGreenLook image:ButtonRightHighlight" then
204                        child:setProperty("NormalImageRightEdge", string.sub(child:getProperty("NormalImageRightEdge"),1,-10) .. "Normal")
205                        child:setProperty("NormalImageLeftEdge", string.sub(child:getProperty("NormalImageLeftEdge"),1,-10) .. "Normal")
206                        child:setProperty("NormalImageBackground", string.sub(child:getProperty("NormalImageBackground"),1,-10) .. "Normal")
207                    end
208                end
209            end
210                i=i+1
211        end
212    end
213   
214    --enter
215    if code == "28" then
216        local item = list[P.index+1]
217        local child = item["button"] 
218        child:setProperty("NormalImageRightEdge", string.sub(child:getProperty("NormalImageRightEdge"),1,-10) .. "Normal")
219        child:setProperty("NormalImageLeftEdge", string.sub(child:getProperty("NormalImageLeftEdge"),1,-10) .. "Normal")
220        child:setProperty("NormalImageBackground", string.sub(child:getProperty("NormalImageBackground"),1,-10) .. "Normal")
221
222        local foo = item["function"]
223        foo()
224    end
225
226end
227
228--write index and oldindex on the console
229--works like buttonIteratorHelper
230function indexTester(list,code,P,n,m)
231    --key down
232    if code == "208" then
233        if P.index < 0 then     -- initial status
234            P.index = 0
235            P.oldindex = -1
236        else
237            P.oldindex = P.index
238            P.index = (P.index + m) % (m*n)
239
240            while list[P.index+1] == nil do
241                P.oldindex = P.index
242                P.index = (P.index + m) % (m*n)
243            end
244        end
245
246    --key up
247    elseif code == "200" then
248        if P.index < 0 then
249            P.index = 0
250            P.oldindex = -1
251        elseif(P.index == 0) then
252            P.oldindex = P.index
253            P.index = m*n-m
254
255            while list[P.index+1] == nil do
256                P.oldindex = P.index
257                P.index = (P.index-m)%(m*n)
258            end
259        else
260            P.oldindex = P.index
261            P.index = (P.index -m) % (m*n)
262
263            while list[P.index+1] == nil do
264                P.oldindex = P.index
265                P.index = P.index -m
266            end
267        end
268
269    --key right
270    elseif code == "205" then
271        if P.index < 0 then
272            P.index = 0
273            P.oldindex = -1
274        elseif (P.index+1) % m == 0 then     -- we are at the right-end of a row
275            P.oldindex = P.index
276            P.index = P.index + 1 -m
277
278            while list[P.index+1] == nil do
279                P.oldindex = P.index
280                P.index = P.index + 1
281            end
282        else
283            P.oldindex = P.index
284            P.index = P.index + 1
285
286            while list[P.index+1] == nil do
287                if (P.index+1) % m == 0 then     -- we are at the right-end of a row
288                    P.oldindex = P.index
289                    P.index = P.index + 1-m
290
291                else   
292                    P.oldindex = P.index
293                    P.index = P.index + 1
294                end
295            end
296        end   
297
298    --key left
299    elseif code == "203" then
300        if P.index < 0 then
301            P.index = 0
302            P.oldindex = -1
303        elseif P.index % m == 0 then         -- we are at the left-end of a row
304            P.oldindex = P.index
305            P.index = P.index +m-1
306
307            while list[P.index+1] == nil do
308                P.oldindex = P.index
309                P.index = P.index -1
310            end
311        else
312            P.oldindex = P.index
313            P.index = P.index -1
314
315            while list[P.index+1] == nil do
316                if P.index % m == 0 then     -- we are at the left-end of a row
317                    P.oldindex = P.index
318                    P.index = P.index -1 + m
319                else               
320                    P.oldindex = P.index
321                    P.index = P.index -1
322                end
323            end   
324        end
325    end   
326
327    cout(0, P.oldindex)
328    cout(0, P.index)
329
330end
331
332
333
334
Note: See TracBrowser for help on using the repository browser.