TBUI.cpp 25 KB

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