1 /*
2  C-Dogs SDL
3  A port of the legendary (and fun) action/arcade cdogs.
4 
5  Copyright (c) 2013-2014, 2016 Cong Xu
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  POSSIBILITY OF SUCH DAMAGE.
28  */
29 #pragma once
30 
31 #include <stdbool.h>
32 
33 #include <cdogs/c_array.h>
34 #include <cdogs/grafx.h>
35 #include <cdogs/pic.h>
36 #include <cdogs/vector.h>
37 
38 #include "editor_brush.h"
39 
40 typedef enum
41 {
42 	UITYPE_NONE,
43 	UITYPE_LABEL,
44 	UITYPE_TEXTBOX,
45 	UITYPE_BUTTON,	// click button with picture
46 	UITYPE_CONTEXT_MENU,	// appear on mouse click
47 	UITYPE_CUSTOM
48 } UIType;
49 
50 #define UI_SELECT_ONLY							1
51 #define UI_LEAVE_YC								2
52 #define UI_LEAVE_XC								4
53 #define UI_SELECT_ONLY_FIRST					8
54 #define UI_ENABLED_WHEN_PARENT_HIGHLIGHTED_ONLY	16
55 
56 typedef struct _UIObject
57 {
58 	UIType Type;
59 	int Id;
60 	int Id2;
61 	int Flags;
62 	struct vec2i Pos;
63 	struct vec2i Size;
64 	bool IsVisible;
65 	// TODO: memory leak with dynamically allocated tooltips
66 	char *Tooltip;
67 	struct _UIObject *Parent;
68 	CArray Children;	// of UIObject *
69 	struct _UIObject *Highlighted;
70 	bool DoNotHighlight;
71 	bool IsBackground;
72 	union
73 	{
74 		// Labels
75 		const char *(*LabelFunc)(struct _UIObject *, void *);
76 		// Text boxes
77 		struct
78 		{
79 			char *(*TextLinkFunc)(struct _UIObject *, void *);
80 			int MaxLen;	// Buffer should hold MaxLen + 1
81 			char **(*TextSourceFunc)(void *);
82 			bool IsEditable;
83 			char *Hint;
84 		} Textbox;
85 		// Button
86 		struct
87 		{
88 			Pic *Pic;
89 			int (*IsDownFunc)(void *);	// whether the button is down
90 		} Button;
91 		// Custom
92 		void (*CustomDrawFunc)(struct _UIObject *, GraphicsDevice *g, struct vec2i, void *);
93 	} u;
94 	char *Label;
95 	bool IsDynamicLabel;
96 	void *Data;
97 	int IsDynamicData;
98 	EditorResult (*ChangeFunc)(void *, int d);
99 	// Function that's called when the control is activated with shift key held
100 	EditorResult (*ChangeFuncAlt)(void *, int d);
101 	// Whether the change func disables parent context menus (default true)
102 	bool ChangeDisablesContext;
103 	void (*OnFocusFunc)(struct _UIObject *, void *);
104 	// Returns whether mission changed
105 	bool (*OnUnfocusFunc)(void *);
106 	// Optional function to check whether this UI object should be visible
107 	void (*CheckVisible)(struct _UIObject *, void *);
108 } UIObject;
109 
110 UIObject *UIObjectCreate(UIType type, int id, struct vec2i pos, struct vec2i size);
111 void UIObjectSetDynamicLabel(UIObject *o, const char *label);
112 void UIButtonSetPic(UIObject *o, Pic *pic);
113 UIObject *UIObjectCopy(const UIObject *o);
114 void UIObjectDestroy(UIObject *o);
115 void UIObjectAddChild(UIObject *o, UIObject *c);
116 void UITabAddChild(UIObject *o, UIObject *c, char *label);
117 void UIObjectHighlight(UIObject *o, const bool shift);
118 // Returns whether mission changed
119 bool UIObjectUnhighlight(UIObject *o, const bool unhighlightParents);
120 int UIObjectIsHighlighted(UIObject *o);
121 EditorResult UIObjectChange(UIObject *o, const int d, const bool shift);
122 
123 // Add and delete chars from the highlighted text box
124 // Returns whether a change has been made
125 EditorResult UIObjectAddChar(UIObject *o, char c);
126 EditorResult UIObjectDelChar(UIObject *o);
127 
128 void UIObjectDraw(
129 	UIObject *o, GraphicsDevice *g, struct vec2i pos, struct vec2i mouse, CArray *drawObjs);
130 
131 // Get the UIObject that is at pos (e.g. for mouse clicks)
132 bool UITryGetObject(UIObject *o, struct vec2i pos, UIObject **out);
133 
134 void UITooltipDraw(GraphicsDevice *device, struct vec2i pos, const char *s);
135