UI.cpp 28 KB

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