about summary refs log tree commit diff
path: root/src/game/editor/ed_editor.hpp
blob: 98d1d9608c661d03f8168f1d4cb7f6c9d6af4acd (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */

#include <base/system.h>
#include <base/math.hpp>

#include <stdlib.h>
#include <math.h>
#include "array.hpp"
#include "../mapitems.hpp"
#include "../client/render.hpp"

#include <engine/e_client_interface.h>
#include <engine/e_datafile.h>
#include <engine/e_config.h>
#include <engine/client/editor.h>

#include <game/client/ui.hpp>

typedef void (*INDEX_MODIFY_FUNC)(int *index);

//CRenderTools m_RenderTools;

// EDITOR SPECIFIC
template<typename T>
void swap(T &a, T &b)
{
	T tmp = a;
	a = b;
	b = tmp;
}

enum
{
	MODE_LAYERS=0,
	MODE_IMAGES,
	
	DIALOG_NONE=0,
	DIALOG_FILE,
};

typedef struct
{
	POINT position;
	int type;
} ENTITY;

class ENVELOPE
{
public:
	int channels;
	array<ENVPOINT> points;
	char name[32];
	float bottom, top;
	
	ENVELOPE(int chan)
	{
		channels = chan;
		name[0] = 0;
		bottom = 0;
		top = 0;
	}
	
	static int sort_comp(const void *v0, const void *v1)
	{
		const ENVPOINT *p0 = (const ENVPOINT *)v0;
		const ENVPOINT *p1 = (const ENVPOINT *)v1;
		if(p0->time < p1->time)
			return -1;
		if(p0->time > p1->time)
			return 1;
		return 0;
	}
	
	void resort()
	{
		qsort(points.getptr(), points.len(), sizeof(ENVPOINT), sort_comp);
		find_top_bottom(0xf);
	}

	void find_top_bottom(int channelmask)
	{
		top = -1000000000.0f;
		bottom = 1000000000.0f;
		for(int i = 0; i < points.len(); i++)
		{
			for(int c = 0; c < channels; c++)
			{
				if(channelmask&(1<<c))
				{
					float v = fx2f(points[i].values[c]);
					if(v > top) top = v;
					if(v < bottom) bottom = v;
				}
			}
		}
	}
	
	int eval(float time, float *result)
	{
		CRenderTools::render_eval_envelope(points.getptr(), points.len(), channels, time, result);
		return channels;
	}
	
	void add_point(int time, int v0, int v1=0, int v2=0, int v3=0)
	{
		ENVPOINT p;
		p.time = time;
		p.values[0] = v0;
		p.values[1] = v1;
		p.values[2] = v2;
		p.values[3] = v3;
		p.curvetype = CURVETYPE_LINEAR;
		points.add(p);
		resort();
	}
	
	float end_time()
	{
		if(points.len())
			return points[points.len()-1].time*(1.0f/1000.0f);
		return 0;
	}
};


class LAYER;
class LAYERGROUP;
class MAP;

class LAYER
{
public:
	class EDITOR *editor;
	class IGraphics *Graphics();

	LAYER()
	{
		type = LAYERTYPE_INVALID;
		type_name = "(invalid)";
		visible = true;
		readonly = false;
		flags = 0;
		editor = 0;
	}
	
	virtual ~LAYER()
	{
	}
	
	
	virtual void brush_selecting(CUIRect rect) {}
	virtual int brush_grab(LAYERGROUP *brush, CUIRect rect) { return 0; }
	virtual void brush_draw(LAYER *brush, float x, float y) {}
	virtual void brush_place(LAYER *brush, float x, float y) {}
	virtual void brush_flip_x() {}
	virtual void brush_flip_y() {}
	virtual void brush_rotate(float amount) {}
	
	virtual void render() {}
	virtual int render_properties(CUIRect *toolbox) { return 0; }
	
	virtual void modify_image_index(INDEX_MODIFY_FUNC func) {}
	virtual void modify_envelope_index(INDEX_MODIFY_FUNC func) {}
	
	virtual void get_size(float *w, float *h) { *w = 0; *h = 0;}
	
	const char *type_name;
	int type;
	int flags;

	bool readonly;
	bool visible;
};

class LAYERGROUP
{
public:
	class MAP *m_pMap;
	
	array<LAYER*> layers;
	
	int offset_x;
	int offset_y;

	int parallax_x;
	int parallax_y;
	
	int use_clipping;
	int clip_x;
	int clip_y;
	int clip_w;
	int clip_h;
	
	const char *name;
	bool game_group;
	bool visible;
	
	LAYERGROUP();
	~LAYERGROUP();
	
	void convert(CUIRect *rect);
	void render();
	void mapscreen();
	void mapping(float *points);
	
	bool is_empty() const;
	void clear();
	void add_layer(LAYER *l);

	void get_size(float *w, float *h);
	
	void delete_layer(int index);
	int swap_layers(int index0, int index1);

	void modify_image_index(INDEX_MODIFY_FUNC func)
	{
		for(int i = 0; i < layers.len(); i++)
			layers[i]->modify_image_index(func);
	}
	
	void modify_envelope_index(INDEX_MODIFY_FUNC func)
	{
		for(int i = 0; i < layers.len(); i++)
			layers[i]->modify_envelope_index(func);
	}
};

class EDITOR_IMAGE : public IMAGE_INFO
{
public:
	EDITOR *editor;
	
	EDITOR_IMAGE(EDITOR *ed)
	{
		editor = editor;
		tex_id = -1;
		name[0] = 0;
		external = 0;
		width = 0;
		height = 0;
		data = 0;
		format = 0;
	}
	
	~EDITOR_IMAGE();
	
	void analyse_tileflags();
	
	int tex_id;
	int external;
	char name[128];
	unsigned char tileflags[256];
};

class MAP
{
	void make_game_group(LAYERGROUP *group);
	void make_game_layer(LAYER *layer);
public:
	EDITOR *editor;

	MAP()
	{
		clean();
	}

	array<LAYERGROUP*> groups;
	array<EDITOR_IMAGE*> images;
	array<ENVELOPE*> envelopes;
	
	class LAYER_GAME *game_layer;
	LAYERGROUP *game_group;
	
	ENVELOPE *new_envelope(int channels)
	{
		ENVELOPE *e = new ENVELOPE(channels);
		envelopes.add(e);
		return e;
	}
	
	LAYERGROUP *new_group()
	{
		LAYERGROUP *g = new LAYERGROUP;
		g->m_pMap = this;
		groups.add(g);
		return g;
	}
	
	int swap_groups(int index0, int index1)
	{
		if(index0 < 0 || index0 >= groups.len()) return index0;
		if(index1 < 0 || index1 >= groups.len()) return index0;
		if(index0 == index1) return index0;
		swap(groups[index0], groups[index1]);
		return index1;
	}
	
	void delete_group(int index)
	{
		if(index < 0 || index >= groups.len()) return;
		delete groups[index];
		groups.removebyindex(index);
	}
	
	void modify_image_index(INDEX_MODIFY_FUNC func)
	{
		for(int i = 0; i < groups.len(); i++)
			groups[i]->modify_image_index(func);
	}
	
	void modify_envelope_index(INDEX_MODIFY_FUNC func)
	{
		for(int i = 0; i < groups.len(); i++)
			groups[i]->modify_envelope_index(func);
	}
	
	void clean();
	void create_default(int entities_texture);

	// io	
	int save(const char *filename);
	int load(const char *filename);
};


struct PROPERTY
{
	const char *name;
	int value;
	int type;
	int min;
	int max;
};

enum
{
	PROPTYPE_NULL=0,
	PROPTYPE_BOOL,
	PROPTYPE_INT_STEP,
	PROPTYPE_INT_SCROLL,
	PROPTYPE_COLOR,
	PROPTYPE_IMAGE,
	PROPTYPE_ENVELOPE,
};

class EDITOR : public IEditor
{
	class IGraphics *m_pGraphics;
	CRenderTools m_RenderTools;
	CUI m_UI;
public:
	
	class IGraphics *Graphics() { return m_pGraphics; };
	CUI *UI() { return &m_UI; }
	CRenderTools *RenderTools() { return &m_RenderTools; }

	EDITOR()
	{
		mode = MODE_LAYERS;
		dialog = 0;
		tooltip = 0;

		world_offset_x = 0;
		world_offset_y = 0;
		editor_offset_x = 0.0f;
		editor_offset_y = 0.0f;
		
		world_zoom = 1.0f;
		zoom_level = 100;
		lock_mouse = false;
		show_mouse_pointer = true;
		mouse_delta_x = 0;
		mouse_delta_y = 0;
		mouse_delta_wx = 0;
		mouse_delta_wy = 0;
		
		gui_active = true;
		proof_borders = false;
		
		show_detail = true;
		animate = false;
		animate_start = 0;
		animate_time = 0;
		animate_speed = 1;
		
		show_envelope_editor = 0;
	}
	
	virtual void Init(class IGraphics *pGraphics);
	virtual void UpdateAndRender();
	
	void invoke_file_dialog(int listdir_type, const char *title, const char *button_text,
		const char *basepath, const char *default_name,
		void (*func)(const char *filename, void *user), void *user);
	
	void reset(bool create_default=true);
	int save(const char *filename);
	int load(const char *filename);
	int append(const char *filename);
	void render();

	QUAD *get_selected_quad();
	LAYER *get_selected_layer_type(int index, int type);
	LAYER *get_selected_layer(int index);
	LAYERGROUP *get_selected_group();
	
	int do_properties(CUIRect *toolbox, PROPERTY *props, int *ids, int *new_val);
	
	int mode;
	int dialog;
	const char *tooltip;

	float world_offset_x;
	float world_offset_y;
	float editor_offset_x;
	float editor_offset_y;
	float world_zoom;
	int zoom_level;
	bool lock_mouse;
	bool show_mouse_pointer;
	bool gui_active;
	bool proof_borders;
	float mouse_delta_x;
	float mouse_delta_y;
	float mouse_delta_wx;
	float mouse_delta_wy;
	
	bool show_detail;
	bool animate;
	int64 animate_start;
	float animate_time;
	float animate_speed;
	
	int show_envelope_editor;
	
	int selected_layer;
	int selected_group;
	int selected_quad;
	int selected_points;
	int selected_envelope;
	int selected_image;
	
	MAP map;
	
	int DoButton_Editor_Common(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
	int DoButton_Editor(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);

	int DoButton_ButtonL(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
	int DoButton_ButtonM(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
	int DoButton_ButtonR(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
	int DoButton_ButtonDec(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
	int DoButton_ButtonInc(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);

	int DoButton_File(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
	
	int DoButton_Menu(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags, const char *pToolTip);
	int DoButton_MenuItem(const void *pID, const char *pText, int Checked, const CUIRect *pRect, int Flags=0, const char *pToolTip=0);
	
	int DoEditBox(void *pID, const CUIRect *pRect, char *pStr, unsigned StrSize, float FontSize, bool Hidden=false);
	//static void draw_editor_button(const void *id, const char *text, int checked, const CUIRect *r, const void *extra);
	//static void draw_editor_button_menuitem(const void *id, const char *text, int checked, const CUIRect *r, const void *extra);

	void render_background(CUIRect view, int texture, float size, float brightness);

	void ui_invoke_popup_menu(void *id, int flags, float x, float y, float w, float h, int (*func)(EDITOR *pEditor, CUIRect rect), void *extra=0);
	void ui_do_popup_menu();
	
	int ui_do_value_selector(void *id, CUIRect *r, const char *label, int current, int min, int max, float scale);

	static int popup_group(EDITOR *pEditor, CUIRect view);
	static int popup_layer(EDITOR *pEditor, CUIRect view);
	static int popup_quad(EDITOR *pEditor, CUIRect view);
	static int popup_point(EDITOR *pEditor, CUIRect view);
	static int popup_select_image(EDITOR *pEditor, CUIRect view);
	static int popup_image(EDITOR *pEditor, CUIRect view);
	static int popup_menu_file(EDITOR *pEditor, CUIRect view);


	void popup_select_image_invoke(int current, float x, float y);
	int popup_select_image_result();
	
	vec4 button_color_mul(const void *id);

	void do_quad_point(QUAD *q, int quad_index, int v);
	void do_map_editor(CUIRect view, CUIRect toolbar);
	void do_toolbar(CUIRect toolbar);
	void do_quad(QUAD *q, int index);
	float ui_do_scrollbar_v(const void *id, const CUIRect *rect, float current);
	vec4 get_button_color(const void *id, int checked);
	
	static void replace_image(const char *filename, void *user);
	static void add_image(const char *filename, void *user);
	
	void render_images(CUIRect toolbox, CUIRect toolbar, CUIRect view);
	void render_layers(CUIRect toolbox, CUIRect toolbar, CUIRect view);
	void render_modebar(CUIRect view);
	void render_statusbar(CUIRect view);
	void render_envelopeeditor(CUIRect view);
	
	void render_menubar(CUIRect menubar);
	void render_file_dialog();
};

// make sure to inline this function
inline class IGraphics *LAYER::Graphics() { return editor->Graphics(); }

//extern EDITOR editor;

typedef struct
{
	int x, y;
	int w, h;
} RECTi;

class LAYER_TILES : public LAYER
{
public:
	LAYER_TILES(int w, int h);
	~LAYER_TILES();

	void resize(int new_w, int new_h);

	void make_palette();
	virtual void render();

	int convert_x(float x) const;
	int convert_y(float y) const;
	void convert(CUIRect rect, RECTi *out);
	void snap(CUIRect *rect);
	void clamp(RECTi *rect);

	virtual void brush_selecting(CUIRect rect);
	virtual int brush_grab(LAYERGROUP *brush, CUIRect rect);
	virtual void brush_draw(LAYER *brush, float wx, float wy);
	virtual void brush_flip_x();
	virtual void brush_flip_y();
	
	virtual int render_properties(CUIRect *toolbox);

	virtual void modify_image_index(INDEX_MODIFY_FUNC func);
	virtual void modify_envelope_index(INDEX_MODIFY_FUNC func);
	
	void prepare_for_save();

	void get_size(float *w, float *h) { *w = width*32.0f;  *h = height*32.0f; }
	
	int tex_id;
	int game;
	int image;
	int width;
	int height;
	TILE *tiles;
};

class LAYER_QUADS : public LAYER
{
public:
	LAYER_QUADS();
	~LAYER_QUADS();

	virtual void render();
	QUAD *new_quad();

	virtual void brush_selecting(CUIRect rect);
	virtual int brush_grab(LAYERGROUP *brush, CUIRect rect);
	virtual void brush_place(LAYER *brush, float wx, float wy);
	virtual void brush_flip_x();
	virtual void brush_flip_y();
	virtual void brush_rotate(float amount);
	
	virtual int render_properties(CUIRect *toolbox);

	virtual void modify_image_index(INDEX_MODIFY_FUNC func);
	virtual void modify_envelope_index(INDEX_MODIFY_FUNC func);
	
	void get_size(float *w, float *h);
	
	int image;
	array<QUAD> quads;
};

class LAYER_GAME : public LAYER_TILES
{
public:
	LAYER_GAME(int w, int h);
	~LAYER_GAME();

	virtual int render_properties(CUIRect *toolbox);
};