UI.cpp 24 KB

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