Skip to content

Commit

Permalink
colorscheme api, library improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcoder04 committed Oct 26, 2022
1 parent 3d7945e commit 945542f
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 21 deletions.
11 changes: 8 additions & 3 deletions app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ App = {
License = "unknown",
RefreshRate = 10,
SolVersion = 0,
Gui = {},
Gui = {
DarkMode = false,
LightColorscheme = {},
DarkColorscheme = {}
},
Data = {
Const = {},
Var = {}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/Canvas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions components/InputField.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,19 +33,19 @@ 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

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)
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion components/Rectangle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 10 additions & 4 deletions components/TextField.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
20 changes: 18 additions & 2 deletions events.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
23 changes: 23 additions & 0 deletions library/colors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions library/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions library/gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion library/internal.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

function Lib.Internal:ShowAboutDialog()
function Lib.Internal.ShowAboutDialog()
Lib.Debug:Print("About Dialog not implemented yet")
end

0 comments on commit 945542f

Please sign in to comment.