-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
501 lines (379 loc) · 13.6 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
--[[
* main.lua
*
]]
local wx = require("wx")
local hsl = require("lib.hsl")
local rybclr = require("lib.RYBColours")
local Canvas = require("lib.canvas")
local Pyramid = require("lib.pyramid")
local Palette = require("lib.palette")
local Ribbon = require("lib.ribbon")
local Sketch = require("lib.sketch")
local Stack = require("lib.stack")
local Scope = require("lib.scope")
local trace = require("lib.trace")
local _frmt = string.format
local _floor = math.floor
local _colours = rybclr.tColours
local m_Sketch = Sketch
local m_Stack = Stack
local m_Scope = Scope
-- ----------------------------------------------------------------------------
--
local m_trace = trace.new("wxHSL")
-- ----------------------------------------------------------------------------
--
local m_App =
{
sAppVersion = "0.0.3", -- application's version
sAppName = "Tavolozza", -- name for the application
sRelDate = "2021/08/16",
bLocked = false,
tPalette = nil,
tControls = { },
tRibbon = nil,
tForeColour = _colours._Green,
tBackColour = _colours.__Magenta,
}
_G.m_App = m_App -- make it globally visible
-- ----------------------------------------------------------------------------
-- default dialog size and position
--
local m_tDefWinProp =
{
window_xy = {20, 20}, -- top, left
window_wh = {990, 580}, -- width, height
use_font = {12, "Calibri"}, -- font for grid and tab
dpi_scale = 1.0, -- scaling
}
-- ----------------------------------------------------------------------------
-- default controls' height
--
local m_tSizes =
{
iPyramid = 300,
iPalette = 150,
iRibbon = 40,
iRadius = 150,
}
-- ----------------------------------------------------------------------------
--
local m_Mainframe =
{
hWindow = nil, -- main frame
hCanvas = nil, -- panel
tWinProps = m_tDefWinProp, -- window layout settings
}
-- ----------------------------------------------------------------------------
-- create a filename just for the machine running on
--
local function SettingsName()
return "palette@" .. wx.wxGetHostName() .. ".ini"
end
-- ----------------------------------------------------------------------------
-- read dialogs' settings from settings file
--
local function ReadSettings()
-- m_trace:line("ReadSettings")
local sFilename = SettingsName()
local fd = io.open(sFilename, "r")
if not fd then return end
fd:close()
local tSettings = dofile(sFilename)
if tSettings then m_Mainframe.tWinProps = tSettings end
end
-- ----------------------------------------------------------------------------
-- save a table to the settings file
--
local function SaveSettings()
-- m_trace:line("SaveSettings")
local fd = io.open(SettingsName(), "w")
if not fd then return end
fd:write("local window_ini =\n{\n")
local tWinProps = m_Mainframe.tWinProps
local sLine
sLine = _frmt("\twindow_xy\t= {%d, %d},\n", tWinProps.window_xy[1], tWinProps.window_xy[2])
fd:write(sLine)
sLine = _frmt("\twindow_wh\t= {%d, %d},\n", tWinProps.window_wh[1], tWinProps.window_wh[2])
fd:write(sLine)
sLine = _frmt("\tuse_font\t= {%d, \"%s\"},\n", tWinProps.use_font[1], tWinProps.use_font[2])
fd:write(sLine)
sLine = _frmt("\tdpi_scale\t= %.2f,\n", tWinProps.dpi_scale)
fd:write(sLine)
fd:write("}\n\nreturn window_ini\n")
io.close(fd)
end
-- ----------------------------------------------------------------------------
-- when a new foreground color is selected
--
local function OnForeColourChanged(inColour, inFunction)
-- m_trace:line("OnForeColourChanged")
if not inColour then return end
m_trace:line("Current foreground: " .. inColour:toString())
if not m_App.bLocked then
if "Ribbon" ~= inFunction then
m_App.tRibbon:SetColours(inColour:offset(180.0), inColour)
end
local tControls = m_App.tControls
for i=1, #tControls do
tControls[i]:SetColours(inColour:offset(180.0), inColour)
end
end
-- update the sketch dialog
--
m_Sketch.SetIndex(2) -- make a choice
m_Sketch.SetColour(inColour) -- apply color
m_Stack.SetColour(inColour) -- foreground variations
m_Scope.SetColour(inColour) -- background variations
-- store and apply
--
m_App.tForeColour = inColour
m_Mainframe.hCanvas:SetForeColour(inColour)
end
-- ----------------------------------------------------------------------------
-- when a new background color is selected
--
local function OnBackColourChanged(inColour, inFunction)
-- m_trace:line("OnBackColourChanged")
if not inColour then return end
m_trace:line("Current background: " .. inColour:toString())
-- update the sketch dialog
--
m_Sketch.SetIndex(1) -- make a choice
m_Sketch.SetColour(inColour) -- apply color
-- store and apply
--
m_App.tBackColour = inColour
m_Mainframe.hCanvas:SetBackColour(inColour)
end
-- ----------------------------------------------------------------------------
-- allows selection but dows not propagate it
--
local function OnEditLock()
-- m_trace:line("OnEditLock")
-- toggle state
--
m_App.bLocked = not m_App.bLocked
-- set a check mark in the related menu
--
local hMenu = m_Mainframe.hWindow:GetMenuBar()
local iItem = hMenu:FindMenuItem("Edit", "Lock")
if 0 < iItem then hMenu:Check(iItem, m_App.bLocked) end
end
-- ----------------------------------------------------------------------------
-- Simple interface to pop up a message
--
local function DlgMessage(inMessage)
wx.wxMessageBox(inMessage, m_App.sAppName,
wx.wxOK + wx.wxICON_INFORMATION, m_Mainframe.hWindow)
end
-- ----------------------------------------------------------------------------
-- Generate a unique new wxWindowID
--
local ID_IDCOUNTER = wx.wxID_HIGHEST + 1
local NewMenuID = function()
ID_IDCOUNTER = ID_IDCOUNTER + 1
return ID_IDCOUNTER
end
-- ----------------------------------------------------------------------------
--
local function OnImport()
-- m_trace:line("OnImport")
end
-- ----------------------------------------------------------------------------
--
local function OnSave()
-- m_trace:line("OnSave")
end
-- ----------------------------------------------------------------------------
--
local function OnAbout()
DlgMessage(_frmt( "%s [%s] Rel. date [%s]\n %s, %s, %s",
m_App.sAppName, m_App.sAppVer, m_App.sRelDate,
_VERSION, wxlua.wxLUA_VERSION_STRING, wx.wxVERSION_STRING))
end
-- ----------------------------------------------------------------------------
-- called when closing the window
--
local function OnCloseMainframe()
-- m_trace:line("OnCloseMainframe")
if not m_Mainframe.hWindow then return end
m_Sketch.CloseSketch()
m_Stack.CloseStack()
m_Scope.CloseScope()
-- need to convert from size to pos
--
local pos = m_Mainframe.hWindow:GetPosition()
local size = m_Mainframe.hWindow:GetSize()
-- update the current settings
--
local tWinProps = { }
tWinProps.window_xy = {pos:GetX(), pos:GetY()}
tWinProps.window_wh = {size:GetWidth(), size:GetHeight()}
tWinProps.use_font = m_Mainframe.tWinProps.use_font -- just copy over
tWinProps.dpi_scale = m_Mainframe.tWinProps.dpi_scale -- " " " "
m_Mainframe.tWinProps = tWinProps -- switch structures
SaveSettings() -- write to file
m_Mainframe.hWindow.Destroy(m_Mainframe.hWindow)
m_Mainframe.hWindow = nil
end
-- ----------------------------------------------------------------------------
--
local function OnSize(event)
-- m_trace:line("OnSize")
local sizeWin = m_Mainframe.hWindow:GetClientRect()
if not next(m_App.tControls) then
event:Skip()
return
end
local iRadius = m_tSizes.iRadius
local iDiameter = iRadius * 2
local iSpace = _floor((sizeWin:GetWidth() - (iDiameter * 3)) / 4)
local iOffset = iSpace + iRadius
for i, pyramid in next, m_App.tControls do
pyramid:SetOrigin((iSpace + iDiameter) * (i - 1) + iOffset, m_tSizes.iPalette + iRadius + 10)
end
m_App.tPalette:SetSize(sizeWin:GetWidth(), m_tSizes.iPalette)
m_App.tRibbon:SetSize(sizeWin:GetWidth(), m_tSizes.iRibbon)
m_App.tRibbon:SetTopLeft(0, sizeWin:GetHeight() - m_tSizes.iRibbon - 10)
event:Skip() -- let the event fall through
end
-- ----------------------------------------------------------------------------
-- DPI chnaged
--
local function OnDPIChanged(event)
m_trace:line("OnDPIChanged")
event:Skip() -- let the event fall through
end
-- ----------------------------------------------------------------------------
-- scale the default height of objects
--
local function Scale()
local dScale = m_Mainframe.tWinProps.dpi_scale
m_tSizes.iPyramid = _floor(m_tSizes.iPyramid * dScale)
m_tSizes.iPalette = _floor(m_tSizes.iPalette * dScale)
m_tSizes.iRibbon = _floor(m_tSizes.iRibbon * dScale)
m_tSizes.iRadius = _floor(m_tSizes.iRadius * dScale)
end
-- ----------------------------------------------------------------------------
-- create a window
--
local function CreateMainFrame(inAppTitle)
-- m_trace:line("CreateMainFrame")
ReadSettings()
Scale()
local tWinProps = m_Mainframe.tWinProps
local pos = tWinProps.window_xy
local size = tWinProps.window_wh
-- create the frame
--
local frame = wx.wxFrame(wx.NULL, wx.wxID_ANY, inAppTitle,
wx.wxPoint(pos[1], pos[2]), wx.wxSize(size[1], size[2]))
frame:SetMinSize(wx.wxSize(300, 250))
-- ------------------------------------------------------------------------
-- create the menus
--
local rcMnuImportFile = NewMenuID()
local rcMnuSaveFile = NewMenuID()
local rcMnuEdLock = NewMenuID()
local rcMnuEdAltBack = NewMenuID()
local mnuFile = wx.wxMenu("", wx.wxMENU_TEAROFF)
mnuFile:Append(rcMnuImportFile, "Import\tCtrl-I", "Read the settings file")
mnuFile:Append(rcMnuSaveFile, "Save\tCtrl-S", "Write the settings file")
mnuFile:AppendSeparator()
mnuFile:Append(wx.wxID_EXIT, "E&xit\tAlt-X", "Quit the program")
local mnuEdit = wx.wxMenu("", wx.wxMENU_TEAROFF)
mnuEdit:Append(rcMnuEdLock, "Lock\tCtrl-L", "Locks current color palette", wx.wxITEM_CHECK)
local mnuHelp = wx.wxMenu("", wx.wxMENU_TEAROFF)
mnuHelp:Append(wx.wxID_ABOUT, "&About", "About the application")
-- create the menu bar and associate sub-menus
--
local mnuBar = wx.wxMenuBar()
mnuBar:Append(mnuFile, "&File")
mnuBar:Append(mnuEdit, "&Edit")
mnuBar:Append(mnuHelp, "&Help")
frame:SetMenuBar(mnuBar)
-- create the canvas
--
local canvas = Canvas.new()
canvas:CreateCanvas(frame)
-- assign event handlers for this frame
--
frame:Connect(wx.wxEVT_SIZE, OnSize)
frame:Connect(wx.wxEVT_CLOSE_WINDOW, OnCloseMainframe)
frame:Connect(wx.wxEVT_DPI_CHANGED, OnDPIChanged)
-- menu event handlers
--
frame:Connect(rcMnuImportFile, wx.wxEVT_COMMAND_MENU_SELECTED, OnImport)
frame:Connect(rcMnuSaveFile, wx.wxEVT_COMMAND_MENU_SELECTED, OnSave)
frame:Connect(rcMnuEdLock, wx.wxEVT_COMMAND_MENU_SELECTED, OnEditLock)
frame:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, OnCloseMainframe)
frame:Connect(wx.wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED, OnAbout)
-- assign an icon to frame
--
local icon = wx.wxIcon("lib/icons/Tavolozza.ico", wx.wxBITMAP_TYPE_ICO)
frame:SetIcon(icon)
-- store interesting members
--
m_Mainframe.hWindow = frame
m_Mainframe.hCanvas = canvas
return true
end
-- ----------------------------------------------------------------------------
--
local function Layout()
local palette = Palette.new()
local ribbon = Ribbon.new("Luminance")
-- inFunction, inRadius, inOctagons, inType
--
local pyramid1 = Pyramid.new("Offset", m_tSizes.iRadius, 5, 8)
local pyramid2 = Pyramid.new("Saturation", m_tSizes.iRadius, 2, 6)
local pyramid3 = Pyramid.new("Luminance", m_tSizes.iRadius, 3, 10)
m_App.tPalette = palette
m_App.tControls = {pyramid1, pyramid2, pyramid3}
m_App.tRibbon = ribbon
m_Mainframe.hCanvas:AddObject(m_App.tPalette)
m_Mainframe.hCanvas:AddObject(m_App.tRibbon)
m_Mainframe.hCanvas:AddObject(m_App.tControls[1])
m_Mainframe.hCanvas:AddObject(m_App.tControls[2])
m_Mainframe.hCanvas:AddObject(m_App.tControls[3])
end
-- ----------------------------------------------------------------------------
--
local function RunApplication()
local sAppTitle = m_App.sAppName .. " [" .. m_App.sAppVersion .. "]"
m_trace:open()
m_trace:time(sAppTitle .. " started")
assert(os.setlocale('us', 'all'))
m_trace:line("Current locale is [" .. os.setlocale() .. "]")
wx.wxGetApp():SetAppName(sAppTitle)
if CreateMainFrame(sAppTitle) and
m_Sketch.CreateSketch() and
m_Stack.CreateStack() and
m_Scope.CreateScope() then
Layout()
m_Mainframe.hWindow:Show(true)
m_Sketch.hWindow:Show(true)
m_Stack.hWindow:Show(true)
m_Scope.hWindow:Show(true)
OnBackColourChanged(m_App.tBackColour)
OnForeColourChanged(m_App.tForeColour)
wx.wxGetApp():SetTopWindow(m_Mainframe.hWindow)
wx.wxGetApp():MainLoop()
end
m_trace:newline(sAppTitle .. " terminated ###")
m_trace:close()
end
-- ----------------------------------------------------------------------------
--
local function SetupPublic()
m_App.ForeColourChanged = OnForeColourChanged
m_App.BackColourChanged = OnBackColourChanged
end
-- ----------------------------------------------------------------------------
--
SetupPublic()
RunApplication()
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------