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

[Lua] Help with buttons

Started by soccer16x, 01 February 2013 - 12:36 PM
soccer16x #1
Posted 01 February 2013 - 01:36 PM
Okay so I have a program thats a calculator, and what I am trying to do is to get rid of one the sections so that way i can redraw that section and then put in different functions there, but what currently happens when i attempted this, all that happened was that everything on the calculator went blank and when you clicked anything the whole screen would go white. Can anyone help me figure out how to do what I want?

The function thats supposed to do all of the stuff is when numb == "F" and i left in what im currently trying to do in there. And here's all the code:
Spoiler
button = {}
input = {}
calc = ""

term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()

function printVerticalLine(lineX, startingY, endingY)
local curX, curY = term.getCursorPos()
term.setBackgroundColor(colors.white)
for lineY = startingY, endingY do
term.setCursorPos(lineX, lineY)
term.write(" ")
end
term.setCursorPos(curX, curY)
end

function printHorizontalLine(yLine, startingX, endingX)
local curX, curY = term.getCursorPos()
term.setBackgroundColor(colors.white)
for xLine = startingX, endingX do
term.setCursorPos(xLine, yLine)
term.write(" ")
end
term.setCursorPos(curX, curY)
end

function drawTable()
 printVerticalLine(2,2,18)
 --Left Border
 printVerticalLine(20,2,18)
 --Right Border
 printVerticalLine(8,6,18)
 --Left Seperator
 printVerticalLine(14,6,18)
 --Right Seperator
 printHorizontalLine(2,2,20)
 --Top
 printHorizontalLine(6,2, 20)
 --Upper Before Keys
 printHorizontalLine(18,2,20)
 --Bottom
 printHorizontalLine(10,2,20)
 --Upper Seperator
 printHorizontalLine(14,2,20)
 --Lower Seperator
 term.setBackgroundColor(colors.black)
end

function drawFunctionsTable()
 printHorizontalLine(6,32,50)
 --Top
 printHorizontalLine(10,32,50)
 printHorizontalLine(14,32,50)
 printHorizontalLine(18,32,50)
 --Bottom
 printVerticalLine(32,6,18)
 --Left Border
 printVerticalLine(38,6,14)
 printVerticalLine(44,6,18)
 printVerticalLine(50,6,18)
 --Right Border

 term.setBackgroundColor(colors.black)
end

function drawExtraButtonsTable()
 --Help, Color settings, 2nd button for filling table with ExtraFunctions
 printVerticalLine(23,6,18)
 --Left Border
 printVerticalLine(29,6,18) 
 --Right Border
 printHorizontalLine(6,23,29)
 --Top
 printHorizontalLine(10,23,29)
 printHorizontalLine(14,23,29)
 printHorizontalLine(18,23,29)
 --Bottom
end

function setTable(name, xmin, xmax, ymin, ymax)
 button[name] = {}
 button[name]["numb"] = name
 button[name]["xmin"] = xmin
 button[name]["xmax"] = xmax
 button[name]["ymin"] = ymin
 button[name]["ymax"] = ymax
end

function fillTable()
 setTable("1", 3, 7, 15, 17)
 setTable("2", 9, 13, 15, 17)
 setTable("3", 15, 19, 15, 17)
 setTable("4", 3, 7, 11, 13)
 setTable("5", 9, 13, 11, 13)
 setTable("6", 15, 19, 11, 13)
 setTable("7", 3, 7, 7, 9)
 setTable("8", 9, 13, 7, 9)
 setTable("9", 15, 19, 7, 9)
end

function fillFunctionsTable()
 setTable("C", 33,37,7,9)
 setTable(".", 39,43,7,9)
 setTable("/", 45,49,7,9)
 setTable("+", 33,37,11,13)
 setTable("-", 39,43,11,13)
 setTable("*", 45,49,11,13)
 setTable("0", 33,43,15,17)
 setTable("=", 45,49,15,17)
end

function fillExtraFunctions()
 setTable("^", 33,37,7,9)
 setTable("rt", 39,43,7,9)
 --setTable("", 45,49,7,9)
 --setTable("", 33,37,11,13)
 --setTable("", 39,43,11,13)
 --setTable("", 45,49,11,13)
 --setTable("", 33,43,15,17)
 --setTable("", 45,49,15,17)
end

function fillExtraButtonsTable()
 setTable("F",24,28,7,9)
 setTable("H",24,28,11,13)
 setTable("c",24,28,15,17)
end

function checkClick(tab, mx, my)
for i, v in pairs(tab) do
if mx >= v.xmin and mx <= v.xmax and my >= v.ymin and my <= v.ymax then
return true, v.numb
end
end
return false, nil
end

function screen()
term.setBackgroundColour(colours.black)
term.setTextColour(colours.white)
for name,data in pairs(button) do
local yspot = (data["ymin"] + data["ymax"]) / 2
local xspot = (data["xmin"] + data["xmax"]) / 2
term.setCursorPos(xspot, yspot)
write(name)
end
end

function redraw()
 term.setCursorPos(4,4)
 term.clearLine()
 drawTable()
end

function displayHelp()
 term.clear()
 Help()
end

function setInput1()
 input[1] = tonumber(calc)
 input[2] = numb
 redraw()
 calc = ""
 numb = ""
end

function enterPress()
 input[3] = tonumber(calc)
 redraw()
 calc = ""
 numb = ""
 if input[2] == "+" then
  Output = input[1] + input[3]
 elseif input[2] == "-" then
  Output = input1 - input[3]
 elseif input[2] == "*" then
  Output = input[1] * input[3]
 elseif input[2] == "/" then
  Output = input[1] / input[3]
 elseif input[2] == "^" then
  Output = input[1] ^ input[3]
 elseif input[2] == "rt" then
  Output = math.sqrt(input[1]) 
 end 
 write(Output)
 input[1] = Output  
end
--[[ Running Code ]]-- 

drawTable()
drawFunctionsTable()
drawExtraButtonsTable()
fillTable()
fillFunctionsTable()
fillExtraButtonsTable()
screen()

while true do
 local e, but, x, y = os.pullEvent("mouse_click")
 if but == 1 then
  bValid, numb = checkClick(button, x, y)
  if bValid then
   if numb == "C" then 
    redraw()
    for i = 1,3 do
     input[i] = nil
    end
    calc = ""
   else
    if numb == "+" then
     setInput1()
    elseif numb == "-" then
     setInput1()
    elseif numb == "*" then
     setInput1()
    elseif numb == "/" then
     setInput1()
    elseif numb == "^" then
     setInput1()
    elseif numb == "rt" then
     setInput1()
    elseif numb == "H" then
     displayHelp()
    elseif numb == "F" then
     term.clear()
     redraw()
     drawFunctionsTable()
     drawExtraButtonsTable()
     fillTable()
     fillExtraFunctions()
     fillExtraButtonsTable()
    elseif numb == "=" then
     enterPress()
    end

    calc = calc .. numb
    term.setCursorPos(4, 4)
    if #calc < 14 then
     write(calc)
    else
     write(calc:sub(#calc-14))
    end
   end
  end
 end
end
Orwell #2
Posted 01 February 2013 - 02:31 PM
term.clear() clears all content on the screen (obviously), but also paints the screen to the background color that is currently set. If you want to redraw certain sections, my advise is either clearLine() (like you do at some places) if the sections span the whole width of the screen.
Or, if it's an arbitrary section, I usually 'paint it over' with all spaces. For example, if you have any rectangle at section and it has the background color 'col', you could do:

--[[ x and y are the right upper corner of the rectangle
	 w and h are the width and height of the rectangle
	 col is the background color that an empty rectangle has
local function clearSection(x,y,w,h,col)
  local hLine = string.rep(" ",w) -- create a string of w spaces
  term.setBackgroundColor(col) -- use the background color
  for dy=1,h do  -- draw all horizontal lines
    term.setCursorPos(x,y+dy-1)
    term.write(hLine)
  end
end

I'm not entirely sure that I correctly understood your issue. If not, be sure to ask again here. :)/>
soccer16x #3
Posted 01 February 2013 - 02:38 PM
Well, that cleared up on thing that i wanted to do, which was just to get rid of one section, but also what i need to do i think is make it so the buttons i have previously go away so i can rewrite new buttons where the old ones were so that way i can have the new buttons do different functions then the previous ones.

So like what i want to do is replace the fillFunctionsTable() with fillExtraFunctions()
soccer16x #4
Posted 02 February 2013 - 07:45 AM
Can anyone help me accomplish this? or would it not be possible…
remiX #5
Posted 02 February 2013 - 09:56 AM
I'll go over it now. Haven't looked at the code yet but so if you click "F" it should do what? Change the options?
remiX #6
Posted 02 February 2013 - 11:15 AM
Ok, well I have fixed it: pastebin

After I fixed it I was messing around with it because it was fun xD And then I forgot what the exactly problem was again! Stupid me!
But it was mostly because you stored everything into one table.

Ag, I'm mad now that I forgot what the problem was again :|
soccer16x #7
Posted 02 February 2013 - 12:29 PM
Yeah, that was what my problem was, I wanted to have it where when you clicked F a new set of available functions would show up, like as of right now the new functions that would show up would be exponents and sq roots. But it looks like your fix should work for it and ill test it soon.
soccer16x #8
Posted 02 February 2013 - 12:45 PM
Okay yeah, tested it and it works exactly how I was trying to get it to work :)/> thanks! Oh and a hint at what is to come, the H button will display just a simple little Help menu and the c button is going to let you pick which color you would like to set the background to be, because white is boring :P/> , and eventually it will save what color you selected so everytime you start the program you'll have that color right off the start but for now im just going to get colors implemented.
remiX #9
Posted 02 February 2013 - 01:02 PM
Ohh xD I was wondering what the c was for! But I did the H for you xD
soccer16x #10
Posted 02 February 2013 - 01:06 PM
Yeah figured someone might have been :P/> and yeah I just noticed, thanks
soccer16x #11
Posted 02 February 2013 - 03:12 PM
Ok, hey when i put in the color functionality it ended up making the script completely not work and just print one line of nothing and then exit out. Can you help? the code is here
remiX #12
Posted 03 February 2013 - 12:39 AM
Ok well, you didn't take my code and edit it from there, but instead you took your old one and added what I had changed - I would guess?

Well, I changed quite a few bugs and now the code you provided still has those old bugs which I fixed and I don't feel like fixing them again :| Like clicking H doesn't work, clicking F and F again will make the . and rt together because you don't clear the screen, doing a sum and then doing another sum will error.

Also I added variables such as:



buttonsMain = {}
buttonsExtra = {}

screenX, screenY = term.getSize()

And they weren't there.
soccer16x #13
Posted 03 February 2013 - 02:28 AM
Yeah what I did was just go through the one you had and replace whatever was different on mine, I guess I must've missed stuff. Ill just use yours now and add in the new things.
soccer16x #14
Posted 03 February 2013 - 03:33 AM
Okay so after putting it back to what you pastebinned, I tried implementing the colors options and things, but when you click "c" nothing happens. So sorry to bother you again, but can you help? I thought I was doing it right :\ but apparently not :\

pastebin here
remiX #15
Posted 03 February 2013 - 06:15 AM
The reason was because you print everything, but then it clears and then prints the buttons for buttonsMain, because you do not have a sleep or os.pullEvent so you can click which colour to choose :P/>

Fixed, finally :D/>
soccer16x #16
Posted 03 February 2013 - 06:56 AM
Yeah, worked perfectly, already pushed update to the program :)/> gave you some credit for the update :)/>