updated Getting-Started.md

Samkist
2022-05-19 13:50:23 -04:00
parent aa149d885e
commit 512adbf8d5

@@ -1,45 +1,66 @@
You want to know how to use basalt for your projects? Easy! Just use the following command in your computercraft computer/turtle/pocket comp. shell:
Basalt aims to be a relatively small, easy to use framework.
Accordingly, we have provided an installation script.
Just use the following command in any CC:Tweaked shell:
`pastebin run ESs1mg7P`
After this is done, edit your program and add the following line on top of your program:
This will download `basalt.lua` to your local directory
To make use of the framework, make use of the following snippet
````lua
--> For those who are unfamiliar with lua, dofile executes the code in the referenced file
local basalt = dofile("basalt.lua")
````
now you are able to access everything you need from basalt.
but how do you actually start?
Here is a simple example of how you would create a simple button on the screen:
````lua
--Here you load the basalt framework into your file:
local basalt = dofile("basalt.lua")
local basalt = dofile("basalt.lua") --> Load the Basalt framework
--Here you create a non-parent frame. The basalt.update/autoUpdate events will always give their events to a non-parent frame! Which means you always need atleast one active non-parent frame!
--As variable you have to use a unique name - otherwise you wont get a frame object back. :show() immediatly shows the object on the screen
local main = basalt.createFrame("mainFrame")
main:show() -- now we make the main frame visible
--> Create the first frame. Please note that Basalt needs at least one active "non-parent" frame to properly supply events
--> When Basalt#createFrame makes use of unique identifiers (commonly referred to as UIDs), meaning that the supplied value must be UNIQUE
--> If the supplied UID is ambiguous, Basalt#createFrame returns a nil value
local mainFrame = basalt.createFrame("mainFrame")
local button = main:addButton("clickableButton") -- here we add a button to the main frame (with a unique name)
button:setPosition(4,4) -- here we just change the position of that button (default position would be 1, 1)
button:setText("Click me!") -- we set the text of that button
local function buttonClick() -- we create a function which the button should call if we click on that button
basalt.debug("I got clicked!")
--> Show the frame to the user
mainFrame:show()
local button = mainFrame:addButton("clickableButton") --> Add a button to the mainFrame (With a unique identifier)
--> Set the position of the button, Button#setPosition follows an x, y pattern.
--> The x value is how far right the object should be from its anchor (negative values from an anchor will travel left)
--> The y value is how far down the object should be from its anchor (negative values from an anchor will travel up)
button:setPosition(4, 4)
button:setText("Click me!") --> Set the text of our button
local function buttonClick() --> This function serves as our click logic
basalt.debug("I got clicked!")
end
button:onClick(buttonClick) -- here we add a onClick event and add the created function to the list
button:show() -- ofc it also has to be visible
--Here you start the event listener.
basalt.autoUpdate()
--> Remember! You cannot supply buttonClick(), that will only supply the result of the function
--> Make sure the button knows which function to call when it's clicked
button:onClick(buttonClick)
button:show() --> Make the button visible, so the user can click it
basalt.autoUpdate() --> Basalt#autoUpdate starts the event listener to detect user input
````
You dont like the amount of lines you need for that? No problem, this does 1-1 the same thing as above:
If you're like us and strive for succinct and beautiful code, here is a cleaner implementation of the code above:
````lua
local basalt = dofile("basalt.lua")
local main = basalt.createFrame("mainFrame"):show()
local button = main:addButton("clickableButton"):setPosition(4,4):setText("Click me!"):onClick(function() basalt.debug("I got clicked!") end):show()
local mainFrame = basalt.createFrame("mainFrame"):show()
local button = mainFrame --> Basalt returns an instance of the object on most methods, to make use of "call-chaining"
:addButton("clickableButton") --> This is an example of call chaining
:setPosition(4,4)
:setText("Click me!")
:onClick(
function()
basalt.debug("I got clicked!")
end)
:show()
basalt.autoUpdate()
````