Small bugfixes
This commit is contained in:
@@ -133,6 +133,21 @@ function BaseFrame:term_resize()
|
|||||||
self._renderUpdate = true
|
self._renderUpdate = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function BaseFrame:key(key)
|
||||||
|
self:fireEvent("key", key)
|
||||||
|
Container.key(self, key)
|
||||||
|
end
|
||||||
|
|
||||||
|
function BaseFrame:key_up(key)
|
||||||
|
self:fireEvent("key_up", key)
|
||||||
|
Container.key_up(self, key)
|
||||||
|
end
|
||||||
|
|
||||||
|
function BaseFrame:char(char)
|
||||||
|
self:fireEvent("char", char)
|
||||||
|
Container.char(self, char)
|
||||||
|
end
|
||||||
|
|
||||||
--- @shortDescription Renders the Frame
|
--- @shortDescription Renders the Frame
|
||||||
--- @protected
|
--- @protected
|
||||||
function BaseFrame:render()
|
function BaseFrame:render()
|
||||||
|
|||||||
@@ -12,11 +12,17 @@ local Image = setmetatable({}, VisualElement)
|
|||||||
Image.__index = Image
|
Image.__index = Image
|
||||||
|
|
||||||
---@property bimg table {} The bimg image data
|
---@property bimg table {} The bimg image data
|
||||||
Image.defineProperty(Image, "bimg", {default = {}, type = "table", canTriggerRender = true})
|
Image.defineProperty(Image, "bimg", {default = {{}}, type = "table", canTriggerRender = true})
|
||||||
---@property currentFrame number 1 Current animation frame
|
---@property currentFrame number 1 Current animation frame
|
||||||
Image.defineProperty(Image, "currentFrame", {default = 1, type = "number", canTriggerRender = true})
|
Image.defineProperty(Image, "currentFrame", {default = 1, type = "number", canTriggerRender = true})
|
||||||
---@property metadata table {} Image metadata (version, palette, etc)
|
---@property metadata table {} Image metadata (version, palette, etc)
|
||||||
Image.defineProperty(Image, "metadata", {default = {}, type = "table"})
|
Image.defineProperty(Image, "metadata", {default = {}, type = "table"})
|
||||||
|
---@property autoResize boolean false Whether to automatically resize the image when content exceeds bounds
|
||||||
|
Image.defineProperty(Image, "autoResize", {default = true, type = "boolean"})
|
||||||
|
---@property offsetX number 0 Horizontal offset for viewing larger images
|
||||||
|
Image.defineProperty(Image, "offsetX", {default = 0, type = "number", canTriggerRender = true})
|
||||||
|
---@property offsetY number 0 Vertical offset for viewing larger images
|
||||||
|
Image.defineProperty(Image, "offsetY", {default = 0, type = "number", canTriggerRender = true})
|
||||||
|
|
||||||
--- Creates a new Image instance
|
--- Creates a new Image instance
|
||||||
--- @shortDescription Creates a new Image instance
|
--- @shortDescription Creates a new Image instance
|
||||||
@@ -24,6 +30,10 @@ Image.defineProperty(Image, "metadata", {default = {}, type = "table"})
|
|||||||
--- @private
|
--- @private
|
||||||
function Image.new()
|
function Image.new()
|
||||||
local self = setmetatable({}, Image):__init()
|
local self = setmetatable({}, Image):__init()
|
||||||
|
self.set("width", 12)
|
||||||
|
self.set("height", 6)
|
||||||
|
self.set("background", colors.black)
|
||||||
|
self.set("z", 5)
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -67,6 +77,41 @@ function Image:loadBimg(bimgData)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Image:resizeImage(width, height)
|
||||||
|
local frames = self.get("bimg")
|
||||||
|
|
||||||
|
for frameIndex, frame in ipairs(frames) do
|
||||||
|
local newFrame = {}
|
||||||
|
for y = 1, height do
|
||||||
|
local text = string.rep(" ", width)
|
||||||
|
local fg = string.rep("f", width)
|
||||||
|
local bg = string.rep("0", width)
|
||||||
|
|
||||||
|
if frame[y] and frame[y][1] then
|
||||||
|
local oldText = frame[y][1]
|
||||||
|
local oldFg = frame[y][2]
|
||||||
|
local oldBg = frame[y][3]
|
||||||
|
|
||||||
|
text = (oldText .. string.rep(" ", width)):sub(1, width)
|
||||||
|
fg = (oldFg .. string.rep("f", width)):sub(1, width)
|
||||||
|
bg = (oldBg .. string.rep("0", width)):sub(1, width)
|
||||||
|
end
|
||||||
|
|
||||||
|
newFrame[y] = {text, fg, bg}
|
||||||
|
end
|
||||||
|
frames[frameIndex] = newFrame
|
||||||
|
end
|
||||||
|
|
||||||
|
self:updateRender()
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function Image:getImageSize()
|
||||||
|
local bimg = self.get("bimg")
|
||||||
|
if not bimg[1] or not bimg[1][1] then return 0, 0 end
|
||||||
|
return #bimg[1][1][1], #bimg[1]
|
||||||
|
end
|
||||||
|
|
||||||
--- Gets pixel information at position
|
--- Gets pixel information at position
|
||||||
--- @shortDescription Gets pixel information at position
|
--- @shortDescription Gets pixel information at position
|
||||||
--- @param x number X position
|
--- @param x number X position
|
||||||
@@ -91,104 +136,77 @@ function Image:getPixelData(x, y)
|
|||||||
return fgColor, bgColor, char
|
return fgColor, bgColor, char
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Sets character at position
|
local function ensureFrame(self, y)
|
||||||
--- @shortDescription Sets character at position
|
|
||||||
--- @param x number X position
|
|
||||||
--- @param y number Y position
|
|
||||||
--- @param char string Single character to set
|
|
||||||
function Image:setChar(x, y, char)
|
|
||||||
if type(char) ~= "string" or #char ~= 1 then return self end
|
|
||||||
|
|
||||||
local frame = self.get("bimg")[self.get("currentFrame")]
|
local frame = self.get("bimg")[self.get("currentFrame")]
|
||||||
if not frame then
|
if not frame then
|
||||||
frame = {{}, {}, {}}
|
frame = {}
|
||||||
self.get("bimg")[self.get("currentFrame")] = frame
|
self.get("bimg")[self.get("currentFrame")] = frame
|
||||||
end
|
end
|
||||||
|
|
||||||
if not frame[y] then
|
if not frame[y] then
|
||||||
frame[y] = {"", "", ""}
|
frame[y] = {"", "", ""}
|
||||||
end
|
end
|
||||||
|
return frame
|
||||||
|
end
|
||||||
|
|
||||||
local text = frame[y][1]
|
function Image:setText(x, y, text)
|
||||||
while #text < x do
|
if type(text) ~= "string" or #text < 1 then return self end
|
||||||
text = text .. " "
|
local frame = ensureFrame(self, y)
|
||||||
|
|
||||||
|
local currentLine = frame[y][1]
|
||||||
|
while #currentLine < x + #text - 1 do
|
||||||
|
currentLine = currentLine .. " "
|
||||||
end
|
end
|
||||||
|
|
||||||
frame[y][1] = text:sub(1, x-1) .. char .. text:sub(x+1)
|
frame[y][1] = currentLine:sub(1, x-1) .. text .. currentLine:sub(x + #text)
|
||||||
self:updateRender()
|
self:updateRender()
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Sets foreground color at position
|
function Image:setFg(x, y, pattern)
|
||||||
--- @shortDescription Sets foreground color at position
|
if type(pattern) ~= "string" or #pattern < 1 then return self end
|
||||||
--- @param x number X position
|
local frame = ensureFrame(self, y)
|
||||||
--- @param y number Y position
|
|
||||||
--- @param color number Color value (0-15)
|
|
||||||
function Image:setFg(x, y, color)
|
|
||||||
if type(color) ~= "number" then return self end
|
|
||||||
|
|
||||||
local frame = self.get("bimg")[self.get("currentFrame")]
|
local currentFg = frame[y][2]
|
||||||
if not frame then
|
while #currentFg < x + #pattern - 1 do
|
||||||
frame = {{}, {}, {}}
|
currentFg = currentFg .. "f"
|
||||||
self.get("bimg")[self.get("currentFrame")] = frame
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if not frame[y] then
|
frame[y][2] = currentFg:sub(1, x-1) .. pattern .. currentFg:sub(x + #pattern)
|
||||||
frame[y] = {"", "", ""}
|
|
||||||
end
|
|
||||||
|
|
||||||
local fg = frame[y][2]
|
|
||||||
while #fg < x do
|
|
||||||
fg = fg .. "f"
|
|
||||||
end
|
|
||||||
|
|
||||||
frame[y][2] = fg:sub(1, x-1) .. tHex[color] .. fg:sub(x+1)
|
|
||||||
self:updateRender()
|
self:updateRender()
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Sets background color at position
|
function Image:setBg(x, y, pattern)
|
||||||
--- @shortDescription Sets background color at position
|
if type(pattern) ~= "string" or #pattern < 1 then return self end
|
||||||
--- @param x number X position
|
local frame = ensureFrame(self, y)
|
||||||
--- @param y number Y position
|
|
||||||
--- @param color number Color value (0-15)
|
|
||||||
function Image:setBg(x, y, color)
|
|
||||||
if type(color) ~= "number" then return self end
|
|
||||||
|
|
||||||
local frame = self.get("bimg")[self.get("currentFrame")]
|
local currentBg = frame[y][3]
|
||||||
if not frame then
|
while #currentBg < x + #pattern - 1 do
|
||||||
frame = {{}, {}, {}}
|
currentBg = currentBg .. "0"
|
||||||
self.get("bimg")[self.get("currentFrame")] = frame
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if not frame[y] then
|
frame[y][3] = currentBg:sub(1, x-1) .. pattern .. currentBg:sub(x + #pattern)
|
||||||
frame[y] = {"", "", ""}
|
|
||||||
end
|
|
||||||
|
|
||||||
local bg = frame[y][3]
|
|
||||||
while #bg < x do
|
|
||||||
bg = bg .. "f"
|
|
||||||
end
|
|
||||||
|
|
||||||
frame[y][3] = bg:sub(1, x-1) .. tHex[color] .. bg:sub(x+1)
|
|
||||||
self:updateRender()
|
self:updateRender()
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Sets all properties at position
|
|
||||||
--- @shortDescription Sets all properties at position
|
|
||||||
--- @param x number X position
|
|
||||||
--- @param y number Y position
|
|
||||||
--- @param char string? Character to set (optional)
|
|
||||||
--- @param fg number? Foreground color (optional)
|
|
||||||
--- @param bg number? Background color (optional)
|
|
||||||
function Image:setPixel(x, y, char, fg, bg)
|
function Image:setPixel(x, y, char, fg, bg)
|
||||||
if char then self:setChar(x, y, char) end
|
if char then self:setText(x, y, char) end
|
||||||
if fg then self:setFg(x, y, fg) end
|
if fg then self:setFg(x, y, fg) end
|
||||||
if bg then self:setBg(x, y, bg) end
|
if bg then self:setBg(x, y, bg) end
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Image:setOffset(x, y)
|
||||||
|
self.set("offsetX", x)
|
||||||
|
self.set("offsetY", y)
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function Image:getOffset()
|
||||||
|
return self.get("offsetX"), self.get("offsetY")
|
||||||
|
end
|
||||||
|
|
||||||
--- Advances to the next frame in the animation
|
--- Advances to the next frame in the animation
|
||||||
--- @shortDescription Advances to the next frame in the animation
|
--- @shortDescription Advances to the next frame in the animation
|
||||||
--- @return Image self The Image instance
|
--- @return Image self The Image instance
|
||||||
@@ -212,13 +230,23 @@ function Image:render()
|
|||||||
local frame = self.get("bimg")[self.get("currentFrame")]
|
local frame = self.get("bimg")[self.get("currentFrame")]
|
||||||
if not frame then return end
|
if not frame then return end
|
||||||
|
|
||||||
for y, line in ipairs(frame) do
|
local offsetX = self.get("offsetX")
|
||||||
local text = line[1]
|
local offsetY = self.get("offsetY")
|
||||||
local fg = line[2]
|
local elementWidth = self.get("width")
|
||||||
local bg = line[3]
|
local elementHeight = self.get("height")
|
||||||
|
|
||||||
if text and fg and bg then
|
for y = 1, elementHeight do
|
||||||
self:blit(1, y, text, fg, bg)
|
local frameY = y + offsetY
|
||||||
|
local line = frame[frameY]
|
||||||
|
|
||||||
|
if line then
|
||||||
|
local text = line[1]:sub(1 + offsetX, elementWidth + offsetX)
|
||||||
|
local fg = line[2]:sub(1 + offsetX, elementWidth + offsetX)
|
||||||
|
local bg = line[3]:sub(1 + offsetX, elementWidth + offsetX)
|
||||||
|
|
||||||
|
if text and fg and bg then
|
||||||
|
self:blit(1 + offsetX, y, text, fg, bg)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -102,6 +102,9 @@ VisualElement.registerEventCallback(VisualElement, "Enter", "mouse_enter", "mous
|
|||||||
VisualElement.registerEventCallback(VisualElement, "LeEave", "mouse_leave", "mouse_move")
|
VisualElement.registerEventCallback(VisualElement, "LeEave", "mouse_leave", "mouse_move")
|
||||||
VisualElement.registerEventCallback(VisualElement, "Focus", "focus", "blur")
|
VisualElement.registerEventCallback(VisualElement, "Focus", "focus", "blur")
|
||||||
VisualElement.registerEventCallback(VisualElement, "Blur", "blur", "focus")
|
VisualElement.registerEventCallback(VisualElement, "Blur", "blur", "focus")
|
||||||
|
VisualElement.registerEventCallback(VisualElement, "Key", "key", "key_up")
|
||||||
|
VisualElement.registerEventCallback(VisualElement, "Char", "char")
|
||||||
|
VisualElement.registerEventCallback(VisualElement, "KeyUp", "key_up", "key")
|
||||||
|
|
||||||
local max, min = math.max, math.min
|
local max, min = math.max, math.min
|
||||||
|
|
||||||
@@ -323,6 +326,24 @@ function VisualElement:calculatePosition()
|
|||||||
return x, y
|
return x, y
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function VisualElement:key(key)
|
||||||
|
if(self.get("focused"))then
|
||||||
|
self:fireEvent("key", key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function VisualElement:key_up(key)
|
||||||
|
if(self.get("focused"))then
|
||||||
|
self:fireEvent("key_up", key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function VisualElement:char(char)
|
||||||
|
if(self.get("focused"))then
|
||||||
|
self:fireEvent("char", char)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
--- Returns the absolute position of the element or the given coordinates.
|
--- Returns the absolute position of the element or the given coordinates.
|
||||||
--- @shortDescription Returns the absolute position of the element
|
--- @shortDescription Returns the absolute position of the element
|
||||||
---@param x? number x position
|
---@param x? number x position
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ end
|
|||||||
function basalt.createFrame()
|
function basalt.createFrame()
|
||||||
local frame = basalt.create("BaseFrame")
|
local frame = basalt.create("BaseFrame")
|
||||||
frame:postInit()
|
frame:postInit()
|
||||||
mainFrame = frame
|
if(mainFrame==nil)then mainFrame = frame end
|
||||||
return frame
|
return frame
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -126,6 +126,7 @@ end
|
|||||||
--- @usage basalt.setActiveFrame(myFrame)
|
--- @usage basalt.setActiveFrame(myFrame)
|
||||||
function basalt.setActiveFrame(frame)
|
function basalt.setActiveFrame(frame)
|
||||||
mainFrame = frame
|
mainFrame = frame
|
||||||
|
mainFrame:updateRender()
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Schedules a function to run in a coroutine
|
--- Schedules a function to run in a coroutine
|
||||||
|
|||||||
Reference in New Issue
Block a user