@Ignatz Here’s a mortgage calculator that uses a class with input/output text boxes and keyboard input. I’m not sure if you would want to use this or not. I even added comments so you don’t have to.
-- mortgage calculator
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
boxTab={} -- table of input/output boxes
-- x,y,w,h,type,text
table.insert(boxTab,input(100,500,120,30,1,"Loan $ amount"))
table.insert(boxTab,input(400,500,120,30,1,"Yearly % ( 5%=5 )"))
table.insert(boxTab,input(700,500,120,30,1,"Number of years"))
table.insert(boxTab,input(400,400,120,30,2,"Monthly payment"))
table.insert(boxTab,input(700,400,120,30,2,"Total payments"))
end
function draw()
background(34, 74, 82, 255) -- screen color
fill(255,0,0) -- set color to red
pushStyle()
fontSize(40)
text("MORTGAGE CALCULATOR",WIDTH/2,HEIGHT-50) -- draw text
popStyle()
fill(224, 149, 127, 255) -- set color
text("( Tap each box to input values )",WIDTH/2,HEIGHT-100) -- draw text
for a,b in pairs(boxTab) do -- loop thru table
b:draw() -- draw each box
end
amt=tonumber(boxTab[1].val) -- starting amount
int=tonumber(boxTab[2].val/100) -- yearly interest
year=tonumber(boxTab[3].val) -- number of years
-- perform interest calculation
i=int/12 -- interest per month
n=year*12 -- total months
w=(1+i)^n
m=amt*i*w/(w-1) -- calculate monthly amount
boxTab[4].val=string.format("%.2f",m) -- save monthly payment
boxTab[5].val=string.format("%.2f",m*n) -- save total payments
end
function touched(t) -- check which box is selected
for nbr,box in pairs(boxTab) do -- loop thru table
if box:touched(t) then -- box touched
return -- dont check other boxes, exit function
end
end
end
function keyboard(k)
for nbr,box in pairs(boxTab) do -- loop thru table
box:keyboard(k) -- get input from box
end
end
input=class()
function input:init(x,y,w,h,type,txt)
self.x=x -- x position
self.y=y -- y position
self.w=w -- width
self.h=h -- height
self.type=type -- 1=input box 2=output box
self.txt=txt -- text to show above box
self.val="0" -- data keyed in box
self.sel=false -- box selected true/false
end
function input:draw()
pushStyle()
rectMode(CORNER)
textMode(CORNER)
fill(0) -- set background for box
if self.sel then -- set selected color for box
fill(105, 101, 31, 255)
end
stroke(255, 66, 0, 255) -- box outline color
strokeWidth(2) -- outline size
rect(self.x,self.y,self.w,self.h) -- draw box
fill(255) -- set text color
text(self.txt,self.x,self.y+self.h) -- box name
text(self.val,self.x+4,self.y+4) -- box text
popStyle()
end
function input:keyboard(k)
if self.sel then
if k==BACKSPACE then -- backspace key pressed
str=str:sub(1,str:len()-1) -- remove last digit
if str=="" then -- blank
str="0" -- make 0
end
elseif k>="0" and k<="9" or k=="." then -- only digits 1 thru 9
if str:sub(1,1)=="0" then -- check if leading 0
str="" -- clear leading 0
end
str=str..k -- update keyed value
end
self.val=str -- save keyed value
end
end
function input:touched(t)
if t.state==BEGAN then
if not isKeyboardShowing() then
showKeyboard() -- show keyboard if its not displayed
end
str="0" -- clear value
for z=1,#boxTab do -- clear selected flag for all boxes
boxTab[z].sel=false
end
-- check which box was selected
if t.x>self.x and t.x<self.x+self.w and
t.y>self.y and t.y<self.y+self.h and self.type==1 then
self.val="0" -- reset val
self.sel=true -- set selected flag
return true -- a box was selected
end
end
return false -- no box was selected
end