- Added monitor support
- Fixed states example - Small changes to Display element
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
local elementManager = require("elementManager")
|
||||
local Container = elementManager.getElement("Container")
|
||||
local errorManager = require("errorManager")
|
||||
local Render = require("render")
|
||||
---@configDescription This is the base frame class. It is the root element of all elements and the only element without a parent.
|
||||
|
||||
@@ -8,14 +9,39 @@ local Render = require("render")
|
||||
---@class BaseFrame : Container
|
||||
---@field _render Render The render object
|
||||
---@field _renderUpdate boolean Whether the render object needs to be updated
|
||||
---@field _peripheralName string The name of a peripheral
|
||||
local BaseFrame = setmetatable({}, Container)
|
||||
BaseFrame.__index = BaseFrame
|
||||
|
||||
---@property text term nil The terminal object to render to
|
||||
local function isPeripheral(t)
|
||||
local ok, result = pcall(function()
|
||||
return peripheral.getType(t)
|
||||
end)
|
||||
if ok then
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
---@property term term|peripheral term.current() The terminal or (monitor) peripheral object to render to
|
||||
BaseFrame.defineProperty(BaseFrame, "term", {default = nil, type = "table", setter = function(self, value)
|
||||
self._peripheralName = nil
|
||||
if self.basalt.getActiveFrame(self._values.term)==self then
|
||||
self.basalt.setActiveFrame(self, false)
|
||||
end
|
||||
if value == nil or value.setCursorPos == nil then
|
||||
return value
|
||||
end
|
||||
|
||||
if(isPeripheral(value)) then
|
||||
self._peripheralName = peripheral.getName(value)
|
||||
end
|
||||
|
||||
self._values.term = value
|
||||
if self.basalt.getActiveFrame(value) == nil then
|
||||
self.basalt.setActiveFrame(self)
|
||||
end
|
||||
|
||||
self._render = Render.new(value)
|
||||
self._renderUpdate = true
|
||||
local width, height = value.getSize()
|
||||
@@ -105,10 +131,39 @@ end
|
||||
--- @param y number The y position to set the cursor to
|
||||
--- @param blink boolean Whether the cursor should blink
|
||||
function BaseFrame:setCursor(x, y, blink, color)
|
||||
local term = self.get("term")
|
||||
local _term = self.get("term")
|
||||
self._render:setCursor(x, y, blink, color)
|
||||
end
|
||||
|
||||
--- @shortDescription Handles monitor touch events
|
||||
--- @param name string The name of the monitor that was touched
|
||||
--- @param x number The x position of the mouse
|
||||
--- @param y number The y position of the mouse
|
||||
--- @protected
|
||||
function BaseFrame:monitor_touch(name, x, y)
|
||||
local _term = self.get("term")
|
||||
if _term == nil then return end
|
||||
if(isPeripheral(_term))then
|
||||
if self._peripheralName == name then
|
||||
self:mouse_click(0, x, y)
|
||||
self.basalt.schedule(function()
|
||||
sleep(0.1)
|
||||
self:mouse_up(0, x, y)
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- @shortDescription Handles mouse click events
|
||||
--- @param button number The button that was clicked
|
||||
--- @param x number The x position of the mouse
|
||||
--- @param y number The y position of the mouse
|
||||
--- @protected
|
||||
function BaseFrame:mouse_click(button, x, y)
|
||||
Container.mouse_click(self, button, x, y)
|
||||
self.basalt.setFocus(self)
|
||||
end
|
||||
|
||||
--- @shortDescription Handles mouse up events
|
||||
--- @param button number The button that was released
|
||||
--- @param x number The x position of the mouse
|
||||
@@ -156,6 +211,17 @@ function BaseFrame:char(char)
|
||||
Container.char(self, char)
|
||||
end
|
||||
|
||||
function BaseFrame:dispatchEvent(event, ...)
|
||||
local _term = self.get("term")
|
||||
if _term == nil then return end
|
||||
if(isPeripheral(_term))then
|
||||
if event == "mouse_click" then
|
||||
return
|
||||
end
|
||||
end
|
||||
Container.dispatchEvent(self, event, ...)
|
||||
end
|
||||
|
||||
--- @shortDescription Renders the Frame
|
||||
--- @protected
|
||||
function BaseFrame:render()
|
||||
|
||||
@@ -2,7 +2,8 @@ local elementManager = require("elementManager")
|
||||
local VisualElement = elementManager.getElement("VisualElement")
|
||||
local getCenteredPosition = require("libraries/utils").getCenteredPosition
|
||||
local deepcopy = require("libraries/utils").deepcopy
|
||||
---@cofnigDescription The Display is a special element which uses the cc window API which you can use.
|
||||
local colorHex = require("libraries/colorHex")
|
||||
---@configDescription The Display is a special element which uses the cc window API which you can use.
|
||||
---@configDefault false
|
||||
|
||||
--- The Display is a special element where you can use the window (term) API to draw on a element, useful when you need to use external APIs.
|
||||
@@ -30,6 +31,8 @@ function Display:init(props, basalt)
|
||||
self.set("type", "Display")
|
||||
self._window = window.create(basalt.getActiveFrame():getTerm(), 1, 1, self.get("width"), self.get("height"), false)
|
||||
local reposition = self._window.reposition
|
||||
local blit = self._window.blit
|
||||
local write = self._window.write
|
||||
self._window.reposition = function(self, x, y, width, height)
|
||||
self.set("x", x)
|
||||
self.set("y", y)
|
||||
@@ -49,6 +52,14 @@ function Display:init(props, basalt)
|
||||
self._window.isVisible = function(self)
|
||||
return self.get("visible")
|
||||
end
|
||||
self._window.blit = function(self, x, y, text, fg, bg)
|
||||
blit(self, x, y, text, fg, bg)
|
||||
self:updateRender()
|
||||
end
|
||||
self._window.write = function(self, x, y, text)
|
||||
write(self, x, y, text)
|
||||
self:updateRender()
|
||||
end
|
||||
|
||||
self:observe("width", function(self, width)
|
||||
local window = self._window
|
||||
@@ -71,6 +82,30 @@ function Display:getWindow()
|
||||
return self._window
|
||||
end
|
||||
|
||||
--- Writes text to the display at the given position with the given foreground and background colors
|
||||
--- @shortDescription Writes text to the display
|
||||
--- @param x number The x position to write to
|
||||
--- @param y number The y position to write to
|
||||
--- @param text string The text to write
|
||||
--- @param fg? colors The foreground color (optional)
|
||||
--- @param bg? colors The background color (optional)
|
||||
--- @return Display self The display instance
|
||||
function Display:write(x, y, text, fg, bg)
|
||||
local window = self._window
|
||||
if window then
|
||||
if fg then
|
||||
window.setTextColor(fg)
|
||||
end
|
||||
if bg then
|
||||
window.setBackgroundColor(bg)
|
||||
end
|
||||
window.setCursorPos(x, y)
|
||||
window.write(text)
|
||||
end
|
||||
self:updateRender()
|
||||
return self
|
||||
end
|
||||
|
||||
--- @shortDescription Renders the Display
|
||||
--- @protected
|
||||
function Display:render()
|
||||
|
||||
Reference in New Issue
Block a user