UI.cpp 24 KB

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