UI.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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(SystemUI::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* systemUI = new SystemUI::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. SystemUI::SystemUI* systemUI = GetSubsystem<SystemUI::SystemUI>();
  337. if (systemUI)
  338. systemUI->Render();
  339. }
  340. void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  341. {
  342. // Get rendering batches from the non-modal UI elements
  343. batches_.Clear();
  344. vertexData_.Clear();
  345. tb::TBRect rect = rootWidget_->GetRect();
  346. IntRect currentScissor = IntRect(0, 0, rect.w, rect.h);
  347. GetBatches(batches_, vertexData_, currentScissor);
  348. }
  349. void UI::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  350. {
  351. //if (!initialized_)
  352. // return;
  353. TBAnimationManager::Update();
  354. rootWidget_->InvokeProcessStates();
  355. rootWidget_->InvokeProcess();
  356. tb::g_renderer->BeginPaint(rootWidget_->GetRect().w, rootWidget_->GetRect().h);
  357. renderer_->currentScissor_ = currentScissor;
  358. renderer_->batches_ = &batches;
  359. renderer_->vertexData_ = &vertexData;
  360. rootWidget_->InvokePaint(tb::TBWidget::PaintProps());
  361. tb::g_renderer->EndPaint();
  362. }
  363. void UI::SubmitBatchVertexData(Texture* texture, const PODVector<float>& vertexData)
  364. {
  365. UIBatch b(BLEND_ALPHA , renderer_->currentScissor_, texture, &vertexData_);
  366. unsigned begin = b.vertexData_->Size();
  367. b.vertexData_->Resize(begin + vertexData.Size());
  368. float* dest = &(b.vertexData_->At(begin));
  369. b.vertexEnd_ = b.vertexData_->Size();
  370. for (unsigned i = 0; i < vertexData.Size(); i++, dest++)
  371. {
  372. *dest = vertexData[i];
  373. }
  374. UIBatch::AddOrMerge(b, batches_);
  375. }
  376. void UI::TBFileReader(const char* filename, void** data, unsigned* length)
  377. {
  378. *data = 0;
  379. *length = 0;
  380. ResourceCache* cache = uiContext_->GetSubsystem<ResourceCache>();
  381. SharedPtr<File> file = cache->GetFile(filename);
  382. if (!file || !file->IsOpen())
  383. {
  384. ATOMIC_LOGERRORF("UI::TBFileReader: Unable to load file: %s", filename);
  385. return;
  386. }
  387. unsigned size = file->GetSize();
  388. if (!size)
  389. return;
  390. void* _data = malloc(size);
  391. if (!_data)
  392. return;
  393. if (file->Read(_data, size) != size)
  394. {
  395. free(_data);
  396. return;
  397. }
  398. *length = size;
  399. *data = _data;
  400. }
  401. void UI::GetTBIDString(unsigned id, String& value)
  402. {
  403. if (!id)
  404. {
  405. value = "";
  406. }
  407. else
  408. {
  409. value = tbidToString_[id];
  410. }
  411. }
  412. void UI::TBIDRegisterStringCallback(unsigned id, const char* value)
  413. {
  414. uiContext_->GetSubsystem<UI>()->tbidToString_[id] = String(value);
  415. }
  416. bool UI::LoadResourceFile(TBWidget* widget, const String& filename)
  417. {
  418. tb::TBNode node;
  419. if (!node.ReadFile(filename.CString()))
  420. return false;
  421. tb::g_widgets_reader->LoadNodeTree(widget, &node);
  422. return true;
  423. }
  424. void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  425. {
  426. using namespace ScreenMode;
  427. rootWidget_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  428. }
  429. void UI::HandleUpdate(StringHash eventType, VariantMap& eventData)
  430. {
  431. if (exitRequested_) {
  432. SendEvent(E_EXITREQUESTED);
  433. exitRequested_ = false;
  434. return;
  435. }
  436. tooltipHoverTime_ += eventData[Update::P_TIMESTEP].GetFloat();
  437. if (tooltipHoverTime_ >= 0.5f)
  438. {
  439. UIWidget* hoveredWidget = GetHoveredWidget();
  440. if (hoveredWidget && !tooltip_ && (hoveredWidget->GetShortened() || hoveredWidget->GetTooltip().Length() > 0))
  441. {
  442. tooltip_ = new UIPopupWindow(context_, true, hoveredWidget, "tooltip");
  443. UILayout* tooltipLayout = new UILayout(context_, UI_AXIS_Y, true);
  444. if (hoveredWidget->GetShortened())
  445. {
  446. UITextField* fullTextField = new UITextField(context_, true);
  447. fullTextField->SetText(hoveredWidget->GetText());
  448. tooltipLayout->AddChild(fullTextField);
  449. }
  450. if (hoveredWidget->GetTooltip().Length() > 0)
  451. {
  452. UITextField* tooltipTextField = new UITextField(context_, true);
  453. tooltipTextField->SetText(hoveredWidget->GetTooltip());
  454. tooltipLayout->AddChild(tooltipTextField);
  455. }
  456. Input* input = GetSubsystem<Input>();
  457. IntVector2 mousePosition = input->GetMousePosition();
  458. tooltip_->AddChild(tooltipLayout);
  459. tooltip_->Show(mousePosition.x_ + 8, mousePosition.y_ + 8);
  460. }
  461. }
  462. else
  463. {
  464. if (tooltip_) tooltip_->Close();
  465. }
  466. SendEvent(E_UIUPDATE);
  467. TBMessageHandler::ProcessMessages();
  468. }
  469. UIWidget* UI::GetHoveredWidget()
  470. {
  471. return WrapWidget(TBWidget::hovered_widget);
  472. }
  473. bool UI::IsWidgetWrapped(tb::TBWidget* widget)
  474. {
  475. return widgetWrap_.Contains(widget);
  476. }
  477. bool UI::UnwrapWidget(tb::TBWidget* widget)
  478. {
  479. if (widgetWrap_.Contains(widget))
  480. {
  481. widget->SetDelegate(0);
  482. widgetWrap_.Erase(widget);
  483. return true;
  484. }
  485. return false;
  486. }
  487. void UI::PruneUnreachableWidgets()
  488. {
  489. HashMap<tb::TBWidget*, SharedPtr<UIWidget>>::Iterator itr;
  490. for (itr = widgetWrap_.Begin(); itr != widgetWrap_.End(); )
  491. {
  492. if ((*itr).first_->GetParent() || (*itr).second_->Refs() > 1)
  493. {
  494. itr++;
  495. continue;
  496. }
  497. itr.GotoNext();
  498. VariantMap eventData;
  499. eventData[WidgetDeleted::P_WIDGET] = (UIWidget*) (*itr).second_;
  500. (*itr).second_->SendEvent(E_WIDGETDELETED, eventData);
  501. tb::TBWidget* toDelete = (*itr).first_;
  502. UnwrapWidget(toDelete);
  503. delete toDelete;
  504. }
  505. }
  506. void UI::WrapWidget(UIWidget* widget, tb::TBWidget* tbwidget)
  507. {
  508. assert (!widgetWrap_.Contains(tbwidget));
  509. widgetWrap_[tbwidget] = widget;
  510. }
  511. UIWidget* UI::WrapWidget(tb::TBWidget* widget)
  512. {
  513. if (!widget)
  514. return NULL;
  515. if (widgetWrap_.Contains(widget))
  516. return widgetWrap_[widget];
  517. // switch this to use a factory?
  518. // this is order dependent as we're using IsOfType which also works if a base class
  519. if (widget->IsOfType<TBPopupWindow>())
  520. {
  521. UIPopupWindow* popupWindow = new UIPopupWindow(context_, false);
  522. popupWindow->SetWidget(widget);
  523. WrapWidget(popupWindow, widget);
  524. return popupWindow;
  525. }
  526. if (widget->IsOfType<TBDimmer>())
  527. {
  528. UIDimmer* dimmer = new UIDimmer(context_, false);
  529. dimmer->SetWidget(widget);
  530. WrapWidget(dimmer, widget);
  531. return dimmer;
  532. }
  533. if (widget->IsOfType<TBScrollContainer>())
  534. {
  535. UIScrollContainer* container = new UIScrollContainer(context_, false);
  536. container->SetWidget(widget);
  537. WrapWidget(container, widget);
  538. return container;
  539. }
  540. if (widget->IsOfType<TBInlineSelect>())
  541. {
  542. UIInlineSelect* select = new UIInlineSelect(context_, false);
  543. select->SetWidget(widget);
  544. WrapWidget(select, widget);
  545. return select;
  546. }
  547. if (widget->IsOfType<TBSlider>())
  548. {
  549. UISlider* slider = new UISlider(context_, false);
  550. slider->SetWidget(widget);
  551. WrapWidget(slider, widget);
  552. return slider;
  553. }
  554. if (widget->IsOfType<TBColorWidget>())
  555. {
  556. UIColorWidget* colorWidget = new UIColorWidget(context_, false);
  557. colorWidget->SetWidget(widget);
  558. WrapWidget(colorWidget, widget);
  559. return colorWidget;
  560. }
  561. if (widget->IsOfType<TBColorWheel>())
  562. {
  563. UIColorWheel* colorWheel = new UIColorWheel(context_, false);
  564. colorWheel->SetWidget(widget);
  565. WrapWidget(colorWheel, widget);
  566. return colorWheel;
  567. }
  568. if (widget->IsOfType<TBSection>())
  569. {
  570. UISection* section = new UISection(context_, false);
  571. section->SetWidget(widget);
  572. WrapWidget(section, widget);
  573. return section;
  574. }
  575. if (widget->IsOfType<TBSeparator>())
  576. {
  577. UISeparator* sep = new UISeparator(context_, false);
  578. sep->SetWidget(widget);
  579. WrapWidget(sep, widget);
  580. return sep;
  581. }
  582. if (widget->IsOfType<TBContainer>())
  583. {
  584. UIContainer* container = new UIContainer(context_, false);
  585. container->SetWidget(widget);
  586. WrapWidget(container, widget);
  587. return container;
  588. }
  589. if (widget->IsOfType<TBSelectDropdown>())
  590. {
  591. UISelectDropdown* select = new UISelectDropdown(context_, false);
  592. select->SetWidget(widget);
  593. WrapWidget(select, widget);
  594. return select;
  595. }
  596. if (widget->IsOfType<TBButton>())
  597. {
  598. // don't wrap the close button of a TBWindow.close
  599. if (widget->GetID() == TBIDC("TBWindow.close"))
  600. return 0;
  601. UIButton* button = new UIButton(context_, false);
  602. button->SetWidget(widget);
  603. WrapWidget(button, widget);
  604. return button;
  605. }
  606. if (widget->IsOfType<TBTextField>())
  607. {
  608. UITextField* textfield = new UITextField(context_, false);
  609. textfield->SetWidget(widget);
  610. WrapWidget(textfield, widget);
  611. return textfield;
  612. }
  613. if (widget->IsOfType<TBEditField>())
  614. {
  615. UIEditField* editfield = new UIEditField(context_, false);
  616. editfield->SetWidget(widget);
  617. WrapWidget(editfield, widget);
  618. return editfield;
  619. }
  620. if (widget->IsOfType<TBSkinImage>())
  621. {
  622. UISkinImage* skinimage = new UISkinImage(context_, "", false);
  623. skinimage->SetWidget(widget);
  624. WrapWidget(skinimage, widget);
  625. return skinimage;
  626. }
  627. if (widget->IsOfType<TBImageWidget>())
  628. {
  629. UIImageWidget* imagewidget = new UIImageWidget(context_, false);
  630. imagewidget->SetWidget(widget);
  631. WrapWidget(imagewidget, widget);
  632. return imagewidget;
  633. }
  634. if (widget->IsOfType<TBClickLabel>())
  635. {
  636. UIClickLabel* nwidget = new UIClickLabel(context_, false);
  637. nwidget->SetWidget(widget);
  638. WrapWidget(nwidget, widget);
  639. return nwidget;
  640. }
  641. if (widget->IsOfType<TBCheckBox>())
  642. {
  643. UICheckBox* nwidget = new UICheckBox(context_, false);
  644. nwidget->SetWidget(widget);
  645. WrapWidget(nwidget, widget);
  646. return nwidget;
  647. }
  648. if (widget->IsOfType<TBBarGraph>())
  649. {
  650. UIBargraph* nwidget = new UIBargraph(context_, false);
  651. nwidget->SetWidget(widget);
  652. WrapWidget(nwidget, widget);
  653. return nwidget;
  654. }
  655. if (widget->IsOfType<TBSelectList>())
  656. {
  657. UISelectList* nwidget = new UISelectList(context_, false);
  658. nwidget->SetWidget(widget);
  659. WrapWidget(nwidget, widget);
  660. return nwidget;
  661. }
  662. if (widget->IsOfType<TBMessageWindow>())
  663. {
  664. UIMessageWindow* nwidget = new UIMessageWindow(context_, NULL, "", false);
  665. nwidget->SetWidget(widget);
  666. WrapWidget(nwidget, widget);
  667. return nwidget;
  668. }
  669. if (widget->IsOfType<TBPromptWindow>())
  670. {
  671. UIPromptWindow* nwidget = new UIPromptWindow(context_, NULL, "", false);
  672. nwidget->SetWidget(widget);
  673. WrapWidget(nwidget, widget);
  674. return nwidget;
  675. }
  676. if (widget->IsOfType<TBFinderWindow>())
  677. {
  678. UIFinderWindow* nwidget = new UIFinderWindow(context_, NULL, "", false);
  679. nwidget->SetWidget(widget);
  680. WrapWidget(nwidget, widget);
  681. return nwidget;
  682. }
  683. if (widget->IsOfType<TBTabContainer>())
  684. {
  685. UITabContainer* nwidget = new UITabContainer(context_, false);
  686. nwidget->SetWidget(widget);
  687. WrapWidget(nwidget, widget);
  688. return nwidget;
  689. }
  690. if (widget->IsOfType<SceneViewWidget>())
  691. {
  692. UISceneView* nwidget = new UISceneView(context_, false);
  693. nwidget->SetWidget(widget);
  694. WrapWidget(nwidget, widget);
  695. return nwidget;
  696. }
  697. if (widget->IsOfType<TBLayout>())
  698. {
  699. UILayout* layout = new UILayout(context_, (UI_AXIS) widget->GetAxis(), false);
  700. layout->SetWidget(widget);
  701. WrapWidget(layout, widget);
  702. return layout;
  703. }
  704. if (widget->IsOfType<TBWidget>())
  705. {
  706. UIWidget* nwidget = new UIWidget(context_, false);
  707. nwidget->SetWidget(widget);
  708. WrapWidget(nwidget, widget);
  709. return nwidget;
  710. }
  711. return 0;
  712. }
  713. void UI::OnWidgetDelete(tb::TBWidget *widget)
  714. {
  715. }
  716. bool UI::OnWidgetDying(tb::TBWidget *widget)
  717. {
  718. return false;
  719. }
  720. void UI::OnWindowClose(tb::TBWindow *window)
  721. {
  722. if (widgetWrap_.Contains(window))
  723. {
  724. UIWidget* widget = widgetWrap_[window];
  725. VariantMap eventData;
  726. eventData[WindowClosed::P_WINDOW] = widget;
  727. widget->SendEvent(E_WINDOWCLOSED, eventData);
  728. }
  729. }
  730. void UI::OnWidgetFocusChanged(TBWidget *widget, bool focused)
  731. {
  732. if (widgetWrap_.Contains(widget))
  733. {
  734. VariantMap evData;
  735. UIWidget* uiWidget = widgetWrap_[widget];
  736. evData[UIWidgetFocusChanged::P_WIDGET] = uiWidget;
  737. evData[UIWidgetFocusChanged::P_FOCUSED] = focused;
  738. uiWidget->SendEvent(E_UIWIDGETFOCUSCHANGED, evData);
  739. }
  740. }
  741. void UI::ShowDebugHud(bool value)
  742. {
  743. SystemUI::DebugHud* hud = GetSubsystem<SystemUI::DebugHud>();
  744. if (!hud)
  745. return;
  746. if (value)
  747. hud->SetMode(SystemUI::DEBUGHUD_SHOW_ALL);
  748. else
  749. hud->SetMode(SystemUI::DEBUGHUD_SHOW_NONE);
  750. }
  751. void UI::ToggleDebugHud()
  752. {
  753. SystemUI::DebugHud* hud = GetSubsystem<SystemUI::DebugHud>();
  754. if (!hud)
  755. return;
  756. hud->ToggleAll();
  757. }
  758. void UI::SetDebugHudExtents(bool useRootExtent, const IntVector2& position, const IntVector2& size)
  759. {
  760. SystemUI::DebugHud* hud = GetSubsystem<SystemUI::DebugHud>();
  761. if (!hud)
  762. return;
  763. hud->SetExtents(useRootExtent, position, size);
  764. }
  765. void UI::CycleDebugHudMode()
  766. {
  767. SystemUI::DebugHud* hud = GetSubsystem<SystemUI::DebugHud>();
  768. if (!hud)
  769. return;
  770. hud->CycleMode();
  771. }
  772. void UI::SetDebugHudProfileMode(DebugHudProfileMode mode)
  773. {
  774. SystemUI::DebugHud* hud = GetSubsystem<SystemUI::DebugHud>();
  775. if (!hud)
  776. return;
  777. hud->SetProfilerMode(mode);
  778. }
  779. void UI::SetDebugHudRefreshInterval(float seconds)
  780. {
  781. SystemUI::DebugHud* hud = GetSubsystem<SystemUI::DebugHud>();
  782. if (!hud)
  783. return;
  784. hud->SetProfilerInterval(seconds);
  785. }
  786. void UI::ShowConsole(bool value)
  787. {
  788. SystemUI::Console* console = GetSubsystem<SystemUI::Console>();
  789. if (!console)
  790. return;
  791. console->SetVisible(value);
  792. consoleVisible_ = console->IsVisible();
  793. }
  794. void UI::ToggleConsole()
  795. {
  796. SystemUI::Console* console = GetSubsystem<SystemUI::Console>();
  797. if (!console)
  798. return;
  799. console->Toggle();
  800. consoleVisible_ = console->IsVisible();
  801. }
  802. void UI::HandleConsoleClosed(StringHash eventType, VariantMap& eventData)
  803. {
  804. consoleVisible_ = false;
  805. }
  806. SystemUI::MessageBox* UI::ShowSystemMessageBox(const String& title, const String& message)
  807. {
  808. ResourceCache* cache = GetSubsystem<ResourceCache>();
  809. XMLFile* xmlFile = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  810. SystemUI::MessageBox* messageBox = new SystemUI::MessageBox(context_, message, title, 0, xmlFile);
  811. return messageBox;
  812. }
  813. UIWidget* UI::GetWidgetAt(int x, int y, bool include_children)
  814. {
  815. if (!initialized_)
  816. return 0;
  817. return WrapWidget(rootWidget_->GetWidgetAt(x, y, include_children));
  818. }
  819. bool UI::OnWidgetInvokeEvent(tb::TBWidget *widget, const tb::TBWidgetEvent &ev)
  820. {
  821. return false;
  822. }
  823. void UI::DebugShowSettingsWindow(UIWidget* parent)
  824. {
  825. #ifdef ATOMIC_DEBUG
  826. if (parent && parent->GetInternalWidget())
  827. tb::ShowDebugInfoSettingsWindow(parent->GetInternalWidget());
  828. #endif
  829. }
  830. }