gridview.lua - crossword.koplugin - Unnamed repository; edit this file 'description' to name the repository. (DIR) Log (DIR) Files (DIR) Refs (DIR) Submodules (DIR) README (DIR) LICENSE --- gridview.lua (4089B) --- 1 local Blitbuffer = require("ffi/blitbuffer") 2 local Device = require("device") 3 local Screen = Device.screen 4 local Size = require("ui/size") 5 local Geom = require("ui/geometry") 6 local GestureRange = require("ui/gesturerange") 7 local FrameContainer = require("ui/widget/container/framecontainer") 8 local InputContainer = require("ui/widget/container/inputcontainer") 9 local VerticalGroup = require("ui/widget/verticalgroup") 10 local UIManager = require("ui/uimanager") 11 local logger = require("logger") 12 local ffi = require("ffi") 13 14 local SoftKeyboard = require("softkeyboard") 15 local GridRow = require("gridrow") 16 local GridSquare = require("gridsquare") 17 local GridClue = require("gridclue") 18 19 local GridView = InputContainer:new{ 20 width = nil, 21 height = nil, 22 size = { 23 cols = nil, 24 rows = nil, 25 }, 26 on_tap_callback = nil, 27 dark_mode = nil, 28 } 29 30 function GridView:init() 31 self.dimen = Geom:new{ 32 w = self.width or Screen:getWidth(), 33 h = self.height or Screen:getHeight(), 34 } 35 self.outer_padding = Size.padding.large 36 self.inner_padding = Size.padding.small 37 self.inner_dimen = Geom:new{ 38 w = self.dimen.w - 2 * self.outer_padding, 39 h = self.dimen.h - self.outer_padding, -- no bottom padding 40 } 41 self.content_width = self.inner_dimen.w 42 -- The pixel dimensions of the squares. Calculate the initial width based on the size 43 -- of the device and the number of columns. Then make a minor adjustment to account for 44 -- the margins. To do this, divide margin in 4 and multiply by the number of columns. 45 self.square_margin = 1 --Size.border.window 46 self.square_width = math.floor( 47 (self.dimen.w - (2 * self.outer_padding) - (2 * self.inner_padding)) 48 / self.size.cols) 49 -- (cont) it should be self.size.cols because that's what we're adjusting for. 50 self.square_height = self.square_width 51 -- Computer the presumed height of the grid. 52 self.grid_height = self.size.rows * self.square_height 53 self:render() 54 end 55 56 function GridView:render() 57 -- Build the row and add the squares. 58 self.rows_view = VerticalGroup:new { border = 0 } 59 for row_num, grid_row in ipairs(self.grid) do 60 local row = self:buildRow(grid_row, row_num) 61 table.insert(self.rows_view, row) 62 end 63 -- Build the container. 64 local gray = ffi.typeof("Color8")(0x22) 65 self[1] = FrameContainer:new{ 66 width = self.inner_dimen.w, 67 height = self.inner_dimen.h, 68 padding = self.outer_padding, 69 padding_bottom = 0, 70 margin = 0, 71 bordersize = 0, 72 background = (self.dark_mode and gray or Blitbuffer.COLOR_BLACK), 73 VerticalGroup:new{ 74 align = "center", 75 -- Add the rows vertical group. 76 self.rows_view, 77 } 78 } 79 end 80 -- Method to help me remember to update only the parts of the grid that need to 81 -- be updated as the player interacts with the grid. 82 function GridView:updateGrid(grid) 83 self.grid = grid 84 end 85 86 function GridView:updateClue(clue_value) 87 self.active_clue = clue_value 88 end 89 -- Given a table containing letters, build a row containing 90 -- squares with said letters. 91 function GridView:buildRow(squares, row_num) 92 local row = GridRow:new{ 93 width = self.inner_dimen.w, 94 height = self.square_height, 95 } 96 for col_num, square in ipairs(squares) do 97 row:addSquare(GridSquare:new{ 98 width = self.square_width, 99 height = self.square_height, 100 margin = self.square_margin, 101 letter_value = square.letter, 102 number_value = square.number, 103 state = square.state, 104 status = square.status, 105 row_num = row_num, -- we pass the row and col so that 106 col_num = col_num, -- the tap callback can propagate values back 107 dark_mode = self.dark_mode, 108 on_tap_callback = function(row_num, col_num) 109 self.on_tap_callback(row_num, col_num) 110 end 111 }) 112 end 113 row:update() 114 return row 115 end 116 117 function GridView:addChars(chars) 118 if self.add_chars_callback then 119 self.add_chars_callback(chars) 120 end 121 end 122 123 return GridView