|
| 1 | +--[[ |
| 2 | + Pixel Vision 8 - Draw Tool |
| 3 | + Copyright (C) 2017, Pixel Vision 8 (http://pixelvision8.com) |
| 4 | + Created by Jesse Freeman (@jessefreeman) |
| 5 | +
|
| 6 | + Please do not copy and distribute verbatim copies |
| 7 | + of this license document, but modifications without |
| 8 | + distributing is allowed. |
| 9 | +]]-- |
| 10 | + |
| 11 | +OptionModal = {} |
| 12 | +OptionModal.__index = OptionModal |
| 13 | + |
| 14 | +function OptionModal:Init(title, message, width, showCancel) |
| 15 | + |
| 16 | + local _exportModal = {} -- our new object |
| 17 | + setmetatable(_exportModal, OptionModal) -- make Account handle lookup |
| 18 | + |
| 19 | + _exportModal:Configure(title, message, width, showCancel) |
| 20 | + |
| 21 | + return _exportModal |
| 22 | + |
| 23 | +end |
| 24 | + |
| 25 | +function OptionModal:Configure(title, message, width, showCancel, okButtonSpriteName) |
| 26 | + |
| 27 | + self.showCancel = showCancel or false |
| 28 | + |
| 29 | + self.okButtonSpriteName = okButtonSpriteName or "ok" |
| 30 | + -- Reset the modal so it redraws correctly when opened |
| 31 | + self.firstRun = nil |
| 32 | + |
| 33 | + width = width or 96 |
| 34 | + |
| 35 | + -- Need to calculate the height ahead of time |
| 36 | + -- Draw message text |
| 37 | + local wrap = WordWrap(message, (width / 4) - 4) |
| 38 | + self.lines = SplitLines(wrap) |
| 39 | + |
| 40 | + height = #self.lines * 8 + 42 |
| 41 | + |
| 42 | + -- Make sure width and height are on the grid |
| 43 | + width = math.floor(width / 8) * 8 |
| 44 | + height = math.floor(height / 8) * 8 |
| 45 | + |
| 46 | + self.canvas = NewCanvas(width, height) |
| 47 | + |
| 48 | + local displaySize = Display() |
| 49 | + |
| 50 | + self.title = title or "Message Modal" |
| 51 | + |
| 52 | + self.rect = { |
| 53 | + x = math.floor(((displaySize.x - width) * .5) / 8) * 8, |
| 54 | + y = math.floor(((displaySize.y - height) * .5) / 8) * 8, |
| 55 | + w = width, |
| 56 | + h = height |
| 57 | + } |
| 58 | + |
| 59 | + self.selectionValue = false |
| 60 | + |
| 61 | +end |
| 62 | + |
| 63 | +function OptionModal:Open() |
| 64 | + |
| 65 | + if(self.firstRun == nil) then |
| 66 | + |
| 67 | + -- Draw the black background |
| 68 | + self.canvas:SetStroke(5, 1) |
| 69 | + self.canvas:SetPattern({0}, 1, 1) |
| 70 | + self.canvas:DrawRectangle(0, 0, self.canvas.width, self.canvas.height, true) |
| 71 | + |
| 72 | + -- Draw the brown background |
| 73 | + self.canvas:SetStroke(12, 1) |
| 74 | + self.canvas:SetPattern({11}, 1, 1) |
| 75 | + self.canvas:DrawRectangle(3, 9, self.canvas.width - 6, self.canvas.height - 12, true) |
| 76 | + |
| 77 | + local tmpX = (self.canvas.width - (#self.title * 4)) * .5 |
| 78 | + |
| 79 | + self.canvas:DrawText(self.title:upper(), tmpX, 1, "small", 15, - 4) |
| 80 | + |
| 81 | + -- draw highlight stroke |
| 82 | + self.canvas:SetStroke(15, 1) |
| 83 | + self.canvas:DrawLine(3, 9, self.canvas.width - 5, 9) |
| 84 | + self.canvas:DrawLine(3, 9, 3, self.canvas.height - 5) |
| 85 | + |
| 86 | + local total = #self.lines |
| 87 | + local startX = 8 |
| 88 | + local startY = 16 |
| 89 | + |
| 90 | + self.selectionGroupData = editorUI:CreateToggleGroup(true) |
| 91 | + |
| 92 | + self.optionGroupData = editorUI:CreateToggleGroup(false) |
| 93 | + self.optionGroupData.onAction = function(value) |
| 94 | + |
| 95 | + local selected = self.optionGroupData.buttons[value].selected |
| 96 | + |
| 97 | + print("Click value", value, selected) |
| 98 | + |
| 99 | + if(Key(Keys.LeftShift) or Key(Keys.RightShift)) then |
| 100 | + |
| 101 | + for i = 1, #self.optionGroupData.buttons do |
| 102 | + |
| 103 | + editorUI:ToggleButton(self.optionGroupData.buttons[i], value, false) |
| 104 | + |
| 105 | + end |
| 106 | + |
| 107 | + end |
| 108 | + |
| 109 | + end |
| 110 | + |
| 111 | + self.buttons = {} |
| 112 | + |
| 113 | + -- We want to render the text from the bottom of the screen so we offset it and loop backwards. |
| 114 | + for i = 1, total do |
| 115 | + |
| 116 | + local newY = (startY + ((i - 1) * 8)) |
| 117 | + |
| 118 | + local firstChar = string.sub(self.lines[i], 0, 1) |
| 119 | + |
| 120 | + -- # are check boxes |
| 121 | + if(firstChar == "#") then |
| 122 | + editorUI:ToggleGroupButton(self.optionGroupData, {x = startX + self.rect.x, y = newY + self.rect.y, w = 8, h = 8}, "checkbox", "Select" .. string.sub(self.lines[i], 3), true) |
| 123 | + |
| 124 | + -- * are radio buttons |
| 125 | + elseif(firstChar == "*") then |
| 126 | + editorUI:ToggleGroupButton(self.selectionGroupData, {x = startX + self.rect.x, y = newY + self.rect.y, w = 8, h = 8}, "radiobutton", "Select" .. string.sub(self.lines[i], 3), true) |
| 127 | + end |
| 128 | + |
| 129 | + self.canvas:DrawText(self.lines[i]:upper(), startX, newY, "medium", 0, - 4) |
| 130 | + |
| 131 | + end |
| 132 | + |
| 133 | + local buttonSize = {x = 32, y = 16} |
| 134 | + |
| 135 | + -- TODO center ok button when no cancel button is shown |
| 136 | + local bX = self.showCancel == true and (self.rect.w - buttonSize.x - 8) or ((self.rect.w - buttonSize.x) * .5) |
| 137 | + |
| 138 | + -- snap the x value to the grid |
| 139 | + bX = math.floor((bX + self.rect.x) / 8) * 8 |
| 140 | + |
| 141 | + -- Fix the button to the bottom of the window |
| 142 | + local bY = math.floor(((self.rect.y + self.rect.h) - buttonSize.y - 8) / 8) * 8 |
| 143 | + |
| 144 | + local backBtnData = editorUI:CreateButton({x = bX, y = bY}, "modal".. self.okButtonSpriteName .. "button", "") |
| 145 | + |
| 146 | + backBtnData.onAction = function() |
| 147 | + |
| 148 | + -- Set value to true when ok is pressed |
| 149 | + self.selectionValue = true |
| 150 | + |
| 151 | + if(self.onParentClose ~= nil) then |
| 152 | + self.onParentClose() |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + table.insert(self.buttons, backBtnData) |
| 157 | + |
| 158 | + if(self.showCancel) then |
| 159 | + |
| 160 | + -- Offset the bX value and snap to the grid |
| 161 | + bX = math.floor((bX - buttonSize.x - 8) / 8) * 8 |
| 162 | + |
| 163 | + local cancelBtnData = editorUI:CreateButton({x = bX, y = bY}, "modalcancelbutton", "") |
| 164 | + |
| 165 | + cancelBtnData.onAction = function() |
| 166 | + |
| 167 | + -- Set value to true when cancel is pressed |
| 168 | + self.selectionValue = false |
| 169 | + |
| 170 | + -- Close the panel |
| 171 | + if(self.onParentClose ~= nil) then |
| 172 | + self.onParentClose() |
| 173 | + end |
| 174 | + end |
| 175 | + |
| 176 | + table.insert(self.buttons, cancelBtnData) |
| 177 | + |
| 178 | + end |
| 179 | + |
| 180 | + self.firstRun = false; |
| 181 | + |
| 182 | + end |
| 183 | + |
| 184 | + for i = 1, #self.buttons do |
| 185 | + editorUI:Invalidate(self.buttons[i]) |
| 186 | + end |
| 187 | + |
| 188 | + self.canvas:DrawPixels(self.rect.x, self.rect.y, DrawMode.TilemapCache) |
| 189 | + |
| 190 | +end |
| 191 | + |
| 192 | +function OptionModal:Update(timeDelta) |
| 193 | + |
| 194 | + for i = 1, #self.buttons do |
| 195 | + editorUI:UpdateButton(self.buttons[i]) |
| 196 | + end |
| 197 | + |
| 198 | + editorUI:UpdateToggleGroup(self.optionGroupData) |
| 199 | + editorUI:UpdateToggleGroup(self.selectionGroupData) |
| 200 | + |
| 201 | + if(Key(Keys.Enter, InputState.Released)) then |
| 202 | + self.selectionValue = true |
| 203 | + self.onParentClose() |
| 204 | + elseif(Key(Keys.Escape, InputState.Released) and self.showCancel) then |
| 205 | + self.selectionValue = false |
| 206 | + self.onParentClose() |
| 207 | + end |
| 208 | + |
| 209 | +end |
0 commit comments