UI.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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/tb_menu_window.h>
  39. #include <TurboBadger/tb_popup_window.h>
  40. #include <TurboBadger/image/tb_image_widget.h>
  41. #include <TurboBadger/tb_atomic_widgets.h>
  42. void register_tbbf_font_renderer();
  43. void register_stb_font_renderer();
  44. void register_freetype_font_renderer();
  45. using namespace tb;
  46. #include "../Core/CoreEvents.h"
  47. #include "../IO/Log.h"
  48. #include "../IO/FileSystem.h"
  49. #include "../Input/Input.h"
  50. #include "../Input/InputEvents.h"
  51. #include "../Resource/ResourceCache.h"
  52. #include "../Graphics/Graphics.h"
  53. #include "../Graphics/GraphicsEvents.h"
  54. #include "../Graphics/Texture2D.h"
  55. #include "../Graphics/VertexBuffer.h"
  56. #include "UIEvents.h"
  57. #include "UIRenderer.h"
  58. #include "UI.h"
  59. #include "UIView.h"
  60. #include "UIButton.h"
  61. #include "UITextField.h"
  62. #include "UIEditField.h"
  63. #include "UILayout.h"
  64. #include "UIImageWidget.h"
  65. #include "UIClickLabel.h"
  66. #include "UICheckBox.h"
  67. #include "UISelectList.h"
  68. #include "UIMessageWindow.h"
  69. #include "UISkinImage.h"
  70. #include "UITabContainer.h"
  71. #include "UISceneView.h"
  72. #include "UIDragDrop.h"
  73. #include "UIContainer.h"
  74. #include "UISection.h"
  75. #include "UIInlineSelect.h"
  76. #include "UIScrollContainer.h"
  77. #include "UISeparator.h"
  78. #include "UIDimmer.h"
  79. #include "UISelectDropdown.h"
  80. #include "UIMenuWindow.h"
  81. #include "UIPopupWindow.h"
  82. #include "UISlider.h"
  83. #include "UIColorWidget.h"
  84. #include "UIColorWheel.h"
  85. #include "UIBargraph.h"
  86. #include "UIPromptWindow.h"
  87. #include "UIFinderWindow.h"
  88. #include "UIPulldownMenu.h"
  89. #include "UIComponent.h"
  90. #include "UIRadioButton.h"
  91. #include "UIScrollBar.h"
  92. #include "SystemUI/SystemUI.h"
  93. #include "SystemUI/SystemUIEvents.h"
  94. #include "SystemUI/DebugHud.h"
  95. #include "SystemUI/Console.h"
  96. #include "SystemUI/MessageBox.h"
  97. namespace tb
  98. {
  99. void TBSystem::RescheduleTimer(double fire_time)
  100. {
  101. }
  102. }
  103. namespace Atomic
  104. {
  105. void RegisterUILibrary(Context* context)
  106. {
  107. UIComponent::RegisterObject(context);
  108. }
  109. WeakPtr<Context> UI::uiContext_;
  110. UI::UI(Context* context) :
  111. Object(context),
  112. rootWidget_(0),
  113. inputDisabled_(false),
  114. keyboardDisabled_(false),
  115. initialized_(false),
  116. skinLoaded_(false),
  117. consoleVisible_(false),
  118. exitRequested_(false),
  119. changedEventsBlocked_(0),
  120. tooltipHoverTime_ (0.0f)
  121. {
  122. RegisterUILibrary(context);
  123. SubscribeToEvent(E_EXITREQUESTED, ATOMIC_HANDLER(UI, HandleExitRequested));
  124. }
  125. UI::~UI()
  126. {
  127. if (initialized_)
  128. {
  129. initialized_ = false;
  130. tb::TBAnimationManager::AbortAllAnimations();
  131. tb::TBWidgetListener::RemoveGlobalListener(this);
  132. TBFile::SetReaderFunction(0);
  133. TBID::tbidRegisterCallback = 0;
  134. tb::TBWidgetsAnimationManager::Shutdown();
  135. delete rootWidget_;
  136. widgetWrap_.Clear();
  137. // leak
  138. //delete TBUIRenderer::renderer_;
  139. tb_core_shutdown();
  140. }
  141. uiContext_ = 0;
  142. }
  143. void UI::HandleExitRequested(StringHash eventType, VariantMap& eventData)
  144. {
  145. Shutdown();
  146. }
  147. void UI::Shutdown()
  148. {
  149. }
  150. bool UI::GetFocusedWidget()
  151. {
  152. if (!TBWidget::focused_widget)
  153. return false;
  154. return TBWidget::focused_widget->IsOfType<TBEditField>();
  155. }
  156. void UI::SetBlockChangedEvents(bool blocked)
  157. {
  158. if (blocked)
  159. changedEventsBlocked_++;
  160. else
  161. {
  162. changedEventsBlocked_--;
  163. if (changedEventsBlocked_ < 0)
  164. {
  165. ATOMIC_LOGERROR("UI::BlockChangedEvents - mismatched block calls, setting to 0");
  166. changedEventsBlocked_ = 0;
  167. }
  168. }
  169. }
  170. void UI::Initialize(const String& languageFile)
  171. {
  172. Graphics* graphics = GetSubsystem<Graphics>();
  173. assert(graphics);
  174. assert(graphics->IsInitialized());
  175. graphics_ = graphics;
  176. uiContext_ = context_;
  177. TBFile::SetReaderFunction(TBFileReader);
  178. TBID::tbidRegisterCallback = UI::TBIDRegisterStringCallback;
  179. TBWidgetsAnimationManager::Init();
  180. renderer_ = new UIRenderer(graphics_->GetContext());
  181. tb_core_init(renderer_, languageFile.CString());
  182. //register_tbbf_font_renderer();
  183. //register_stb_font_renderer();
  184. register_freetype_font_renderer();
  185. rootWidget_ = new TBWidget();
  186. int width = graphics_->GetWidth();
  187. int height = graphics_->GetHeight();
  188. rootWidget_->SetSize(width, height);
  189. rootWidget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
  190. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(UI, HandleUpdate));
  191. SubscribeToEvent(E_SCREENMODE, ATOMIC_HANDLER(UI, HandleScreenMode));
  192. SubscribeToEvent(E_CONSOLECLOSED, ATOMIC_HANDLER(UI, HandleConsoleClosed));
  193. SubscribeToEvent(E_POSTUPDATE, ATOMIC_HANDLER(UI, HandlePostUpdate));
  194. SubscribeToEvent(E_RENDERUPDATE, ATOMIC_HANDLER(UI, HandleRenderUpdate));
  195. // register the UIDragDrop subsystem (after we have subscribed to events, so it is processed after)
  196. context_->RegisterSubsystem(new UIDragDrop(context_));
  197. tb::TBWidgetListener::AddGlobalListener(this);
  198. initialized_ = true;
  199. SystemUI* systemUI = new SystemUI(context_);
  200. context_->RegisterSubsystem(systemUI);
  201. systemUI->CreateConsoleAndDebugHud();
  202. //TB_DEBUG_SETTING(LAYOUT_BOUNDS) = 1;
  203. }
  204. void UI::LoadSkin(const String& skin, const String& overrideSkin)
  205. {
  206. // Load the default skin, and override skin (if any)
  207. tb::g_tb_skin->Load(skin.CString(), overrideSkin.CString());
  208. skinLoaded_ = true;
  209. }
  210. void UI::LoadDefaultPlayerSkin()
  211. {
  212. ResourceCache* cache = GetSubsystem<ResourceCache>();
  213. String skin = "DefaultUI/skin/skin.tb.txt";
  214. String overrideSkin;
  215. // see if we have an override skin
  216. SharedPtr<File> skinFile = cache->GetFile("UI/Skin/skin.ui.txt", false);
  217. if (skinFile.NotNull())
  218. {
  219. skinFile->Close();
  220. skin = "UI/Skin/skin.ui.txt";
  221. }
  222. // see if we have an override skin
  223. SharedPtr<File> overrideFile = cache->GetFile("UI/Skin/Override/skin.ui.txt", false);
  224. if (overrideFile.NotNull())
  225. {
  226. overrideFile->Close();
  227. overrideSkin = "UI/Skin/Override/skin.ui.txt";
  228. }
  229. LoadSkin(skin, overrideSkin);
  230. if (skin == "DefaultUI/skin/skin.tb.txt")
  231. {
  232. AddFont("DefaultUI/fonts/vera.ttf", "Vera");
  233. SetDefaultFont("Vera", 12);
  234. }
  235. }
  236. void UI::SetDefaultFont(const String& name, int size)
  237. {
  238. tb::TBFontDescription fd;
  239. fd.SetID(tb::TBIDC(name.CString()));
  240. fd.SetSize(tb::g_tb_skin->GetDimensionConverter()->DpToPx(size));
  241. tb::g_font_manager->SetDefaultFontDescription(fd);
  242. // Create the font now.
  243. tb::TBFontFace *font = tb::g_font_manager->CreateFontFace(tb::g_font_manager->GetDefaultFontDescription());
  244. // Render some glyphs in one go now since we know we are going to use them. It would work fine
  245. // without this since glyphs are rendered when needed, but with some extra updating of the glyph bitmap.
  246. if (font)
  247. font->RenderGlyphs(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~•·åäöÅÄÖ");
  248. }
  249. void UI::AddFont(const String& fontFile, const String& name)
  250. {
  251. tb::g_font_manager->AddFontInfo(fontFile.CString(), name.CString());
  252. }
  253. void UI::AddUIView(UIView* uiView)
  254. {
  255. rootWidget_->AddChild(uiView->GetInternalWidget());
  256. uiViews_.Push(SharedPtr<UIView>(uiView));
  257. if (!focusedView_ && uiView)
  258. {
  259. uiView->SetFocus();
  260. }
  261. }
  262. void UI::RemoveUIView(UIView* uiView)
  263. {
  264. if (focusedView_ == uiView)
  265. {
  266. SetFocusedView(0);
  267. }
  268. rootWidget_->RemoveChild(uiView->GetInternalWidget());
  269. uiViews_.Remove(SharedPtr<UIView>(uiView));
  270. }
  271. void UI::SetFocusedView(UIView* uiView)
  272. {
  273. if (focusedView_ == uiView)
  274. {
  275. return;
  276. }
  277. focusedView_ = uiView;
  278. if (focusedView_)
  279. {
  280. focusedView_->BecomeFocused();
  281. }
  282. else
  283. {
  284. focusedView_ = 0;
  285. // look for first auto activated UIView, and recurse
  286. Vector<SharedPtr<UIView>>::Iterator itr = uiViews_.Begin();
  287. while (itr != uiViews_.End())
  288. {
  289. if ((*itr)->GetAutoFocus())
  290. {
  291. SetFocusedView(*itr);
  292. return;
  293. }
  294. itr++;
  295. }
  296. }
  297. }
  298. void UI::Render(bool resetRenderTargets)
  299. {
  300. Vector<SharedPtr<UIView>>::Iterator itr = uiViews_.Begin();
  301. while (itr != uiViews_.End())
  302. {
  303. (*itr)->Render(resetRenderTargets);
  304. itr++;
  305. }
  306. }
  307. void UI::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  308. {
  309. TBAnimationManager::Update();
  310. rootWidget_->InvokeProcessStates();
  311. rootWidget_->InvokeProcess();
  312. }
  313. void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  314. {
  315. Vector<SharedPtr<UIView>>::Iterator itr = uiViews_.Begin();
  316. while (itr != uiViews_.End())
  317. {
  318. (*itr)->UpdateUIBatches();
  319. itr++;
  320. }
  321. }
  322. void UI::TBFileReader(const char* filename, void** data, unsigned* length)
  323. {
  324. *data = 0;
  325. *length = 0;
  326. ResourceCache* cache = uiContext_->GetSubsystem<ResourceCache>();
  327. SharedPtr<File> file = cache->GetFile(filename);
  328. if (!file || !file->IsOpen())
  329. {
  330. ATOMIC_LOGERRORF("UI::TBFileReader: Unable to load file: %s", filename);
  331. return;
  332. }
  333. unsigned size = file->GetSize();
  334. if (!size)
  335. return;
  336. void* _data = malloc(size);
  337. if (!_data)
  338. return;
  339. if (file->Read(_data, size) != size)
  340. {
  341. free(_data);
  342. return;
  343. }
  344. *length = size;
  345. *data = _data;
  346. }
  347. void UI::GetTBIDString(unsigned id, String& value)
  348. {
  349. if (!id)
  350. {
  351. value = "";
  352. }
  353. else
  354. {
  355. value = tbidToString_[id];
  356. }
  357. }
  358. void UI::TBIDRegisterStringCallback(unsigned id, const char* value)
  359. {
  360. uiContext_->GetSubsystem<UI>()->tbidToString_[id] = String(value);
  361. }
  362. bool UI::LoadResourceFile(TBWidget* widget, const String& filename)
  363. {
  364. tb::TBNode node;
  365. if (!node.ReadFile(filename.CString()))
  366. return false;
  367. tb::g_widgets_reader->LoadNodeTree(widget, &node);
  368. return true;
  369. }
  370. void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  371. {
  372. using namespace ScreenMode;
  373. rootWidget_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  374. }
  375. void UI::HandleUpdate(StringHash eventType, VariantMap& eventData)
  376. {
  377. if (exitRequested_) {
  378. SendEvent(E_EXITREQUESTED);
  379. exitRequested_ = false;
  380. return;
  381. }
  382. tooltipHoverTime_ += eventData[Update::P_TIMESTEP].GetFloat();
  383. if (tooltipHoverTime_ >= 0.5f)
  384. {
  385. UIWidget* hoveredWidget = GetHoveredWidget();
  386. if (hoveredWidget && !tooltip_ && (hoveredWidget->GetShortened() || hoveredWidget->GetTooltip().Length() > 0))
  387. {
  388. tooltip_ = new UIPopupWindow(context_, true, hoveredWidget, "tooltip");
  389. UILayout* tooltipLayout = new UILayout(context_, UI_AXIS_Y, true);
  390. if (hoveredWidget->GetShortened())
  391. {
  392. UITextField* fullTextField = new UITextField(context_, true);
  393. fullTextField->SetText(hoveredWidget->GetText());
  394. tooltipLayout->AddChild(fullTextField);
  395. }
  396. if (hoveredWidget->GetTooltip().Length() > 0)
  397. {
  398. UITextField* tooltipTextField = new UITextField(context_, true);
  399. tooltipTextField->SetText(hoveredWidget->GetTooltip());
  400. tooltipLayout->AddChild(tooltipTextField);
  401. }
  402. Input* input = GetSubsystem<Input>();
  403. IntVector2 mousePosition = input->GetMousePosition();
  404. tooltip_->AddChild(tooltipLayout);
  405. tooltip_->Show(mousePosition.x_ + 8, mousePosition.y_ + 8);
  406. }
  407. }
  408. else
  409. {
  410. if (tooltip_) tooltip_->Close();
  411. }
  412. SendEvent(E_UIUPDATE);
  413. TBMessageHandler::ProcessMessages();
  414. }
  415. UIWidget* UI::GetHoveredWidget()
  416. {
  417. return WrapWidget(TBWidget::hovered_widget);
  418. }
  419. bool UI::IsWidgetWrapped(tb::TBWidget* widget)
  420. {
  421. return widgetWrap_.Contains(widget);
  422. }
  423. bool UI::UnwrapWidget(tb::TBWidget* widget)
  424. {
  425. if (widgetWrap_.Contains(widget))
  426. {
  427. widget->SetDelegate(0);
  428. widgetWrap_.Erase(widget);
  429. return true;
  430. }
  431. return false;
  432. }
  433. void UI::PruneUnreachableWidgets()
  434. {
  435. HashMap<tb::TBWidget*, SharedPtr<UIWidget>>::Iterator itr;
  436. for (itr = widgetWrap_.Begin(); itr != widgetWrap_.End(); )
  437. {
  438. if ((*itr).first_->GetParent() || (*itr).second_->Refs() > 1)
  439. {
  440. itr++;
  441. continue;
  442. }
  443. itr.GotoNext();
  444. VariantMap eventData;
  445. eventData[WidgetDeleted::P_WIDGET] = (UIWidget*) (*itr).second_;
  446. (*itr).second_->SendEvent(E_WIDGETDELETED, eventData);
  447. tb::TBWidget* toDelete = (*itr).first_;
  448. UnwrapWidget(toDelete);
  449. delete toDelete;
  450. }
  451. }
  452. void UI::WrapWidget(UIWidget* widget, tb::TBWidget* tbwidget)
  453. {
  454. assert (!widgetWrap_.Contains(tbwidget));
  455. widgetWrap_[tbwidget] = widget;
  456. }
  457. UIWidget* UI::WrapWidget(tb::TBWidget* widget)
  458. {
  459. if (!widget)
  460. return NULL;
  461. if (widgetWrap_.Contains(widget))
  462. return widgetWrap_[widget];
  463. // switch this to use a factory?
  464. // this is order dependent as we're using IsOfType which also works if a base class
  465. if (widget->IsOfType<TBPopupWindow>())
  466. {
  467. UIPopupWindow* popupWindow = new UIPopupWindow(context_, false);
  468. popupWindow->SetWidget(widget);
  469. WrapWidget(popupWindow, widget);
  470. return popupWindow;
  471. }
  472. if (widget->IsOfType<TBDimmer>())
  473. {
  474. UIDimmer* dimmer = new UIDimmer(context_, false);
  475. dimmer->SetWidget(widget);
  476. WrapWidget(dimmer, widget);
  477. return dimmer;
  478. }
  479. if (widget->IsOfType<TBScrollContainer>())
  480. {
  481. UIScrollContainer* container = new UIScrollContainer(context_, false);
  482. container->SetWidget(widget);
  483. WrapWidget(container, widget);
  484. return container;
  485. }
  486. if (widget->IsOfType<TBInlineSelect>())
  487. {
  488. UIInlineSelect* select = new UIInlineSelect(context_, false);
  489. select->SetWidget(widget);
  490. WrapWidget(select, widget);
  491. return select;
  492. }
  493. if (widget->IsOfType<TBSlider>())
  494. {
  495. UISlider* slider = new UISlider(context_, false);
  496. slider->SetWidget(widget);
  497. WrapWidget(slider, widget);
  498. return slider;
  499. }
  500. if (widget->IsOfType<TBScrollBar>())
  501. {
  502. UIScrollBar* slider = new UIScrollBar(context_, false);
  503. slider->SetWidget(widget);
  504. WrapWidget(slider, widget);
  505. return slider;
  506. }
  507. if (widget->IsOfType<TBColorWidget>())
  508. {
  509. UIColorWidget* colorWidget = new UIColorWidget(context_, false);
  510. colorWidget->SetWidget(widget);
  511. WrapWidget(colorWidget, widget);
  512. return colorWidget;
  513. }
  514. if (widget->IsOfType<TBColorWheel>())
  515. {
  516. UIColorWheel* colorWheel = new UIColorWheel(context_, false);
  517. colorWheel->SetWidget(widget);
  518. WrapWidget(colorWheel, widget);
  519. return colorWheel;
  520. }
  521. if (widget->IsOfType<TBSection>())
  522. {
  523. UISection* section = new UISection(context_, false);
  524. section->SetWidget(widget);
  525. WrapWidget(section, widget);
  526. return section;
  527. }
  528. if (widget->IsOfType<TBSeparator>())
  529. {
  530. UISeparator* sep = new UISeparator(context_, false);
  531. sep->SetWidget(widget);
  532. WrapWidget(sep, widget);
  533. return sep;
  534. }
  535. if (widget->IsOfType<TBContainer>())
  536. {
  537. UIContainer* container = new UIContainer(context_, false);
  538. container->SetWidget(widget);
  539. WrapWidget(container, widget);
  540. return container;
  541. }
  542. if (widget->IsOfType<TBSelectDropdown>())
  543. {
  544. UISelectDropdown* select = new UISelectDropdown(context_, false);
  545. select->SetWidget(widget);
  546. WrapWidget(select, widget);
  547. return select;
  548. }
  549. if (widget->IsOfType<TBPulldownMenu>())
  550. {
  551. UIPulldownMenu* select = new UIPulldownMenu(context_, false);
  552. select->SetWidget(widget);
  553. WrapWidget(select, widget);
  554. return select;
  555. }
  556. if (widget->IsOfType<TBButton>())
  557. {
  558. // don't wrap the close button of a TBWindow.close
  559. if (widget->GetID() == TBIDC("TBWindow.close"))
  560. return 0;
  561. UIButton* button = new UIButton(context_, false);
  562. button->SetWidget(widget);
  563. WrapWidget(button, widget);
  564. return button;
  565. }
  566. if (widget->IsOfType<TBTextField>())
  567. {
  568. UITextField* textfield = new UITextField(context_, false);
  569. textfield->SetWidget(widget);
  570. WrapWidget(textfield, widget);
  571. return textfield;
  572. }
  573. if (widget->IsOfType<TBEditField>())
  574. {
  575. UIEditField* editfield = new UIEditField(context_, false);
  576. editfield->SetWidget(widget);
  577. WrapWidget(editfield, widget);
  578. return editfield;
  579. }
  580. if (widget->IsOfType<TBSkinImage>())
  581. {
  582. UISkinImage* skinimage = new UISkinImage(context_, "", false);
  583. skinimage->SetWidget(widget);
  584. WrapWidget(skinimage, widget);
  585. return skinimage;
  586. }
  587. if (widget->IsOfType<TBImageWidget>())
  588. {
  589. UIImageWidget* imagewidget = new UIImageWidget(context_, false);
  590. imagewidget->SetWidget(widget);
  591. WrapWidget(imagewidget, widget);
  592. return imagewidget;
  593. }
  594. if (widget->IsOfType<TBClickLabel>())
  595. {
  596. UIClickLabel* nwidget = new UIClickLabel(context_, false);
  597. nwidget->SetWidget(widget);
  598. WrapWidget(nwidget, widget);
  599. return nwidget;
  600. }
  601. if (widget->IsOfType<TBCheckBox>())
  602. {
  603. UICheckBox* nwidget = new UICheckBox(context_, false);
  604. nwidget->SetWidget(widget);
  605. WrapWidget(nwidget, widget);
  606. return nwidget;
  607. }
  608. if (widget->IsOfType<TBRadioButton>())
  609. {
  610. UIRadioButton* nwidget = new UIRadioButton(context_, false);
  611. nwidget->SetWidget(widget);
  612. WrapWidget(nwidget, widget);
  613. return nwidget;
  614. }
  615. if (widget->IsOfType<TBBarGraph>())
  616. {
  617. UIBargraph* nwidget = new UIBargraph(context_, false);
  618. nwidget->SetWidget(widget);
  619. WrapWidget(nwidget, widget);
  620. return nwidget;
  621. }
  622. if (widget->IsOfType<TBSelectList>())
  623. {
  624. UISelectList* nwidget = new UISelectList(context_, false);
  625. nwidget->SetWidget(widget);
  626. WrapWidget(nwidget, widget);
  627. return nwidget;
  628. }
  629. if (widget->IsOfType<TBMessageWindow>())
  630. {
  631. UIMessageWindow* nwidget = new UIMessageWindow(context_, NULL, "", false);
  632. nwidget->SetWidget(widget);
  633. WrapWidget(nwidget, widget);
  634. return nwidget;
  635. }
  636. if (widget->IsOfType<TBPromptWindow>())
  637. {
  638. UIPromptWindow* nwidget = new UIPromptWindow(context_, NULL, "", false);
  639. nwidget->SetWidget(widget);
  640. WrapWidget(nwidget, widget);
  641. return nwidget;
  642. }
  643. if (widget->IsOfType<TBFinderWindow>())
  644. {
  645. UIFinderWindow* nwidget = new UIFinderWindow(context_, NULL, "", false);
  646. nwidget->SetWidget(widget);
  647. WrapWidget(nwidget, widget);
  648. return nwidget;
  649. }
  650. if (widget->IsOfType<TBTabContainer>())
  651. {
  652. UITabContainer* nwidget = new UITabContainer(context_, false);
  653. nwidget->SetWidget(widget);
  654. WrapWidget(nwidget, widget);
  655. return nwidget;
  656. }
  657. if (widget->IsOfType<SceneViewWidget>())
  658. {
  659. UISceneView* nwidget = new UISceneView(context_, false);
  660. nwidget->SetWidget(widget);
  661. WrapWidget(nwidget, widget);
  662. return nwidget;
  663. }
  664. if (widget->IsOfType<TBLayout>())
  665. {
  666. UILayout* layout = new UILayout(context_, (UI_AXIS) widget->GetAxis(), false);
  667. layout->SetWidget(widget);
  668. WrapWidget(layout, widget);
  669. return layout;
  670. }
  671. if (widget->IsOfType<TBWidget>())
  672. {
  673. UIWidget* nwidget = new UIWidget(context_, false);
  674. nwidget->SetWidget(widget);
  675. WrapWidget(nwidget, widget);
  676. return nwidget;
  677. }
  678. return 0;
  679. }
  680. void UI::OnWidgetDelete(tb::TBWidget *widget)
  681. {
  682. }
  683. bool UI::OnWidgetDying(tb::TBWidget *widget)
  684. {
  685. return false;
  686. }
  687. void UI::OnWindowClose(tb::TBWindow *window)
  688. {
  689. if (widgetWrap_.Contains(window))
  690. {
  691. UIWidget* widget = widgetWrap_[window];
  692. VariantMap eventData;
  693. eventData[WindowClosed::P_WINDOW] = widget;
  694. widget->SendEvent(E_WINDOWCLOSED, eventData);
  695. }
  696. }
  697. void UI::OnWidgetFocusChanged(TBWidget *widget, bool focused)
  698. {
  699. if (widgetWrap_.Contains(widget))
  700. {
  701. VariantMap evData;
  702. UIWidget* uiWidget = widgetWrap_[widget];
  703. evData[UIWidgetFocusChanged::P_WIDGET] = uiWidget;
  704. evData[UIWidgetFocusChanged::P_FOCUSED] = focused;
  705. uiWidget->SendEvent(E_UIWIDGETFOCUSCHANGED, evData);
  706. }
  707. }
  708. void UI::ShowDebugHud(bool value)
  709. {
  710. DebugHud* hud = GetSubsystem<DebugHud>();
  711. if (!hud)
  712. return;
  713. if (value)
  714. hud->SetMode(DEBUGHUD_SHOW_ALL);
  715. else
  716. hud->SetMode(DEBUGHUD_SHOW_NONE);
  717. }
  718. void UI::ToggleDebugHud()
  719. {
  720. DebugHud* hud = GetSubsystem<DebugHud>();
  721. if (!hud)
  722. return;
  723. hud->ToggleAll();
  724. }
  725. void UI::SetDebugHudExtents(bool useRootExtent, const IntVector2& position, const IntVector2& size)
  726. {
  727. DebugHud* hud = GetSubsystem<DebugHud>();
  728. if (!hud)
  729. return;
  730. hud->SetExtents(useRootExtent, position, size);
  731. }
  732. void UI::CycleDebugHudMode()
  733. {
  734. DebugHud* hud = GetSubsystem<DebugHud>();
  735. if (!hud)
  736. return;
  737. hud->CycleMode();
  738. }
  739. void UI::SetDebugHudProfileMode(DebugHudProfileMode mode)
  740. {
  741. DebugHud* hud = GetSubsystem<DebugHud>();
  742. if (!hud)
  743. return;
  744. hud->SetProfilerMode(mode);
  745. }
  746. void UI::SetDebugHudRefreshInterval(float seconds)
  747. {
  748. DebugHud* hud = GetSubsystem<DebugHud>();
  749. if (!hud)
  750. return;
  751. hud->SetProfilerInterval(seconds);
  752. }
  753. void UI::ShowConsole(bool value)
  754. {
  755. Console* console = GetSubsystem<Console>();
  756. if (!console)
  757. return;
  758. console->SetVisible(value);
  759. consoleVisible_ = console->IsVisible();
  760. }
  761. void UI::ToggleConsole()
  762. {
  763. Console* console = GetSubsystem<Console>();
  764. if (!console)
  765. return;
  766. console->Toggle();
  767. consoleVisible_ = console->IsVisible();
  768. }
  769. void UI::HandleConsoleClosed(StringHash eventType, VariantMap& eventData)
  770. {
  771. consoleVisible_ = false;
  772. }
  773. MessageBox* UI::ShowSystemMessageBox(const String& title, const String& message)
  774. {
  775. MessageBox* messageBox = new MessageBox(context_, message, title);
  776. return messageBox;
  777. }
  778. UIWidget* UI::GetWidgetAt(int x, int y, bool include_children)
  779. {
  780. if (!initialized_)
  781. return 0;
  782. return WrapWidget(rootWidget_->GetWidgetAt(x, y, include_children));
  783. }
  784. bool UI::OnWidgetInvokeEvent(tb::TBWidget *widget, const tb::TBWidgetEvent &ev)
  785. {
  786. return false;
  787. }
  788. void UI::DebugShowSettingsWindow(UIWidget* parent)
  789. {
  790. #ifdef ATOMIC_DEBUG
  791. if (parent && parent->GetInternalWidget())
  792. tb::ShowDebugInfoSettingsWindow(parent->GetInternalWidget());
  793. #endif
  794. }
  795. }