diff options
Diffstat (limited to 'src/gui/window.cpp')
| -rw-r--r-- | src/gui/window.cpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/gui/window.cpp b/src/gui/window.cpp new file mode 100644 index 0000000..0183d8c --- /dev/null +++ b/src/gui/window.cpp @@ -0,0 +1,113 @@ +#include "base.h" + +#include "../render/gl.h" + +void gui_window_draw_fn( void* ptr ) { + GUI_WINDOW* wnd = (GUI_WINDOW*)ptr; + + CLR clr = gui_is_fg_window( wnd )? ui_clr.border : ui_clr.border_inactive; + gui_draw_frect( wnd->x, wnd->y, wnd->w, wnd->h, clr ); + gui_draw_frect( wnd->x + 1, wnd->y + 1, wnd->w - 2, wnd->h - 2, ui_clr.bg ); + + wnd->children.each( fn( GUI_BASE** ptr ) { + GUI_BASE* it = *ptr; + if( it->draw_fn ) it->draw_fn( it ); + else dlog( "window_draw_fn() : child %p no draw_fn", it ); + } ); +} + +GUI_WINDOW* gui_window( I32 x, I32 y, I32 w, I32 h ) { + GUI_WINDOW* wnd = new GUI_WINDOW; + wnd->x = x; + wnd->y = y; + wnd->xbound = wnd->w = w; + wnd->ybound = wnd->h = h; + wnd->draw_fn = gui_window_draw_fn; + strcpy( wnd->name, "BASE_WINDOW" ); + + _gui.windows.push( wnd ); + gui_set_window( wnd ); + gui_set_view( 0 ); + + gui_view( 1, 1, w - 2, h - 2 ); + return wnd; +} + +GUI_WINDOW* gui_window( I32 w, I32 h ) { + return gui_window( 0, 0, w, h ); +} + +void gui_title_draw_fn( void* ptr ) { + GUI_TITLE* t = (GUI_TITLE*)ptr; + + I32 relx = gui_relx( t ); + I32 rely = gui_rely( t ); + gui_draw_str( relx + 5, rely + 3, 0, FNT_JPN12, ui_clr.txt, t->name ); +} + +void gui_title_input_fn( void* ptr ) { + GUI_TITLE* t = (GUI_TITLE*)ptr; + GUI_WINDOW* w = gui_get_parent_wnd( t ); + + if( w->locked ) + return; + + I32 x = gui_relx( t ); + I32 y = gui_rely( t ); + + U8 m1 = gui_mbutton_down( 0 ); + I32 mx, my; + gui_cursor_pos( &mx, &my ); + + if( !m1 ) { + t->held = 0; + return; + } + + if( !t->held && + mx >= x && mx <= x + t->w && + my >= y && my <= y + t->h + ) { + I32 wx = w->x; + I32 wy = w->y; + + I32 moffx = mx - wx; + I32 moffy = my - wy; + + t->xoff = moffx; + t->yoff = moffy; + + t->held = 1; + } + else if( t->held ) { + w->x = mx - t->xoff; + w->y = my - t->yoff; + + if( w->x > _gui.gl2d->gl->canvas_size[0] - 5 ) + w->x = _gui.gl2d->gl->canvas_size[0] - 5; + if( w->x + w->w < 5 ) + w->x = 5 - w->w; + if( w->y > _gui.gl2d->gl->canvas_size[1] - 5 ) + w->y = _gui.gl2d->gl->canvas_size[1] - 5; + if( w->y + w->h < 5 ) + w->y = 5 - w->h; + } +} + +GUI_TITLE* gui_title( const char* title ) { + if( !gui_check_target() ) return 0; + + GUI_TITLE* t = new GUI_TITLE; + t->parent = gui_get_view(); + t->x = 0; + t->y = 0; + t->h = 25; + t->w = t->parent->w; + t->draw_fn = gui_title_draw_fn; + t->input_fn = gui_title_input_fn; + strcpy( t->name, title ); + + gui_get_view()->children.push( t ); + + return t; +} |
