UIOffscreenView.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_widgets.h>
  23. #include "../Graphics/Graphics.h"
  24. #include "../Input/Input.h"
  25. #include "../Input/InputEvents.h"
  26. #include "UI.h"
  27. #include "UIOffscreenView.h"
  28. using namespace tb;
  29. namespace Atomic
  30. {
  31. MODIFIER_KEYS GetModifierKeys(int qualifiers, bool superKey);
  32. SPECIAL_KEY GetSpecialKey(int keycode);
  33. UIOffscreenView::UIOffscreenView(Context* context) : UIWidget(context, false)
  34. {
  35. widget_ = new TBWidget();
  36. widget_->SetDelegate(this);
  37. widget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
  38. UI* ui = GetSubsystem<UI>();
  39. ui->WrapWidget(this, widget_);
  40. texture_ = new Texture2D(context_);
  41. clearRenderTargetEachFrame_ = true;
  42. // Set initial size for view
  43. SetSize(512, 512);
  44. // Set initial filtering mode to bilinear.
  45. GetTexture2D()->SetFilterMode(Atomic::FILTER_BILINEAR);
  46. // Add our self to the ui subsystem.
  47. ui->GetOffscreenViews().Insert(this);
  48. }
  49. UIOffscreenView::~UIOffscreenView()
  50. {
  51. UI* ui = GetSubsystem<UI>();
  52. // Remove our self from the ui subsystem, if it's still around.
  53. if (ui)
  54. ui->GetOffscreenViews().Erase(this);
  55. }
  56. void UIOffscreenView::SetSize(int width, int height)
  57. {
  58. Graphics* graphics = GetSubsystem<Graphics>();
  59. widget_->SetSize(width, height);
  60. GetTexture2D()->SetSize(width, height, graphics->GetRGBAFormat(), Atomic::TEXTURE_RENDERTARGET);
  61. }
  62. Material* UIOffscreenView::GetMaterial()
  63. {
  64. if (material_.Null())
  65. {
  66. // If the convenience material doesn't exist, then create it.
  67. ResourceCache* cache = GetSubsystem<ResourceCache>();
  68. material_ = new Material(context_);
  69. material_->SetTechnique(0, cache->GetResource<Technique>("Techniques/Diff.xml"));
  70. material_->SetTexture(Atomic::TU_DIFFUSE, texture_);
  71. }
  72. return material_;
  73. }
  74. void UIOffscreenView::RegisterInput(Camera* camera, Octree* octree, Drawable* drawable, const IntRect& rect)
  75. {
  76. inputCamera_ = camera;
  77. inputOctree_ = octree;
  78. inputDrawable_ = drawable;
  79. inputRect_ = rect;
  80. }
  81. void UIOffscreenView::UnregisterInput()
  82. {
  83. inputCamera_.Reset();
  84. inputOctree_.Reset();
  85. inputDrawable_.Reset();
  86. inputRect_ = IntRect::ZERO;
  87. }
  88. bool UIOffscreenView::SavePNG(Serializer& dest) const
  89. {
  90. Image image(context_);
  91. if (!image.SetSize(GetWidth(), GetHeight(), 4))
  92. return false;
  93. if (!GetTexture2D()->GetData(0, image.GetData()))
  94. return false;
  95. if (!image.Save(dest))
  96. return false;
  97. return true;
  98. }
  99. bool UIOffscreenView::SavePNG(const String& fileName) const
  100. {
  101. File outFile(context_, fileName, FILE_WRITE);
  102. if (!outFile.IsOpen())
  103. return false;
  104. return SavePNG(outFile);
  105. }
  106. void UIOffscreenView::InvokeRightPointerDown(int x, int y, int click_count, int qualifiers, bool superKeyDown)
  107. {
  108. widget_->InvokeRightPointerDown(x, y, click_count, GetModifierKeys(qualifiers, superKeyDown));
  109. }
  110. void UIOffscreenView::InvokeRightPointerUp(int x, int y, int qualifiers, bool superKeyDown)
  111. {
  112. widget_->InvokeRightPointerUp(x, y, GetModifierKeys(qualifiers, superKeyDown));
  113. }
  114. void UIOffscreenView::InvokePointerDown(int x, int y, int click_count, int qualifiers, bool touch, int touchId, bool superKeyDown)
  115. {
  116. widget_->InvokePointerDown(x, y, click_count, GetModifierKeys(qualifiers, superKeyDown), touch, touchId);
  117. }
  118. void UIOffscreenView::InvokePointerUp(int x, int y, int qualifiers, bool touch, int touchId, bool superKeyDown)
  119. {
  120. widget_->InvokePointerUp(x, y, GetModifierKeys(qualifiers, superKeyDown), touch, touchId);
  121. }
  122. void UIOffscreenView::InvokePointerMove(int x, int y, int qualifiers, bool touch, int touchId, bool superKeyDown)
  123. {
  124. widget_->InvokePointerMove(x, y, GetModifierKeys(qualifiers, superKeyDown), touch, touchId);
  125. }
  126. void UIOffscreenView::InvokeWheel(int x, int y, int delta_x, int delta_y, int qualifiers, bool superKeyDown)
  127. {
  128. widget_->InvokeWheel(x, y, delta_x, delta_y, GetModifierKeys(qualifiers, superKeyDown));
  129. }
  130. bool UIOffscreenView::InvokeKey(int key, int keycode, bool down, int qualifiers, bool superKeyDown)
  131. {
  132. return widget_->InvokeKey(key, GetSpecialKey(keycode), GetModifierKeys(qualifiers, superKeyDown), down);
  133. }
  134. }