UI.cpp 20 KB

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