UI.cpp 10 KB

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