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)
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 x = (1-math.abs(h%2-1))*c
local m,r,b,g = (l-.5*c), 0,0,0
@ -47,7 +47,20 @@ function color.hsl(h, s, l)
elseif h < 4 then r,b,g = 0,x,c
elseif h < 5 then r,b,g = x,0,c
else r,b,g = c,0,x
end return (r+m)*255,(g+m)*255,(b+m)*255
end return (r+m)*255, (g+m)*255, (b+m)*255
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)
@ -56,7 +69,16 @@ function color.isLight(r, g, b)
elseif type(r) == "table" then
r, g, b = r[1], r[2], r[3]
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
function color.rgbToHex(r, g, b)

View File

@ -3,32 +3,51 @@ local theme = {}
local defaultFont = love.graphics.getFont()
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 colors = {}
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
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
return colors
end
function theme:random(seed)
function theme:random(seed, lightness, rand)
local seed = seed or math.random(0,9999999999)
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))
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.colorButtonNormal = color.lighten(t.colorButtonNormal, .2)
else
t.colorPrimaryText = color.darken(t.colorPrimaryText, .3)
end
if not color.isLight(t.colorButtonNormal) then
if color.getAverageLightness(t.colorPrimary)<.5 then
t.colorButtonText = color.lighten(t.colorButtonText, .5)
else
t.colorButtonText = color.darken(t.colorButtonText, .3)
end
end
t.seed = seed
return t
end

View File

@ -142,7 +142,7 @@ updater:newThread("GUI Hotkey Manager",function()
end
end)
function gui:SetHotKey(keys, conn)
function gui:setHotKey(keys, conn)
has_hotkey = true
local conn = conn or multi:newConnection():fastMode()
table.insert(hot_keys, {Ref=self, Connection = conn, Keys = {unpack(keys)}})
@ -153,27 +153,27 @@ end
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
gui.HotKeys.OnSelectAll = gui:SetHotKey({"lctrl","a"})
+ gui:SetHotKey({"rctrl","a"})
gui.HotKeys.OnSelectAll = gui:setHotKey({"lctrl","a"})
+ gui:setHotKey({"rctrl","a"})
gui.HotKeys.OnCopy = gui:SetHotKey({"lctrl","c"})
+ gui:SetHotKey({"rctrl","c"})
gui.HotKeys.OnCopy = gui:setHotKey({"lctrl","c"})
+ gui:setHotKey({"rctrl","c"})
gui.HotKeys.OnPaste = gui:SetHotKey({"lctrl","v"})
+ gui:SetHotKey({"rctrl","v"})
gui.HotKeys.OnPaste = gui:setHotKey({"lctrl","v"})
+ gui:setHotKey({"rctrl","v"})
gui.HotKeys.OnCut = gui:SetHotKey({"lctrl","x"})
+ gui:SetHotKey({"rctrl","x"})
gui.HotKeys.OnCut = gui:setHotKey({"lctrl","x"})
+ gui:setHotKey({"rctrl","x"})
gui.HotKeys.OnUndo = gui:SetHotKey({"lctrl","z"})
+ gui:SetHotKey({"rctrl","z"})
gui.HotKeys.OnUndo = gui:setHotKey({"lctrl","z"})
+ gui:setHotKey({"rctrl","z"})
gui.HotKeys.OnRedo = gui:SetHotKey({"lctrl","y"})
+ gui:SetHotKey({"rctrl","y"})
+ gui:SetHotKey({"lctrl", "lshift", "z"})
+ gui:SetHotKey({"rctrl", "lshift", "z"})
+ gui:SetHotKey({"lctrl", "rshift", "z"})
+ gui:SetHotKey({"rctrl", "rshift", "z"})
gui.HotKeys.OnRedo = gui:setHotKey({"lctrl","y"})
+ gui:setHotKey({"rctrl","y"})
+ gui:setHotKey({"lctrl", "lshift", "z"})
+ gui:setHotKey({"rctrl", "lshift", "z"})
+ gui:setHotKey({"lctrl", "rshift", "z"})
+ gui:setHotKey({"rctrl", "rshift", "z"})
-- 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
end
function gui:getAllChildren(callback)
local Stuff = {}
function Seek(Items)
for i=1,#Items do
if Items[i].visible==true then
if callback then callback(Items[i]) end
table.insert(Stuff,Items[i])
local NItems = Items[i]:getChildren()
if NItems ~= nil then
Seek(NItems)
function gui:getAllChildren(vis)
local children = self:getChildren()
local allChildren = {}
for i, child in ipairs(children) do
if not(vis) and child.visible == true then
allChildren[#allChildren + 1] = child
local grandChildren = child:getAllChildren()
for j, grandChild in ipairs(grandChildren) do
allChildren[#allChildren + 1] = grandChild
end
end
end
end
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
return allChildren
end
function gui:newThread(func)
@ -562,6 +549,16 @@ function gui:newBase(typ,x, y, w, h, sx, sy, sw, sh, virtual)
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)
hierarchy = bool
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 fh = Font:getHeight()
self.textOffsetY = floor(((height-bottom) - top)/2)--(height-(bottom - top))/2
print(self.text,top,bottom,self.textOffsetY)
self.OnFontUpdated:Fire(self)
return s - (4+(n or 0))
end
@ -1243,6 +1239,8 @@ local draw_handler = function(child)
local type = child.type
local vis = child.visibility
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.y = y
child.w = w
@ -1267,9 +1265,9 @@ local draw_handler = function(child)
-- Set color
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.rectangle("line", x, y, w, h--[[, rx, ry, segments]])
love.graphics.rectangle("line", x, y, w, h, rx, ry, segments)
-- Start object specific stuff
drawtypes[band(type,video)](child,x,y,w,h)