commit b895dc97749d98449d47f4c7a4e9d55fa3de1a6a
parent 314530f9686530999bdf7fe68f45e730b4a7bf63
Author: klewer-martin <martin.cachari@gmail.com>
Date: Wed, 20 Apr 2022 00:05:59 -0300
Added arrow keys and vim keys to go up/down
Now the menu supports UP/DOWN keys as well as J/K to select next or previous elements
respectively
Diffstat:
M | main.c | | | 45 | ++++++++++++++++++++++++++++++--------------- |
1 file changed, 30 insertions(+), 15 deletions(-)
diff --git a/main.c b/main.c
@@ -11,26 +11,28 @@
#define BAR_H 14
#define BAR_BORDER 2
-#define TOTAL_ENTRIES 6
+#define TOTAL_ENTRIES 11
#define ELEMENTS_PADDING 3
#define NORM_COLOR 0x010101FF
#define SEL_COLOR 0xCC0000FF
+#define WIN_DEFAULT_W 800
+#define WIN_DEFAULT_H 600
+
#define UNHEX(color) \
((color) >> (8 * 3)) & 0xFF, \
((color) >> (8 * 2)) & 0xFF, \
((color) >> (8 * 1)) & 0xFF, \
((color) >> (8 * 0)) & 0xFF
-
typedef struct Entry {
char *text;
bool selected;
size_t index;
} Entry;
-void next_selection(Entry entries[]) {
+void select_next(Entry entries[]) {
for(int i = 0; i < TOTAL_ENTRIES; i++) {
if(entries[i].selected == true) {
entries[i].selected = false;
@@ -44,7 +46,7 @@ void next_selection(Entry entries[]) {
}
}
-void prev_selection(Entry entries[]) {
+void select_prev(Entry entries[]) {
for(int i = 0; i < TOTAL_ENTRIES; i++) {
if(entries[i].selected == true) {
entries[i].selected = false;
@@ -140,24 +142,22 @@ int main (void) {
"menu",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
- 800,
- 600,
+ WIN_DEFAULT_W,
+ WIN_DEFAULT_H,
SDL_WINDOW_RESIZABLE
);
rend = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- if (!win || !rend)
- return 1;
- else if(TTF_Init() == -1)
- return 1;
+ if (!win || !rend) return 1;
+ else if(TTF_Init() == -1) return 1;
font = TTF_OpenFont(FONT_NAME, FONT_SIZE);
bool run = true;
SDL_Event event;
- win_w = 800;
- win_h = 600;
+ win_w = WIN_DEFAULT_W;
+ win_h = WIN_DEFAULT_H;
Entry entries[TOTAL_ENTRIES] = {0};
@@ -171,7 +171,12 @@ int main (void) {
entries[2].text = "Insertion sort";
entries[3].text = "Merge sort";
entries[4].text = "Quick sort";
- entries[5].text = "Shell sort";
+ entries[5].text = "Quick sort (dual pivots)";
+ entries[6].text = "Quick sort (LR pointers)";
+ entries[7].text = "Quick sort (LL pointers)";
+ entries[8].text = "Shell sort";
+ entries[9].text = "Radix sort";
+ entries[10].text = "Heap sort";
entries[0].selected = true;
SDL_Keymod mod;
@@ -185,13 +190,23 @@ int main (void) {
case SDL_KEYDOWN:
/* printf("SDL_GetModState() == %X\n", SDL_GetModState()); */
switch(event.key.keysym.scancode) {
+ case SDL_SCANCODE_J:
+ case SDL_SCANCODE_DOWN:
+ select_next(entries);
+ break;
+ case SDL_SCANCODE_K:
+ case SDL_SCANCODE_UP:
+ select_prev(entries);
+ break;
case SDL_SCANCODE_TAB:
mod = SDL_GetModState();
if((mod == KMOD_LSHIFT) || (mod == KMOD_RSHIFT)) {
- prev_selection(entries);
+ select_prev(entries);
break;
} else
- next_selection(entries);
+ select_next(entries);
+ break;
+ case SDL_SCANCODE_RETURN:
break;
default: break;
}