UI.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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_TOUCHBEGIN, HANDLER(UI, HandleTouchBegin));
  154. SubscribeToEvent(E_TOUCHEND, HANDLER(UI, HandleTouchEnd));
  155. SubscribeToEvent(E_TOUCHMOVE, HANDLER(UI, HandleTouchMove));
  156. SubscribeToEvent(E_RENDERUPDATE, HANDLER(UI, HandleRenderUpdate));
  157. tb::TBWidgetListener::AddGlobalListener(this);
  158. initialized_ = true;
  159. SystemUI::SystemUI* systemUI = new SystemUI::SystemUI(context_);
  160. context_->RegisterSubsystem(systemUI);
  161. systemUI->CreateConsoleAndDebugHud();
  162. //TB_DEBUG_SETTING(LAYOUT_BOUNDS) = 1;
  163. }
  164. void UI::LoadSkin(const String& skin, const String& overrideSkin)
  165. {
  166. // Load the default skin, and override skin (if any)
  167. tb::g_tb_skin->Load(skin.CString(), overrideSkin.CString());
  168. skinLoaded_ = true;
  169. }
  170. void UI::LoadDefaultPlayerSkin()
  171. {
  172. ResourceCache* cache = GetSubsystem<ResourceCache>();
  173. String skin = "DefaultUI/skin/skin.tb.txt";
  174. String overrideSkin;
  175. // see if we have an override skin
  176. SharedPtr<File> skinFile = cache->GetFile("UI/Skin/skin.ui.txt", false);
  177. if (skinFile.NotNull())
  178. {
  179. skinFile->Close();
  180. skin = "UI/Skin/skin.ui.txt";
  181. }
  182. // see if we have an override skin
  183. SharedPtr<File> overrideFile = cache->GetFile("UI/Skin/Override/skin.ui.txt", false);
  184. if (overrideFile.NotNull())
  185. {
  186. overrideFile->Close();
  187. overrideSkin = "UI/Skin/Override/skin.ui.txt";
  188. }
  189. LoadSkin(skin, overrideSkin);
  190. if (skin == "DefaultUI/skin/skin.tb.txt")
  191. {
  192. AddFont("DefaultUI/fonts/vera.ttf", "Vera");
  193. SetDefaultFont("Vera", 12);
  194. }
  195. }
  196. void UI::SetDefaultFont(const String& name, int size)
  197. {
  198. tb::TBFontDescription fd;
  199. fd.SetID(tb::TBIDC(name.CString()));
  200. fd.SetSize(tb::g_tb_skin->GetDimensionConverter()->DpToPx(12));
  201. tb::g_font_manager->SetDefaultFontDescription(fd);
  202. // Create the font now.
  203. tb::TBFontFace *font = tb::g_font_manager->CreateFontFace(tb::g_font_manager->GetDefaultFontDescription());
  204. // Render some glyphs in one go now since we know we are going to use them. It would work fine
  205. // without this since glyphs are rendered when needed, but with some extra updating of the glyph bitmap.
  206. if (font)
  207. font->RenderGlyphs(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~•·åäöÅÄÖ");
  208. }
  209. void UI::AddFont(const String& fontFile, const String& name)
  210. {
  211. tb::g_font_manager->AddFontInfo(fontFile.CString(), name.CString());
  212. }
  213. void UI::Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd)
  214. {
  215. if (batches.Empty())
  216. return;
  217. Vector2 invScreenSize(1.0f / (float)graphics_->GetWidth(), 1.0f / (float)graphics_->GetHeight());
  218. Vector2 scale(2.0f * invScreenSize.x_, -2.0f * invScreenSize.y_);
  219. Vector2 offset(-1.0f, 1.0f);
  220. Matrix4 projection(Matrix4::IDENTITY);
  221. projection.m00_ = scale.x_;
  222. projection.m03_ = offset.x_;
  223. projection.m11_ = scale.y_;
  224. projection.m13_ = offset.y_;
  225. projection.m22_ = 1.0f;
  226. projection.m23_ = 0.0f;
  227. projection.m33_ = 1.0f;
  228. graphics_->ClearParameterSources();
  229. graphics_->SetColorWrite(true);
  230. graphics_->SetCullMode(CULL_NONE);
  231. graphics_->SetDepthTest(CMP_ALWAYS);
  232. graphics_->SetDepthWrite(false);
  233. graphics_->SetFillMode(FILL_SOLID);
  234. graphics_->SetStencilTest(false);
  235. graphics_->ResetRenderTargets();
  236. graphics_->SetVertexBuffer(buffer);
  237. ShaderVariation* noTextureVS = graphics_->GetShader(VS, "Basic", "VERTEXCOLOR");
  238. ShaderVariation* diffTextureVS = graphics_->GetShader(VS, "Basic", "DIFFMAP VERTEXCOLOR");
  239. ShaderVariation* noTexturePS = graphics_->GetShader(PS, "Basic", "VERTEXCOLOR");
  240. ShaderVariation* diffTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP VERTEXCOLOR");
  241. ShaderVariation* diffMaskTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP ALPHAMASK VERTEXCOLOR");
  242. ShaderVariation* alphaTexturePS = graphics_->GetShader(PS, "Basic", "ALPHAMAP VERTEXCOLOR");
  243. unsigned alphaFormat = Graphics::GetAlphaFormat();
  244. for (unsigned i = batchStart; i < batchEnd; ++i)
  245. {
  246. const UIBatch& batch = batches[i];
  247. if (batch.vertexStart_ == batch.vertexEnd_)
  248. continue;
  249. ShaderVariation* ps;
  250. ShaderVariation* vs;
  251. if (!batch.texture_)
  252. {
  253. ps = noTexturePS;
  254. vs = noTextureVS;
  255. }
  256. else
  257. {
  258. // If texture contains only an alpha channel, use alpha shader (for fonts)
  259. vs = diffTextureVS;
  260. if (batch.texture_->GetFormat() == alphaFormat)
  261. ps = alphaTexturePS;
  262. else if (batch.blendMode_ != BLEND_ALPHA && batch.blendMode_ != BLEND_ADDALPHA && batch.blendMode_ != BLEND_PREMULALPHA)
  263. ps = diffMaskTexturePS;
  264. else
  265. ps = diffTexturePS;
  266. }
  267. graphics_->SetShaders(vs, ps);
  268. if (graphics_->NeedParameterUpdate(SP_OBJECT, this))
  269. graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  270. if (graphics_->NeedParameterUpdate(SP_CAMERA, this))
  271. graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
  272. if (graphics_->NeedParameterUpdate(SP_MATERIAL, this))
  273. graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  274. graphics_->SetBlendMode(batch.blendMode_);
  275. graphics_->SetScissorTest(true, batch.scissor_);
  276. graphics_->SetTexture(0, batch.texture_);
  277. graphics_->Draw(TRIANGLE_LIST, batch.vertexStart_ / UI_VERTEX_SIZE, (batch.vertexEnd_ - batch.vertexStart_) /
  278. UI_VERTEX_SIZE);
  279. }
  280. }
  281. void UI::SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData)
  282. {
  283. if (vertexData.Empty())
  284. return;
  285. // Update quad geometry into the vertex buffer
  286. // Resize the vertex buffer first if too small or much too large
  287. unsigned numVertices = vertexData.Size() / UI_VERTEX_SIZE;
  288. if (dest->GetVertexCount() < numVertices || dest->GetVertexCount() > numVertices * 2)
  289. dest->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
  290. dest->SetData(&vertexData[0]);
  291. }
  292. void UI::Render(bool resetRenderTargets)
  293. {
  294. SetVertexData(vertexBuffer_, vertexData_);
  295. Render(vertexBuffer_, batches_, 0, batches_.Size());
  296. GetSubsystem<SystemUI::SystemUI>()->Render();
  297. }
  298. void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  299. {
  300. // Get rendering batches from the non-modal UI elements
  301. batches_.Clear();
  302. vertexData_.Clear();
  303. tb::TBRect rect = rootWidget_->GetRect();
  304. IntRect currentScissor = IntRect(0, 0, rect.w, rect.h);
  305. GetBatches(batches_, vertexData_, currentScissor);
  306. }
  307. void UI::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  308. {
  309. //if (!initialized_)
  310. // return;
  311. TBAnimationManager::Update();
  312. rootWidget_->InvokeProcessStates();
  313. rootWidget_->InvokeProcess();
  314. tb::g_renderer->BeginPaint(rootWidget_->GetRect().w, rootWidget_->GetRect().h);
  315. renderer_->currentScissor_ = currentScissor;
  316. renderer_->batches_ = &batches;
  317. renderer_->vertexData_ = &vertexData;
  318. rootWidget_->InvokePaint(tb::TBWidget::PaintProps());
  319. tb::g_renderer->EndPaint();
  320. }
  321. void UI::SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData)
  322. {
  323. UIBatch b(BLEND_ALPHA , renderer_->currentScissor_, texture, &vertexData_);
  324. unsigned begin = b.vertexData_->Size();
  325. b.vertexData_->Resize(begin + vertexData.Size());
  326. float* dest = &(b.vertexData_->At(begin));
  327. b.vertexEnd_ = b.vertexData_->Size();
  328. for (unsigned i = 0; i < vertexData.Size(); i++, dest++)
  329. {
  330. *dest = vertexData[i];
  331. }
  332. UIBatch::AddOrMerge(b, batches_);
  333. }
  334. void UI::TBFileReader(const char* filename, void** data, unsigned* length)
  335. {
  336. *data = 0;
  337. *length = 0;
  338. ResourceCache* cache = uiContext_->GetSubsystem<ResourceCache>();
  339. SharedPtr<File> file = cache->GetFile(filename);
  340. if (!file || !file->IsOpen())
  341. {
  342. LOGERRORF("UI::TBFileReader: Unable to load file: %s", filename);
  343. return;
  344. }
  345. unsigned size = file->GetSize();
  346. if (!size)
  347. return;
  348. void* _data = malloc(size);
  349. if (!_data)
  350. return;
  351. if (file->Read(_data, size) != size)
  352. {
  353. free(_data);
  354. return;
  355. }
  356. *length = size;
  357. *data = _data;
  358. }
  359. void UI::GetTBIDString(unsigned id, String& value)
  360. {
  361. if (!id)
  362. {
  363. value = "";
  364. }
  365. else
  366. {
  367. value = tbidToString_[id];
  368. }
  369. }
  370. void UI::TBIDRegisterStringCallback(unsigned id, const char* value)
  371. {
  372. uiContext_->GetSubsystem<UI>()->tbidToString_[id] = String(value);
  373. }
  374. bool UI::LoadResourceFile(TBWidget* widget, const String& filename)
  375. {
  376. tb::TBNode node;
  377. if (!node.ReadFile(filename.CString()))
  378. return false;
  379. tb::g_widgets_reader->LoadNodeTree(widget, &node);
  380. return true;
  381. }
  382. void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  383. {
  384. using namespace ScreenMode;
  385. rootWidget_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  386. //SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  387. }
  388. void UI::HandleUpdate(StringHash eventType, VariantMap& eventData)
  389. {
  390. if (exitRequested_) {
  391. SendEvent(E_EXITREQUESTED);
  392. exitRequested_ = false;
  393. return;
  394. }
  395. TBMessageHandler::ProcessMessages();
  396. }
  397. bool UI::IsWidgetWrapped(tb::TBWidget* widget)
  398. {
  399. return widgetWrap_.Contains(widget);
  400. }
  401. bool UI::UnwrapWidget(tb::TBWidget* widget)
  402. {
  403. if (widgetWrap_.Contains(widget))
  404. {
  405. widgetWrap_.Erase(widget);
  406. return true;
  407. }
  408. return false;
  409. }
  410. void UI::PruneUnreachableWidgets()
  411. {
  412. HashMap<tb::TBWidget*, SharedPtr<UIWidget>>::Iterator itr;
  413. for (itr = widgetWrap_.Begin(); itr != widgetWrap_.End(); )
  414. {
  415. if ((*itr).first_->GetParent() || (*itr).second_->JSGetHeapPtr())
  416. {
  417. itr++;
  418. continue;
  419. }
  420. tb::TBWidget* toDelete = (*itr).first_;
  421. itr.GotoNext();
  422. delete toDelete;
  423. // this will likely be flagged by valgrind as accessing invalid memory
  424. assert(!widgetWrap_.Contains(toDelete));
  425. }
  426. }
  427. void UI::WrapWidget(UIWidget* widget, tb::TBWidget* tbwidget)
  428. {
  429. assert (!widgetWrap_.Contains(tbwidget));
  430. widgetWrap_[tbwidget] = widget;
  431. }
  432. UIWidget* UI::WrapWidget(tb::TBWidget* widget)
  433. {
  434. if (!widget)
  435. return NULL;
  436. if (widgetWrap_.Contains(widget))
  437. return widgetWrap_[widget];
  438. // switch this to use a factory?
  439. // this is order dependent as we're using IsOfType which also works if a base class
  440. if (widget->IsOfType<TBDimmer>())
  441. {
  442. UIDimmer* dimmer = new UIDimmer(context_, false);
  443. dimmer->SetWidget(widget);
  444. widgetWrap_[widget] = dimmer;
  445. return dimmer;
  446. }
  447. if (widget->IsOfType<TBScrollContainer>())
  448. {
  449. UIScrollContainer* container = new UIScrollContainer(context_, false);
  450. container->SetWidget(widget);
  451. widgetWrap_[widget] = container;
  452. return container;
  453. }
  454. if (widget->IsOfType<TBInlineSelect>())
  455. {
  456. UIInlineSelect* select = new UIInlineSelect(context_, false);
  457. select->SetWidget(widget);
  458. widgetWrap_[widget] = select;
  459. return select;
  460. }
  461. if (widget->IsOfType<TBSection>())
  462. {
  463. UISection* section = new UISection(context_, false);
  464. section->SetWidget(widget);
  465. widgetWrap_[widget] = section;
  466. return section;
  467. }
  468. if (widget->IsOfType<TBSeparator>())
  469. {
  470. UISeparator* sep = new UISeparator(context_, false);
  471. sep->SetWidget(widget);
  472. widgetWrap_[widget] = sep;
  473. return sep;
  474. }
  475. if (widget->IsOfType<TBContainer>())
  476. {
  477. UIContainer* container = new UIContainer(context_, false);
  478. container->SetWidget(widget);
  479. widgetWrap_[widget] = container;
  480. return container;
  481. }
  482. if (widget->IsOfType<TBSelectDropdown>())
  483. {
  484. UISelectDropdown* select = new UISelectDropdown(context_, false);
  485. select->SetWidget(widget);
  486. widgetWrap_[widget] = select;
  487. return select;
  488. }
  489. if (widget->IsOfType<TBButton>())
  490. {
  491. // don't wrap the close button of a TBWindow.close
  492. if (widget->GetID() == TBIDC("TBWindow.close"))
  493. return 0;
  494. UIButton* button = new UIButton(context_, false);
  495. button->SetWidget(widget);
  496. widgetWrap_[widget] = button;
  497. return button;
  498. }
  499. if (widget->IsOfType<TBTextField>())
  500. {
  501. UITextField* textfield = new UITextField(context_, false);
  502. textfield->SetWidget(widget);
  503. widgetWrap_[widget] = textfield;
  504. return textfield;
  505. }
  506. if (widget->IsOfType<TBEditField>())
  507. {
  508. UIEditField* editfield = new UIEditField(context_, false);
  509. editfield->SetWidget(widget);
  510. widgetWrap_[widget] = editfield;
  511. return editfield;
  512. }
  513. if (widget->IsOfType<TBSkinImage>())
  514. {
  515. UISkinImage* skinimage = new UISkinImage(context_, "", false);
  516. skinimage->SetWidget(widget);
  517. widgetWrap_[widget] = skinimage;
  518. return skinimage;
  519. }
  520. if (widget->IsOfType<TBImageWidget>())
  521. {
  522. UIImageWidget* imagewidget = new UIImageWidget(context_, false);
  523. imagewidget->SetWidget(widget);
  524. widgetWrap_[widget] = imagewidget;
  525. return imagewidget;
  526. }
  527. if (widget->IsOfType<TBClickLabel>())
  528. {
  529. UIClickLabel* nwidget = new UIClickLabel(context_, false);
  530. nwidget->SetWidget(widget);
  531. widgetWrap_[widget] = nwidget;
  532. return nwidget;
  533. }
  534. if (widget->IsOfType<TBCheckBox>())
  535. {
  536. UICheckBox* nwidget = new UICheckBox(context_, false);
  537. nwidget->SetWidget(widget);
  538. widgetWrap_[widget] = nwidget;
  539. return nwidget;
  540. }
  541. if (widget->IsOfType<TBSelectList>())
  542. {
  543. UISelectList* nwidget = new UISelectList(context_, false);
  544. nwidget->SetWidget(widget);
  545. widgetWrap_[widget] = nwidget;
  546. return nwidget;
  547. }
  548. if (widget->IsOfType<TBMessageWindow>())
  549. {
  550. UIMessageWindow* nwidget = new UIMessageWindow(context_, NULL, "", false);
  551. nwidget->SetWidget(widget);
  552. widgetWrap_[widget] = nwidget;
  553. return nwidget;
  554. }
  555. if (widget->IsOfType<TBTabContainer>())
  556. {
  557. UITabContainer* nwidget = new UITabContainer(context_, false);
  558. nwidget->SetWidget(widget);
  559. widgetWrap_[widget] = nwidget;
  560. return nwidget;
  561. }
  562. if (widget->IsOfType<SceneViewWidget>())
  563. {
  564. UISceneView* nwidget = new UISceneView(context_, false);
  565. nwidget->SetWidget(widget);
  566. widgetWrap_[widget] = nwidget;
  567. return nwidget;
  568. }
  569. if (widget->IsOfType<TBLayout>())
  570. {
  571. UILayout* layout = new UILayout(context_, (UI_AXIS) widget->GetAxis(), false);
  572. layout->SetWidget(widget);
  573. widgetWrap_[widget] = layout;
  574. return layout;
  575. }
  576. if (widget->IsOfType<TBWidget>())
  577. {
  578. UIWidget* nwidget = new UIWidget(context_, false);
  579. nwidget->SetWidget(widget);
  580. widgetWrap_[widget] = nwidget;
  581. return nwidget;
  582. }
  583. return 0;
  584. }
  585. void UI::OnWidgetDelete(tb::TBWidget *widget)
  586. {
  587. }
  588. bool UI::OnWidgetDying(tb::TBWidget *widget)
  589. {
  590. return false;
  591. }
  592. void UI::ShowDebugHud(bool value)
  593. {
  594. SystemUI::DebugHud* hud = GetSubsystem<SystemUI::DebugHud>();
  595. if (!hud)
  596. return;
  597. if (value)
  598. hud->SetMode(SystemUI::DEBUGHUD_SHOW_ALL);
  599. else
  600. hud->SetMode(SystemUI::DEBUGHUD_SHOW_NONE);
  601. }
  602. void UI::ToggleDebugHud()
  603. {
  604. SystemUI::DebugHud* hud = GetSubsystem<SystemUI::DebugHud>();
  605. if (!hud)
  606. return;
  607. hud->ToggleAll();
  608. }
  609. void UI::ShowConsole(bool value)
  610. {
  611. SystemUI::Console* console = GetSubsystem<SystemUI::Console>();
  612. if (!console)
  613. return;
  614. console->SetVisible(value);
  615. consoleVisible_ = console->IsVisible();
  616. }
  617. void UI::ToggleConsole()
  618. {
  619. SystemUI::Console* console = GetSubsystem<SystemUI::Console>();
  620. if (!console)
  621. return;
  622. console->Toggle();
  623. consoleVisible_ = console->IsVisible();
  624. }
  625. void UI::HandleConsoleClosed(StringHash eventType, VariantMap& eventData)
  626. {
  627. consoleVisible_ = false;
  628. }
  629. SystemUI::MessageBox* UI::ShowSystemMessageBox(const String& title, const String& message)
  630. {
  631. ResourceCache* cache = GetSubsystem<ResourceCache>();
  632. XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  633. SystemUI::MessageBox* messageBox = new SystemUI::MessageBox(context_, message, title, 0, xmlFile);
  634. return messageBox;
  635. }
  636. }