updated docs
This commit is contained in:
@@ -1,41 +1,41 @@
|
||||
# Changing button colors
|
||||
|
||||
Here i want to explain to you how you would create a button with the default color gray, and as long as the user is clicking on the button it will change its color to black (the default frame-background is lightGray).
|
||||
|
||||
To make this possible the button needs 1 onClick event, 1 onClickUp event and 1 onLoseFocus event.
|
||||
|
||||
Very interesting sidetip: events can hold multiple functions!<br>
|
||||
**Example snippet:**
|
||||
````lua
|
||||
local function buttonColoring()
|
||||
-- here you can add some coloring for your button
|
||||
end
|
||||
local function buttonLogic()
|
||||
-- here you can add some logic for your button
|
||||
end
|
||||
local button = mainFrame:addButton("ExampleButton"):show()
|
||||
button:onClick(buttonColoring):onClick(buttonLogic) -- yes this would work, if not its a bug!
|
||||
````
|
||||
|
||||
This means you can create a function wich handles only the coloring side of your button, and if your button also needs some logic you just create your own unique function for that and add it to your button.
|
||||
|
||||
With this knowledge we create now a function where we pass a button-object as parameter and this will setup the coloring of our button:
|
||||
|
||||
**Example snippet:**
|
||||
````lua
|
||||
local basalt = dofile("basalt.lua")
|
||||
local mainFrame = basalt.createFrame("mainFrame"):show()
|
||||
local button = mainFrame:addButton("firstButton"):setPosition(3,3):setSize(12,3):setText("Click me"):setBackground(colors.gray):setForeground(colors.black):show()
|
||||
|
||||
local button2 = mainFrame:addButton("secondButton"):setPosition(25,3):setSize(16,3):setText("Another Btn"):setBackground(colors.gray):setForeground(colors.black):show()
|
||||
|
||||
local function setupButtonColoring(btn)
|
||||
btn:onClick(function() btn:setBackground(colors.black) btn:setForeground(colors.lightGray) end)
|
||||
btn:onClickUp(function() btn:setBackground(colors.gray) btn:setForeground(colors.black) end)
|
||||
btn:onLoseFocus(function() btn:setBackground(colors.gray) btn:setForeground(colors.black) end)
|
||||
end
|
||||
setupButtonColoring(button)
|
||||
setupButtonColoring(button2)
|
||||
|
||||
basalt.autoUpdate()
|
||||
````
|
||||
Here I want to explain to you how you would create a button with the default color gray, and as long as the user is clicking on the button it will change its color to black (the default frame-background is lightGray).
|
||||
|
||||
To make this possible the button needs 1 onClick event, 1 onClickUp event and 1 onLoseFocus event.
|
||||
|
||||
Very interesting sidetip: events can hold multiple functions!<br>
|
||||
**Example snippet:**
|
||||
````lua
|
||||
local function buttonColoring()
|
||||
-- here you can add some coloring for your button
|
||||
end
|
||||
local function buttonLogic()
|
||||
-- here you can add some logic for your button
|
||||
end
|
||||
local button = mainFrame:addButton("ExampleButton"):show()
|
||||
button:onClick(buttonColoring):onClick(buttonLogic) -- yes this would work, if not its a bug!
|
||||
````
|
||||
|
||||
This means you can create a function wich handles only the coloring side of your button, and if your button also needs some logic you just create your own unique function for that and add it to your button.
|
||||
|
||||
With this knowledge we create now a function where we pass a button-object as parameter and this will setup the coloring of our button:
|
||||
|
||||
**Example snippet:**
|
||||
````lua
|
||||
local basalt = dofile("basalt.lua")
|
||||
local mainFrame = basalt.createFrame("mainFrame"):show()
|
||||
local button = mainFrame:addButton("firstButton"):setPosition(3,3):setSize(12,3):setText("Click me"):setBackground(colors.gray):setForeground(colors.black):show()
|
||||
|
||||
local button2 = mainFrame:addButton("secondButton"):setPosition(25,3):setSize(16,3):setText("Another Btn"):setBackground(colors.gray):setForeground(colors.black):show()
|
||||
|
||||
local function setupButtonColoring(btn)
|
||||
btn:onClick(function() btn:setBackground(colors.black) btn:setForeground(colors.lightGray) end)
|
||||
btn:onClickUp(function() btn:setBackground(colors.gray) btn:setForeground(colors.black) end)
|
||||
btn:onLoseFocus(function() btn:setBackground(colors.gray) btn:setForeground(colors.black) end)
|
||||
end
|
||||
setupButtonColoring(button)
|
||||
setupButtonColoring(button2)
|
||||
|
||||
basalt.autoUpdate()
|
||||
````
|
||||
|
||||
Now you've got a function which sets your buttons up.
|
||||
@@ -1,57 +1,56 @@
|
||||
# Executing your own logic
|
||||
|
||||
You question yourself how you can execute your own logic while basalt is also active? There are multiple ways of doing that:
|
||||
|
||||
## Number 1:
|
||||
Using parallel.waitForAll
|
||||
|
||||
````lua
|
||||
local basalt = dofile("basalt.lua")
|
||||
|
||||
local mainFrame = basalt.createFrame("mainFrame"):show()-- lets create a frame and a button without functionality
|
||||
mainFrame:addButton("aButton"):onClick(function() end):show()
|
||||
|
||||
local function yourCustomHandler()
|
||||
while true do
|
||||
-- add your logic here
|
||||
os.sleep(1) -- you need something which calls coroutine.yield(), yes os.sleep does that os.pullEvent() aswell
|
||||
end
|
||||
end
|
||||
|
||||
parallel.waitForAll(basalt.autoUpdate, yourCustomHandler) -- here it will handle your function (yourCustomHandler) and basalts handlers at the time
|
||||
````
|
||||
You can read [here](http://www.computercraft.info/wiki/Parallel_(API)) what exactly parallel.waitForAll() does
|
||||
|
||||
## Number 2:
|
||||
Using threads
|
||||
|
||||
````lua
|
||||
local basalt = dofile("basalt.lua")
|
||||
|
||||
local mainFrame = basalt.createFrame("mainFrame"):show()-- lets create a frame, a button without functionality and a thread
|
||||
mainFrame:addButton("aButton"):onClick(function() end):show()
|
||||
local thread = mainFrame:addThread("customHandlerExecutingThread")
|
||||
|
||||
local function yourCustomHandler()
|
||||
while true do
|
||||
-- add your logic here
|
||||
os.sleep(1) -- you need something which calls coroutine.yield(), yes os.sleep does that os.pullEvent() aswell
|
||||
end
|
||||
end
|
||||
thread:start(yourCustomHandler) -- this will create a coroutine and starts the coroutine, os.sleep does the rest, so you just have to call start once.
|
||||
````
|
||||
|
||||
## Number 3:
|
||||
Using timers
|
||||
|
||||
````lua
|
||||
local basalt = dofile("basalt.lua")
|
||||
|
||||
local mainFrame = basalt.createFrame("mainFrame"):show()-- lets create a frame, a button without functionality and a timer
|
||||
mainFrame:addButton("aButton"):onClick(function() end):show()
|
||||
local timer = mainFrame:addTimer("customHandlerExecutingTimer")
|
||||
|
||||
local function yourCustomHandler()
|
||||
-- add your logic here
|
||||
end
|
||||
timer:onCall(yourCustomHandler):setTime(1, -1):start() -- this will call your function every second until you :cancel() the timer
|
||||
You question yourself how you can execute your own logic while basalt is also active? There are multiple ways of doing that:
|
||||
|
||||
## Method 1:
|
||||
Using parallel.waitForAll
|
||||
|
||||
````lua
|
||||
local basalt = dofile("basalt.lua")
|
||||
|
||||
local mainFrame = basalt.createFrame("mainFrame"):show()-- lets create a frame and a button without functionality
|
||||
mainFrame:addButton("aButton"):onClick(function() end):show()
|
||||
|
||||
local function yourCustomHandler()
|
||||
while true do
|
||||
-- add your logic here
|
||||
os.sleep(1) -- you need something which calls coroutine.yield(), yes os.sleep does that os.pullEvent() aswell
|
||||
end
|
||||
end
|
||||
|
||||
parallel.waitForAll(basalt.autoUpdate, yourCustomHandler) -- here it will handle your function (yourCustomHandler) and basalts handlers at the time
|
||||
````
|
||||
You can read [here](http://www.computercraft.info/wiki/Parallel_(API)) what exactly parallel.waitForAll() does
|
||||
|
||||
## Method 2:
|
||||
Using threads
|
||||
|
||||
````lua
|
||||
local basalt = dofile("basalt.lua")
|
||||
|
||||
local mainFrame = basalt.createFrame("mainFrame"):show()-- lets create a frame, a button without functionality and a thread
|
||||
mainFrame:addButton("aButton"):onClick(function() end):show()
|
||||
local thread = mainFrame:addThread("customHandlerExecutingThread")
|
||||
|
||||
local function yourCustomHandler()
|
||||
while true do
|
||||
-- add your logic here
|
||||
os.sleep(1) -- you need something which calls coroutine.yield(), yes os.sleep does that os.pullEvent() aswell
|
||||
end
|
||||
end
|
||||
thread:start(yourCustomHandler) -- this will create a coroutine and starts the coroutine, os.sleep does the rest, so you just have to call start once.
|
||||
````
|
||||
|
||||
## Method 3:
|
||||
Using timers
|
||||
|
||||
````lua
|
||||
local basalt = dofile("basalt.lua")
|
||||
|
||||
local mainFrame = basalt.createFrame("mainFrame"):show()-- lets create a frame, a button without functionality and a timer
|
||||
mainFrame:addButton("aButton"):onClick(function() end):show()
|
||||
local timer = mainFrame:addTimer("customHandlerExecutingTimer")
|
||||
|
||||
local function yourCustomHandler()
|
||||
-- add your logic here
|
||||
end
|
||||
timer:onCall(yourCustomHandler):setTime(1, -1):start() -- this will call your function every second until you :cancel() the timer
|
||||
````
|
||||
Reference in New Issue
Block a user