-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomComboWnd.cpp
354 lines (309 loc) · 9.43 KB
/
CustomComboWnd.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. */
// CustomComboWnd.cpp : implementation file
//
#include "StdH.h"
#include "WorldEditor.h"
#include "CustomComboWnd.h"
#ifdef _DEBUG
#undef new
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define SPACING_H PIX(4)
#define SPACING_V PIX(4)
/*
#define COLOR_UNDER_MOUSE (C_GRAY|CT_OPAQUE)
#define COLOR_SELECTED (C_vdRED|CT_OPAQUE)
#define COLOR_SELECTED_UNDER_MOUSE (C_RED|CT_OPAQUE)
*/
#define COLOR_UNDER_MOUSE (C_RED|CT_OPAQUE)
#define COLOR_SELECTED (C_mdGRAY|CT_OPAQUE)
#define COLOR_SELECTED_UNDER_MOUSE (C_RED|CT_OPAQUE)
/////////////////////////////////////////////////////////////////////////////
// CCustomComboWnd
CCustomComboWnd::CCustomComboWnd()
{
m_pfResult=NULL;
m_pOnSelect=NULL;
m_pDrawPort = NULL;
m_pViewPort = NULL;
// mark that timer is not yet started
m_iTimerID = -1;
}
PIX GetFixedTextWidth( const CTString &strText, CFontData *pfd)
{
return strText.Length()*pfd->fd_pixCharWidth;
}
PIX GetFixedTextHeight( CFontData *pfd)
{
return pfd->fd_pixCharHeight;
}
void CCustomComboWnd::GetComboLineSize(PIX &pixMaxWidth, PIX &pixMaxHeight)
{
pixMaxWidth=0;
pixMaxHeight=GetFixedTextHeight(_pfdConsoleFont)+SPACING_V;
FOREACHINDYNAMICCONTAINER(m_dcComboLines, CComboLine, itcl)
{
CComboLine &cl=*itcl;
PIX pixIconW=cl.cl_boxIcon.Max()(1)-cl.cl_boxIcon.Min()(1);
PIX pixIconH=cl.cl_boxIcon.Max()(2)-cl.cl_boxIcon.Min()(2);
PIX pixLineWidth=GetFixedTextWidth(cl.cl_strText, _pfdConsoleFont)+pixIconW;
pixMaxWidth=Max(pixMaxWidth,pixLineWidth+SPACING_H);
pixMaxHeight=Max(pixMaxHeight,pixIconH+SPACING_V);
}
}
CCustomComboWnd::~CCustomComboWnd()
{
if( m_pViewPort != NULL)
{
_pGfx->DestroyWindowCanvas( m_pViewPort);
m_pViewPort = NULL;
}
}
#define LINES_PER_X 1
#define LINES_PER_Y 10
#define CLIENT_BORDER 2
PIXaabbox2D CCustomComboWnd::GetLineBBox( INDEX iLine)
{
PIX pixWidth, pixHeight;
GetComboLineSize(pixWidth, pixHeight);
// return calculated box
return PIXaabbox2D( PIX2D(0, pixHeight*iLine), PIX2D(pixWidth, pixHeight*(iLine+1)-1) );
}
BEGIN_MESSAGE_MAP(CCustomComboWnd, CWnd)
//{{AFX_MSG_MAP(CCustomComboWnd)
ON_WM_PAINT()
ON_WM_KILLFOCUS()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_DESTROY()
ON_WM_TIMER()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCustomComboWnd message handlers
void CCustomComboWnd::OnPaint()
{
{
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_vdGRAY|CT_OPAQUE);
// erase z-buffer
m_pDrawPort->FillZBuffer(ZBUF_BACK);
// for all lines
for( INDEX iLine=0; iLine<m_dcComboLines.Count(); iLine++)
{
// get current line's box in pixels inside window
PIXaabbox2D boxLine = GetLineBBox( iLine);
PIX2D pixMin=boxLine.Min();
PIX2D pixMax=boxLine.Max();
pixMax(1)-=1;
pixMax(2)-=1;
PIXaabbox2D boxLineDecreased=PIXaabbox2D(pixMin,pixMax);
PIXaabbox2D boxPoint( PIX2D( ptMouse.x, ptMouse.y), PIX2D(ptMouse.x, ptMouse.y) );
BOOL bUnderMouse=(boxLine & boxPoint) == boxPoint;
BOOL bSelected=(m_pfResult!=NULL && iLine==*m_pfResult);
COLOR colFill=C_BLACK|CT_TRANSPARENT;
if(bUnderMouse&&bSelected) colFill=COLOR_SELECTED_UNDER_MOUSE;
else if(bUnderMouse) colFill=COLOR_UNDER_MOUSE;
else if(bSelected) colFill=COLOR_SELECTED;
RenderOneLine( iLine, boxLineDecreased, m_pDrawPort, colFill);
}
m_pDrawPort->DrawBorder( 0,0, m_pDrawPort->GetWidth(),m_pDrawPort->GetHeight(), C_mdGRAY|CT_OPAQUE);
// unlock the drawport
m_pDrawPort->Unlock();
// if there is a valid viewport
if (m_pViewPort!=NULL)
{
m_pViewPort->SwapBuffers();
}
}
}
BOOL CCustomComboWnd::Initialize(FLOAT *pfResult, void (*pOnSelect)(INDEX iSelected),
PIX pixX, PIX pixY, BOOL bDown/*=FALSE*/)
{
m_pfResult=pfResult;
m_pOnSelect=pOnSelect;
// calculate window's size
CRect rectWindow;
rectWindow.left = pixX;
rectWindow.bottom = pixY;
PIX pixWidth, pixHeight;
GetComboLineSize(pixWidth, pixHeight);
pixHeight=pixHeight*m_dcComboLines.Count()-1;
rectWindow.right = rectWindow.left + pixWidth;
if( bDown)
{
rectWindow.top = rectWindow.bottom;
rectWindow.bottom+=pixHeight;
}
else
{
rectWindow.top = rectWindow.bottom - pixHeight;
}
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("Custom combo"), 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 custom combo!"));
return FALSE;
}
_pGfx->CreateWindowCanvas( m_hWnd, &m_pViewPort, &m_pDrawPort);
}
return TRUE;
}
INDEX CCustomComboWnd::InsertItem(CTString strText, CTFileName fnmIcons/*=""*/, MEXaabbox2D boxIcon/*=dummy box*/)
{
CComboLine *pcl=new(CComboLine);
pcl->cl_fnmTexture=fnmIcons;
pcl->cl_boxIcon=boxIcon;
pcl->cl_strText=strText;
m_dcComboLines.Add(pcl);
INDEX iCount=m_dcComboLines.Count();
pcl->cl_ulValue=iCount-1;
pcl->cl_colText=C_YELLOW|CT_OPAQUE;
return iCount-1;
}
void CCustomComboWnd::SetItemValue(INDEX iItem, ULONG ulValue)
{
m_dcComboLines[iItem].cl_ulValue=ulValue;
}
void CCustomComboWnd::SetItemColor(INDEX iItem, COLOR col)
{
m_dcComboLines[iItem].cl_colText=col;
}
void CCustomComboWnd::RenderOneLine( INDEX iLine, PIXaabbox2D rectLine, CDrawPort *pdp, COLOR colFill)
{
CComboLine &cl=m_dcComboLines[iLine];
pdp->Fill(rectLine.Min()(1)-1, rectLine.Min()(2)-1,
rectLine.Max()(1)-rectLine.Min()(1)+1, rectLine.Max()(2)-rectLine.Min()(2)+1,
colFill);
if(cl.cl_fnmTexture!="")
{
CTextureObject to;
try
{
to.SetData_t(cl.cl_fnmTexture);
PIXaabbox2D rectIcon=PIXaabbox2D( rectLine.Min()+PIX2D(SPACING_H/2,SPACING_V/2),
PIX2D(rectLine.Min()+cl.cl_boxIcon.Size())-PIX2D(SPACING_H/2,SPACING_V/2));
pdp->PutTexture( &to, rectIcon, cl.cl_boxIcon);
}
catch(char *strError)
{
(void) strError;
}
}
pdp->SetFont( _pfdConsoleFont);
pdp->SetTextAspect( 1.0f);
pdp->SetTextScaling( 1.0f);
PIX pixX=rectLine.Min()(1)+cl.cl_boxIcon.Size()(1)+SPACING_H/2;
PIX pixTextH= GetFixedTextHeight( _pfdConsoleFont);
PIX pixY=rectLine.Min()(2)+(rectLine.Size()(2)-pixTextH)/2;
pdp->PutText( cl.cl_strText, pixX, pixY, cl.cl_colText);
}
void CCustomComboWnd::OnKillFocus(CWnd* pNewWnd)
{
CMainFrame* pMainFrame = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
if( pNewWnd!=pMainFrame->m_pwndToolTip && pNewWnd!=this)
{
// destroy combo
DestroyWindow();
DeleteTempMap();
}
}
void CCustomComboWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
}
void CCustomComboWnd::OnMouseMove(UINT nFlags, CPoint point)
{
Invalidate(FALSE);
CWnd::OnMouseMove(nFlags, point);
}
void CCustomComboWnd::OnDestroy()
{
KillTimer( m_iTimerID);
CWnd::OnDestroy();
}
void CCustomComboWnd::OnTimer(UINT nIDEvent)
{
Invalidate(FALSE);
CWnd::OnTimer(nIDEvent);
}
BOOL CCustomComboWnd::PreTranslateMessage(MSG* pMsg)
{
if( pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_ESCAPE)
{
DestroyWindow();
DeleteTempMap();
return TRUE;
}
return CWnd::PreTranslateMessage(pMsg);
}
void CCustomComboWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
PIXaabbox2D boxPoint( PIX2D( point.x, point.y), PIX2D(point.x, point.y) );
// for all lines
for( INDEX iLine=0; iLine<m_dcComboLines.Count(); iLine++)
{
if( (GetLineBBox( iLine) & boxPoint) == boxPoint)
{
if(m_pfResult!=NULL)
{
*m_pfResult= m_dcComboLines[iLine].cl_ulValue;
// destroy combo
DestroyWindow();
DeleteTempMap();
return;
}
if(m_pOnSelect!=NULL)
{
void (*pOnSelect)(INDEX iSelected)=m_pOnSelect;
INDEX iValue=m_dcComboLines[iLine].cl_ulValue;
// destroy combo
DestroyWindow();
DeleteTempMap();
pOnSelect(iValue);
return;
}
}
}
CWnd::OnLButtonUp(nFlags, point);
}