Asked chatgpt to rewrite the gui:getAllChildren() method. I had to edit it a bit for visibility testing, but it did a great job!

This commit is contained in:
Ryan Ward 2023-01-16 12:38:39 -05:00
parent afa6108e1a
commit c58a79e275
3 changed files with 99 additions and 60 deletions

View File

@ -37,7 +37,7 @@ local mt = {
function color.hsl(h, s, l) function color.hsl(h, s, l)
if s<=0 then return l,l,l end if s<=0 then return l,l,l end
h, s, l = h/256*6, s/255, l/255 h, s, l = h/360*6, s/100, l/100
local c = (1-math.abs(2*l-1))*s local c = (1-math.abs(2*l-1))*s
local x = (1-math.abs(h%2-1))*c local x = (1-math.abs(h%2-1))*c
local m,r,b,g = (l-.5*c), 0,0,0 local m,r,b,g = (l-.5*c), 0,0,0
@ -50,13 +50,35 @@ function color.hsl(h, s, l)
end return (r+m)*255, (g+m)*255, (b+m)*255 end return (r+m)*255, (g+m)*255, (b+m)*255
end end
min = math.min
max = math.max
abs = math.abs
function color.hsv(h, s, v)
local k1 = v*(1-s)
local k2 = v - k1
local r = min (max (3*abs (((h )/180)%2-1)-1, 0), 1)
local g = min (max (3*abs (((h -120)/180)%2-1)-1, 0), 1)
local b = min (max (3*abs (((h +120)/180)%2-1)-1, 0), 1)
return k1 + k2 * r, k1 + k2 * g, k1 + k2 * b
end
function color.isLight(r, g, b) function color.isLight(r, g, b)
if type(r) == "string" then if type(r) == "string" then
r, g, b = tonumber(string.sub(r,1,2),16),tonumber(string.sub(r,3,4),16),tonumber(string.sub(r,5,6),16) r, g, b = tonumber(string.sub(r,1,2),16),tonumber(string.sub(r,3,4),16),tonumber(string.sub(r,5,6),16)
elseif type(r) == "table" then elseif type(r) == "table" then
r, g, b = r[1], r[2], r[3] r, g, b = r[1], r[2], r[3]
end end
return (r + g + b) / 3 >= 128 return (r + g + b) / 3 >= .5
end
function color.getAverageLightness(r, g, b)
if type(r) == "string" then
r, g, b = tonumber(string.sub(r,1,2),16),tonumber(string.sub(r,3,4),16),tonumber(string.sub(r,5,6),16)
elseif type(r) == "table" then
r, g, b = r[1], r[2], r[3]
end
return (r + g + b) / 3
end end
function color.rgbToHex(r, g, b) function color.rgbToHex(r, g, b)

View File

@ -3,32 +3,51 @@ local theme = {}
local defaultFont = love.graphics.getFont() local defaultFont = love.graphics.getFont()
theme.__index = theme theme.__index = theme
local function generate_harmonious_colors(num_colors) local function generate_harmonious_colors(num_colors, lightness)
local base_hue = math.random(0, 360) -- random starting hue local base_hue = math.random(0, 360) -- random starting hue
local colors = {} local colors = {}
for i = 1, num_colors do for i = 1, num_colors do
local new_hue = (base_hue + (360 / num_colors) * i) % 360 -- offset hue by 1/n of the color wheel local new_hue = (base_hue + (360 / num_colors) * i) % 360 -- offset hue by 1/n of the color wheel
table.insert(colors, color.new(color.hsl(new_hue, math.random(45, 110), math.random(30, 80)))) if lightness == "dark" then
table.insert(colors, color.new(color.hsl(new_hue, math.random(45, 55), math.random(30, 40))))
elseif lightness == "light" then
table.insert(colors, color.new(color.hsl(new_hue, math.random(45, 55), math.random(60, 80))))
else
table.insert(colors, color.new(color.hsl(new_hue, math.random(45, 55), math.random(30, 80))))
end
end end
return colors return colors
end end
function theme:random(seed) function theme:random(seed, lightness, rand)
local seed = seed or math.random(0,9999999999) local seed = seed or math.random(0,9999999999)
math.randomseed(seed) math.randomseed(seed)
local harmonious_colors = generate_harmonious_colors(3) local harmonious_colors = generate_harmonious_colors(3, lightness)
local t = theme:new(unpack(harmonious_colors)) local t = theme:new(unpack(harmonious_colors))
if not color.isLight(t.colorPrimary) then
if lightness == "dark" then
t.colorPrimaryText = color.lighten(t.colorPrimaryText, .8)
t.colorButtonText = color.lighten(t.colorButtonText, .7)
elseif lightness == "light" then
t.colorPrimaryText = color.darken(t.colorPrimaryText, .8)
t.colorButtonText = color.darken(t.colorButtonText, .7)
else
if color.getAverageLightness(t.colorPrimary)<.5 then
t.colorPrimaryText = color.lighten(t.colorPrimaryText, .5) t.colorPrimaryText = color.lighten(t.colorPrimaryText, .5)
t.colorButtonNormal = color.lighten(t.colorButtonNormal, .2) t.colorButtonNormal = color.lighten(t.colorButtonNormal, .2)
else else
t.colorPrimaryText = color.darken(t.colorPrimaryText, .3) t.colorPrimaryText = color.darken(t.colorPrimaryText, .3)
end end
if not color.isLight(t.colorButtonNormal) then
if color.getAverageLightness(t.colorPrimary)<.5 then
t.colorButtonText = color.lighten(t.colorButtonText, .5) t.colorButtonText = color.lighten(t.colorButtonText, .5)
else else
t.colorButtonText = color.darken(t.colorButtonText, .3) t.colorButtonText = color.darken(t.colorButtonText, .3)
end end
end
t.seed = seed t.seed = seed
return t return t
end end

View File

@ -142,7 +142,7 @@ updater:newThread("GUI Hotkey Manager",function()
end end
end) end)
function gui:SetHotKey(keys, conn) function gui:setHotKey(keys, conn)
has_hotkey = true has_hotkey = true
local conn = conn or multi:newConnection():fastMode() local conn = conn or multi:newConnection():fastMode()
table.insert(hot_keys, {Ref=self, Connection = conn, Keys = {unpack(keys)}}) table.insert(hot_keys, {Ref=self, Connection = conn, Keys = {unpack(keys)}})
@ -153,27 +153,27 @@ end
gui.HotKeys = {} gui.HotKeys = {}
-- Connections can be added together to create an OR logic to them, they can be multiplied together to create an AND logic to them -- Connections can be added together to create an OR logic to them, they can be multiplied together to create an AND logic to them
gui.HotKeys.OnSelectAll = gui:SetHotKey({"lctrl","a"}) gui.HotKeys.OnSelectAll = gui:setHotKey({"lctrl","a"})
+ gui:SetHotKey({"rctrl","a"}) + gui:setHotKey({"rctrl","a"})
gui.HotKeys.OnCopy = gui:SetHotKey({"lctrl","c"}) gui.HotKeys.OnCopy = gui:setHotKey({"lctrl","c"})
+ gui:SetHotKey({"rctrl","c"}) + gui:setHotKey({"rctrl","c"})
gui.HotKeys.OnPaste = gui:SetHotKey({"lctrl","v"}) gui.HotKeys.OnPaste = gui:setHotKey({"lctrl","v"})
+ gui:SetHotKey({"rctrl","v"}) + gui:setHotKey({"rctrl","v"})
gui.HotKeys.OnCut = gui:SetHotKey({"lctrl","x"}) gui.HotKeys.OnCut = gui:setHotKey({"lctrl","x"})
+ gui:SetHotKey({"rctrl","x"}) + gui:setHotKey({"rctrl","x"})
gui.HotKeys.OnUndo = gui:SetHotKey({"lctrl","z"}) gui.HotKeys.OnUndo = gui:setHotKey({"lctrl","z"})
+ gui:SetHotKey({"rctrl","z"}) + gui:setHotKey({"rctrl","z"})
gui.HotKeys.OnRedo = gui:SetHotKey({"lctrl","y"}) gui.HotKeys.OnRedo = gui:setHotKey({"lctrl","y"})
+ gui:SetHotKey({"rctrl","y"}) + gui:setHotKey({"rctrl","y"})
+ gui:SetHotKey({"lctrl", "lshift", "z"}) + gui:setHotKey({"lctrl", "lshift", "z"})
+ gui:SetHotKey({"rctrl", "lshift", "z"}) + gui:setHotKey({"rctrl", "lshift", "z"})
+ gui:SetHotKey({"lctrl", "rshift", "z"}) + gui:setHotKey({"lctrl", "rshift", "z"})
+ gui:SetHotKey({"rctrl", "rshift", "z"}) + gui:setHotKey({"rctrl", "rshift", "z"})
-- Utils -- Utils
@ -251,32 +251,19 @@ function gui:getAbsolutes() -- returns x, y, w, h
(self.parent.h*self.dualDim.scale.size.y)+self.dualDim.offset.size.y (self.parent.h*self.dualDim.scale.size.y)+self.dualDim.offset.size.y
end end
function gui:getAllChildren(callback) function gui:getAllChildren(vis)
local Stuff = {} local children = self:getChildren()
function Seek(Items) local allChildren = {}
for i=1,#Items do for i, child in ipairs(children) do
if Items[i].visible==true then if not(vis) and child.visible == true then
if callback then callback(Items[i]) end allChildren[#allChildren + 1] = child
table.insert(Stuff,Items[i]) local grandChildren = child:getAllChildren()
local NItems = Items[i]:getChildren() for j, grandChild in ipairs(grandChildren) do
if NItems ~= nil then allChildren[#allChildren + 1] = grandChild
Seek(NItems)
end end
end end
end end
end return allChildren
local Objs = self:getChildren()
for i=1,#Objs do
if Objs[i].visible==true then
if callback then callback(Objs[i]) end
table.insert(Stuff, Objs[i])
local Items = Objs[i]:getChildren()
if Items ~= nil then
Seek(Items)
end
end
end
return Stuff
end end
function gui:newThread(func) function gui:newThread(func)
@ -562,6 +549,16 @@ function gui:newBase(typ,x, y, w, h, sx, sy, sw, sh, virtual)
end end
end) end)
function c:setRoundness(rx,ry,seg)
self.roundness = true
self.__rx, self.__ry, self.__segments = rx or 5, ry or 5, seg or 30
end
function c:setRoundnessDirection(hori, vert)
self.__rhori = hori
self.__rvert = vert
end
function c:respectHierarchy(bool) function c:respectHierarchy(bool)
hierarchy = bool hierarchy = bool
end end
@ -758,7 +755,6 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh)
local top, bottom = self:calculateFontOffset(Font, 0) local top, bottom = self:calculateFontOffset(Font, 0)
local fh = Font:getHeight() local fh = Font:getHeight()
self.textOffsetY = floor(((height-bottom) - top)/2)--(height-(bottom - top))/2 self.textOffsetY = floor(((height-bottom) - top)/2)--(height-(bottom - top))/2
print(self.text,top,bottom,self.textOffsetY)
self.OnFontUpdated:Fire(self) self.OnFontUpdated:Fire(self)
return s - (4+(n or 0)) return s - (4+(n or 0))
end end
@ -1243,6 +1239,8 @@ local draw_handler = function(child)
local type = child.type local type = child.type
local vis = child.visibility local vis = child.visibility
local x, y, w, h = child:getAbsolutes() local x, y, w, h = child:getAbsolutes()
local roundness = child.roundness
local rx, ry, segments = child.__rx, child.__ry, child.__segments
child.x = x child.x = x
child.y = y child.y = y
child.w = w child.w = w
@ -1267,9 +1265,9 @@ local draw_handler = function(child)
-- Set color -- Set color
love.graphics.setColor(bg[1],bg[2],bg[3],vis) love.graphics.setColor(bg[1],bg[2],bg[3],vis)
love.graphics.rectangle("fill", x, y, w, h--[[, rx, ry, segments]]) love.graphics.rectangle("fill", x, y, w, h, rx, ry, segments)
love.graphics.setColor(bbg[1],bbg[2],bbg[3],vis) love.graphics.setColor(bbg[1],bbg[2],bbg[3],vis)
love.graphics.rectangle("line", x, y, w, h--[[, rx, ry, segments]]) love.graphics.rectangle("line", x, y, w, h, rx, ry, segments)
-- Start object specific stuff -- Start object specific stuff
drawtypes[band(type,video)](child,x,y,w,h) drawtypes[band(type,video)](child,x,y,w,h)