summaryrefslogtreecommitdiff
path: root/utils/xbox/FontMaker/fontmakerwnd.cpp
blob: a81a24e6c8ebbe0d6d1c6a9d4bd96fa24d56dde4 (plain) (blame)
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
//-----------------------------------------------------------------------------
// Name: FontMakerWnd.cpp
//
// Desc: The window and scroll view class for the fontmaker app
//
// Hist: 09.06.02 - Revised Fontmaker sample
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "FontMaker.h"
#include "Glyphs.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CTextureFont g_Font;
BOOL        g_bIsGlyphSelected     = FALSE;
int         g_iSelectedGlyphNum    = 0;
WCHAR       g_cSelectedGlyph       = '\0';
GLYPH_ATTR* g_pSelectedGylph       = NULL;

// Colors
#define COLOR_WHITE   ( RGB(255,255,255) )
#define COLOR_BLACK   ( RGB(  0,  0,  0) )
#define COLOR_BLUE    ( RGB(  0,  0,255) )
#define COLOR_RED     ( RGB(255,  0,  0) )
#define COLOR_DARKRED ( RGB(128,  0,  0) )

//-----------------------------------------------------------------------------
// CFontMakerFrameWnd
//-----------------------------------------------------------------------------

IMPLEMENT_DYNCREATE(CFontMakerFrameWnd, CFrameWnd)

BEGIN_MESSAGE_MAP(CFontMakerFrameWnd, CFrameWnd)
    //{{AFX_MSG_MAP(CFontMakerFrameWnd)
    ON_WM_CREATE()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

int CFontMakerFrameWnd::OnCreate( LPCREATESTRUCT pCreateStruct )
{
    if( CFrameWnd::OnCreate( pCreateStruct ) == -1 )
        return -1;

    // Create a docked dialog bar
    EnableDocking( CBRS_ALIGN_ANY );
    if( !m_wndDialogBar.Create( this, IDD_DIALOGBAR,
                                CBRS_LEFT, IDD_DIALOGBAR ) )
    {
        TRACE0("Failed to create DlgBar\n");
        return -1;
    }

    return 0;
}

//-----------------------------------------------------------------------------
// CFontMakerView
//-----------------------------------------------------------------------------

IMPLEMENT_DYNCREATE(CFontMakerView, CScrollView)

BEGIN_MESSAGE_MAP(CFontMakerView, CScrollView)
    //{{AFX_MSG_MAP(CFontMakerView)
    ON_WM_LBUTTONDOWN()
    ON_WM_ERASEBKGND()
    ON_WM_KEYDOWN()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

//-----------------------------------------------------------------------------
// Name: ~CFontMakerView()
// Desc: Destructor with special cleanup
//-----------------------------------------------------------------------------
CFontMakerView::~CFontMakerView()
{
    // Cleanup before exitting
    m_memDC.DeleteDC();
}

//-----------------------------------------------------------------------------
// Name: OnInitialUpdate()
// Desc: Called when the view is created.
//-----------------------------------------------------------------------------
void CFontMakerView::OnInitialUpdate()
{
    // Set the scroll bars
    CScrollView::OnInitialUpdate();
    CSize szTotal( g_Font.m_dwTextureWidth, g_Font.m_dwTextureHeight );
    SetScrollSizes( MM_TEXT, szTotal );
    CPoint ptCenter( 0, 0 );
    CenterOnPoint( ptCenter );

    // Create a secondary DC, used for drawing
    m_memDC.CreateCompatibleDC( GetDC() );
}

//-----------------------------------------------------------------------------
// Name: OnEraseBkgnd()
// Desc: Overridden function to prevent the view from flickering during resizes
//       and redraws
//-----------------------------------------------------------------------------
BOOL CFontMakerView::OnEraseBkgnd( CDC* pDC ) 
{
    return TRUE;
}

//-----------------------------------------------------------------------------
// Name: RenderSelectedGlyph() 
// Desc: Highlight the selected glyph
//-----------------------------------------------------------------------------
VOID RenderSelectedGlyph( CDC* pDC )
{
    // If no glyph is selected, return
    if( FALSE == g_bIsGlyphSelected )
        return;

    HRGN rgn = CreateRectRgn( 0, 0, g_Font.m_dwTextureWidth, g_Font.m_dwTextureHeight );
    SelectClipRgn( pDC->m_hDC, rgn );

    // Draw a higlighted background for the selected glyph
    {
        int x = g_pSelectedGylph->x;
        int y = g_pSelectedGylph->y;
        int w = g_pSelectedGylph->w;
        int h = g_pSelectedGylph->h;
        pDC->FillSolidRect( x, y, w, h, COLOR_DARKRED );
    }

    // Setup the DC for drawing text
    pDC->SetTextColor( COLOR_WHITE );
    pDC->SelectObject( g_Font.m_hFont );
    pDC->SetTextAlign( TA_LEFT|TA_TOP );
    pDC->SetMapMode( MM_TEXT );
    pDC->SetBkMode( TRANSPARENT );
    pDC->SetBkColor( COLOR_BLUE );

    CPen pen( PS_SOLID, 1, COLOR_RED );
    pDC->SelectObject( &pen );

    // Render the selected glyph
    GLYPH_ATTR* pGlyph = g_pSelectedGylph;
    {
        WCHAR c = g_cSelectedGlyph;
        WCHAR str[2] = L"A";
        str[0] = c ? c : 0xffff;

        if ( g_Font.m_ValidGlyphs[c] == 2 )
        {
            // Draw a square box for a placeholder for custom glyph graphics
            pDC->FillSolidRect( pGlyph->x, pGlyph->y, pGlyph->w, pGlyph->h, COLOR_BLACK );
        }
        else
        {
            int sx = pGlyph->x;
            int sy = pGlyph->y;

            // Adjust ccordinates to account for the leading edge
            if ( str[0] > 0x1000 )
            {
            }
            else
            {
                sx -= pGlyph->a;

                if ( g_Font.m_bOutlineEffect )
                    sx -= 1;
            }

            if ( g_Font.m_bOutlineEffect )
            {
                sx++;
                sy++;

                pDC->SetTextColor( COLOR_BLACK );
                ExtTextOutW( pDC->m_hDC, sx-1, sy-1, ETO_OPAQUE, NULL, str, 1, NULL );
                ExtTextOutW( pDC->m_hDC, sx+0, sy-1, ETO_OPAQUE, NULL, str, 1, NULL );
                ExtTextOutW( pDC->m_hDC, sx+1, sy-1, ETO_OPAQUE, NULL, str, 1, NULL );
                ExtTextOutW( pDC->m_hDC, sx-1, sy+0, ETO_OPAQUE, NULL, str, 1, NULL );
                ExtTextOutW( pDC->m_hDC, sx+1, sy+0, ETO_OPAQUE, NULL, str, 1, NULL );
                ExtTextOutW( pDC->m_hDC, sx-1, sy+1, ETO_OPAQUE, NULL, str, 1, NULL );
                ExtTextOutW( pDC->m_hDC, sx+0, sy+1, ETO_OPAQUE, NULL, str, 1, NULL );
                ExtTextOutW( pDC->m_hDC, sx+1, sy+1, ETO_OPAQUE, NULL, str, 1, NULL );
            }

            if ( g_Font.m_bShadowEffect )
            {
                pDC->SetTextColor( COLOR_BLACK );
                ExtTextOutW( pDC->m_hDC, sx+2, sy+2, ETO_OPAQUE, NULL, str, 1, NULL );
            }

            // Output the letter
            pDC->SetTextColor( COLOR_WHITE );
            ExtTextOutW( pDC->m_hDC, sx, sy, ETO_OPAQUE, NULL, str, 1, NULL );
        }
    }

    // Draw a red outline around the selected glyph
    {
        // Create a red pen
        CPen pen( PS_SOLID, 1, COLOR_RED );
        pDC->SelectObject( &pen );

        // Draw the outline
        int x1 = pGlyph->x - 1;
        int y1 = pGlyph->y - 1;
        int x2 = pGlyph->x + pGlyph->w;
        int y2 = pGlyph->y + pGlyph->h;

        pDC->MoveTo( x1, y1 );
        pDC->LineTo( x2, y1 );
        pDC->LineTo( x2, y2 );
        pDC->LineTo( x1, y2 );
        pDC->LineTo( x1, y1 );
    }

    SelectClipRgn( pDC->m_hDC, NULL );
    DeleteObject( rgn );
}

//-----------------------------------------------------------------------------
// Name: OnDraw()
// Desc: Overridden draw function. Draws the font glyphs if a font is loaded,
//       else just a black background.
//-----------------------------------------------------------------------------
VOID CFontMakerView::OnNewFontGlyphs()
{
    // Select the resulting bits into our memory DC
    static CBitmap Bitmap;
    Bitmap.DeleteObject();
    Bitmap.CreateBitmap( g_Font.m_dwTextureWidth, g_Font.m_dwTextureHeight, 1, 32, g_Font.m_pBits );
    m_memDC.SelectObject( &Bitmap );

    // Trigger the view to be re-drawn
    OnUpdate(0,0,0);
}

//-----------------------------------------------------------------------------
// Name: OnDraw()
// Desc: Overridden draw function. Draws the font glyphs if a font is loaded,
//       else just a black background.
//-----------------------------------------------------------------------------
VOID CFontMakerView::OnDraw( CDC* pDC )
{
    RECT rc;
    GetClientRect( &rc );

    // Draw the view
    if ( g_Font.m_hFont == NULL && !g_Font.m_pCustomFilename )
    {
        // Don't display any scroll bars
        CSize sizeTotal( 0, 0 );
        SetScrollSizes( MM_TEXT, sizeTotal );

        // Draw a black background
        pDC->FillSolidRect( 0, 0, rc.right, rc.bottom, COLOR_BLACK );
    }
    else
    {
        // Set the scroll sizes for the view
        CSize sizeTotal( g_Font.m_dwTextureWidth, g_Font.m_dwTextureHeight );
        SetScrollSizes( MM_TEXT, sizeTotal );

        // Draw the view's black areas
        pDC->FillSolidRect( g_Font.m_dwTextureWidth, 0, max( (int)g_Font.m_dwTextureWidth, rc.right), g_Font.m_dwTextureHeight, COLOR_BLACK );
        pDC->FillSolidRect( 0, g_Font.m_dwTextureHeight, max( (int)g_Font.m_dwTextureWidth, rc.right), max( (int)g_Font.m_dwTextureHeight, rc.bottom), COLOR_BLACK );

        // Display the bitmap of all the rendered glyphs
        pDC->BitBlt( 0, 0, g_Font.m_dwTextureWidth, g_Font.m_dwTextureHeight, &m_memDC, 0, 0, SRCCOPY );
    
        // Draw the selected glyph, if any
        RenderSelectedGlyph( pDC );
    }
}

//-----------------------------------------------------------------------------
// Name: OnLButtonDown()
// Desc: Overridden function to select a glyph when the user clicks the mouse.
//-----------------------------------------------------------------------------
void CFontMakerView::OnLButtonDown( UINT nFlags, CPoint point ) 
{
	if ( !g_Font.m_hFont )
		return;

    // Correct the mouseclick point to account for the scroll position
    point += GetScrollPosition();

    // Find out which glyph, if any, is selected by the mouse click
    theApp.UpdateSelectedGlyph( FALSE );
    
    for( DWORD i=0; i<g_Font.m_dwNumGlyphs && g_Font.m_pGlyphs; i++ )
    {
        GLYPH_ATTR* pGlyph = &g_Font.m_pGlyphs[i];
    
        if ( point.x >= pGlyph->x-2 && point.x <= pGlyph->x + pGlyph->w )
        {
            if ( point.y >= pGlyph->y-2 && point.y <= pGlyph->y + pGlyph->h )
            {
                theApp.UpdateSelectedGlyph( TRUE, i );
                break;
            }
        }
    }

    // Redraw the view to show the newly selected glyph
    Invalidate( TRUE );

    // Call the base class' handler
    CScrollView::OnLButtonDown( nFlags, point );
}

//-----------------------------------------------------------------------------
// Name: OnKeyDown()
// Desc: Overridden function to select a glyph with the keyboard.
//-----------------------------------------------------------------------------
void CFontMakerView::OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags ) 
{
	if ( !g_Font.m_hFont )
		return;
	
	if( g_Font.m_dwNumGlyphs > 0 )
    {
        if( FALSE == g_bIsGlyphSelected )
        {
            if( nChar == 37 || nChar == 38 || nChar == 39 || nChar == 40 ) // Left, right, up or down
            {
                // Select the first glyph
                theApp.UpdateSelectedGlyph( TRUE, 0 );
            }
        }
        else
        {
            if( nChar == 37 ) // Left
                theApp.UpdateSelectedGlyph( TRUE, max( 0, g_iSelectedGlyphNum-1 ) );
            else if( nChar == 39 ) // Right
                theApp.UpdateSelectedGlyph( TRUE, min( (int)g_Font.m_dwNumGlyphs-1, g_iSelectedGlyphNum+1 ) );
            else if( nChar == 38 || nChar == 40 ) // Up or down
            {
                // Find the closest glyph above or below to move to
                int x = g_pSelectedGylph->x + g_pSelectedGylph->w/2;
                int y = g_pSelectedGylph->y + g_pSelectedGylph->h/2;

                if( nChar == 38 ) y -= g_pSelectedGylph->h;
                if( nChar == 40 ) y += g_pSelectedGylph->h;

                for( DWORD i=0; i<g_Font.m_dwNumGlyphs; i++ )
                {
                    GLYPH_ATTR* pGlyph = &g_Font.m_pGlyphs[i];
                
                    if( x >= pGlyph->x-2 && x <= pGlyph->x + pGlyph->w )
                    {
                        if( y >= pGlyph->y-2 && y <= pGlyph->y + pGlyph->h )
                        {
                            theApp.UpdateSelectedGlyph( TRUE, i );
                            break;
                        }
                    }
                }
            }
			else if ( nChar == 46 )
			{
				// delete a glyph
				theApp.OnGlyphsCustom();

				// mark glyph as invalid
				g_Font.DeleteGlyph( g_cSelectedGlyph );

				// reset to the first glyph
				theApp.UpdateSelectedGlyph( FALSE );
			}
			else if ( nChar == 45 )
			{
				// insert a glyph
				theApp.OnGlyphsCustom();

				// reset to the first glyph
				theApp.UpdateSelectedGlyph( FALSE );
			
				theApp.InsertGlyph();
			}
        }

        // Redraw the view to show the newly selected glyph
        Invalidate( TRUE );
    }

    CScrollView::OnKeyDown( nChar, nRepCnt, nFlags );
}