UI.cpp 22 KB

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