UIView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //
  2. // Copyright (c) 2014-2015, 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 "../IO/Log.h"
  24. #include "../Graphics/Graphics.h"
  25. #include "../Graphics/VertexBuffer.h"
  26. #include "../Graphics/Texture2D.h"
  27. #include "../Input/InputEvents.h"
  28. #include "UI.h"
  29. #include "UIView.h"
  30. #include "UIRenderer.h"
  31. using namespace tb;
  32. namespace Atomic
  33. {
  34. UIView::UIView(Context* context) : UIWidget(context, false),
  35. autoFocus_(true),
  36. mouseEnabled_(true),
  37. keyboardEnabled_(true)
  38. {
  39. graphics_ = GetSubsystem<Graphics>();
  40. assert(graphics_.NotNull());
  41. assert(graphics_->IsInitialized());
  42. ui_ = GetSubsystem<UI>();
  43. assert(ui_.NotNull());
  44. renderer_ = ui_->GetRenderer();
  45. widget_ = new TBWidget();
  46. widget_->SetDelegate(this);
  47. // Set gravity all so we resize correctly
  48. widget_->SetGravity(WIDGET_GRAVITY_ALL);
  49. ui_->WrapWidget(this, widget_);
  50. // Set initial size for view
  51. TBRect rect = ui_->GetRootWidget()->GetRect();
  52. widget_->SetSize(rect.w, rect.h);
  53. vertexBuffer_ = new VertexBuffer(context_);
  54. ui_->AddUIView(this);
  55. }
  56. UIView::~UIView()
  57. {
  58. }
  59. void UIView::SetFocus()
  60. {
  61. if (ui_.Null() || ui_->GetFocusedView() == this)
  62. {
  63. return;
  64. }
  65. if (ui_->GetFocusedView())
  66. {
  67. ui_->GetFocusedView()->ResignFocus();
  68. }
  69. UIWidget::SetFocus();
  70. ui_->SetFocusedView(this);
  71. }
  72. bool UIView::GetFocus() const
  73. {
  74. if (ui_.Null())
  75. {
  76. return false;
  77. }
  78. return ui_->GetFocusedView() == this;
  79. }
  80. void UIView::BecomeFocused()
  81. {
  82. widget_->SetZ(WIDGET_Z_TOP);
  83. SubscribeToEvent(E_MOUSEBUTTONDOWN, ATOMIC_HANDLER(UIView, HandleMouseButtonDown));
  84. SubscribeToEvent(E_MOUSEBUTTONUP, ATOMIC_HANDLER(UIView, HandleMouseButtonUp));
  85. SubscribeToEvent(E_MOUSEMOVE, ATOMIC_HANDLER(UIView, HandleMouseMove));
  86. SubscribeToEvent(E_MOUSEWHEEL, ATOMIC_HANDLER(UIView, HandleMouseWheel));
  87. SubscribeToEvent(E_KEYDOWN, ATOMIC_HANDLER(UIView, HandleKeyDown));
  88. SubscribeToEvent(E_KEYUP, ATOMIC_HANDLER(UIView, HandleKeyUp));
  89. SubscribeToEvent(E_TEXTINPUT, ATOMIC_HANDLER(UIView, HandleTextInput));
  90. SubscribeToEvent(E_TOUCHBEGIN, ATOMIC_HANDLER(UIView, HandleTouchBegin));
  91. SubscribeToEvent(E_TOUCHEND, ATOMIC_HANDLER(UIView, HandleTouchEnd));
  92. SubscribeToEvent(E_TOUCHMOVE, ATOMIC_HANDLER(UIView, HandleTouchMove));
  93. }
  94. void UIView::ResignFocus()
  95. {
  96. if (ui_.Null() || ui_->GetFocusedView() != this)
  97. {
  98. return;
  99. }
  100. widget_->SetZ(WIDGET_Z_BOTTOM);
  101. UnsubscribeFromEvent(E_MOUSEBUTTONDOWN);
  102. UnsubscribeFromEvent(E_MOUSEBUTTONUP);
  103. UnsubscribeFromEvent(E_MOUSEMOVE);
  104. UnsubscribeFromEvent(E_MOUSEWHEEL);
  105. UnsubscribeFromEvent(E_KEYDOWN);
  106. UnsubscribeFromEvent(E_KEYUP);
  107. UnsubscribeFromEvent(E_TEXTINPUT);
  108. UnsubscribeFromEvent(E_TOUCHBEGIN);
  109. UnsubscribeFromEvent(E_TOUCHEND);
  110. UnsubscribeFromEvent(E_TOUCHMOVE);
  111. ui_->SetFocusedView(0);
  112. }
  113. void UIView::Remove()
  114. {
  115. ResignFocus();
  116. if (ui_.NotNull())
  117. {
  118. ui_->RemoveUIView(this);
  119. }
  120. UIWidget::Remove();
  121. }
  122. bool UIView::SetRenderToTexture(bool value, const int width, const int height)
  123. {
  124. if (!value && renderTexture_.NotNull())
  125. {
  126. renderTexture_ = 0;
  127. }
  128. else if (value && renderTexture_.Null())
  129. {
  130. renderTexture_ = new Texture2D(context_);
  131. SetAutoFocus(false);
  132. }
  133. return SetSize(width, height);
  134. }
  135. bool UIView::SetSize(int width, int height)
  136. {
  137. if (!widget_)
  138. return false;
  139. if (width < UIVIEW_MIN_TEXTURE_SIZE || width > UIVIEW_MAX_TEXTURE_SIZE ||
  140. height < UIVIEW_MIN_TEXTURE_SIZE || height > UIVIEW_MAX_TEXTURE_SIZE)
  141. {
  142. ATOMIC_LOGERROR("UIView::SetSize() - Attempting to set invalid size, failed");
  143. return false;
  144. }
  145. if (renderTexture_.NotNull())
  146. {
  147. renderTexture_->SetSize(width, height, graphics_->GetRGBAFormat(), Atomic::TEXTURE_RENDERTARGET);
  148. renderTexture_->SetFilterMode(FILTER_BILINEAR);
  149. renderTexture_->SetAddressMode(COORD_U, ADDRESS_CLAMP);
  150. renderTexture_->SetAddressMode(COORD_V, ADDRESS_CLAMP);
  151. renderTexture_->SetNumLevels(1); // No mipmaps
  152. renderTexture_->GetRenderSurface()->SetUpdateMode(SURFACE_MANUALUPDATE);
  153. }
  154. return UIWidget::SetSize(width, height);
  155. }
  156. void UIView::Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd)
  157. {
  158. if (batches.Empty())
  159. {
  160. graphics_->ResetRenderTargets();
  161. return;
  162. }
  163. IntVector2 size;
  164. if (renderTexture_)
  165. {
  166. size.x_ = renderTexture_->GetWidth();
  167. size.y_ = renderTexture_->GetHeight();
  168. }
  169. else
  170. {
  171. size.x_ = graphics_->GetWidth();
  172. size.y_ = graphics_->GetHeight();
  173. }
  174. bool scissorEnabled = true;
  175. IntRect rect(0, 0, size.x_, size.y_);
  176. Vector2 invScreenSize(1.0f / (float)size.x_, 1.0f / (float)size.y_);
  177. Vector2 scale(2.0f * invScreenSize.x_, -2.0f * invScreenSize.y_);
  178. Vector2 offset(-1.0f, 1.0f);
  179. // On OpenGL, flip the projection if rendering to a texture so that the texture can be addressed in the same way
  180. // as a render texture produced on Direct3D
  181. #ifdef ATOMIC_OPENGL
  182. if (renderTexture_)
  183. {
  184. // ATOMIC ISSUE: https://github.com/AtomicGameEngine/AtomicGameEngine/issues/1581
  185. // this needs to be fixed, scissors can't be disabled
  186. // and there is a flip to D3D in SetScissorTest
  187. scissorEnabled = false;
  188. offset.y_ = -offset.y_;
  189. scale.y_ = -scale.y_;
  190. }
  191. #endif
  192. Matrix4 projection(Matrix4::IDENTITY);
  193. projection.m00_ = scale.x_;
  194. projection.m03_ = offset.x_;
  195. projection.m11_ = scale.y_;
  196. projection.m13_ = offset.y_;
  197. projection.m22_ = 1.0f;
  198. projection.m23_ = 0.0f;
  199. projection.m33_ = 1.0f;
  200. graphics_->ClearParameterSources();
  201. graphics_->SetColorWrite(true);
  202. graphics_->SetCullMode(CULL_NONE);
  203. graphics_->SetDepthTest(CMP_ALWAYS);
  204. graphics_->SetDepthWrite(false);
  205. graphics_->SetFillMode(FILL_SOLID);
  206. graphics_->SetStencilTest(false);
  207. graphics_->ResetRenderTargets();
  208. if (renderTexture_)
  209. {
  210. graphics_->SetRenderTarget(0, renderTexture_->GetRenderSurface());
  211. }
  212. graphics_->SetViewport(rect);
  213. graphics_->SetVertexBuffer(buffer);
  214. ShaderVariation* noTextureVS = graphics_->GetShader(VS, "Basic", "VERTEXCOLOR");
  215. ShaderVariation* diffTextureVS = graphics_->GetShader(VS, "Basic", "DIFFMAP VERTEXCOLOR");
  216. ShaderVariation* noTexturePS = graphics_->GetShader(PS, "Basic", "VERTEXCOLOR");
  217. ShaderVariation* diffTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP VERTEXCOLOR");
  218. ShaderVariation* diffMaskTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP ALPHAMASK VERTEXCOLOR");
  219. ShaderVariation* alphaTexturePS = graphics_->GetShader(PS, "Basic", "ALPHAMAP VERTEXCOLOR");
  220. unsigned alphaFormat = Graphics::GetAlphaFormat();
  221. if (renderTexture_)
  222. {
  223. graphics_->Clear(Atomic::CLEAR_COLOR);
  224. }
  225. for (unsigned i = batchStart; i < batchEnd; ++i)
  226. {
  227. const UIBatch& batch = batches[i];
  228. if (batch.vertexStart_ == batch.vertexEnd_)
  229. continue;
  230. ShaderVariation* ps;
  231. ShaderVariation* vs;
  232. if (!batch.texture_)
  233. {
  234. ps = noTexturePS;
  235. vs = noTextureVS;
  236. }
  237. else
  238. {
  239. // If texture contains only an alpha channel, use alpha shader (for fonts)
  240. vs = diffTextureVS;
  241. if (batch.texture_->GetFormat() == alphaFormat)
  242. ps = alphaTexturePS;
  243. else if (batch.blendMode_ != BLEND_ALPHA && batch.blendMode_ != BLEND_ADDALPHA && batch.blendMode_ != BLEND_PREMULALPHA)
  244. ps = diffMaskTexturePS;
  245. else
  246. ps = diffTexturePS;
  247. }
  248. graphics_->SetShaders(vs, ps);
  249. if (graphics_->NeedParameterUpdate(SP_OBJECT, this))
  250. graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  251. if (graphics_->NeedParameterUpdate(SP_CAMERA, this))
  252. graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
  253. if (graphics_->NeedParameterUpdate(SP_MATERIAL, this))
  254. graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  255. graphics_->SetBlendMode(batch.blendMode_);
  256. graphics_->SetScissorTest(scissorEnabled, batch.scissor_);
  257. graphics_->SetTexture(0, batch.texture_);
  258. graphics_->Draw(TRIANGLE_LIST, batch.vertexStart_ / UI_VERTEX_SIZE, (batch.vertexEnd_ - batch.vertexStart_) /
  259. UI_VERTEX_SIZE);
  260. }
  261. if (renderTexture_)
  262. {
  263. graphics_->ResetRenderTargets();
  264. }
  265. }
  266. void UIView::SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData)
  267. {
  268. if (vertexData.Empty())
  269. return;
  270. // Update quad geometry into the vertex buffer
  271. // Resize the vertex buffer first if too small or much too large
  272. unsigned numVertices = vertexData.Size() / UI_VERTEX_SIZE;
  273. if (dest->GetVertexCount() < numVertices || dest->GetVertexCount() > numVertices * 2)
  274. dest->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
  275. dest->SetData(&vertexData[0]);
  276. }
  277. void UIView::Render(bool resetRenderTargets)
  278. {
  279. SetVertexData(vertexBuffer_, vertexData_);
  280. Render(vertexBuffer_, batches_, 0, batches_.Size());
  281. }
  282. void UIView::UpdateUIBatches()
  283. {
  284. batches_.Clear();
  285. vertexData_.Clear();
  286. tb::TBRect rect = widget_->GetRect();
  287. IntRect currentScissor = IntRect(0, 0, rect.w, rect.h);
  288. GetBatches(batches_, vertexData_, currentScissor);
  289. }
  290. void UIView::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  291. {
  292. tb::g_renderer->BeginPaint(currentScissor.Width(), currentScissor.Height());
  293. renderer_->currentScissor_ = currentScissor;
  294. renderer_->batches_ = &batches;
  295. renderer_->vertexData_ = &vertexData;
  296. widget_->InvokePaint(tb::TBWidget::PaintProps());
  297. tb::g_renderer->EndPaint();
  298. }
  299. void UIView::SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData)
  300. {
  301. UIBatch b(BLEND_ALPHA , renderer_->currentScissor_, texture, &vertexData_);
  302. unsigned begin = b.vertexData_->Size();
  303. b.vertexData_->Resize(begin + vertexData.Size());
  304. float* dest = &(b.vertexData_->At(begin));
  305. b.vertexEnd_ = b.vertexData_->Size();
  306. for (unsigned i = 0; i < vertexData.Size(); i++, dest++)
  307. {
  308. *dest = vertexData[i];
  309. }
  310. UIBatch::AddOrMerge(b, batches_);
  311. }
  312. }