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.
This commit is contained in:
parent
8caeaaf071
commit
f9486b6bfd
|
@ -0,0 +1,10 @@
|
||||||
|
project(
|
||||||
|
'efl-example-calculator', 'cs',
|
||||||
|
version : '0.0.1',
|
||||||
|
meson_version : '>= 0.38.0')
|
||||||
|
|
||||||
|
efl_mono = dependency('efl-mono', version : '>=1.20.99')
|
||||||
|
efl_mono_libs = efl_mono.get_pkgconfig_variable('mono_libs')
|
||||||
|
|
||||||
|
subdir('src')
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
public class Calculator : Efl.Csharp.Application
|
||||||
|
{
|
||||||
|
private Efl.Ui.Text screen;
|
||||||
|
private int prevValue = 0;
|
||||||
|
private int currValue = 0;
|
||||||
|
private char operation = '=';
|
||||||
|
private bool mustOverwrite = false;
|
||||||
|
|
||||||
|
// Quits the application
|
||||||
|
private void GUIQuitCb(object sender, Efl.Gfx.EntityVisibilityChangedEventArgs ea)
|
||||||
|
{
|
||||||
|
if (ea.arg == false)
|
||||||
|
Efl.App.AppMain.Quit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Operate()
|
||||||
|
{
|
||||||
|
switch (operation)
|
||||||
|
{
|
||||||
|
case '+':
|
||||||
|
currValue += prevValue;
|
||||||
|
break;
|
||||||
|
case '-':
|
||||||
|
currValue = prevValue - currValue;
|
||||||
|
break;
|
||||||
|
case '*':
|
||||||
|
currValue *= prevValue;
|
||||||
|
break;
|
||||||
|
case '/':
|
||||||
|
currValue = prevValue / currValue;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ButtonPressedCb(char button)
|
||||||
|
{
|
||||||
|
if (button >= '0' && button <= '9')
|
||||||
|
{
|
||||||
|
if (mustOverwrite)
|
||||||
|
{
|
||||||
|
screen.SetText("");
|
||||||
|
mustOverwrite = false;
|
||||||
|
}
|
||||||
|
screen.SetText(screen.GetText() + button.ToString());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
switch (button)
|
||||||
|
{
|
||||||
|
case 'C':
|
||||||
|
screen.SetText("0");
|
||||||
|
break;
|
||||||
|
case '+':
|
||||||
|
case '-':
|
||||||
|
case '*':
|
||||||
|
case '/':
|
||||||
|
case '=':
|
||||||
|
if (operation != '=')
|
||||||
|
{
|
||||||
|
Operate();
|
||||||
|
screen.SetText(currValue.ToString());
|
||||||
|
}
|
||||||
|
operation = button;
|
||||||
|
mustOverwrite = true;
|
||||||
|
prevValue = currValue;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ScreenChangedCb(object sender, EventArgs ea)
|
||||||
|
{
|
||||||
|
string text = "";
|
||||||
|
string str = screen.GetText();
|
||||||
|
int d;
|
||||||
|
if (str == "" || str == "-")
|
||||||
|
{
|
||||||
|
text = "0";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
d = Convert.ToInt32(str);
|
||||||
|
text = d.ToString();
|
||||||
|
currValue = d;
|
||||||
|
}
|
||||||
|
catch {}
|
||||||
|
}
|
||||||
|
if (text != str) screen.SetText(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
// text is what is drawn on the button, which might be a multi-byte unicode string.
|
||||||
|
// command is a single-char id for the button.
|
||||||
|
private void AddButton(Efl.Ui.Table table, string text, char command, int posx, int posy, int r, int g, int b)
|
||||||
|
{
|
||||||
|
var button = new Efl.Ui.Button(table);
|
||||||
|
table.PackTable(button, posx, posy, 1, 1);
|
||||||
|
button.ClickedEvent += (object sender, Efl.Input.ClickableClickedEventArgs ea) => {
|
||||||
|
ButtonPressedCb(command);
|
||||||
|
};
|
||||||
|
// Buttons can only have simple text (no font, styles or markup) but can swallow
|
||||||
|
// any other object we want.
|
||||||
|
// Therefore we create a more complex Efl_Ui_Text object and use it as content for the button.
|
||||||
|
var label = new Efl.Ui.Text(table);
|
||||||
|
label.Editable = false;
|
||||||
|
label.Halign = 0.5;
|
||||||
|
label.Valign = 0.5;
|
||||||
|
label.Color = (r, g, b, 255);
|
||||||
|
label.SetText(text);
|
||||||
|
label.Font = ("Sans", (Efl.Font.Size)36);
|
||||||
|
button.Content = label;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnInitialize(string[] args)
|
||||||
|
{
|
||||||
|
var win = new Efl.Ui.Win(Efl.App.AppMain);
|
||||||
|
win.SetText("EFL Calculator");
|
||||||
|
win.Autohide = true;
|
||||||
|
win.VisibilityChangedEvent += GUIQuitCb;
|
||||||
|
|
||||||
|
var table = new Efl.Ui.Table(win);
|
||||||
|
win.Content = table;
|
||||||
|
table.TableSize = (4, 5);
|
||||||
|
table.HintSizeMin = new Eina.Size2D(300, 400);
|
||||||
|
|
||||||
|
AddButton(table, "1", '1', 0, 3, 255, 255, 255);
|
||||||
|
AddButton(table, "2", '2', 1, 3, 255, 255, 255);
|
||||||
|
AddButton(table, "3", '3', 2, 3, 255, 255, 255);
|
||||||
|
AddButton(table, "4", '4', 0, 2, 255, 255, 255);
|
||||||
|
AddButton(table, "5", '5', 1, 2, 255, 255, 255);
|
||||||
|
AddButton(table, "6", '6', 2, 2, 255, 255, 255);
|
||||||
|
AddButton(table, "7", '7', 0, 1, 255, 255, 255);
|
||||||
|
AddButton(table, "8", '8', 1, 1, 255, 255, 255);
|
||||||
|
AddButton(table, "9", '9', 2, 1, 255, 255, 255);
|
||||||
|
AddButton(table, "0", '0', 1, 4, 255, 255, 255);
|
||||||
|
AddButton(table, "+", '+', 3, 1, 128, 128, 128);
|
||||||
|
AddButton(table, "−", '-', 3, 2, 128, 128, 128);
|
||||||
|
AddButton(table, "×", '*', 3, 3, 128, 128, 128);
|
||||||
|
AddButton(table, "÷", '/', 3, 4, 128, 128, 128);
|
||||||
|
AddButton(table, "=", '=', 2, 4, 128, 128, 128);
|
||||||
|
AddButton(table, "C", 'C', 0, 4, 0, 0, 0);
|
||||||
|
|
||||||
|
screen = new Efl.Ui.Text(table);
|
||||||
|
screen.SetText("0");
|
||||||
|
screen.Font = ("Sans", (Efl.Font.Size)48);
|
||||||
|
screen.Multiline = false;
|
||||||
|
screen.Editable = false;
|
||||||
|
screen.SelectionAllowed = false;
|
||||||
|
table.PackTable(screen, 0, 0, 4, 1);
|
||||||
|
screen.Halign = 0.9;
|
||||||
|
screen.Valign = 0.5;
|
||||||
|
screen.EffectType = Efl.TextStyleEffectType.Glow;
|
||||||
|
screen.GlowColor = (128, 128, 128, 128);
|
||||||
|
screen.ChangedEvent += ScreenChangedCb;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Example
|
||||||
|
{
|
||||||
|
#if WIN32
|
||||||
|
[STAThreadAttribute()]
|
||||||
|
#endif
|
||||||
|
public static void Main()
|
||||||
|
{
|
||||||
|
var calculator = new Calculator();
|
||||||
|
calculator.Launch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
src = files([
|
||||||
|
'calculator.cs',
|
||||||
|
])
|
||||||
|
|
||||||
|
deps = [efl_mono]
|
||||||
|
|
||||||
|
executable('efl_example_calculator', src,
|
||||||
|
dependencies : deps,
|
||||||
|
cs_args : efl_mono_libs,
|
||||||
|
install : true
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue