From: Daniel Liu Date: Sun, 31 Jan 2021 04:51:32 +0000 (-0500) Subject: more menu controls X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=676735938747a951747af87ce2e6789553dc83b2;p=taskasaur.git more menu controls --- diff --git a/config.h b/config.h index 153eb9c..a38ea94 100644 --- a/config.h +++ b/config.h @@ -6,7 +6,6 @@ static char font[] = "Source Code Pro:size=12"; - /* COLORS */ /* Use the ncurses defined colors, here's a list of them: @@ -29,6 +28,9 @@ static int non_selected_color = COLOR_WHITE; #define BINDING_SCROLL_DOWN 'j' #define BINDING_JUMP_TOP 'g' #define BINDING_JUMP_BOTTOM 'G' +#define BINDING_MOVE_ITEM_UP 'K' +#define BINDING_MOVE_ITEM_DOWN 'J' +#define BINDING_DELETE_ITEM 'D' #define BINDING_SELECT '\n' #endif diff --git a/headers/menu.h b/headers/menu.h index 0d17779..b1ef7ee 100644 --- a/headers/menu.h +++ b/headers/menu.h @@ -9,7 +9,10 @@ typedef enum { MENU_UP = 0, MENU_DOWN, MENU_TOP, - MENU_BOTTOM + MENU_BOTTOM, + MENU_MOVE_UP, + MENU_MOVE_DOWN, + MENU_DELETE } MenuAction; typedef struct Menu Menu; @@ -18,7 +21,6 @@ typedef struct MenuItem MenuItem; extern MenuItem* create_menuitem(char* contents); extern Menu* create_menu(MenuItem** item_list); -extern int set_menu_items(Menu* menu, MenuItem** item_list); extern int set_menu_win(Menu* menu, WINDOW* win); extern MenuItem* get_menu_items(Menu* menu); diff --git a/menu.c b/menu.c index 777e334..5016dcc 100644 --- a/menu.c +++ b/menu.c @@ -1,6 +1,7 @@ #include #include +#include #include #include "headers/menu.h" @@ -20,7 +21,8 @@ typedef struct Menu { int max_width; } Menu; -int render_menu(Menu* menu); +int swap_item(Menu* menu, int src_index, int dest_index); +int delete_item(Menu* menu, int index); MenuItem* create_menuitem(char* contents) @@ -39,22 +41,14 @@ create_menu(MenuItem** item_list) Menu* new_menu; new_menu = malloc(sizeof(Menu)); - set_menu_items(new_menu, item_list); + new_menu->menu_items = item_list; + new_menu->menu_length = array_length(MenuItem*, item_list); new_menu->selected_item = 0; set_menu_win(new_menu, stdscr); return new_menu; } -int -set_menu_items(Menu* menu, MenuItem** item_list) -{ - menu->menu_items = item_list; - menu->menu_length = array_length(MenuItem*, item_list); - - return 0; -} - int set_menu_win(Menu* menu, WINDOW* win) { @@ -70,6 +64,43 @@ get_menu_items(Menu* menu) return NULL; } +int +swap_item(Menu* menu, int src_index, int dest_index) +{ + MenuItem* temp; + + temp = menu->menu_items[dest_index]; + menu->menu_items[dest_index] = menu->menu_items[src_index]; + menu->menu_items[src_index] = temp; + + return 0; +} + +int +delete_item(Menu* menu, int index) +{ + if (index < 0 || index > menu->menu_length-1) return -1; + + int temp_size = (menu->menu_length-index-1)*sizeof(MenuItem*); + MenuItem* temp[temp_size]; + + /* might break if last item? */ + memcpy(temp, menu->menu_items[index+1], temp_size); + memcpy(menu->menu_items[index], temp, temp_size); + + menu->menu_items = realloc(menu->menu_items, menu->menu_length*sizeof(MenuItem*)); + menu->menu_items[menu->menu_length-1] = 0; // preserve null at end + + menu->menu_length -= 1; + + /* also move the current selected position if it's last */ + if (menu->selected_item > menu->menu_length-1) { + menu->selected_item = menu->menu_length-1; + } + + return 0; +} + int menu_driver(Menu* menu, MenuAction action) { @@ -78,16 +109,37 @@ menu_driver(Menu* menu, MenuAction action) case MENU_UP: menu->selected_item = menu->selected_item-1 >= 0 ? menu->selected_item-1 : 0; break; + case MENU_DOWN: menu->selected_item = menu->selected_item+1 <= menu->menu_length-1 ? menu->selected_item+1 : menu->menu_length-1; break; + case MENU_TOP: menu->selected_item = 0; break; + case MENU_BOTTOM: menu->selected_item = menu->menu_length-1; break; - default: + + case MENU_MOVE_UP: + if (menu->selected_item <= 0) break; + swap_item(menu, menu->selected_item, menu->selected_item-1); + menu->selected_item -= 1; + break; + + case MENU_MOVE_DOWN: + if (menu->selected_item >= menu->menu_length-1) break; + swap_item(menu, menu->selected_item, menu->selected_item+1); + menu->selected_item += 1; + break; + + case MENU_DELETE: + delete_item(menu, menu->selected_item); + wclear(menu->menu_win); + break; + + default: // This is here for debug, disable later fprintf(stderr, "Invalid menu action"); } diff --git a/taskasaur.c b/taskasaur.c index c8fd8e6..08c5695 100644 --- a/taskasaur.c +++ b/taskasaur.c @@ -16,11 +16,12 @@ main(int argc, char** argv) /* init curses */ init_tscurses(); - MenuItem** item_list = malloc(6*sizeof(MenuItem*)); - for (int i = 0; i < 5; i++) { - item_list[i] = create_menuitem("Many of you are probably feeling a little sad."); - } - item_list[5] = 0; + MenuItem** item_list = malloc(5*sizeof(MenuItem*)); + item_list[0] = create_menuitem("Many of you are probably feeling a little sad."); + item_list[1] = create_menuitem("This is ok. Sadness is a normal human emotion."); + item_list[2] = create_menuitem("I encourage you to watch the movie Inside Out"); + item_list[3] = create_menuitem("one of the best movies of all time."); + item_list[4] = 0; Menu* menu = create_menu(item_list); render_menu(menu); @@ -41,6 +42,16 @@ main(int argc, char** argv) case BINDING_JUMP_BOTTOM: menu_driver(menu, MENU_BOTTOM); break; + case BINDING_MOVE_ITEM_UP: + menu_driver(menu, MENU_MOVE_UP); + break; + case BINDING_MOVE_ITEM_DOWN: + menu_driver(menu, MENU_MOVE_DOWN); + break; + case BINDING_DELETE_ITEM: + menu_driver(menu, MENU_DELETE); + break; + } render_menu(menu);