Subversion Repositories spk

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
79 cycrow 1
//************************************************************************
2
//
3
// LCDStreamingText.cpp
4
//
5
// The CLCDStreamingText class draws streaming text onto the LCD.
6
// Streaming text is a single line of text that is repeatedly streamed
7
// horizontally across the LCD.
8
// 
9
// Logitech LCD SDK
10
//
11
// Copyright 2005 Logitech Inc.
12
//************************************************************************
13
 
14
#include "LCDStreamingText.h"
15
//#include <math.h>
16
 
17
//************************************************************************
18
//
19
// CLCDStreamingText::CLCDStreamingText
20
//
21
//************************************************************************
22
 
23
CLCDStreamingText::CLCDStreamingText()
24
{
25
//    m_sText.clear();
26
  //s  m_sGapText.clear();
27
 
28
    m_sGapText.assign(_T("   "));
29
    m_hFont = NULL;
30
    m_nTextAlignment = DT_LEFT;
31
}
32
 
33
 
34
//************************************************************************
35
//
36
// CLCDStreamingText::~CLCDStreamingText
37
//
38
//************************************************************************
39
 
40
CLCDStreamingText::~CLCDStreamingText()
41
{
42
    RemoveAllText();
43
    if (m_hFont)
44
    {
45
        DeleteObject(m_hFont);
46
        m_hFont = NULL;
47
    }
48
}
49
 
50
 
51
//************************************************************************
52
//
53
// CLCDStreamingText::Initialize
54
//
55
//************************************************************************
56
 
57
HRESULT CLCDStreamingText::Initialize(void)
58
{
59
    m_eState = STATE_DELAY;
60
    m_dwStartDelay  = 2000;
61
    m_dwSpeed       = 30;
62
	m_dwStepInPixels = 7;
63
    m_dwLastUpdate = 0;
64
    m_dwEllapsedTime = 0;
65
    m_dwLastUpdate = GetTickCount();
66
    m_bRecalcExtent = FALSE;
67
    m_pQueueHead = NULL;
68
    m_fFractDistance = 0.0f;
69
 
70
    m_hFont = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
71
    if(NULL != m_hFont)
72
    {
73
        SetFontPointSize(DEFAULT_POINTSIZE);
74
    }
75
 
76
    return CLCDCollection::Initialize();
77
}
78
 
79
 
80
//************************************************************************
81
//
82
// CLCDStreamingText::ResetUpdate
83
//
84
//************************************************************************
85
 
86
void CLCDStreamingText::ResetUpdate(void)
87
{
88
    m_eState = STATE_DELAY;
89
    m_dwEllapsedTime = 0;
90
    m_dwLastUpdate = GetTickCount();
91
    m_pQueueHead = NULL;
92
    m_fFractDistance = 0.0f;
93
 
94
    // remove, re-add, and recalculate the text
95
    m_bRecalcExtent = TRUE;
96
    RemoveAllText();
97
    AddText(m_sText.c_str());
98
}
99
 
100
 
101
//************************************************************************
102
//
103
// CLCDStreamingText::Show
104
//
105
//************************************************************************
106
 
107
void CLCDStreamingText::Show(BOOL bShow)
108
{
109
    CLCDCollection::Show(bShow);
110
}
111
 
112
 
113
//************************************************************************
114
//
115
// CLCDStreamingText::SetText
116
//
117
//************************************************************************
118
 
119
void CLCDStreamingText::SetText(LPCTSTR szText)
120
{
121
    LCDUIASSERT(NULL != szText);
122
    if(szText && _tcscmp(m_sText.c_str(), szText))
123
    {
124
        m_sText.assign(szText);
125
        m_bRecalcExtent = TRUE;
126
        ResetUpdate();
127
    }
128
}
129
 
130
void CLCDStreamingText::SetText( const std::string &szText)
131
{
132
	m_sText.assign(szText);
133
    m_bRecalcExtent = TRUE;
134
    ResetUpdate();
135
}
136
 
137
 
138
//************************************************************************
139
//
140
// CLCDStreamingText::SetOrigin
141
//
142
//************************************************************************
143
 
144
void CLCDStreamingText::SetOrigin(POINT pt)
145
{
146
	m_Origin = pt;
147
	SetOrigin(pt.x, pt.y);
148
}
149
 
150
 
151
//************************************************************************
152
//
153
// CLCDStreamingText::SetOrigin
154
//
155
//************************************************************************
156
 
157
void CLCDStreamingText::SetOrigin(int nX, int nY)
158
{
159
	m_Origin.x = nX;
160
	m_Origin.y = nY;
161
 
162
    LCD_OBJECT_LIST::iterator it = m_Objects.begin();
163
	if (it != m_Objects.end())
164
	{
165
        CLCDBase *pObject = *it;
166
		POINT ptOldOrigin = pObject->GetOrigin();
167
		pObject->SetOrigin(nX, nY);
168
 
169
		if ( (ptOldOrigin.x != nX) && (ptOldOrigin.y != nY) )
170
		{
171
			ResetUpdate();
172
		}
173
	}
174
}
175
 
176
 
177
//************************************************************************
178
//
179
// CLCDStreamingText::SetGapText
180
//
181
//************************************************************************
182
 
183
void CLCDStreamingText::SetGapText(LPCTSTR szGapText)
184
{
185
    LCDUIASSERT(NULL != szGapText);
186
    if(szGapText && _tcscmp(m_sGapText.c_str(), szGapText))
187
    {
188
        m_sGapText.assign(szGapText);
189
        m_bRecalcExtent = TRUE;
190
    }
191
}
192
 
193
 
194
//************************************************************************
195
//
196
// CLCDStreamingText::SetFont
197
//
198
//************************************************************************
199
 
200
void CLCDStreamingText::SetFont(LOGFONT& lf)
201
{
202
    if (m_hFont)
203
    {
204
        DeleteObject(m_hFont);
205
        m_hFont = NULL;
206
    }
207
 
208
    m_hFont = CreateFontIndirect(&lf);
209
    m_bRecalcExtent = TRUE;
210
}
211
 
212
 
213
//************************************************************************
214
//
215
// CLCDStreamingText::GetFont
216
//
217
//************************************************************************
218
 
219
HFONT CLCDStreamingText::GetFont()
220
{
221
    return m_hFont;
222
}
223
 
224
 
225
//************************************************************************
226
//
227
// CLCDText::SetFontFaceName
228
//
229
//************************************************************************
230
 
231
void CLCDStreamingText::SetFontFaceName(LPCTSTR szFontName)
232
{
233
    // if NULL, uses the default gui font
234
    if (NULL == szFontName)
235
        return;
236
 
237
    LOGFONT lf;
238
    ZeroMemory(&lf, sizeof(lf));
239
    GetObject(m_hFont, sizeof(LOGFONT), &lf);
240
 
241
    DeleteObject(m_hFont);
242
    m_hFont = NULL;
243
 
244
    _tcsncpy(lf.lfFaceName, szFontName, LF_FACESIZE);
245
 
246
    m_hFont = CreateFontIndirect(&lf);
247
}
248
 
249
 
250
//************************************************************************
251
//
252
// CLCDStreamingText::SetFontPointSize
253
//
254
//************************************************************************
255
 
256
void CLCDStreamingText::SetFontPointSize(int nPointSize)
257
{
258
    LOGFONT lf;
259
    ZeroMemory(&lf, sizeof(lf));
260
    GetObject(m_hFont, sizeof(LOGFONT), &lf);
261
 
262
    DeleteObject(m_hFont);
263
    m_hFont = NULL;
264
 
265
    lf.lfHeight = -MulDiv(nPointSize, DEFAULT_DPI, 72);
266
 
267
    m_hFont = CreateFontIndirect(&lf);
268
}
269
 
270
 
271
//************************************************************************
272
//
273
// CLCDStreamingText::SetFontWeight
274
//
275
//************************************************************************
276
 
277
void CLCDStreamingText::SetFontWeight(int nWeight)
278
{
279
    LOGFONT lf;
280
    ZeroMemory(&lf, sizeof(lf));
281
    GetObject(m_hFont, sizeof(LOGFONT), &lf);
282
 
283
    DeleteObject(m_hFont);
284
    m_hFont = NULL;
285
 
286
    lf.lfWeight = nWeight;
287
 
288
    m_hFont = CreateFontIndirect(&lf);
289
}
290
 
291
 
292
//************************************************************************
293
//
294
// CLCDStreamingText::SetAlignment
295
//
296
// only relevant if no streaming
297
//************************************************************************
298
 
299
void CLCDStreamingText::SetAlignment(int nAlignment)
300
{
301
    m_nTextAlignment = nAlignment;
302
}
303
 
304
 
305
//************************************************************************
306
//
307
// CLCDStreamingText::AddText
308
//
309
//************************************************************************
310
 
311
int CLCDStreamingText::AddText(LPCTSTR szText)
312
{
313
 
314
    CLCDText* pText = new CLCDText;
315
    pText->Initialize();
316
    pText->SetText(szText);
317
    pText->SetOrigin(GetOrigin().x, GetOrigin().y);
318
    pText->SetLogicalOrigin(GetLogicalOrigin().x, GetLogicalOrigin().y);
319
    pText->SetSize(GetWidth(), GetHeight());
320
    pText->SetBackgroundMode(OPAQUE);
321
 
322
    LOGFONT lf;
323
    GetObject(m_hFont, sizeof(LOGFONT), &lf);
324
    pText->SetFont(lf);
325
 
326
    m_bRecalcExtent = TRUE;
327
 
328
    m_Objects.push_back(pText);
329
 
330
    if (NULL == m_pQueueHead)
331
    {
332
        m_pQueueHead = pText;
333
    }
334
 
335
    // return the zero-based index
336
    return (int)(m_Objects.size()-1);
337
}
338
 
339
 
340
//************************************************************************
341
//
342
// CLCDStreamingText::RemoveText
343
//
344
//************************************************************************
345
 
346
void CLCDStreamingText::RemoveText(int nIndex)
347
{
348
    LCD_OBJECT_LIST::iterator it = m_Objects.begin();
349
    int i = 0;
350
    while(it != m_Objects.end())
351
    {
352
        CLCDBase *pObject = *it;
353
        LCDUIASSERT(NULL != pObject);
354
 
355
        if (i == nIndex)
356
        {
357
            m_Objects.erase(it);
358
            if(NULL != pObject)
359
            {
360
                delete pObject;
361
            }
362
            break;
363
        }
364
 
365
        ++it;
366
        ++i;
367
    }
368
}
369
 
370
 
371
//************************************************************************
372
//
373
// CLCDStreamingText::RemoveAllText
374
//
375
//************************************************************************
376
 
377
void CLCDStreamingText::RemoveAllText()
378
{
379
    LCD_OBJECT_LIST::iterator it = m_Objects.begin();
380
    while(it != m_Objects.end())
381
    {
382
        // we need to delete all the text objects that were placed in to m_Objects
383
        CLCDBase *pObject = *it;
384
        LCDUIASSERT(NULL != pObject);
385
 
386
        if(NULL != pObject)
387
        {
388
            delete pObject;
389
        }
390
        ++it;
391
    }
392
    m_Objects.clear();
393
}
394
 
395
 
396
//************************************************************************
397
//
398
// CLCDStreamingText::SetStartDelay
399
//
400
//************************************************************************
401
 
402
void CLCDStreamingText::SetStartDelay(DWORD dwMilliseconds)
403
{
404
    m_dwStartDelay = dwMilliseconds;
405
}
406
 
407
 
408
//************************************************************************
409
//
410
// CLCDStreamingText::SetSpeed
411
//
412
//************************************************************************
413
 
414
void CLCDStreamingText::SetSpeed(DWORD dwSpeed)
415
{
416
    m_dwSpeed = dwSpeed;
417
}
418
 
419
//************************************************************************
420
//
421
// CLCDStreamingText::SetScrollingStep: sets the number of pixels the text 
422
// will jump when it scrolls
423
//
424
//************************************************************************
425
 
426
void CLCDStreamingText::SetScrollingStep(DWORD dwStepInPixels)
427
{
428
    m_dwStepInPixels = dwStepInPixels;
429
}
430
 
431
//************************************************************************
432
//
433
// CLCDStreamingText::OnUpdate
434
//
435
//************************************************************************
436
 
437
void CLCDStreamingText::OnUpdate(DWORD dwTimestamp)
438
{
439
    m_dwEllapsedTime = (dwTimestamp - m_dwLastUpdate);
440
}
441
 
442
 
443
//************************************************************************
444
//
445
// CLCDStreamingText::OnDraw
446
//
447
//************************************************************************
448
 
449
void CLCDStreamingText::OnDraw(CLCDGfx &rGfx)
450
{
451
 
452
    if (m_bRecalcExtent)
453
    {
454
        // this just recalculates the text extents
455
        RecalcTextBoxes(rGfx);
456
        m_bRecalcExtent = FALSE;
457
        return;
458
    }
459
 
460
    switch(m_eState)
461
    {
462
    case STATE_DELAY:
463
        if (m_dwEllapsedTime > m_dwStartDelay)
464
        {
465
            m_eState = STATE_SCROLL;
466
            m_dwEllapsedTime = 0;
467
            m_dwLastUpdate = GetTickCount();
468
        }
469
        break;
470
    case STATE_SCROLL:
471
        {
472
            // update the positions
473
            float fDistance = (float)(m_dwSpeed * m_dwEllapsedTime) / 1000.0f;
474
            m_dwLastUpdate = GetTickCount();
475
 
476
            if (m_Objects.size() > 1)
477
            {
478
                // extract any previous fractional remainder
479
                // and add it to the current distance
480
                float fTotDistance = (fDistance + m_fFractDistance);
481
                m_fFractDistance = (fTotDistance >= (float)m_dwStepInPixels) ? (float)(fTotDistance - (int)fTotDistance) : fTotDistance;
482
 
483
                if (fTotDistance < 0.0f)
484
                    fTotDistance = 0.0f;
485
                if (m_fFractDistance < 0.0f)
486
                    m_fFractDistance = 0.0f;
487
 
488
				if (fTotDistance >= (float)m_dwStepInPixels)
489
					ApplyOrigins(-1 * (int)fTotDistance); // left
490
            }
491
        }
492
        break;
493
    default:
494
        break;
495
    }
496
 
497
    CLCDCollection::OnDraw(rGfx);
498
}
499
 
500
 
501
//************************************************************************
502
//
503
// CLCDStreamingText::C
504
//
505
//************************************************************************
506
 
507
BOOL CLCDStreamingText::RecalcTextBoxes(CLCDGfx &rGfx)
508
{
509
 
510
    // check if we need to add another text box
511
    LCD_OBJECT_LIST::iterator it = m_Objects.begin();
512
 
513
    if (it == m_Objects.end())
514
        return FALSE;
515
 
516
    CLCDBase* pObject = *it;
517
    CLCDText* pText = (CLCDText*)pObject;
518
    LCDUIASSERT(NULL != pObject);
519
 
520
    LOGFONT lf;
521
    GetObject(m_hFont, sizeof(LOGFONT), &lf);
522
    pText->SetFont(lf);
523
 
524
    // this will re-evaluate the main text object
525
    LCDUIASSERT(m_Objects.size() == 1);
526
    CLCDCollection::OnDraw(rGfx);
527
 
528
    if (it != m_Objects.end())
529
    {
530
        if (pText->GetHExtent().cx > GetWidth())
531
        {
532
 
533
            pText->SetAlignment(DT_LEFT);
534
 
535
            // add a gap
536
            AddText(m_sGapText.c_str());
537
            // add another text
538
            AddText(m_sText.c_str());
539
            // add last gap
540
            AddText(m_sGapText.c_str());
541
        }
542
        else
543
        {
544
            pText->SetAlignment(m_nTextAlignment);
545
        }
546
    }
547
 
548
    // this will re-evaluate the other text objects
549
    CLCDCollection::OnDraw(rGfx);
550
    RecalcTextBoxOrigins();
551
 
552
    return TRUE;
553
}
554
 
555
 
556
//************************************************************************
557
//
558
// CLCDStreamingText::RecalcTextBoxOrigins
559
//
560
// Puts all the text boxes in order next to each other
561
//************************************************************************
562
 
563
void CLCDStreamingText::RecalcTextBoxOrigins()
564
{
565
 
566
    if (m_Objects.size() <= 1)
567
        return;
568
 
569
    // draw everyone to the left by the offset
570
    int nOrgOffset = 0;
571
    LCD_OBJECT_LIST::iterator it = m_Objects.begin();
572
    while(it != m_Objects.end())
573
    {
574
        CLCDBase* pObject = *it;
575
        CLCDText* pText = (CLCDText*)pObject;
576
        LCDUIASSERT(NULL != pObject);
577
 
578
        pText->SetLogicalSize(pText->GetHExtent().cx, pText->GetHExtent().cy);
579
 
580
        // string can be empty which generates zero logical space
581
        //LCDUIASSERT(pText->GetLogicalSize().cx);
582
        //LCDUIASSERT(pText->GetLogicalSize().cy);
583
 
584
        POINT& ptOrigin = pText->GetLogicalOrigin();
585
 
586
		if (nOrgOffset == 0)
587
		{
588
			nOrgOffset = pText->GetLogicalOrigin().x;
589
		}
590
 
591
        pText->SetLogicalOrigin(nOrgOffset, ptOrigin.y);
592
        nOrgOffset += pText->GetHExtent().cx;
593
 
594
        ++it;
595
    }
596
 
597
}
598
 
599
 
600
//************************************************************************
601
//
602
// CLCDStreamingText::ApplyOrigins
603
//
604
//************************************************************************
605
 
606
void CLCDStreamingText::ApplyOrigins(int nOffset)
607
{
608
 
609
    // draw everyone to the left by the offset
610
    LCD_OBJECT_LIST::iterator it = m_Objects.begin();
611
    while(it != m_Objects.end())
612
    {
613
        CLCDBase* pObject = *it;
614
        CLCDText* pText = (CLCDText*)pObject;
615
        LCDUIASSERT(NULL != pObject);
616
 
617
        POINT& ptOrigin = pText->GetLogicalOrigin();
618
 
619
        pText->SetLogicalOrigin(ptOrigin.x + nOffset, ptOrigin.y);
620
 
621
        ++it;
622
    }
623
 
624
    // If the active box is no longer visible, 
625
    // pop it off the push it to the end of the list
626
    if (abs(m_pQueueHead->GetLogicalOrigin().x) >= m_pQueueHead->GetHExtent().cx)
627
    {
628
        m_Objects.pop_front();
629
        m_Objects.push_back(m_pQueueHead);
630
        RecalcTextBoxOrigins();
631
        m_pQueueHead = (CLCDText*)*m_Objects.begin();
632
    }
633
 
634
}
635
 
636
 
637
//** end of LCDStreamingText.cpp *******************************************