aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@bordeaux-inp.fr>2024-06-28 18:12:22 +0900
committerTristan Riehs <tristan.riehs@bordeaux-inp.fr>2024-06-28 18:12:22 +0900
commit0856d2961d9e7b3a9e36ddfdfc1985ca5cab13a0 (patch)
tree1bbb39a35ea04b3b63720548f30eda937e99eec2
parentc21b78b105be95bc48d31d9b55e3877c34a666b2 (diff)
Make progress on TUI
User input reading does not work for now.
-rw-r--r--src/tui.c97
1 files changed, 83 insertions, 14 deletions
diff --git a/src/tui.c b/src/tui.c
index 0706b2d..b7fb20f 100644
--- a/src/tui.c
+++ b/src/tui.c
@@ -15,11 +15,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <curses.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "disp.h"
#include "calc_data.h"
+#include "input.h"
static struct calc_data calcs[CALC_COUNT] = {0};
static int current_idx = 0;
@@ -29,38 +31,105 @@ void
init(void)
{
win = initscr();
+ echo();
+ cbreak();
clear();
refresh();
}
+static void
+draw_res(void)
+{
+ int j = current_idx;
+
+ while (calcs[j].ms != 0)
+ {
+ printw("%d + %d = %d\t%s\t%dms\n",
+ calcs[j].x, calcs[j].y,
+ calcs[j].input,
+ calcs[j].right ? "RIGHT" : "WRONG",
+ calcs[j].ms);
+
+ j = loop_backwards(j);
+ }
+}
+
+static void
+draw_calc(void)
+{
+ printw("%d + %d = %d",
+ calcs[current_idx].x, calcs[current_idx].y,
+ calcs[current_idx].input);
+}
+
+static void
+redraw(void)
+{
+ clear();
+ move(0, 0);
+ draw_res();
+ draw_calc();
+ refresh();
+}
+
void
display_calc(int x, int y)
{
+ current_idx = next_calc(current_idx);
calcs[current_idx].x = x;
calcs[current_idx].y = y;
- clear();
- printw("%d + %d = ", x, y);
- refresh();
+ calcs[current_idx].input = 0;
+ calcs[current_idx].ms = 0;
+ redraw();
}
+#define case_digit(digit) \
+ case digit: \
+ calcs[current_idx].input = \
+ add_input_digit(calcs[current_idx].input, digit - '0'); \
+ break;
+
int
read_input(void)
{
- char c = getchar();
-
- switch(c)
+ int c = getch();
+ while (c != ERR)
{
+ switch(c)
+ {
#ifndef NDEBUG
- case 'g':
- case 'r':
- return DISP_RELOAD;
+ case 'g':
+ case 'r':
+ return DISP_RELOAD;
#endif
- case 'q':
- return DISP_QUIT;
- default:
- return DISP_ERR;
+ case 'q':
+ return DISP_QUIT;
+ case '\n':
+ case KEY_ENTER:
+ printf("ENTER PRESSED\n\r");
+ return calcs[current_idx].input;
+ case KEY_BACKSPACE:
+ calcs[current_idx].input =
+ rm_input_digit(calcs[current_idx].input);
+ break;
+ case_digit ('0')
+ case_digit ('1')
+ case_digit ('2')
+ case_digit ('3')
+ case_digit ('4')
+ case_digit ('5')
+ case_digit ('6')
+ case_digit ('7')
+ case_digit ('8')
+ case_digit ('9')
+ default:
+ return DISP_ERR;
+ }
+
+ redraw();
+ c = getchar();
}
-
+
return DISP_ERR;
}