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

Monitor Coordinate Logic

Started by rules54, 15 July 2013 - 09:51 AM
rules54 #1
Posted 15 July 2013 - 11:51 AM
I'm writing a program which is to include DireWolf20's Button API. However, I don't like it's lack of flexibility. I want my program to be usable with monitors of varying sizes. So I'm trying to figure up the logic to determine the xMin, xMax, yMin, yMax of all of the buttons. The easiest way to tackle this seems to start off by finding the largest string length so all buttons can be of equal size.

Even without knowledge of the button API, this should be an easy concept for someone to grasp, and someone more experience to point me in the right direction. We have a set monitor size, set button size, and set padding between buttons and the monitor edge. I don't know how to go about determining where to put the buttons based on how many buttons there are and how much monitor room there is. Here is some code that I will eventually scrap but attempted to solve this problem nonetheless:


  for k,v in pairs(spawners) do
	local xmin, ymin, xmax, ymax = 0
	for i=1, possibleButtonColumns do
	  if buttonCurrent <= possibleButtonColumns then
		rowCurrent = 1
	  else
		if buttonCurrent <= pos
	if buttonCurrent <= possibleButtonColumns then
	  rowCurrent = 1
	else
	local rowCurrent = math.floor(buttonCurrent/possibleButtonColumns)
	local columnCurrent = math.floor(buttonCurrent/possibleButtonRows)
	if buttonCurrent == 1 then
	  xmin = 2 xmax = xmin+buttonSize-1
	  ymin = 3 ymax = ymin+2
	else
	 xmin = columnCurrent*buttonSize+2 xmax = xmin + buttonSize-1
	 ymin = (rowCurrent*3)+2 ymax = ymin + 2
	end
	button.setTable(v.name, v.toggle, xmin, xmax, ymin, ymax)
	buttonCurrent = buttonCurrent + 1
  end

To clarify, I want to be able to evenly space buttons according to how many buttons there are. I could figure out multiple pages on my own if we can figure out the spacing.

Here is the link to my full code, which is embedded in it's usage: http://pastebin.com/Q5nSMXEr
Edited on 15 July 2013 - 09:54 AM
Bubba #2
Posted 15 July 2013 - 02:09 PM
Here's a bit of psuedo-code that should help you out:

local termX, termY = term.getSize()

local nButtons = #buttonList --#This is the number of buttons that you have. I assigned it to the length of "buttonList", but you could get the number any way you wish

local nMaxButtonLength = nMaxButtonText + (nPadding*2) --#Account for having a bit of visual "sugar" on both sides of the button.
--#Otherwise the wrapping is just too close to the text

local nButtonDistance = (termX-(nMaxButtonLength*nButtons))/(nButtons-1) --#The distance in between each button
nButtonDistance = math.floor(nButtonDistance)

nButtonDistance is the variable that you are looking for. Say that the terminal is 100 pixels wide. You have these three buttons:
"Test"
"Hello world!" <-nMaxButtonLength = 12 + (padding*2) Generally padding is 1 pixels
"Goodbye"

(100 - (14*3))/(3-1)
(100 - (48)) / (2)
(52)/(2) = 27

So you have a length of 27 pixels to the right of each button.

Here is an example of how you could use that nButtonDistance variable.

local xCoord = 1 --#The current x-coord
for index=1, #buttons do
  drawButton(xCoord,yCoord,buttons[index]) --#Assuming that you have a drawButton function that takes (xcoord,ycoord,button)
  xCoord = xCoord + nMaxButtonLength + nButtonDistance
end

This will evenly spread your buttons across the screen.

Here is an example button api that I wrote up. Obviously it has no text, but you get the point :)/> Lower or raise the nButtons/nMaxButtonText variables to see the different effects.

Images below:
Spoiler
rules54 #3
Posted 16 July 2013 - 12:55 PM
Thank you! This helps a lot.

Also I can't help but notice your username is Bubba. Do you make youtube videos? I found a four part series on ComputerCraft GUis where the commentator referred to himself as Bubba.
Bubba #4
Posted 16 July 2013 - 01:35 PM
Thank you! This helps a lot.

Also I can't help but notice your username is Bubba. Do you make youtube videos? I found a four part series on ComputerCraft GUis where the commentator referred to himself as Bubba.

Indeed, that is me :)/>
rules54 #5
Posted 17 July 2013 - 02:23 AM
Code
button API
retreiver

When I call on the table at LN 201, none of the buttons are drawn. I believe it has something to do with the table creation process at LN 190. Everything inserts alright expect for the function button.setTable() I think it's the 2nd argument of button.setTable, v.toggle, which is for some reason not being called correctly later on. if you have any workarounds, or tips or anything really, let me know.

If you want to recreate my setup, here's a screenshot.
Spoiler

If you're willing to take a shot at it, I'd appreciate it. If not, I understand.