e1792bf67e5deb84cbad692ff119759996de5897
[nit.git] / examples / calculator / src / calculator_android.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Android calculator application
18 module calculator_android is
19 app_name "app.nit Calc."
20 app_version(0, 1, git_revision)
21 java_package "org.nitlanguage.calculator"
22
23 # Use a translucent background and lock in portrait mode
24 android_manifest_activity """
25 android:theme="@android:style/Theme.Holo.Wallpaper"
26 android:screenOrientation="portrait""""
27 end
28
29 # FIXME the next line should import `android` only when it uses nit_activity
30 import android::log
31 import android::ui
32
33 import calculator_logic
34
35 redef class App
36 private var context = new CalculatorContext
37
38 # The main display, at the top of the screen
39 private var display: EditText
40
41 # Maps operators as `String` to their `Button`
42 private var op2but = new HashMap[String, Button]
43
44 # Has this window been initialized?
45 private var inited = false
46
47 redef fun init_window
48 do
49 super
50
51 if inited then return
52 inited = true
53
54 # Setup UI
55 var context = native_activity
56 var layout = new NativeLinearLayout(context)
57 layout.set_vertical
58
59 # Display screen
60 var display = new EditText
61 layout.add_view_with_weight(display.native, 1.0)
62 display.text_size = 36.0
63 self.display = display
64
65 # Buttons; numbers and operators
66 var ops = [["7", "8", "9", "+"],
67 ["4", "5", "6", "-"],
68 ["1", "2", "3", "*"],
69 ["0", ".", "C", "/"],
70 ["="]]
71
72 for line in ops do
73 var buts_layout = new NativeLinearLayout(context)
74 buts_layout.set_horizontal
75 layout.add_view_with_weight(buts_layout, 1.0)
76
77 for op in line do
78 var but = new Button
79 but.text = op
80 but.text_size = 40
81 buts_layout.add_view_with_weight(but.native, 1.0)
82 op2but[op] = but
83 end
84 end
85
86 context.content_view = layout
87 end
88
89 redef fun catch_event(event)
90 do
91 if event isa ClickEvent then
92 var sender = event.sender
93 var op = sender.text
94
95 if op == "." then
96 sender.enabled = false
97 context.switch_to_decimals
98 else if op.is_numeric then
99 var n = op.to_i
100 context.push_digit n
101 else
102 op2but["."].enabled = true
103 context.push_op op.chars.first
104 end
105
106 display.text = context.display_text
107 end
108 end
109 end