Debian Codegraph @

src/cdogsed/editor_ui_weapons.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, 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_weapons.h"
29
30 #include <cdogs/font.h>
31
32 #include "editor_ui_common.h"
33
34 typedef enum
35 {
36 SELECT_ALL,
37 SELECT_NONE,
38 SELECT_INVERSE
39 } SelectMode;
40 typedef struct
41 {
42 Campaign *co;
43 SelectMode Mode;
44 } MissionAllGunsData;
45 static bool HasWeapon(const Mission *m, const WeaponClass *wc);
46 static void AddAllWeapons(Mission *m);
47 static EditorResult MissionSelectWeapons(void *vData, int d)
48 {
49 UNUSED(d);
50 MissionAllGunsData *data = vData;
51 Mission *currentMission = CampaignGetCurrentMission(data->co);
52 if (currentMission == NULL)
53 return EDITOR_RESULT_NONE;
54 switch (data->Mode)
55 {
56 case SELECT_ALL:
57 AddAllWeapons(currentMission);
58 break;
59 case SELECT_NONE:
60 CArrayClear(&currentMission->Weapons);
61 break;
62 case SELECT_INVERSE: {
63 // Add all the weapons not currently available, then delete the first
64 // entries in the weapons array corresponding to the previously
65 // available weapons
66 const int availableWeapons = (int)currentMission->Weapons.size;
67 AddAllWeapons(currentMission);
68 for (int i = 0; i < availableWeapons; i++)
69 {
70 CArrayDelete(&currentMission->Weapons, 0);
71 }
72 }
73 break;
74 default:
75 CASSERT(false, "unknown case");
76 break;
77 }
78 return EDITOR_RESULT_CHANGED;
79 }
80 static void AddAllWeapons(Mission *m)
81 {
82 CA_FOREACH(const WeaponClass, wc, gWeaponClasses.Guns)
83 if (wc->IsRealGun && !HasWeapon(m, wc))
84 {
85 CArrayPushBack(&m->Weapons, &wc);
86 }
87 CA_FOREACH_END()
88 CA_FOREACH(const WeaponClass, wc, gWeaponClasses.CustomGuns)
89 if (wc->IsRealGun && !HasWeapon(m, wc))
90 {
91 CArrayPushBack(&m->Weapons, &wc);
92 }
93 CA_FOREACH_END()
94 }
95
96 typedef struct
97 {
98 Campaign *co;
99 const WeaponClass *Gun;
100 } MissionGunData;
101 static void MissionDrawWeaponStatus(
102 UIObject *o, GraphicsDevice *g, struct vec2i pos, void *vData)
103 {
104 UNUSED(g);
105 const MissionGunData *data = vData;
106 const Mission *currentMission = CampaignGetCurrentMission(data->co);
107 if (currentMission == NULL)
108 return;
109 const bool hasWeapon = HasWeapon(currentMission, data->Gun);
110 DisplayFlag(
111 svec2i_add(pos, o->Pos), data->Gun->name, hasWeapon,
112 UIObjectIsHighlighted(o));
113 }
114 static EditorResult MissionChangeWeapon(void *vData, int d)
115 {
116 UNUSED(d);
117 MissionGunData *data = vData;
118 bool hasWeapon = false;
119 int weaponIndex = -1;
120 Mission *currentMission = CampaignGetCurrentMission(data->co);
121 CA_FOREACH(const WeaponClass *, wc, currentMission->Weapons)
122 if (data->Gun == *wc)
123 {
124 hasWeapon = true;
125 weaponIndex = _ca_index;
126 break;
127 }
128 CA_FOREACH_END()
129 if (hasWeapon)
130 {
131 CArrayDelete(&currentMission->Weapons, weaponIndex);
132 }
133 else
134 {
135 CArrayPushBack(&currentMission->Weapons, &data->Gun);
136 }
137 return EDITOR_RESULT_CHANGED;
138 }
139 // Check if the current mission has this gun available already
140 static bool HasWeapon(const Mission *m, const WeaponClass *wc)
141 {
142 CA_FOREACH(const WeaponClass *, wc2, m->Weapons)
143 if (wc == *wc2)
144 {
145 return true;
146 }
147 CA_FOREACH_END()
148 return false;
149 }
150
151 #define HEADER_COLUMN_WIDTH 75
152 #define COLUMN_WIDTH 90
153
154 static void CreateWeaponSpecialToggleObj(
155 Campaign *co, UIObject *c, const struct vec2i pos,
156 const SelectMode mode, char *label);
157 static void CreateWeaponToggleObjs(
158 Campaign *co, UIObject *c, const UIObject *o, int *idx,
159 const int rows, CArray *guns);
160 UIObject *CreateWeaponObjs(Campaign *co)
161 {
162 const int th = FontH();
163 UIObject *c =
164 UIObjectCreate(UITYPE_CONTEXT_MENU, 0, svec2i_zero(), svec2i_zero());
165 c->Flags = UI_ENABLED_WHEN_PARENT_HIGHLIGHTED_ONLY;
166
167 // Add special toggle controls
168 CreateWeaponSpecialToggleObj(
169 co, c, svec2i_zero(), SELECT_ALL, "Select All");
170 CreateWeaponSpecialToggleObj(
171 co, c, svec2i(HEADER_COLUMN_WIDTH, 0), SELECT_NONE, "Select None");
172 CreateWeaponSpecialToggleObj(
173 co, c, svec2i(HEADER_COLUMN_WIDTH * 2, 0), SELECT_INVERSE, "Invert");
174
175 // Create a dummy label that can be clicked to close the context menu
176 CreateCloseLabel(c, svec2i(HEADER_COLUMN_WIDTH * 3, 0));
177
178 // Add a toggle entry for each gun
179 UIObject *o =
180 UIObjectCreate(UITYPE_CUSTOM, 0, svec2i_zero(), svec2i(80, th));
181 o->u.CustomDrawFunc = MissionDrawWeaponStatus;
182 o->ChangeFunc = MissionChangeWeapon;
183 o->ChangeDisablesContext = false;
184 o->Flags = UI_LEAVE_YC;
185 const int rows = 10;
186 int idx = 0;
187 CreateWeaponToggleObjs(co, c, o, &idx, rows, &gWeaponClasses.Guns);
188 CreateWeaponToggleObjs(co, c, o, &idx, rows, &gWeaponClasses.CustomGuns);
189
190 UIObjectDestroy(o);
191 return c;
192 }
193 static void CreateWeaponSpecialToggleObj(
194 Campaign *co, UIObject *c, const struct vec2i pos,
195 const SelectMode mode, char *label)
196 {
197 const int th = FontH();
198 UIObject *o = UIObjectCreate(
199 UITYPE_LABEL, 0, svec2i_zero(), svec2i(HEADER_COLUMN_WIDTH - 10, th));
200 o->Label = label;
201 o->ChangeFunc = MissionSelectWeapons;
202 o->ChangeDisablesContext = false;
203 CMALLOC(o->Data, sizeof(MissionAllGunsData));
204 o->IsDynamicData = true;
205 ((MissionAllGunsData *)o->Data)->co = co;
206 ((MissionAllGunsData *)o->Data)->Mode = mode;
207 o->Pos = pos;
208 UIObjectAddChild(c, o);
209 }
210 static void CreateWeaponToggleObjs(
211 Campaign *co, UIObject *c, const UIObject *o, int *idx,
212 const int rows, CArray *guns)
213 {
214 const int th = FontH();
215 CA_FOREACH(const WeaponClass, wc, *guns)
216 if (!wc->IsRealGun)
217 {
218 continue;
219 }
220 UIObject *o2 = UIObjectCopy(o);
221 CMALLOC(o2->Data, sizeof(MissionGunData));
222 o2->IsDynamicData = true;
223 ((MissionGunData *)o2->Data)->co = co;
224 ((MissionGunData *)o2->Data)->Gun = wc;
225 o2->Pos = svec2i(*idx / rows * COLUMN_WIDTH, (*idx % rows) * th + th * 2);
226 UIObjectAddChild(c, o2);
227 (*idx)++;
228 CA_FOREACH_END()
229 }