diff options
author | Xavi Artigas <xavierartigas@yahoo.es> | 2019-10-23 23:12:18 +0200 |
---|---|---|
committer | Xavi Artigas <xavierartigas@yahoo.es> | 2019-10-24 10:54:17 +0200 |
commit | f9486b6bfdb35193ecc9b43bcd22105c966b86a4 (patch) | |
tree | 40c1add598aa6eb452e5ad4497563834e1965f8e | |
parent | 8caeaaf071150cb6d6b51e94fddf1c3141cc9f81 (diff) |
Example C# calculator app
For simplicity, only integer operations and no direct editing of the numbers.
This is meant as a simple UI example, not as a CS exercise.
-rw-r--r-- | apps/csharp/calculator/meson.build | 10 | ||||
-rw-r--r-- | apps/csharp/calculator/src/calculator.cs | 176 | ||||
-rw-r--r-- | apps/csharp/calculator/src/meson.build | 12 |
3 files changed, 198 insertions, 0 deletions
diff --git a/apps/csharp/calculator/meson.build b/apps/csharp/calculator/meson.build new file mode 100644 index 00000000..6b10b343 --- /dev/null +++ b/apps/csharp/calculator/meson.build | |||
@@ -0,0 +1,10 @@ | |||
1 | project( | ||
2 | 'efl-example-calculator', 'cs', | ||
3 | version : '0.0.1', | ||
4 | meson_version : '>= 0.38.0') | ||
5 | |||
6 | efl_mono = dependency('efl-mono', version : '>=1.20.99') | ||
7 | efl_mono_libs = efl_mono.get_pkgconfig_variable('mono_libs') | ||
8 | |||
9 | subdir('src') | ||
10 | |||
diff --git a/apps/csharp/calculator/src/calculator.cs b/apps/csharp/calculator/src/calculator.cs new file mode 100644 index 00000000..35e426de --- /dev/null +++ b/apps/csharp/calculator/src/calculator.cs | |||
@@ -0,0 +1,176 @@ | |||
1 | using System; | ||
2 | |||
3 | public class Calculator : Efl.Csharp.Application | ||
4 | { | ||
5 | private Efl.Ui.Text screen; | ||
6 | private int prevValue = 0; | ||
7 | private int currValue = 0; | ||
8 | private char operation = '='; | ||
9 | private bool mustOverwrite = false; | ||
10 | |||
11 | // Quits the application | ||
12 | private void GUIQuitCb(object sender, Efl.Gfx.EntityVisibilityChangedEventArgs ea) | ||
13 | { | ||
14 | if (ea.arg == false) | ||
15 | Efl.App.AppMain.Quit(0); | ||
16 | } | ||
17 | |||
18 | private void Operate() | ||
19 | { | ||
20 | switch (operation) | ||
21 | { | ||
22 | case '+': | ||
23 | currValue += prevValue; | ||
24 | break; | ||
25 | case '-': | ||
26 | currValue = prevValue - currValue; | ||
27 | break; | ||
28 | case '*': | ||
29 | currValue *= prevValue; | ||
30 | break; | ||
31 | case '/': | ||
32 | currValue = prevValue / currValue; | ||
33 | break; | ||
34 | default: | ||
35 | break; | ||
36 | } | ||
37 | } | ||
38 | |||
39 | private void ButtonPressedCb(char button) | ||
40 | { | ||
41 | if (button >= '0' && button <= '9') | ||
42 | { | ||
43 | if (mustOverwrite) | ||
44 | { | ||
45 | screen.SetText(""); | ||
46 | mustOverwrite = false; | ||
47 | } | ||
48 | screen.SetText(screen.GetText() + button.ToString()); | ||
49 | } | ||
50 | else | ||
51 | { | ||
52 | switch (button) | ||
53 | { | ||
54 | case 'C': | ||
55 | screen.SetText("0"); | ||
56 | break; | ||
57 | case '+': | ||
58 | case '-': | ||
59 | case '*': | ||
60 | case '/': | ||
61 | case '=': | ||
62 | if (operation != '=') | ||
63 | { | ||
64 | Operate(); | ||
65 | screen.SetText(currValue.ToString()); | ||
66 | } | ||
67 | operation = button; | ||
68 | mustOverwrite = true; | ||
69 | prevValue = currValue; | ||
70 | break; | ||
71 | default: | ||
72 | break; | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | |||
77 | private void ScreenChangedCb(object sender, EventArgs ea) | ||
78 | { | ||
79 | string text = ""; | ||
80 | string str = screen.GetText(); | ||
81 | int d; | ||
82 | if (str == "" || str == "-") | ||
83 | { | ||
84 | text = "0"; | ||
85 | } | ||
86 | else | ||
87 | { | ||
88 | try | ||
89 | { | ||
90 | d = Convert.ToInt32(str); | ||
91 | text = d.ToString(); | ||
92 | currValue = d; | ||
93 | } | ||
94 | catch {} | ||
95 | } | ||
96 | if (text != str) screen.SetText(text); | ||
97 | } | ||
98 | |||
99 | // text is what is drawn on the button, which might be a multi-byte unicode string. | ||
100 | // command is a single-char id for the button. | ||
101 | private void AddButton(Efl.Ui.Table table, string text, char command, int posx, int posy, int r, int g, int b) | ||
102 | { | ||
103 | var button = new Efl.Ui.Button(table); | ||
104 | table.PackTable(button, posx, posy, 1, 1); | ||
105 | button.ClickedEvent += (object sender, Efl.Input.ClickableClickedEventArgs ea) => { | ||
106 | ButtonPressedCb(command); | ||
107 | }; | ||
108 | // Buttons can only have simple text (no font, styles or markup) but can swallow | ||
109 | // any other object we want. | ||
110 | // Therefore we create a more complex Efl_Ui_Text object and use it as content for the button. | ||
111 | var label = new Efl.Ui.Text(table); | ||
112 | label.Editable = false; | ||
113 | label.Halign = 0.5; | ||
114 | label.Valign = 0.5; | ||
115 | label.Color = (r, g, b, 255); | ||
116 | label.SetText(text); | ||
117 | label.Font = ("Sans", (Efl.Font.Size)36); | ||
118 | button.Content = label; | ||
119 | } | ||
120 | |||
121 | protected override void OnInitialize(string[] args) | ||
122 | { | ||
123 | var win = new Efl.Ui.Win(Efl.App.AppMain); | ||
124 | win.SetText("EFL Calculator"); | ||
125 | win.Autohide = true; | ||
126 | win.VisibilityChangedEvent += GUIQuitCb; | ||
127 | |||
128 | var table = new Efl.Ui.Table(win); | ||
129 | win.Content = table; | ||
130 | table.TableSize = (4, 5); | ||
131 | table.HintSizeMin = new Eina.Size2D(300, 400); | ||
132 | |||
133 | AddButton(table, "1", '1', 0, 3, 255, 255, 255); | ||
134 | AddButton(table, "2", '2', 1, 3, 255, 255, 255); | ||
135 | AddButton(table, "3", '3', 2, 3, 255, 255, 255); | ||
136 | AddButton(table, "4", '4', 0, 2, 255, 255, 255); | ||
137 | AddButton(table, "5", '5', 1, 2, 255, 255, 255); | ||
138 | AddButton(table, "6", '6', 2, 2, 255, 255, 255); | ||
139 | AddButton(table, "7", '7', 0, 1, 255, 255, 255); | ||
140 | AddButton(table, "8", '8', 1, 1, 255, 255, 255); | ||
141 | AddButton(table, "9", '9', 2, 1, 255, 255, 255); | ||
142 | AddButton(table, "0", '0', 1, 4, 255, 255, 255); | ||
143 | AddButton(table, "+", '+', 3, 1, 128, 128, 128); | ||
144 | AddButton(table, "−", '-', 3, 2, 128, 128, 128); | ||
145 | AddButton(table, "×", '*', 3, 3, 128, 128, 128); | ||
146 | AddButton(table, "÷", '/', 3, 4, 128, 128, 128); | ||
147 | AddButton(table, "=", '=', 2, 4, 128, 128, 128); | ||
148 | AddButton(table, "C", 'C', 0, 4, 0, 0, 0); | ||
149 | |||
150 | screen = new Efl.Ui.Text(table); | ||
151 | screen.SetText("0"); | ||
152 | screen.Font = ("Sans", (Efl.Font.Size)48); | ||
153 | screen.Multiline = false; | ||
154 | screen.Editable = false; | ||
155 | screen.SelectionAllowed = false; | ||
156 | table.PackTable(screen, 0, 0, 4, 1); | ||
157 | screen.Halign = 0.9; | ||
158 | screen.Valign = 0.5; | ||
159 | screen.EffectType = Efl.TextStyleEffectType.Glow; | ||
160 | screen.GlowColor = (128, 128, 128, 128); | ||
161 | screen.ChangedEvent += ScreenChangedCb; | ||
162 | } | ||
163 | } | ||
164 | |||
165 | public class Example | ||
166 | { | ||
167 | #if WIN32 | ||
168 | [STAThreadAttribute()] | ||
169 | #endif | ||
170 | public static void Main() | ||
171 | { | ||
172 | var calculator = new Calculator(); | ||
173 | calculator.Launch(); | ||
174 | } | ||
175 | } | ||
176 | |||
diff --git a/apps/csharp/calculator/src/meson.build b/apps/csharp/calculator/src/meson.build new file mode 100644 index 00000000..04ab5e10 --- /dev/null +++ b/apps/csharp/calculator/src/meson.build | |||
@@ -0,0 +1,12 @@ | |||
1 | src = files([ | ||
2 | 'calculator.cs', | ||
3 | ]) | ||
4 | |||
5 | deps = [efl_mono] | ||
6 | |||
7 | executable('efl_example_calculator', src, | ||
8 | dependencies : deps, | ||
9 | cs_args : efl_mono_libs, | ||
10 | install : true | ||
11 | ) | ||
12 | |||