UI.cpp 10 KB

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