Clone of Akilla's ac2d @ https://github.com/deregtd/AC2D

cStaticText.h 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include "WindowLib/cWindow.h"
  3. extern float g_fFontWidth[25][256];
  4. enum eTextMode {
  5. eLeft,
  6. eRight,
  7. eCenter,
  8. eTop,
  9. eBottom
  10. };
  11. class cStaticText : public CWindow, private RenderEventAbstractor< cStaticText > {
  12. public:
  13. cStaticText()
  14. {
  15. m_Color[0] = 0xFF;
  16. m_Color[1] = 0xFF;
  17. m_Color[2] = 0xFF;
  18. m_dwFontSize = 11;
  19. m_HTextAlign = eLeft;
  20. m_bMultiline = false;
  21. m_VTextAlign = eCenter;
  22. AddRenderEventHandler( *(RenderEventAbstractor< cStaticText > *)this );
  23. }
  24. private:
  25. bool RenderEventAbstractor< cStaticText >::OnRender( IWindow & Window, double TimeSlice )
  26. {
  27. float iLeft = GetAbsoluteLeft(),
  28. iRight = iLeft + GetWidth(),
  29. iTop = GetAbsoluteTop(),
  30. iBottom = iTop + GetHeight();
  31. glBindTexture(GL_TEXTURE_2D, 0);
  32. glListBase(0x01000000 + (m_dwFontSize << 8));
  33. glColor3ub(m_Color[0],m_Color[1],m_Color[2]);
  34. float fX = 0, fY = 0;
  35. if (m_HTextAlign == eLeft) fX = iLeft;
  36. if (m_HTextAlign == eCenter) fX = iLeft + (iRight - iLeft)/2 - m_fTextWidth/2;
  37. if (m_HTextAlign == eRight) fX = iRight - m_fTextWidth;
  38. if (m_VTextAlign == eTop) fY = iTop + m_dwFontSize;
  39. if (m_VTextAlign == eCenter) fY = iTop + (iBottom - iTop)/2 + m_dwFontSize/2 - 1;
  40. if (m_VTextAlign == eBottom) fY = iBottom;
  41. glRasterPos2f(fX, fY);
  42. glCallLists((DWORD) m_Text.size(), GL_UNSIGNED_BYTE, m_Text.c_str());
  43. return true;
  44. }
  45. public:
  46. void SetText( std::string Text )
  47. {
  48. m_Text = Text;
  49. CalcTextSize();
  50. }
  51. void CalcTextSize()
  52. {
  53. m_fTextWidth = 0;
  54. for (int i=0; i<(int)m_Text.size(); i++)
  55. m_fTextWidth += g_fFontWidth[m_dwFontSize][m_Text[i]];
  56. }
  57. std::string GetText() const
  58. {
  59. return m_Text;
  60. }
  61. void SetTextColor(DWORD Color)
  62. {
  63. m_Color[0] = (BYTE) Color;
  64. m_Color[1] = (BYTE) (Color >> 8);
  65. m_Color[2] = (BYTE) (Color >> 16);
  66. }
  67. void SetMultiline(bool ML)
  68. {
  69. m_bMultiline = ML;
  70. }
  71. void SetTextHAlign(eTextMode NewMode)
  72. {
  73. m_HTextAlign = NewMode;
  74. }
  75. void SetTextVAlign(eTextMode NewMode)
  76. {
  77. m_VTextAlign = NewMode;
  78. }
  79. void SetTextSize(DWORD FontSize)
  80. {
  81. m_dwFontSize = FontSize;
  82. CalcTextSize();
  83. }
  84. private:
  85. DWORD m_dwFontSize;
  86. std::string m_Text;
  87. bool m_bMultiline;
  88. BYTE m_Color[3];
  89. eTextMode m_HTextAlign, m_VTextAlign;
  90. float m_fTextWidth;
  91. };