Debian Codegraph @

editor/WaterProceduralDialog.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // WaterProceduralDialog.cpp : implementation file
20 //
21
22 #include "mfc_compatibility.h"
23 #include "editor.h"
24 #include "game.h"
25 #include "manage.h"
26 #include "texpage.h"
27
28 #include "WaterProceduralDialog.h"
29 // #include "procedurals.h"
30
31 #ifdef _DEBUG
32 #define new DEBUG_NEW
33 #undef THIS_FILE
34 static char THIS_FILE[] = __FILE__;
35 #endif
36
37 /////////////////////////////////////////////////////////////////////////////
38 // CWaterProceduralDialog dialog
39
40 #define MAX_SLIDER_RANGE 255
41
42 CWaterProceduralDialog::CWaterProceduralDialog(CWnd *pParent /*=NULL*/)
43 : CDialog(CWaterProceduralDialog::IDD, pParent) {
44 //{{AFX_DATA_INIT(CWaterProceduralDialog)
45 // NOTE: the ClassWizard will add member initialization here
46 //}}AFX_DATA_INIT
47 }
48
49 void CWaterProceduralDialog::DoDataExchange(CDataExchange *pDX) {
50 CDialog::DoDataExchange(pDX);
51 //{{AFX_DATA_MAP(CWaterProceduralDialog)
52 DDX_Control(pDX, IDC_WP_LIGHT_SLIDER, m_light_slider);
53 DDX_Control(pDX, IDC_WP_THICKNESS_SLIDER, m_thickness_slider);
54 DDX_Control(pDX, IDC_WP_SIZE_SLIDER, m_size_slider);
55 DDX_Control(pDX, IDC_WP_HEIGHT_SLIDER, m_height_slider);
56 DDX_Control(pDX, IDC_WP_FREQUENCY_SLIDER, m_frequency_slider);
57 //}}AFX_DATA_MAP
58 }
59
60 BEGIN_MESSAGE_MAP(CWaterProceduralDialog, CDialog)
61 //{{AFX_MSG_MAP(CWaterProceduralDialog)
62 ON_CBN_SELENDOK(IDC_PROCEDURAL_PULLDOWN, OnSelendokProceduralPulldown)
63 ON_WM_TIMER()
64 ON_BN_CLICKED(IDC_CLEAR_PROCEDURALS, OnClearProcedurals)
65 ON_WM_LBUTTONDOWN()
66 ON_WM_LBUTTONUP()
67 ON_WM_MOUSEMOVE()
68 ON_EN_KILLFOCUS(IDC_PROC_EVAL_TIME_EDIT, OnKillfocusProcEvalTimeEdit)
69 ON_EN_KILLFOCUS(IDC_PROC_OSC_TIME_EDIT, OnKillfocusProcOscTimeEdit)
70 ON_EN_KILLFOCUS(IDC_PROC_OSC_VALUE_EDIT, OnKillfocusProcOscValueEdit)
71 ON_BN_CLICKED(IDC_COPY_PROCEDURAL, OnCopyProcedural)
72 //}}AFX_MSG_MAP
73 END_MESSAGE_MAP()
74
75 /////////////////////////////////////////////////////////////////////////////
76 // CWaterProceduralDialog message handlers
77
78 void CWaterProceduralDialog::OnSelendokProceduralPulldown() {
79 int cur = SendDlgItemMessage(IDC_PROCEDURAL_PULLDOWN, CB_GETCURSEL, 0, 0);
80 m_proc_type = cur;
81 UpdateDialog();
82 }
83
84 static static_proc_element save_elements[8000];
85 BOOL CWaterProceduralDialog::DestroyWindow() {
86 // TODO: Add your specialized code here and/or call the base class
87 CWnd::KillTimer(1);
88
89 int n = D3EditState.texdlg_texture;
90
91 int save_num = 0;
92
93 // Restore our static proc elements
94 save_num = GameTextures[n].procedural->num_static_elements;
95
96 if (save_num > 0)
97 memcpy(save_elements, GameTextures[n].procedural->static_proc_elements, save_num * sizeof(static_proc_element));
98
99 FreeStaticProceduralsForTexture(n);
100 Sleep(2000);
101
102 if (save_num > 0) {
103 AllocateStaticProceduralsForTexture(n, save_num);
104 memcpy(GameTextures[n].procedural->static_proc_elements, save_elements, save_num * sizeof(static_proc_element));
105 GameTextures[n].procedural->num_static_elements = save_num;
106 } else {
107 GameTextures[n].flags &= ~(TF_PROCEDURAL | TF_WATER_PROCEDURAL);
108 }
109
110 bm_FreeBitmap(m_window_bitmap);
111 m_TextureSurf.free();
112
113 return CDialog::DestroyWindow();
114 }
115
116 void CWaterProceduralDialog::PrintValue(int whichbox, uint8_t val) {
117 CEdit *ebox;
118 char str[255];
119
120 ebox = (CEdit *)GetDlgItem(whichbox);
121 sprintf(str, "%d", val);
122 ebox->SetWindowText(str);
123 }
124
125 void CWaterProceduralDialog::UpdateDialog() {
126 int n = D3EditState.texdlg_texture;
127
128 CEdit *ebox;
129 char str[255];
130 ebox = (CEdit *)GetDlgItem(IDC_PROC_EVAL_TIME_EDIT);
131 sprintf(str, "%f", GameTextures[n].procedural->evaluation_time);
132 ebox->SetWindowText(str);
133
134 ebox = (CEdit *)GetDlgItem(IDC_PROC_OSC_TIME_EDIT);
135 sprintf(str, "%f", GameTextures[n].procedural->osc_time);
136 ebox->SetWindowText(str);
137
138 ebox = (CEdit *)GetDlgItem(IDC_PROC_OSC_VALUE_EDIT);
139 sprintf(str, "%d", GameTextures[n].procedural->osc_value);
140 ebox->SetWindowText(str);
141
142 // Update procedural box
143 SendDlgItemMessage(IDC_PROCEDURAL_PULLDOWN, CB_RESETCONTENT, 0, 0);
144 for (int i = 0; stricmp("End", WaterProcNames[i]) != 0; i++) {
145 SendDlgItemMessage(IDC_PROCEDURAL_PULLDOWN, CB_ADDSTRING, 0, (LPARAM)(LPCTSTR)WaterProcNames[i]);
146 }
147 SendDlgItemMessage(IDC_PROCEDURAL_PULLDOWN, CB_SELECTSTRING, 0, (LPARAM)(LPCTSTR)WaterProcNames[m_proc_type]);
148
149 // fill in the combo box for copy procedural data
150 CComboBox *combo;
151 combo = (CComboBox *)GetDlgItem(IDC_PROC_COPY_LIST);
152 combo->ResetContent();
153 for (i = 0; i < MAX_TEXTURES; i++) {
154 if (!GameTextures[i].used)
155 continue;
156 if (i == D3EditState.texdlg_texture)
157 continue;
158 if (!(GameTextures[i].flags & TF_PROCEDURAL))
159 continue;
160 if (!(GameTextures[i].flags & TF_WATER_PROCEDURAL))
161 continue;
162 combo->AddString(GameTextures[i].name);
163 }
164 combo->SetCurSel(0);
165 }
166
167 void CWaterProceduralDialog::UpdateProcView() {
168 int n = D3EditState.texdlg_texture;
169
170 FrameCount++;
171
172 CWnd *texwnd;
173 RECT rect;
174
175 texwnd = GetDlgItem(IDC_WATER_PROCEDURAL_VIEW);
176 texwnd->GetWindowRect(&rect);
177 ScreenToClient(&rect);
178
179 int w = rect.right - rect.left;
180 int h = rect.bottom - rect.top;
181
182 Desktop_surf->attach_to_window((unsigned)m_hWnd);
183
184 int bm_handle = GetTextureBitmap(n, 0, 1);
185
186 if (GameBitmaps[bm_handle].flags & BF_MIPMAPPED)
187 GameBitmaps[m_window_bitmap].flags |= BF_MIPMAPPED;
188
189 bm_ScaleBitmapToBitmap(m_window_bitmap, bm_handle);
190
191 m_TextureSurf.load(m_window_bitmap);
192
193 int x = rect.left + ((rect.right - rect.left) / 2) - m_TextureSurf.width() / 2;
194 int y = rect.top + ((rect.bottom - rect.top) / 2) - m_TextureSurf.height() / 2;
195
196 Desktop_surf->blt(x, y, &m_TextureSurf);
197
198 // Do slider values
199 m_frequency = 255 - m_frequency_slider.GetPos();
200 m_height = m_height_slider.GetPos();
201 m_size = m_size_slider.GetPos();
202
203 GameTextures[n].procedural->light = m_light_slider.GetPos();
204 GameTextures[n].procedural->thickness = m_thickness_slider.GetPos();
205
206 PrintValue(IDC_SIZE_TEXT, m_size);
207 PrintValue(IDC_HEIGHT_TEXT, m_height);
208 PrintValue(IDC_FREQ_TEXT, 255 - m_frequency);
209 PrintValue(IDC_LIGHT_TEXT, GameTextures[n].procedural->light);
210 PrintValue(IDC_THICKNESS_TEXT, GameTextures[n].procedural->thickness);
211 }
212
213 void CWaterProceduralDialog::OnTimer(UINT nIDEvent) {
214 // TODO: Add your message handler code here and/or call default
215 UpdateProcView();
216
217 CDialog::OnTimer(nIDEvent);
218 }
219
220 BOOL CWaterProceduralDialog::OnInitDialog() {
221 CDialog::OnInitDialog();
222
223 CWnd::SetTimer(1, 33, NULL); // 30 frames per second
224
225 int save_num = 0;
226 int n = D3EditState.texdlg_texture;
227
228 // Make a temp buffer for our static elements
229 if (GameTextures[n].procedural->num_static_elements > 0) {
230 save_num = GameTextures[n].procedural->num_static_elements;
231 memcpy(save_elements, GameTextures[n].procedural->static_proc_elements, save_num * sizeof(static_proc_element));
232 }
233
234 FreeStaticProceduralsForTexture(n);
235 Sleep(2000);
236 AllocateStaticProceduralsForTexture(n, 8000);
237
238 memcpy(GameTextures[n].procedural->static_proc_elements, save_elements, save_num * sizeof(static_proc_element));
239 GameTextures[n].procedural->num_static_elements = save_num;
240
241 m_button_down = 0;
242 m_proc_type = 0;
243 m_size = 1;
244 m_height = 1;
245 m_frequency = 0;
246
247 // Alloc bitmap
248 CWnd *texwnd;
249 RECT rect;
250
251 texwnd = GetDlgItem(IDC_WATER_PROCEDURAL_VIEW);
252 texwnd->GetWindowRect(&rect);
253 ScreenToClient(&rect);
254
255 int w = rect.right - rect.left;
256 int h = rect.bottom - rect.top;
257
258 m_window_bitmap = bm_AllocBitmap(w, h, ((w * h) / 3) * 2);
259 ASSERT(m_window_bitmap >= 0);
260 m_TextureSurf.create(bm_w(m_window_bitmap, 0), bm_h(m_window_bitmap, 0), bm_bpp(m_window_bitmap));
261
262 m_frequency_slider.SetRange(0, MAX_SLIDER_RANGE);
263 m_frequency_slider.SetPos(255 - m_frequency);
264
265 m_height_slider.SetRange(1, MAX_SLIDER_RANGE);
266 m_height_slider.SetPos(m_height);
267
268 m_size_slider.SetRange(1, MAX_SLIDER_RANGE);
269 m_size_slider.SetPos(m_size);
270
271 m_light_slider.SetRange(0, 12);
272 m_light_slider.SetPos(GameTextures[n].procedural->light);
273
274 m_thickness_slider.SetRange(0, 12);
275 m_thickness_slider.SetPos(GameTextures[n].procedural->thickness);
276
277 UpdateDialog();
278
279 // TODO: Add extra initialization here
280
281 return TRUE; // return TRUE unless you set the focus to a control
282 // EXCEPTION: OCX Property Pages should return FALSE
283 }
284
285 void CWaterProceduralDialog::OnClearProcedurals() { ClearAllProceduralsFromTexture(D3EditState.texdlg_texture); }
286
287 void CWaterProceduralDialog::HarvestValue(int whichbox, uint8_t *val) {
288 CEdit *ebox;
289 char name[20];
290 int num = 0;
291
292 ebox = (CEdit *)GetDlgItem(whichbox);
293 ebox->GetWindowText(name, 20);
294
295 num = atoi(name);
296 if (num < 0)
297 num = 0;
298 if (num > 255)
299 num = 255;
300
301 *val = num;
302
303 UpdateDialog();
304 }
305
306 void CWaterProceduralDialog::OnLButtonDown(UINT nFlags, CPoint point) {
307 CPoint tpoint = point;
308 CWnd *texwnd;
309 RECT rect;
310 int n = D3EditState.texdlg_texture;
311
312 texwnd = GetDlgItem(IDC_WATER_PROCEDURAL_VIEW);
313 texwnd->GetWindowRect(&rect);
314
315 int w = rect.right - rect.left;
316 int h = rect.bottom - rect.top;
317
318 MapWindowPoints(texwnd, &tpoint, 1);
319
320 if (tpoint.x < 0)
321 return;
322 if (tpoint.y < 0)
323 return;
324
325 if (tpoint.x >= w)
326 return;
327 if (tpoint.y >= h)
328 return;
329
330 int bm_handle = GameTextures[n].procedural->procedural_bitmap;
331 int bw = bm_w(bm_handle, 0);
332 int bh = bm_h(bm_handle, 0);
333
334 int dest_x = ((float)tpoint.x * (float)((float)bw / (float)w));
335 int dest_y = ((float)tpoint.y * (float)((float)bh / (float)h));
336
337 CreateElement(tpoint.x, tpoint.y);
338
339 m_button_down = 1;
340
341 CDialog::OnLButtonDown(nFlags, point);
342 }
343
344 void CWaterProceduralDialog::OnLButtonUp(UINT nFlags, CPoint point) {
345 m_button_down = 0;
346 CDialog::OnLButtonUp(nFlags, point);
347 }
348
349 void CWaterProceduralDialog::OnMouseMove(UINT nFlags, CPoint point) {
350 CPoint tpoint = point;
351 CWnd *texwnd;
352 RECT rect;
353 int n = D3EditState.texdlg_texture;
354
355 if (m_button_down == 0)
356 return;
357
358 texwnd = GetDlgItem(IDC_WATER_PROCEDURAL_VIEW);
359 texwnd->GetWindowRect(&rect);
360
361 int w = rect.right - rect.left;
362 int h = rect.bottom - rect.top;
363
364 MapWindowPoints(texwnd, &tpoint, 1);
365
366 if (tpoint.x < 0)
367 return;
368 if (tpoint.y < 0)
369 return;
370
371 if (tpoint.x >= w)
372 return;
373 if (tpoint.y >= h)
374 return;
375
376 if (m_proc_type != PROC_WATER_NONE)
377 CreateElement(tpoint.x, tpoint.y);
378
379 CDialog::OnMouseMove(nFlags, point);
380 }
381
382 // Creates a procedural element at x,y
383 static_proc_element *CWaterProceduralDialog::CreateElement(int x, int y) {
384 int n = D3EditState.texdlg_texture;
385 proc_struct *procedural = GameTextures[n].procedural;
386
387 int index;
388
389 if (GameTextures[n].procedural->num_static_elements >= MAX_PROC_ELEMENTS)
390 return NULL;
391 else
392 index = GameTextures[n].procedural->num_static_elements;
393
394 static_proc_element *proc_elem = &procedural->static_proc_elements[index];
395
396 CWnd *texwnd;
397 RECT rect;
398
399 texwnd = GetDlgItem(IDC_WATER_PROCEDURAL_VIEW);
400 texwnd->GetWindowRect(&rect);
401
402 int w = rect.right - rect.left;
403 int h = rect.bottom - rect.top;
404
405 int bm_handle = GameTextures[n].procedural->procedural_bitmap;
406 int bw = bm_w(bm_handle, 0);
407 int bh = bm_h(bm_handle, 0);
408
409 int dest_x = ((float)x * (float)((float)bw / (float)w));
410 int dest_y = ((float)y * (float)((float)bh / (float)h));
411
412 proc_elem->type = m_proc_type;
413
414 proc_elem->x1 = dest_x;
415 proc_elem->y1 = dest_y;
416 proc_elem->size = m_size;
417 proc_elem->speed = m_height;
418 proc_elem->frequency = m_frequency;
419
420 procedural->num_static_elements++;
421
422 return &procedural->static_proc_elements[index];
423 }
424
425 void CWaterProceduralDialog::OnKillfocusProcEvalTimeEdit() {
426 int n = D3EditState.texdlg_texture;
427
428 CEdit *ebox;
429 char name[20];
430 float num = 0;
431
432 ebox = (CEdit *)GetDlgItem(IDC_PROC_EVAL_TIME_EDIT);
433 ebox->GetWindowText(name, 20);
434
435 num = atof(name);
436 if (num < 0)
437 num = 0;
438 if (num > 255)
439 num = 255;
440
441 GameTextures[n].procedural->evaluation_time = num;
442
443 UpdateDialog();
444 }
445
446 void CWaterProceduralDialog::OnKillfocusProcOscTimeEdit() {
447 int n = D3EditState.texdlg_texture;
448
449 CEdit *ebox;
450 char name[20];
451 float num = 0;
452
453 ebox = (CEdit *)GetDlgItem(IDC_PROC_OSC_TIME_EDIT);
454 ebox->GetWindowText(name, 20);
455
456 num = atof(name);
457 if (num < 0)
458 num = 0;
459 if (num > 255)
460 num = 255;
461
462 GameTextures[n].procedural->osc_time = num;
463
464 UpdateDialog();
465 }
466
467 void CWaterProceduralDialog::OnKillfocusProcOscValueEdit() {
468 int n = D3EditState.texdlg_texture;
469
470 CEdit *ebox;
471 char name[20];
472 int num = 0;
473
474 ebox = (CEdit *)GetDlgItem(IDC_PROC_OSC_VALUE_EDIT);
475 ebox->GetWindowText(name, 20);
476
477 num = atoi(name);
478 if (num < 0)
479 num = 0;
480 if (num > 200)
481 num = 200;
482
483 GameTextures[n].procedural->osc_value = num;
484
485 UpdateDialog();
486 }
487
488 void CWaterProceduralDialog::OnCopyProcedural() {
489 // get the name of the texture
490 CComboBox *combo;
491 CString str;
492 int src_id;
493
494 combo = (CComboBox *)GetDlgItem(IDC_PROC_COPY_LIST);
495 combo->GetLBText(combo->GetCurSel(), str);
496 src_id = FindTextureName(str.GetBuffer(0));
497 ASSERT(src_id != -1);
498
499 bool do_pal = false;
500 char buffer[512];
501 sprintf(buffer, "Are you sure you want to replace the procedural elements of %s with %s?",
502 GameTextures[D3EditState.texdlg_texture].name, GameTextures[src_id].name);
503 if (MessageBox(buffer, "Confirm", MB_YESNO) != IDYES)
504 return;
505 if (MessageBox("Would you like to replace the palette also?", "Palette", MB_YESNO) == IDYES)
506 do_pal = true;
507
508 mngs_texture_page src_page, dest_page;
509 mng_AssignTextureToTexPage(src_id, &src_page);
510 mng_AssignTextureToTexPage(D3EditState.texdlg_texture, &dest_page);
511
512 ClearAllProceduralsFromTexture(D3EditState.texdlg_texture);
513 FreeProceduralForTexture(D3EditState.texdlg_texture);
514
515 int i;
516 if (src_page.tex_struct.flags & TF_WATER_PROCEDURAL)
517 dest_page.tex_struct.flags |= TF_WATER_PROCEDURAL;
518 else
519 dest_page.tex_struct.flags &= ~TF_WATER_PROCEDURAL;
520
521 if (do_pal) {
522 for (i = 0; i < 255; i++) {
523 dest_page.proc_palette[i] = src_page.proc_palette[i];
524 }
525 }
526 dest_page.proc_heat = src_page.proc_heat;
527 dest_page.proc_light = src_page.proc_light;
528 dest_page.proc_thickness = src_page.proc_thickness;
529 dest_page.proc_evaluation_time = src_page.proc_evaluation_time;
530 dest_page.osc_time = src_page.osc_time;
531 dest_page.osc_value = src_page.osc_value;
532 dest_page.num_proc_elements = src_page.num_proc_elements;
533
534 for (i = 0; i < dest_page.num_proc_elements; i++) {
535 dest_page.proc_type[i] = src_page.proc_type[i];
536 dest_page.proc_frequency[i] = src_page.proc_frequency[i];
537 dest_page.proc_speed[i] = src_page.proc_speed[i];
538 dest_page.proc_size[i] = src_page.proc_size[i];
539 dest_page.proc_x1[i] = src_page.proc_x1[i];
540 dest_page.proc_y1[i] = src_page.proc_y1[i];
541 dest_page.proc_x2[i] = src_page.proc_x2[i];
542 dest_page.proc_y2[i] = src_page.proc_y2[i];
543 }
544
545 AllocateProceduralForTexture(D3EditState.texdlg_texture);
546 AllocateStaticProceduralsForTexture(D3EditState.texdlg_texture, dest_page.num_proc_elements);
547 GameTextures[D3EditState.texdlg_texture].procedural->num_static_elements = dest_page.num_proc_elements;
548 GameTextures[D3EditState.texdlg_texture].procedural->heat = dest_page.proc_heat;
549 GameTextures[D3EditState.texdlg_texture].procedural->light = dest_page.proc_light;
550 GameTextures[D3EditState.texdlg_texture].procedural->thickness = dest_page.proc_thickness;
551 GameTextures[D3EditState.texdlg_texture].procedural->evaluation_time = dest_page.proc_evaluation_time;
552 GameTextures[D3EditState.texdlg_texture].procedural->osc_time = dest_page.osc_time;
553 GameTextures[D3EditState.texdlg_texture].procedural->osc_value = dest_page.osc_value;
554
555 // Last 2 colors are the same
556 dest_page.proc_palette[255] = dest_page.proc_palette[254];
557 memcpy(GameTextures[D3EditState.texdlg_texture].procedural->palette, dest_page.proc_palette, 256 * 2);
558
559 for (i = 0; i < dest_page.num_proc_elements; i++) {
560 static_proc_element *proc = &GameTextures[D3EditState.texdlg_texture].procedural->static_proc_elements[i];
561 proc->type = dest_page.proc_type[i];
562 proc->frequency = dest_page.proc_frequency[i];
563 proc->speed = dest_page.proc_speed[i];
564 proc->size = dest_page.proc_size[i];
565 proc->x1 = dest_page.proc_x1[i];
566 proc->y1 = dest_page.proc_y1[i];
567 proc->x2 = dest_page.proc_x2[i];
568 proc->y2 = dest_page.proc_y2[i];
569 }
570
571 UpdateDialog();
572 }