UI.cpp 20 KB

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