From 18601d54f7393c849eaa90f671fe8e627c0677b0 Mon Sep 17 00:00:00 2001
From: Robert Jelic <36573031+NoryiE@users.noreply.github.com>
Date: Sun, 28 Aug 2022 20:25:42 +0200
Subject: [PATCH] Some updates
---
docs/_sidebar.md | 1 +
docs/objects/Basalt/autoUpdate.md | 1 -
docs/objects/Object/disable.md | 4 +++
docs/objects/Object/enable.md | 4 +++
docs/objects/Object/getPosition.md | 6 ++++
docs/objects/Object/getSize.md | 6 ++++
docs/objects/Object/onClick.md | 27 +++++++++++++++
docs/objects/Object/onDrag.md | 29 ++++++++++++++--
docs/objects/Object/setPosition.md | 13 +++++--
docs/objects/Object/setSize.md | 4 +--
docs/tips/dynamicvalues.md | 54 ++++++++++++++++++++++++++++++
11 files changed, 141 insertions(+), 8 deletions(-)
create mode 100644 docs/tips/dynamicvalues.md
diff --git a/docs/_sidebar.md b/docs/_sidebar.md
index 488f803..f7a3d5d 100644
--- a/docs/_sidebar.md
+++ b/docs/_sidebar.md
@@ -28,3 +28,4 @@
- [Your Logic](tips/logic.md)
- [Button coloring](tips/buttonColoring.md)
- [Designing/Animating](tips/design.md)
+ - [Dynamic Values](tips/dynamicvalues.md)
diff --git a/docs/objects/Basalt/autoUpdate.md b/docs/objects/Basalt/autoUpdate.md
index e7697cb..9038719 100644
--- a/docs/objects/Basalt/autoUpdate.md
+++ b/docs/objects/Basalt/autoUpdate.md
@@ -8,6 +8,5 @@ This starts the event and draw handler for you. The listeners will run until you
* Enable the basalt listeners, otherwise the screen will not continue to update
```lua
local main = basalt.createFrame()
-
basalt.autoUpdate()
```
\ No newline at end of file
diff --git a/docs/objects/Object/disable.md b/docs/objects/Object/disable.md
index e69de29..6c97de3 100644
--- a/docs/objects/Object/disable.md
+++ b/docs/objects/Object/disable.md
@@ -0,0 +1,4 @@
+## disable
+Disables the object's event listeners
+
+This will disable the object. Which means it doesn't listen to any events anymore.
\ No newline at end of file
diff --git a/docs/objects/Object/enable.md b/docs/objects/Object/enable.md
index e69de29..f277cb8 100644
--- a/docs/objects/Object/enable.md
+++ b/docs/objects/Object/enable.md
@@ -0,0 +1,4 @@
+## enable
+Enables the object's event listeners
+
+If the object's is disabled, it will stop listening to incoming events, this will reenable it.
\ No newline at end of file
diff --git a/docs/objects/Object/getPosition.md b/docs/objects/Object/getPosition.md
index e69de29..b4e35e5 100644
--- a/docs/objects/Object/getPosition.md
+++ b/docs/objects/Object/getPosition.md
@@ -0,0 +1,6 @@
+## getPosition
+Returns the object's position
+
+#### Returns:
+1. `number` x
+2. `number` y
diff --git a/docs/objects/Object/getSize.md b/docs/objects/Object/getSize.md
index e69de29..70ca747 100644
--- a/docs/objects/Object/getSize.md
+++ b/docs/objects/Object/getSize.md
@@ -0,0 +1,6 @@
+## getSize
+Returns the object's size
+
+#### Returns:
+1. `number` w
+2. `number` h
diff --git a/docs/objects/Object/onClick.md b/docs/objects/Object/onClick.md
index 1ccbbe4..31f1d4d 100644
--- a/docs/objects/Object/onClick.md
+++ b/docs/objects/Object/onClick.md
@@ -17,4 +17,31 @@ function buttonOnClick()
basalt.debug("Button got clicked!")
end
button:onClick(buttonOnClick)
+```
+
+Here is also a example on how you could create double clicks:
+```lua
+local basalt = require("basalt")
+local doubleClickMaxTime = 0.25 -- in seconds
+
+local main = basalt.createFrame()
+local button = main:addButton()
+
+local function createDoubleClick(btn, func) -- here we create a function where we can pass buttons (or other object if you'd like to) and a function which will get called by double clicking.
+ local doubleClick = 0
+ btn:onClick(function()
+ if(os.epoch("local")-doubleClickMaxTime*1000<=doubleClick)then
+ func()
+ end
+ doubleClick = os.epoch("local")
+ end)
+end
+
+local function debugSomething()
+ basalt.debug("hello")
+end
+
+createDoubleClick(button, debugSomething) -- this is how you will create a double click.
+
+basalt.autoUpdate()
```
\ No newline at end of file
diff --git a/docs/objects/Object/onDrag.md b/docs/objects/Object/onDrag.md
index 9589cd7..0fe014d 100644
--- a/docs/objects/Object/onDrag.md
+++ b/docs/objects/Object/onDrag.md
@@ -2,8 +2,7 @@
`onDrag(self, event, button, x, y, xOffset, yOffset)`
The computercraft event which triggers this method is `mouse_drag`.
-Here is a example on how to add a onDrag event to your button:
-
+This is a example on how you would create a movable button:
```lua
local basalt = require("basalt")
@@ -14,7 +13,31 @@ local button = main:addButton()
:setText("Click")
function buttonOnDrag(self, button, x, y, xOffset, yOffset)
- basalt.debug("Someone drags me (i know i wont reposition myself)!")
+ self:setPosition(-xOffset, -yOffset, true) -- we need to reverse the offset and true means to add the offset instead of changing it.
end
button:onDrag(buttonOnDrag)
+
+basalt.autoUpdate()
+```
+
+Another example on how you could change the frame's offset by dragging around.
+```lua
+local basalt = require("basalt")
+
+local main = basalt.createFrame()
+ :onDrag(function(self, button, x, y, xOffset, yOffset)
+ local xO, yO = self:getOffset()
+ self:setOffset(xO-xOffset, yO-yOffset, true) -- we need to reverse the offset and true means to add the offset instead of changing it.
+ end)
+
+local button = main:addButton()
+ :setPosition(3,3)
+ :setSize(12,3)
+ :setText("Click")
+local button2 = main:addButton()
+ :setPosition(16,3)
+ :setSize(12,3)
+ :setText("Click")
+
+basalt.autoUpdate()
```
diff --git a/docs/objects/Object/setPosition.md b/docs/objects/Object/setPosition.md
index 26e3ecd..4ba0413 100644
--- a/docs/objects/Object/setPosition.md
+++ b/docs/objects/Object/setPosition.md
@@ -1,8 +1,8 @@
## setPosition
Changes the position relative to its parent frame
#### Parameters:
-1. `number` x coordinate
-2. `number` y coordinate
+1. `number|string` x coordinate as number or dynamicvalue as string
+2. `number|string` y coordinate as number or dynamicvalue as string
3. `boolean` Whether it will add/remove to the current coordinates instead of setting them
#### Returns:
@@ -16,4 +16,13 @@ mainFrame:addButton():setPosition(2,3)
```
```xml
+```
+
+if you prefer to use dynamic values:
+```lua
+local mainFrame = basalt.createFrame()
+mainFrame:addButton():setPosition("parent.w * 0.5", 23)
+```
+```xml
+
```
\ No newline at end of file
diff --git a/docs/objects/Object/setSize.md b/docs/objects/Object/setSize.md
index 347e63d..335a332 100644
--- a/docs/objects/Object/setSize.md
+++ b/docs/objects/Object/setSize.md
@@ -1,8 +1,8 @@
## setSize
Changes the object size
#### Parameters:
-1. `number` width
-2. `number` height
+1. `number|string` width as number or dynamicvalue as string
+2. `number|string` height as number or dynamicvalue as string
#### Returns:
1. `object` The object in use
diff --git a/docs/tips/dynamicvalues.md b/docs/tips/dynamicvalues.md
new file mode 100644
index 0000000..7f49631
--- /dev/null
+++ b/docs/tips/dynamicvalues.md
@@ -0,0 +1,54 @@
+Dynamic Values is a way to position or size your object based on the position/size of other objects. This makes it pretty easy to create programs where the terminal size
+doesn't matter.
+
+A dynamic value is always a string, and you can add anything you want in there. Here are some examples:
+"5 + 2"
+"(5 + 2) * 3"
+"math.floor(5+2.2)"
+"objectid.x + objectid.y + objectid.w + objectid+h"
+"parent.w"
+"self.w"
+
+Parent always refers to it's parent object, self always refers to itself.
+
+## Positioning
+Here i will show you a example on how to position a button next to another button, doesn't matter which size it has.
+
+```lua
+local basalt = require("basalt")
+
+local main = basalt.createFrame()
+
+local button = main:addButton("firstBtn")
+ :setBackground(colors.black)
+ :setForeground(colors.lightGray)
+ :setPosition(2, 2)
+local button2 = main:addButton()
+ :setBackground(colors.black)
+ :setForeground(colors.lightGray)
+ :setPosition("firstBtn.w + firstBtn.x + 2", "firstBtn.y")
+
+basalt.autoUpdate()
+```
+Now, you can move the first button and the second button would also move. Doesn't matter how.
+
+## Sizing
+This is a example on how you can create buttons where the size is always based on it's parent frame
+```lua
+local basalt = require("basalt")
+
+local main = basalt.createFrame()
+
+local button = main:addButton("firstBtn")
+ :setBackground(colors.black)
+ :setForeground(colors.lightGray)
+ :setPosition(2, 2)
+ :setSize("parent.w - 2", 3)
+local button2 = main:addButton()
+ :setBackground(colors.black)
+ :setForeground(colors.lightGray)
+ :setPosition(2, "firstBtn.y + firstBtn.h + 1")
+ :setSize("parent.w - 2", 3)
+
+basalt.autoUpdate()
+```
\ No newline at end of file