UI.cpp 26 KB

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