BaseRender.cpp 4.9 KB

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