guiBox.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "guiBox.h"
  17. GUIBox::GUIBox(gui::IGUIEnvironment *env, gui::IGUIElement *parent, s32 id,
  18. const core::rect<s32> &rectangle,
  19. const std::array<video::SColor, 4> &colors,
  20. const std::array<video::SColor, 4> &bordercolors,
  21. const std::array<s32, 4> &borderwidths) :
  22. gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle),
  23. m_colors(colors),
  24. m_bordercolors(bordercolors),
  25. m_borderwidths(borderwidths)
  26. {
  27. }
  28. void GUIBox::draw()
  29. {
  30. if (!IsVisible)
  31. return;
  32. std::array<s32, 4> negative_borders = {0, 0, 0, 0};
  33. std::array<s32, 4> positive_borders = {0, 0, 0, 0};
  34. for (size_t i = 0; i <= 3; i++) {
  35. if (m_borderwidths[i] > 0)
  36. positive_borders[i] = m_borderwidths[i];
  37. else
  38. negative_borders[i] = m_borderwidths[i];
  39. }
  40. v2s32 upperleft = AbsoluteRect.UpperLeftCorner;
  41. v2s32 lowerright = AbsoluteRect.LowerRightCorner;
  42. v2s32 topleft_border = {
  43. upperleft.X - positive_borders[3],
  44. upperleft.Y - positive_borders[0]
  45. };
  46. v2s32 topleft_rect = {
  47. upperleft.X - negative_borders[3],
  48. upperleft.Y - negative_borders[0]
  49. };
  50. v2s32 lowerright_border = {
  51. lowerright.X + positive_borders[1],
  52. lowerright.Y + positive_borders[2]
  53. };
  54. v2s32 lowerright_rect = {
  55. lowerright.X + negative_borders[1],
  56. lowerright.Y + negative_borders[2]
  57. };
  58. core::rect<s32> main_rect(
  59. topleft_rect.X,
  60. topleft_rect.Y,
  61. lowerright_rect.X,
  62. lowerright_rect.Y
  63. );
  64. std::array<core::rect<s32>, 4> border_rects;
  65. border_rects[0] = core::rect<s32>(
  66. topleft_border.X,
  67. topleft_border.Y,
  68. lowerright_border.X,
  69. topleft_rect.Y
  70. );
  71. border_rects[1] = core::rect<s32>(
  72. lowerright_rect.X,
  73. topleft_rect.Y,
  74. lowerright_border.X,
  75. lowerright_rect.Y
  76. );
  77. border_rects[2] = core::rect<s32>(
  78. topleft_border.X,
  79. lowerright_rect.Y,
  80. lowerright_border.X,
  81. lowerright_border.Y
  82. );
  83. border_rects[3] = core::rect<s32>(
  84. topleft_border.X,
  85. topleft_rect.Y,
  86. topleft_rect.X,
  87. lowerright_rect.Y
  88. );
  89. video::IVideoDriver *driver = Environment->getVideoDriver();
  90. driver->draw2DRectangle(main_rect, m_colors[0], m_colors[1], m_colors[3],
  91. m_colors[2], &AbsoluteClippingRect);
  92. for (size_t i = 0; i <= 3; i++)
  93. driver->draw2DRectangle(m_bordercolors[i], border_rects[i],
  94. &AbsoluteClippingRect);
  95. IGUIElement::draw();
  96. }