-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWndTerrainTilePalette.cpp
354 lines (305 loc) · 9.6 KB
/
WndTerrainTilePalette.cpp
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
/* Copyright (c) 2002-2012 Croteam Ltd.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as published by
the Free Software Foundation
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
// WndTerrainTilePalette.cpp : implementation file
//
#include "StdH.h"
#include "WorldEditor.h"
#include "WndTerrainTilePalette.h"
#ifdef _DEBUG
#undef new
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define PIX_TILE_WIDTH 64
#define PIX_TILE_HEIGHT 64
/////////////////////////////////////////////////////////////////////////////
// CWndTerrainTilePalette
CWndTerrainTilePalette::CWndTerrainTilePalette()
{
m_ptd=NULL;
m_pDrawPort = NULL;
m_pViewPort = NULL;
m_iTimerID = -1;
}
CWndTerrainTilePalette::~CWndTerrainTilePalette()
{
// free allocated tile info structures
for(INDEX i=0; i<m_dcTileInfo.Count(); i++)
{
delete &m_dcTileInfo[i];
}
m_dcTileInfo.Clear();
if( m_pViewPort != NULL)
{
_pGfx->DestroyWindowCanvas( m_pViewPort);
m_pViewPort = NULL;
}
}
BEGIN_MESSAGE_MAP(CWndTerrainTilePalette, CWnd)
//{{AFX_MSG_MAP(CWndTerrainTilePalette)
ON_WM_PAINT()
ON_WM_KILLFOCUS()
ON_WM_LBUTTONDOWN()
ON_WM_TIMER()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWndTerrainTilePalette message handlers
PIXaabbox2D CWndTerrainTilePalette::GetTileBBox( INDEX iTile)
{
PIXaabbox2D boxScr=PIXaabbox2D(
PIX2D( (iTile%m_ctPaletteTilesH)*PIX_TILE_WIDTH+1,
(iTile/m_ctPaletteTilesH)*PIX_TILE_HEIGHT+1),
PIX2D( (iTile%m_ctPaletteTilesH+1)*PIX_TILE_WIDTH+1,
(iTile/m_ctPaletteTilesH+1)*PIX_TILE_HEIGHT+1) );
// return calculated box
return boxScr;
}
void CWndTerrainTilePalette::OnPaint()
{
CTerrainLayer *ptlLayer=GetLayer();
if(ptlLayer==NULL) return;
{
CPaintDC dc(this); // device context for painting
}
if( m_iTimerID == -1)
{
m_iTimerID = (int) SetTimer( 1, 10, NULL);
}
POINT ptMouse;
GetCursorPos( &ptMouse);
ScreenToClient( &ptMouse);
// if there is a valid drawport, and the drawport can be locked
if( (m_pDrawPort != NULL) && (m_pDrawPort->Lock()) )
{
CWorldEditorView *pWorldEditorView = theApp.GetActiveView();
ASSERT( pWorldEditorView != NULL);
// clear background
m_pDrawPort->Fill( C_BLACK|CT_OPAQUE);
// erase z-buffer
m_pDrawPort->FillZBuffer(ZBUF_BACK);
CTextureObject to;
to.SetData(m_ptd);
PIX pixTexW=m_ptd->GetPixWidth();
PIX pixTexH=m_ptd->GetPixHeight();
PIX pixTileSize=pixTexW/m_ctTilesPerRaw;
PIX pixdpw=m_pDrawPort->GetWidth();
PIX pixdph=m_pDrawPort->GetHeight();
for(INDEX iTile=0; iTile<m_dcTileInfo.Count(); iTile++)
{
CTileInfo &ti=m_dcTileInfo[iTile];
MEXaabbox2D boxTex=MEXaabbox2D(
MEX2D(ti.ti_ix*pixTileSize, ti.ti_iy*pixTileSize),
MEX2D((ti.ti_ix+1)*pixTileSize, (ti.ti_iy+1)*pixTileSize) );
PIXaabbox2D boxScr=GetTileBBox(iTile);
// draw tile
FLOAT fU0=boxTex.Min()(1)/(FLOAT)pixTexW;
FLOAT fV0=boxTex.Min()(2)/(FLOAT)pixTexH;
FLOAT fU1=boxTex.Min()(1)/(FLOAT)pixTexW;
FLOAT fV1=boxTex.Max()(2)/(FLOAT)pixTexH;
FLOAT fU2=boxTex.Max()(1)/(FLOAT)pixTexW;
FLOAT fV2=boxTex.Max()(2)/(FLOAT)pixTexH;
FLOAT fU3=boxTex.Max()(1)/(FLOAT)pixTexW;
FLOAT fV3=boxTex.Min()(2)/(FLOAT)pixTexH;
FLOAT fI0=boxScr.Min()(1);
FLOAT fJ0=boxScr.Min()(2);
FLOAT fI1=boxScr.Min()(1);
FLOAT fJ1=boxScr.Max()(2);
FLOAT fI2=boxScr.Max()(1);
FLOAT fJ2=boxScr.Max()(2);
FLOAT fI3=boxScr.Max()(1);
FLOAT fJ3=boxScr.Min()(2);
if(ti.ti_bFlipX)
{
Swap(fI0,fI3);
Swap(fJ0,fJ3);
Swap(fI1,fI2);
Swap(fJ1,fJ2);
}
if(ti.ti_bFlipY)
{
Swap(fI0,fI1);
Swap(fJ0,fJ1);
Swap(fI2,fI3);
Swap(fJ2,fJ3);
}
if(ti.ti_bSwapXY)
{
Swap(fI1,fI3);
Swap(fJ1,fJ3);
}
m_pDrawPort->InitTexture( &to);
COLOR col=C_WHITE|CT_OPAQUE;
m_pDrawPort->AddTexture(fI0, fJ0, fU0, fV0, col,
fI1, fJ1, fU1, fV1, col,
fI2, fJ2, fU2, fV2, col,
fI3, fJ3, fU3, fV3, col);
m_pDrawPort->FlushRenderingQueue();
// draw border
m_pDrawPort->DrawBorder( boxScr.Min()(1), boxScr.Min()(2), boxScr.Size()(1), boxScr.Size()(2), C_lGRAY|CT_OPAQUE);
}
m_pDrawPort->DrawBorder( 0,0, m_pDrawPort->GetWidth(),m_pDrawPort->GetHeight(), C_vlGRAY|CT_OPAQUE);
// draw selected tile
CTileInfo &ti=m_dcTileInfo[ptlLayer->tl_iSelectedTile];
MEXaabbox2D boxTex=MEXaabbox2D(
MEX2D(ti.ti_ix*pixTileSize, ti.ti_iy*pixTileSize),
MEX2D((ti.ti_ix+1)*pixTileSize, (ti.ti_iy+1)*pixTileSize) );
PIXaabbox2D boxScr=GetTileBBox(ptlLayer->tl_iSelectedTile);
TIME tm=_pTimer->GetRealTimeTick();
FLOAT fFactor=sin(tm*8)/2.0f+0.5f;
COLOR colSelected=LerpColor(C_vlGRAY,C_RED,fFactor);
m_pDrawPort->DrawBorder(boxScr.Min()(1), boxScr.Min()(2),
boxScr.Size()(1), boxScr.Size()(2),
colSelected|CT_OPAQUE);
// draw tile under mouse
PIXaabbox2D boxPoint( PIX2D( ptMouse.x, ptMouse.y), PIX2D(ptMouse.x, ptMouse.y));
for( INDEX itum=0; itum<m_dcTileInfo.Count(); itum++)
{
CTileInfo &ti=m_dcTileInfo[itum];
PIXaabbox2D boxScr=GetTileBBox(itum);
if( (boxScr & boxPoint) == boxPoint)
{
INDEX iRot=((ULONG)(tm*25.0f))&7;
ULONG ulLineType=0x0f0f0f0f<<iRot;
m_pDrawPort->DrawBorder(boxScr.Min()(1), boxScr.Min()(2),
boxScr.Size()(1), boxScr.Size()(2),
C_BLUE|CT_OPAQUE, ulLineType);
break;
}
}
// unlock the drawport
m_pDrawPort->Unlock();
// if there is a valid viewport
if (m_pViewPort!=NULL)
{
m_pViewPort->SwapBuffers();
}
}
}
BOOL CWndTerrainTilePalette::Initialize(PIX pixX, PIX pixY, CTextureData *ptd, BOOL bCenter/*=TRUE*/)
{
m_ptd=ptd;
// obtain tile info array
ObtainLayerTileInfo( &m_dcTileInfo, ptd, m_ctTilesPerRaw);
INDEX ctTiles=m_dcTileInfo.Count();
if(ctTiles==0) return FALSE;
m_ctPaletteTilesH=sqrt((FLOAT)ctTiles);
// calculate window's size
CRect rectWindow;
PIX pixWidth=PIX_TILE_WIDTH*m_ctPaletteTilesH;
PIX pixHeight=((ctTiles-1)/m_ctPaletteTilesH+1)*PIX_TILE_HEIGHT;
if(bCenter)
{
rectWindow.left = pixX-pixWidth/2;
rectWindow.top = pixY-pixHeight/2;
}
else
{
rectWindow.left = pixX;
rectWindow.top = pixY;
}
PIX pixScreenWidth = ::GetSystemMetrics(SM_CXSCREEN);
PIX pixScreenHeight = ::GetSystemMetrics(SM_CYSCREEN);
if( rectWindow.left+pixWidth>pixScreenWidth)
{
rectWindow.left=pixScreenWidth-pixWidth;
}
if( rectWindow.top+pixHeight>pixScreenHeight)
{
rectWindow.top=pixScreenHeight-pixHeight;
}
rectWindow.right = rectWindow.left + pixWidth+2;
rectWindow.bottom = rectWindow.top + pixHeight+2;
if( IsWindow(m_hWnd))
{
SetWindowPos( NULL, rectWindow.left, rectWindow.top,
rectWindow.right-rectWindow.left, rectWindow.top-rectWindow.bottom,
SWP_NOZORDER | SWP_NOACTIVATE);
ShowWindow(SW_SHOW);
}
else
{
// create window
CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
BOOL bResult = CreateEx( WS_EX_TOOLWINDOW,
NULL, _T("Terrain tile palette"), WS_CHILD|WS_POPUP|WS_VISIBLE,
rectWindow.left, rectWindow.top, rectWindow.Width(), rectWindow.Height(),
pMainFrame->m_hWnd, NULL, NULL);
if( !bResult)
{
AfxMessageBox( _T("Error: Failed to create terrain tile palette window!"));
return FALSE;
}
_pGfx->CreateWindowCanvas( m_hWnd, &m_pViewPort, &m_pDrawPort);
}
return TRUE;
}
void CWndTerrainTilePalette::OnKillFocus(CWnd* pNewWnd)
{
CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
if(pNewWnd!=pMainFrame->m_pwndToolTip && pNewWnd!=this)
{
DestroyWindow();
DeleteTempMap();
}
}
BOOL CWndTerrainTilePalette::PreTranslateMessage(MSG* pMsg)
{
if( pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_ESCAPE)
{
DestroyWindow();
DeleteTempMap();
return TRUE;
}
return CWnd::PreTranslateMessage(pMsg);
}
void CWndTerrainTilePalette::OnLButtonDown(UINT nFlags, CPoint point)
{
}
void CWndTerrainTilePalette::OnTimer(UINT nIDEvent)
{
POINT pt;
GetCursorPos( &pt);
CRect rectWnd;
GetWindowRect(rectWnd);
if(pt.x<rectWnd.left || pt.x>rectWnd.right ||
pt.y<rectWnd.top || pt.y>rectWnd.bottom)
{
DestroyWindow();
DeleteTempMap();
return;
}
Invalidate(FALSE);
CWnd::OnTimer(nIDEvent);
}
void CWndTerrainTilePalette::OnLButtonUp(UINT nFlags, CPoint point)
{
PIXaabbox2D boxPoint( PIX2D( point.x, point.y), PIX2D(point.x, point.y) );
// for all tiles
for( INDEX iTile=0; iTile<m_dcTileInfo.Count(); iTile++)
{
if( (GetTileBBox(iTile) & boxPoint) == boxPoint)
{
CTerrainLayer *ptlLayer=GetLayer();
if(ptlLayer==NULL) return;
if( ptlLayer->tl_ltType==LT_TILE)
{
ptlLayer->tl_iSelectedTile=iTile;
}
break;
}
}
DestroyWindow();
DeleteTempMap();
}