UIOffscreenView.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. UIOffscreenView::UIOffscreenView(Context* context) : UIWidget(context, false)
  32. {
  33. widget_ = new TBWidget();
  34. widget_->SetDelegate(this);
  35. widget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
  36. UI* ui = GetSubsystem<UI>();
  37. ui->WrapWidget(this, widget_);
  38. texture_ = new Texture2D(context_);
  39. clearRenderTargetEachFrame_ = true;
  40. // Set initial size for view
  41. SetSize(512, 512);
  42. // Set initial filtering mode to bilinear.
  43. GetTexture2D()->SetFilterMode(Atomic::FILTER_BILINEAR);
  44. // Add our self to the ui subsystem.
  45. ui->GetOffscreenViews()->Insert(this);
  46. }
  47. UIOffscreenView::~UIOffscreenView()
  48. {
  49. UI* ui = GetSubsystem<UI>();
  50. // Remove our self from the ui subsystem, if it's still around.
  51. if (ui)
  52. ui->GetOffscreenViews()->Erase(this);
  53. }
  54. void UIOffscreenView::SetSize(int width, int height)
  55. {
  56. Graphics* graphics = GetSubsystem<Graphics>();
  57. widget_->SetSize(width, height);
  58. GetTexture2D()->SetSize(width, height, graphics->GetRGBAFormat(), Atomic::TEXTURE_RENDERTARGET);
  59. }
  60. Material* UIOffscreenView::GetMaterial()
  61. {
  62. if (material_.Null())
  63. {
  64. // If the convenience material doesn't exist, then create it.
  65. ResourceCache* cache = GetSubsystem<ResourceCache>();
  66. material_ = new Material(context_);
  67. material_->SetTechnique(0, cache->GetResource<Technique>("Techniques/Diff.xml"));
  68. material_->SetTexture(Atomic::TU_DIFFUSE, texture_);
  69. }
  70. return material_;
  71. }
  72. void UIOffscreenView::RegisterInput(Camera* camera, Octree* octree, Drawable* drawable, const IntRect& rect)
  73. {
  74. inputCamera_ = camera;
  75. inputOctree_ = octree;
  76. inputDrawable_ = drawable;
  77. inputRect_ = rect;
  78. }
  79. void UIOffscreenView::UnregisterInput()
  80. {
  81. inputCamera_.Reset();
  82. inputOctree_.Reset();
  83. inputDrawable_.Reset();
  84. inputRect_ = IntRect::ZERO;
  85. }
  86. bool UIOffscreenView::SavePNGTo(Serializer& serializer) const
  87. {
  88. Image image(context_);
  89. if (!image.SetSize(GetWidth(), GetHeight(), 4))
  90. return false;
  91. if (!GetTexture2D()->GetData(0, image.GetData()))
  92. return false;
  93. if (!image.Save(serializer))
  94. return false;
  95. return true;
  96. }
  97. bool UIOffscreenView::SavePNG(const String& fileName) const
  98. {
  99. File outFile(context_, fileName, FILE_WRITE);
  100. if (!outFile.IsOpen())
  101. return false;
  102. return SavePNGTo(outFile);
  103. }
  104. static inline MODIFIER_KEYS GetModifierKeys(int qualifiers, bool superKey)
  105. {
  106. MODIFIER_KEYS code = TB_MODIFIER_NONE;
  107. if (qualifiers & QUAL_ALT) code |= TB_ALT;
  108. if (qualifiers & QUAL_CTRL) code |= TB_CTRL;
  109. if (qualifiers & QUAL_SHIFT) code |= TB_SHIFT;
  110. if (superKey) code |= TB_SUPER;
  111. return code;
  112. }
  113. static inline SPECIAL_KEY GetSpecialKey(int keycode)
  114. {
  115. SPECIAL_KEY specialKey;
  116. switch (keycode)
  117. {
  118. case KEY_RETURN:
  119. case KEY_RETURN2:
  120. case KEY_KP_ENTER:
  121. specialKey = TB_KEY_ENTER;
  122. break;
  123. case KEY_F1:
  124. specialKey = TB_KEY_F1;
  125. break;
  126. case KEY_F2:
  127. specialKey = TB_KEY_F2;
  128. break;
  129. case KEY_F3:
  130. specialKey = TB_KEY_F3;
  131. break;
  132. case KEY_F4:
  133. specialKey = TB_KEY_F4;
  134. break;
  135. case KEY_F5:
  136. specialKey = TB_KEY_F5;
  137. break;
  138. case KEY_F6:
  139. specialKey = TB_KEY_F6;
  140. break;
  141. case KEY_F7:
  142. specialKey = TB_KEY_F7;
  143. break;
  144. case KEY_F8:
  145. specialKey = TB_KEY_F8;
  146. break;
  147. case KEY_F9:
  148. specialKey = TB_KEY_F9;
  149. break;
  150. case KEY_F10:
  151. specialKey = TB_KEY_F10;
  152. break;
  153. case KEY_F11:
  154. specialKey = TB_KEY_F11;
  155. break;
  156. case KEY_F12:
  157. specialKey = TB_KEY_F12;
  158. break;
  159. case KEY_LEFT:
  160. specialKey = TB_KEY_LEFT;
  161. break;
  162. case KEY_UP:
  163. specialKey = TB_KEY_UP;
  164. break;
  165. case KEY_RIGHT:
  166. specialKey = TB_KEY_RIGHT;
  167. break;
  168. case KEY_DOWN:
  169. specialKey = TB_KEY_DOWN;
  170. break;
  171. case KEY_PAGEUP:
  172. specialKey = TB_KEY_PAGE_UP;
  173. break;
  174. case KEY_PAGEDOWN:
  175. specialKey = TB_KEY_PAGE_DOWN;
  176. break;
  177. case KEY_HOME:
  178. specialKey = TB_KEY_HOME;
  179. break;
  180. case KEY_END:
  181. specialKey = TB_KEY_END;
  182. break;
  183. case KEY_INSERT:
  184. specialKey = TB_KEY_INSERT;
  185. break;
  186. case KEY_TAB:
  187. specialKey = TB_KEY_TAB;
  188. break;
  189. case KEY_DELETE:
  190. specialKey = TB_KEY_DELETE;
  191. break;
  192. case KEY_BACKSPACE:
  193. specialKey = TB_KEY_BACKSPACE;
  194. break;
  195. case KEY_ESCAPE:
  196. specialKey = TB_KEY_ESC;
  197. break;
  198. default:
  199. specialKey = TB_KEY_UNDEFINED;
  200. break;
  201. }
  202. return specialKey;
  203. }
  204. void UIOffscreenView::InvokeRightPointerDown(int x, int y, int click_count, int qualifiers, bool superDown)
  205. {
  206. widget_->InvokeRightPointerDown(x, y, click_count, GetModifierKeys(qualifiers, superDown));
  207. }
  208. void UIOffscreenView::InvokeRightPointerUp(int x, int y, int qualifiers, bool superDown)
  209. {
  210. widget_->InvokeRightPointerUp(x, y, GetModifierKeys(qualifiers, superDown));
  211. }
  212. void UIOffscreenView::InvokePointerDown(int x, int y, int click_count, int qualifiers, bool touch, int touchId, bool superDown)
  213. {
  214. widget_->InvokePointerDown(x, y, click_count, GetModifierKeys(qualifiers, superDown), touch, touchId);
  215. }
  216. void UIOffscreenView::InvokePointerUp(int x, int y, int qualifiers, bool touch, int touchId, bool superDown)
  217. {
  218. widget_->InvokePointerUp(x, y, GetModifierKeys(qualifiers, superDown), touch, touchId);
  219. }
  220. void UIOffscreenView::InvokePointerMove(int x, int y, int qualifiers, bool touch, int touchId, bool superDown)
  221. {
  222. widget_->InvokePointerMove(x, y, GetModifierKeys(qualifiers, superDown), touch, touchId);
  223. }
  224. void UIOffscreenView::InvokeWheel(int x, int y, int delta_x, int delta_y, int qualifiers, bool superDown)
  225. {
  226. widget_->InvokeWheel(x, y, delta_x, delta_y, GetModifierKeys(qualifiers, superDown));
  227. }
  228. bool UIOffscreenView::InvokeKey(int key, int keycode, bool down, int qualifiers, bool superDown)
  229. {
  230. return widget_->InvokeKey(key, GetSpecialKey(keycode), GetModifierKeys(qualifiers, superDown), down);
  231. }
  232. }