UIState.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include <TurboBadger/tb_core.h>
  2. #include <TurboBadger/tb_system.h>
  3. #include <TurboBadger/tb_debug.h>
  4. #include <TurboBadger/animation/tb_widget_animation.h>
  5. #include <TurboBadger/renderers/tb_renderer_batcher.h>
  6. #include <TurboBadger/tb_font_renderer.h>
  7. #include <TurboBadger/tb_node_tree.h>
  8. #include <TurboBadger/tb_widgets_reader.h>
  9. #include <TurboBadger/tb_window.h>
  10. void register_tbbf_font_renderer();
  11. void register_stb_font_renderer();
  12. void register_freetype_font_renderer();
  13. using namespace tb;
  14. #include "../Core/CoreEvents.h"
  15. #include "../Input/Input.h"
  16. #include "../Input/InputEvents.h"
  17. #include "../Resource/ResourceCache.h"
  18. #include "../Graphics/Graphics.h"
  19. #include "../Graphics/GraphicsEvents.h"
  20. #include "../Graphics/Texture2D.h"
  21. #include "../Graphics/VertexBuffer.h"
  22. #include "UIRenderer.h"
  23. #include "UIState.h"
  24. namespace Atomic
  25. {
  26. WeakPtr<Context> UIState::readerContext_;
  27. UIState::UIState(Context* context) :
  28. Object(context),
  29. rootWidget_(0),
  30. inputDisabled_(false),
  31. keyboardDisabled_(false)
  32. {
  33. Graphics* graphics = GetSubsystem<Graphics>();
  34. assert(graphics);
  35. assert(graphics->IsInitialized());
  36. graphics_ = graphics;
  37. vertexBuffer_ = new VertexBuffer(context_);
  38. Initialize();
  39. }
  40. UIState::~UIState()
  41. {
  42. tb::TBWidgetsAnimationManager::Shutdown();
  43. delete rootWidget_;
  44. // leak
  45. //delete TBUIRenderer::renderer_;
  46. tb_core_shutdown();
  47. }
  48. // refactor
  49. void UIState::Initialize()
  50. {
  51. readerContext_ = context_;
  52. TBFile::SetReaderFunction(TBFileReader);
  53. TBWidgetsAnimationManager::Init();
  54. renderer_ = new UIRenderer(graphics_->GetContext());
  55. tb_core_init(renderer_, "AtomicEditor/resources/language/lng_en.tb.txt");
  56. // Load the default skin, and override skin that contains the graphics specific to the demo.
  57. tb::g_tb_skin->Load("AtomicEditor/resources/default_skin/skin.tb.txt", "AtomicEditor/editor/skin/skin.tb.txt");
  58. //register_tbbf_font_renderer();
  59. //register_stb_font_renderer();
  60. register_freetype_font_renderer();
  61. tb::g_font_manager->AddFontInfo("AtomicEditor/resources/MesloLGS-Regular.ttf", "Monaco");
  62. tb::g_font_manager->AddFontInfo("AtomicEditor/resources/vera.ttf", "Vera");
  63. tb::TBFontDescription fd;
  64. fd.SetID(tb::TBIDC("Vera"));
  65. fd.SetSize(tb::g_tb_skin->GetDimensionConverter()->DpToPx(12));
  66. tb::g_font_manager->SetDefaultFontDescription(fd);
  67. // Create the font now.
  68. tb::TBFontFace *font = tb::g_font_manager->CreateFontFace(tb::g_font_manager->GetDefaultFontDescription());
  69. // Render some glyphs in one go now since we know we are going to use them. It would work fine
  70. // without this since glyphs are rendered when needed, but with some extra updating of the glyph bitmap.
  71. if (font)
  72. font->RenderGlyphs(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~•·åäöÅÄÖ");
  73. rootWidget_ = new TBWidget();
  74. //rootWidget_->SetSkinBg(tb::TBIDC("background"));
  75. int width = graphics_->GetWidth();
  76. int height = graphics_->GetHeight();
  77. rootWidget_->SetSize(width, height);
  78. //SetSize(width, height);
  79. rootWidget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
  80. SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UIState, HandleMouseButtonDown));
  81. SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UIState, HandleMouseButtonUp));
  82. SubscribeToEvent(E_MOUSEMOVE, HANDLER(UIState, HandleMouseMove));
  83. SubscribeToEvent(E_MOUSEWHEEL, HANDLER(UIState, HandleMouseWheel));
  84. SubscribeToEvent(E_KEYDOWN, HANDLER(UIState, HandleKeyDown));
  85. SubscribeToEvent(E_KEYUP, HANDLER(UIState, HandleKeyUp));
  86. SubscribeToEvent(E_TEXTINPUT, HANDLER(UIState, HandleTextInput));
  87. SubscribeToEvent(E_UPDATE, HANDLER(UIState, HandleUpdate));
  88. SubscribeToEvent(E_RENDERUPDATE, HANDLER(UIState, HandleRenderUpdate));
  89. //TB_DEBUG_SETTING(LAYOUT_BOUNDS) = 1;
  90. }
  91. void UIState::Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd)
  92. {
  93. if (batches.Empty())
  94. return;
  95. Vector2 invScreenSize(1.0f / (float)graphics_->GetWidth(), 1.0f / (float)graphics_->GetHeight());
  96. Vector2 scale(2.0f * invScreenSize.x_, -2.0f * invScreenSize.y_);
  97. Vector2 offset(-1.0f, 1.0f);
  98. Matrix4 projection(Matrix4::IDENTITY);
  99. projection.m00_ = scale.x_;
  100. projection.m03_ = offset.x_;
  101. projection.m11_ = scale.y_;
  102. projection.m13_ = offset.y_;
  103. projection.m22_ = 1.0f;
  104. projection.m23_ = 0.0f;
  105. projection.m33_ = 1.0f;
  106. graphics_->ClearParameterSources();
  107. graphics_->SetColorWrite(true);
  108. graphics_->SetCullMode(CULL_NONE);
  109. graphics_->SetDepthTest(CMP_ALWAYS);
  110. graphics_->SetDepthWrite(false);
  111. graphics_->SetDrawAntialiased(false);
  112. graphics_->SetFillMode(FILL_SOLID);
  113. graphics_->SetStencilTest(false);
  114. graphics_->ResetRenderTargets();
  115. graphics_->SetVertexBuffer(buffer);
  116. ShaderVariation* noTextureVS = graphics_->GetShader(VS, "Basic", "VERTEXCOLOR");
  117. ShaderVariation* diffTextureVS = graphics_->GetShader(VS, "Basic", "DIFFMAP VERTEXCOLOR");
  118. ShaderVariation* noTexturePS = graphics_->GetShader(PS, "Basic", "VERTEXCOLOR");
  119. ShaderVariation* diffTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP VERTEXCOLOR");
  120. ShaderVariation* diffMaskTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP ALPHAMASK VERTEXCOLOR");
  121. ShaderVariation* alphaTexturePS = graphics_->GetShader(PS, "Basic", "ALPHAMAP VERTEXCOLOR");
  122. unsigned alphaFormat = Graphics::GetAlphaFormat();
  123. for (unsigned i = batchStart; i < batchEnd; ++i)
  124. {
  125. const UIBatch& batch = batches[i];
  126. if (batch.vertexStart_ == batch.vertexEnd_)
  127. continue;
  128. ShaderVariation* ps;
  129. ShaderVariation* vs;
  130. if (!batch.texture_)
  131. {
  132. ps = noTexturePS;
  133. vs = noTextureVS;
  134. }
  135. else
  136. {
  137. // If texture contains only an alpha channel, use alpha shader (for fonts)
  138. vs = diffTextureVS;
  139. if (batch.texture_->GetFormat() == alphaFormat)
  140. ps = alphaTexturePS;
  141. else if (batch.blendMode_ != BLEND_ALPHA && batch.blendMode_ != BLEND_ADDALPHA && batch.blendMode_ != BLEND_PREMULALPHA)
  142. ps = diffMaskTexturePS;
  143. else
  144. ps = diffTexturePS;
  145. }
  146. graphics_->SetShaders(vs, ps);
  147. if (graphics_->NeedParameterUpdate(SP_OBJECTTRANSFORM, this))
  148. graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  149. if (graphics_->NeedParameterUpdate(SP_CAMERA, this))
  150. graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
  151. if (graphics_->NeedParameterUpdate(SP_MATERIAL, this))
  152. graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  153. graphics_->SetBlendMode(batch.blendMode_);
  154. graphics_->SetScissorTest(true, batch.scissor_);
  155. graphics_->SetTexture(0, batch.texture_);
  156. graphics_->Draw(TRIANGLE_LIST, batch.vertexStart_ / UI_VERTEX_SIZE, (batch.vertexEnd_ - batch.vertexStart_) /
  157. UI_VERTEX_SIZE);
  158. }
  159. }
  160. void UIState::SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData)
  161. {
  162. if (vertexData.Empty())
  163. return;
  164. // Update quad geometry into the vertex buffer
  165. // Resize the vertex buffer first if too small or much too large
  166. unsigned numVertices = vertexData.Size() / UI_VERTEX_SIZE;
  167. if (dest->GetVertexCount() < numVertices || dest->GetVertexCount() > numVertices * 2)
  168. dest->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
  169. dest->SetData(&vertexData[0]);
  170. }
  171. void UIState::Render()
  172. {
  173. SetVertexData(vertexBuffer_, vertexData_);
  174. Render(vertexBuffer_, batches_, 0, batches_.Size());
  175. }
  176. void UIState::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  177. {
  178. // Get rendering batches from the non-modal UI elements
  179. batches_.Clear();
  180. vertexData_.Clear();
  181. tb::TBRect rect = rootWidget_->GetRect();
  182. IntRect currentScissor = IntRect(0, 0, rect.w, rect.h);
  183. GetBatches(batches_, vertexData_, currentScissor);
  184. }
  185. void UIState::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  186. {
  187. //if (!initialized_)
  188. // return;
  189. TBAnimationManager::Update();
  190. rootWidget_->InvokeProcessStates();
  191. rootWidget_->InvokeProcess();
  192. tb::g_renderer->BeginPaint(rootWidget_->GetRect().w, rootWidget_->GetRect().h);
  193. renderer_->currentScissor_ = currentScissor;
  194. renderer_->batches_ = &batches;
  195. renderer_->vertexData_ = &vertexData;
  196. rootWidget_->InvokePaint(tb::TBWidget::PaintProps());
  197. tb::g_renderer->EndPaint();
  198. }
  199. void UIState::SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData)
  200. {
  201. UIBatch b(BLEND_ALPHA , renderer_->currentScissor_, texture, &vertexData_);
  202. unsigned begin = b.vertexData_->Size();
  203. b.vertexData_->Resize(begin + vertexData.Size());
  204. float* dest = &(b.vertexData_->At(begin));
  205. b.vertexEnd_ = b.vertexData_->Size();
  206. for (unsigned i = 0; i < vertexData.Size(); i++, dest++)
  207. {
  208. *dest = vertexData[i];
  209. }
  210. UIBatch::AddOrMerge(b, batches_);
  211. }
  212. void UIState::TBFileReader(const char* filename, void** data, unsigned* length)
  213. {
  214. *data = 0;
  215. *length = 0;
  216. ResourceCache* cache = readerContext_->GetSubsystem<ResourceCache>();
  217. SharedPtr<File> file = cache->GetFile(filename);
  218. if (!file || !file->IsOpen())
  219. return;
  220. unsigned size = file->GetSize();
  221. if (!size)
  222. return;
  223. void* _data = malloc(size);
  224. if (!_data)
  225. return;
  226. if (file->Read(_data, size) != size)
  227. {
  228. free(_data);
  229. return;
  230. }
  231. *length = size;
  232. *data = _data;
  233. }
  234. bool UIState::LoadResourceFile(TBWidget* widget, const String& filename)
  235. {
  236. tb::TBNode node;
  237. // TODO: use Urho resources
  238. if (!node.ReadFile(filename.CString()))
  239. return false;
  240. tb::g_widgets_reader->LoadNodeTree(widget, &node);
  241. return true;
  242. }
  243. void UIState::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  244. {
  245. using namespace ScreenMode;
  246. rootWidget_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  247. //SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  248. }
  249. void UIState::HandleUpdate(StringHash eventType, VariantMap& eventData)
  250. {
  251. TBMessageHandler::ProcessMessages();
  252. }
  253. }