Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/menu/data/gui/scripts/GUITools.lua @ 7663

Last change on this file since 7663 was 7663, checked in by konrad, 13 years ago

key handling in some menu sheets has been added.

  • Property svn:eol-style set to native
File size: 11.2 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
58--@arguments:
59--  list: 2-dimensional table, arguments are items that contain a button and its function
60--  code: code of any key on the keyboard
61--  P: menusheet
62--  n: number of rows
63--  m: number of colums
64
65function buttonIteratorHelper(list, code, P, n, m)
66
67    --key down
68    if code == "208" then
69        if P.index < 0 then     -- initial status
70            P.index = 0
71            P.oldindex = -1
72        else
73            P.oldindex = P.index
74            P.index = (P.index + m) % (m*n)
75
76            while list[P.index+1] == nil do
77                P.oldindex = P.index
78                P.index = (P.index + m) % (m*n)
79            end
80        end
81
82    --key up
83    elseif code == "200" then
84        if P.index < 0 then
85            P.index = 0
86            P.oldindex = -1
87        elseif(P.index == 0) then
88            P.oldindex = P.index
89            P.index = m*n-m
90
91            while list[P.index+1] == nil do
92                P.oldindex = P.index
93                P.index = (P.index-m)%(m*n)
94            end
95        else
96            P.oldindex = P.index
97            P.index = (P.index -m) % (m*n)
98
99            while list[P.index+1] == nil do
100                cout(0,P.index)
101                P.oldindex = P.index
102                P.index = (P.index-m)%(m*n)
103            end
104        end
105
106    --key right
107    elseif code == "205" then
108        if P.index < 0 then
109            P.index = 0
110            P.oldindex = -1
111        elseif (P.index+1) % m == 0 then     -- we are at the right-end of a row
112            P.oldindex = P.index
113            P.index = P.index + 1 -m
114
115            while list[P.index+1] == nil do
116                P.oldindex = P.index
117                P.index = P.index + 1
118            end
119        else
120            P.oldindex = P.index
121            P.index = P.index + 1
122
123            while list[P.index+1] == nil do
124                if (P.index+1) % m == 0 then     -- we are at the right-end of a row
125                    P.oldindex = P.index
126                    P.index = P.index + 1-m
127
128                else   
129                    P.oldindex = P.index
130                    P.index = P.index + 1
131                end
132            end
133        end   
134
135    --key left
136    elseif code == "203" then
137        if P.index < 0 then
138            P.index = 0
139            P.oldindex = -1
140        elseif P.index % m == 0 then         -- we are at the left-end of a row
141            P.oldindex = P.index
142            P.index = P.index +m-1
143
144            while list[P.index+1] == nil do
145                P.oldindex = P.index
146                P.index = P.index -1
147            end
148        else
149            P.oldindex = P.index
150            P.index = P.index -1
151
152            while list[P.index+1] == nil do
153                if P.index % m == 0 then     -- we are at the left-end of a row
154                    P.oldindex = P.index
155                    P.index = P.index -1 + m
156                else               
157                    P.oldindex = P.index
158                    P.index = P.index -1
159                end
160            end   
161        end
162    end
163       
164    if (code == "208" or code == "200" or code == "203" or code == "205") and P.oldindex~= P.index then
165
166        local system = CEGUI.System:getSingleton()
167        local window = winMgr:getWindow("orxonox/MainMenuBackground")
168
169        local item = list[P.index+1]
170        local child = item["button"] 
171
172        --teste ob der Button nicht schon gehighlightet ist
173        cout(0,child:getProperty("NormalImageRightEdge"))
174        if child:getProperty("NormalImageRightEdge") == "set:TaharezGreenLook image:ButtonRightHighlight" then
175            --nop
176        else
177            child:setProperty("NormalImageRightEdge", string.sub(child:getProperty("NormalImageRightEdge"),1,-7) .. "Highlight")
178            child:setProperty("NormalImageLeftEdge", string.sub(child:getProperty("NormalImageLeftEdge"),1,-7) .. "Highlight")
179            child:setProperty("NormalImageBackground", string.sub(child:getProperty("NormalImageBackground"),1,-7) .. "Highlight")
180            if P.oldindex >= 0 then
181                if list[P.oldindex+1] ~= nil then
182                    local item = list[P.oldindex+1]
183                    local oldChild = item["button"]
184                    oldChild:setProperty("NormalImageRightEdge", string.sub(oldChild:getProperty("NormalImageRightEdge"),1,-10) .. "Normal")
185                    oldChild:setProperty("NormalImageLeftEdge", string.sub(oldChild:getProperty("NormalImageLeftEdge"),1,-10) .. "Normal")
186                    oldChild:setProperty("NormalImageBackground", string.sub(oldChild:getProperty("NormalImageBackground"),1,-10) .. "Normal")
187                end
188            end
189        end
190
191        local i = 1
192        while i < (n*m) do
193            if i == P.index +1 then 
194                i = i+1
195            else
196                if list[i] ~= nil then 
197                local item = list[i]
198                local child = item["button"]
199                    if child:getProperty("NormalImageRightEdge") == "set:TaharezGreenLook image:ButtonRightHighlight" then
200                        child:setProperty("NormalImageRightEdge", string.sub(child:getProperty("NormalImageRightEdge"),1,-10) .. "Normal")
201                        child:setProperty("NormalImageLeftEdge", string.sub(child:getProperty("NormalImageLeftEdge"),1,-10) .. "Normal")
202                        child:setProperty("NormalImageBackground", string.sub(child:getProperty("NormalImageBackground"),1,-10) .. "Normal")
203                    end
204                end
205            end
206                i=i+1
207        end
208    end
209   
210    --enter
211    if code == "28" then
212        local item = list[P.index+1]
213        local child = item["button"] 
214        child:setProperty("NormalImageRightEdge", string.sub(child:getProperty("NormalImageRightEdge"),1,-10) .. "Normal")
215        child:setProperty("NormalImageLeftEdge", string.sub(child:getProperty("NormalImageLeftEdge"),1,-10) .. "Normal")
216        child:setProperty("NormalImageBackground", string.sub(child:getProperty("NormalImageBackground"),1,-10) .. "Normal")
217
218        local foo = item["function"]
219        foo()
220    end
221
222    cout(0, P.oldindex)
223    cout(0, P.index)
224
225end
226
227function indexTester(list,code,P,n,m)
228    --key down
229    if code == "208" then
230        if P.index < 0 then     -- initial status
231            P.index = 0
232            P.oldindex = -1
233        else
234            P.oldindex = P.index
235            P.index = (P.index + m) % (m*n)
236
237            while list[P.index+1] == nil do
238                P.oldindex = P.index
239                P.index = (P.index + m) % (m*n)
240            end
241        end
242
243    --key up
244    elseif code == "200" then
245        if P.index < 0 then
246            P.index = 0
247            P.oldindex = -1
248        elseif(P.index == 0) then
249            P.oldindex = P.index
250            P.index = m*n-m
251
252            while list[P.index+1] == nil do
253                P.oldindex = P.index
254                P.index = (P.index-m)%(m*n)
255            end
256        else
257            P.oldindex = P.index
258            P.index = (P.index -m) % (m*n)
259
260            while list[P.index+1] == nil do
261                P.oldindex = P.index
262                P.index = P.index -m
263            end
264        end
265
266    --key right
267    elseif code == "205" then
268        if P.index < 0 then
269            P.index = 0
270            P.oldindex = -1
271        elseif (P.index+1) % m == 0 then     -- we are at the right-end of a row
272            P.oldindex = P.index
273            P.index = P.index + 1 -m
274
275            while list[P.index+1] == nil do
276                P.oldindex = P.index
277                P.index = P.index + 1
278            end
279        else
280            P.oldindex = P.index
281            P.index = P.index + 1
282
283            while list[P.index+1] == nil do
284                if (P.index+1) % m == 0 then     -- we are at the right-end of a row
285                    P.oldindex = P.index
286                    P.index = P.index + 1-m
287
288                else   
289                    P.oldindex = P.index
290                    P.index = P.index + 1
291                end
292            end
293        end   
294
295    --key left
296    elseif code == "203" then
297        if P.index < 0 then
298            P.index = 0
299            P.oldindex = -1
300        elseif P.index % m == 0 then         -- we are at the left-end of a row
301            P.oldindex = P.index
302            P.index = P.index +m-1
303
304            while list[P.index+1] == nil do
305                P.oldindex = P.index
306                P.index = P.index -1
307            end
308        else
309            P.oldindex = P.index
310            P.index = P.index -1
311
312            while list[P.index+1] == nil do
313                if P.index % m == 0 then     -- we are at the left-end of a row
314                    P.oldindex = P.index
315                    P.index = P.index -1 + m
316                else               
317                    P.oldindex = P.index
318                    P.index = P.index -1
319                end
320            end   
321        end
322    end   
323
324    cout(0, P.oldindex)
325    cout(0, P.index)
326
327end
328
329
330
331
Note: See TracBrowser for help on using the repository browser.