Changeset 7928 for code/branches/usability/data/gui/scripts/GUISheet.lua
- Timestamp:
- Feb 20, 2011, 12:42:28 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/usability/data/gui/scripts/GUISheet.lua
r7927 r7928 34 34 35 35 -- set the selected button's state 36 if self.buttons and self:hasSelection() then 37 self:setButtonStateSelected() 38 end 36 self:setSelectedButtonsStateToSelected() 39 37 40 38 self:onShow() … … 107 105 108 106 -- 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) 107 function P:initButtons(rows, columns) 111 108 self.rows = rows 112 109 self.columns = columns … … 114 111 self.selectedRow = 0 115 112 self.selectedColumn = 0 116 117 if ratio then 118 self.ratio = ratio 119 else 120 self.ratio = 1 121 end113 self.ratio = 1 114 end 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) 117 function P:setRatio(ratio) 118 self.ratio = ratio 122 119 end 123 120 124 121 -- Defines the button for a given position in the table. The upper-left button is at position (1, 1) 125 122 function 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 128 144 129 145 self.buttons[(row - 1) * self.columns + (column - 1)] = button … … 132 148 -- Returns the button at a given position in the table. The upper-left button is at position (1, 1) 133 149 function 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 135 155 end 136 156 137 157 -- Returns the selected button 138 158 function 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 140 164 end 141 165 142 166 -- Presses the selected button if any 143 167 function P:pressSelectedButton() 144 if self: hasSelection() then168 if self:getSelectedButton() then 145 169 self.pressedEnter = true 146 170 self:getSelectedButton().callback() … … 151 175 -- Sets the selection to a given row and column. The upper-left button is at position (1, 1) 152 176 function P:setSelection(row, column) 177 if not self.buttons then 178 return 179 end 180 153 181 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 .. ")") 154 182 155 if self:hasSelection() then 156 self:setButtonStateNormal() 157 end 183 self:setSelectedButtonsStateToNormal() 158 184 159 185 self.selectedRow = row 160 186 self.selectedColumn = column 161 187 162 self:setButtonStateSelected() 188 self:setSelectedButtonsStateToSelected() 189 end 190 191 -- Sets the selection to the button closest to the given row and column. The upper-left button is at position (1, 1) 192 function 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 163 222 end 164 223 … … 175 234 -- Generic move function, the values are determined at runtime depending on the arguments 176 235 function P:moveSelection(relMove, selectedThis, selectedOther, limitThis, limitOther, isRow) 236 if not self.buttons then 237 return 238 end 239 177 240 -- if there's no selection yet, prepare it such that the selection enters the table from the desired side 178 241 if self.selectedRow > 0 or self.selectedColumn > 0 then 179 self:set ButtonStateNormal()242 self:setSelectedButtonsStateToNormal() 180 243 else 181 244 if relMove > 0 then … … 199 262 -- if the button is deactivated, search the button closest to the desired location 200 263 if self:getSelectedButton() == nil then 201 local min = self.rows + self.columns * self.ratio264 local min = 1000000 202 265 local minV1, minV2 203 266 local limit, step … … 247 310 end 248 311 249 if self:hasSelection() == true then 250 self:setButtonStateSelected() 251 end 312 self:setSelectedButtonsStateToSelected() 252 313 end 253 314 254 315 -- Resets the selection 255 316 function P:resetSelection() 256 if self:hasSelection() then 257 self:setButtonStateNormal() 258 end 317 self:setSelectedButtonsStateToNormal() 259 318 260 319 self.selectedRow = 0 … … 286 345 287 346 -- Sets the selected button's state to normal 288 function P:set ButtonStateNormal()289 self:set ButtonState("Normal")347 function P:setSelectedButtonsStateToNormal() 348 self:setSelectedButtonsState("Normal") 290 349 end 291 350 292 351 -- Sets the selected button's state to selected 293 function P:set ButtonStateSelected()294 self:set ButtonState("Selected")352 function P:setSelectedButtonsStateToSelected() 353 self:setSelectedButtonsState("Selected") 295 354 end 296 355 297 356 -- Sets the selected button's state to pushed 298 function P:set ButtonStatePushed()299 self:set ButtonState("Pushed")357 function P:setSelectedButtonsStateToPushed() 358 self:setSelectedButtonsState("Pushed") 300 359 end 301 360 302 361 -- Sets the selected button's state 303 function P:set ButtonState(state)362 function P:setSelectedButtonsState(state) 304 363 if self:getSelectedButton() then 305 364 local element = self:getSelectedButton().button
Note: See TracChangeset
for help on using the changeset viewer.