aboutsummaryrefslogtreecommitdiff
path: root/nuklear/demo/calculator.c
diff options
context:
space:
mode:
Diffstat (limited to 'nuklear/demo/calculator.c')
-rw-r--r--nuklear/demo/calculator.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/nuklear/demo/calculator.c b/nuklear/demo/calculator.c
new file mode 100644
index 0000000..b871301
--- /dev/null
+++ b/nuklear/demo/calculator.c
@@ -0,0 +1,64 @@
+/* nuklear - v1.00 - public domain */
+static void
+calculator(struct nk_context *ctx)
+{
+ if (nk_begin(ctx, "Calculator", nk_rect(10, 10, 180, 250),
+ NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_MOVABLE))
+ {
+ static int set = 0, prev = 0, op = 0;
+ static const char numbers[] = "789456123";
+ static const char ops[] = "+-*/";
+ static double a = 0, b = 0;
+ static double *current = &a;
+
+ size_t i = 0;
+ int solve = 0;
+ {int len; char buffer[256];
+ nk_layout_row_dynamic(ctx, 35, 1);
+ len = snprintf(buffer, 256, "%.2f", *current);
+ nk_edit_string(ctx, NK_EDIT_SIMPLE, buffer, &len, 255, nk_filter_float);
+ buffer[len] = 0;
+ *current = atof(buffer);}
+
+ nk_layout_row_dynamic(ctx, 35, 4);
+ for (i = 0; i < 16; ++i) {
+ if (i >= 12 && i < 15) {
+ if (i > 12) continue;
+ if (nk_button_label(ctx, "C")) {
+ a = b = op = 0; current = &a; set = 0;
+ } if (nk_button_label(ctx, "0")) {
+ *current = *current*10.0f; set = 0;
+ } if (nk_button_label(ctx, "=")) {
+ solve = 1; prev = op; op = 0;
+ }
+ } else if (((i+1) % 4)) {
+ if (nk_button_text(ctx, &numbers[(i/4)*3+i%4], 1)) {
+ *current = *current * 10.0f + numbers[(i/4)*3+i%4] - '0';
+ set = 0;
+ }
+ } else if (nk_button_text(ctx, &ops[i/4], 1)) {
+ if (!set) {
+ if (current != &b) {
+ current = &b;
+ } else {
+ prev = op;
+ solve = 1;
+ }
+ }
+ op = ops[i/4];
+ set = 1;
+ }
+ }
+ if (solve) {
+ if (prev == '+') a = a + b;
+ if (prev == '-') a = a - b;
+ if (prev == '*') a = a * b;
+ if (prev == '/') a = a / b;
+ current = &a;
+ if (set) current = &b;
+ b = 0; set = 0;
+ }
+ }
+ nk_end(ctx);
+}
+