DecoratorDefender.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "DecoratorDefender.h"
  29. #include <RmlUi/Core/Element.h>
  30. #include <RmlUi/Core/Texture.h>
  31. #include <RmlUi/Core/Math.h>
  32. #include <ShellOpenGL.h>
  33. DecoratorDefender::~DecoratorDefender()
  34. {
  35. }
  36. bool DecoratorDefender::Initialise(const Rml::Core::String& image_source, const Rml::Core::String& image_path)
  37. {
  38. image_index = LoadTexture(image_source, image_path);
  39. if (image_index == -1)
  40. {
  41. return false;
  42. }
  43. return true;
  44. }
  45. /// Called on a decorator to generate any required per-element data for a newly decorated element.
  46. Rml::Core::DecoratorDataHandle DecoratorDefender::GenerateElementData(Rml::Core::Element* RMLUI_UNUSED_PARAMETER(element)) const
  47. {
  48. RMLUI_UNUSED(element);
  49. return Rml::Core::Decorator::INVALID_DECORATORDATAHANDLE;
  50. }
  51. // Called to release element data generated by this decorator.
  52. void DecoratorDefender::ReleaseElementData(Rml::Core::DecoratorDataHandle RMLUI_UNUSED_PARAMETER(element_data)) const
  53. {
  54. RMLUI_UNUSED(element_data);
  55. }
  56. // Called to render the decorator on an element.
  57. void DecoratorDefender::RenderElement(Rml::Core::Element* element, Rml::Core::DecoratorDataHandle RMLUI_UNUSED_PARAMETER(element_data)) const
  58. {
  59. RMLUI_UNUSED(element_data);
  60. Rml::Core::Vector2f position = element->GetAbsoluteOffset(Rml::Core::Box::PADDING).Round();
  61. Rml::Core::Vector2f size = element->GetBox().GetSize(Rml::Core::Box::PADDING).Round();
  62. glEnable(GL_TEXTURE_2D);
  63. glBindTexture(GL_TEXTURE_2D, (GLuint) GetTexture(image_index)->GetHandle(element->GetRenderInterface()));
  64. Rml::Core::Colourb colour = element->GetProperty< Rml::Core::Colourb >("color");
  65. glColor4ubv(colour);
  66. glBegin(GL_QUADS);
  67. glVertex2f(position.x, position.y);
  68. glTexCoord2f(0, 1);
  69. glVertex2f(position.x, position.y + size.y);
  70. glTexCoord2f(1, 1);
  71. glVertex2f(position.x + size.x, position.y + size.y);
  72. glTexCoord2f(1, 0);
  73. glVertex2f(position.x + size.x, position.y);
  74. glTexCoord2f(0, 0);
  75. glEnd();
  76. glColor4ub(255, 255, 255, 255);
  77. }