From 945542f3de6a649f64d0c918119aaf4b7dc6deb2 Mon Sep 17 00:00:00 2001 From: alexcoder04 Date: Wed, 26 Oct 2022 18:16:10 +0200 Subject: [PATCH] colorscheme api, library improvements --- app.lua | 11 ++++++++--- components/Canvas.lua | 2 +- components/InputField.lua | 10 +++++----- components/Rectangle.lua | 2 +- components/TextField.lua | 14 ++++++++++---- events.lua | 20 ++++++++++++++++++-- library/colors.lua | 23 +++++++++++++++++++++++ library/debug.lua | 4 ++-- library/gui.lua | 4 ++-- library/internal.lua | 2 +- 10 files changed, 71 insertions(+), 21 deletions(-) diff --git a/app.lua b/app.lua index 27662ae..80f274c 100644 --- a/app.lua +++ b/app.lua @@ -7,7 +7,11 @@ App = { License = "unknown", RefreshRate = 10, SolVersion = 0, - Gui = {}, + Gui = { + DarkMode = false, + LightColorscheme = {}, + DarkColorscheme = {} + }, Data = { Const = {}, Var = {} @@ -50,8 +54,9 @@ function App:_onElementClick() end function App:_draw(gc) - if App.Gui.Background ~= nil then - gc:setColorRGB(unpack(App.Gui.Background)) + local bg = Lib.Colors.Background() + if bg ~= {255, 255, 255} then + gc:setColorRGB(unpack(bg)) gc:fillRect(0, 0, platform.window:width(), platform.window:height()) gc:setColorRGB(0, 0, 0) end diff --git a/components/Canvas.lua b/components/Canvas.lua index 3349b7f..e018b0c 100644 --- a/components/Canvas.lua +++ b/components/Canvas.lua @@ -29,7 +29,7 @@ function Components.Base.Canvas:_draw(gc, focused) gc:setColorRGB(0, 0, 0) self:Draw(gc) if focused then - Lib.Gui:DrawFocusBox(self.PosX, self.PosY, self.Width, self.Height, gc) + Lib.Gui.DrawFocusBox(self.PosX, self.PosY, self.Width, self.Height, gc) end end diff --git a/components/InputField.lua b/components/InputField.lua index 597e4b7..bde8b66 100644 --- a/components/InputField.lua +++ b/components/InputField.lua @@ -16,7 +16,7 @@ function Components.Base.InputField:new(o) end function Components.Base.InputField:_touches(x, y) - local w, h = Lib.Gui:GetStringSize(self.Value) + local w, h = Lib.Gui.GetStringSize(self.Value) if x >= self.PosX and x <= (self.PosX + self.Width) then if y >= self.PosY and y <= (self.PosY + h) then return true @@ -33,11 +33,11 @@ end function Components.Base.InputField:_get_draw_text(focused) local dt = self.Value - local w, h = Lib.Gui:GetStringSize(dt .. self:_get_cursor(focused)) + local w, h = Lib.Gui.GetStringSize(dt .. self:_get_cursor(focused)) if w <= self.Width then return dt .. self:_get_cursor(focused) end while w > self.Width do dt = dt:sub(2) - w, h = Lib.Gui:GetStringSize("..." .. dt .. self:_get_cursor(focused)) + w, h = Lib.Gui.GetStringSize("..." .. dt .. self:_get_cursor(focused)) end return "..." .. dt .. self:_get_cursor(focused) end @@ -45,7 +45,7 @@ end function Components.Base.InputField:_draw(gc, focused) self._cursor_on = not self._cursor_on local dt = self:_get_draw_text(focused) - local w, h = Lib.Gui:GetStringSize(dt) + local w, h = Lib.Gui.GetStringSize(dt) if not focused then gc:setColorRGB(unpack(Lib.Colors.Silver)) gc:fillRect(self.PosX, self.PosY, self.Width, h) @@ -58,7 +58,7 @@ function Components.Base.InputField:_draw(gc, focused) gc:drawRect(self.PosX, self.PosY, self.Width, h) gc:setColorRGB(0, 0, 0) if focused then - Lib.Gui:DrawFocusBox(self.PosX, self.PosY, self.Width, h, gc) + Lib.Gui.DrawFocusBox(self.PosX, self.PosY, self.Width, h, gc) end end diff --git a/components/Rectangle.lua b/components/Rectangle.lua index 0b2c9cc..b4ed09b 100644 --- a/components/Rectangle.lua +++ b/components/Rectangle.lua @@ -33,6 +33,6 @@ function Components.Base.Rectangle:_draw(gc, focused) end gc:setColorRGB(0, 0, 0) if focused then - Lib.Gui:DrawFocusBox(self.PosX, self.PosY, self.Width, self.Height, gc) + Lib.Gui.DrawFocusBox(self.PosX, self.PosY, self.Width, self.Height, gc) end end diff --git a/components/TextField.lua b/components/TextField.lua index 989d55e..8fb24ac 100644 --- a/components/TextField.lua +++ b/components/TextField.lua @@ -16,7 +16,7 @@ function Components.Base.TextField:new(o) end function Components.Base.TextField:_touches(x, y) - local w, h = Lib.Gui:GetStringSize(self.Label) + local w, h = Lib.Gui.GetStringSize(self.Label) if x >= self.PosX and x <= (self.PosX + w) then if y >= self.PosY and y <= (self.PosY + h) then return true @@ -26,16 +26,22 @@ function Components.Base.TextField:_touches(x, y) end function Components.Base.TextField:_draw(gc, focused) - gc:setColorRGB(unpack(self.Color)) + local c + if type(self.Color) == "function" then + c = self.Color() + else + c = self.Color + end + gc:setColorRGB(unpack(c)) gc:setFont("sansserif", "r", self.FontSize) gc:drawString(self.Label, self.PosX, self.PosY, "top") gc:setFont("sansserif", "r", 12) - local w, h = Lib.Gui:GetStringSize(self.Label) + local w, h = Lib.Gui.GetStringSize(self.Label) if self.Border then gc:drawRect(self.PosX, self.PosY, w, h) end gc:setColorRGB(0, 0, 0) if focused then - Lib.Gui:DrawFocusBox(self.PosX, self.PosY, w, h, gc) + Lib.Gui.DrawFocusBox(self.PosX, self.PosY, w, h, gc) end end diff --git a/events.lua b/events.lua index b77bc1f..b054117 100644 --- a/events.lua +++ b/events.lua @@ -1,11 +1,24 @@ function on.restore(state) - App.Data.Var = state + App.Data.Var = state.data + App.Gui.DarkMode = state.darkMode end function on.construction() math.randomseed(timer:getMilliSecCounter()) timer.start(App.RefreshRate) + App.Gui.LightColorscheme = { + Background = Lib.Colors.White, + Foreground = Lib.Colors.Black, + Secondary = Lib.Colors.Grey, + Accent = Lib.Colors.Blue + } + App.Gui.DarkColorscheme = { + Background = Lib.Colors.Black, + Foreground = Lib.Colors.White, + Secondary = Lib.Colors.Silver, + Accent = Lib.Colors.Blue + } if init ~= nil then init() end @@ -97,5 +110,8 @@ function on.timer() end function on.save() - return App.Data.Var + return { + data = App.Data.Var, + darkMode = App.Gui.DarkMode + } end diff --git a/library/colors.lua b/library/colors.lua index e23bdad..babfb35 100644 --- a/library/colors.lua +++ b/library/colors.lua @@ -33,3 +33,26 @@ Lib.Colors = { White = { 255, 255, 255 }, Yellow = { 255, 255, 0 } } + +function _get_color_mode() + if App.Gui.DarkMode then + return "DarkColorscheme" + end + return "LightColorscheme" +end + +function Lib.Colors.Background() + return App.Gui[_get_color_mode()].Background +end + +function Lib.Colors.Foreground() + return App.Gui[_get_color_mode()].Foreground +end + +function Lib.Colors.Secondary() + return App.Gui[_get_color_mode()].Secondary +end + +function Lib.Colors.Accent() + return App.Gui[_get_color_mode()].Accent +end diff --git a/library/debug.lua b/library/debug.lua index 5d666c4..75a9fa0 100644 --- a/library/debug.lua +++ b/library/debug.lua @@ -2,12 +2,12 @@ Lib.Debug.Buffer = nil Lib.Debug.FlashRedraws = false -function Lib.Debug:Print(message) +function Lib.Debug.Print(message) Lib.Debug.Buffer = message platform.window:invalidate() end -function Lib.Debug:UnPrint() +function Lib.Debug.UnPrint() Lib.Debug.Buffer = nil platform.window:invalidate() end diff --git a/library/gui.lua b/library/gui.lua index b4e90bd..3823e92 100644 --- a/library/gui.lua +++ b/library/gui.lua @@ -3,11 +3,11 @@ function _getStringSize(str, gc) return gc:getStringWidth(str), gc:getStringHeight(str) end -function Lib.Gui:GetStringSize(str) +function Lib.Gui.GetStringSize(str) return platform.withGC(_getStringSize, str) end -function Lib.Gui:DrawFocusBox(x, y, w, h, gc) +function Lib.Gui.DrawFocusBox(x, y, w, h, gc) gc:setColorRGB(unpack(Lib.Colors.Blue)) gc:drawRect(x, y, w, h) gc:setColorRGB(0, 0, 0) diff --git a/library/internal.lua b/library/internal.lua index 08cd9b6..4492bdd 100644 --- a/library/internal.lua +++ b/library/internal.lua @@ -1,4 +1,4 @@ -function Lib.Internal:ShowAboutDialog() +function Lib.Internal.ShowAboutDialog() Lib.Debug:Print("About Dialog not implemented yet") end