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

Is there any easier way to do this?

Started by Cranium, 06 December 2012 - 05:46 AM
Cranium #1
Posted 06 December 2012 - 06:46 AM
I have been playign around with teh colors in advanced computers, and have been making some new GUIs with fancy colors. The problem I am having is that it takes SOOOOO much more to just make something look good!
Is there any way that I can shorten this code?

--gui functions
local function draw()
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
local b = string.len("+---++---++---+")/2
local x = (tX/2)-b
term.setCursorPos(x,3)
write("+---++---++---+")
term.setCursorPos(x,4)
write("| ")
term.setTextColor(colors.gray)
write("1")
term.setTextColor(colors.white)
write(" || ")
term.setTextColor(colors.gray)
write("2")
term.setTextColor(colors.white)
write(" || ")
term.setTextColor(colors.gray)
write("3")
term.setTextColor(colors.white)
write(" |")
term.setCursorPos(x,5)
write("+---++---++---+")
term.setCursorPos(x,6)
write("+---++---++---+")
term.setCursorPos(x,7)
write("| ")
term.setTextColor(colors.gray)
write("4")
term.setTextColor(colors.white)
write(" || ")
term.setTextColor(colors.gray)
write("5")
term.setTextColor(colors.white)
write(" || ")
term.setTextColor(colors.gray)
write("6")
term.setTextColor(colors.white)
write(" |")
term.setCursorPos(x,8)
write("+---++---++---+")
term.setCursorPos(x,9)
write("+---++---++---+")
term.setCursorPos(x,10)
write("| ")
term.setTextColor(colors.gray)
write("7")
term.setTextColor(colors.white)
write(" || ")
term.setTextColor(colors.gray)
write("8")
term.setTextColor(colors.white)
write(" || ")
term.setTextColor(colors.gray)
write("9")
term.setTextColor(colors.white)
write(" |")
term.setCursorPos(x,11)
write("+---++---++---+")
term.setCursorPos(x,12)
write("+---++---++---+")
term.setCursorPos(x,13)
write("|")
term.setTextColor(colors.red)
write("CLR")
term.setTextColor(colors.white)
write("|| ")
term.setTextColor(colors.gray)
write("0")
term.setTextColor(colors.white)
write(" ||")
term.setTextColor(colors.green)
write("ENT")
term.setTextColor(colors.white)
write("|")
term.setCursorPos(x,14)
write("+---++---++---+")
end
bjornir90 #2
Posted 06 December 2012 - 06:52 AM
It's the problem with GUI, as you need to write something else egery time you must write it… I believe you can create a function that take the color, the cursor pos and the text and write it because you do that very often :)/>
Lyqyd #3
Posted 06 December 2012 - 07:24 AM
Separate out the parts of it. Make a function that draws the box, then draw in the rest afterward.
Kingdaro #4
Posted 06 December 2012 - 07:46 AM
I ran the code in ccemu and I think I know what you're going for. You could actually store all of your buttons' positions in a table, or generate them with a for loop and some nice math.


-- lay out the button labels
local labels = {
	' 1 ', ' 2 ', ' 3 ',
	' 4 ', ' 5 ', ' 6 ',
	' 7 ', ' 8 ', ' 9 ',
	'CLR', ' 0 ', 'ENT'
}

-- generate the objects
local objects = {}
for i=1, #labels do
	table.insert(objects, {
		x = ((i - 1)%3 + 1)*5;
		y = math.ceil(i/3) * 3;
		label = labels[i];
		-- make CLR red and ENT green
		color = i==10 and colors.red or i==12 and colors.green or colors.gray;
	})
end

local function draw()
	term.clear()
	for i=1, #objects do
		local obj = objects[i]
		term.setTextColor(colors.white)
		
		for num, line in pairs{'+---+','|   |','+---+'} do
			term.setCursorPos(obj.x, obj.y + num - 1)
			write(line)
		end
		
		term.setCursorPos(obj.x+1, obj.y+1)
		term.setTextColor(obj.color)
		write(obj.label)
	end
end

Excuse the tabs, I made this in NP++ :lol:/>/>
Cranium #5
Posted 06 December 2012 - 07:56 AM
Separate out the parts of it. Make a function that draws the box, then draw in the rest afterward.
I don't look forward to that :P/>

I ran the code in ccemu and I think I know what you're going for. You could actually store all of your buttons' positions in a table, or generate them with a for loop and some nice math.


-- lay out the button labels
local labels = {
	' 1 ', ' 2 ', ' 3 ',
	' 4 ', ' 5 ', ' 6 ',
	' 7 ', ' 8 ', ' 9 ',
	'CLR', ' 0 ', 'ENT'
}

-- generate the objects
local objects = {}
for i=1, #labels do
	table.insert(objects, {
		x = ((i - 1)%3 + 1)*5;
		y = math.ceil(i/3) * 3;
		label = labels[i];
		-- make CLR red and ENT green
		color = i==10 and colors.red or i==12 and colors.green or colors.gray;
	})
end

local function draw()
	term.clear()
	for i=1, #objects do
		local obj = objects[i]
		term.setTextColor(colors.white)
		
		for num, line in pairs{'+---+','|   |','+---+'} do
			term.setCursorPos(obj.x, obj.y + num - 1)
			write(line)
		end
		
		term.setCursorPos(obj.x+1, obj.y+1)
		term.setTextColor(obj.color)
		write(obj.label)
	end
end

Excuse the tabs, I made this in NP++ :lol:/>/>/>

Love it. I just love it. My work oriented brain doesn't understand it right now, but i'll pick it apart when I get home and put my code oriented brain back in.
Cranium #6
Posted 06 December 2012 - 09:30 AM
I ran the code in ccemu and I think I know what you're going for. You could actually store all of your buttons' positions in a table, or generate them with a for loop and some nice math.

-snip-

Excuse the tabs, I made this in NP++ :lol:/>/>
Ok, so using THIS, I was able to test it. It's not centered, and I would like it to move down two blocks. What part of your code deals with that?
Kingdaro #7
Posted 06 December 2012 - 10:06 AM
Should probably explain the basic premise of the entire thing, that'd be helpful.


-- lay out the button labels
local labels = {
        ' 1 ', ' 2 ', ' 3 ',
        ' 4 ', ' 5 ', ' 6 ',
        ' 7 ', ' 8 ', ' 9 ',
        'CLR', ' 0 ', 'ENT'
}

This is only for reference, so that I have a nice table of labels to go through and generate.


-- generate the objects
local objects = {}
for i=1, #labels do
        table.insert(objects, {
                x = ((i - 1)%3 + 1)*5;
                y = math.ceil(i/3) * 3;
                label = labels[i];
                -- make CLR red and ENT green
                color = i==10 and colors.red or i==12 and colors.green or colors.gray;
        })
end

This part of the code goes through all the labels, and through wonderful math, generates x and y values in the following pattern, (1,1) (1,2) (1,3) (2,1) (2,2) (2,3) and so on. However I added *3 and *5 at the end of each formula to accommodate for the sizes of the buttons.

Each button "object" has no width or height, only position, label, and color.


local function draw()
        term.clear()
        for i=1, #objects do
                local obj = objects[i]
                term.setTextColor(colors.white)

                for num, line in pairs{'+---+','|   |','+---+'} do
                        term.setCursorPos(obj.x, obj.y + num - 1)
                        write(line)
                end

                term.setCursorPos(obj.x+1, obj.y+1)
                term.setTextColor(obj.color)
                write(obj.label)
        end
end

This part goes through the objects table, which contains a list of tables containing x and y values. For each button, it draws the "frame" first. I've defined a constant table to go through with pairs(), and it writes each line of the button's "frame". The x and y position are at the top-left of each button.

It then goes to the middle of the frame, and draws the button's label with the button's color.

So to answer your question, to center the buttons, you could either change the formula in the part that actually generates the button objects, or you could simply edit the draw function to draw all of the buttons with an added X value to make them centered.
Cranium #8
Posted 06 December 2012 - 10:11 AM
Simple. I just did this for the position lines!

x = (((i - 1)%3 + 1)*5) + 14;
y = (math.ceil(i/3) * 3) + 3;