BaseRender.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Gwen.h"
  7. #include "Gwen/BaseRender.h"
  8. #include "Gwen/Utility.h"
  9. #include "Gwen/Platform.h"
  10. #include <math.h>
  11. #include <cmath>
  12. namespace Gwen
  13. {
  14. namespace Renderer
  15. {
  16. Base::Base()
  17. {
  18. m_RenderOffset = Gwen::Point(0, 0);
  19. m_fScale = 1.0f;
  20. }
  21. Base::~Base()
  22. {
  23. if (GetCTT())
  24. GetCTT()->ShutDown();
  25. }
  26. void Base::RenderText(Gwen::Font* pFont, Gwen::Point pos, const Gwen::String& text)
  27. {
  28. Gwen::UnicodeString str = Gwen::Utility::StringToUnicode(text);
  29. RenderText(pFont, pos, str);
  30. }
  31. Gwen::Point Base::MeasureText(Gwen::Font* pFont, const Gwen::String& text)
  32. {
  33. Gwen::UnicodeString str = Gwen::Utility::StringToUnicode(text);
  34. return MeasureText(pFont, str);
  35. }
  36. void Base::DrawLinedRect(Gwen::Rect rect)
  37. {
  38. DrawFilledRect(Gwen::Rect(rect.x, rect.y, rect.w, 1));
  39. DrawFilledRect(Gwen::Rect(rect.x, rect.y + rect.h - 1, rect.w, 1));
  40. DrawFilledRect(Gwen::Rect(rect.x, rect.y, 1, rect.h));
  41. DrawFilledRect(Gwen::Rect(rect.x + rect.w - 1, rect.y, 1, rect.h));
  42. };
  43. void Base::DrawPixel(int x, int y)
  44. {
  45. DrawFilledRect(Gwen::Rect(x, y, 1, 1));
  46. }
  47. void Base::DrawShavedCornerRect(Gwen::Rect rect, bool bSlight)
  48. {
  49. // Draw INSIDE the w/h.
  50. rect.w -= 1;
  51. rect.h -= 1;
  52. if (bSlight)
  53. {
  54. DrawFilledRect(Gwen::Rect(rect.x + 1, rect.y, rect.w - 1, 1));
  55. DrawFilledRect(Gwen::Rect(rect.x + 1, rect.y + rect.h, rect.w - 1, 1));
  56. DrawFilledRect(Gwen::Rect(rect.x, rect.y + 1, 1, rect.h - 1));
  57. DrawFilledRect(Gwen::Rect(rect.x + rect.w, rect.y + 1, 1, rect.h - 1));
  58. return;
  59. }
  60. DrawPixel(rect.x + 1, rect.y + 1);
  61. DrawPixel(rect.x + rect.w - 1, rect.y + 1);
  62. DrawPixel(rect.x + 1, rect.y + rect.h - 1);
  63. DrawPixel(rect.x + rect.w - 1, rect.y + rect.h - 1);
  64. DrawFilledRect(Gwen::Rect(rect.x + 2, rect.y, rect.w - 3, 1));
  65. DrawFilledRect(Gwen::Rect(rect.x + 2, rect.y + rect.h, rect.w - 3, 1));
  66. DrawFilledRect(Gwen::Rect(rect.x, rect.y + 2, 1, rect.h - 3));
  67. DrawFilledRect(Gwen::Rect(rect.x + rect.w, rect.y + 2, 1, rect.h - 3));
  68. }
  69. void Base::Translate(int& x, int& y)
  70. {
  71. x += m_RenderOffset.x;
  72. y += m_RenderOffset.y;
  73. x = std::ceil(((float)x) * m_fScale);
  74. y = std::ceil(((float)y) * m_fScale);
  75. }
  76. void Base::Translate(Gwen::Rect& rect)
  77. {
  78. Translate(rect.x, rect.y);
  79. rect.w = std::ceil(((float)rect.w) * m_fScale);
  80. rect.h = std::ceil(((float)rect.h) * m_fScale);
  81. }
  82. void Gwen::Renderer::Base::SetClipRegion(Gwen::Rect rect)
  83. {
  84. m_rectClipRegion = rect;
  85. }
  86. void Base::AddClipRegion(Gwen::Rect rect)
  87. {
  88. rect.x = m_RenderOffset.x;
  89. rect.y = m_RenderOffset.y;
  90. Gwen::Rect out = rect;
  91. if (rect.x < m_rectClipRegion.x)
  92. {
  93. out.w -= (m_rectClipRegion.x - out.x);
  94. out.x = m_rectClipRegion.x;
  95. }
  96. if (rect.y < m_rectClipRegion.y)
  97. {
  98. out.h -= (m_rectClipRegion.y - out.y);
  99. out.y = m_rectClipRegion.y;
  100. }
  101. if (rect.x + rect.w > m_rectClipRegion.x + m_rectClipRegion.w)
  102. {
  103. out.w = (m_rectClipRegion.x + m_rectClipRegion.w) - out.x;
  104. }
  105. if (rect.y + rect.h > m_rectClipRegion.y + m_rectClipRegion.h)
  106. {
  107. out.h = (m_rectClipRegion.y + m_rectClipRegion.h) - out.y;
  108. }
  109. m_rectClipRegion = out;
  110. }
  111. const Gwen::Rect& Base::ClipRegion() const
  112. {
  113. return m_rectClipRegion;
  114. }
  115. bool Base::ClipRegionVisible()
  116. {
  117. if (m_rectClipRegion.w <= 0 || m_rectClipRegion.h <= 0)
  118. return false;
  119. return true;
  120. }
  121. void Base::DrawMissingImage(Gwen::Rect pTargetRect)
  122. {
  123. SetDrawColor(Colors::Red);
  124. DrawFilledRect(pTargetRect);
  125. }
  126. /*
  127. If they haven't defined these font functions in their renderer code
  128. we just draw some rects where the letters would be to give them an idea.
  129. */
  130. void Base::RenderText(Gwen::Font* pFont, Gwen::Point pos, const Gwen::UnicodeString& text)
  131. {
  132. float fSize = pFont->size * Scale();
  133. for (float i = 0; i < text.length(); i++)
  134. {
  135. wchar_t chr = text[i];
  136. if (chr == ' ') continue;
  137. Gwen::Rect r(pos.x + i * fSize * 0.4, pos.y, fSize * 0.4 - 1, fSize);
  138. /*
  139. This isn't important, it's just me messing around changing the
  140. shape of the rect based on the letter.. just for fun.
  141. */
  142. if (chr == 'l' || chr == 'i' || chr == '!' || chr == 't')
  143. {
  144. r.w = 1;
  145. }
  146. else if (chr >= 'a' && chr <= 'z')
  147. {
  148. r.y += fSize * 0.5f;
  149. r.h -= fSize * 0.4f;
  150. }
  151. else if (chr == '.' || chr == ',')
  152. {
  153. r.x += 2;
  154. r.y += r.h - 2;
  155. r.w = 2;
  156. r.h = 2;
  157. }
  158. else if (chr == '\'' || chr == '`' || chr == '"')
  159. {
  160. r.x += 3;
  161. r.w = 2;
  162. r.h = 2;
  163. }
  164. if (chr == 'o' || chr == 'O' || chr == '0')
  165. DrawLinedRect(r);
  166. else
  167. DrawFilledRect(r);
  168. }
  169. }
  170. Gwen::Point Base::MeasureText(Gwen::Font* pFont, const Gwen::UnicodeString& text)
  171. {
  172. Gwen::Point p;
  173. p.x = pFont->size * Scale() * (float)text.length() * 0.4;
  174. p.y = pFont->size * Scale();
  175. return p;
  176. }
  177. } // namespace Renderer
  178. } // namespace Gwen