UI.cpp 27 KB

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