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