UI.cpp 23 KB

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