guiBackgroundImage.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. Part of Minetest
  3. Copyright (C) 2013 RealBadAngel, Maciej Kasatkin <mk@realbadangel.pl>
  4. Permission to use, copy, modify, and distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "guiBackgroundImage.h"
  16. #include "client/guiscalingfilter.h"
  17. #include "log.h"
  18. GUIBackgroundImage::GUIBackgroundImage(gui::IGUIEnvironment *env,
  19. gui::IGUIElement *parent, s32 id, const core::rect<s32> &rectangle,
  20. const std::string &name, const core::rect<s32> &middle,
  21. ISimpleTextureSource *tsrc, bool autoclip) :
  22. gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle),
  23. m_name(name), m_middle(middle), m_tsrc(tsrc), m_autoclip(autoclip)
  24. {
  25. }
  26. void GUIBackgroundImage::draw()
  27. {
  28. if (!IsVisible)
  29. return;
  30. video::ITexture *texture = m_tsrc->getTexture(m_name);
  31. if (!texture) {
  32. errorstream << "GUIBackgroundImage::draw() Unable to load texture:"
  33. << std::endl;
  34. errorstream << "\t" << m_name << std::endl;
  35. return;
  36. }
  37. core::rect<s32> rect = AbsoluteRect;
  38. if (m_autoclip)
  39. rect.LowerRightCorner += Parent->getAbsoluteClippingRect().getSize();
  40. video::IVideoDriver *driver = Environment->getVideoDriver();
  41. core::rect<s32> srcrect(core::position2d<s32>(0, 0),
  42. core::dimension2di(texture->getOriginalSize()));
  43. if (m_middle.getArea() == 0) {
  44. const video::SColor color(255, 255, 255, 255);
  45. const video::SColor colors[] = {color, color, color, color};
  46. draw2DImageFilterScaled(driver, texture, rect, srcrect, nullptr, colors, true);
  47. } else {
  48. draw2DImage9Slice(driver, texture, rect, srcrect, m_middle);
  49. }
  50. IGUIElement::draw();
  51. }