From 4b2d417d75b68513194bb419490c62ca34a9d7cd Mon Sep 17 00:00:00 2001 From: Robert Jelic <36573031+NoryiE@users.noreply.github.com> Date: Wed, 3 May 2023 19:50:35 +0200 Subject: [PATCH] Small Update - Fixed MonitorFrame - Added MonitorFrame Docs --- Basalt/main.lua | 47 +++++++++------- Basalt/objects/BaseFrame.lua | 18 ++++++- Basalt/objects/Container.lua | 2 +- Basalt/objects/Dropdown.lua | 8 ++- Basalt/objects/MonitorFrame.lua | 57 +++++++++++++++++--- docs/_sidebar.md | 1 + docs/objects/MonitorFrame.md | 10 ++++ docs/objects/MonitorFrame/setMonitor.md | 38 +++++++++++++ docs/objects/MonitorFrame/setMonitorGroup.md | 41 ++++++++++++++ 9 files changed, 190 insertions(+), 32 deletions(-) create mode 100644 docs/objects/MonitorFrame.md create mode 100644 docs/objects/MonitorFrame/setMonitor.md create mode 100644 docs/objects/MonitorFrame/setMonitorGroup.md diff --git a/Basalt/main.lua b/Basalt/main.lua index a318325..d2bdddf 100644 --- a/Basalt/main.lua +++ b/Basalt/main.lua @@ -15,7 +15,7 @@ local version = "1.7.0" local projectDirectory = fs.getDir(table.pack(...)[2] or "") -local activeKey, frames, monFrames, monGroups, variables, schedules = {}, {}, {}, {}, {}, {} +local activeKey, frames, monFrames, variables, schedules = {}, {}, {}, {}, {} local mainFrame, activeFrame, focusedObject, updaterActive local basalt = {} @@ -191,10 +191,6 @@ local function drawFrames() v:render() v:updateTerm() end - for _,v in pairs(monGroups)do - v[1]:render() - v[1]:updateTerm() - end end local stopped, moveX, moveY = nil, nil, nil @@ -270,13 +266,9 @@ local function basaltUpdateEvent(event, ...) end if(event == "monitor_touch") then - if(monFrames[a[2]]~=nil)then - monFrames[a[2]]:mouseHandler(1, a[2], a[3], true) - activeFrame = monFrames[a[2]] - end - if(count(monGroups)>0)then - for k,v in pairs(monGroups)do - v[1]:mouseHandler(1, a[2], a[3], true, a[1]) + for k,v in pairs(monFrames)do + if(v:mouseHandler(1, a[2], a[3], true, a[1]))then + activeFrame = v end end handleSchedules(event, ...) @@ -319,11 +311,26 @@ local function basaltUpdateEvent(event, ...) end end +local function createFrame(name) + for _, v in pairs(frames) do + if (v:getName() == name) then + return nil + end + end + local newFrame = _OBJECTS["BaseFrame"](name, bInstance) + newFrame:init() + newFrame:load() + newFrame:draw() + table.insert(frames, newFrame) + if(mainFrame==nil)and(newFrame:getName()~="basaltDebuggingFrame")then + newFrame:show() + end + return newFrame +end + basalt = { logging = false, dynamicValueEvents = false, - setTheme = setTheme, - getTheme = getTheme, drawFrames = drawFrames, log = log, getVersion = function() @@ -449,20 +456,20 @@ basalt = { schedule = schedule, - createFrame = function(name) + addFrame = createFrame, + createFrame = createFrame, + + addMonitor = function(name) for _, v in pairs(frames) do if (v:getName() == name) then return nil end end - local newFrame = _OBJECTS["BaseFrame"](name, bInstance) + local newFrame = _OBJECTS["MonitorFrame"](name, bInstance) newFrame:init() newFrame:load() newFrame:draw() - table.insert(frames, newFrame) - if(mainFrame==nil)and(newFrame:getName()~="basaltDebuggingFrame")then - newFrame:show() - end + table.insert(monFrames, newFrame) return newFrame end, diff --git a/Basalt/objects/BaseFrame.lua b/Basalt/objects/BaseFrame.lua index 3c32d53..b744948 100644 --- a/Basalt/objects/BaseFrame.lua +++ b/Basalt/objects/BaseFrame.lua @@ -142,12 +142,18 @@ return function(name, basalt) end, updateTerm = function(self) - basaltDraw.update() + if(basaltDraw~=nil)then + basaltDraw.update() + end end, setTerm = function(self, newTerm) termObject = newTerm - basaltDraw = drawSystem(termObject) + if(newTerm==nil)then + basaltDraw = nil + else + basaltDraw = drawSystem(termObject) + end return self end, @@ -187,6 +193,14 @@ return function(name, basalt) end, } + for k,v in pairs({mouse_click={"mouseHandler", true},mouse_up={"mouseUpHandler", false},mouse_drag={"dragHandler", false},mouse_scroll={"scrollHandler", true},mouse_hover={"hoverHandler", false}})do + object[v[1]] = function(self, btn, x, y, ...) + if(base[v[1]](self, btn, x, y, ...))then + basalt.setActiveFrame(self) + end + end + end + for k,v in pairs({"drawBackgroundBox", "drawForegroundBox", "drawTextBox"})do object[v] = function(self, x, y, width, height, symbol) local obx, oby = self:getPosition() diff --git a/Basalt/objects/Container.lua b/Basalt/objects/Container.lua index 26bc2ff..c8802d8 100644 --- a/Basalt/objects/Container.lua +++ b/Basalt/objects/Container.lua @@ -351,8 +351,8 @@ return function(name, basalt) if(v[2])then self:removeFocusedObject() end - return true end + return true end end end diff --git a/Basalt/objects/Dropdown.lua b/Basalt/objects/Dropdown.lua index ff10488..e03d2bb 100644 --- a/Basalt/objects/Dropdown.lua +++ b/Basalt/objects/Dropdown.lua @@ -56,7 +56,7 @@ return function(name, basalt) return dropdownW, dropdownH end, - mouseHandler = function(self, button, x, y) + mouseHandler = function(self, button, x, y, isMon) if (isOpened) then local obx, oby = self:getAbsolutePosition() if(button==1)then @@ -69,6 +69,12 @@ return function(name, basalt) self:updateDraw() local val = self:sendEvent("mouse_click", self, "mouse_click", dir, x, y) if(val==false)then return val end + if(isMon)then + basalt.schedule(function() + sleep(0.1) + self:mouseUpHandler(button, x, y) + end)() + end return true end end diff --git a/Basalt/objects/MonitorFrame.lua b/Basalt/objects/MonitorFrame.lua index 55fd448..1da103c 100644 --- a/Basalt/objects/MonitorFrame.lua +++ b/Basalt/objects/MonitorFrame.lua @@ -1,12 +1,17 @@ +local basaltMon = require("basaltMon") local max,min,sub,rep = math.max,math.min,string.sub,string.rep + return function(name, basalt) local base = basalt.getObject("BaseFrame")(name, basalt) local objectType = "MonitorFrame" - - base:hide() + + base:setTerm(nil) + local isMonitorGroup = false + local monGroup local object = { + getType = function() return objectType end, @@ -19,22 +24,58 @@ return function(name, basalt) return base end, - setMonitor = function(self, name) - local mon = peripheral.wrap(name) - if(mon~=nil)then - self:setTerm(mon) + setMonitor = function(self, newMon) + if(type(newMon)=="string")then + local mon = peripheral.wrap(newMon) + if(mon~=nil)then + self:setTerm(mon) + end + elseif(type(newMon)=="table")then + self:setTerm(newMon) end return self end, + + setMonitorGroup = function(self, monGrp) + monGroup = basaltMon(monGrp) + self:setTerm(monGroup) + isMonitorGroup = true + return self + end, + + render = function(self) + if(self:getTerm()~=nil)then + base.render(self) + end + end, show = function(self) - if(basalt.getTerm()~=self:getTerm())then - base.show() + base:getBase().show(self) + basalt.setActiveFrame(self) + for k,v in pairs(colors)do + if(type(v)=="number")then + termObject.setPaletteColor(v, colors.packRGB(term.nativePaletteColor((v)))) + end + end + for k,v in pairs(colorTheme)do + if(type(v)=="number")then + termObject.setPaletteColor(type(k)=="number" and k or colors[k], v) + else + local r,g,b = table.unpack(v) + termObject.setPaletteColor(type(k)=="number" and k or colors[k], r,g,b) + end end return self end, } + object.mouseHandler = function(self, btn, x, y, isMon, monitor, ...) + if(isMonitorGroup)then + x, y = monGroup.calculateClick(monitor, x, y) + end + base.mouseHandler(self, btn, x, y, isMon, monitor, ...) + end + object.__index = object return setmetatable(object, base) end \ No newline at end of file diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 13bf16f..b5b2839 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -11,6 +11,7 @@ - [ChangeableObject](objects/ChangeableObject.md) - [Container](objects/Container.md) - [BaseFrame](objects/BaseFrame.md) + - [BaseFrame](objects/MonitorFrame.md) - [Frame](objects/Frame.md) - [Flexbox](objects/Flexbox.md) - [MovableFrame](objects/MovableFrame.md) diff --git a/docs/objects/MonitorFrame.md b/docs/objects/MonitorFrame.md new file mode 100644 index 0000000..a2a4a29 --- /dev/null +++ b/docs/objects/MonitorFrame.md @@ -0,0 +1,10 @@ +MonitorFrame is a subclass of the BaseFrame class. It is specifically designed for use with in-game monitors, allowing you to display content on monitors and manage multiple monitor groups. MonitorFrame inherits all methods from BaseFrame and provides additional functionality for working with in-game monitors. + +To create a MonitorFrame, you can use the basalt.addMonitor() function. + +In addition to the Object, VisualObject, Container, and BaseFrame methods, MonitorFrame also has the following methods: + +| | | +|---|---| +|[setMonitor](objects/MonitorFrame/setMonitor.md)|Sets the in-game monitor to display the MonitorFrame content +|[setMonitorGroup](objects/MonitorFrame/setMonitorGroup.md)|Sets the monitor group for managing multiple monitors diff --git a/docs/objects/MonitorFrame/setMonitor.md b/docs/objects/MonitorFrame/setMonitor.md new file mode 100644 index 0000000..482b072 --- /dev/null +++ b/docs/objects/MonitorFrame/setMonitor.md @@ -0,0 +1,38 @@ +## setMonitor + +### Description + +Associates the MonitorFrame with an in-game monitor. The content of the MonitorFrame will be displayed on the specified monitor. + +### Parameters + +1. `string|table` the monitor object or name + +### Returns + +1. `object` The object in use + +### Usage + +* Creates a MonitorFrame and associates it with a monitor: + +```lua +local basalt = require("basalt") + +local monitor = peripheral.wrap("top") -- Assuming a monitor is on the top side +local monitorFrame = basalt.addMonitor() +monitorFrame:setMonitor(monitor) + +basalt.autoUpdate() +``` + +or + +```lua +local basalt = require("basalt") + +local monitorFrame = basalt.addMonitor() +monitorFrame:setMonitor("top") + +basalt.autoUpdate() +``` diff --git a/docs/objects/MonitorFrame/setMonitorGroup.md b/docs/objects/MonitorFrame/setMonitorGroup.md new file mode 100644 index 0000000..5151656 --- /dev/null +++ b/docs/objects/MonitorFrame/setMonitorGroup.md @@ -0,0 +1,41 @@ +## setMonitorGroup + +### Description + +Sets the monitor group for the MonitorFrame. This can be used to combine multiple in-game monitors into a single large monitor. The content of the MonitorFrame will be displayed across all monitors in the group. + +### Parameters + +1. `table` A table containing monitor names connected to the computer, organized by their position in the group. + +Table Layout: + +```lua +{ + [y1] = {"x1", "x2", "x3"} + [y2] = {"x1", "x2", "x3"} +} +``` + +### Returns + +1. `object` The object in use + +### Usage + +* Creates a MonitorFrame and combines two monitors into a single large monitor: + +```lua +local basalt = require("basalt") + +local monitorFrame = basalt.addMonitor() + +local monitorGroup = { + [1] = {"monitor_1", "monitor_2"}, + [2] = {"monitor_3", "monitor_4"} +} + +monitorFrame:setMonitorGroup(monitorGroup) + +basalt.autoUpdate() +```