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

[Buttons] Auto alignment

Started by Goof, 29 June 2015 - 10:46 PM
Goof #1
Posted 30 June 2015 - 12:46 AM
Hey folks


i am having trouble auto aligning buttons, where the X is defined by 1,2,3,4, like this:

{
  Name = 'Button1',
  Text = 'Button1',
  Application = 'anywhere.app',
  X = 1,
  Y = 1, 
},
{
  Name = 'Button2',
  Text = 'Button2',
  Application = 'anywhere.app',
  X = 2,
  Y = 1, 
},
Note, that the X and Y are not EXACT positions, which is the cause of my problem.

so in theory, i want the script to take the first button with X=1 and
set that as the first button, of course. the next button needs to be the X PLUS the width and a space.

This is the output of an automated X, with the preset of max 3 buttons per Y
Spoiler
Code:
Spoiler

local Count = 0
local Width = 15
local Height = 3
for k, v in ipairs(Buttons) do
    local X = 3 + (Count%3)*(Width+1)
    local Y = 2 + (math.floor(Count/3)*(Height+1))
    Count = Count + 1
    -- use X and Y here to create a button.
    api.createButton(X, Y, Width, Height, colors.gray)
end

This is the output of my stupid attempt at making it auto calculate FROM the x
Spoiler
Spoiler

local Count = 0
local Width = 15
local Height = 3
for k, v in ipairs(Buttons) do
    local X = 3 + v.X * (Count%3)*Width
    local Y = 2 + (math.floor(Count/3)*(Height+1))
    Count = Count + 1
    -- use X and Y here to create a button.
    api.createButton(X, Y, Width, Height, colors.gray)
end

So the question becomes, how to make it autoalign, with a preset WIDTH and HEIGHT, but a varying X and Y, which needs to be the parent to the WIDTH and HEIGTH.


Thanks in Advance
Edited on 29 June 2015 - 10:46 PM
Bomb Bloke #2
Posted 30 June 2015 - 02:21 AM
I'm not entirely sure I understand what you're asking, but it sounds like you want to do this?:

local Width = 15
local Height = 3
for k, v in ipairs(Buttons) do
    local X = 4 + (v.X - 1) * (Width + 1)
    local Y = 3 + (v.Y - 1) * (Height + 1)
    -- use X and Y here to create a button.
    api.createButton(X, Y, Width, Height, colors.gray)
end
Goof #3
Posted 30 June 2015 - 02:42 AM
I'm not entirely sure I understand what you're asking, but it sounds like you want to do this?:

local Width = 15
local Height = 3
for k, v in ipairs(Buttons) do
	local X = 4 + (v.X - 1) * (Width + 1)
	local Y = 3 + (v.Y - 1) * (Height + 1)
	-- use X and Y here to create a button.
	api.createButton(X, Y, Width, Height, colors.gray)
end

Yes, thats doing exactly what i want.
Thanks for your response