1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
/* Project Magic 2.0
Destroyer Studios LLC
Copyright © 1997-2004. All Rights Reserved.
SUBSYSTEM: Magic.exe
FILE: UVMapView.cpp
AUTHOR: John DiCamillo
OVERVIEW
========
Implementation of the UVMapView class
*/
#include "stdafx.h"
#include "Magic.h"
#include "MagicDoc.h"
#include "UVMapView.h"
#include "Selector.h"
#include "Selection.h"
#include "ActiveWindow.h"
#include "Color.h"
#include "Screen.h"
#include "Video.h"
DWORD GetRealTime();
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// +--------------------------------------------------------------------+
UVMapView::UVMapView(Window* c)
: View(c), material(0), zoom(1), x_offset(0), y_offset(0),
nmarks(0), select_mode(SELECT_REPLACE), active(false)
{
}
UVMapView::~UVMapView()
{
}
// +--------------------------------------------------------------------+
const int BATCH_SIZE = 64;
void
UVMapView::Refresh()
{
video = Video::GetInstance();
if (!video)
return;
window->FillRect(window->GetRect(), Color::LightGray);
if (material && material->tex_diffuse) {
Bitmap* bmp = material->tex_diffuse;
int w = bmp->Width();
int h = bmp->Height();
double cx = window->Width() / 2 + x_offset;
double cy = window->Height() / 2 + y_offset;
int x1 = (int) (cx - (zoom * w/2));
int x2 = (int) (cx + (zoom * w/2));
int y1 = (int) (cy - (zoom * h/2));
int y2 = (int) (cy + (zoom * h/2));
window->DrawBitmap(x1, y1, x2, y2, bmp);
ListIter<Poly> iter = polys;
while (++iter) {
Poly* p = iter.value();
VertexSet* vset = p->vertex_set;
if (p->material != material)
continue;
for (int i = 0; i < p->nverts; i++) {
int n1 = p->verts[i];
int n2 = p->verts[0];
if (i < p->nverts-1)
n2 = p->verts[i+1];
double tu1 = vset->tu[n1];
double tv1 = vset->tv[n1];
double tu2 = vset->tu[n2];
double tv2 = vset->tv[n2];
x1 = (int) (cx + zoom * w * (tu1-0.5));
x2 = (int) (cx + zoom * w * (tu2-0.5));
y1 = (int) (cy + zoom * h * (tv1-0.5));
y2 = (int) (cy + zoom * h * (tv2-0.5));
window->DrawLine(x1, y1, x2, y2, Color::Yellow);
if (IsSelected(p, i))
window->FillRect(x1-3, y1-3, x1+3, y1+3, Color::Yellow);
else
window->DrawRect(x1-2, y1-2, x1+2, y1+2, Color::Yellow);
}
}
}
if (active && nmarks > 1) {
float points[4*BATCH_SIZE + 4];
for (int batch = 0; batch < nmarks; batch += BATCH_SIZE) {
int end = batch + BATCH_SIZE;
if (end > nmarks-1)
end = nmarks-1;
int nlines = end-batch;
for (int i = 0; i < nlines; i++) {
points[4*i+0] = (float) marks[batch + i].x;
points[4*i+1] = (float) marks[batch + i].y;
points[4*i+2] = (float) marks[batch + i + 1].x;
points[4*i+3] = (float) marks[batch + i + 1].y;
}
video->DrawScreenLines(nlines, points, Color::Cyan);
}
}
const char* title = "UV Editor";
int len = strlen(title);
Rect r(6,4,200,20);
r.x += window->GetRect().x;
r.y += window->GetRect().y;
video->DrawText(title, len, r, DT_LEFT|DT_SINGLELINE, Color::Black);
r.x--;
r.y--;
video->DrawText(title, len, r, DT_LEFT|DT_SINGLELINE, Color::White);
}
// +--------------------------------------------------------------------+
void
UVMapView::UseMaterial(Material* m)
{
if (material != m) {
material = m;
zoom = 1;
x_offset = 0;
y_offset = 0;
}
}
void
UVMapView::UsePolys(List<Poly>& p)
{
polys.clear();
polys.append(p);
}
void
UVMapView::MoveBy(double dx, double dy)
{
x_offset += dx;
y_offset += dy;
}
void
UVMapView::DragBy(double dx, double dy)
{
if (!material || !material->tex_diffuse || selverts.size() < 1)
return;
Bitmap* bmp = material->tex_diffuse;
double w = zoom * bmp->Width();
double h = zoom * bmp->Height();
float du = (float) (dx/w);
float dv = (float) (dy/h);
for (auto svi = selverts.begin(); svi != selverts.end(); ++svi) {
DWORD value = *svi;
DWORD p = value >> 16;
DWORD n = value & 0xffff;
Poly* poly = polys[p];
if (poly && n < poly->nverts) {
VertexSet* vset = poly->vertex_set;
int v = poly->verts[n];
vset->tu[v] += du;
vset->tv[v] += dv;
}
}
}
// +----------------------------------------------------------------------+
void
UVMapView::Clear()
{
selverts.clear();
}
void
UVMapView::Begin(int seln_mode)
{
nmarks = 0;
select_mode = seln_mode;
active = true;
}
void
UVMapView::AddMark(CPoint& p)
{
if (nmarks < MAX_MARK)
marks[nmarks++] = p;
}
void
UVMapView::End()
{
active = false;
// get the model:
if (!nmarks || !material || !material->tex_diffuse) return;
// if not adding to selection:
if (select_mode == SELECT_REPLACE) {
Clear();
}
Bitmap* bmp = material->tex_diffuse;
int w = bmp->Width();
int h = bmp->Height();
double cx = window->Width() / 2 + x_offset;
double cy = window->Height() / 2 + y_offset;
// if only one mark:
if (nmarks < 2) {
// find all selected verts:
ListIter<Poly> iter = polys;
while (++iter) {
Poly* p = iter.value();
VertexSet* vset = p->vertex_set;
for (int i = 0; i < p->nverts; i++) {
int n1 = p->verts[i];
double tu1 = vset->tu[n1];
double tv1 = vset->tv[n1];
int x1 = (int) (cx + zoom * w * (tu1-0.5));
int y1 = (int) (cy + zoom * h * (tv1-0.5));
int dx = abs(marks[0].x - x1);
int dy = abs(marks[0].y - y1);
if (dx < 4 && dy < 4) {
WORD p_index = iter.index();
DWORD value = (p_index << 16) | i;
if (select_mode == SELECT_REMOVE) {
for (auto svi = selverts.begin(); svi != selverts.end(); ++svi) {
if (*svi == value) {
selverts.erase(svi);
}
}
}
else {
bool contains = false;
for (auto svi = selverts.begin(); svi != selverts.end(); ++svi) {
if (*svi == value) {
contains = true;
}
}
if (!contains)
selverts.push_back(value);
}
}
}
}
}
// otherwise, build a region:
else {
CRgn rgn;
rgn.CreatePolygonRgn(marks, nmarks, WINDING);
// find all selected verts:
ListIter<Poly> iter = polys;
while (++iter) {
Poly* p = iter.value();
VertexSet* vset = p->vertex_set;
for (int i = 0; i < p->nverts; i++) {
int n1 = p->verts[i];
double tu1 = vset->tu[n1];
double tv1 = vset->tv[n1];
int x1 = (int) (cx + zoom * w * (tu1-0.5));
int y1 = (int) (cy + zoom * h * (tv1-0.5));
CPoint p(x1,y1);
if (rgn.PtInRegion(p)) {
WORD p_index = iter.index();
DWORD value = (p_index << 16) | i;
if (select_mode == SELECT_REMOVE) {
for (auto svi = selverts.begin(); svi != selverts.end(); ++svi) {
if (*svi == value)
selverts.erase(svi);
}
}
else {
bool contains = false;
for (auto svi = selverts.begin(); svi != selverts.end(); ++svi) {
if (*svi == value)
contains = true;
}
if (!contains)
selverts.push_back(value);
}
}
}
}
}
nmarks = 0;
}
// +----------------------------------------------------------------------+
void
UVMapView::SelectAll()
{
selverts.clear();
ListIter<Poly> iter = polys;
while (++iter) {
Poly* p = iter.value();
if (p->material != material)
continue;
for (int i = 0; i < p->nverts; i++) {
WORD p_index = iter.index();
DWORD value = (p_index << 16) | i;
selverts.push_back(value);
}
}
}
void
UVMapView::SelectNone()
{
selverts.clear();
}
void
UVMapView::SelectInverse()
{
ListIter<Poly> iter = polys;
while (++iter) {
Poly* p = iter.value();
if (p->material != material)
continue;
for (int i = 0; i < p->nverts; i++) {
WORD p_index = iter.index();
DWORD value = (p_index << 16) | i;
bool contains = false;
auto svi = selverts.begin();
for (; svi != selverts.end(); ++svi) {
if (*svi == value) {
contains = true;
break;
}
}
if (contains)
selverts.erase(svi);
else
selverts.push_back(value);
}
}
}
bool
UVMapView::IsSelected(Poly* poly, WORD v)
{
WORD p = polys.index(poly);
DWORD value = (p << 16) | v;
bool contains = false;
for (auto svi = selverts.begin(); svi != selverts.end(); ++svi) {
if (*svi == value)
return true;
}
return false;
}
bool
UVMapView::WillSelect(CPoint& p)
{
if (!material || !material->tex_diffuse)
return false;
Bitmap* bmp = material->tex_diffuse;
int w = bmp->Width();
int h = bmp->Height();
double cx = window->Width() / 2 + x_offset;
double cy = window->Height() / 2 + y_offset;
// find first selected vert:
ListIter<Poly> iter = polys;
while (++iter) {
Poly* poly = iter.value();
VertexSet* vset = poly->vertex_set;
for (int i = 0; i < poly->nverts; i++) {
int n1 = poly->verts[i];
double tu1 = vset->tu[n1];
double tv1 = vset->tv[n1];
int x1 = (int) (cx + zoom * w * (tu1-0.5));
int y1 = (int) (cy + zoom * h * (tv1-0.5));
int dx = abs(p.x - x1);
int dy = abs(p.y - y1);
if (dx < 4 && dy < 4) {
return true;
}
}
}
return false;
}
|