UI.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. //TB_DEBUG_SETTING(LAYOUT_BOUNDS) = 1;
  200. }
  201. void UI::LoadSkin(const String& skin, const String& overrideSkin)
  202. {
  203. // Load the default skin, and override skin (if any)
  204. tb::g_tb_skin->Load(skin.CString(), overrideSkin.CString());
  205. skinLoaded_ = true;
  206. }
  207. void UI::LoadDefaultPlayerSkin()
  208. {
  209. ResourceCache* cache = GetSubsystem<ResourceCache>();
  210. String skin = "DefaultUI/skin/skin.tb.txt";
  211. String overrideSkin;
  212. // see if we have an override skin
  213. SharedPtr<File> skinFile = cache->GetFile("UI/Skin/skin.ui.txt", false);
  214. if (skinFile.NotNull())
  215. {
  216. skinFile->Close();
  217. skin = "UI/Skin/skin.ui.txt";
  218. }
  219. // see if we have an override skin
  220. SharedPtr<File> overrideFile = cache->GetFile("UI/Skin/Override/skin.ui.txt", false);
  221. if (overrideFile.NotNull())
  222. {
  223. overrideFile->Close();
  224. overrideSkin = "UI/Skin/Override/skin.ui.txt";
  225. }
  226. LoadSkin(skin, overrideSkin);
  227. if (skin == "DefaultUI/skin/skin.tb.txt")
  228. {
  229. AddFont("DefaultUI/fonts/vera.ttf", "Vera");
  230. SetDefaultFont("Vera", 12);
  231. }
  232. }
  233. void UI::SetDefaultFont(const String& name, int size)
  234. {
  235. tb::TBFontDescription fd;
  236. fd.SetID(tb::TBIDC(name.CString()));
  237. fd.SetSize(tb::g_tb_skin->GetDimensionConverter()->DpToPx(size));
  238. tb::g_font_manager->SetDefaultFontDescription(fd);
  239. // Create the font now.
  240. tb::TBFontFace *font = tb::g_font_manager->CreateFontFace(tb::g_font_manager->GetDefaultFontDescription());
  241. // Render some glyphs in one go now since we know we are going to use them. It would work fine
  242. // without this since glyphs are rendered when needed, but with some extra updating of the glyph bitmap.
  243. if (font)
  244. font->RenderGlyphs(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~•·åäöÅÄÖ");
  245. }
  246. void UI::AddFont(const String& fontFile, const String& name)
  247. {
  248. tb::g_font_manager->AddFontInfo(fontFile.CString(), name.CString());
  249. }
  250. void UI::AddUIView(UIView* uiView)
  251. {
  252. rootWidget_->AddChild(uiView->GetInternalWidget());
  253. uiViews_.Push(SharedPtr<UIView>(uiView));
  254. if (!focusedView_ && uiView)
  255. {
  256. uiView->SetFocus();
  257. }
  258. }
  259. void UI::RemoveUIView(UIView* uiView)
  260. {
  261. if (focusedView_ == uiView)
  262. {
  263. SetFocusedView(0);
  264. }
  265. rootWidget_->RemoveChild(uiView->GetInternalWidget());
  266. uiViews_.Remove(SharedPtr<UIView>(uiView));
  267. }
  268. void UI::SetFocusedView(UIView* uiView)
  269. {
  270. if (focusedView_ == uiView)
  271. {
  272. return;
  273. }
  274. focusedView_ = uiView;
  275. if (focusedView_)
  276. {
  277. focusedView_->BecomeFocused();
  278. }
  279. else
  280. {
  281. focusedView_ = 0;
  282. // look for first auto activated UIView, and recurse
  283. Vector<SharedPtr<UIView>>::Iterator itr = uiViews_.Begin();
  284. while (itr != uiViews_.End())
  285. {
  286. if ((*itr)->GetAutoFocus())
  287. {
  288. SetFocusedView(*itr);
  289. return;
  290. }
  291. itr++;
  292. }
  293. }
  294. }
  295. void UI::Render(bool resetRenderTargets)
  296. {
  297. Vector<SharedPtr<UIView>>::Iterator itr = uiViews_.Begin();
  298. while (itr != uiViews_.End())
  299. {
  300. (*itr)->Render(resetRenderTargets);
  301. itr++;
  302. }
  303. }
  304. void UI::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  305. {
  306. TBAnimationManager::Update();
  307. rootWidget_->InvokeProcessStates();
  308. rootWidget_->InvokeProcess();
  309. }
  310. void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  311. {
  312. Vector<SharedPtr<UIView>>::Iterator itr = uiViews_.Begin();
  313. while (itr != uiViews_.End())
  314. {
  315. (*itr)->UpdateUIBatches();
  316. itr++;
  317. }
  318. }
  319. void UI::TBFileReader(const char* filename, void** data, unsigned* length)
  320. {
  321. *data = 0;
  322. *length = 0;
  323. ResourceCache* cache = uiContext_->GetSubsystem<ResourceCache>();
  324. SharedPtr<File> file = cache->GetFile(filename);
  325. if (!file || !file->IsOpen())
  326. {
  327. ATOMIC_LOGERRORF("UI::TBFileReader: Unable to load file: %s", filename);
  328. return;
  329. }
  330. unsigned size = file->GetSize();
  331. if (!size)
  332. return;
  333. void* _data = malloc(size);
  334. if (!_data)
  335. return;
  336. if (file->Read(_data, size) != size)
  337. {
  338. free(_data);
  339. return;
  340. }
  341. *length = size;
  342. *data = _data;
  343. }
  344. void UI::GetTBIDString(unsigned id, String& value)
  345. {
  346. if (!id)
  347. {
  348. value = "";
  349. }
  350. else
  351. {
  352. value = tbidToString_[id];
  353. }
  354. }
  355. void UI::TBIDRegisterStringCallback(unsigned id, const char* value)
  356. {
  357. uiContext_->GetSubsystem<UI>()->tbidToString_[id] = String(value);
  358. }
  359. bool UI::LoadResourceFile(TBWidget* widget, const String& filename)
  360. {
  361. tb::TBNode node;
  362. if (!node.ReadFile(filename.CString()))
  363. return false;
  364. tb::g_widgets_reader->LoadNodeTree(widget, &node);
  365. return true;
  366. }
  367. void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  368. {
  369. using namespace ScreenMode;
  370. rootWidget_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  371. }
  372. void UI::HandleUpdate(StringHash eventType, VariantMap& eventData)
  373. {
  374. if (exitRequested_) {
  375. SendEvent(E_EXITREQUESTED);
  376. exitRequested_ = false;
  377. return;
  378. }
  379. tooltipHoverTime_ += eventData[Update::P_TIMESTEP].GetFloat();
  380. if (tooltipHoverTime_ >= 0.5f)
  381. {
  382. UIWidget* hoveredWidget = GetHoveredWidget();
  383. if (hoveredWidget && !tooltip_ && (hoveredWidget->GetShortened() || hoveredWidget->GetTooltip().Length() > 0))
  384. {
  385. tooltip_ = new UIPopupWindow(context_, true, hoveredWidget, "tooltip");
  386. UILayout* tooltipLayout = new UILayout(context_, UI_AXIS_Y, true);
  387. if (hoveredWidget->GetShortened())
  388. {
  389. UITextField* fullTextField = new UITextField(context_, true);
  390. fullTextField->SetText(hoveredWidget->GetText());
  391. tooltipLayout->AddChild(fullTextField);
  392. }
  393. if (hoveredWidget->GetTooltip().Length() > 0)
  394. {
  395. UITextField* tooltipTextField = new UITextField(context_, true);
  396. tooltipTextField->SetText(hoveredWidget->GetTooltip());
  397. tooltipLayout->AddChild(tooltipTextField);
  398. }
  399. Input* input = GetSubsystem<Input>();
  400. IntVector2 mousePosition = input->GetMousePosition();
  401. tooltip_->AddChild(tooltipLayout);
  402. tooltip_->Show(mousePosition.x_ + 8, mousePosition.y_ + 8);
  403. }
  404. }
  405. else
  406. {
  407. if (tooltip_) tooltip_->Close();
  408. }
  409. SendEvent(E_UIUPDATE);
  410. TBMessageHandler::ProcessMessages();
  411. }
  412. UIWidget* UI::GetHoveredWidget()
  413. {
  414. return WrapWidget(TBWidget::hovered_widget);
  415. }
  416. bool UI::IsWidgetWrapped(tb::TBWidget* widget)
  417. {
  418. return widgetWrap_.Contains(widget);
  419. }
  420. bool UI::UnwrapWidget(tb::TBWidget* widget)
  421. {
  422. if (widgetWrap_.Contains(widget))
  423. {
  424. widget->SetDelegate(0);
  425. widgetWrap_.Erase(widget);
  426. return true;
  427. }
  428. return false;
  429. }
  430. void UI::PruneUnreachableWidgets()
  431. {
  432. HashMap<tb::TBWidget*, SharedPtr<UIWidget>>::Iterator itr;
  433. for (itr = widgetWrap_.Begin(); itr != widgetWrap_.End(); )
  434. {
  435. if ((*itr).first_->GetParent() || (*itr).second_->Refs() > 1)
  436. {
  437. itr++;
  438. continue;
  439. }
  440. itr.GotoNext();
  441. VariantMap eventData;
  442. eventData[WidgetDeleted::P_WIDGET] = (UIWidget*) (*itr).second_;
  443. (*itr).second_->SendEvent(E_WIDGETDELETED, eventData);
  444. tb::TBWidget* toDelete = (*itr).first_;
  445. UnwrapWidget(toDelete);
  446. delete toDelete;
  447. }
  448. }
  449. void UI::WrapWidget(UIWidget* widget, tb::TBWidget* tbwidget)
  450. {
  451. assert (!widgetWrap_.Contains(tbwidget));
  452. widgetWrap_[tbwidget] = widget;
  453. }
  454. UIWidget* UI::WrapWidget(tb::TBWidget* widget)
  455. {
  456. if (!widget)
  457. return NULL;
  458. if (widgetWrap_.Contains(widget))
  459. return widgetWrap_[widget];
  460. // switch this to use a factory?
  461. // this is order dependent as we're using IsOfType which also works if a base class
  462. if (widget->IsOfType<TBPopupWindow>())
  463. {
  464. UIPopupWindow* popupWindow = new UIPopupWindow(context_, false);
  465. popupWindow->SetWidget(widget);
  466. WrapWidget(popupWindow, widget);
  467. return popupWindow;
  468. }
  469. if (widget->IsOfType<TBDimmer>())
  470. {
  471. UIDimmer* dimmer = new UIDimmer(context_, false);
  472. dimmer->SetWidget(widget);
  473. WrapWidget(dimmer, widget);
  474. return dimmer;
  475. }
  476. if (widget->IsOfType<TBScrollContainer>())
  477. {
  478. UIScrollContainer* container = new UIScrollContainer(context_, false);
  479. container->SetWidget(widget);
  480. WrapWidget(container, widget);
  481. return container;
  482. }
  483. if (widget->IsOfType<TBInlineSelect>())
  484. {
  485. UIInlineSelect* select = new UIInlineSelect(context_, false);
  486. select->SetWidget(widget);
  487. WrapWidget(select, widget);
  488. return select;
  489. }
  490. if (widget->IsOfType<TBSlider>())
  491. {
  492. UISlider* slider = new UISlider(context_, false);
  493. slider->SetWidget(widget);
  494. WrapWidget(slider, widget);
  495. return slider;
  496. }
  497. if (widget->IsOfType<TBScrollBar>())
  498. {
  499. UIScrollBar* slider = new UIScrollBar(context_, false);
  500. slider->SetWidget(widget);
  501. WrapWidget(slider, widget);
  502. return slider;
  503. }
  504. if (widget->IsOfType<TBColorWidget>())
  505. {
  506. UIColorWidget* colorWidget = new UIColorWidget(context_, false);
  507. colorWidget->SetWidget(widget);
  508. WrapWidget(colorWidget, widget);
  509. return colorWidget;
  510. }
  511. if (widget->IsOfType<TBColorWheel>())
  512. {
  513. UIColorWheel* colorWheel = new UIColorWheel(context_, false);
  514. colorWheel->SetWidget(widget);
  515. WrapWidget(colorWheel, widget);
  516. return colorWheel;
  517. }
  518. if (widget->IsOfType<TBSection>())
  519. {
  520. UISection* section = new UISection(context_, false);
  521. section->SetWidget(widget);
  522. WrapWidget(section, widget);
  523. return section;
  524. }
  525. if (widget->IsOfType<TBSeparator>())
  526. {
  527. UISeparator* sep = new UISeparator(context_, false);
  528. sep->SetWidget(widget);
  529. WrapWidget(sep, widget);
  530. return sep;
  531. }
  532. if (widget->IsOfType<TBContainer>())
  533. {
  534. UIContainer* container = new UIContainer(context_, false);
  535. container->SetWidget(widget);
  536. WrapWidget(container, widget);
  537. return container;
  538. }
  539. if (widget->IsOfType<TBSelectDropdown>())
  540. {
  541. UISelectDropdown* select = new UISelectDropdown(context_, false);
  542. select->SetWidget(widget);
  543. WrapWidget(select, widget);
  544. return select;
  545. }
  546. if (widget->IsOfType<TBPulldownMenu>())
  547. {
  548. UIPulldownMenu* select = new UIPulldownMenu(context_, false);
  549. select->SetWidget(widget);
  550. WrapWidget(select, widget);
  551. return select;
  552. }
  553. if (widget->IsOfType<TBButton>())
  554. {
  555. // don't wrap the close button of a TBWindow.close
  556. if (widget->GetID() == TBIDC("TBWindow.close"))
  557. return 0;
  558. UIButton* button = new UIButton(context_, false);
  559. button->SetWidget(widget);
  560. WrapWidget(button, widget);
  561. return button;
  562. }
  563. if (widget->IsOfType<TBTextField>())
  564. {
  565. UITextField* textfield = new UITextField(context_, false);
  566. textfield->SetWidget(widget);
  567. WrapWidget(textfield, widget);
  568. return textfield;
  569. }
  570. if (widget->IsOfType<TBEditField>())
  571. {
  572. UIEditField* editfield = new UIEditField(context_, false);
  573. editfield->SetWidget(widget);
  574. WrapWidget(editfield, widget);
  575. return editfield;
  576. }
  577. if (widget->IsOfType<TBSkinImage>())
  578. {
  579. UISkinImage* skinimage = new UISkinImage(context_, "", false);
  580. skinimage->SetWidget(widget);
  581. WrapWidget(skinimage, widget);
  582. return skinimage;
  583. }
  584. if (widget->IsOfType<TBImageWidget>())
  585. {
  586. UIImageWidget* imagewidget = new UIImageWidget(context_, false);
  587. imagewidget->SetWidget(widget);
  588. WrapWidget(imagewidget, widget);
  589. return imagewidget;
  590. }
  591. if (widget->IsOfType<TBClickLabel>())
  592. {
  593. UIClickLabel* nwidget = new UIClickLabel(context_, false);
  594. nwidget->SetWidget(widget);
  595. WrapWidget(nwidget, widget);
  596. return nwidget;
  597. }
  598. if (widget->IsOfType<TBCheckBox>())
  599. {
  600. UICheckBox* nwidget = new UICheckBox(context_, false);
  601. nwidget->SetWidget(widget);
  602. WrapWidget(nwidget, widget);
  603. return nwidget;
  604. }
  605. if (widget->IsOfType<TBRadioButton>())
  606. {
  607. UIRadioButton* nwidget = new UIRadioButton(context_, false);
  608. nwidget->SetWidget(widget);
  609. WrapWidget(nwidget, widget);
  610. return nwidget;
  611. }
  612. if (widget->IsOfType<TBBarGraph>())
  613. {
  614. UIBargraph* nwidget = new UIBargraph(context_, false);
  615. nwidget->SetWidget(widget);
  616. WrapWidget(nwidget, widget);
  617. return nwidget;
  618. }
  619. if (widget->IsOfType<TBSelectList>())
  620. {
  621. UISelectList* nwidget = new UISelectList(context_, false);
  622. nwidget->SetWidget(widget);
  623. WrapWidget(nwidget, widget);
  624. return nwidget;
  625. }
  626. if (widget->IsOfType<TBMessageWindow>())
  627. {
  628. UIMessageWindow* nwidget = new UIMessageWindow(context_, NULL, "", false);
  629. nwidget->SetWidget(widget);
  630. WrapWidget(nwidget, widget);
  631. return nwidget;
  632. }
  633. if (widget->IsOfType<TBPromptWindow>())
  634. {
  635. UIPromptWindow* nwidget = new UIPromptWindow(context_, NULL, "", false);
  636. nwidget->SetWidget(widget);
  637. WrapWidget(nwidget, widget);
  638. return nwidget;
  639. }
  640. if (widget->IsOfType<TBFinderWindow>())
  641. {
  642. UIFinderWindow* nwidget = new UIFinderWindow(context_, NULL, "", false);
  643. nwidget->SetWidget(widget);
  644. WrapWidget(nwidget, widget);
  645. return nwidget;
  646. }
  647. if (widget->IsOfType<TBTabContainer>())
  648. {
  649. UITabContainer* nwidget = new UITabContainer(context_, false);
  650. nwidget->SetWidget(widget);
  651. WrapWidget(nwidget, widget);
  652. return nwidget;
  653. }
  654. if (widget->IsOfType<SceneViewWidget>())
  655. {
  656. UISceneView* nwidget = new UISceneView(context_, false);
  657. nwidget->SetWidget(widget);
  658. WrapWidget(nwidget, widget);
  659. return nwidget;
  660. }
  661. if (widget->IsOfType<TBLayout>())
  662. {
  663. UILayout* layout = new UILayout(context_, (UI_AXIS) widget->GetAxis(), false);
  664. layout->SetWidget(widget);
  665. WrapWidget(layout, widget);
  666. return layout;
  667. }
  668. if (widget->IsOfType<TBWidget>())
  669. {
  670. UIWidget* nwidget = new UIWidget(context_, false);
  671. nwidget->SetWidget(widget);
  672. WrapWidget(nwidget, widget);
  673. return nwidget;
  674. }
  675. return 0;
  676. }
  677. void UI::OnWidgetDelete(tb::TBWidget *widget)
  678. {
  679. }
  680. bool UI::OnWidgetDying(tb::TBWidget *widget)
  681. {
  682. return false;
  683. }
  684. void UI::OnWindowClose(tb::TBWindow *window)
  685. {
  686. if (widgetWrap_.Contains(window))
  687. {
  688. UIWidget* widget = widgetWrap_[window];
  689. VariantMap eventData;
  690. eventData[WindowClosed::P_WINDOW] = widget;
  691. widget->SendEvent(E_WINDOWCLOSED, eventData);
  692. }
  693. }
  694. void UI::OnWidgetFocusChanged(TBWidget *widget, bool focused)
  695. {
  696. if (widgetWrap_.Contains(widget))
  697. {
  698. VariantMap evData;
  699. UIWidget* uiWidget = widgetWrap_[widget];
  700. evData[UIWidgetFocusChanged::P_WIDGET] = uiWidget;
  701. evData[UIWidgetFocusChanged::P_FOCUSED] = focused;
  702. uiWidget->SendEvent(E_UIWIDGETFOCUSCHANGED, evData);
  703. }
  704. }
  705. void UI::ShowDebugHud(bool value)
  706. {
  707. DebugHud* hud = GetSubsystem<DebugHud>();
  708. if (!hud)
  709. return;
  710. if (value)
  711. hud->SetMode(DEBUGHUD_SHOW_ALL);
  712. else
  713. hud->SetMode(DEBUGHUD_SHOW_NONE);
  714. }
  715. void UI::ToggleDebugHud()
  716. {
  717. DebugHud* hud = GetSubsystem<DebugHud>();
  718. if (!hud)
  719. return;
  720. hud->ToggleAll();
  721. }
  722. void UI::SetDebugHudExtents(bool useRootExtent, const IntVector2& position, const IntVector2& size)
  723. {
  724. DebugHud* hud = GetSubsystem<DebugHud>();
  725. if (!hud)
  726. return;
  727. hud->SetExtents(useRootExtent, position, size);
  728. }
  729. void UI::CycleDebugHudMode()
  730. {
  731. DebugHud* hud = GetSubsystem<DebugHud>();
  732. if (!hud)
  733. return;
  734. hud->CycleMode();
  735. }
  736. void UI::SetDebugHudProfileMode(DebugHudProfileMode mode)
  737. {
  738. DebugHud* hud = GetSubsystem<DebugHud>();
  739. if (!hud)
  740. return;
  741. hud->SetProfilerMode(mode);
  742. }
  743. void UI::SetDebugHudRefreshInterval(float seconds)
  744. {
  745. DebugHud* hud = GetSubsystem<DebugHud>();
  746. if (!hud)
  747. return;
  748. hud->SetProfilerInterval(seconds);
  749. }
  750. void UI::ShowConsole(bool value)
  751. {
  752. Console* console = GetSubsystem<Console>();
  753. if (!console)
  754. return;
  755. console->SetVisible(value);
  756. consoleVisible_ = console->IsVisible();
  757. }
  758. void UI::ToggleConsole()
  759. {
  760. Console* console = GetSubsystem<Console>();
  761. if (!console)
  762. return;
  763. console->Toggle();
  764. consoleVisible_ = console->IsVisible();
  765. }
  766. void UI::HandleConsoleClosed(StringHash eventType, VariantMap& eventData)
  767. {
  768. consoleVisible_ = false;
  769. }
  770. MessageBox* UI::ShowSystemMessageBox(const String& title, const String& message)
  771. {
  772. MessageBox* messageBox = new MessageBox(context_, message, title);
  773. return messageBox;
  774. }
  775. UIWidget* UI::GetWidgetAt(int x, int y, bool include_children)
  776. {
  777. if (!initialized_)
  778. return 0;
  779. return WrapWidget(rootWidget_->GetWidgetAt(x, y, include_children));
  780. }
  781. bool UI::OnWidgetInvokeEvent(tb::TBWidget *widget, const tb::TBWidgetEvent &ev)
  782. {
  783. return false;
  784. }
  785. void UI::DebugShowSettingsWindow(UIWidget* parent)
  786. {
  787. #ifdef ATOMIC_DEBUG
  788. if (parent && parent->GetInternalWidget())
  789. tb::ShowDebugInfoSettingsWindow(parent->GetInternalWidget());
  790. #endif
  791. }
  792. }