UI.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. #include <TurboBadger/tb_message_window.h>
  11. #include <TurboBadger/tb_editfield.h>
  12. #include <TurboBadger/tb_select.h>
  13. #include <TurboBadger/tb_inline_select.h>
  14. #include <TurboBadger/tb_tab_container.h>
  15. #include <TurboBadger/tb_toggle_container.h>
  16. #include <TurboBadger/tb_scroll_container.h>
  17. #include <TurboBadger/image/tb_image_widget.h>
  18. void register_tbbf_font_renderer();
  19. void register_stb_font_renderer();
  20. void register_freetype_font_renderer();
  21. using namespace tb;
  22. #include "../Core/CoreEvents.h"
  23. #include "../IO/Log.h"
  24. #include "../Input/Input.h"
  25. #include "../Input/InputEvents.h"
  26. #include "../Resource/ResourceCache.h"
  27. #include "../Graphics/Graphics.h"
  28. #include "../Graphics/GraphicsEvents.h"
  29. #include "../Graphics/Texture2D.h"
  30. #include "../Graphics/VertexBuffer.h"
  31. #include "UIEvents.h"
  32. #include "UIRenderer.h"
  33. #include "UI.h"
  34. #include "UIButton.h"
  35. #include "UITextField.h"
  36. #include "UIEditField.h"
  37. #include "UILayout.h"
  38. #include "UIImageWidget.h"
  39. #include "UIClickLabel.h"
  40. #include "UICheckBox.h"
  41. #include "UISelectList.h"
  42. #include "UIMessageWindow.h"
  43. #include "UISkinImage.h"
  44. #include "UITabContainer.h"
  45. #include "UISceneView.h"
  46. #include "UIDragDrop.h"
  47. #include "UIContainer.h"
  48. #include "UISection.h"
  49. #include "UIInlineSelect.h"
  50. #include "UIScrollContainer.h"
  51. #include "UISeparator.h"
  52. #include "UIDimmer.h"
  53. #include "SystemUI/SystemUI.h"
  54. #include "SystemUI/SystemUIEvents.h"
  55. #include "SystemUI/DebugHud.h"
  56. #include "SystemUI/Console.h"
  57. namespace tb
  58. {
  59. void TBSystem::RescheduleTimer(double fire_time)
  60. {
  61. }
  62. }
  63. namespace Atomic
  64. {
  65. WeakPtr<Context> UI::uiContext_;
  66. UI::UI(Context* context) :
  67. Object(context),
  68. rootWidget_(0),
  69. inputDisabled_(false),
  70. keyboardDisabled_(false),
  71. initialized_(false),
  72. skinLoaded_(false),
  73. consoleVisible_(false)
  74. {
  75. }
  76. UI::~UI()
  77. {
  78. if (initialized_)
  79. {
  80. tb::TBWidgetListener::RemoveGlobalListener(this);
  81. TBFile::SetReaderFunction(0);
  82. TBID::tbidRegisterCallback = 0;
  83. tb::TBWidgetsAnimationManager::Shutdown();
  84. widgetWrap_.Clear();
  85. delete rootWidget_;
  86. // leak
  87. //delete TBUIRenderer::renderer_;
  88. tb_core_shutdown();
  89. }
  90. uiContext_ = 0;
  91. }
  92. void UI::Shutdown()
  93. {
  94. SetInputDisabled(true);
  95. }
  96. void UI::Initialize(const String& languageFile)
  97. {
  98. Graphics* graphics = GetSubsystem<Graphics>();
  99. assert(graphics);
  100. assert(graphics->IsInitialized());
  101. graphics_ = graphics;
  102. vertexBuffer_ = new VertexBuffer(context_);
  103. uiContext_ = context_;
  104. TBFile::SetReaderFunction(TBFileReader);
  105. TBID::tbidRegisterCallback = UI::TBIDRegisterStringCallback;
  106. TBWidgetsAnimationManager::Init();
  107. renderer_ = new UIRenderer(graphics_->GetContext());
  108. tb_core_init(renderer_, languageFile.CString());
  109. //register_tbbf_font_renderer();
  110. //register_stb_font_renderer();
  111. register_freetype_font_renderer();
  112. rootWidget_ = new TBWidget();
  113. int width = graphics_->GetWidth();
  114. int height = graphics_->GetHeight();
  115. rootWidget_->SetSize(width, height);
  116. rootWidget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
  117. // register the UIDragDrop subsystem
  118. context_->RegisterSubsystem(new UIDragDrop(context_));
  119. SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UI, HandleMouseButtonDown));
  120. SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UI, HandleMouseButtonUp));
  121. SubscribeToEvent(E_MOUSEMOVE, HANDLER(UI, HandleMouseMove));
  122. SubscribeToEvent(E_MOUSEWHEEL, HANDLER(UI, HandleMouseWheel));
  123. SubscribeToEvent(E_KEYDOWN, HANDLER(UI, HandleKeyDown));
  124. SubscribeToEvent(E_KEYUP, HANDLER(UI, HandleKeyUp));
  125. SubscribeToEvent(E_TEXTINPUT, HANDLER(UI, HandleTextInput));
  126. SubscribeToEvent(E_UPDATE, HANDLER(UI, HandleUpdate));
  127. SubscribeToEvent(SystemUI::E_CONSOLECLOSED, HANDLER(UI, HandleConsoleClosed));
  128. SubscribeToEvent(E_RENDERUPDATE, HANDLER(UI, HandleRenderUpdate));
  129. tb::TBWidgetListener::AddGlobalListener(this);
  130. initialized_ = true;
  131. SystemUI::SystemUI* systemUI = new SystemUI::SystemUI(context_);
  132. context_->RegisterSubsystem(systemUI);
  133. systemUI->CreateConsoleAndDebugHud();
  134. //TB_DEBUG_SETTING(LAYOUT_BOUNDS) = 1;
  135. }
  136. void UI::LoadSkin(const String& skin, const String& overrideSkin)
  137. {
  138. // Load the default skin, and override skin (if any)
  139. tb::g_tb_skin->Load(skin.CString(), overrideSkin.CString());
  140. skinLoaded_ = true;
  141. }
  142. void UI::LoadDefaultPlayerSkin()
  143. {
  144. LoadSkin("DefaultUI/skin/skin.tb.txt");
  145. AddFont("DefaultUI/fonts/vera.ttf", "Vera");
  146. SetDefaultFont("Vera", 12);
  147. }
  148. void UI::SetDefaultFont(const String& name, int size)
  149. {
  150. tb::TBFontDescription fd;
  151. fd.SetID(tb::TBIDC(name.CString()));
  152. fd.SetSize(tb::g_tb_skin->GetDimensionConverter()->DpToPx(12));
  153. tb::g_font_manager->SetDefaultFontDescription(fd);
  154. // Create the font now.
  155. tb::TBFontFace *font = tb::g_font_manager->CreateFontFace(tb::g_font_manager->GetDefaultFontDescription());
  156. // Render some glyphs in one go now since we know we are going to use them. It would work fine
  157. // without this since glyphs are rendered when needed, but with some extra updating of the glyph bitmap.
  158. if (font)
  159. font->RenderGlyphs(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~•·åäöÅÄÖ");
  160. }
  161. void UI::AddFont(const String& fontFile, const String& name)
  162. {
  163. tb::g_font_manager->AddFontInfo(fontFile.CString(), name.CString());
  164. }
  165. void UI::Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd)
  166. {
  167. if (batches.Empty())
  168. return;
  169. Vector2 invScreenSize(1.0f / (float)graphics_->GetWidth(), 1.0f / (float)graphics_->GetHeight());
  170. Vector2 scale(2.0f * invScreenSize.x_, -2.0f * invScreenSize.y_);
  171. Vector2 offset(-1.0f, 1.0f);
  172. Matrix4 projection(Matrix4::IDENTITY);
  173. projection.m00_ = scale.x_;
  174. projection.m03_ = offset.x_;
  175. projection.m11_ = scale.y_;
  176. projection.m13_ = offset.y_;
  177. projection.m22_ = 1.0f;
  178. projection.m23_ = 0.0f;
  179. projection.m33_ = 1.0f;
  180. graphics_->ClearParameterSources();
  181. graphics_->SetColorWrite(true);
  182. graphics_->SetCullMode(CULL_NONE);
  183. graphics_->SetDepthTest(CMP_ALWAYS);
  184. graphics_->SetDepthWrite(false);
  185. graphics_->SetFillMode(FILL_SOLID);
  186. graphics_->SetStencilTest(false);
  187. graphics_->ResetRenderTargets();
  188. graphics_->SetVertexBuffer(buffer);
  189. ShaderVariation* noTextureVS = graphics_->GetShader(VS, "Basic", "VERTEXCOLOR");
  190. ShaderVariation* diffTextureVS = graphics_->GetShader(VS, "Basic", "DIFFMAP VERTEXCOLOR");
  191. ShaderVariation* noTexturePS = graphics_->GetShader(PS, "Basic", "VERTEXCOLOR");
  192. ShaderVariation* diffTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP VERTEXCOLOR");
  193. ShaderVariation* diffMaskTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP ALPHAMASK VERTEXCOLOR");
  194. ShaderVariation* alphaTexturePS = graphics_->GetShader(PS, "Basic", "ALPHAMAP VERTEXCOLOR");
  195. unsigned alphaFormat = Graphics::GetAlphaFormat();
  196. for (unsigned i = batchStart; i < batchEnd; ++i)
  197. {
  198. const UIBatch& batch = batches[i];
  199. if (batch.vertexStart_ == batch.vertexEnd_)
  200. continue;
  201. ShaderVariation* ps;
  202. ShaderVariation* vs;
  203. if (!batch.texture_)
  204. {
  205. ps = noTexturePS;
  206. vs = noTextureVS;
  207. }
  208. else
  209. {
  210. // If texture contains only an alpha channel, use alpha shader (for fonts)
  211. vs = diffTextureVS;
  212. if (batch.texture_->GetFormat() == alphaFormat)
  213. ps = alphaTexturePS;
  214. else if (batch.blendMode_ != BLEND_ALPHA && batch.blendMode_ != BLEND_ADDALPHA && batch.blendMode_ != BLEND_PREMULALPHA)
  215. ps = diffMaskTexturePS;
  216. else
  217. ps = diffTexturePS;
  218. }
  219. graphics_->SetShaders(vs, ps);
  220. if (graphics_->NeedParameterUpdate(SP_OBJECT, this))
  221. graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  222. if (graphics_->NeedParameterUpdate(SP_CAMERA, this))
  223. graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
  224. if (graphics_->NeedParameterUpdate(SP_MATERIAL, this))
  225. graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  226. graphics_->SetBlendMode(batch.blendMode_);
  227. graphics_->SetScissorTest(true, batch.scissor_);
  228. graphics_->SetTexture(0, batch.texture_);
  229. graphics_->Draw(TRIANGLE_LIST, batch.vertexStart_ / UI_VERTEX_SIZE, (batch.vertexEnd_ - batch.vertexStart_) /
  230. UI_VERTEX_SIZE);
  231. }
  232. }
  233. void UI::SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData)
  234. {
  235. if (vertexData.Empty())
  236. return;
  237. // Update quad geometry into the vertex buffer
  238. // Resize the vertex buffer first if too small or much too large
  239. unsigned numVertices = vertexData.Size() / UI_VERTEX_SIZE;
  240. if (dest->GetVertexCount() < numVertices || dest->GetVertexCount() > numVertices * 2)
  241. dest->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
  242. dest->SetData(&vertexData[0]);
  243. }
  244. void UI::Render(bool resetRenderTargets)
  245. {
  246. SetVertexData(vertexBuffer_, vertexData_);
  247. Render(vertexBuffer_, batches_, 0, batches_.Size());
  248. GetSubsystem<SystemUI::SystemUI>()->Render();
  249. }
  250. void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  251. {
  252. // Get rendering batches from the non-modal UI elements
  253. batches_.Clear();
  254. vertexData_.Clear();
  255. tb::TBRect rect = rootWidget_->GetRect();
  256. IntRect currentScissor = IntRect(0, 0, rect.w, rect.h);
  257. GetBatches(batches_, vertexData_, currentScissor);
  258. }
  259. void UI::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  260. {
  261. //if (!initialized_)
  262. // return;
  263. TBAnimationManager::Update();
  264. rootWidget_->InvokeProcessStates();
  265. rootWidget_->InvokeProcess();
  266. tb::g_renderer->BeginPaint(rootWidget_->GetRect().w, rootWidget_->GetRect().h);
  267. renderer_->currentScissor_ = currentScissor;
  268. renderer_->batches_ = &batches;
  269. renderer_->vertexData_ = &vertexData;
  270. rootWidget_->InvokePaint(tb::TBWidget::PaintProps());
  271. tb::g_renderer->EndPaint();
  272. }
  273. void UI::SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData)
  274. {
  275. UIBatch b(BLEND_ALPHA , renderer_->currentScissor_, texture, &vertexData_);
  276. unsigned begin = b.vertexData_->Size();
  277. b.vertexData_->Resize(begin + vertexData.Size());
  278. float* dest = &(b.vertexData_->At(begin));
  279. b.vertexEnd_ = b.vertexData_->Size();
  280. for (unsigned i = 0; i < vertexData.Size(); i++, dest++)
  281. {
  282. *dest = vertexData[i];
  283. }
  284. UIBatch::AddOrMerge(b, batches_);
  285. }
  286. void UI::TBFileReader(const char* filename, void** data, unsigned* length)
  287. {
  288. *data = 0;
  289. *length = 0;
  290. ResourceCache* cache = uiContext_->GetSubsystem<ResourceCache>();
  291. SharedPtr<File> file = cache->GetFile(filename);
  292. if (!file || !file->IsOpen())
  293. {
  294. LOGERRORF("UI::TBFileReader: Unable to load file: %s", filename);
  295. return;
  296. }
  297. unsigned size = file->GetSize();
  298. if (!size)
  299. return;
  300. void* _data = malloc(size);
  301. if (!_data)
  302. return;
  303. if (file->Read(_data, size) != size)
  304. {
  305. free(_data);
  306. return;
  307. }
  308. *length = size;
  309. *data = _data;
  310. }
  311. void UI::GetTBIDString(unsigned id, String& value)
  312. {
  313. if (!id)
  314. {
  315. value = "";
  316. }
  317. else
  318. {
  319. value = tbidToString_[id];
  320. }
  321. }
  322. void UI::TBIDRegisterStringCallback(unsigned id, const char* value)
  323. {
  324. uiContext_->GetSubsystem<UI>()->tbidToString_[id] = String(value);
  325. }
  326. bool UI::LoadResourceFile(TBWidget* widget, const String& filename)
  327. {
  328. tb::TBNode node;
  329. if (!node.ReadFile(filename.CString()))
  330. return false;
  331. tb::g_widgets_reader->LoadNodeTree(widget, &node);
  332. return true;
  333. }
  334. void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  335. {
  336. using namespace ScreenMode;
  337. rootWidget_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  338. //SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  339. }
  340. void UI::HandleUpdate(StringHash eventType, VariantMap& eventData)
  341. {
  342. TBMessageHandler::ProcessMessages();
  343. }
  344. bool UI::IsWidgetWrapped(tb::TBWidget* widget)
  345. {
  346. return widgetWrap_.Contains(widget);
  347. }
  348. bool UI::UnwrapWidget(tb::TBWidget* widget)
  349. {
  350. if (widgetWrap_.Contains(widget))
  351. {
  352. widgetWrap_.Erase(widget);
  353. return true;
  354. }
  355. return false;
  356. }
  357. void UI::PruneUnreachableWidgets()
  358. {
  359. HashMap<tb::TBWidget*, SharedPtr<UIWidget>>::Iterator itr;
  360. for (itr = widgetWrap_.Begin(); itr != widgetWrap_.End(); )
  361. {
  362. if ((*itr).first_->GetParent() || (*itr).second_->JSGetHeapPtr())
  363. {
  364. itr++;
  365. continue;
  366. }
  367. tb::TBWidget* toDelete = (*itr).first_;
  368. itr.GotoNext();
  369. delete toDelete;
  370. // this will likely be flagged by valgrind as accessing invalid memory
  371. assert(!widgetWrap_.Contains(toDelete));
  372. }
  373. }
  374. void UI::WrapWidget(UIWidget* widget, tb::TBWidget* tbwidget)
  375. {
  376. assert (!widgetWrap_.Contains(tbwidget));
  377. widgetWrap_[tbwidget] = widget;
  378. }
  379. UIWidget* UI::WrapWidget(tb::TBWidget* widget)
  380. {
  381. if (!widget)
  382. return NULL;
  383. if (widgetWrap_.Contains(widget))
  384. return widgetWrap_[widget];
  385. // switch this to use a factory?
  386. // this is order dependent as we're using IsOfType which also works if a base class
  387. if (widget->IsOfType<TBDimmer>())
  388. {
  389. UIDimmer* dimmer = new UIDimmer(context_, false);
  390. dimmer->SetWidget(widget);
  391. widgetWrap_[widget] = dimmer;
  392. return dimmer;
  393. }
  394. if (widget->IsOfType<TBScrollContainer>())
  395. {
  396. UIScrollContainer* container = new UIScrollContainer(context_, false);
  397. container->SetWidget(widget);
  398. widgetWrap_[widget] = container;
  399. return container;
  400. }
  401. if (widget->IsOfType<TBInlineSelect>())
  402. {
  403. UIInlineSelect* select = new UIInlineSelect(context_, false);
  404. select->SetWidget(widget);
  405. widgetWrap_[widget] = select;
  406. return select;
  407. }
  408. if (widget->IsOfType<TBSection>())
  409. {
  410. UISection* section = new UISection(context_, false);
  411. section->SetWidget(widget);
  412. widgetWrap_[widget] = section;
  413. return section;
  414. }
  415. if (widget->IsOfType<TBSeparator>())
  416. {
  417. UISeparator* sep = new UISeparator(context_, false);
  418. sep->SetWidget(widget);
  419. widgetWrap_[widget] = sep;
  420. return sep;
  421. }
  422. if (widget->IsOfType<TBContainer>())
  423. {
  424. UIContainer* container = new UIContainer(context_, false);
  425. container->SetWidget(widget);
  426. widgetWrap_[widget] = container;
  427. return container;
  428. }
  429. if (widget->IsOfType<TBButton>())
  430. {
  431. // don't wrap the close button of a TBWindow.close
  432. if (widget->GetID() == TBIDC("TBWindow.close"))
  433. return 0;
  434. UIButton* button = new UIButton(context_, false);
  435. button->SetWidget(widget);
  436. widgetWrap_[widget] = button;
  437. return button;
  438. }
  439. if (widget->IsOfType<TBTextField>())
  440. {
  441. UITextField* textfield = new UITextField(context_, false);
  442. textfield->SetWidget(widget);
  443. widgetWrap_[widget] = textfield;
  444. return textfield;
  445. }
  446. if (widget->IsOfType<TBEditField>())
  447. {
  448. UIEditField* editfield = new UIEditField(context_, false);
  449. editfield->SetWidget(widget);
  450. widgetWrap_[widget] = editfield;
  451. return editfield;
  452. }
  453. if (widget->IsOfType<TBSkinImage>())
  454. {
  455. UISkinImage* skinimage = new UISkinImage(context_, "", false);
  456. skinimage->SetWidget(widget);
  457. widgetWrap_[widget] = skinimage;
  458. return skinimage;
  459. }
  460. if (widget->IsOfType<TBImageWidget>())
  461. {
  462. UIImageWidget* imagewidget = new UIImageWidget(context_, false);
  463. imagewidget->SetWidget(widget);
  464. widgetWrap_[widget] = imagewidget;
  465. return imagewidget;
  466. }
  467. if (widget->IsOfType<TBClickLabel>())
  468. {
  469. UIClickLabel* nwidget = new UIClickLabel(context_, false);
  470. nwidget->SetWidget(widget);
  471. widgetWrap_[widget] = nwidget;
  472. return nwidget;
  473. }
  474. if (widget->IsOfType<TBCheckBox>())
  475. {
  476. UICheckBox* nwidget = new UICheckBox(context_, false);
  477. nwidget->SetWidget(widget);
  478. widgetWrap_[widget] = nwidget;
  479. return nwidget;
  480. }
  481. if (widget->IsOfType<TBSelectList>())
  482. {
  483. UISelectList* nwidget = new UISelectList(context_, false);
  484. nwidget->SetWidget(widget);
  485. widgetWrap_[widget] = nwidget;
  486. return nwidget;
  487. }
  488. if (widget->IsOfType<TBMessageWindow>())
  489. {
  490. UIMessageWindow* nwidget = new UIMessageWindow(context_, NULL, "", false);
  491. nwidget->SetWidget(widget);
  492. widgetWrap_[widget] = nwidget;
  493. return nwidget;
  494. }
  495. if (widget->IsOfType<TBTabContainer>())
  496. {
  497. UITabContainer* nwidget = new UITabContainer(context_, false);
  498. nwidget->SetWidget(widget);
  499. widgetWrap_[widget] = nwidget;
  500. return nwidget;
  501. }
  502. if (widget->IsOfType<SceneViewWidget>())
  503. {
  504. UISceneView* nwidget = new UISceneView(context_, false);
  505. nwidget->SetWidget(widget);
  506. widgetWrap_[widget] = nwidget;
  507. return nwidget;
  508. }
  509. if (widget->IsOfType<TBLayout>())
  510. {
  511. UILayout* layout = new UILayout(context_, (UI_AXIS) widget->GetAxis(), false);
  512. layout->SetWidget(widget);
  513. widgetWrap_[widget] = layout;
  514. return layout;
  515. }
  516. if (widget->IsOfType<TBWidget>())
  517. {
  518. UIWidget* nwidget = new UIWidget(context_, false);
  519. nwidget->SetWidget(widget);
  520. widgetWrap_[widget] = nwidget;
  521. return nwidget;
  522. }
  523. return 0;
  524. }
  525. void UI::OnWidgetDelete(tb::TBWidget *widget)
  526. {
  527. }
  528. bool UI::OnWidgetDying(tb::TBWidget *widget)
  529. {
  530. return false;
  531. }
  532. void UI::ShowDebugHud(bool value)
  533. {
  534. SystemUI::DebugHud* hud = GetSubsystem<SystemUI::DebugHud>();
  535. if (!hud)
  536. return;
  537. if (value)
  538. hud->SetMode(SystemUI::DEBUGHUD_SHOW_ALL);
  539. else
  540. hud->SetMode(SystemUI::DEBUGHUD_SHOW_NONE);
  541. }
  542. void UI::ToggleDebugHud()
  543. {
  544. SystemUI::DebugHud* hud = GetSubsystem<SystemUI::DebugHud>();
  545. if (!hud)
  546. return;
  547. hud->ToggleAll();
  548. }
  549. void UI::ShowConsole(bool value)
  550. {
  551. SystemUI::Console* console = GetSubsystem<SystemUI::Console>();
  552. if (!console)
  553. return;
  554. console->SetVisible(value);
  555. consoleVisible_ = console->IsVisible();
  556. }
  557. void UI::ToggleConsole()
  558. {
  559. SystemUI::Console* console = GetSubsystem<SystemUI::Console>();
  560. if (!console)
  561. return;
  562. console->Toggle();
  563. consoleVisible_ = console->IsVisible();
  564. }
  565. void UI::HandleConsoleClosed(StringHash eventType, VariantMap& eventData)
  566. {
  567. consoleVisible_ = false;
  568. }
  569. }