summaryrefslogtreecommitdiff
path: root/src/gui/window.cpp
blob: 500cb3d451380f17c40c01a7d564264c28d7c3cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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.batch->gl->canvas_size[0] - 5 )
      w->x = _gui.batch->gl->canvas_size[0] - 5;
    if( w->x + w->w < 5 )
      w->x = 5 - w->w;
    if( w->y > _gui.batch->gl->canvas_size[1] - 5 )
      w->y = _gui.batch->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;
}