From 63dbc6e26275e30ca101b52f29c0baf2eefc7157 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sun, 25 Jan 2015 18:32:17 -0500 Subject: [PATCH] examples/calculator: revamp style of the calculator code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- examples/calculator/Makefile | 4 +- examples/calculator/src/calculator_gtk.nit | 75 +++++++++++++------------- examples/calculator/src/calculator_logic.nit | 19 +++++-- 3 files changed, 55 insertions(+), 43 deletions(-) diff --git a/examples/calculator/Makefile b/examples/calculator/Makefile index f16f885..43cdcc4 100644 --- a/examples/calculator/Makefile +++ b/examples/calculator/Makefile @@ -1,6 +1,8 @@ all: mkdir -p bin/ - ../../bin/nitc --dir bin/ src/calculator_gtk.nit src/calculator_test.nit + + # Compile in global mode to silence warnings on unused GTK deprecated + ../../bin/nitc --global --dir bin/ src/calculator_gtk.nit src/calculator_test.nit android: mkdir -p bin/ res/ diff --git a/examples/calculator/src/calculator_gtk.nit b/examples/calculator/src/calculator_gtk.nit index bd2bd11..bd01986 100644 --- a/examples/calculator/src/calculator_gtk.nit +++ b/examples/calculator/src/calculator_gtk.nit @@ -21,17 +21,18 @@ import calculator_logic import gtk +# GTK calculator UI class CalculatorGui super GtkCallable - var win : GtkWindow is noinit - var container : GtkGrid is noinit + private var win: GtkWindow is noinit + private var container: GtkGrid is noinit - var lbl_disp : GtkLabel is noinit - var but_eq : GtkButton is noinit - var but_dot : GtkButton is noinit + private var lbl_disp: GtkLabel is noinit + private var but_eq: GtkButton is noinit + private var but_dot: GtkButton is noinit - var context = new CalculatorContext + private var context = new CalculatorContext redef fun signal(sender, op) do @@ -54,57 +55,57 @@ class CalculatorGui do init_gtk - win = new GtkWindow( 0 ) + win = new GtkWindow(0) - container = new GtkGrid(5,5,true) - win.add( container ) + container = new GtkGrid(5, 5, true) + win.add(container) - lbl_disp = new GtkLabel( "_" ) - container.attach( lbl_disp, 0, 0, 5, 1 ) + lbl_disp = new GtkLabel("_") + container.attach(lbl_disp, 0, 0, 5, 1) - # digits + # Digits for n in [0..9] do - var but = new GtkButton.with_label( n.to_s ) - but.request_size( 64, 64 ) - but.signal_connect( "clicked", self, n ) + var but = new GtkButton.with_label(n.to_s) + but.request_size(64, 64) + but.signal_connect("clicked", self, n) if n == 0 then - container.attach( but, 0, 4, 1, 1 ) - else container.attach( but, (n-1)%3, 3-(n-1)/3, 1, 1 ) + container.attach(but, 0, 4, 1, 1) + else container.attach(but, (n-1)%3, 3-(n-1)/3, 1, 1) end - # operators + # Operators var r = 1 - for op in ['+', '-', '*', '/' ] do - var but = new GtkButton.with_label( op.to_s ) - but.request_size( 64, 64 ) - but.signal_connect( "clicked", self, op ) - container.attach( but, 3, r, 1, 1 ) + for op in ['+', '-', '*', '/'] do + var but = new GtkButton.with_label(op.to_s) + but.request_size(64, 64) + but.signal_connect("clicked", self, op) + container.attach(but, 3, r, 1, 1) r+=1 end # = - but_eq = new GtkButton.with_label( "=" ) - but_eq.request_size( 64, 64 ) - but_eq.signal_connect( "clicked", self, '=' ) - container.attach( but_eq, 4, 3, 1, 2 ) + but_eq = new GtkButton.with_label("=") + but_eq.request_size(64, 64) + but_eq.signal_connect("clicked", self, '=') + container.attach(but_eq, 4, 3, 1, 2) # . - but_dot = new GtkButton.with_label( "." ) - but_dot.request_size( 64, 64 ) - but_dot.signal_connect( "clicked", self, '.' ) - container.attach( but_dot, 1, 4, 1, 1 ) - - #C - var but_c = new GtkButton.with_label( "C" ) - but_c.request_size( 64, 64 ) + but_dot = new GtkButton.with_label(".") + but_dot.request_size(64, 64) + but_dot.signal_connect("clicked", self, '.') + container.attach(but_dot, 1, 4, 1, 1) + + # C + var but_c = new GtkButton.with_label("C") + but_c.request_size(64, 64) but_c.signal_connect("clicked", self, 'C') - container.attach( but_c, 2, 4, 1, 1 ) + container.attach(but_c, 2, 4, 1, 1) win.show_all end end -# graphical application +# Do not show GUI in when testing if "NIT_TESTING".environ == "true" then exit 0 var app = new CalculatorGui diff --git a/examples/calculator/src/calculator_logic.nit b/examples/calculator/src/calculator_logic.nit index c5fbaf7..57d3b5a 100644 --- a/examples/calculator/src/calculator_logic.nit +++ b/examples/calculator/src/calculator_logic.nit @@ -1,7 +1,5 @@ # This file is part of NIT ( http://www.nitlanguage.org ). # -# Copyright 2013-2014 Alexis Laferrière -# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -17,12 +15,18 @@ # Business logic of a calculator module calculator_logic +# Hold the state of the calculator and its services class CalculatorContext + # Result of the last operation var result: nullable Numeric = null + # Last operation pushed with `push_op`, to be executed on the next push var last_op: nullable Char = null + # Value currently being entered var current: nullable FlatBuffer = null + + # Text to display on screen fun display_text: String do var result = result @@ -51,7 +55,8 @@ class CalculatorContext return buf.to_s end - fun push_op( op : Char ) + # Push operation `op`, will usually execute the last operation + fun push_op(op: Char) do apply_last_op_if_any if op == 'C' then @@ -65,7 +70,8 @@ class CalculatorContext self.current = null end - fun push_digit( digit : Int ) + # Push a digit + fun push_digit(digit: Int) do var current = current if current == null then current = new FlatBuffer @@ -78,6 +84,7 @@ class CalculatorContext end end + # Switch entry mode from integer to decimal fun switch_to_decimals do var current = current @@ -86,7 +93,8 @@ class CalculatorContext self.current = current end - fun apply_last_op_if_any + # Execute the last operation it not null + protected fun apply_last_op_if_any do var op = last_op @@ -106,6 +114,7 @@ class CalculatorContext else if op == '*' then result = result.mul(current.to_n) end + self.result = result self.current = null end -- 1.7.9.5