From 2973df584978d9aae6f164ce0204179c8a28b3ae Mon Sep 17 00:00:00 2001 From: 2lag <96544487+2lag@users.noreply.github.com> Date: Sat, 8 Aug 2026 17:33:36 +0200 Subject: console, textbox changes, some other shit --- src/editor/editor.cpp | 6 +- src/editor/editor.h | 36 +----- src/editor/editor_contextmenu.cpp | 254 -------------------------------------- src/editor/editor_menubar.cpp | 58 +++++---- src/editor/editor_window.cpp | 12 +- 5 files changed, 47 insertions(+), 319 deletions(-) delete mode 100644 src/editor/editor_contextmenu.cpp (limited to 'src/editor') diff --git a/src/editor/editor.cpp b/src/editor/editor.cpp index 2e76039..18ae062 100644 --- a/src/editor/editor.cpp +++ b/src/editor/editor.cpp @@ -648,6 +648,7 @@ GAME_EDITOR* editor_create( GAME_DATA* game ) { gui_init( game ); e->wnd = gui_editorwindow( 800, 600 ); + game->console_window = gui_consolewindow( 10, 10, 256, 128 ); gui_end(); return e; @@ -663,6 +664,7 @@ STAT editor_close( GAME_EDITOR* e ) { I32 w = e->wnd->w, h = e->wnd->h; gui_free( e->wnd ); + e->wnd = 0; editor_clear_gui_state_refs( e ); e->wnd = gui_editorwindow( w, h ); } ); @@ -701,8 +703,8 @@ STAT editor_new_map( GAME_EDITOR* e, const char* mapname ) { m->props.push( { .clr = { 1.f, 1.f, 1.f, 1.f } } ); m->walls.push( { .propid = 0, - .start = { 0 , 0, -40.f }, - .end = { 10.f, 0, 80.f }, + .start = { 0.f, 0.f, -40.f }, + .end = { 10.f, 0.f, 80.f }, } ); CFG_SECTION* map_cfg = map_serialize( m ); diff --git a/src/editor/editor.h b/src/editor/editor.h index 3d27951..6d7d747 100644 --- a/src/editor/editor.h +++ b/src/editor/editor.h @@ -70,19 +70,6 @@ enum EditorObjectType_t { EDITOR_OBJECT_ENTITY = 1 }; -typedef void( *EDITOR_CONTEXTMENU_CALLBACK )( void* data ); -struct EDITOR_CONTEXTMENU_ITEM; -typedef U8( *EDITOR_CONTEXTMENU_ENABLED_CALLBACK )( EDITOR_CONTEXTMENU_ITEM* item ); - -struct EDITOR_CONTEXTMENU_ITEM { - char text[64]{}; - void* data{}; - EDITOR_CONTEXTMENU_CALLBACK cb{}; - EDITOR_CONTEXTMENU_ENABLED_CALLBACK enabled_cb{}; - U8 enabled{1}; - LIST items{}; -}; - struct GAME_EDITOR; typedef void( *EDITOR_GRID_DEP_CALLBACK )( GAME_EDITOR* e ); @@ -184,7 +171,7 @@ struct GAME_EDITOR { I32 view_mode{}; I32 view2d_type{}; - struct GUI_EDITOR_CONTEXTMENU* contextmenu{}; + struct GUI_CONTEXTMENU* contextmenu{}; GUI_BASE* header_toolbar{}; GUI_LABEL* header_viewtype_label{}; GUI_BUTTON* header_back{}; @@ -271,17 +258,6 @@ extern void editor_undo_record_change_prop( GAME_EDITOR* e, FNV1A hash, void* ol extern U8 editor_undo( GAME_EDITOR* e ); extern U8 editor_redo( GAME_EDITOR* e ); -extern void editor_show_contextmenu( I32 x, I32 y, LIST* items, const char* title = 0 ); -extern void editor_hide_contextmenu(); -extern U8 editor_contextmenu_open(); -extern void editor_contextmenu_set_item( - EDITOR_CONTEXTMENU_ITEM* item, - const char* text, - EDITOR_CONTEXTMENU_CALLBACK cb = 0, - void* data = 0, - EDITOR_CONTEXTMENU_ENABLED_CALLBACK enabled_cb = 0 -); - struct GUI_EDITORWINDOW : GUI_WINDOW {}; struct GUI_EDITOR_3DVIEW : GUI_VIEW { U8 heldoutbounds; @@ -374,19 +350,11 @@ struct GUI_EDITOR_VIEWTYPE_SEGMENT : GUI_BASE { I32 held_seg{-1}; }; -struct GUI_EDITOR_CONTEXTMENU : GUI_BASE { - LIST* items{}; - U8 initheld{1}; - U8 held{}; - I32 held_idx{-1}; - char title[64]{}; -}; - struct GUI_EDITOR_TOOLBAR : GUI_BASE { U8 held{}; I32 held_root{-1}; I32 active_root{-1}; - LIST entries{}; + LIST entries{}; }; extern GUI_EDITORWINDOW* gui_editorwindow( I32 w, I32 h ); diff --git a/src/editor/editor_contextmenu.cpp b/src/editor/editor_contextmenu.cpp deleted file mode 100644 index 4f9dbe6..0000000 --- a/src/editor/editor_contextmenu.cpp +++ /dev/null @@ -1,254 +0,0 @@ -#include "editor_gui_internal.h" - -U8 editor_contextmenu_item_enabled( EDITOR_CONTEXTMENU_ITEM* item ) { - if( !item ) - return 0; - - if( item->enabled_cb ) - return item->enabled_cb( item ); - - return item->enabled; -} - -I32 editor_contextmenu_title_h( GUI_EDITOR_CONTEXTMENU* menu ) { - return ( menu && menu->title[0] ) ? 18 : 0; -} - -I32 editor_contextmenu_row_h() { - return 20; -} - -I32 editor_contextmenu_width( LIST* items, const char* title ) { - I32 width = EDITOR_TOOLBAR_DROPDOWN_W; - if( title && title[0] ) { - I32 text_w = 0; - gui_draw_get_str_bounds( &text_w, 0, FNT_JPN12, "%s", title ); - width = max( width, text_w + 18 ); - } - - if( !items ) - return width; - - for( I32 i = 0; i < (I32)items->size; ++i ) { - I32 text_w = 0; - gui_draw_get_str_bounds( &text_w, 0, FNT_JPN12, "%s", ( *items )[i].text ); - width = max( width, text_w + 20 ); - } - - return width; -} - -I32 editor_contextmenu_height( LIST* items, const char* title ) { - I32 height = 4 + editor_contextmenu_row_h() * ( items ? (I32)items->size : 0 ); - if( title && title[0] ) - height += 18 + 1; - return max( height, editor_contextmenu_row_h() + 4 ); -} - -static I32 editor_contextmenu_items_y( GUI_EDITOR_CONTEXTMENU* menu ) { - I32 y = gui_rely( menu ) + 2; - if( menu->title[0] ) - y += editor_contextmenu_title_h( menu ) + 1; - return y; -} - -I32 editor_contextmenu_hit_test( GUI_EDITOR_CONTEXTMENU* menu, I32 mx, I32 my ) { - if( !menu || !menu->items ) - return -1; - - I32 x = gui_relx( menu ); - I32 y = gui_rely( menu ); - if( mx < x || mx >= x + menu->w || my < y || my >= y + menu->h ) - return -1; - - I32 items_y = editor_contextmenu_items_y( menu ); - if( my < items_y ) - return -1; - - I32 idx = ( my - items_y ) / editor_contextmenu_row_h(); - if( idx < 0 || idx >= (I32)menu->items->size ) - return -1; - - return idx; -} - -void gui_editor_contextmenu_draw_fn( void* ptr ) { - GUI_EDITOR_CONTEXTMENU* menu = (GUI_EDITOR_CONTEXTMENU*)ptr; - if( !menu || !menu->items || !menu->items->size ) - return; - - I32 x = gui_relx( menu ); - I32 y = gui_rely( menu ); - I32 mx = 0, my = 0; - if( editor_menu_hover_mask_active ) { - mx = editor_menu_hover_real_x; - my = editor_menu_hover_real_y; - } else { - gui_cursor_pos( &mx, &my ); - } - I32 hit = editor_contextmenu_hit_test( menu, mx, my ); - U8 m1 = gui_mbutton_down( GUI_MBTNLEFT ); - CLR hover_fill = CLR::blend( ui_clr.border, ui_clr.bg, 0.70f ); - CLR active_fill = CLR::blend( ui_clr.bg_sec, ui_clr.border, 0.22f ); - - CLR border = gui_is_fg_window( menu ) ? ui_clr.border : ui_clr.border_inactive; - gui_draw_frect( x, y, menu->w, menu->h, border ); - gui_draw_frect( x + 1, y + 1, max( 1, menu->w - 2 ), max( 1, menu->h - 2 ), ui_clr.bg_sec ); - - I32 cy = y + 2; - if( menu->title[0] ) { - gui_draw_str( x + 8, cy + 1, ALIGN_L, FNT_JPN12, ui_clr.txt, "%s", menu->title ); - cy += editor_contextmenu_title_h( menu ); - gui_draw_line( x + 1, cy, x + menu->w - 2, cy, border ); - cy += 1; - } - - for( I32 i = 0; i < (I32)menu->items->size; ++i ) { - EDITOR_CONTEXTMENU_ITEM* item = &( *menu->items )[i]; - U8 enabled = editor_contextmenu_item_enabled( item ); - U8 hovered = hit == i; - U8 active = menu->held && menu->held_idx == i && m1 && enabled; - - CLR fill = ui_clr.bg_sec; - if( active ) - fill = active_fill; - else if( hovered ) - fill = hover_fill; - - gui_draw_frect( x + 1, cy + i * editor_contextmenu_row_h(), max( 1, menu->w - 2 ), editor_contextmenu_row_h(), fill ); - gui_draw_str( - x + 8, - cy + i * editor_contextmenu_row_h() + 2, - ALIGN_L, - FNT_JPN12, - enabled ? ui_clr.txt : ui_clr.txt_inactive, - "%s", - item->text - ); - } -} - -void gui_editor_contextmenu_input_fn( void* ptr ) { - GUI_EDITOR_CONTEXTMENU* menu = (GUI_EDITOR_CONTEXTMENU*)ptr; - if( !menu || !menu->items || !menu->items->size ) - return; - - U8 m1 = gui_mbutton_down( GUI_MBTNLEFT ); - U8 m2 = gui_mbutton_down( GUI_MBTNRIGHT ); - if( menu->initheld ) { - if( m1 || m2 ) - return; - menu->initheld = 0; - return; - } - - I32 mx = 0, my = 0; - gui_cursor_pos( &mx, &my ); - I32 hit = editor_contextmenu_hit_test( menu, mx, my ); - - if( m1 || m2 ) { - if( !menu->held ) { - menu->held = 1; - menu->held_idx = hit; - } - return; - } - - if( !menu->held ) - return; - - if( hit == -1 ) { - editor_hide_contextmenu(); - return; - } - - if( hit == menu->held_idx ) { - EDITOR_CONTEXTMENU_ITEM* item = &( *menu->items )[hit]; - if( editor_contextmenu_item_enabled( item ) && item->cb ) { - item->cb( item->data ); - editor_hide_contextmenu(); - return; - } - } - - menu->held = 0; - menu->held_idx = -1; -} - -void editor_hide_contextmenu() { - if( !editor || !editor->gui.contextmenu ) - return; - - GUI_EDITOR_CONTEXTMENU* menu = editor->gui.contextmenu; - editor->gui.contextmenu = 0; - - if( menu->parent ) { - I32 idx = menu->parent->children.idx_of( (GUI_BASE*)menu ); - if( idx != -1 ) - menu->parent->children.erase( idx ); - } - - GUI_EDITOR_TOOLBAR* bar = (GUI_EDITOR_TOOLBAR*)editor->gui.header_toolbar; - if( bar ) - bar->active_root = -1; - - gui_free( menu ); -} - -U8 editor_contextmenu_open() { - return editor && editor->gui.contextmenu && editor->gui.contextmenu->enabled; -} - - -void editor_contextmenu_set_item( - EDITOR_CONTEXTMENU_ITEM* item, - const char* text, - EDITOR_CONTEXTMENU_CALLBACK cb, - void* data, - EDITOR_CONTEXTMENU_ENABLED_CALLBACK enabled_cb -) { - if( !item ) - return; - - item->items.clear(); - snprintf( item->text, sizeof( item->text ), "%s", text ? text : "" ); - item->cb = cb; - item->data = data; - item->enabled = 1; - item->enabled_cb = enabled_cb; -} - -void editor_show_contextmenu( I32 x, I32 y, LIST* items, const char* title ) { - editor_hide_contextmenu(); - if( !editor || !editor->wnd || !items || !items->size ) - return; - - GUI_EDITOR_CONTEXTMENU* menu = new GUI_EDITOR_CONTEXTMENU; - menu->items = items; - menu->draw_fn = gui_editor_contextmenu_draw_fn; - menu->input_fn = gui_editor_contextmenu_input_fn; - menu->enabled = 1; - menu->initheld = 1; - menu->held = 0; - menu->held_idx = -1; - menu->parent = editor->wnd; - snprintf( menu->name, sizeof( menu->name ), "EDITOR_CONTEXTMENU" ); - snprintf( menu->title, sizeof( menu->title ), "%s", title ? title : "" ); - - menu->w = editor_contextmenu_width( items, title ); - menu->h = editor_contextmenu_height( items, title ); - menu->xbound = menu->w; - menu->ybound = menu->h; - - if( x + menu->w > editor->wnd->w - 2 ) - x = max( 1, editor->wnd->w - menu->w - 2 ); - if( y + menu->h > editor->wnd->h - 2 ) - y = max( 1, editor->wnd->h - menu->h - 2 ); - if( x < 1 ) x = 1; - if( y < 1 ) y = 1; - - menu->x = x; - menu->y = y; - editor->wnd->children.push( menu ); - editor->gui.contextmenu = menu; -} diff --git a/src/editor/editor_menubar.cpp b/src/editor/editor_menubar.cpp index cef2650..2b2a155 100644 --- a/src/editor/editor_menubar.cpp +++ b/src/editor/editor_menubar.cpp @@ -1,3 +1,4 @@ +#include "editor.h" #include "editor_gui_internal.h" U8 editor_menu_hover_mask_active = 0; @@ -36,7 +37,15 @@ void editor_apply_view_mode( GAME_EDITOR* e ) { } void editor_toolbar_close_menu() { - editor_hide_contextmenu(); + if( !editor ) + return; + + GUI_EDITOR_TOOLBAR* tb = (GUI_EDITOR_TOOLBAR*)editor->gui.header_toolbar; + + if( tb ) + tb->active_root = -1; + + gui_hide_contextmenu( editor->gui.contextmenu ); } void editor_set_view_mode( I32 mode ) { @@ -89,11 +98,11 @@ void editor_contextmenu_set_tool_cb( void* data ) { editor_settool( (U8)(I64)data ); } -U8 editor_contextmenu_undo_enabled( EDITOR_CONTEXTMENU_ITEM* ) { +U8 editor_contextmenu_undo_enabled( GUI_CONTEXTMENU_ITEM* ) { return editor && editor->undo_actions.size > 0; } -U8 editor_contextmenu_redo_enabled( EDITOR_CONTEXTMENU_ITEM* ) { +U8 editor_contextmenu_redo_enabled( GUI_CONTEXTMENU_ITEM* ) { return editor && editor->redo_actions.size > 0; } @@ -104,31 +113,31 @@ void editor_toolbar_init_entries( GUI_EDITOR_TOOLBAR* bar ) { bar->entries.clear(); bar->entries.resize( 4 ); - editor_contextmenu_set_item( &bar->entries[0], "file" ); + gui_contextmenu_set_item( &bar->entries[0], "file" ); bar->entries[0].items.resize( 1 ); - editor_contextmenu_set_item( &bar->entries[0].items[0], "save", editor_contextmenu_save_cb ); + gui_contextmenu_set_item( &bar->entries[0].items[0], "save", editor_contextmenu_save_cb ); - editor_contextmenu_set_item( &bar->entries[1], "edit" ); + gui_contextmenu_set_item( &bar->entries[1], "edit" ); bar->entries[1].items.resize( 2 ); - editor_contextmenu_set_item( &bar->entries[1].items[0], "undo", editor_contextmenu_undo_cb, 0, editor_contextmenu_undo_enabled ); - editor_contextmenu_set_item( &bar->entries[1].items[1], "redo", editor_contextmenu_redo_cb, 0, editor_contextmenu_redo_enabled ); + gui_contextmenu_set_item( &bar->entries[1].items[0], "undo", editor_contextmenu_undo_cb, 0, editor_contextmenu_undo_enabled ); + gui_contextmenu_set_item( &bar->entries[1].items[1], "redo", editor_contextmenu_redo_cb, 0, editor_contextmenu_redo_enabled ); - editor_contextmenu_set_item( &bar->entries[2], "view" ); + gui_contextmenu_set_item( &bar->entries[2], "view" ); bar->entries[2].items.resize( 3 ); - editor_contextmenu_set_item( &bar->entries[2].items[0], "2d view", editor_contextmenu_set_view_mode_cb, (void*)(I64)EDITOR_VIEWMODE_2D ); - editor_contextmenu_set_item( &bar->entries[2].items[1], "3d view", editor_contextmenu_set_view_mode_cb, (void*)(I64)EDITOR_VIEWMODE_3D ); - editor_contextmenu_set_item( &bar->entries[2].items[2], "simulation", editor_contextmenu_set_view_mode_cb, (void*)(I64)EDITOR_VIEWMODE_SIM ); + gui_contextmenu_set_item( &bar->entries[2].items[0], "2d view", editor_contextmenu_set_view_mode_cb, (void*)(I64)EDITOR_VIEWMODE_2D ); + gui_contextmenu_set_item( &bar->entries[2].items[1], "3d view", editor_contextmenu_set_view_mode_cb, (void*)(I64)EDITOR_VIEWMODE_3D ); + gui_contextmenu_set_item( &bar->entries[2].items[2], "simulation", editor_contextmenu_set_view_mode_cb, (void*)(I64)EDITOR_VIEWMODE_SIM ); - editor_contextmenu_set_item( &bar->entries[3], "tools" ); + gui_contextmenu_set_item( &bar->entries[3], "tools" ); bar->entries[3].items.resize( 5 ); - editor_contextmenu_set_item( &bar->entries[3].items[0], "none", editor_contextmenu_set_tool_cb, (void*)(I64)EDITOR_TOOL_NONE ); - editor_contextmenu_set_item( &bar->entries[3].items[1], "select", editor_contextmenu_set_tool_cb, (void*)(I64)EDITOR_TOOL_SELECT ); - editor_contextmenu_set_item( &bar->entries[3].items[2], "wall", editor_contextmenu_set_tool_cb, (void*)(I64)EDITOR_TOOL_WALL ); - editor_contextmenu_set_item( &bar->entries[3].items[3], "poly", editor_contextmenu_set_tool_cb, (void*)(I64)EDITOR_TOOL_POLY ); - editor_contextmenu_set_item( &bar->entries[3].items[4], "object", editor_contextmenu_set_tool_cb, (void*)(I64)EDITOR_TOOL_OBJECT ); + gui_contextmenu_set_item( &bar->entries[3].items[0], "none", editor_contextmenu_set_tool_cb, (void*)(I64)EDITOR_TOOL_NONE ); + gui_contextmenu_set_item( &bar->entries[3].items[1], "select", editor_contextmenu_set_tool_cb, (void*)(I64)EDITOR_TOOL_SELECT ); + gui_contextmenu_set_item( &bar->entries[3].items[2], "wall", editor_contextmenu_set_tool_cb, (void*)(I64)EDITOR_TOOL_WALL ); + gui_contextmenu_set_item( &bar->entries[3].items[3], "poly", editor_contextmenu_set_tool_cb, (void*)(I64)EDITOR_TOOL_POLY ); + gui_contextmenu_set_item( &bar->entries[3].items[4], "object", editor_contextmenu_set_tool_cb, (void*)(I64)EDITOR_TOOL_OBJECT ); } -I32 editor_toolbar_root_width( const EDITOR_CONTEXTMENU_ITEM* entry ) { +I32 editor_toolbar_root_width( const GUI_CONTEXTMENU_ITEM* entry ) { if( !entry ) return 44; @@ -196,7 +205,7 @@ void gui_editor_toolbar_draw_fn( void* ptr ) { U8 hovered = hit.root == i; U8 active = bar->held && bar->held_root == i && m1; - U8 open = editor_contextmenu_open() && bar->active_root == i; + U8 open = gui_contextmenu_open( editor->gui.contextmenu ) && bar->active_root == i; CLR fill = ui_clr.bg_sec; if( active ) @@ -228,13 +237,14 @@ void gui_editor_toolbar_input_fn( void* ptr ) { } if( bar->held && bar->held_root == hit.root && hit.root >= 0 ) { - if( editor_contextmenu_open() && bar->active_root == hit.root ) { - editor_hide_contextmenu(); + if( gui_contextmenu_open( editor->gui.contextmenu ) && bar->active_root == hit.root ) { + editor_toolbar_close_menu(); } else { I32 rx = 0; editor_toolbar_root_rect( bar, hit.root, &rx, 0, 0, 0 ); - editor_show_contextmenu( rx, gui_rely( bar ) + bar->h, &bar->entries[hit.root].items ); - bar->active_root = hit.root; + LIST* items = &bar->entries[hit.root].items; + gui_show_contextmenu( rx, gui_rely( bar ) + bar->h, items, editor->wnd, 0, items->size, &editor->gui.contextmenu ); + bar->active_root = editor->gui.contextmenu ? hit.root : -1; } } diff --git a/src/editor/editor_window.cpp b/src/editor/editor_window.cpp index f81b1a8..5cb04c8 100644 --- a/src/editor/editor_window.cpp +++ b/src/editor/editor_window.cpp @@ -7,7 +7,7 @@ void gui_editorwindow_draw_fn( void* ptr ) { editor_raise_header_toolbar( editor ); GUI_BASE* contextmenu = ( editor && editor->map ) ? (GUI_BASE*)editor->gui.contextmenu : 0; - U8 menu_open = editor_contextmenu_open(); + U8 menu_open = gui_contextmenu_open( (GUI_CONTEXTMENU*)contextmenu ); F32 saved_mx = input.mouse.pos.x; F32 saved_my = input.mouse.pos.y; @@ -34,11 +34,12 @@ void gui_editorwindow_draw_fn( void* ptr ) { else dlog( "gui_editorwindow_draw_fn(): child %p has no draw_fn", it ); } ); + input.mouse.pos.x = saved_mx; + input.mouse.pos.y = saved_my; + if( contextmenu && contextmenu->enabled && contextmenu->draw_fn ) contextmenu->draw_fn( contextmenu ); - input.mouse.pos.x = saved_mx; - input.mouse.pos.y = saved_my; editor_menu_hover_mask_active = 0; } @@ -48,14 +49,14 @@ static void gui_editorwindow_input_fn( void* ptr ) { GUI_BASE* toolbar = ( editor && editor->map ) ? editor->gui.header_toolbar : 0; GUI_BASE* contextmenu = ( editor && editor->map ) ? (GUI_BASE*)editor->gui.contextmenu : 0; - U8 menu_was_open = editor_contextmenu_open(); + U8 menu_was_open = gui_contextmenu_open( (GUI_CONTEXTMENU*)contextmenu ); if( toolbar && toolbar->enabled && toolbar->input_fn ) toolbar->input_fn( toolbar ); if( menu_was_open && editor && editor->gui.contextmenu && editor->gui.contextmenu->input_fn ) editor->gui.contextmenu->input_fn( editor->gui.contextmenu ); - if( menu_was_open || editor_contextmenu_open() ) + if( menu_was_open || gui_contextmenu_open( editor ? editor->gui.contextmenu : 0 ) ) return; wnd->children.each( fn( GUI_BASE** childptr ) { @@ -88,6 +89,7 @@ GUI_EDITORWINDOW* gui_editorwindow( I32 w, I32 h ) { GUI_EDITORWINDOW* wnd = gui_editorwindow_create( w, h ); GAME_EDITOR* e = editor; LIST* map_list = editor_get_map_list( e ); + gui_list( wnd->w / 2 - 100, EDITOR_LAYOUT_CONTENT_Y, 200, 200, "map list", map_list, &editor->gui.map_select ); gui_button( wnd->w / 2 - 100, EDITOR_LAYOUT_CONTENT_Y + 220, 95, 25, "new map", editor_new_map_cb ); gui_button( wnd->w / 2 + 5, EDITOR_LAYOUT_CONTENT_Y + 220, 95, 25, "load map", editor_load_map_cb ); -- cgit v1.2.3