-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathRichText.cpp
More file actions
2753 lines (2393 loc) · 76.5 KB
/
RichText.cpp
File metadata and controls
2753 lines (2393 loc) · 76.5 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "vgui_controls/pch_vgui_controls.h"
#include "vgui/ILocalize.h"
// memdbgon must be the last include file in a .cpp file
#include "tier0/memdbgon.h"
enum
{
MAX_BUFFER_SIZE = 999999, // maximum size of text buffer
DRAW_OFFSET_X = 3,
DRAW_OFFSET_Y = 1,
};
using namespace vgui;
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
namespace vgui
{
//#define DRAW_CLICK_PANELS
//-----------------------------------------------------------------------------
// Purpose: Panel used for clickable URL's
//-----------------------------------------------------------------------------
class ClickPanel : public Panel
{
DECLARE_CLASS_SIMPLE( ClickPanel, Panel );
public:
ClickPanel(Panel *parent)
{
_viewIndex = 0;
_textIndex = 0;
SetParent(parent);
AddActionSignalTarget(parent);
SetCursor(dc_hand);
SetPaintBackgroundEnabled(false);
SetPaintEnabled(false);
// SetPaintAppearanceEnabled(false);
#if defined( DRAW_CLICK_PANELS )
SetPaintEnabled(true);
#endif
}
void SetTextIndex( int linkStartIndex, int viewStartIndex )
{
_textIndex = linkStartIndex;
_viewIndex = viewStartIndex;
}
#if defined( DRAW_CLICK_PANELS )
virtual void Paint()
{
surface()->DrawSetColor( Color( 255, 0, 0, 255 ) );
surface()->DrawOutlinedRect( 0, 0, GetWide(), GetTall() );
}
#endif
int GetTextIndex()
{
return _textIndex;
}
int GetViewTextIndex()
{
return _viewIndex;
}
void OnMousePressed(MouseCode code)
{
if (code == MOUSE_LEFT)
{
PostActionSignal(new KeyValues("ClickPanel", "index", _textIndex));
}
else
{
GetParent()->OnMousePressed( code );
}
}
private:
int _textIndex;
int _viewIndex;
};
//-----------------------------------------------------------------------------
// Purpose: Panel used only to draw the interior border region
//-----------------------------------------------------------------------------
class RichTextInterior : public Panel
{
DECLARE_CLASS_SIMPLE( RichTextInterior, Panel );
public:
RichTextInterior( RichText *pParent, const char *pchName ) : BaseClass( pParent, pchName )
{
SetKeyBoardInputEnabled( false );
SetMouseInputEnabled( false );
SetPaintBackgroundEnabled( false );
SetPaintEnabled( false );
m_pRichText = pParent;
}
/* virtual IAppearance *GetAppearance()
{
if ( m_pRichText->IsScrollbarVisible() )
return m_pAppearanceScrollbar;
return BaseClass::GetAppearance();
}*/
virtual void ApplySchemeSettings( IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
// m_pAppearanceScrollbar = FindSchemeAppearance( pScheme, "scrollbar_visible" );
}
private:
RichText *m_pRichText;
// IAppearance *m_pAppearanceScrollbar;
};
}; // namespace vgui
DECLARE_BUILD_FACTORY( RichText );
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
RichText::RichText(Panel *parent, const char *panelName) : BaseClass(parent, panelName)
{
m_bAllTextAlphaIsZero = false;
_font = INVALID_FONT;
m_hFontUnderline = INVALID_FONT;
m_bRecalcLineBreaks = true;
m_pszInitialText = NULL;
_cursorPos = 0;
_mouseSelection = false;
_mouseDragSelection = false;
_vertScrollBar = new ScrollBar(this, "ScrollBar", true);
_vertScrollBar->AddActionSignalTarget(this);
_recalcSavedRenderState = true;
_maxCharCount = (64 * 1024);
AddActionSignalTarget(this);
m_pInterior = new RichTextInterior( this, NULL );
//a -1 for _select[0] means that the selection is empty
_select[0] = -1;
_select[1] = -1;
m_pEditMenu = NULL;
SetCursor(dc_ibeam);
//position the cursor so it is at the end of the text
GotoTextEnd();
// set default foreground color to black
_defaultTextColor = Color(0, 0, 0, 0);
// initialize the line break array
InvalidateLineBreakStream();
if ( IsProportional() )
{
int width, height;
int sw,sh;
surface()->GetProportionalBase( width, height );
surface()->GetScreenSize(sw, sh);
_drawOffsetX = static_cast<int>( static_cast<float>( DRAW_OFFSET_X )*( static_cast<float>( sw )/ static_cast<float>( width )));
_drawOffsetY = static_cast<int>( static_cast<float>( DRAW_OFFSET_Y )*( static_cast<float>( sw )/ static_cast<float>( width )));
}
else
{
_drawOffsetX = DRAW_OFFSET_X;
_drawOffsetY = DRAW_OFFSET_Y;
}
// add a basic format string
TFormatStream stream;
stream.color = _defaultTextColor;
stream.fade.flFadeStartTime = 0.0f;
stream.fade.flFadeLength = -1.0f;
stream.pixelsIndent = 0;
stream.textStreamIndex = 0;
stream.textClickable = false;
m_FormatStream.AddToTail(stream);
m_bResetFades = false;
m_bInteractive = true;
m_bUnusedScrollbarInvis = false;
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
RichText::~RichText()
{
delete [] m_pszInitialText;
delete m_pEditMenu;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void RichText::SetDrawOffsets( int ofsx, int ofsy )
{
_drawOffsetX = ofsx;
_drawOffsetY = ofsy;
}
//-----------------------------------------------------------------------------
// Purpose: sets it as drawing text only - used for embedded RichText control into other text drawing situations
//-----------------------------------------------------------------------------
void RichText::SetDrawTextOnly()
{
SetDrawOffsets( 0, 0 );
SetPaintBackgroundEnabled( false );
// SetPaintAppearanceEnabled( false );
SetPostChildPaintEnabled( false );
m_pInterior->SetVisible( false );
SetVerticalScrollbar( false );
}
//-----------------------------------------------------------------------------
// Purpose: configures colors
//-----------------------------------------------------------------------------
void RichText::ApplySchemeSettings(IScheme *pScheme)
{
BaseClass::ApplySchemeSettings(pScheme);
_font = pScheme->GetFont("Default", IsProportional() );
m_hFontUnderline = pScheme->GetFont("DefaultUnderline", IsProportional() );
SetFgColor(GetSchemeColor("RichText.TextColor", pScheme));
SetBgColor(GetSchemeColor("RichText.BgColor", pScheme));
_selectionTextColor = GetSchemeColor("RichText.SelectedTextColor", GetFgColor(), pScheme);
_selectionColor = GetSchemeColor("RichText.SelectedBgColor", pScheme);
if ( Q_strlen( pScheme->GetResourceString( "RichText.InsetX" ) ) )
{
SetDrawOffsets( atoi( pScheme->GetResourceString( "RichText.InsetX" ) ), atoi( pScheme->GetResourceString( "RichText.InsetY" ) ) );
}
}
//-----------------------------------------------------------------------------
// Purpose: if the default format color isn't set then set it
//-----------------------------------------------------------------------------
void RichText::SetFgColor( Color color )
{
// Replace default format color if
// the stream is empty and the color is the default ( or the previous FgColor )
if ( m_FormatStream.Size() == 1 &&
( m_FormatStream[0].color == _defaultTextColor || m_FormatStream[0].color == GetFgColor() ) )
{
m_FormatStream[0].color = color;
}
BaseClass::SetFgColor( color );
}
//-----------------------------------------------------------------------------
// Purpose: Sends a message if the data has changed
// Turns off any selected text in the window if we are not using the edit menu
//-----------------------------------------------------------------------------
void RichText::OnKillFocus()
{
// check if we clicked the right mouse button or if it is down
bool mouseRightClicked = input()->WasMousePressed(MOUSE_RIGHT);
bool mouseRightUp = input()->WasMouseReleased(MOUSE_RIGHT);
bool mouseRightDown = input()->IsMouseDown(MOUSE_RIGHT);
if (mouseRightClicked || mouseRightDown || mouseRightUp )
{
// get the start and ends of the selection area
int start, end;
if (GetSelectedRange(start, end)) // we have selected text
{
// see if we clicked in the selection area
int startX, startY;
CursorToPixelSpace(start, startX, startY);
int endX, endY;
CursorToPixelSpace(end, endX, endY);
int cursorX, cursorY;
input()->GetCursorPos(cursorX, cursorY);
ScreenToLocal(cursorX, cursorY);
// check the area vertically
// we need to handle the horizontal edge cases eventually
int fontTall = GetLineHeight();
endY = endY + fontTall;
if ((startY < cursorY) && (endY > cursorY))
{
// if we clicked in the selection area, leave the text highlighted
return;
}
}
}
// clear any selection
SelectNone();
// chain
BaseClass::OnKillFocus();
}
//-----------------------------------------------------------------------------
// Purpose: Wipe line breaks after the size of a panel has been changed
//-----------------------------------------------------------------------------
void RichText::OnSizeChanged( int wide, int tall )
{
BaseClass::OnSizeChanged( wide, tall );
// blow away the line breaks list
_invalidateVerticalScrollbarSlider = true;
InvalidateLineBreakStream();
InvalidateLayout();
if ( _vertScrollBar->IsVisible() )
{
_vertScrollBar->MakeReadyForUse();
m_pInterior->SetBounds( 0, 0, wide - _vertScrollBar->GetWide(), tall );
}
else
{
m_pInterior->SetBounds( 0, 0, wide, tall );
}
}
const wchar_t *RichText::ResolveLocalizedTextAndVariables( char const *pchLookup, wchar_t *outbuf, size_t outbufsizeinbytes )
{
if ( pchLookup[ 0 ] == '#' )
{
// try lookup in localization tables
StringIndex_t index = g_pVGuiLocalize->FindIndex( pchLookup + 1 );
if ( index == INVALID_LOCALIZE_STRING_INDEX )
{
/* // if it's not found, maybe it's a special expanded variable - look for an expansion
char rgchT[MAX_PATH];
// get the variables
KeyValues *variables = GetDialogVariables_R();
if ( variables )
{
// see if any are any special vars to put in
for ( KeyValues *pkv = variables->GetFirstSubKey(); pkv != NULL; pkv = pkv->GetNextKey() )
{
if ( !Q_strncmp( pkv->GetName(), "$", 1 ) )
{
// make a new lookup, with this key appended
Q_snprintf( rgchT, sizeof( rgchT ), "%s%s=%s", pchLookup, pkv->GetName(), pkv->GetString() );
index = localize()->FindIndex( rgchT );
break;
}
}
}
*/
}
// see if we have a valid string
if ( index != INVALID_LOCALIZE_STRING_INDEX )
{
wchar_t *format = g_pVGuiLocalize->GetValueByIndex( index );
Assert( format );
if ( format )
{
/*// Try and substitute variables if any
KeyValues *variables = GetDialogVariables_R();
if ( variables )
{
localize()->ConstructString( outbuf, outbufsizeinbytes, index, variables );
return outbuf;
}*/
}
V_wcsncpy( outbuf, format, outbufsizeinbytes );
return outbuf;
}
}
Q_UTF8ToUnicode( pchLookup, outbuf, outbufsizeinbytes );
return outbuf;
}
//-----------------------------------------------------------------------------
// Purpose: Set the text array
// Using this function will cause all lineBreaks to be discarded.
// This is because this fxn replaces the contents of the text buffer.
// For modifying large buffers use insert functions.
//-----------------------------------------------------------------------------
void RichText::SetText(const char *text)
{
if (!text)
{
text = "";
}
wchar_t unicode[1024];
if (text[0] == '#')
{
ResolveLocalizedTextAndVariables( text, unicode, sizeof( unicode ) );
SetText( unicode );
return;
}
// convert to unicode
Q_UTF8ToUnicode(text, unicode, sizeof(unicode));
SetText(unicode);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void RichText::SetText(const wchar_t *text)
{
// reset the formatting stream
m_FormatStream.RemoveAll();
TFormatStream stream;
stream.color = GetFgColor();
stream.fade.flFadeLength = -1.0f;
stream.fade.flFadeStartTime = 0.0f;
stream.pixelsIndent = 0;
stream.textStreamIndex = 0;
stream.textClickable = false;
m_FormatStream.AddToTail(stream);
// set the new text stream
m_TextStream.RemoveAll();
if ( text && *text )
{
int textLen = wcslen(text) + 1;
m_TextStream.EnsureCapacity(textLen);
for(int i = 0; i < textLen; i++)
{
m_TextStream.AddToTail(text[i]);
}
}
GotoTextStart();
SelectNone();
// blow away the line breaks list
InvalidateLineBreakStream();
InvalidateLayout();
}
//-----------------------------------------------------------------------------
// Purpose: Given cursor's position in the text buffer, convert it to
// the local window's x and y pixel coordinates
// Input: cursorPos: cursor index
// Output: cx, cy, the corresponding coords in the local window
//-----------------------------------------------------------------------------
void RichText::CursorToPixelSpace(int cursorPos, int &cx, int &cy)
{
int yStart = _drawOffsetY;
int x = _drawOffsetX, y = yStart;
_pixelsIndent = 0;
int lineBreakIndexIndex = 0;
for (int i = GetStartDrawIndex(lineBreakIndexIndex); i < m_TextStream.Count(); i++)
{
wchar_t ch = m_TextStream[i];
// if we've found the position, break
if (cursorPos == i)
{
// if we've passed a line break go to that
if (m_LineBreaks[lineBreakIndexIndex] == i)
{
// add another line
AddAnotherLine(x, y);
lineBreakIndexIndex++;
}
break;
}
// if we've passed a line break go to that
if (m_LineBreaks[lineBreakIndexIndex] == i)
{
// add another line
AddAnotherLine(x, y);
lineBreakIndexIndex++;
}
// add to the current position
x += surface()->GetCharacterWidth(_font, ch);
}
cx = x;
cy = y;
}
//-----------------------------------------------------------------------------
// Purpose: Converts local pixel coordinates to an index in the text buffer
//-----------------------------------------------------------------------------
int RichText::PixelToCursorSpace(int cx, int cy)
{
int fontTall = GetLineHeight();
// where to start reading
int yStart = _drawOffsetY;
int x = _drawOffsetX, y = yStart;
_pixelsIndent = 0;
int lineBreakIndexIndex = 0;
int startIndex = GetStartDrawIndex(lineBreakIndexIndex);
if (_recalcSavedRenderState)
{
RecalculateDefaultState(startIndex);
}
_pixelsIndent = m_CachedRenderState.pixelsIndent;
_currentTextClickable = m_CachedRenderState.textClickable;
TRenderState renderState = m_CachedRenderState;
bool onRightLine = false;
int i;
for (i = startIndex; i < m_TextStream.Count(); i++)
{
wchar_t ch = m_TextStream[i];
renderState.x = x;
if ( UpdateRenderState( i, renderState ) )
{
x = renderState.x;
}
// if we are on the right line but off the end of if put the cursor at the end of the line
if (m_LineBreaks[lineBreakIndexIndex] == i)
{
// add another line
AddAnotherLine(x, y);
lineBreakIndexIndex++;
if (onRightLine)
break;
}
// check to see if we're on the right line
if (cy < yStart)
{
// cursor is above panel
onRightLine = true;
}
else if (cy >= y && (cy < (y + fontTall + _drawOffsetY)))
{
onRightLine = true;
}
int wide = surface()->GetCharacterWidth(_font, ch);
// if we've found the position, break
if (onRightLine)
{
if (cx > GetWide()) // off right side of window
{
}
else if (cx < (_drawOffsetX + renderState.pixelsIndent) || cy < yStart) // off left side of window
{
// Msg( "PixelToCursorSpace() off left size, returning %d '%c'\n", i, m_TextStream[i] );
return i; // move cursor one to left
}
if (cx >= x && cx < (x + wide))
{
// check which side of the letter they're on
if (cx < (x + (wide * 0.5))) // left side
{
// Msg( "PixelToCursorSpace() on the left size, returning %d '%c'\n", i, m_TextStream[i] );
return i;
}
else // right side
{
// Msg( "PixelToCursorSpace() on the right size, returning %d '%c'\n", i + 1, m_TextStream[i + 1] );
return i + 1;
}
}
}
x += wide;
}
// Msg( "PixelToCursorSpace() never hit, returning %d\n", i );
return i;
}
//-----------------------------------------------------------------------------
// Purpose: Draws a string of characters in the panel
// Input: iFirst - Index of the first character to draw
// iLast - Index of the last character to draw
// renderState - Render state to use
// font- font to use
// Output: returns the width of the character drawn
//-----------------------------------------------------------------------------
int RichText::DrawString(int iFirst, int iLast, TRenderState &renderState, HFont font)
{
// VPROF( "RichText::DrawString" );
// Calculate the render size
int fontTall = surface()->GetFontTall(font);
// BUGBUG John: This won't exactly match the rendered size
int charWide = 0;
for ( int i = iFirst; i <= iLast; i++ )
{
wchar_t ch = m_TextStream[i];
#if USE_GETKERNEDCHARWIDTH
wchar_t chBefore = 0;
wchar_t chAfter = 0;
if ( i > 0 )
chBefore = m_TextStream[i-1];
if ( i < iLast )
chAfter = m_TextStream[i+1];
float flWide = 0.0f, flabcA = 0.0f;
surface()->GetKernedCharWidth(font, ch, chBefore, chAfter, flWide, flabcA);
if ( ch == L' ' )
flWide = ceil( flWide );
charWide += floor( flWide + 0.6 );
#else
charWide += surface()->GetCharacterWidth(font, ch);
#endif
}
// draw selection, if any
int selection0 = -1, selection1 = -1;
GetSelectedRange(selection0, selection1);
if (iFirst >= selection0 && iFirst < selection1)
{
// draw background selection color
surface()->DrawSetColor(_selectionColor);
surface()->DrawFilledRect(renderState.x, renderState.y, renderState.x + charWide, renderState.y + 1 + fontTall);
// reset text color
surface()->DrawSetTextColor(_selectionTextColor);
m_bAllTextAlphaIsZero = false;
}
else
{
surface()->DrawSetTextColor(renderState.textColor);
}
if ( renderState.textColor.a() != 0 )
{
m_bAllTextAlphaIsZero = false;
surface()->DrawSetTextPos(renderState.x, renderState.y);
surface()->DrawPrintText(&m_TextStream[iFirst], iLast - iFirst + 1);
}
return charWide;
}
//-----------------------------------------------------------------------------
// Purpose: Finish drawing url
//-----------------------------------------------------------------------------
void RichText::FinishingURL(int x, int y)
{
// finishing URL
if ( _clickableTextPanels.IsValidIndex( _clickableTextIndex ) )
{
ClickPanel *clickPanel = _clickableTextPanels[ _clickableTextIndex ];
int px, py;
clickPanel->GetPos(px, py);
int fontTall = GetLineHeight();
clickPanel->SetSize( MAX( x - px, 6 ), y - py + fontTall );
clickPanel->SetVisible(true);
// if we haven't actually advanced any, step back and ignore this one
// this is probably a data input problem though, need to find root cause
if ( x - px <= 0 )
{
--_clickableTextIndex;
clickPanel->SetVisible(false);
}
}
}
void RichText::CalculateFade( TRenderState &renderState )
{
if ( m_FormatStream.IsValidIndex( renderState.formatStreamIndex ) )
{
if ( m_bResetFades == false )
{
if ( m_FormatStream[renderState.formatStreamIndex].fade.flFadeLength != -1.0f )
{
float frac = ( m_FormatStream[renderState.formatStreamIndex].fade.flFadeStartTime - system()->GetCurrentTime() ) / m_FormatStream[renderState.formatStreamIndex].fade.flFadeLength;
int alpha = frac * m_FormatStream[renderState.formatStreamIndex].fade.iOriginalAlpha;
alpha = clamp( alpha, 0, m_FormatStream[renderState.formatStreamIndex].fade.iOriginalAlpha );
renderState.textColor.SetColor( renderState.textColor.r(), renderState.textColor.g(), renderState.textColor.b(), alpha );
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Draws the text in the panel
//-----------------------------------------------------------------------------
void RichText::Paint()
{
// Assume the worst
m_bAllTextAlphaIsZero = true;
HFont hFontCurrent = _font;
// hide all the clickable panels until we know where they are to reside
for (int j = 0; j < _clickableTextPanels.Count(); j++)
{
_clickableTextPanels[j]->SetVisible(false);
}
if ( !HasText() )
return;
int wide, tall;
GetSize( wide, tall );
int lineBreakIndexIndex = 0;
int startIndex = GetStartDrawIndex(lineBreakIndexIndex);
_currentTextClickable = false;
_clickableTextIndex = GetClickableTextIndexStart(startIndex);
// recalculate and cache the render state at the render start
if (_recalcSavedRenderState)
{
RecalculateDefaultState(startIndex);
}
// copy off the cached render state
TRenderState renderState = m_CachedRenderState;
_pixelsIndent = m_CachedRenderState.pixelsIndent;
_currentTextClickable = m_CachedRenderState.textClickable;
renderState.textClickable = _currentTextClickable;
if ( m_FormatStream.IsValidIndex( renderState.formatStreamIndex ) )
renderState.textColor = m_FormatStream[renderState.formatStreamIndex].color;
CalculateFade( renderState );
renderState.formatStreamIndex++;
if ( _currentTextClickable )
{
_clickableTextIndex = startIndex;
}
// where to start drawing
renderState.x = _drawOffsetX + _pixelsIndent;
renderState.y = _drawOffsetY;
// draw the text
int selection0 = -1, selection1 = -1;
GetSelectedRange(selection0, selection1);
surface()->DrawSetTextFont( hFontCurrent );
for (int i = startIndex; i < m_TextStream.Count() && renderState.y < tall; )
{
// 1.
// Update our current render state based on the formatting and color streams,
// this has to happen if it's our very first iteration, or if we are actually changing
// state.
int nXBeforeStateChange = renderState.x;
if ( UpdateRenderState(i, renderState) || i == startIndex )
{
// check for url state change
if (renderState.textClickable != _currentTextClickable)
{
if (renderState.textClickable)
{
// entering new URL
_clickableTextIndex++;
hFontCurrent = m_hFontUnderline;
surface()->DrawSetTextFont( hFontCurrent );
// set up the panel
ClickPanel *clickPanel = _clickableTextPanels.IsValidIndex( _clickableTextIndex ) ? _clickableTextPanels[_clickableTextIndex] : NULL;
if (clickPanel)
{
clickPanel->SetPos(renderState.x, renderState.y);
}
}
else
{
FinishingURL(nXBeforeStateChange, renderState.y);
hFontCurrent = _font;
surface()->DrawSetTextFont( hFontCurrent );
}
_currentTextClickable = renderState.textClickable;
}
}
// 2.
// if we've passed a line break go to that
if ( m_LineBreaks.IsValidIndex( lineBreakIndexIndex ) && m_LineBreaks[lineBreakIndexIndex] <= i )
{
if (_currentTextClickable)
{
FinishingURL(renderState.x, renderState.y);
}
// add another line
AddAnotherLine(renderState.x, renderState.y);
lineBreakIndexIndex++;
// Skip white space unless the previous line ended from the hard carriage return
if ( i && ( m_TextStream[i-1] != '\n' ) && ( m_TextStream[i-1] != '\r') )
{
while ( m_TextStream[i] == L' ' )
{
if ( i+1 < m_TextStream.Count() )
++i;
else
break;
}
}
if (renderState.textClickable)
{
// move to the next URL
_clickableTextIndex++;
ClickPanel *clickPanel = _clickableTextPanels.IsValidIndex( _clickableTextIndex ) ? _clickableTextPanels[_clickableTextIndex] : NULL;
if (clickPanel)
{
clickPanel->SetPos(renderState.x, renderState.y);
}
}
}
// 3.
// Calculate the range of text to draw all at once
int iLim = m_TextStream.Count();
// Stop at the next format change
if ( m_FormatStream.IsValidIndex(renderState.formatStreamIndex) &&
m_FormatStream[renderState.formatStreamIndex].textStreamIndex < iLim &&
m_FormatStream[renderState.formatStreamIndex].textStreamIndex >= i &&
m_FormatStream[renderState.formatStreamIndex].textStreamIndex )
{
iLim = m_FormatStream[renderState.formatStreamIndex].textStreamIndex;
}
// Stop at the next line break
if ( m_LineBreaks.IsValidIndex( lineBreakIndexIndex ) && m_LineBreaks[lineBreakIndexIndex] < iLim )
iLim = m_LineBreaks[lineBreakIndexIndex];
// Stop when entering or exiting the selected range
if ( i < selection0 && iLim >= selection0 )
iLim = selection0;
if ( i >= selection0 && i < selection1 && iLim >= selection1 )
iLim = selection1;
// Handle non-drawing characters specially
for ( int iT = i; iT < iLim; iT++ )
{
if ( iswcntrl(m_TextStream[iT]) )
{
iLim = iT;
break;
}
}
// 4.
// Draw the current text range
if ( iLim <= i )
{
if ( m_TextStream[i] == '\t' )
{
int dxTabWidth = 8 * surface()->GetCharacterWidth(hFontCurrent, ' ');
dxTabWidth = MAX( 1, dxTabWidth );
renderState.x = ( dxTabWidth * ( 1 + ( renderState.x / dxTabWidth ) ) );
}
i++;
}
else
{
renderState.x += DrawString(i, iLim - 1, renderState, hFontCurrent );
i = iLim;
}
}
if (renderState.textClickable)
{
FinishingURL(renderState.x, renderState.y);
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int RichText::GetClickableTextIndexStart(int startIndex)
{
// cycle to the right url panel for what is visible after the startIndex.
for (int i = 0; i < _clickableTextPanels.Count(); i++)
{
if (_clickableTextPanels[i]->GetViewTextIndex() >= startIndex)
{
return i - 1;
}
}
return -1;
}
//-----------------------------------------------------------------------------
// Purpose: Recalcultes the formatting state from the specified index
//-----------------------------------------------------------------------------
void RichText::RecalculateDefaultState(int startIndex)
{
if (!HasText() )
return;
Assert(startIndex < m_TextStream.Count());
m_CachedRenderState.textColor = GetFgColor();
_pixelsIndent = 0;
_currentTextClickable = false;
_clickableTextIndex = GetClickableTextIndexStart(startIndex);
// find where in the formatting stream we need to be
GenerateRenderStateForTextStreamIndex(startIndex, m_CachedRenderState);
_recalcSavedRenderState = false;
}
//-----------------------------------------------------------------------------
// Purpose: updates a render state based on the formatting and color streams
// Output: true if we changed the render state
//-----------------------------------------------------------------------------
bool RichText::UpdateRenderState(int textStreamPos, TRenderState &renderState)
{
// check the color stream
if (m_FormatStream.IsValidIndex(renderState.formatStreamIndex) &&
m_FormatStream[renderState.formatStreamIndex].textStreamIndex == textStreamPos)
{
// set the current formatting
renderState.textColor = m_FormatStream[renderState.formatStreamIndex].color;
renderState.textClickable = m_FormatStream[renderState.formatStreamIndex].textClickable;
CalculateFade( renderState );
int indentChange = m_FormatStream[renderState.formatStreamIndex].pixelsIndent - renderState.pixelsIndent;
renderState.pixelsIndent = m_FormatStream[renderState.formatStreamIndex].pixelsIndent;
if (indentChange)
{
renderState.x = renderState.pixelsIndent + _drawOffsetX;
}
//!! for supporting old functionality, store off state in globals
_pixelsIndent = renderState.pixelsIndent;
// move to the next position in the color stream
renderState.formatStreamIndex++;
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Returns the index in the format stream for the specified text stream index
//-----------------------------------------------------------------------------
int RichText::FindFormatStreamIndexForTextStreamPos(int textStreamIndex)
{
int formatStreamIndex = 0;
for (; m_FormatStream.IsValidIndex(formatStreamIndex); formatStreamIndex++)
{
if (m_FormatStream[formatStreamIndex].textStreamIndex > textStreamIndex)
break;
}
// step back to the color change before the new line
formatStreamIndex--;
if (!m_FormatStream.IsValidIndex(formatStreamIndex))
{
formatStreamIndex = 0;
}
return formatStreamIndex;
}
//-----------------------------------------------------------------------------
// Purpose: Generates a base renderstate given a index into the text stream