ui functions in a dedicated object

This commit is contained in:
Babz 2021-09-01 11:04:31 +02:00
parent 54fd2e2acb
commit f81dd12d7e
4 changed files with 59 additions and 44 deletions

View File

@ -17,6 +17,7 @@ include_directories(include)
set(SOURCES
src/main.c
src/term.c
src/ui.c
# ...
)
# shared assets

View File

@ -4,8 +4,7 @@
#include "term.h"
#include <gint/display.h>
#include <stdio.h>
#include <string.h>
#include "ui.h"
static char *LOREM_IPSUM =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vitae purus dolor. Praesent in ex tempus, auctor velit at, "
@ -22,58 +21,18 @@ static char *LOREM_IPSUM =
"risus iaculis sed.";
static volatile int key_poll_timeout;
static int tick_ctr = 0;
static int callback_keypoll(void) {
key_poll_timeout = 0;
return TIMER_STOP;
}
int shift_ctr = 0;
static char statusbar[UNS_TERM_COLS + 1];
static void set_statusbar(void) {
// set green bar
for (int i = 0; i < UNS_TERM_COLS; i++)
tgrid_sets(0, i, C_NONE, C_GREEN, " ");
// then add actutal text
sprintf(statusbar, "↑Aa %d %d", tick_ctr, shift_ctr);
tgrid_sets(0, 0, C_BLACK, C_GREEN, statusbar);
}
static void center_str(int len, char buf[len], const char* s) {
const int l = strlen(s);
const int before = len/2 - l/2;
int i;
for (i=0; i < before; i++)
buf[i] = ' ';
for (int j = 0; j < l; j++)
buf[i++] = s[j];
for (; i < len; i++)
buf[i] = ' ';
}
static char fnkeys[6][11] = {"opt 1-", "opt 2--", "opt 3---", "opt 4----", "opt 5-----", "opt 6"};
static void set_menubar(void) {
int col = 0;
for (int i = 0; i < 6; i++) {
char buf[11] = "";
center_str(10, buf, fnkeys[i]);
buf[10] = '\0';
tgrid_sets(UNS_TERM_ROWS - 1, col, C_WHITE, C_BLUE, buf);
col += 10 + 1;
}
}
int main(void) {
const int timer = timer_configure(TIMER_ANY, 15625, GINT_CALL(callback_keypoll));
int tick_ctr = 0;
while (1) {
tgrid_sets(1, 0, C_WHITE, C_BLACK, LOREM_IPSUM);
set_statusbar();
set_statusbar(tick_ctr);
set_menubar();
tgrid_display();

46
src/ui.c Normal file
View File

@ -0,0 +1,46 @@
#include "ui.h"
#include "term.h"
#include <gint/display.h>
#include <stdio.h>
#include <string.h>
void set_statusbar(int tick_ctr) {
// set green bar
for (int i = 0; i < UNS_TERM_COLS; i++)
tgrid_sets(0, i, C_NONE, C_GREEN, " ");
// then add actutal text
char statusbar[UNS_TERM_COLS + 1];
sprintf(statusbar, "↑Aa %d", tick_ctr);
tgrid_sets(0, 0, C_BLACK, C_GREEN, statusbar);
}
static void center_str(int len, char buf[len], const char *s) {
const int l = strlen(s);
const int before = len / 2 - l / 2;
int i;
for (i = 0; i < before; i++)
buf[i] = ' ';
for (int j = 0; j < l; j++)
buf[i++] = s[j];
for (; i < len; i++)
buf[i] = ' ';
}
char fnkeys[6][11] = {"opt 1-", "opt 2--", "opt 3---", "opt 4----", "opt 5-----", "opt 6"};
void set_menubar(void) {
int col = 0;
for (int i = 0; i < 6; i++) {
char buf[11] = "";
center_str(10, buf, fnkeys[i]);
buf[10] = '\0';
tgrid_sets(UNS_TERM_ROWS - 1, col, C_WHITE, C_BLUE, buf);
col += 10 + 1;
}
}

9
src/ui.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef UNS_UI_H
#define UNS_UI_H
void set_statusbar(int tick_ctr);
extern char fnkeys[6][11];
void set_menubar(void);
#endif // #ifndef UNS_UI_H