Texturing.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #pragma once
  2. #ifndef GWEN_SKINS_TEXTURING_H
  3. #define GWEN_SKINS_TEXTURING_H
  4. #include "Gwen/Gwen.h"
  5. #include "Gwen/Texture.h"
  6. namespace Gwen
  7. {
  8. namespace Skin
  9. {
  10. namespace Texturing
  11. {
  12. struct Single
  13. {
  14. Single()
  15. {
  16. texture = NULL;
  17. }
  18. void Init( Texture* pTexture, float x, float y, float w, float h )
  19. {
  20. texture = pTexture;
  21. float texw = texture->width;
  22. float texh = texture->height;
  23. uv[0] = x / texw;
  24. uv[1] = y / texh;
  25. uv[2] = (x+w) / texw;
  26. uv[3] = (y+h) / texh;
  27. }
  28. void Draw( Gwen::Renderer::Base* render, Gwen::Rect r, const Gwen::Color& col = Gwen::Colors::White )
  29. {
  30. render->SetDrawColor( col );
  31. render->DrawTexturedRect( texture, r, uv[0], uv[1],uv[2], uv[3] );
  32. }
  33. Texture* texture;
  34. float uv[4];
  35. };
  36. struct Bordered
  37. {
  38. Bordered()
  39. {
  40. texture = NULL;
  41. }
  42. void Init( Texture* pTexture, float x, float y, float w, float h, Margin in_margin, float DrawMarginScale = 1.0f )
  43. {
  44. texture = pTexture;
  45. margin = in_margin;
  46. SetRect( 0, x, y, margin.left, margin.top );
  47. SetRect( 1, x+margin.left, y, w - margin.left - margin.right - 1, margin.top );
  48. SetRect( 2, (x + w) - margin.right, y, margin.right, margin.top );
  49. SetRect( 3, x, y+margin.top, margin.left, h - margin.top - margin.bottom - 1 );
  50. SetRect( 4, x+margin.left, y+margin.top, w - margin.left - margin.right - 1, h - margin.top - margin.bottom - 1 );
  51. SetRect( 5, (x + w) - margin.right, y+margin.top, margin.right, h - margin.top - margin.bottom - 1 );
  52. SetRect( 6, x, (y+h)-margin.bottom, margin.left, margin.bottom );
  53. SetRect( 7, x+margin.left, (y+h)-margin.bottom, w - margin.left - margin.right - 1, margin.bottom );
  54. SetRect( 8, (x + w) - margin.right, (y+h)-margin.bottom, margin.right, margin.bottom );
  55. margin.left *= DrawMarginScale;
  56. margin.right *= DrawMarginScale;
  57. margin.top *= DrawMarginScale;
  58. margin.bottom *= DrawMarginScale;
  59. width = w - x;
  60. height = h - y;
  61. }
  62. void SetRect( int iNum, float x, float y, float w, float h )
  63. {
  64. float texw = texture->width;
  65. float texh = texture->height;
  66. //x -= 1.0f;
  67. //y -= 1.0f;
  68. rects[iNum].uv[0] = x / texw;
  69. rects[iNum].uv[1] = y / texh;
  70. rects[iNum].uv[2] = (x+w) / texw;
  71. rects[iNum].uv[3] = (y+h) / texh;
  72. // rects[iNum].uv[0] += 1.0f / texture->width;
  73. // rects[iNum].uv[1] += 1.0f / texture->width;
  74. }
  75. void Draw( Gwen::Renderer::Base* render, Gwen::Rect r, const Gwen::Color& col = Gwen::Colors::White )
  76. {
  77. render->SetDrawColor( col );
  78. if ( r.w < width && r.h < height )
  79. {
  80. render->DrawTexturedRect( texture,
  81. r,
  82. rects[0].uv[0], rects[0].uv[1], rects[8].uv[2], rects[8].uv[3] );
  83. return;
  84. }
  85. DrawRect( render, 0, r.x, r.y, margin.left, margin.top );
  86. DrawRect( render, 1, r.x + margin.left, r.y, r.w - margin.left - margin.right, margin.top );
  87. DrawRect( render, 2, (r.x + r.w) - margin.right, r.y, margin.right, margin.top );
  88. DrawRect( render, 3, r.x, r.y+margin.top, margin.left, r.h - margin.top - margin.bottom );
  89. DrawRect( render, 4, r.x + margin.left, r.y+margin.top, r.w - margin.left - margin.right, r.h - margin.top - margin.bottom );
  90. DrawRect( render, 5, (r.x + r.w) - margin.right, r.y+margin.top, margin.right, r.h - margin.top - margin.bottom );
  91. DrawRect( render, 6, r.x, (r.y+r.h) - margin.bottom, margin.left, margin.bottom );
  92. DrawRect( render, 7, r.x + margin.left, (r.y+r.h) - margin.bottom, r.w - margin.left - margin.right, margin.bottom );
  93. DrawRect( render, 8, (r.x + r.w) - margin.right, (r.y+r.h) - margin.bottom, margin.right, margin.bottom );
  94. }
  95. void DrawRect( Gwen::Renderer::Base* render, int i, int x, int y, int w, int h )
  96. {
  97. render->DrawTexturedRect( texture,
  98. Gwen::Rect( x, y, w, h ),
  99. rects[i].uv[0], rects[i].uv[1], rects[i].uv[2], rects[i].uv[3] );
  100. }
  101. Texture* texture;
  102. struct SubRect
  103. {
  104. float uv[4];
  105. };
  106. SubRect rects[9];
  107. Margin margin;
  108. float width;
  109. float height;
  110. };
  111. }
  112. }
  113. }
  114. #endif