TBUI.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. #include "Precompiled.h"
  2. #ifdef ATOMIC_TBUI
  3. #include "../Core/Context.h"
  4. #include "../Core/CoreEvents.h"
  5. #include "../Core/Profiler.h"
  6. #include "../IO/Log.h"
  7. #include "../IO/File.h"
  8. #include "../Resource/ResourceCache.h"
  9. #include "../Graphics/Graphics.h"
  10. #include "../Graphics/GraphicsEvents.h"
  11. #include "../Graphics/Texture2D.h"
  12. #include "../Graphics/VertexBuffer.h"
  13. #include "../Input/Input.h"
  14. #include "../Input/InputEvents.h"
  15. #include "../UI/UI.h"
  16. #include "../UI/TBUI.h"
  17. #include <TurboBadger/tb_core.h>
  18. #include <TurboBadger/tb_system.h>
  19. #include <TurboBadger/tb_debug.h>
  20. #include <TurboBadger/animation/tb_widget_animation.h>
  21. #include <TurboBadger/renderers/tb_renderer_batcher.h>
  22. #include <TurboBadger/tb_font_renderer.h>
  23. #include <TurboBadger/tb_node_tree.h>
  24. #include <TurboBadger/tb_widgets_reader.h>
  25. #include <TurboBadger/tb_window.h>
  26. void register_tbbf_font_renderer();
  27. void register_stb_font_renderer();
  28. void register_freetype_font_renderer();
  29. using namespace tb;
  30. namespace tb
  31. {
  32. void TBSystem::RescheduleTimer(double fire_time)
  33. {
  34. }
  35. }
  36. namespace Atomic
  37. {
  38. extern const char* UI_CATEGORY;
  39. class TBUIRenderer;
  40. class TBUIBitmap : public tb::TBBitmap
  41. {
  42. public:
  43. TBUIBitmap(TBUIRenderer *renderer);
  44. virtual ~TBUIBitmap();
  45. bool Init(int width, int height, tb::uint32 *data);
  46. virtual int Width() { return width_; }
  47. virtual int Height() { return height_; }
  48. virtual void SetData(tb::uint32 *data);
  49. public:
  50. TBUIRenderer *renderer_;
  51. int width_;
  52. int height_;
  53. SharedPtr<Texture2D> texture_;
  54. };
  55. TBUIBitmap::TBUIBitmap(TBUIRenderer *renderer)
  56. : renderer_(renderer), width_(0), height_(0)
  57. {
  58. }
  59. class TBUIRenderer : public TBRendererBatcher
  60. {
  61. public:
  62. WeakPtr<Context> context_;
  63. WeakPtr<TBUI> tbui_;
  64. PODVector<UIBatch>* batches_;
  65. PODVector<float>* vertexData_;
  66. IntRect currentScissor_;
  67. TBUIRenderer(Context* context)
  68. {
  69. context_ = context;
  70. }
  71. virtual ~TBUIRenderer()
  72. {
  73. }
  74. void BeginPaint(int render_target_w, int render_target_h)
  75. {
  76. TBRendererBatcher::BeginPaint(render_target_w, render_target_h);
  77. }
  78. void EndPaint()
  79. {
  80. TBRendererBatcher::EndPaint();
  81. }
  82. TBBitmap *CreateBitmap(int width, int height, uint32 *data)
  83. {
  84. TBUIBitmap *bitmap = new TBUIBitmap(this);
  85. if (!bitmap->Init(width, height, data))
  86. {
  87. delete bitmap;
  88. return nullptr;
  89. }
  90. return bitmap;
  91. }
  92. void RenderBatch(Batch *batch)
  93. {
  94. if (!batch->vertex_count)
  95. return;
  96. Texture2D* texture = NULL;
  97. if (batch->bitmap)
  98. {
  99. TBUIBitmap* tbuibitmap = (TBUIBitmap*)batch->bitmap;
  100. texture = tbuibitmap->texture_;
  101. }
  102. UIBatch b(tbui_, BLEND_ALPHA , currentScissor_, texture, vertexData_);
  103. float fadeAlpha = tbui_->GetFadeAlpha();
  104. unsigned begin = b.vertexData_->Size();
  105. b.vertexData_->Resize(begin + batch->vertex_count * UI_VERTEX_SIZE);
  106. float* dest = &(b.vertexData_->At(begin));
  107. b.vertexEnd_ = b.vertexData_->Size();
  108. // winding is reversed, however switching it causes rendering
  109. // issues, so turned off culling in UI module
  110. //for (int i = batch->vertex_count - 1; i >= 0; i--)
  111. for (int i = 0; i < batch->vertex_count; i++)
  112. {
  113. Vertex* v = &batch->vertex[i];
  114. dest[0] = v->x; dest[1] = v->y; dest[2] = 0.0f;
  115. v->a *= fadeAlpha;
  116. ((unsigned&)dest[3]) = v->col;
  117. dest[4] = v->u; dest[5] = v->v;
  118. dest += UI_VERTEX_SIZE;
  119. }
  120. UIBatch::AddOrMerge(b, *batches_);
  121. }
  122. void SetClipRect(const TBRect &rect)
  123. {
  124. currentScissor_.top_ = rect.y;
  125. currentScissor_.bottom_ = rect.y + rect.h;
  126. currentScissor_.left_ = rect.x;
  127. currentScissor_.right_ = rect.x + rect.w;
  128. }
  129. static TBUIRenderer* renderer_;
  130. };
  131. TBUIRenderer* TBUIRenderer::renderer_ = NULL;
  132. TBUIBitmap::~TBUIBitmap()
  133. {
  134. // Must flush and unbind before we delete the texture
  135. renderer_->FlushBitmap(this);
  136. }
  137. bool TBUIBitmap::Init(int width, int height, tb::uint32 *data)
  138. {
  139. assert(width == tb::TBGetNearestPowerOfTwo(width));
  140. assert(height == tb::TBGetNearestPowerOfTwo(height));
  141. width_ = width;
  142. height_ = height;
  143. texture_ = new Texture2D(renderer_->context_);
  144. texture_->SetMipsToSkip(0, 0);
  145. texture_->SetNumLevels(1);
  146. texture_->SetSize(width_, height_, Graphics::GetRGBAFormat(), TEXTURE_STATIC);
  147. texture_->SetFilterMode(FILTER_NEAREST);
  148. SetData(data);
  149. return true;
  150. }
  151. void TBUIBitmap::SetData(tb::uint32 *data)
  152. {
  153. renderer_->FlushBitmap(this);
  154. texture_->SetData(0, 0, 0, width_, height_, data);
  155. }
  156. class TBUIRootWidget : public TBWidget
  157. {
  158. public:
  159. };
  160. WeakPtr<Context> TBUI::readerContext_;
  161. TBUI::TBUI(Context* context) :
  162. UIElement(context),
  163. rootWidget_(0),
  164. initialized_(false),
  165. inputDisabled_(false),
  166. fadeAlpha_(1.0f),
  167. fadeTarget_(1.0f),
  168. currentFadeTime_(0.0f),
  169. fadeTime_(0.0f)
  170. {
  171. SubscribeToEvent(E_SCREENMODE, HANDLER(TBUI, HandleScreenMode));
  172. }
  173. TBUI::~TBUI()
  174. {
  175. if (initialized_)
  176. {
  177. tb::TBWidgetsAnimationManager::Shutdown();
  178. delete rootWidget_;
  179. // leak
  180. //delete TBUIRenderer::renderer_;
  181. tb_core_shutdown();
  182. }
  183. }
  184. void TBUI::Initialize()
  185. {
  186. assert(!initialized_);
  187. Graphics* graphics = GetSubsystem<Graphics>();
  188. assert(graphics);
  189. assert(graphics->IsInitialized());
  190. readerContext_ = context_;
  191. TBFile::SetReaderFunction(TBFileReader);
  192. graphics_ = graphics;
  193. vertexBuffer_ = new VertexBuffer(context_);
  194. TBWidgetsAnimationManager::Init();
  195. TBUIRenderer::renderer_ = new TBUIRenderer(graphics->GetContext());
  196. TBUIRenderer::renderer_->tbui_ = this;
  197. tb_core_init(TBUIRenderer::renderer_, "AtomicEditor/resources/language/lng_en.tb.txt");
  198. // Load the default skin, and override skin that contains the graphics specific to the demo.
  199. tb::g_tb_skin->Load("AtomicEditor/resources/default_skin/skin.tb.txt", "AtomicEditor/editor/skin/skin.tb.txt");
  200. register_tbbf_font_renderer();
  201. //register_stb_font_renderer();
  202. register_freetype_font_renderer();
  203. tb::g_font_manager->AddFontInfo("AtomicEditor/resources/MesloLGS-Regular.ttf", "Monaco");
  204. tb::g_font_manager->AddFontInfo("AtomicEditor/resources/vera.ttf", "Vera");
  205. tb::TBFontDescription fd;
  206. fd.SetID(tb::TBIDC("Vera"));
  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. rootWidget_ = new TBUIRootWidget();
  216. //rootWidget_->SetSkinBg(tb::TBIDC("background"));
  217. int width = graphics->GetWidth();
  218. int height = graphics->GetHeight();
  219. rootWidget_->SetSize(width, height);
  220. SetSize(width, height);
  221. rootWidget_->SetVisibilility(tb::WIDGET_VISIBILITY_VISIBLE);
  222. SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(TBUI, HandleMouseButtonDown));
  223. SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(TBUI, HandleMouseButtonUp));
  224. SubscribeToEvent(E_MOUSEMOVE, HANDLER(TBUI, HandleMouseMove));
  225. SubscribeToEvent(E_MOUSEWHEEL, HANDLER(TBUI, HandleMouseWheel));
  226. SubscribeToEvent(E_KEYDOWN, HANDLER(TBUI, HandleKeyDown));
  227. SubscribeToEvent(E_KEYUP, HANDLER(TBUI, HandleKeyUp));
  228. SubscribeToEvent(E_TEXTINPUT, HANDLER(TBUI, HandleTextInput));
  229. SubscribeToEvent(E_UPDATE, HANDLER(TBUI, HandleUpdate));
  230. SubscribeToEvent(E_RENDERUPDATE, HANDLER(TBUI, HandleRenderUpdate));
  231. //TB_DEBUG_SETTING(LAYOUT_BOUNDS) = 1;
  232. initialized_ = true;
  233. }
  234. void TBUI::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  235. {
  236. if (!initialized_)
  237. return;
  238. TBAnimationManager::Update();
  239. rootWidget_->InvokeProcessStates();
  240. rootWidget_->InvokeProcess();
  241. if (fadeAlpha_ > 0.0f)
  242. {
  243. tb::g_renderer->BeginPaint(rootWidget_->GetRect().w, rootWidget_->GetRect().h);
  244. TBUIRenderer::renderer_->currentScissor_ = currentScissor;
  245. TBUIRenderer::renderer_->batches_ = &batches;
  246. TBUIRenderer::renderer_->vertexData_ = &vertexData;
  247. rootWidget_->InvokePaint(tb::TBWidget::PaintProps());
  248. tb::g_renderer->EndPaint();
  249. }
  250. }
  251. void TBUI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  252. {
  253. if (!initialized_)
  254. return;
  255. using namespace ScreenMode;
  256. rootWidget_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  257. SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  258. }
  259. void TBUI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  260. {
  261. if (!initialized_ || inputDisabled_)
  262. return;
  263. using namespace MouseButtonDown;
  264. unsigned button = eventData[P_BUTTON].GetUInt();
  265. IntVector2 pos;
  266. pos = GetSubsystem<UI>()->GetCursorPosition();
  267. static double last_time = 0;
  268. static int counter = 1;
  269. Time* t = GetSubsystem<Time>();
  270. double time = t->GetElapsedTime() * 1000;
  271. if (time < last_time + 600)
  272. counter++;
  273. else
  274. counter = 1;
  275. last_time = time;
  276. if (button == MOUSEB_RIGHT)
  277. rootWidget_->InvokeRightPointerDown(pos.x_, pos.y_, counter, tb::TB_MODIFIER_NONE);
  278. else
  279. rootWidget_->InvokePointerDown(pos.x_, pos.y_, counter, tb::TB_MODIFIER_NONE, false);
  280. }
  281. void TBUI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
  282. {
  283. if (!initialized_ || inputDisabled_)
  284. return;
  285. using namespace MouseButtonUp;
  286. unsigned button = eventData[P_BUTTON].GetUInt();
  287. IntVector2 pos;
  288. pos = GetSubsystem<UI>()->GetCursorPosition();
  289. if (button == MOUSEB_RIGHT)
  290. rootWidget_->InvokeRightPointerUp(pos.x_, pos.y_, tb::TB_MODIFIER_NONE);
  291. else
  292. rootWidget_->InvokePointerUp(pos.x_, pos.y_, tb::TB_MODIFIER_NONE, false);
  293. }
  294. void TBUI::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  295. {
  296. using namespace MouseMove;
  297. if (!initialized_ || inputDisabled_)
  298. return;
  299. int px = eventData[P_X].GetInt();
  300. int py = eventData[P_Y].GetInt();
  301. rootWidget_->InvokePointerMove(px, py, tb::TB_MODIFIER_NONE, false);
  302. }
  303. void TBUI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
  304. {
  305. if (!initialized_ || inputDisabled_)
  306. return;
  307. using namespace MouseWheel;
  308. int delta = eventData[P_WHEEL].GetInt();
  309. Input* input = GetSubsystem<Input>();
  310. rootWidget_->InvokeWheel(input->GetMousePosition().x_, input->GetMousePosition().y_, 0, delta > 0 ? -1 : 1, tb::TB_MODIFIER_NONE);
  311. }
  312. static MODIFIER_KEYS GetModifierKeys(int qualifiers, bool superKey)
  313. {
  314. MODIFIER_KEYS code = TB_MODIFIER_NONE;
  315. if (qualifiers & QUAL_ALT) code |= TB_ALT;
  316. if (qualifiers & QUAL_CTRL) code |= TB_CTRL;
  317. if (qualifiers & QUAL_SHIFT) code |= TB_SHIFT;
  318. if (superKey) code |= TB_SUPER;
  319. return code;
  320. }
  321. // @return Return the upper case of a ascii charcter. Only for shortcut handling.
  322. static int toupr_ascii(int ascii)
  323. {
  324. if (ascii >= 'a' && ascii <= 'z')
  325. return ascii + 'A' - 'a';
  326. return ascii;
  327. }
  328. static bool InvokeShortcut(int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool down)
  329. {
  330. #ifdef __APPLE__
  331. bool shortcut_key = (modifierkeys & TB_SUPER) ? true : false;
  332. #else
  333. bool shortcut_key = (modifierkeys & TB_CTRL) ? true : false;
  334. #endif
  335. if (!TBWidget::focused_widget || !down || !shortcut_key)
  336. return false;
  337. bool reverse_key = (modifierkeys & TB_SHIFT) ? true : false;
  338. int upper_key = toupr_ascii(key);
  339. TBID id;
  340. if (upper_key == 'X')
  341. id = TBIDC("cut");
  342. else if (upper_key == 'C' || special_key == TB_KEY_INSERT)
  343. id = TBIDC("copy");
  344. else if (upper_key == 'V' || (special_key == TB_KEY_INSERT && reverse_key))
  345. id = TBIDC("paste");
  346. else if (upper_key == 'A')
  347. id = TBIDC("selectall");
  348. else if (upper_key == 'Z' || upper_key == 'Y')
  349. {
  350. bool undo = upper_key == 'Z';
  351. if (reverse_key)
  352. undo = !undo;
  353. id = undo ? TBIDC("undo") : TBIDC("redo");
  354. }
  355. else if (upper_key == 'N')
  356. id = TBIDC("new");
  357. else if (upper_key == 'O')
  358. id = TBIDC("open");
  359. else if (upper_key == 'S')
  360. id = TBIDC("save");
  361. else if (upper_key == 'W')
  362. id = TBIDC("close");
  363. else if (upper_key == 'F')
  364. id = TBIDC("find");
  365. else if (upper_key == 'G' && (modifierkeys & TB_SHIFT))
  366. id = TBIDC("findprev");
  367. else if (upper_key == 'G')
  368. id = TBIDC("findnext");
  369. else if (upper_key == 'P')
  370. id = TBIDC("play");
  371. else if (upper_key == 'B')
  372. id = TBIDC("beautify");
  373. else if (special_key == TB_KEY_PAGE_UP)
  374. id = TBIDC("prev_doc");
  375. else if (special_key == TB_KEY_PAGE_DOWN)
  376. id = TBIDC("next_doc");
  377. else
  378. return false;
  379. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  380. ev.modifierkeys = modifierkeys;
  381. ev.ref_id = id;
  382. return TBWidget::focused_widget->InvokeEvent(ev);
  383. }
  384. static bool InvokeKey(TBWidget* root, unsigned int key, SPECIAL_KEY special_key, MODIFIER_KEYS modifierkeys, bool keydown)
  385. {
  386. if (InvokeShortcut(key, special_key, modifierkeys, keydown))
  387. return true;
  388. root->InvokeKey(key, special_key, modifierkeys, keydown);
  389. return true;
  390. }
  391. void TBUI::HandleKey(bool keydown, int keycode, int scancode)
  392. {
  393. if (keycode == KEY_LGUI || keycode == KEY_RGUI)
  394. return;
  395. Input* input = GetSubsystem<Input>();
  396. int qualifiers = input->GetQualifiers();
  397. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  398. MODIFIER_KEYS mod = GetModifierKeys(qualifiers, superdown);
  399. switch (keycode)
  400. {
  401. case KEY_RETURN:
  402. case KEY_RETURN2:
  403. case KEY_KP_ENTER:
  404. InvokeKey(rootWidget_, 0, TB_KEY_ENTER, mod, keydown);
  405. break;
  406. case KEY_F1:
  407. InvokeKey(rootWidget_, 0, TB_KEY_F1, mod, keydown);
  408. break;
  409. case KEY_F2:
  410. InvokeKey(rootWidget_, 0, TB_KEY_F2, mod, keydown);
  411. break;
  412. case KEY_F3:
  413. InvokeKey(rootWidget_, 0, TB_KEY_F3, mod, keydown);
  414. break;
  415. case KEY_F4:
  416. InvokeKey(rootWidget_, 0, TB_KEY_F4, mod, keydown);
  417. break;
  418. case KEY_F5:
  419. InvokeKey(rootWidget_, 0, TB_KEY_F5, mod, keydown);
  420. break;
  421. case KEY_F6:
  422. InvokeKey(rootWidget_, 0, TB_KEY_F6, mod, keydown);
  423. break;
  424. case KEY_F7:
  425. InvokeKey(rootWidget_, 0, TB_KEY_F7, mod, keydown);
  426. break;
  427. case KEY_F8:
  428. InvokeKey(rootWidget_, 0, TB_KEY_F8, mod, keydown);
  429. break;
  430. case KEY_F9:
  431. InvokeKey(rootWidget_, 0, TB_KEY_F9, mod, keydown);
  432. break;
  433. case KEY_F10:
  434. InvokeKey(rootWidget_, 0, TB_KEY_F10, mod, keydown);
  435. break;
  436. case KEY_F11:
  437. InvokeKey(rootWidget_, 0, TB_KEY_F11, mod, keydown);
  438. break;
  439. case KEY_F12:
  440. InvokeKey(rootWidget_, 0, TB_KEY_F12, mod, keydown);
  441. break;
  442. case KEY_LEFT:
  443. InvokeKey(rootWidget_, 0, TB_KEY_LEFT, mod, keydown);
  444. break;
  445. case KEY_UP:
  446. InvokeKey(rootWidget_, 0, TB_KEY_UP, mod, keydown);
  447. break;
  448. case KEY_RIGHT:
  449. InvokeKey(rootWidget_, 0, TB_KEY_RIGHT, mod, keydown);
  450. break;
  451. case KEY_DOWN:
  452. InvokeKey(rootWidget_, 0, TB_KEY_DOWN, mod, keydown);
  453. break;
  454. case KEY_PAGEUP:
  455. InvokeKey(rootWidget_, 0, TB_KEY_PAGE_UP, mod, keydown);
  456. break;
  457. case KEY_PAGEDOWN:
  458. InvokeKey(rootWidget_, 0, TB_KEY_PAGE_DOWN, mod, keydown);
  459. break;
  460. case KEY_HOME:
  461. InvokeKey(rootWidget_, 0, TB_KEY_HOME, mod, keydown);
  462. break;
  463. case KEY_END:
  464. InvokeKey(rootWidget_, 0, TB_KEY_END, mod, keydown);
  465. break;
  466. case KEY_INSERT:
  467. InvokeKey(rootWidget_, 0, TB_KEY_INSERT, mod, keydown);
  468. break;
  469. case KEY_TAB:
  470. InvokeKey(rootWidget_, 0, TB_KEY_TAB, mod, keydown);
  471. break;
  472. case KEY_DELETE:
  473. InvokeKey(rootWidget_, 0, TB_KEY_DELETE, mod, keydown);
  474. break;
  475. case KEY_BACKSPACE:
  476. InvokeKey(rootWidget_, 0, TB_KEY_BACKSPACE, mod, keydown);
  477. break;
  478. case KEY_ESC:
  479. InvokeKey(rootWidget_, 0, TB_KEY_ESC, mod, keydown);
  480. break;
  481. default:
  482. if (mod & TB_SUPER)
  483. {
  484. InvokeKey(rootWidget_, keycode, TB_KEY_UNDEFINED, mod, keydown);
  485. }
  486. }
  487. }
  488. void TBUI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  489. {
  490. if (inputDisabled_)
  491. return;
  492. using namespace KeyDown;
  493. int keycode = eventData[P_KEY].GetInt();
  494. int scancode = eventData[P_SCANCODE].GetInt();
  495. HandleKey(true, keycode, scancode);
  496. }
  497. void TBUI::HandleKeyUp(StringHash eventType, VariantMap& eventData)
  498. {
  499. if (inputDisabled_)
  500. return;
  501. using namespace KeyUp;
  502. int keycode = eventData[P_KEY].GetInt();
  503. int scancode = eventData[P_SCANCODE].GetInt();
  504. HandleKey(false, keycode, scancode);
  505. }
  506. void TBUI::HandleTextInput(StringHash eventType, VariantMap& eventData)
  507. {
  508. if (inputDisabled_)
  509. return;
  510. using namespace TextInput;
  511. const String& text = eventData[P_TEXT].GetString();
  512. for (unsigned i = 0; i < text.Length(); i++)
  513. {
  514. InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, true);
  515. InvokeKey(rootWidget_, text[i], TB_KEY_UNDEFINED, TB_MODIFIER_NONE, false);
  516. }
  517. }
  518. void TBUI::HandleUpdate(StringHash eventType, VariantMap& eventData)
  519. {
  520. TBMessageHandler::ProcessMessages();
  521. // Timestep parameter is same no matter what event is being listened to
  522. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  523. if (fadeTarget_ != fadeAlpha_)
  524. {
  525. currentFadeTime_ += timeStep;
  526. if (currentFadeTime_ >= fadeTime_)
  527. {
  528. fadeAlpha_ = fadeTarget_;
  529. }
  530. else
  531. {
  532. if (fadeAlpha_ > fadeTarget_)
  533. {
  534. fadeAlpha_ = 1.0f - (currentFadeTime_/fadeTime_);
  535. }
  536. else
  537. fadeAlpha_ = (currentFadeTime_/fadeTime_ * (fadeTarget_));
  538. }
  539. }
  540. }
  541. void TBUI::FadeOut(float time)
  542. {
  543. fadeTarget_ = 0.0f;
  544. currentFadeTime_ = 0.0f;
  545. fadeTime_ = time;
  546. }
  547. void TBUI::FadeIn(float time)
  548. {
  549. fadeTarget_ = 1.0f;
  550. currentFadeTime_ = 0.0f;
  551. fadeTime_ = time;
  552. }
  553. bool TBUI::LoadResourceFile(TBWidget* widget, const String& filename)
  554. {
  555. tb::TBNode node;
  556. // TODO: use Urho resources
  557. if (!node.ReadFile(filename.CString()))
  558. return false;
  559. tb::g_widgets_reader->LoadNodeTree(widget, &node);
  560. return true;
  561. }
  562. void TBUI::Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd)
  563. {
  564. if (batches.Empty())
  565. return;
  566. Vector2 invScreenSize(1.0f / (float)graphics_->GetWidth(), 1.0f / (float)graphics_->GetHeight());
  567. Vector2 scale(2.0f * invScreenSize.x_, -2.0f * invScreenSize.y_);
  568. Vector2 offset(-1.0f, 1.0f);
  569. Matrix4 projection(Matrix4::IDENTITY);
  570. projection.m00_ = scale.x_;
  571. projection.m03_ = offset.x_;
  572. projection.m11_ = scale.y_;
  573. projection.m13_ = offset.y_;
  574. projection.m22_ = 1.0f;
  575. projection.m23_ = 0.0f;
  576. projection.m33_ = 1.0f;
  577. graphics_->ClearParameterSources();
  578. graphics_->SetColorWrite(true);
  579. graphics_->SetCullMode(CULL_NONE);
  580. graphics_->SetDepthTest(CMP_ALWAYS);
  581. graphics_->SetDepthWrite(false);
  582. graphics_->SetDrawAntialiased(false);
  583. graphics_->SetFillMode(FILL_SOLID);
  584. graphics_->SetStencilTest(false);
  585. graphics_->SetVertexBuffer(buffer);
  586. ShaderVariation* noTextureVS = graphics_->GetShader(VS, "Basic", "VERTEXCOLOR");
  587. ShaderVariation* diffTextureVS = graphics_->GetShader(VS, "Basic", "DIFFMAP VERTEXCOLOR");
  588. ShaderVariation* noTexturePS = graphics_->GetShader(PS, "Basic", "VERTEXCOLOR");
  589. ShaderVariation* diffTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP VERTEXCOLOR");
  590. ShaderVariation* diffMaskTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP ALPHAMASK VERTEXCOLOR");
  591. ShaderVariation* alphaTexturePS = graphics_->GetShader(PS, "Basic", "ALPHAMAP VERTEXCOLOR");
  592. unsigned alphaFormat = Graphics::GetAlphaFormat();
  593. for (unsigned i = batchStart; i < batchEnd; ++i)
  594. {
  595. const UIBatch& batch = batches[i];
  596. if (batch.vertexStart_ == batch.vertexEnd_)
  597. continue;
  598. ShaderVariation* ps;
  599. ShaderVariation* vs;
  600. if (!batch.texture_)
  601. {
  602. ps = noTexturePS;
  603. vs = noTextureVS;
  604. }
  605. else
  606. {
  607. // If texture contains only an alpha channel, use alpha shader (for fonts)
  608. vs = diffTextureVS;
  609. if (batch.texture_->GetFormat() == alphaFormat)
  610. ps = alphaTexturePS;
  611. else if (batch.blendMode_ != BLEND_ALPHA && batch.blendMode_ != BLEND_ADDALPHA && batch.blendMode_ != BLEND_PREMULALPHA)
  612. ps = diffMaskTexturePS;
  613. else
  614. ps = diffTexturePS;
  615. }
  616. graphics_->SetShaders(vs, ps);
  617. if (graphics_->NeedParameterUpdate(SP_OBJECTTRANSFORM, this))
  618. graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  619. if (graphics_->NeedParameterUpdate(SP_CAMERA, this))
  620. graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
  621. if (graphics_->NeedParameterUpdate(SP_MATERIAL, this))
  622. graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  623. graphics_->SetBlendMode(batch.blendMode_);
  624. graphics_->SetScissorTest(true, batch.scissor_);
  625. graphics_->SetTexture(0, batch.texture_);
  626. graphics_->Draw(TRIANGLE_LIST, batch.vertexStart_ / UI_VERTEX_SIZE, (batch.vertexEnd_ - batch.vertexStart_) /
  627. UI_VERTEX_SIZE);
  628. }
  629. }
  630. void TBUI::SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData)
  631. {
  632. if (vertexData.Empty())
  633. return;
  634. // Update quad geometry into the vertex buffer
  635. // Resize the vertex buffer first if too small or much too large
  636. unsigned numVertices = vertexData.Size() / UI_VERTEX_SIZE;
  637. if (dest->GetVertexCount() < numVertices || dest->GetVertexCount() > numVertices * 2)
  638. dest->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
  639. dest->SetData(&vertexData[0]);
  640. }
  641. void TBUI::Render()
  642. {
  643. SetVertexData(vertexBuffer_, vertexData_);
  644. Render(vertexBuffer_, batches_, 0, batches_.Size());
  645. }
  646. void TBUI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  647. {
  648. PROFILE(GetTBUIBatches);
  649. // Get rendering batches from the non-modal UI elements
  650. batches_.Clear();
  651. vertexData_.Clear();
  652. const IntVector2& rootSize = GetSize();
  653. IntRect currentScissor = IntRect(0, 0, rootSize.x_, rootSize.y_);
  654. GetBatches(batches_, vertexData_, currentScissor);
  655. }
  656. void TBUI::TBFileReader(const char* filename, void** data, unsigned* length)
  657. {
  658. *data = 0;
  659. *length = 0;
  660. ResourceCache* cache = readerContext_->GetSubsystem<ResourceCache>();
  661. SharedPtr<File> file = cache->GetFile(filename);
  662. if (!file->IsOpen())
  663. return;
  664. unsigned size = file->GetSize();
  665. if (!size)
  666. return;
  667. void* _data = malloc(size);
  668. if (!_data)
  669. return;
  670. if (file->Read(_data, size) != size)
  671. {
  672. free(_data);
  673. return;
  674. }
  675. *length = size;
  676. *data = _data;
  677. }
  678. }
  679. #endif