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

Lua[Help] with a mouse controlled calculator

Started by soccer16x, 29 January 2013 - 04:29 PM
soccer16x #1
Posted 29 January 2013 - 05:29 PM
Okay, so basically what I am trying to do is have a calculator that you operate all by click the mouse, I have gotten the writing of the calculator down and the clicking of the buttons, but what i have not gotten is how to turn what the person inputs into something that i could use to add together. I was thinking maybe that there is like a way to make each individual button press an individual string and then you'd be able to connect the strings together? But honestly I have no clue on how I could achieve what I'm trying to get at here. So if anyone has the time could they give me some helpful tips or some code that they'd be able to contribute to this calculator?
GravityScore #2
Posted 29 January 2013 - 05:45 PM
Well, when a person clicks a button, you could have an input string which you append the number clicked to. When the person clicks =, you could evaluate that string. Also, when the user types in a number you could do the same, and append the typed number to the input string.


input = "" -- The input string you will want to evaluate

while true do
  local e, but, x, y = os.pullEvent()
  if e == "mouse_click" then
	if theUserClickedACalculatorButton == "=" then
      -- Evaluate
	else
      input = input .. theNumberTheUserClicked
    end
  elseif e == "key" then
	if but:find("[1234567890]{1}") then
	  input = input .. but
	end
  end
end
soccer16x #3
Posted 29 January 2013 - 05:57 PM
Well, when a person clicks a button, you could have an input string which you append the number clicked to. When the person clicks =, you could evaluate that string. Also, when the user types in a number you could do the same, and append the typed number to the input string.


input = "" -- The input string you will want to evaluate

while true do
  local e, but, x, y = os.pullEvent()
  if e == "mouse_click" then
	if theUserClickedACalculatorButton == "=" then
	  -- Evaluate
	else
	  input = input .. theNumberTheUserClicked
	end
  elseif e == "key" then
	if but:find("[1234567890]{1}") then
	  input = input .. but
	end
  end
end

This would still work if the user was using multiple numbers before& after the operation correct? Also what do you mean by evaluate?
ikke009 #4
Posted 29 January 2013 - 09:52 PM
with evaluate he means calculate the result from the input.
But i wonder how that would work if your input is a string? When i read the OP i thought of having each input character in a table, and then have complicated code to get some results out of it, but there must be a simpler way.. toNumber() maybe?
GravityScore #5
Posted 29 January 2013 - 10:55 PM
with evaluate he means calculate the result from the input.
But i wonder how that would work if your input is a string? When i read the OP i thought of having each input character in a table, and then have complicated code to get some results out of it, but there must be a simpler way.. toNumber() maybe?

There is - you could use loadstring to run the string and get the evaluated expression. You just have to make sure there is only the math code in the string, which there should be if you write the program correctly.
remiX #6
Posted 30 January 2013 - 03:20 AM
What I would do is have a table with each number in a table with each x position and y position in the table as well for easy printing and checking if you clicked a number, enter or clear.

I made a Simple Moveable Keypad a while back for someone so you could check that to see how I did it.

The way you can move it is by changing the startXpos and the startYpos values. This value is where the number '1' will be printed within the keypad so don't make the starting position 1, 1.
Also the codes can be easily changed of course.

Also, right now the keypad accepts the number only when you actually click the number, not 1 left or 1 right of it for the perfect gap. If you want it like this, change the checkMouseClick function to this:


function checkMouseClick(_xPos, _yPos)
	for i = 1, #t_keypad do
		if _xPos >= t_keypad[i].xPos - 1 and _xPos <= (t_keypad[i].xPos + (string.len(t_keypad[i].text))) and
		_yPos == t_keypad[i].yPos then
			return true, t_keypad[i].text
		end
	end
	return false, nil
end
soccer16x #7
Posted 30 January 2013 - 07:20 AM
I already have a table that writes it and does button clicks and things like that, what I was needing was a way to turn those clicks into a way where I could get it to work, and I think what's been said above will work and once I get back home ill try to implement it and get it to work.
remiX #8
Posted 30 January 2013 - 07:24 AM
Well show us the code so we can see how you're doing it. You will need to iterate through the table checking if the clicked position could be equal to where the text from that table is printed.
soccer16x #9
Posted 30 January 2013 - 08:48 AM
this is my code as of right now : http://pastebin.com/Xe1jFBCv
remiX #10
Posted 30 January 2013 - 09:30 AM
What you have is fine. But you need to change the checkxy to return the number clicked…

Your version fixed

My version (38 lines shorter) - your fill function wasn't needed :P/>

Also, you will need to add enter and clear… also the number 0…
soccer16x #11
Posted 30 January 2013 - 10:39 AM
Thanks, and yeah i know i still need to add enter,multiply, devide, add, subtract, and 0. Will all of those still work in the version that you had made?

EDIT: Okay well I figured out how to clear which was relatively easy, but I still dont know how I would implement multiplying, dividing, adding, and subtracting
remiX #12
Posted 30 January 2013 - 05:47 PM
Thanks, and yeah i know i still need to add enter,multiply, devide, add, subtract, and 0. Will all of those still work in the version that you had made?

EDIT: Okay well I figured out how to clear which was relatively easy, but I still dont know how I would implement multiplying, dividing, adding, and subtracting

Yeah it will work?

Well, you need to add more buttons into the buttons table etc, not hard :P/>
soccer16x #13
Posted 30 January 2013 - 06:08 PM
I got it to work, ill add it eventually to programs in case anyone else just feels like having a calculator :P/> , and once i fix up a few things and im planning on writing a help menu and just adding general keyboard support to it.
remiX #14
Posted 30 January 2013 - 11:32 PM
Good to know it's going well :P/> goodluck!
soccer16x #15
Posted 31 January 2013 - 05:43 PM
Here's the "finished" program if you'd like to check it out: http://www.computercraft.info/forums2/index.php?/topic/10079-simple-clickable-calculator/