Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7928


Ignore:
Timestamp:
Feb 20, 2011, 12:42:28 PM (14 years ago)
Author:
landauf
Message:

more improvements for keyboard control of menus:

  • added setSelectionNear(r, c) function which tries to select the button closest to the given row/column
  • no initialization required anymore, the button-table grows dynamically if new buttons are inserted
Location:
code/branches/usability/data/gui/scripts
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • code/branches/usability/data/gui/scripts/AudioMenu.lua

    r7922 r7928  
    4343    end
    4444
    45     P:initButtons(1, 1)
    4645    P:setButton(1, 1, {
    4746            ["button"] = winMgr:getWindow("orxonox/AudioBackButton"),
  • code/branches/usability/data/gui/scripts/ControlsMenu.lua

    r7922 r7928  
    88
    99    --buttons are arranged in a 3x1 matrix:
    10     P:initButtons(3, 1)
    11 
    1210    P:setButton(1, 1, {
    1311            ["button"] = winMgr:getWindow("orxonox/MouseControlsButton"),
  • code/branches/usability/data/gui/scripts/CreditsMenu.lua

    r7922 r7928  
    66
    77function P.onLoad()
    8     P:initButtons(1, 1)
    98    P:setButton(1, 1, {
    109            ["button"] = winMgr:getWindow("orxonox/CreditsBackButton"),
  • code/branches/usability/data/gui/scripts/DecisionPopup.lua

    r7922 r7928  
    66
    77    --button are arranged in a 1x2 matrix
    8     P:initButtons(1, 2)
    9 
    108    P:setButton(1, 1, {
    119            ["button"] = winMgr:getWindow("orxonox/DecisionPopup_button_yes"),
  • code/branches/usability/data/gui/scripts/GUISheet.lua

    r7927 r7928  
    3434
    3535    -- set the selected button's state
    36     if self.buttons and self:hasSelection() then
    37         self:setButtonStateSelected()
    38     end
     36    self:setSelectedButtonsStateToSelected()
    3937
    4038    self:onShow()
     
    107105
    108106-- Initializes the buttons table, used to control the menu with the keyboard
    109 -- ratio: the button's with divided by the button's height (used to calculate distance between buttons - adjust this until you get the desired behavior)
    110 function P:initButtons(rows, columns, ratio)
     107function P:initButtons(rows, columns)
    111108    self.rows = rows
    112109    self.columns = columns
     
    114111    self.selectedRow = 0
    115112    self.selectedColumn = 0
    116 
    117     if ratio then
    118         self.ratio = ratio
    119     else
    120         self.ratio = 1
    121     end
     113    self.ratio = 1
     114end
     115
     116-- ratio: the button's with divided by the button's height (used to calculate distance between buttons - adjust this until you get the desired behavior)
     117function P:setRatio(ratio)
     118    self.ratio = ratio
    122119end
    123120
    124121-- Defines the button for a given position in the table. The upper-left button is at position (1, 1)
    125122function P:setButton(row, column, button)
    126     assert(self.rows ~= nil and self.columns ~= nil and self.buttons ~= nil, "You have to call initButtons() before using setButton()")
    127     assert(row > 0 and column > 0 and row <= self.rows and column <= self.columns, "(" .. row .. "/" .. column .. ") is not in the valid bounds of the table (1/1)-(" .. self.rows .. "/" .. self.columns .. ")")
     123    if not self.buttons then
     124        -- init the table
     125        self:initButtons(row, column)
     126    elseif row > self.rows or column > self.columns then
     127        -- rearrange the table
     128        local maxRows = math.max(self.rows, row)
     129        local maxColumns = math.max(self.columns, column)
     130
     131        for r = self.rows, 1, -1 do
     132            for c = self.columns, 1, -1 do
     133                local b = self:getButton(r, c)
     134                if b then
     135                    self.buttons[(r - 1) * self.columns + (c - 1)] = nil
     136                    self.buttons[(r - 1) * maxColumns + (c - 1)] = b
     137                end
     138            end
     139        end
     140
     141        self.rows = maxRows
     142        self.columns = maxColumns
     143    end
    128144
    129145    self.buttons[(row - 1) * self.columns + (column - 1)] = button
     
    132148-- Returns the button at a given position in the table. The upper-left button is at position (1, 1)
    133149function P:getButton(row, column)
    134     return self.buttons[(row - 1) * self.columns + (column - 1)]
     150    if self.buttons then
     151        return self.buttons[(row - 1) * self.columns + (column - 1)]
     152    else
     153        return nil
     154    end
    135155end
    136156
    137157-- Returns the selected button
    138158function P:getSelectedButton()
    139     return self:getButton(self.selectedRow, self.selectedColumn)
     159    if self:hasSelection() then
     160        return self:getButton(self.selectedRow, self.selectedColumn)
     161    else
     162        return nil
     163    end
    140164end
    141165
    142166-- Presses the selected button if any
    143167function P:pressSelectedButton()
    144     if self:hasSelection() then
     168    if self:getSelectedButton() then
    145169        self.pressedEnter = true
    146170        self:getSelectedButton().callback()
     
    151175-- Sets the selection to a given row and column. The upper-left button is at position (1, 1)
    152176function P:setSelection(row, column)
     177    if not self.buttons then
     178        return
     179    end
     180
    153181    assert(row > 0 and column > 0 and row <= self.rows and column <= self.columns, "(" .. row .. "/" .. column .. ") is not in the valid bounds of the table (1/1)-(" .. self.rows .. "/" .. self.columns .. ")")
    154182
    155     if self:hasSelection() then
    156         self:setButtonStateNormal()
    157     end
     183    self:setSelectedButtonsStateToNormal()
    158184
    159185    self.selectedRow = row
    160186    self.selectedColumn = column
    161187
    162     self:setButtonStateSelected()
     188    self:setSelectedButtonsStateToSelected()
     189end
     190
     191-- Sets the selection to the button closest to the given row and column. The upper-left button is at position (1, 1)
     192function P:setSelectionNear(row, column)
     193    if not self.buttons then
     194        return
     195    end
     196
     197    assert(row > 0 and column > 0 and row <= self.rows and column <= self.columns, "(" .. row .. "/" .. column .. ") is not in the valid bounds of the table (1/1)-(" .. self.rows .. "/" .. self.columns .. ")")
     198
     199    if self:getButton(row, column) then
     200        self:setSelection(row, column)
     201    else
     202        local min = 1000000
     203        local minRow, minColumn
     204
     205        for r = 1, self.rows do
     206            for c = 1, self.columns do
     207                if self:getButton(r, c) then
     208                    local distance = math.sqrt((row - r)^2 + ((column - c) * self.ratio)^2)
     209                    if distance < min then
     210                        min = distance; minRow = r; minColumn = c
     211                    end
     212                end
     213            end
     214        end
     215
     216        if minRow and minColumn then
     217            self:setSelection(minRow, minColumn)
     218        else
     219            self:resetSelection()
     220        end
     221    end
    163222end
    164223
     
    175234-- Generic move function, the values are determined at runtime depending on the arguments
    176235function P:moveSelection(relMove, selectedThis, selectedOther, limitThis, limitOther, isRow)
     236    if not self.buttons then
     237        return
     238    end
     239
    177240    -- if there's no selection yet, prepare it such that the selection enters the table from the desired side
    178241    if self.selectedRow > 0 or self.selectedColumn > 0 then
    179         self:setButtonStateNormal()
     242        self:setSelectedButtonsStateToNormal()
    180243    else
    181244        if relMove > 0 then
     
    199262    -- if the button is deactivated, search the button closest to the desired location
    200263    if self:getSelectedButton() == nil then
    201         local min = self.rows + self.columns * self.ratio
     264        local min = 1000000
    202265        local minV1, minV2
    203266        local limit, step
     
    247310    end
    248311
    249     if self:hasSelection() == true then
    250         self:setButtonStateSelected()
    251     end
     312    self:setSelectedButtonsStateToSelected()
    252313end
    253314
    254315-- Resets the selection
    255316function P:resetSelection()
    256     if self:hasSelection() then
    257         self:setButtonStateNormal()
    258     end
     317    self:setSelectedButtonsStateToNormal()
    259318
    260319    self.selectedRow = 0
     
    286345
    287346-- Sets the selected button's state to normal
    288 function P:setButtonStateNormal()
    289     self:setButtonState("Normal")
     347function P:setSelectedButtonsStateToNormal()
     348    self:setSelectedButtonsState("Normal")
    290349end
    291350
    292351-- Sets the selected button's state to selected
    293 function P:setButtonStateSelected()
    294     self:setButtonState("Selected")
     352function P:setSelectedButtonsStateToSelected()
     353    self:setSelectedButtonsState("Selected")
    295354end
    296355
    297356-- Sets the selected button's state to pushed
    298 function P:setButtonStatePushed()
    299     self:setButtonState("Pushed")
     357function P:setSelectedButtonsStateToPushed()
     358    self:setSelectedButtonsState("Pushed")
    300359end
    301360
    302361-- Sets the selected button's state
    303 function P:setButtonState(state)
     362function P:setSelectedButtonsState(state)
    304363    if self:getSelectedButton() then
    305364        local element = self:getSelectedButton().button
  • code/branches/usability/data/gui/scripts/GraphicsMenu.lua

    r7922 r7928  
    8686    block = false
    8787
    88     P:initButtons(1, 1)
    8988    P:setButton(1, 1, {
    9089            ["button"] = winMgr:getWindow("orxonox/GraphicsBackButton"),
  • code/branches/usability/data/gui/scripts/HostMenu.lua

    r7922 r7928  
    1515    button:setSelected(false)
    1616    P.createLevelList()
    17 
    18     P:initButtons(1, 2)
    1917
    2018    P:setButton(1, 1, {
  • code/branches/usability/data/gui/scripts/InGameMenu.lua

    r7922 r7928  
    88
    99    --button are arranged in a 4x1 matrix, the left lower item is nil
    10     P:initButtons(4, 1)
    11 
    1210    P:setButton(1, 1, {
    1311            ["button"] = winMgr:getWindow("orxonox/InGameMenu_ReturnButton"),
  • code/branches/usability/data/gui/scripts/KeyBindMenu.lua

    r7924 r7928  
    101101    orxonox.KeyBinderManager:getInstance():registerKeybindCallback(funct)
    102102
    103     P:initButtons(1, 1)
    104103    P:setButton(1, 1, {
    105104            ["button"] = winMgr:getWindow("orxonox/KeyBindBackButton"),
  • code/branches/usability/data/gui/scripts/MainMenu.lua

    r7922 r7928  
    66function P.onLoad()
    77    --buttons are arranged in a 6x1 Matrix (list)
    8     P:initButtons(6, 1)
    9 
    108    P:setButton(1, 1, {
    119            ["button"] = winMgr:getWindow("orxonox/QuickGameTestButton"),
  • code/branches/usability/data/gui/scripts/MiscConfigMenu.lua

    r7924 r7928  
    8585    P.createLines()
    8686
    87     P:initButtons(1, 1)
    8887    P:setButton(1, 1, {
    8988            ["button"] = winMgr:getWindow("orxonox/MiscConfigMenu/MiscConfigBackButton"),
  • code/branches/usability/data/gui/scripts/MouseControlsMenu.lua

    r7924 r7928  
    3333    end
    3434
    35     P:initButtons(1, 1)
    3635    P:setButton(1, 1, {
    3736            ["button"] = winMgr:getWindow("orxonox/MouseControlsBackButton"),
  • code/branches/usability/data/gui/scripts/MultiplayerMenu.lua

    r7926 r7928  
    1010    P.multiplayerMode = "startClient"
    1111
    12     --button are arranged in a 2x2 matrix, the lower items are both the back button
    13     P:initButtons(2, 3)
    14 
     12    --button are arranged in a 3x2 matrix, Join and Host buttons are in the upper left and middle, the back button in the lower right of the table
    1513    P:setButton(1, 1, {
    1614            ["button"] = winMgr:getWindow("orxonox/MultiplayerJoinButton"),
  • code/branches/usability/data/gui/scripts/SettingsMenu.lua

    r7924 r7928  
    66function P.onLoad()
    77    --"Gameplay" and "Multiplayer Options" are not integrated in the list
    8     --buttons are arranged in a 4x2 matrix. The lower-right element is not in the matrix!
    9     P:initButtons(4, 2)
    10 
     8    --buttons are arranged in a 4x2 matrix.
    119    P:setButton(1, 2, {
    1210            ["button"] = winMgr:getWindow("orxonox/SettingsMenu/GraphicsButton"),
  • code/branches/usability/data/gui/scripts/SheetManager.lua

    r7927 r7928  
    133133
    134134    -- select first button if the menu was opened with the keyboard
    135     if previous and previous.pressedEnter and menuSheet.buttons and menuSheet:hasSelection() == false then
    136         menuSheet:moveSelectionRow(1)
     135    if previous and previous.pressedEnter and menuSheet:hasSelection() == false then
     136        menuSheet:setSelectionNear(1, 1)
    137137    end
    138138
  • code/branches/usability/data/gui/scripts/SingleplayerMenu.lua

    r7922 r7928  
    1414
    1515    --buttons are arranged in a 1x2 matrix
    16     P:initButtons(1, 2)
    17 
    1816    P:setButton(1, 1, {
    1917            ["button"] = winMgr:getWindow("orxonox/SingleplayerStartButton"),
Note: See TracChangeset for help on using the changeset viewer.