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

Button API Help

Started by grand_mind1, 21 May 2013 - 03:15 PM
grand_mind1 #1
Posted 21 May 2013 - 05:15 PM
I just got back to a program I was working on a few weeks ago which is a button api so I can easily make buttons or boxes and center the text and all that stuff that is usually involved with buttons. I have most of it working but I have one problem that I can't figure out with the part that centers the text. I'm pretty sure I am doing something wrong with the text centering on the X axis. This is what is looks like when just putting a button at 1,1:


However when I try to put a button next to it, things get a bit wonky:


The text from the second button next to it is put in an unexpected place. I worked out the math with the specific coordinates and that is exactly where the text is supposed to be according to the code/math.
API:
http://pastebin.com/KgBJMtBg
Code used to run it:
http://pastebin.com/w0bxsynv

If someone could please take the time to read my code and try to figure out what is wrong, that would be great.
Help is appreciated!
Thanks! :D/>
H4X0RZ #2
Posted 21 May 2013 - 05:49 PM
I can't look at your code, but have an Idea:

function newButton(sX,sY,eX,eY,bgcol,txtcol,txt,centered) --add your other params and code
local sX = sX
local sY = sY
local eX = eX
local eY = eY
local currY = sY
local centered = centered or false
term.setBackgroundColor(bgcol)
term.setTextColor(txtcol)
term.setCursorPos(sX,sY)
for i = sY, eY do
term.setCursorPos(sX, currY)
for u = sX,eX do
term.write(" ")
end
currY = currY + 1
end
if centered then
term.setCursorPos((eX-string.len(txt))/2, eY/2)
term.write(txt)
else
term.setCursorPos(sX,sY)
term.write(txt)
end
end

I think it will work :)/>
grand_mind1 #3
Posted 21 May 2013 - 05:54 PM
Um thanks but no thanks. I don't really like just copying and pasting code that someone else made. I would rather just fix what I have. Although I guess it could be possible that my code can't do what I want without a complete re-write. Again, thanks for taking your time to reply.
LordIkol #4
Posted 22 May 2013 - 05:26 AM
Hi Grand,

You made a mistake in Calculating the x axis.

this should work.
first i switched your calculation so that it adds the value you calculated to the xmin.
then i recognized that xmax is not the endposition of the button instead it is the lengh of the button.
so you just have to Subtract the text size from xmax divide by 2 and then add this to your startingposition.


function createButton(name)
	    mon.setBackgroundColor(button[name]["color"])
	    xLoc =button[name]["xmin"] + (math.ceil((button[name]["xmax"] - string.len(button[name]["text"]))/2))
	    yLoc = math.floor((button[name]["ymax"]+button[name]["ymin"])/2)
	    createBox(name)
	    mon.setCursorPos(xLoc,yLoc)
	    mon.write(button[name]["text"])
end

hope that helps

Greets Loki