UI.cpp 23 KB

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