Debian Codegraph @

similar/editor/texpage.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Routines for displaying texture pages
23 *
24 */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30
31 #include "inferno.h"
32 #include "gameseg.h"
33 #include "editor.h" // For TMAP_CURBOX??????
34 #include "gr.h" // For canves, font stuff
35 #include "ui.h" // For UI_GADGET stuff
36 #include "textures.h" // For NumTextures
37 #include "dxxerror.h"
38 #include "key.h"
39 #include "event.h"
40 #include "gamesave.h"
41 #include "mission.h"
42
43 #include "texpage.h"
44 #include "piggy.h"
45
46 #include "compiler-range_for.h"
47
48 constexpr std::integral_constant<unsigned, 12> TMAPS_PER_PAGE{};
49
50 static std::array<std::unique_ptr<UI_GADGET_USERBOX>, TMAPS_PER_PAGE> TmapBox;
51 static std::unique_ptr<UI_GADGET_USERBOX> TmapCurrent;
52
53 texture_index CurrentTexture; // Used globally
54
55 #if DXX_BUILD_DESCENT == 1
56 #define DXX_TEXTURE_INITIALIZER(D1, D2) D1
57 #elif DXX_BUILD_DESCENT == 2
58 #define DXX_TEXTURE_INITIALIZER(D1, D2) D2
59 #endif
60
61 int TextureLights = DXX_TEXTURE_INITIALIZER(263, 275);
62 int TextureEffects = DXX_TEXTURE_INITIALIZER(327, 308);
63 int TextureMetals = DXX_TEXTURE_INITIALIZER(156, 202);
64
65 namespace {
66
67 class texture_dialog
68 {
69 public:
70 std::unique_ptr<UI_GADGET_BUTTON> prev_texture, next_texture, first_texture, metal_texture, light_texture, effects_texture;
71 };
72
73 }
74
75 static texture_dialog texpage_dialog;
76
77 static int TexturePage = 0;
78
79 static grs_subcanvas_ptr TmapnameCanvas;
80 static void texpage_print_name(d_fname name)
81 {
82 name.back() = 0;
83
84 gr_set_current_canvas( TmapnameCanvas );
85 gr_string(*grd_curcanv, *grd_curcanv->cv_font, 0, 0, name);
86 }
87
88 //Redraw the list of textures, based on TexturePage
89 static void texpage_redraw()
90 {
91 for (int i = 0; i < TMAPS_PER_PAGE; i++)
92 {
93 gr_set_current_canvas(TmapBox[i]->canvas);
94 if (i + TexturePage*TMAPS_PER_PAGE < NumTextures)
95 {
96 PIGGY_PAGE_IN(Textures[i + TexturePage*TMAPS_PER_PAGE]);
97 gr_ubitmap(*grd_curcanv, GameBitmaps[Textures[i + TexturePage * TMAPS_PER_PAGE]]);
98 } else
99 gr_clear_canvas(*grd_curcanv, CGREY);
100 }
101 }
102
103 //shows the current texture, updating the window and printing the name, base
104 //on CurrentTexture
105 static void texpage_show_current()
106 {
107 auto &TmapInfo = LevelUniqueTmapInfoState.TmapInfo;
108 gr_set_current_canvas(TmapCurrent->canvas);
109 const auto ct{CurrentTexture};
110 auto &t = Textures[ct];
111 PIGGY_PAGE_IN(t);
112 gr_ubitmap(*grd_curcanv, GameBitmaps[t]);
113 texpage_print_name(TmapInfo[ct].filename);
114 }
115
116 int texpage_goto_first()
117 {
118 TexturePage=0;
119 texpage_redraw();
120 return 1;
121 }
122
123 static int texpage_goto_metals()
124 {
125
126 TexturePage=TextureMetals/TMAPS_PER_PAGE;
127 texpage_redraw();
128 return 1;
129 }
130
131
132 // Goto lights (paste ons)
133 static int texpage_goto_lights()
134 {
135 TexturePage=TextureLights/TMAPS_PER_PAGE;
136 texpage_redraw();
137 return 1;
138 }
139
140 static int texpage_goto_effects()
141 {
142 TexturePage=TextureEffects/TMAPS_PER_PAGE;
143 texpage_redraw();
144 return 1;
145 }
146
147 static int texpage_goto_prev()
148 {
149 if (TexturePage > 0) {
150 TexturePage--;
151 texpage_redraw();
152 }
153 return 1;
154 }
155
156 static int texpage_goto_next()
157 {
158 if ((TexturePage + 1)*TMAPS_PER_PAGE < NumTextures)
159 {
160 TexturePage++;
161 texpage_redraw();
162 }
163 return 1;
164 }
165
166 //NOTE: this code takes the texture map number, not this index in the
167 //list of available textures. There are different if there are holes in
168 //the list
169 int texpage_grab_current(texture1_value n)
170 {
171 CurrentTexture = get_texture_index(n);
172
173 TexturePage = CurrentTexture / TMAPS_PER_PAGE;
174
175 if (TexturePage*TMAPS_PER_PAGE < NumTextures)
176 texpage_redraw();
177
178 texpage_show_current();
179
180 return 1;
181 }
182
183
184 // INIT TEXTURE STUFF
185
186 void texpage_init( UI_DIALOG * dlg )
187 {
188 auto &t = texpage_dialog;
189 t.prev_texture = ui_add_gadget_button(*dlg, TMAPCURBOX_X + 00, TMAPCURBOX_Y - 24, 30, 20, "<<", texpage_goto_prev);
190 t.next_texture = ui_add_gadget_button(*dlg, TMAPCURBOX_X + 32, TMAPCURBOX_Y - 24, 30, 20, ">>", texpage_goto_next);
191
192 t.first_texture = ui_add_gadget_button(*dlg, TMAPCURBOX_X + 00, TMAPCURBOX_Y - 48, 15, 20, "T", texpage_goto_first);
193 t.metal_texture = ui_add_gadget_button(*dlg, TMAPCURBOX_X + 17, TMAPCURBOX_Y - 48, 15, 20, "M", texpage_goto_metals);
194 t.light_texture = ui_add_gadget_button(*dlg, TMAPCURBOX_X + 34, TMAPCURBOX_Y - 48, 15, 20, "L", texpage_goto_lights);
195 t.effects_texture = ui_add_gadget_button(*dlg, TMAPCURBOX_X + 51, TMAPCURBOX_Y - 48, 15, 20, "E", texpage_goto_effects);
196
197 for (int i=0;i<TMAPS_PER_PAGE;i++)
198 TmapBox[i] = ui_add_gadget_userbox(*dlg, TMAPBOX_X + (i / 3) * (2 + TMAPBOX_W), TMAPBOX_Y + (i % 3) * (2 + TMAPBOX_H), TMAPBOX_W, TMAPBOX_H);
199
200 TmapCurrent = ui_add_gadget_userbox(*dlg, TMAPCURBOX_X, TMAPCURBOX_Y, 64, 64);
201
202 TmapnameCanvas = gr_create_sub_canvas(grd_curscreen->sc_canvas, TMAPCURBOX_X , TMAPCURBOX_Y + TMAPBOX_H + 10, 100, 20);
203 }
204
205 void texpage_close()
206 {
207 TmapnameCanvas.reset();
208 }
209
210
211 // DO TEXTURE STUFF
212
213 #define MAX_REPLACEMENTS 32
214
215 namespace {
216
217 struct replacement
218 {
219 texture_index n;
220 texture_index old;
221 };
222
223 int Num_replacements{0};
224 static std::array<replacement, MAX_REPLACEMENTS> Replacement_list;
225
226 }
227
228 int texpage_do(const d_event &event)
229 {
230 if (event.type == event_type::ui_dialog_draw)
231 {
232 gr_set_current_canvas( TmapnameCanvas );
233 gr_set_curfont(*grd_curcanv, *ui_small_font.get());
234 gr_set_fontcolor(*grd_curcanv, CBLACK, CWHITE);
235 texpage_redraw();
236 // Don't reset the current tmap every time we go back to the editor.
237 // CurrentTexture = TexturePage*TMAPS_PER_PAGE;
238 texpage_show_current();
239 return 1;
240 }
241
242 for (int i=0; i<TMAPS_PER_PAGE; i++ ) {
243 if (GADGET_PRESSED(TmapBox[i].get()) && (i + TexturePage*TMAPS_PER_PAGE < NumTextures))
244 {
245 CurrentTexture = i + TexturePage*TMAPS_PER_PAGE;
246 texpage_show_current();
247
248 if (keyd_pressed[KEY_LSHIFT]) {
249 Replacement_list[Num_replacements].old = CurrentTexture;
250 }
251
252 if (keyd_pressed[KEY_LCTRL]) {
253 Replacement_list[Num_replacements].n = CurrentTexture;
254 Num_replacements++;
255 }
256
257 return 1;
258 }
259 }
260
261 return 0;
262 }
263
264 void init_replacements(void)
265 {
266 Num_replacements = 0;
267 }
268
269 void do_replacements(void)
270 {
271 med_compress_mine();
272
273 for (int replnum=0; replnum<Num_replacements; replnum++) {
274 const auto old_tmap_num = Replacement_list[replnum].old;
275 const auto new_tmap_num = Replacement_list[replnum].n;
276
277 range_for (unique_segment &segp, vmsegptr)
278 {
279 range_for (auto &sidep, segp.sides)
280 {
281 if (get_texture_index(sidep.tmap_num) == old_tmap_num) {
282 sidep.tmap_num = build_texture1_value(new_tmap_num);
283 }
284 if (sidep.tmap_num2 != texture2_value::None && get_texture_index(sidep.tmap_num2) == old_tmap_num)
285 {
286 if (new_tmap_num == 0) {
287 Int3(); // Error. You have tried to replace a tmap_num2 with
288 // the 0th tmap_num2 which is ILLEGAL!
289 } else {
290 sidep.tmap_num2 = build_texture2_value(new_tmap_num, get_texture_rotation_high(sidep.tmap_num2));
291 }
292 }
293 }
294 }
295 }
296
297 }
298
299 void do_replacements_all(void)
300 {
301 for (int i = 0; i < Current_mission->last_level; ++i)
302 {
303 load_level(
304 #if DXX_BUILD_DESCENT == 2
305 LevelSharedSegmentState.DestructibleLights,
306 #endif
307 Current_mission->level_names[i]);
308 do_replacements();
309 save_level(
310 #if DXX_BUILD_DESCENT == 2
311 LevelSharedSegmentState.DestructibleLights,
312 #endif
313 Current_mission->level_names[i]);
314 }
315
316 for (int i = 0; i < -Current_mission->last_secret_level; ++i)
317 {
318 load_level(
319 #if DXX_BUILD_DESCENT == 2
320 LevelSharedSegmentState.DestructibleLights,
321 #endif
322 Current_mission->secret_level_names[i]);
323 do_replacements();
324 save_level(
325 #if DXX_BUILD_DESCENT == 2
326 LevelSharedSegmentState.DestructibleLights,
327 #endif
328 Current_mission->secret_level_names[i]);
329 }
330
331 }
332