Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NGUI examples
#3
(01-21-2024, 03:15 PM)Marcus Wrote: I must have planned to write some examples for the widget library ngui, for I found a couple in a folder. I've attached the ones I found and I'll try to write some more - there are many widgets to cover.

buttons.n7 is probably a good start.
...

I am trying to use NGUI:Button and here it is a Calculator (simplified version) 
Limitation : ... it's my old brain and my little knowledge ... Big Grin Big Grin Big Grin
  - calculation between 1 digit number only e.g : 3 + 4
  - to make a calculation, use 1 type of operator only  e.g : +, -, / or *
  - use equal sign "=" to find the result and to clear the calculator's screen

Code:
'----------------
' INITIALIZATION
'----------------
'#win32 'turn off for debugging purpose
include "ngui.n7" 'GUI library
set window "Calculator simplified",32,85,false,4

visible wRoot           'main container
visible displayLabel    'display result
visible i=0             'counter
visible number = []     'hold the value of button.txt
visible op              'hold the type of math operation

Layout()

'--------------
' MAIN PROGRAM
'--------------
EnterMainLoop(wRoot)

'-----------
' FUNCTIONS
'-----------
function Layout()

    wRoot = VBox(SIZE_EXPAND,SIZE_EXPAND) 'create container

    line0 = HBox(SIZE_EXPAND,SIZE_AUTO)
    wRoot.Add(line0)    'add line0 of HBox to container
    line0.SetPadding(1) 'spacing between HBox and the container
    displayLabel = Label("Hello",SIZE_EXPAND,14)
    line0.Add(displayLabel)

    line1 = HBox(SIZE_EXPAND,SIZE_AUTO)
    wRoot.Add(line1)   
    line1.SetPadding(1);line1.SetSpacing(1)
    line1.Add(TextButton("7",SIZE_EXPAND,14,Operand))
    line1.Add(TextButton("8",SIZE_EXPAND,14,Operand))
    line1.Add(TextButton("9",SIZE_EXPAND,14,Operand))
    line1.Add(TextButton("+",5,14,Operator))

    line2 = HBox(SIZE_EXPAND,SIZE_AUTO)
    wRoot.Add(line2)
    line2.SetPadding(1);line2.SetSpacing(1)
    line2.Add(TextButton("4",SIZE_EXPAND,14,Operand))
    line2.Add(TextButton("5",SIZE_EXPAND,14,Operand))
    line2.Add(TextButton("6",SIZE_EXPAND,14,Operand))
    line2.Add(TextButton("-",SIZE_EXPAND,14,Operator))

    line3 = HBox(SIZE_EXPAND,SIZE_AUTO)
    wRoot.Add(line3)
    line3.SetPadding(1);line3.SetSpacing(1)
    line3.Add(TextButton("1",SIZE_EXPAND,14,Operand))
    line3.Add(TextButton("2",SIZE_EXPAND,14,Operand))
    line3.Add(TextButton("3",SIZE_EXPAND,14,Operand))
    line3.Add(TextButton("x",5,14,Operator))

    line4 = HBox(SIZE_EXPAND,SIZE_AUTO)
    wRoot.Add(line4)
    line4.SetPadding(1);line4.SetSpacing(1)
    line4.Add(TextButton("0",SIZE_EXPAND,14,Operand))
    line4.Add(TextButton("=",SIZE_EXPAND,14,Result))
    line4.Add(TextButton(":",5,14,Operator))

endfunc

function Operand(button)
    pln i 'for debugging purpose
    number[i] = button.txt
    pln number[i] 'for debugging purpose
    displayLabel.SetText(number[i]) 
    i = i+1   
    if i > 2 then displayLabel.SetText("Error")'only allowed 1 digit calculation
endfunc

function Operator(button)
    op =  button.txt
    pln op 'for debuging purpose
    displayLabel.SetText(op)
endfunc

function Result(button)
    pln i 'for debugging purpose
    if i = 2 then
        select case op
            case "+";displayLabel.SetText(int(number[0])+int(number[1]))
            case "-";displayLabel.SetText(int(number[0])-int(number[1]))
            case "x";displayLabel.SetText(int(number[0])*int(number[1]))
            case ":";
                if number[1] > 0 then
                    displayLabel.SetText(int(number[0])/int(number[1]))
                else
                    displayLabel.SetText("Error")
                endif
        endsel
    else
        displayLabel.SetText("")
    endif
    i = 0 'counter reset to zero for the next calculation
endfunc


Attached Files
.n7   calculator.n7 (Size: 3.38 KB / Downloads: 7)
Reply


Messages In This Thread
NGUI examples - by Marcus - 01-21-2024, 03:15 PM
RE: NGUI examples - by kevin - 01-21-2024, 05:07 PM
RE: NGUI examples - by 1micha.elok - 01-30-2024, 03:46 AM
RE: NGUI examples - by Marcus - 01-30-2024, 09:40 PM
RE: NGUI examples - by 1micha.elok - 02-01-2024, 09:29 AM
RE: NGUI examples - by kevin - 01-31-2024, 02:32 PM
RE: NGUI examples - by Marcus - 01-31-2024, 04:11 PM
RE: NGUI examples - by Marcus - 02-01-2024, 01:45 PM
RE: NGUI examples - by 1micha.elok - 02-05-2024, 09:09 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)