Debian Codegraph @

src/cdogsed/editor_ui_color.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2013-2016, 2019-2020 Cong Xu
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include "editor_ui_color.h"
29
30 #include <cdogs/draw/drawtools.h>
31 #include <cdogs/font.h>
32
33 #include "editor_ui.h"
34 #include "editor_ui_common.h"
35
36 typedef struct
37 {
38 color_t Color;
39 char Text[COLOR_STR_BUF];
40 void *Data;
41 ColorPickerGetFunc GetFunc;
42 ColorPickerChangeFunc ChangeFunc;
43 } ColorTextData;
44 static char *ColorPickerText(UIObject *o, void *data);
45 static bool ColorPickerSetFromText(void *data);
46 typedef struct
47 {
48 color_t Color;
49 struct vec2i SwatchSize;
50 struct vec2i SwatchPad;
51 void *Data;
52 ColorPickerChangeFunc ChangeFunc;
53 } ColorPickerData;
54 static void ColorPickerUpdateText(UIObject *o, void *data);
55 static EditorResult ColorPickerChange(void *data, int d);
56 static void ColorPickerDrawSwatch(
57 UIObject *o, GraphicsDevice *g, struct vec2i pos, void *data);
58 // Create a colour picker using the C-Dogs palette
59 UIObject *CreateColorPicker(
60 const struct vec2i pos, void *data, ColorPickerGetFunc getFunc,
61 ColorPickerChangeFunc changeFunc)
62 {
63 UIObject *c = UIObjectCreate(UITYPE_CONTEXT_MENU, 0, pos, svec2i_zero());
64 c->IsDynamicData = true;
65 c->Data = data;
66
67 // Create a text input area for the colour hex code
68 UIObject *oText = UIObjectCreate(
69 UITYPE_TEXTBOX, 0, svec2i_zero(), FontStrSize("#abcdef"));
70 oText->u.Textbox.IsEditable = true;
71 oText->u.Textbox.TextLinkFunc = ColorPickerText;
72 oText->u.Textbox.MaxLen = sizeof((ColorTextData *)0)->Text - 1;
73 CSTRDUP(oText->u.Textbox.Hint, "(enter color hex value)");
74 oText->IsDynamicData = true;
75 CMALLOC(oText->Data, sizeof(ColorTextData));
76 // Initialise colour and text
77 ((ColorTextData *)oText->Data)->Color = getFunc(data);
78 ColorStr(((ColorTextData *)oText->Data)->Text, getFunc(data));
79 ((ColorTextData *)oText->Data)->Data = data;
80 ((ColorTextData *)oText->Data)->GetFunc = getFunc;
81 ((ColorTextData *)oText->Data)->ChangeFunc = changeFunc;
82 oText->OnUnfocusFunc = ColorPickerSetFromText;
83 // Constantly check the current mission colour and update text accordingly
84 oText->CheckVisible = ColorPickerUpdateText;
85 UIObjectAddChild(c, oText);
86
87 const int yStart = oText->Size.y + 4;
88
89 // Create colour squares from the palette
90 const struct vec2i swatchSize = svec2i(5, 5);
91 const struct vec2i swatchPad = svec2i(2, 2);
92 UIObject *o = UIObjectCreate(
93 UITYPE_CUSTOM, 0, svec2i_zero(), svec2i_add(swatchSize, swatchPad));
94 o->ChangeFunc = ColorPickerChange;
95 o->u.CustomDrawFunc = ColorPickerDrawSwatch;
96 const Pic *palette = PicManagerGetPic(&gPicManager, "palette");
97 struct vec2i v;
98 for (v.y = 0; v.y < palette->size.y; v.y++)
99 {
100 for (v.x = 0; v.x < palette->size.x; v.x++)
101 {
102 const color_t colour =
103 PIXEL2COLOR(palette->Data[v.x + v.y * palette->size.x]);
104 if (colour.a == 0)
105 {
106 continue;
107 }
108 UIObject *o2 = UIObjectCopy(o);
109 o2->IsDynamicData = true;
110 CMALLOC(o2->Data, sizeof(ColorPickerData));
111 ((ColorPickerData *)o2->Data)->Color = colour;
112 ((ColorPickerData *)o2->Data)->SwatchSize = swatchSize;
113 ((ColorPickerData *)o2->Data)->SwatchPad = swatchPad;
114 ((ColorPickerData *)o2->Data)->Data = data;
115 ((ColorPickerData *)o2->Data)->ChangeFunc = changeFunc;
116 o2->Pos = svec2i_multiply(v, o->Size);
117 o2->Pos.y += yStart;
118 UIObjectAddChild(c, o2);
119 }
120 }
121 UIObjectDestroy(o);
122
123 // Create a dummy label that can be clicked to close the context menu
124 CreateCloseLabel(
125 c,
126 svec2i(
127 palette->size.x * (swatchSize.x + swatchPad.x) - FontStrW("Close"),
128 0));
129
130 return c;
131 }
132 static void ColorPickerUpdateText(UIObject *o, void *data)
133 {
134 // Only update text if the text box doesn't have focus
135 if (o->Parent->Highlighted == o)
136 return;
137 ColorTextData *cData = data;
138 cData->Color = cData->GetFunc(cData->Data);
139 ColorStr(cData->Text, cData->Color);
140 }
141 static char *ColorPickerText(UIObject *o, void *data)
142 {
143 UNUSED(o);
144 ColorTextData *cData = data;
145 return cData->Text;
146 }
147 static bool ColorPickerSetFromText(void *data)
148 {
149 ColorTextData *cData = data;
150 const color_t colour = StrColor(cData->Text);
151 cData->ChangeFunc(colour, cData->Data);
152 return true;
153 }
154 static EditorResult ColorPickerChange(void *data, int d)
155 {
156 UNUSED(d);
157 ColorPickerData *mc = data;
158 mc->ChangeFunc(mc->Color, mc->Data);
159 // Changing colour updates the masked pics for the mission, which requires
160 // a reload
161 return EDITOR_RESULT_CHANGED_AND_RELOAD;
162 }
163 static void ColorPickerDrawSwatch(
164 UIObject *o, GraphicsDevice *g, struct vec2i pos, void *data)
165 {
166 UNUSED(o);
167 const ColorPickerData *cpd = data;
168 DrawRectangle(
169 g,
170 svec2i_add(
171 svec2i_add(pos, o->Pos), svec2i_scale_divide(cpd->SwatchPad, 2)),
172 cpd->SwatchSize, cpd->Color, true);
173 }
174
175 typedef enum
176 {
177 MISSION_COLOR_WALL,
178 MISSION_COLOR_FLOOR,
179 MISSION_COLOR_ROOM,
180 MISSION_COLOR_EXTRA,
181 MISSION_COLOR_COUNT
182 } MissionColorType;
183 typedef struct
184 {
185 Campaign *C;
186 MissionColorType Type;
187 } MissionColorData;
188 static const char *MissionGetColorStr(UIObject *o, void *data);
189 static void MissionColorChange(const color_t c, void *data);
190 static color_t CampaignGetMissionColor(void *data);
191 struct vec2i CreateColorObjs(
192 Campaign *co, UIObject *c, struct vec2i pos)
193 {
194 const int th = FontH();
195
196 UIObject *o =
197 UIObjectCreate(UITYPE_LABEL, 0, svec2i_zero(), svec2i(100, th));
198 o->u.LabelFunc = MissionGetColorStr;
199
200 for (int i = 0; i < (int)MISSION_COLOR_COUNT; i++)
201 {
202 UIObject *o2 = UIObjectCopy(o);
203 o2->IsDynamicData = true;
204 CMALLOC(o2->Data, sizeof(MissionColorData));
205 ((MissionColorData *)o2->Data)->C = co;
206 ((MissionColorData *)o2->Data)->Type = (MissionColorType)i;
207 o2->Pos = pos;
208
209 MissionColorData *mcd;
210 CMALLOC(mcd, sizeof *mcd);
211 mcd->C = co;
212 mcd->Type = (MissionColorType)i;
213 UIObjectAddChild(
214 o2, CreateColorPicker(
215 svec2i_zero(), mcd, CampaignGetMissionColor,
216 MissionColorChange));
217 UIObjectAddChild(c, o2);
218 pos.y += th;
219 }
220
221 UIObjectDestroy(o);
222 return pos;
223 }
224 static const char *MissionGetColorStr(UIObject *o, void *data)
225 {
226 static char s[128];
227 UNUSED(o);
228 MissionColorData *mc = data;
229 static const char *colourTypeNames[] = {"Walls", "Floors", "Rooms",
230 "Extra"};
231 char c[COLOR_STR_BUF];
232 ColorStr(c, CampaignGetMissionColor(mc));
233 sprintf(s, "%s: #%s", colourTypeNames[(int)mc->Type], c);
234 return s;
235 }
236 static void MissionColorChange(const color_t c, void *data)
237 {
238 MissionColorData *mcd = data;
239 Mission *m = CampaignGetCurrentMission(mcd->C);
240 if (m->Type == MAPTYPE_STATIC)
241 return;
242 MissionTileClasses *mtc = m->Type == MAPTYPE_CLASSIC
243 ? &m->u.Classic.TileClasses
244 : &m->u.Cave.TileClasses;
245 switch (mcd->Type)
246 {
247 case MISSION_COLOR_WALL:
248 mtc->Wall.Mask = c;
249 mtc->Wall.Pic = TileClassGetPic(&gPicManager, &mtc->Wall);
250 break;
251 case MISSION_COLOR_FLOOR:
252 mtc->Floor.Mask = c;
253 mtc->Floor.Pic = TileClassGetPic(&gPicManager, &mtc->Floor);
254 break;
255 case MISSION_COLOR_ROOM:
256 mtc->Room.Mask = c;
257 mtc->Room.Pic = TileClassGetPic(&gPicManager, &mtc->Room);
258 break;
259 case MISSION_COLOR_EXTRA:
260 mtc->Wall.MaskAlt = c;
261 mtc->Floor.MaskAlt = c;
262 mtc->Room.MaskAlt = c;
263 mtc->Wall.Pic = TileClassGetPic(&gPicManager, &mtc->Wall);
264 mtc->Floor.Pic = TileClassGetPic(&gPicManager, &mtc->Floor);
265 mtc->Room.Pic = TileClassGetPic(&gPicManager, &mtc->Room);
266 break;
267 default:
268 CASSERT(false, "Unexpected mission colour");
269 break;
270 }
271 }
272 static color_t CampaignGetMissionColor(void *data)
273 {
274 MissionColorData *mcd = data;
275 const Mission *m = CampaignGetCurrentMission(mcd->C);
276 if (m == NULL)
277 return colorWhite;
278 if (m->Type == MAPTYPE_STATIC)
279 return colorWhite;
280 const MissionTileClasses *mtc = m->Type == MAPTYPE_CLASSIC
281 ? &m->u.Classic.TileClasses
282 : &m->u.Cave.TileClasses;
283 switch (mcd->Type)
284 {
285 case MISSION_COLOR_WALL:
286 return mtc->Wall.Mask;
287 case MISSION_COLOR_FLOOR:
288 return mtc->Floor.Mask;
289 case MISSION_COLOR_ROOM:
290 return mtc->Room.Mask;
291 case MISSION_COLOR_EXTRA:
292 return mtc->Wall.MaskAlt;
293 default:
294 CASSERT(false, "Unexpected mission colour");
295 return colorWhite;
296 }
297 }