actually useful calculator example
This commit is contained in:
parent
62ebd6592d
commit
4a690fdeb5
@ -195,6 +195,8 @@ fn void Ctx.input_char(&ctx, char c)
|
|||||||
ctx.input_text_utf8(b[..]);
|
ctx.input_text_utf8(b[..]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn String Ctx.get_keys(&ctx) => (String)ctx.input.keyboard.text[:ctx.input.keyboard.text_len];
|
||||||
|
|
||||||
// Modifier keys, like control or backspace
|
// Modifier keys, like control or backspace
|
||||||
// TODO: make this call repetible to input modkeys one by one
|
// TODO: make this call repetible to input modkeys one by one
|
||||||
fn void Ctx.input_mod_keys(&ctx, ModKeys modkeys)
|
fn void Ctx.input_mod_keys(&ctx, ModKeys modkeys)
|
||||||
|
87
src/main.c3
87
src/main.c3
@ -319,40 +319,91 @@ fn void debug_app(ugui::Ctx* ui)
|
|||||||
ui.div_end()!!;
|
ui.div_end()!!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
import std::os::process;
|
||||||
|
|
||||||
fn void calculator(ugui::Ctx* ui)
|
fn void calculator(ugui::Ctx* ui)
|
||||||
{
|
{
|
||||||
|
static char[128] buffer;
|
||||||
|
static usz len;
|
||||||
|
bool eval;
|
||||||
|
|
||||||
|
// keyboard input
|
||||||
|
switch(ui.get_keys()) {
|
||||||
|
case "+": nextcase;
|
||||||
|
case "-": nextcase;
|
||||||
|
case "*": nextcase;
|
||||||
|
case "/": nextcase;
|
||||||
|
case "(": nextcase;
|
||||||
|
case ")": nextcase;
|
||||||
|
case "0": nextcase;
|
||||||
|
case "1": nextcase;
|
||||||
|
case "2": nextcase;
|
||||||
|
case "3": nextcase;
|
||||||
|
case "4": nextcase;
|
||||||
|
case "5": nextcase;
|
||||||
|
case "6": nextcase;
|
||||||
|
case "7": nextcase;
|
||||||
|
case "8": nextcase;
|
||||||
|
case "9":
|
||||||
|
buffer[len++] = ui.get_keys()[0];
|
||||||
|
case "\n":
|
||||||
|
eval = true;
|
||||||
|
case "c":
|
||||||
|
len = 0;
|
||||||
|
case "d":
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ui input/output
|
||||||
ui.@div(ugui::DIV_FILL) {
|
ui.@div(ugui::DIV_FILL) {
|
||||||
ui.layout_set_column()!!;
|
ui.layout_set_column()!!;
|
||||||
|
|
||||||
ui.@div({0,0,250,50}) {
|
ui.@div({0,0,250,50}) {
|
||||||
ui.text_unbounded("80085")!!;
|
ui.text_unbounded((String)buffer[:len])!!;
|
||||||
}!!;
|
}!!;
|
||||||
|
|
||||||
ui.@div({0,0,250,-100}) {
|
ui.@div({0,0,250,-100}) {
|
||||||
ui.layout_set_row()!!;
|
ui.layout_set_row()!!;
|
||||||
|
|
||||||
ui.button("7")!!;
|
ui.button("7")!!.mouse_press ? buffer[len++] = '7' : 0;
|
||||||
ui.button("8")!!;
|
ui.button("8")!!.mouse_press ? buffer[len++] = '8' : 0;
|
||||||
ui.button("9")!!;
|
ui.button("9")!!.mouse_press ? buffer[len++] = '9' : 0;
|
||||||
ui.layout_next_row()!!;
|
ui.layout_next_row()!!;
|
||||||
ui.button("4")!!;
|
ui.button("4")!!.mouse_press ? buffer[len++] = '4' : 0;
|
||||||
ui.button("5")!!;
|
ui.button("5")!!.mouse_press ? buffer[len++] = '5' : 0;
|
||||||
ui.button("6")!!;
|
ui.button("6")!!.mouse_press ? buffer[len++] = '6' : 0;
|
||||||
ui.layout_next_row()!!;
|
ui.layout_next_row()!!;
|
||||||
ui.button("3")!!;
|
ui.button("3")!!.mouse_press ? buffer[len++] = '3' : 0;
|
||||||
ui.button("2")!!;
|
ui.button("2")!!.mouse_press ? buffer[len++] = '2' : 0;
|
||||||
ui.button("1")!!;
|
ui.button("1")!!.mouse_press ? buffer[len++] = '1' : 0;
|
||||||
ui.layout_next_row()!!;
|
ui.layout_next_row()!!;
|
||||||
ui.button(".")!!;
|
ui.button("0")!!.mouse_press ? buffer[len++] = '0' : 0;
|
||||||
ui.button("0")!!;
|
ui.button(".")!!.mouse_press ? buffer[len++] = '.' : 0;
|
||||||
//ui.button("")!!;
|
ui.button("(")!!.mouse_press ? buffer[len++] = '(' : 0;
|
||||||
|
|
||||||
ui.layout_next_column()!!;
|
ui.layout_next_column()!!;
|
||||||
ui.layout_set_column()!!;
|
|
||||||
ui.button("+")!!;
|
ui.@div({0,0,0,-1}) {
|
||||||
ui.button("-")!!;
|
ui.button("C")!!.mouse_press ? len = 0 : 0;
|
||||||
ui.button("*")!!;
|
ui.button("D")!!.mouse_press ? len-- : 0;
|
||||||
ui.button("/")!!;
|
ui.layout_next_row()!!;
|
||||||
|
ui.button("x")!!.mouse_press ? buffer[len++] = '*' : 0;
|
||||||
|
ui.button("/")!!.mouse_press ? buffer[len++] = '/' : 0;
|
||||||
|
ui.layout_next_row()!!;
|
||||||
|
ui.button("+")!!.mouse_press ? buffer[len++] = '+' : 0;
|
||||||
|
ui.button("-")!!.mouse_press ? buffer[len++] = '-' : 0;
|
||||||
|
ui.layout_next_row()!!;
|
||||||
|
ui.button(")")!!.mouse_press ? buffer[len++] = ')' : 0;
|
||||||
|
|
||||||
|
// eval the expression with 'bc'
|
||||||
|
if (ui.button("=")!!.mouse_press || eval) {
|
||||||
|
char[128] out;
|
||||||
|
String y = string::tformat("echo '%s' | bc", (String)buffer[:len]);
|
||||||
|
String x = process::execute_stdout_to_buffer(out[:128], (String[]){"sh", "-c", y}) ?? "";
|
||||||
|
buffer[:x.len] = x[..];
|
||||||
|
len = x.len;
|
||||||
|
}
|
||||||
|
}!!;
|
||||||
}!!;
|
}!!;
|
||||||
}!!;
|
}!!;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user