Threaded image loading

This commit is contained in:
Ryan Ward 2023-01-17 23:45:55 -05:00
parent 80e678ce35
commit 946fd79e37
3 changed files with 165 additions and 53 deletions

View File

@ -0,0 +1,27 @@
local gui = require("gui")
function gui:enableGrid(cellSize)
local grid
if cellSize ~= nil then grid = true end
local cellSize = cellSize or 10 -- Width and height of cells.
self.post = function(self)
local gridLines = {}
if not grid then return end
local xx, yy, windowWidth, windowHeight = self:getAbsolutes()
-- Vertical lines.
for x = cellSize, windowWidth, cellSize do
local line = {xx + x, yy + 0, xx + x, yy + windowHeight}
table.insert(gridLines, line)
end
-- Horizontal lines.
for y = cellSize, windowHeight, cellSize do
local line = {xx + 0, yy + y, xx + windowWidth, yy + y}
table.insert(gridLines, line)
end
love.graphics.setLineWidth(1)
for i, line in ipairs(gridLines) do love.graphics.line(line) end
end
end

View File

@ -0,0 +1,96 @@
local gui = require("gui")
local default_theme = theme:new("64342e", "b2989e", "909b9a")
function gui:newWindow(x, y, w, h, text, draggable)
local parent = self
local pointer = love.mouse.getCursor()
local theme = default_theme
local header = self:newFrame(x, y, w, 30)
header:setRoundness(10, 10, nil, "top")
local window = header:newFrame(0, 30, w, h - 30)
local title = header:newTextLabel(text or "", 5, 0, w - 35, 30)
title.visibility = 0
title.ignore = true
local X = header:newTextButton("", -25, -25, 20, 20, 1, 1)
X:setRoundness(10, 10)
X.align = gui.ALIGN_CENTER
X.color = color.red
local darkenX = color.darken(color.red, .2)
X.OnEnter(function(self) self.color = darkenX end)
X.OnExit(function(self) self.color = color.red end)
if draggable then
header:enableDragging(gui.MOUSE_PRIMARY)
header:OnDragging(function(self, dx, dy)
self:move(dx, dy)
-- window:move(dx,dy)
end)
end
-- Mutate the event args to point to our window object
window.OnClose = function() return window end % X.OnPressed
window.OnClose(function()
header:setParent(gui.virtual)
love.mouse.setCursor(pointer)
end)
function window:close() -- The OnClose connection itself does not modify values at all!
window.OnClose:Fire(self)
end
function window:open() header:setParent(parent) end
function window:setTheme(th)
theme = th
title.textColor = theme.colorPrimaryText
title:setFont(theme.fontPrimary)
title:fitFont()
header.color = theme.colorPrimaryDark
window.color = theme.colorPrimary
local elements = self:getAllChildren()
for _, element in pairs(elements) do
if element:hasType(gui.TYPE_BUTTON) then
element:setFont(theme.fontButton)
element.color = theme.colorButtonNormal
element.textColor = theme.colorButtonText
if not element.__registeredTheme then
element.OnEnter(function(self)
self.color = theme.colorButtonHighlight
end)
element.OnExit(function(self)
self.color = theme.colorButtonNormal
end)
end
element:fitFont()
element.align = gui.ALIGN_CENTER
element.__registeredTheme = true
elseif element:hasType(gui.TYPE_TEXT) then
element.color = theme.colorPrimary
element:setFont(theme.fontPrimary)
element.textColor = theme.colorPrimaryText
element:fitFont()
element.align = gui.ALIGN_CENTER
elseif element:hasType(gui.TYPE_FRAME) then
if element.__isHeader then
element.color = theme.colorPrimaryDark
else
element.color = theme.colorPrimary
end
end
end
end
function window:getTheme() return theme end
thread:newThread(function() window:setTheme(theme) end)
return window
end

View File

@ -998,74 +998,63 @@ gui.Events.OnKeyPressed(function(key, scancode, isrepeat)
end) end)
-- Images -- Images
local load_image = THREAD:newFunction(function(path)
require("love.image")
return love.image.newImageData(path)
end)
function gui:newImageBase(typ, x, y, w, h, sx, sy, sw, sh) function gui:newImageBase(typ, x, y, w, h, sx, sy, sw, sh)
local c = self:newBase(image + typ, x, y, w, h, sx, sy, sw, sh) local c = self:newBase(image + typ, x, y, w, h, sx, sy, sw, sh)
c.color = color.white c.color = color.white
c.visibility = 0 c.visibility = 0
local IMAGE local IMAGE
function c:getUniques() function c:getUniques()
return gui.getUniques(c, { return gui.getUniques(c, {
-- Recreating the image object using set image is the way to go -- Recreating the image object using set image is the way to go
DO = {[[setImage]], c.image or IMAGE} DO = {[[setImage]], c.image or IMAGE}
}) })
end end
function c:setImage(i, x, y, w, h)
c.setImage = function(self, i, x, y, w, h)
if i == nil then return end if i == nil then return end
load_image(i).OnReturn(function(img)
img = love.graphics.newImage(img)
IMAGE = i IMAGE = i
if type(i) == "string" then i = image_cache[i] or i end if type(i) == "string" then i = image_cache[i] or i end
if i and x then if i and x then
self.imageHeigth = h self.imageHeigth = h
self.imageWidth = w self.imageWidth = w
if type(i) == "string" then if type(i) == "string" then
image_cache[i] = love.graphics.newImage(i) image_cache[i] = img
i = image_cache[i] i = image_cache[i]
end end
self.image = i self.image = i
self.image:setWrap("repeat", "repeat") self.image:setWrap("repeat", "repeat")
self.imageColor = color.white self.imageColor = color.white
self.quad = love.graphics.newQuad(x, y, w, h, self.image:getWidth(), self.image:getHeight()) self.quad = love.graphics.newQuad(x, y, w, h, self.image:getWidth(), self.image:getHeight())
self.imageVisibility = 1 self.imageVisibility = 1
return return
end end
if not first_loop then
-- Wait one cycle for things to load up
drawer:newThread(function()
thread.yield()
local img
if type(i) == "userdata" and i:type() == "Image" then if type(i) == "userdata" and i:type() == "Image" then
img = i img = i
elseif type(i) == "userdata" and i:type() == "ImageData" then
img = love.graphics.newImage(i)
else
img = love.graphics.newImage(i)
image_cache[i] = love.image.newImageData(i)
end end
local x, y, w, h = self:getAbsolutes() local x, y, w, h = self:getAbsolutes()
self.imageColor = color.white self.imageColor = color.white
self.imageVisibility = 1 self.imageVisibility = 1
self.image = img self.image = img
self.image:setWrap("repeat", "repeat")
self.imageHeigth = img:getHeight() self.imageHeigth = img:getHeight()
self.imageWidth = img:getWidth() self.imageWidth = img:getWidth()
self.quad = love.graphics.newQuad(0, 0, w, h, self.imageWidth, self.quad = love.graphics.newQuad(0, 0, self.imageWidth, self.imageHeigth, self.imageWidth, self.imageHeigth)
self.imageHeigth)
end) end)
return
end
local img
if type(i) == "userdata" and i:type() == "Image" then
img = i
else
img = love.graphics.newImage(i)
end
local x, y, w, h = self:getAbsolutes()
self.imageColor = color.white
self.imageVisibility = 1
self.image = img
self.imageHeigth = img:getHeight()
self.imageWidth = img:getWidth()
self.quad = love.graphics.newQuad(0, 0, w, h, self.imageWidth,
self.imageHeigth)
end end
return c return c
end end