From 93023da1f9ed1e40d4e4317d20dbf8398017eee7 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 5 Aug 2023 22:32:24 -0400 Subject: start dynamic menus --- main.c | 2 +- menu.c | 24 ++++++------------------ ted.c | 5 +++++ ted.h | 56 ++++++++++++++++++++++++++++++++++++++------------------ 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/main.c b/main.c index 3c23958..55fb94a 100644 --- a/main.c +++ b/main.c @@ -98,7 +98,7 @@ static Rect message_box_rect(Ted *ted) { float window_width = ted->window_width, window_height = ted->window_height; float char_height = text_font_char_height(font); return rect_centered(Vec2(window_width * 0.5f, window_height * 0.9f), - Vec2(menu_get_width(ted), 3 * char_height + 2 * padding)); + Vec2(ted_get_menu_width(ted), 3 * char_height + 2 * padding)); } #if DEBUG diff --git a/menu.c b/menu.c index f9101ac..426f5df 100644 --- a/menu.c +++ b/menu.c @@ -115,22 +115,6 @@ void menu_escape(Ted *ted) { } } -float menu_get_width(Ted *ted) { - const Settings *settings = ted_active_settings(ted); - return minf(settings->max_menu_width, ted->window_width - 2.0f * settings->padding); -} - -Rect menu_rect(Ted *ted) { - Settings *settings = ted_active_settings(ted); - float window_width = ted->window_width, window_height = ted->window_height; - float padding = settings->padding; - float menu_width = menu_get_width(ted); - return rect( - Vec2(window_width * 0.5f - 0.5f * menu_width, padding), - Vec2(menu_width, window_height - 2 * padding) - ); -} - void menu_update(Ted *ted) { Menu menu = ted->menu; const Settings *settings = ted_active_settings(ted); @@ -345,6 +329,7 @@ void menu_render(Ted *ted) { const float char_height = text_font_char_height(font); const float char_height_bold = text_font_char_height(font_bold); const float line_buffer_height = ted_line_buffer_height(ted); + const float padding = settings->padding; // render backdrop gl_geometry_rect(rect(Vec2(0, 0), Vec2(window_width, window_height)), colors[COLOR_MENU_BACKDROP]); @@ -360,9 +345,12 @@ void menu_render(Ted *ted) { return; } + const float menu_width = ted_get_menu_width(ted); + Rect bounds = rect( + Vec2(window_width * 0.5f - 0.5f * menu_width, padding), + Vec2(menu_width, window_height - 2 * padding) + ); - float padding = settings->padding; - Rect bounds = menu_rect(ted); float x1, y1, x2, y2; rect_coords(bounds, &x1, &y1, &x2, &y2); diff --git a/ted.c b/ted.c index f3cd7a3..10567eb 100644 --- a/ted.c +++ b/ted.c @@ -681,6 +681,11 @@ void ted_reload_all(Ted *ted) { } } +float ted_get_menu_width(Ted *ted) { + const Settings *settings = ted_active_settings(ted); + return minf(settings->max_menu_width, ted->window_width - 2.0f * settings->padding); +} + // load/reload configs void ted_load_configs(Ted *ted) { diff --git a/ted.h b/ted.h index 80c6fdd..b05b43e 100644 --- a/ted.h +++ b/ted.h @@ -112,22 +112,6 @@ typedef u8 SyntaxCharType; /// `*state` was derived from calling this function on line `n-1`. typedef void (*SyntaxHighlightFunction)(SyntaxState *state, const char32_t *line, u32 line_len, SyntaxCharType *char_types); -/// Information about a programming language -/// -/// Used for dynamic language registration. -/// Please zero all the fields of the struct which you aren't using. -/// -/// The fields `id` and `name` MUST NOT be 0, or `ted` will reject your language. -typedef struct { - /// Language ID number. For user-defined languages, this must be `>= LANG_USER_MIN` and `< LANG_USER_MAX`. - /// - /// To avoid conflict, try picking a unique number. - Language id; - char name[30]; - char lsp_identifier[32]; - SyntaxHighlightFunction highlighter; - char reserved[128]; -} LanguageInfo; /// for tex #define SYNTAX_MATH SYNTAX_STRING @@ -263,6 +247,42 @@ typedef enum { MENU_RENAME_SYMBOL, } Menu; +/// Information about a programming language +/// +/// Used for dynamic language registration. +/// Please zero all the fields of the struct which you aren't using. +/// +/// The fields `id` and `name` MUST NOT be 0, or `ted` will reject your language. +typedef struct { + /// Language ID number. For user-defined languages, this must be `>= LANG_USER_MIN` and `< LANG_USER_MAX`. + /// + /// To avoid conflict, try picking a unique number. + Language id; + char name[30]; + char lsp_identifier[32]; + SyntaxHighlightFunction highlighter; + char reserved[128]; +} LanguageInfo; + +typedef struct { + /// identifier used to open the menu. + char name[16]; + /// if non-NULL this will be called right after the menu is opened + void (*open)(Ted *ted); + /// if non-NULL this will be called every frame + /// before anything is rendered to the screen. + void (*update)(Ted *ted); + /// if non-NULL this will be called every frame + /// after buffers, etc. have been rendered to the screen + /// (when the menu should be rendered) + void (*render)(Ted *ted); + /// if non-NULL this will be called right before the menu is closed. + /// if it returns false, the menu will not be closed. + bool (*close)(Ted *ted); + /// should be zeroed -- reserved for future use. + char reserved[128]; +} MenuInfo; + // === buffer.c === /// Returns `true` if the buffer is in view-only mode. bool buffer_is_view_only(TextBuffer *buffer); @@ -779,8 +799,6 @@ void menu_close(Ted *ted); void menu_open(Ted *ted, Menu menu); /// process a :escape command (by default this happens when the escape key is pressed) void menu_escape(Ted *ted); -/// get width of menu in pixels -float menu_get_width(Ted *ted); /// get rectangle which menu takes up Rect menu_rect(Ted *ted); @@ -905,6 +923,8 @@ void *ted_malloc(Ted *ted, size_t size); void *ted_calloc(Ted *ted, size_t n, size_t size); /// allocate memory, producing an error message and returning NULL on failure void *ted_realloc(Ted *ted, void *p, size_t new_size); +/// get width of menu (e.g. "open file" menu) in pixels +float ted_get_menu_width(Ted *ted); /// Check the various places a ted data file could be /// (i.e. look for it in the local and global data directories), /// and return the full path. -- cgit v1.2.3