This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Hellreaper's profile picture

Requesting help with buttons

Started by Hellreaper, 15 March 2016 - 03:56 PM
Hellreaper #1
Posted 15 March 2016 - 04:56 PM
I made this little button API because I wanted to learn how to make one, but I am stuck here…

What I wanted it to do is copy the template to every file u wanted to use it, whilst making sure the textbox is always alligned to the dimensions of the text

But every time I run the codeit gives me an error -> window:88: Expected number
I have no idea what this error would mean…

http://pastebin.com/gXy3hTYe
Gumball #2
Posted 15 March 2016 - 09:46 PM
I made this little button API because I wanted to learn how to make one, but I am stuck here…

What I wanted it to do is copy the template to every file u wanted to use it, whilst making sure the textbox is always alligned to the dimensions of the text

But every time I run the codeit gives me an error -> window:88: Expected number
I have no idea what this error would mean…

http://pastebin.com/gXy3hTYe

You don't need to draw a box around the text, just use term.setBackgroundColor(color)
That sets the pixel color of the text, textColor sets the font color. You don't need SLC either, that is only if you have menu's, and there are still more efficient ways to do that.
Also the error is that variables are case sensitive, the main variable TextColor is used as textColor in the text color.

Use this code instead: CmysABAu

Spoiler

displayText = "template"
--The X position on the screen
startX = 2
--The Y position on the screen
startY = 2
--Use 0 if only a single line
height = 1
width = #displayText
--Color of the button and text
backgroundColor  = colors.blue
textColor = colors.white
---------------------------------------------------
slc = 0
term.setCursorPos(startX, startY)
term.setBackgroundColor(backgroundColor)
term.setTextColor(textColor)
write(displayText)

while true do
  local event, button, X, Y = os.pullEvent() --DO NOT os.pullEventRaw unless you don't want the program to be terminatable.
	  if(event == "mouse_click") then
		if(X >= startX and X <= width and Y >= startY and Y <= height) then
		  --Write what the button has to do in the if
		  if(true) then
			os.reboot()
		  end
		end
	 end
end

Hope this helps :)/>
-Bird
Edited on 15 March 2016 - 09:07 PM
Hellreaper #3
Posted 16 March 2016 - 12:28 AM
oh my ^^ Thx for that, I'll try it out tomorrow ^^ when the server is online on the 2.4.2 version since we cant get it to update xd.
Appears it was just some minor tweaks. I started lua today so a bit messy still hehe.

One more thing though, what would be the fastest way to make multiple buttons?

- just copy the program to a new name and change the parameters?
- create a function that calls this program with all parameters?
- or any better suggestion?

I learn C++ at uni atm so I can kinda understand the base of lua as well (imo its easier than c++ but i just dont know all the functions / syntax)
valithor #4
Posted 16 March 2016 - 04:21 AM
edit:

Just realized I misunderstood what you were asking. The information below might be useful if you want to improve what you created, but it will not answer your question.

__________________________________________________________________________

One more thing though, what would be the fastest way to make multiple buttons?

The easiest way would be to create a function, where you pass it arguments, and it puts it into a table. You would then have another function that loops through the table and checks to see if it is within the parameters.

Seeing as you said you have started Lua today, I will give a very short explanation of tables. Tables are basically just a box of variables. The variables can be referenced with keys, which returns the value of the variable.

short tbl examples:
Spoilerexample:


tbl = {
  ["key1"] = "value1",
  ["key2"] = "value2"
}

Here we have defined a table named "tbl" with two variables inside of it. The first variable has the key "key1" and the value "value1" in order to reference this variable you would do something like:

print(tbl["key1"]) --# prints value1
print(tbl["key2"]) --# prints value2

You can add new values to a table at anytime by doing something like:

tbl["key3"] = "value3"
print(tbl["key3"] --# prints value3

Example code:

local buttons = {} --# creating a table to hold the buttons we are going to create

local function createButton(name,startX,endX,startY,endY)  --# the name is the name of the button, it needs to be a unique identifier
  buttons[name] = {["startX"] = startX, ["endx"] = endX, ["startY"] = startY, ["endY"] = endY}
end

local function check(x,y)
  for key, value in pairs(buttons) do --# looping through the buttons table, each iteration key is equal to the key and value to the value
	if value["startX"] <= x and value["endX"] >= x and value["startY"] >= startY and value["endY"] <= endY then
	  return key --# key is the name.  We return the name, so you know which button was pressed.
	end
  end
end

Example of creating and checking for a button:

createButton("button1",1,1,5,10)
createButton("button2",2,2,5,10)
check(2,6) --# returns button2
check(1,7) --# returns button1

--# example in actual use
local event, button, x, y = os.pullEvent("mouse_click")
local clicked = check(x,y)
if clicked == "button1" then
  --# button1 was pressed
elseif clicked == "button2" then
  --# button2 was pressed
end
Edited on 16 March 2016 - 03:31 AM
Hellreaper #5
Posted 16 March 2016 - 08:37 AM
So tables are arrays, if I understand correctly

Thx for the explanation, Im implementing it now.
I'll report back when I get it working (or run into problems)
Bomb Bloke #6
Posted 16 March 2016 - 08:52 AM
So tables are arrays, if I understand correctly

Not exactly - they're a bit more flexible, but you can certainly treat them as such.
Hellreaper #7
Posted 16 March 2016 - 01:29 PM
I continued to work on it using the info you guys gave me which was nice but I get a weird effect now
I tried putting it on a position on the screen so I can see whats going on but now I get this


local buttons = {}
local function createButton(name,startX,endX,startY,endY, offset, color)
  buttons[name] = {["startX"] = startX, ["endx"] = endX, ["startY"] = startY, ["endY"] = endY}
  term.setCursorPos(startX, startY)
  paintutils.drawFilledBox(startX-offset, startY-offset, endX+offset, endY+offset, color)
  print(name)
end
local function check(x,y)
  for key, value in pairs(buttons) do
	    if value["startX"] <= x and value["endX"] >= x and value["startY"] >= y and value["endY"] <= y then
		  return key
	    end
  end
end
createButton("Reboot", 5, 10, 5, 10, 0, colors.green)
local event, button, x, y = os.pullEvent("mouse_click")
local clicked = check(x,y)
if clicked == "Reboot" then
os.reboot()
end

http://puu.sh/nIAhn/2f0cc6b7b2.png

http://pastebin.com/NPyi1J9X
valithor #8
Posted 16 March 2016 - 02:19 PM
I continued to work on it using the info you guys gave me which was nice but I get a weird effect now
I tried putting it on a position on the screen so I can see whats going on but now I get this


local buttons = {}
local function createButton(name,startX,endX,startY,endY, offset, color)
  buttons[name] = {["startX"] = startX, ["endx"] = endX, ["startY"] = startY, ["endY"] = endY}
  term.setCursorPos(startX, startY)
  paintutils.drawFilledBox(startX-offset, startY-offset, endX+offset, endY+offset, color)
  print(name)
end
local function check(x,y)
  for key, value in pairs(buttons) do
		if value["startX"] <= x and value["endX"] >= x and value["startY"] >= y and value["endY"] <= y then
		  return key
		end
  end
end
createButton("Reboot", 5, 10, 5, 10, 0, colors.green)
local event, button, x, y = os.pullEvent("mouse_click")
local clicked = check(x,y)
if clicked == "Reboot" then
os.reboot()
end

http://puu.sh/nIAhn/2f0cc6b7b2.png

http://pastebin.com/NPyi1J9X

Keys in tables are case sensitive, so the problem is in createButton the key is being set as "endx", but in the check function it is being checked as "endX".

I also messed up in the example I gave you. The comparisons for startY and endY need to be reversed to where it is:


value["startY"] <= startY and value["endY"] >= endY

Instead of:


value["startY"] >= startY and value["endY"] <= endY
Edited on 16 March 2016 - 01:20 PM
Hellreaper #9
Posted 16 March 2016 - 07:00 PM
Ok guys, Im almost there. The only thing I need now is to set the BoxColor back to normal when I print the Rebooting… after the button is pressed.
also RGB cant be used for color? only the preset and combine versions of them


local buttons = {}
local function createButton(name,startX, startY, color)
  buttons[name] = {["startX"] = startX, ["startY"] = startY}
  endX = startX+string.len(name)
  endY = startY+string.len(name)
  paintutils.drawFilledBox(startX, startY, endX-startX, startY, color)
  term.setCursorPos(startX, startY)
  print(name)
end
local function check(x,y)
  for key, value in pairs(buttons) do
	    if value["startX"] <= x and endX >= x and value["startY"] <= y and endY >= y then
		  return key
	    end
  end
end
createButton("Reboot", 5, 1, colors.green)
local event, button, x, y = os.pullEvent("mouse_click")
local clicked = check(x,y)
if clicked == "Reboot" then
textutils.slowPrint("Rebooting...")
os.reboot()
end
KingofGamesYami #10
Posted 16 March 2016 - 08:28 PM
You can only use the 16 color pallet CC has provided. The numbers expected by color manipulation functions are powers of two, 2^1, 2^2, 2^3, 2^4, etc.

I'm unsure as to why you'd want to change the color the instant before you reboot the computer, since nobody will ever view the sub-millisecond change.
valithor #11
Posted 16 March 2016 - 08:54 PM
I'm unsure as to why you'd want to change the color the instant before you reboot the computer, since nobody will ever view the sub-millisecond change.

Since he is using slowPrint I would assume he wants to change the color right before he prints, so it is gone when the computer is printing "Rebooting…"
Hellreaper #12
Posted 16 March 2016 - 09:46 PM
Yeah the color issue is that it prints the "Rebooting…" with the green box on it, which is what I want to avoid if possible
Lupus590 #13
Posted 16 March 2016 - 10:16 PM
set the background colour back to black
Edited on 16 March 2016 - 09:16 PM
Hellreaper #14
Posted 16 March 2016 - 11:33 PM
set the background colour back to black

So that means the last parameter for a Fill command changes the background color entirely, thats a bit unusual if its in the function.
If u had to set it before that it would make sense but I guess I'll just have to do deal with it. thx for all the help (and pretty fast as well)
Lupus590 #15
Posted 17 March 2016 - 09:31 AM
What?

lets try an example


term.setBackgroundColour(colours.blue)
--# any writes or prints to the screen will now have a blue background

term.setBackgroundColour(colours.black)
--# any writes or prints now have a black background (which is the default)
--# this doesn't affect any writes or prints above this function call
--# it only affects those below it UNTIL this function is called again with a different colour

term.setBackgroundColour(colours.blue)
--# and now we are back to blue, anything that what printed after setting it black but before this is still in black
Edited on 17 March 2016 - 08:34 AM
Bomb Bloke #16
Posted 17 March 2016 - 10:04 AM
What?

He gets all that, he was simply surprised to learn that paintutils.drawFilledBox() also changes the background colour, same as term.setBackgroundColour() itself does.

(Which is, of course, because the former works by calling the latter, and then writing some empty spaces to the terminal.)
Hellreaper #17
Posted 18 March 2016 - 12:38 PM
Yeah, I expected that the paintUtils.DrawFilledBox() would set the color back to default at the end.
Just a minor thing I wasnt aware of