TBUI.cpp 25 KB

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