From e35708902ccab26095621f43ee3fd01a46737ed5 Mon Sep 17 00:00:00 2001 From: Robert Jelic <36573031+NoryiE@users.noreply.github.com> Date: Wed, 10 May 2023 17:21:17 +0200 Subject: [PATCH] Docs - added a onSelect event for lists - added docs for onSelect - added addPlugin and addObject docs --- Basalt/objects/List.lua | 31 ++++++++++++++++++++++++++---- docs/objects/Basalt.md | 2 ++ docs/objects/Basalt/addObject.md | 24 +++++++++++++++++++++++ docs/objects/Basalt/addPlugin.md | 24 +++++++++++++++++++++++ docs/objects/List.md | 8 +++++++- docs/objects/List/onSelect.md | 33 ++++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 docs/objects/Basalt/addObject.md create mode 100644 docs/objects/Basalt/addPlugin.md create mode 100644 docs/objects/List/onSelect.md diff --git a/Basalt/objects/List.lua b/Basalt/objects/List.lua index 7e06d9d..965b484 100644 --- a/Basalt/objects/List.lua +++ b/Basalt/objects/List.lua @@ -84,7 +84,16 @@ return function(name, basalt) end, removeItem = function(self, index) - table.remove(list, index) + if(type(index)=="number")then + table.remove(list, index) + elseif(type(index)=="table")then + for k,v in pairs(list)do + if(v==index)then + table.remove(list, k) + break + end + end + end self:updateDraw() return self end, @@ -193,6 +202,7 @@ return function(name, basalt) if (list[n + yOffset] ~= nil) then if (obx <= x) and (obx + w > x) and (oby + n - 1 == y) then self:setValue(list[n + yOffset]) + self:selectHandler() self:updateDraw() end end @@ -211,11 +221,22 @@ return function(name, basalt) return self:mouseHandler(1, x, y) end, + onSelect = function(self, ...) + for _,v in pairs(table.pack(...))do + if(type(v)=="function")then + self:registerEvent("select_item", v) + end + end + return self + end, + + selectHandler = function(self) + self:sendEvent("select_item", self:getValue()) + end, + draw = function(self) base.draw(self) self:addDraw("list", function() - local parent = self:getParent() - local obx, oby = self:getPosition() local w, h = self:getSize() for n = 1, h do if list[n + yOffset] then @@ -224,7 +245,9 @@ return function(name, basalt) if list[n + yOffset] == self:getValue() and selectionColorActive then fg, bg = itemSelectedFG, itemSelectedBG end - self:addBlit(1, n, t, tHex[fg]:rep(#t), tHex[bg]:rep(#t)) + self:addText(1, n, t) + self:addBG(1, n, tHex[bg]:rep(w)) + self:addFG(1, n, tHex[fg]:rep(w)) end end end) diff --git a/docs/objects/Basalt.md b/docs/objects/Basalt.md index 3ac6ec1..cc32d8e 100644 --- a/docs/objects/Basalt.md +++ b/docs/objects/Basalt.md @@ -33,6 +33,8 @@ You are now able to access the following list of methods: |[setVariable](objects/Basalt/setVariable.md)|Sets a variable which you can access via XML |[stopUpdate / stop](objects/Basalt/stopUpdate.md)|Stops the currently active event and draw listener |[update](objects/Basalt/update.md)|Starts the event and draw listener once +|[addObject](objects/Basalt/addObject.md)|Adds new object files/folders +|[addPlugin](objects/Basalt/addPlugin.md)|Adds new plugin files/folders ## Examples diff --git a/docs/objects/Basalt/addObject.md b/docs/objects/Basalt/addObject.md new file mode 100644 index 0000000..102f906 --- /dev/null +++ b/docs/objects/Basalt/addObject.md @@ -0,0 +1,24 @@ +## addObject + +### Description + +The `basalt.addObject` method allows you to add new custom objects (elements) to the Basalt framework. This enables extending the framework with additional functionality tailored to specific needs. It's important to note that this method must be called before `basalt.autoUpdate` and can only be used during the initialization phase, not during runtime. + +### Parameters + +1. `string` The path to the Lua file or folder containing the custom object(s). + +### Usage + +* Loads a custom objects folder: + +```lua +local basalt = require("basalt") + +-- Add the custom object(s): +basalt.addObject("objects") + +-- Rest of the code + +basalt.autoUpdate() +``` diff --git a/docs/objects/Basalt/addPlugin.md b/docs/objects/Basalt/addPlugin.md new file mode 100644 index 0000000..7d9c555 --- /dev/null +++ b/docs/objects/Basalt/addPlugin.md @@ -0,0 +1,24 @@ +## addPlugin + +### Description + +The `basalt.addPlugin` method allows you to add new custom plugins to the Basalt framework. This enables extending the framework with additional functionality tailored to specific needs. It's important to note that this method must be called before `basalt.autoUpdate` and can only be used during the initialization phase, not during runtime. + +### Parameters + +1. `string` The path to the Lua file or folder containing the plugin(s). + +### Usage + +* Loads a custom plugins folder: + +```lua +local basalt = require("basalt") + +-- Add the custom plugin(s): +basalt.addPlugin("plugin") + +-- Rest of the code + +basalt.autoUpdate() +``` diff --git a/docs/objects/List.md b/docs/objects/List.md index 27a5188..1a15e3e 100644 --- a/docs/objects/List.md +++ b/docs/objects/List.md @@ -21,6 +21,12 @@ In addition to the Object and VisualObject methods, lists also have the followin |[getSelectionColor](objects/List/getSelectionColor.md)|Returns default bg and fg selection color |[isSelectionColorActive](objects/List/isSelectionColorActive.md)|Returns if it is using selection color +## Events + +| | | +|---|---| +|[onSelect](objects/List/onSelect.md)|Fires when a item on the list get's selected + A item-table in lists looks like the following example: ```lua @@ -43,7 +49,7 @@ aList:addItem("Item 1") aList:addItem("Item 2", colors.yellow) aList:addItem("Item 3", colors.yellow, colors.green) -aList:onChange(function(self, item) +aList:onSelect(function(self, item) basalt.debug("Selected item: ", item.text) end) ``` diff --git a/docs/objects/List/onSelect.md b/docs/objects/List/onSelect.md new file mode 100644 index 0000000..21fe125 --- /dev/null +++ b/docs/objects/List/onSelect.md @@ -0,0 +1,33 @@ +## onSelect + +### Description + +`onSelect(self, event, item)` + +The onSelect event is triggered when a item on the list gets selected. + +### Returns + +1. `object` The object in use + +### Usage + +* Add an onSelect event to a list: + +```lua +local basalt = require("basalt") + +local main = basalt.createFrame() +local list = main:addList() + +list:addItem("Entry 1") +list:addItem("Entry 2") + +function listOnSelect(self, event, item) + basalt.debug("Item got selected:", item.text) +end + +list:onSelect(listOnSelect) + +basalt.autoUpdate() +```