UI.cpp 25 KB

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