Debian Codegraph @

src/cdogsed/mission_options.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (c) 2020-2021 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 "mission_options.h"
29
30 #include "nk_window.h"
31
32 #define WIDTH 800
33 #define HEIGHT 200
34 #define ROW_HEIGHT 25
35
36 typedef struct
37 {
38 Mission *m;
39 char Title[256];
40 char Description[1024];
41 char MusicFilename[256];
42 EditorResult result;
43 } MissionOptionsData;
44
45 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data);
46 static void CheckTextChanged(
47 char **dst, const char *src, EditorResult *result);
48 EditorResult EditMissionOptions(EventHandlers *handlers, Mission *m)
49 {
50 NKWindowConfig cfg;
51 memset(&cfg, 0, sizeof cfg);
52 cfg.Title = "Mission Options";
53 cfg.Size = svec2i(WIDTH, HEIGHT);
54 color_t bg = {41, 26, 26, 255};
55 cfg.BG = bg;
56 cfg.Handlers = handlers;
57 cfg.Draw = Draw;
58
59 NKWindowInit(&cfg);
60
61 MissionOptionsData data;
62 memset(&data, 0, sizeof data);
63 data.m = m;
64 if (m->Title)
65 {
66 strcpy(data.Title, m->Title);
67 }
68 if (m->Description)
69 {
70 strcpy(data.Description, m->Description);
71 }
72 if (m->Music.Type == MUSIC_SRC_DYNAMIC && m->Music.Data.Filename)
73 {
74 strcpy(data.MusicFilename, m->Music.Data.Filename);
75 }
76 cfg.DrawData = &data;
77
78 NKWindow(cfg);
79
80 CheckTextChanged(&m->Title, data.Title, &data.result);
81 CheckTextChanged(&m->Description, data.Description, &data.result);
82 if (m->Music.Type == MUSIC_SRC_DYNAMIC)
83 {
84 CheckTextChanged(&m->Music.Data.Filename, data.MusicFilename, &data.result);
85 }
86 else
87 {
88 CFREE(m->Music.Data.Filename);
89 CSTRDUP(m->Music.Data.Filename, "");
90 }
91 return data.result;
92 }
93 static void CheckTextChanged(char **dst, const char *src, EditorResult *result)
94 {
95 if (*dst == NULL || strcmp(src, *dst) != 0)
96 {
97 CFREE(*dst);
98 CSTRDUP(*dst, src);
99 *result = EDITOR_RESULT_CHANGED;
100 }
101 }
102
103 static bool Draw(SDL_Window *win, struct nk_context *ctx, void *data)
104 {
105 UNUSED(win);
106 bool changed = false;
107 MissionOptionsData *mData = data;
108
109 if (nk_begin(
110 ctx, "", nk_rect(0, 0, WIDTH, HEIGHT),
111 NK_WINDOW_BORDER))
112 {
113 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
114 DrawTextbox(ctx, mData->Title, 256, "Title", NK_EDIT_FIELD);
115 nk_layout_row_dynamic(ctx, ROW_HEIGHT * 3, 1);
116 DrawTextbox(ctx, mData->Description, 1024, "Description", NK_EDIT_BOX);
117 nk_layout_row_dynamic(ctx, ROW_HEIGHT, 1);
118 bool dynamicMusic = mData->m->Music.Type == MUSIC_SRC_DYNAMIC;
119 if (DrawCheckbox(ctx, "Custom Music", NULL, &dynamicMusic))
120 {
121 changed = true;
122 mData->m->Music.Type = dynamicMusic ? MUSIC_SRC_DYNAMIC : MUSIC_SRC_GENERAL;
123 }
124 if (dynamicMusic)
125 {
126 DrawTextbox(ctx, mData->MusicFilename, 256, "Music Filename", NK_EDIT_FIELD);
127 }
128 if (DrawCheckbox(
129 ctx, "Persist weapons & ammo",
130 "Whether weapons picked up and extra ammo are "
131 "available in this mission",
132 &mData->m->WeaponPersist))
133 {
134 changed = true;
135 }
136 if (DrawCheckbox(
137 ctx, "Skip debrief",
138 "Skip the end-of-mission summary screen",
139 &mData->m->SkipDebrief))
140 {
141 changed = true;
142 }
143 nk_end(ctx);
144 }
145 if (changed)
146 {
147 mData->result = EDITOR_RESULT_CHANGED;
148 }
149 return true;
150 }