summaryrefslogtreecommitdiff
path: root/src/editor/editor_window.cpp
blob: f81b1a840f411e671e2a8450e3114f97552047db (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "editor_gui_internal.h"

#include "../game.h"

void gui_editorwindow_draw_fn( void* ptr ) {
  GUI_EDITORWINDOW* wnd = (GUI_EDITORWINDOW*)ptr;
  editor_raise_header_toolbar( editor );

  GUI_BASE* contextmenu = ( editor && editor->map ) ? (GUI_BASE*)editor->gui.contextmenu : 0;
  U8 menu_open = editor_contextmenu_open();

  F32 saved_mx = input.mouse.pos.x;
  F32 saved_my = input.mouse.pos.y;
  if( menu_open ) {
    editor_menu_hover_mask_active = 1;
    editor_menu_hover_real_x = (I32)saved_mx;
    editor_menu_hover_real_y = (I32)saved_my;
    input.mouse.pos.x = -100000.f;
    input.mouse.pos.y = -100000.f;
  } else {
    editor_menu_hover_mask_active = 0;
  }

  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 == contextmenu )
      return;
    if( !it->enabled ) return;
    if( it->draw_fn ) it->draw_fn( it );
    else dlog( "gui_editorwindow_draw_fn(): child %p has no draw_fn", it );
  } );

  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;
}

static void gui_editorwindow_input_fn( void* ptr ) {
  GUI_EDITORWINDOW* wnd = (GUI_EDITORWINDOW*)ptr;
  editor_raise_header_toolbar( editor );

  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();
  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() )
    return;

  wnd->children.each( fn( GUI_BASE** childptr ) {
    GUI_BASE* child = *childptr;
    if( !child || child == toolbar || child == contextmenu || !child->enabled || !child->input_fn )
      return;
    child->input_fn( child );
  } );
}

GUI_EDITORWINDOW* gui_editorwindow_create( I32 w, I32 h ) {
  GUI_EDITORWINDOW* wnd = new GUI_EDITORWINDOW;
  wnd->x = 0;
  wnd->y = 0;
  wnd->xbound = wnd->w = w;
  wnd->ybound = wnd->h = h;
  wnd->locked = 1;
  wnd->draw_fn = gui_editorwindow_draw_fn;
  wnd->input_fn = gui_editorwindow_input_fn;
  strcpy( wnd->name, "EDITORWINDOW" );

  _gui.windows.push( wnd );
  gui_set_window( wnd );
  gui_set_view( 0 );
  gui_view( 1, 1, w - 2, h - 2 );
  return wnd;
}

GUI_EDITORWINDOW* gui_editorwindow( I32 w, I32 h ) {
  GUI_EDITORWINDOW* wnd = gui_editorwindow_create( w, h );
  GAME_EDITOR* e = editor;
  LIST<GUI_LIST_ENTRY>* 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 );
  return wnd;
}

void editor_update_properties_column( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  gui_editor_propview_update( egui->props );
}

void editor_create_properties_column( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  egui->props = gui_editor_propview( 10, EDITOR_LAYOUT_CONTENT_Y, 300, 460 );
  gui_editor_propview_select( egui->props, e->map, EDITOR_SELECT_ORIGIN );
}

void editor_create_auxiliary_panels( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  egui->assets = gui_editor_infobox( 10, EDITOR_LAYOUT_CONTENT_Y + 310, 300, 180, EDITOR_INFOBOX_ASSETS, "EDITOR_ASSETS_VIEW" );
  egui->status = gui_editor_infobox( 10, 568, 780, EDITOR_LAYOUT_STATUS_H, EDITOR_INFOBOX_STATUS, "EDITOR_STATUS_VIEW" );
}

void editor_update_toolview( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  gui_editor_toolview_update( egui->tool );
}

void editor_settool( U8 t ) {
  editor->tool.type = t;
  if( editor->gui.v2d )
    editor->gui.v2d->poly_drag = 0;
  if( editor->gui.tool )
    editor_update_toolview( editor );
}

void settool( U8 t ) {
  editor_settool( t );
}

const char* editor_tool_name() {
  switch( editor->tool.type ) {
    case EDITOR_TOOL_SELECT: return "select";
    case EDITOR_TOOL_WALL: return "wall";
    case EDITOR_TOOL_POLY: return "poly";
    case EDITOR_TOOL_OBJECT: return "object";
    default: return "none";
  }
}

static U8 EDITOR_TOOL_BUTTON_TYPES[] = {
  EDITOR_TOOL_NONE,
  EDITOR_TOOL_SELECT,
  EDITOR_TOOL_WALL,
  EDITOR_TOOL_POLY,
  EDITOR_TOOL_OBJECT
};

static void settool_btn_cb( void* ptr ) {
  GUI_BUTTON* btn = (GUI_BUTTON*)ptr;
  if( !btn || !btn->extra )
    return;

  settool( *(U8*)btn->extra );
}

void editor_create_toolview_column( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;

  I32 x = 320;
  I32 y = EDITOR_LAYOUT_CONTENT_Y + EDITOR_LAYOUT_VIEW_DEFAULT_H + EDITOR_LAYOUT_VIEW_TOOL_GAP + EDITOR_LAYOUT_TOOL_BTN_TOP_GAP;
  I32 off = 23;
  const char* labels[] = { "none", "select", "wall", "poly", "object" };

  for( U32 i = 0; i < sizeof( labels ) / sizeof( labels[0] ); ++i ) {
    GUI_BUTTON* btn = gui_button( x, y, 45, 20, labels[i], settool_btn_cb );
    btn->extra = &EDITOR_TOOL_BUTTON_TYPES[i];
    y += off;
  }

  egui->tool = gui_editor_toolview( 370, y - EDITOR_LAYOUT_TOOL_BTN_TOP_GAP, 300, 150 );
  gui_editor_toolview_update( egui->tool );
}

void editor_create_game_view_column( GAME_EDITOR* e ) {
  GAME_EDITOR::EDITOR_GUI* egui = &e->gui;
  I32 x = 320, y = EDITOR_LAYOUT_CONTENT_Y;
  egui->v2d = gui_editor_2dview( x, y, 468, 370 );
  egui->v3d = gui_editor_3dview( x, y, 468, 370 );
  egui->v2d->enabled = 1;
  egui->v3d->enabled = 0;
}

void editor_create_map_view( GAME_EDITOR* e ) {
  if( !e->map ) {
    dlog( "editor_create_map_views() : no map loaded\n" );
    return;
  }

  GUI_EDITORWINDOW* w = e->wnd;
  w->children.each( fn( GUI_BASE** ptr ) {
    gui_free( *ptr );
  } );
  w->children.clear();
  editor_clear_gui_state_refs( e );

  e->gui.view2d_type = EDITOR_2DVIEW_TOP_DOWN;
  e->gui.view_mode = EDITOR_VIEWMODE_2D;

  gui_set_window( w );
  gui_set_view( 0 );
  gui_view( 1, 1, w->w - 2, w->h - 2 );

  editor_create_header_controls( e );
  editor_create_properties_column( e );
  editor_create_toolview_column( e );
  editor_create_game_view_column( e );
  editor_create_auxiliary_panels( e );
  editor_layout_map_view( e );
}

void editor_resize( GAME_EDITOR* e, I32 w, I32 h ) {
  if( !e || !e->wnd )
    return;

  w = max( 1, w );
  h = max( 1, h );

  GUI_EDITORWINDOW* wnd = e->wnd;
  if( wnd->w == w && wnd->h == h )
    return;

  editor_set_bounds( wnd, 0, 0, w, h );
  editor_resize_base_view( wnd );

  if( e->map ) editor_layout_map_view( e );
  else editor_layout_start_menu( e );

  if( !e->gui.new_map_popup )
    return;

  GUI_WINDOW* popup = e->gui.new_map_popup;
  if( popup->x > w - 5 ) popup->x = w - 5;
  if( popup->x + popup->w < 5 ) popup->x = 5 - popup->w;
  if( popup->y > h - 5 ) popup->y = h - 5;
  if( popup->y + popup->h < 5 ) popup->y = 5 - popup->h;
}

void close_new_map_popup( void* ) {
  gui_push_callback( pfn( void* ) {
    GUI_WINDOW* popup = editor->gui.new_map_popup;
    if( !popup )
      return;

    gui_free( popup );
    editor->gui.new_map_popup = 0;
  } );
}

void editor_new_map_cb( void* ptr ) {
  if( editor->gui.new_map_popup )
    return;

  GUI_WINDOW* cwnd = gui_get_window();
  GUI_VIEW* view = gui_get_view();

  I32 wx = 250;
  I32 wy = 140;
  I32 ww = 300;
  I32 wh = 200;

  editor->gui.new_map_popup = gui_window( wx, wy, ww, wh );
  gui_title( "new map" );
  GUI_TEXTBOX* tb = gui_textbox( 10, 20, ww - 20, 20, "name", 32 );
  tb->active = 1;

  gui_button( ww - 50 - 70, wh - 20 - 10, 50, 20, "ok", pfn( void* ptr ) {
    GUI_BUTTON* btn = (GUI_BUTTON*)ptr;
    GUI_TEXTBOX* name = (GUI_TEXTBOX*)gui_find_node( btn->parent, "name" );
    if( !name )
      return;

    editor_new_map( editor, name->value );
    close_new_map_popup( 0 );
  } );

  gui_button( ww - 50 - 10, wh - 20 - 10, 50, 20, "cancel", close_new_map_popup );
  gui_set_window( cwnd );
  gui_set_view( view );
}

void editor_load_map_cb( void* ptr ) {
  GUI_LIST* list = (GUI_LIST*)gui_find_node( editor->wnd, "map list" );
  GUI_LIST_ENTRY* e = gui_list_get_selected( list );
  if( !e )
    return;

  char full_path[256];
  sprintf( full_path, "../assets/maps/%s", e->title );

  if( editor->map )
    editor_close( editor );

  editor_load_map( editor, full_path );
}