Threaded image loading
This commit is contained in:
parent
80e678ce35
commit
946fd79e37
@ -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
|
||||||
@ -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
|
||||||
95
init.lua
95
init.lua
@ -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)
|
|
||||||
if i == nil then return end
|
|
||||||
IMAGE = i
|
|
||||||
if type(i) == "string" then i = image_cache[i] or i end
|
|
||||||
if i and x then
|
|
||||||
self.imageHeigth = h
|
|
||||||
self.imageWidth = w
|
|
||||||
if type(i) == "string" then
|
|
||||||
image_cache[i] = love.graphics.newImage(i)
|
|
||||||
i = image_cache[i]
|
|
||||||
end
|
|
||||||
self.image = i
|
|
||||||
self.image:setWrap("repeat","repeat")
|
|
||||||
self.imageColor = color.white
|
|
||||||
self.quad = love.graphics.newQuad(x, y, w, h, self.image:getWidth(), self.image:getHeight())
|
|
||||||
self.imageVisibility = 1
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if not first_loop then
|
c.setImage = function(self, i, x, y, w, h)
|
||||||
-- Wait one cycle for things to load up
|
if i == nil then return end
|
||||||
drawer:newThread(function()
|
load_image(i).OnReturn(function(img)
|
||||||
thread.yield()
|
img = love.graphics.newImage(img)
|
||||||
local img
|
IMAGE = i
|
||||||
if type(i) == "userdata" and i:type() == "Image" then
|
if type(i) == "string" then i = image_cache[i] or i end
|
||||||
img = i
|
|
||||||
elseif type(i) == "userdata" and i:type() == "ImageData" then
|
if i and x then
|
||||||
img = love.graphics.newImage(i)
|
self.imageHeigth = h
|
||||||
else
|
self.imageWidth = w
|
||||||
img = love.graphics.newImage(i)
|
|
||||||
image_cache[i] = love.image.newImageData(i)
|
if type(i) == "string" then
|
||||||
|
image_cache[i] = img
|
||||||
|
i = image_cache[i]
|
||||||
end
|
end
|
||||||
local x, y, w, h = self:getAbsolutes()
|
|
||||||
|
self.image = i
|
||||||
|
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.imageVisibility = 1
|
self.imageVisibility = 1
|
||||||
self.image = img
|
|
||||||
self.imageHeigth = img:getHeight()
|
return
|
||||||
self.imageWidth = img:getWidth()
|
end
|
||||||
self.quad = love.graphics.newQuad(0, 0, w, h, self.imageWidth,
|
|
||||||
self.imageHeigth)
|
if type(i) == "userdata" and i:type() == "Image" then
|
||||||
end)
|
img = i
|
||||||
return
|
end
|
||||||
end
|
|
||||||
local img
|
local x, y, w, h = self:getAbsolutes()
|
||||||
if type(i) == "userdata" and i:type() == "Image" then
|
self.imageColor = color.white
|
||||||
img = i
|
self.imageVisibility = 1
|
||||||
else
|
self.image = img
|
||||||
img = love.graphics.newImage(i)
|
self.image:setWrap("repeat", "repeat")
|
||||||
end
|
self.imageHeigth = img:getHeight()
|
||||||
local x, y, w, h = self:getAbsolutes()
|
self.imageWidth = img:getWidth()
|
||||||
self.imageColor = color.white
|
self.quad = love.graphics.newQuad(0, 0, self.imageWidth, self.imageHeigth, self.imageWidth, self.imageHeigth)
|
||||||
self.imageVisibility = 1
|
end)
|
||||||
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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user