CGuiHandler.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * CGuiHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CGuiHandler.h"
  12. #include "../lib/CondSh.h"
  13. #include "CIntObject.h"
  14. #include "CursorHandler.h"
  15. #include "ShortcutHandler.h"
  16. #include "FramerateManager.h"
  17. #include "WindowHandler.h"
  18. #include "InterfaceEventDispatcher.h"
  19. #include "../CGameInfo.h"
  20. #include "../render/Colors.h"
  21. #include "../renderSDL/SDL_Extensions.h"
  22. #include "../renderSDL/ScreenHandler.h"
  23. #include "../CMT.h"
  24. #include "../CPlayerInterface.h"
  25. #include "../battle/BattleInterface.h"
  26. #include "../../lib/CThreadHelper.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include <SDL_render.h>
  29. #include <SDL_timer.h>
  30. #include <SDL_events.h>
  31. #include <SDL_keycode.h>
  32. #ifdef VCMI_APPLE
  33. #include <dispatch/dispatch.h>
  34. #endif
  35. #ifdef VCMI_IOS
  36. #include "ios/utils.h"
  37. #endif
  38. CGuiHandler GH;
  39. extern std::queue<SDL_Event> SDLEventsQueue;
  40. extern boost::mutex eventsM;
  41. boost::thread_specific_ptr<bool> inGuiThread;
  42. SObjectConstruction::SObjectConstruction(CIntObject *obj)
  43. :myObj(obj)
  44. {
  45. GH.createdObj.push_front(obj);
  46. GH.captureChildren = true;
  47. }
  48. SObjectConstruction::~SObjectConstruction()
  49. {
  50. assert(GH.createdObj.size());
  51. assert(GH.createdObj.front() == myObj);
  52. GH.createdObj.pop_front();
  53. GH.captureChildren = GH.createdObj.size();
  54. }
  55. SSetCaptureState::SSetCaptureState(bool allow, ui8 actions)
  56. {
  57. previousCapture = GH.captureChildren;
  58. GH.captureChildren = false;
  59. prevActions = GH.defActionsDef;
  60. GH.defActionsDef = actions;
  61. }
  62. SSetCaptureState::~SSetCaptureState()
  63. {
  64. GH.captureChildren = previousCapture;
  65. GH.defActionsDef = prevActions;
  66. }
  67. void CGuiHandler::init()
  68. {
  69. eventDispatcherInstance = std::make_unique<InterfaceEventDispatcher>();
  70. windowHandlerInstance = std::make_unique<WindowHandler>();
  71. screenHandlerInstance = std::make_unique<ScreenHandler>();
  72. shortcutsHandlerInstance = std::make_unique<ShortcutHandler>();
  73. framerateManagerInstance = std::make_unique<FramerateManager>(settings["video"]["targetfps"].Integer());
  74. isPointerRelativeMode = settings["general"]["userRelativePointer"].Bool();
  75. pointerSpeedMultiplier = settings["general"]["relativePointerSpeedMultiplier"].Float();
  76. }
  77. void CGuiHandler::handleEvents()
  78. {
  79. eventDispatcher().dispatchTimer(framerateManager().getElapsedMilliseconds());
  80. //player interface may want special event handling
  81. if(nullptr != LOCPLINT && LOCPLINT->capturedAllEvents())
  82. return;
  83. boost::unique_lock<boost::mutex> lock(eventsM);
  84. while(!SDLEventsQueue.empty())
  85. {
  86. eventDispatcher().allowEventHandling(true);
  87. SDL_Event currentEvent = SDLEventsQueue.front();
  88. if (currentEvent.type == SDL_MOUSEMOTION)
  89. {
  90. cursorPosition = Point(currentEvent.motion.x, currentEvent.motion.y);
  91. mouseButtonsMask = currentEvent.motion.state;
  92. }
  93. SDLEventsQueue.pop();
  94. // In a sequence of mouse motion events, skip all but the last one.
  95. // This prevents freezes when every motion event takes longer to handle than interval at which
  96. // the events arrive (like dragging on the minimap in world view, with redraw at every event)
  97. // so that the events would start piling up faster than they can be processed.
  98. if ((currentEvent.type == SDL_MOUSEMOTION) && !SDLEventsQueue.empty() && (SDLEventsQueue.front().type == SDL_MOUSEMOTION))
  99. continue;
  100. handleCurrentEvent(currentEvent);
  101. }
  102. }
  103. void CGuiHandler::convertTouchToMouse(SDL_Event * current)
  104. {
  105. int rLogicalWidth, rLogicalHeight;
  106. SDL_RenderGetLogicalSize(mainRenderer, &rLogicalWidth, &rLogicalHeight);
  107. int adjustedMouseY = (int)(current->tfinger.y * rLogicalHeight);
  108. int adjustedMouseX = (int)(current->tfinger.x * rLogicalWidth);
  109. current->button.x = adjustedMouseX;
  110. current->motion.x = adjustedMouseX;
  111. current->button.y = adjustedMouseY;
  112. current->motion.y = adjustedMouseY;
  113. }
  114. void CGuiHandler::fakeMoveCursor(float dx, float dy)
  115. {
  116. int x, y, w, h;
  117. SDL_Event event;
  118. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0, 0, 0};
  119. sme.state = SDL_GetMouseState(&x, &y);
  120. SDL_GetWindowSize(mainWindow, &w, &h);
  121. sme.x = CCS->curh->position().x + (int)(GH.pointerSpeedMultiplier * w * dx);
  122. sme.y = CCS->curh->position().y + (int)(GH.pointerSpeedMultiplier * h * dy);
  123. vstd::abetween(sme.x, 0, w);
  124. vstd::abetween(sme.y, 0, h);
  125. event.motion = sme;
  126. SDL_PushEvent(&event);
  127. }
  128. void CGuiHandler::fakeMouseMove()
  129. {
  130. fakeMoveCursor(0, 0);
  131. }
  132. void CGuiHandler::startTextInput(const Rect & whereInput)
  133. {
  134. #ifdef VCMI_APPLE
  135. dispatch_async(dispatch_get_main_queue(), ^{
  136. #endif
  137. // TODO ios: looks like SDL bug actually, try fixing there
  138. auto renderer = SDL_GetRenderer(mainWindow);
  139. float scaleX, scaleY;
  140. SDL_Rect viewport;
  141. SDL_RenderGetScale(renderer, &scaleX, &scaleY);
  142. SDL_RenderGetViewport(renderer, &viewport);
  143. #ifdef VCMI_IOS
  144. const auto nativeScale = iOS_utils::screenScale();
  145. scaleX /= nativeScale;
  146. scaleY /= nativeScale;
  147. #endif
  148. SDL_Rect rectInScreenCoordinates;
  149. rectInScreenCoordinates.x = (viewport.x + whereInput.x) * scaleX;
  150. rectInScreenCoordinates.y = (viewport.y + whereInput.y) * scaleY;
  151. rectInScreenCoordinates.w = whereInput.w * scaleX;
  152. rectInScreenCoordinates.h = whereInput.h * scaleY;
  153. SDL_SetTextInputRect(&rectInScreenCoordinates);
  154. if (SDL_IsTextInputActive() == SDL_FALSE)
  155. {
  156. SDL_StartTextInput();
  157. }
  158. #ifdef VCMI_APPLE
  159. });
  160. #endif
  161. }
  162. void CGuiHandler::stopTextInput()
  163. {
  164. #ifdef VCMI_APPLE
  165. dispatch_async(dispatch_get_main_queue(), ^{
  166. #endif
  167. if (SDL_IsTextInputActive() == SDL_TRUE)
  168. {
  169. SDL_StopTextInput();
  170. }
  171. #ifdef VCMI_APPLE
  172. });
  173. #endif
  174. }
  175. void CGuiHandler::fakeMouseButtonEventRelativeMode(bool down, bool right)
  176. {
  177. SDL_Event event;
  178. SDL_MouseButtonEvent sme = {SDL_MOUSEBUTTONDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  179. if(!down)
  180. {
  181. sme.type = SDL_MOUSEBUTTONUP;
  182. }
  183. sme.button = right ? SDL_BUTTON_RIGHT : SDL_BUTTON_LEFT;
  184. sme.x = CCS->curh->position().x;
  185. sme.y = CCS->curh->position().y;
  186. float xScale, yScale;
  187. int w, h, rLogicalWidth, rLogicalHeight;
  188. SDL_GetWindowSize(mainWindow, &w, &h);
  189. SDL_RenderGetLogicalSize(mainRenderer, &rLogicalWidth, &rLogicalHeight);
  190. SDL_RenderGetScale(mainRenderer, &xScale, &yScale);
  191. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  192. moveCursorToPosition( Point(
  193. (int)(sme.x * xScale) + (w - rLogicalWidth * xScale) / 2,
  194. (int)(sme.y * yScale + (h - rLogicalHeight * yScale) / 2)));
  195. SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
  196. event.button = sme;
  197. SDL_PushEvent(&event);
  198. }
  199. void CGuiHandler::handleCurrentEvent( SDL_Event & current )
  200. {
  201. switch (current.type)
  202. {
  203. case SDL_KEYDOWN:
  204. return handleEventKeyDown(current);
  205. case SDL_KEYUP:
  206. return handleEventKeyUp(current);
  207. case SDL_MOUSEMOTION:
  208. return handleEventMouseMotion(current);
  209. case SDL_MOUSEBUTTONDOWN:
  210. return handleEventMouseButtonDown(current);
  211. case SDL_MOUSEWHEEL:
  212. return handleEventMouseWheel(current);
  213. case SDL_TEXTINPUT:
  214. return handleEventTextInput(current);
  215. case SDL_TEXTEDITING:
  216. return handleEventTextEditing(current);
  217. case SDL_MOUSEBUTTONUP:
  218. return handleEventMouseButtonUp(current);
  219. case SDL_FINGERMOTION:
  220. return handleEventFingerMotion(current);
  221. case SDL_FINGERDOWN:
  222. return handleEventFingerDown(current);
  223. case SDL_FINGERUP:
  224. return handleEventFingerUp(current);
  225. }
  226. }
  227. void CGuiHandler::handleEventKeyDown(SDL_Event & current)
  228. {
  229. SDL_KeyboardEvent key = current.key;
  230. if(key.repeat != 0)
  231. return; // ignore periodic event resends
  232. assert(key.state == SDL_PRESSED);
  233. if(current.type == SDL_KEYDOWN && key.keysym.sym >= SDLK_F1 && key.keysym.sym <= SDLK_F15 && settings["session"]["spectate"].Bool())
  234. {
  235. //TODO: we need some central place for all interface-independent hotkeys
  236. Settings s = settings.write["session"];
  237. switch(key.keysym.sym)
  238. {
  239. case SDLK_F5:
  240. if(settings["session"]["spectate-locked-pim"].Bool())
  241. LOCPLINT->pim->unlock();
  242. else
  243. LOCPLINT->pim->lock();
  244. s["spectate-locked-pim"].Bool() = !settings["session"]["spectate-locked-pim"].Bool();
  245. break;
  246. case SDLK_F6:
  247. s["spectate-ignore-hero"].Bool() = !settings["session"]["spectate-ignore-hero"].Bool();
  248. break;
  249. case SDLK_F7:
  250. s["spectate-skip-battle"].Bool() = !settings["session"]["spectate-skip-battle"].Bool();
  251. break;
  252. case SDLK_F8:
  253. s["spectate-skip-battle-result"].Bool() = !settings["session"]["spectate-skip-battle-result"].Bool();
  254. break;
  255. default:
  256. break;
  257. }
  258. return;
  259. }
  260. auto shortcutsVector = shortcutsHandler().translateKeycode(key.keysym.sym);
  261. eventDispatcher().dispatchShortcutPressed(shortcutsVector);
  262. }
  263. void CGuiHandler::handleEventKeyUp(SDL_Event & current)
  264. {
  265. SDL_KeyboardEvent key = current.key;
  266. if(key.repeat != 0)
  267. return; // ignore periodic event resends
  268. assert(key.state == SDL_RELEASED);
  269. auto shortcutsVector = shortcutsHandler().translateKeycode(key.keysym.sym);
  270. eventDispatcher().dispatchShortcutReleased(shortcutsVector);
  271. }
  272. void CGuiHandler::handleEventMouseMotion(SDL_Event & current)
  273. {
  274. eventDispatcher().dispatchMouseMoved(Point(current.motion.x, current.motion.y));
  275. }
  276. void CGuiHandler::handleEventMouseButtonDown(SDL_Event & current)
  277. {
  278. switch(current.button.button)
  279. {
  280. case SDL_BUTTON_LEFT:
  281. if (current.button.clicks > 1)
  282. eventDispatcher().dispatchMouseDoubleClick(Point(current.button.x, current.button.y));
  283. else
  284. eventDispatcher().dispatchMouseButtonPressed(MouseButton::LEFT, Point(current.button.x, current.button.y));
  285. break;
  286. case SDL_BUTTON_RIGHT:
  287. eventDispatcher().dispatchMouseButtonPressed(MouseButton::RIGHT, Point(current.button.x, current.button.y));
  288. break;
  289. case SDL_BUTTON_MIDDLE:
  290. eventDispatcher().dispatchMouseButtonPressed(MouseButton::MIDDLE, Point(current.button.x, current.button.y));
  291. break;
  292. }
  293. }
  294. void CGuiHandler::handleEventMouseWheel(SDL_Event & current)
  295. {
  296. // SDL doesn't have the proper values for mouse positions on SDL_MOUSEWHEEL, refetch them
  297. int x = 0, y = 0;
  298. SDL_GetMouseState(&x, &y);
  299. eventDispatcher().dispatchMouseScrolled(Point(current.wheel.x, current.wheel.y), Point(x, y));
  300. }
  301. void CGuiHandler::handleEventTextInput(SDL_Event & current)
  302. {
  303. eventDispatcher().dispatchTextInput(current.text.text);
  304. }
  305. void CGuiHandler::handleEventTextEditing(SDL_Event & current)
  306. {
  307. eventDispatcher().dispatchTextEditing(current.text.text);
  308. }
  309. void CGuiHandler::handleEventMouseButtonUp(SDL_Event & current)
  310. {
  311. if(!multifinger)
  312. {
  313. switch(current.button.button)
  314. {
  315. case SDL_BUTTON_LEFT:
  316. eventDispatcher().dispatchMouseButtonReleased(MouseButton::LEFT, Point(current.button.x, current.button.y));
  317. break;
  318. case SDL_BUTTON_RIGHT:
  319. eventDispatcher().dispatchMouseButtonReleased(MouseButton::RIGHT, Point(current.button.x, current.button.y));
  320. break;
  321. case SDL_BUTTON_MIDDLE:
  322. eventDispatcher().dispatchMouseButtonReleased(MouseButton::MIDDLE, Point(current.button.x, current.button.y));
  323. break;
  324. }
  325. }
  326. }
  327. void CGuiHandler::handleEventFingerMotion(SDL_Event & current)
  328. {
  329. if(isPointerRelativeMode)
  330. {
  331. fakeMoveCursor(current.tfinger.dx, current.tfinger.dy);
  332. }
  333. }
  334. void CGuiHandler::handleEventFingerDown(SDL_Event & current)
  335. {
  336. auto fingerCount = SDL_GetNumTouchFingers(current.tfinger.touchId);
  337. multifinger = fingerCount > 1;
  338. if(isPointerRelativeMode)
  339. {
  340. if(current.tfinger.x > 0.5)
  341. {
  342. bool isRightClick = current.tfinger.y < 0.5;
  343. fakeMouseButtonEventRelativeMode(true, isRightClick);
  344. }
  345. }
  346. #ifndef VCMI_IOS
  347. else if(fingerCount == 2)
  348. {
  349. convertTouchToMouse(&current);
  350. eventDispatcher().dispatchMouseMoved(Point(current.button.x, current.button.y));
  351. eventDispatcher().dispatchMouseButtonPressed(MouseButton::RIGHT, Point(current.button.x, current.button.y));
  352. }
  353. #endif //VCMI_IOS
  354. }
  355. void CGuiHandler::handleEventFingerUp(SDL_Event & current)
  356. {
  357. #ifndef VCMI_IOS
  358. auto fingerCount = SDL_GetNumTouchFingers(current.tfinger.touchId);
  359. #endif //VCMI_IOS
  360. if(isPointerRelativeMode)
  361. {
  362. if(current.tfinger.x > 0.5)
  363. {
  364. bool isRightClick = current.tfinger.y < 0.5;
  365. fakeMouseButtonEventRelativeMode(false, isRightClick);
  366. }
  367. }
  368. #ifndef VCMI_IOS
  369. else if(multifinger)
  370. {
  371. convertTouchToMouse(&current);
  372. eventDispatcher().dispatchMouseMoved(Point(current.button.x, current.button.y));
  373. eventDispatcher().dispatchMouseButtonReleased(MouseButton::RIGHT, Point(current.button.x, current.button.y));
  374. multifinger = fingerCount != 0;
  375. }
  376. #endif //VCMI_IOS
  377. }
  378. void CGuiHandler::renderFrame()
  379. {
  380. // Updating GUI requires locking pim mutex (that protects screen and GUI state).
  381. // During game:
  382. // When ending the game, the pim mutex might be hold by other thread,
  383. // that will notify us about the ending game by setting terminate_cond flag.
  384. //in PreGame terminate_cond stay false
  385. bool acquiredTheLockOnPim = false; //for tracking whether pim mutex locking succeeded
  386. while(!terminate_cond->get() && !(acquiredTheLockOnPim = CPlayerInterface::pim->try_lock())) //try acquiring long until it succeeds or we are told to terminate
  387. boost::this_thread::sleep(boost::posix_time::milliseconds(1));
  388. if(acquiredTheLockOnPim)
  389. {
  390. // If we are here, pim mutex has been successfully locked - let's store it in a safe RAII lock.
  391. boost::unique_lock<boost::recursive_mutex> un(*CPlayerInterface::pim, boost::adopt_lock);
  392. if(nullptr != curInt)
  393. curInt->update();
  394. if(settings["video"]["showfps"].Bool())
  395. drawFPSCounter();
  396. SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
  397. SDL_RenderClear(mainRenderer);
  398. SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
  399. CCS->curh->render();
  400. SDL_RenderPresent(mainRenderer);
  401. windows().onFrameRendered();
  402. }
  403. framerateManager().framerateDelay(); // holds a constant FPS
  404. }
  405. CGuiHandler::CGuiHandler()
  406. : defActionsDef(0)
  407. , captureChildren(false)
  408. , multifinger(false)
  409. , mouseButtonsMask(0)
  410. , curInt(nullptr)
  411. , fakeStatusBar(std::make_shared<EmptyStatusBar>())
  412. , terminate_cond (new CondSh<bool>(false))
  413. {
  414. }
  415. CGuiHandler::~CGuiHandler()
  416. {
  417. delete terminate_cond;
  418. }
  419. ShortcutHandler & CGuiHandler::shortcutsHandler()
  420. {
  421. assert(shortcutsHandlerInstance);
  422. return *shortcutsHandlerInstance;
  423. }
  424. FramerateManager & CGuiHandler::framerateManager()
  425. {
  426. assert(framerateManagerInstance);
  427. return *framerateManagerInstance;
  428. }
  429. void CGuiHandler::moveCursorToPosition(const Point & position)
  430. {
  431. SDL_WarpMouseInWindow(mainWindow, position.x, position.y);
  432. }
  433. bool CGuiHandler::isKeyboardCtrlDown() const
  434. {
  435. #ifdef VCMI_MAC
  436. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  437. #else
  438. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  439. #endif
  440. }
  441. bool CGuiHandler::isKeyboardAltDown() const
  442. {
  443. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT];
  444. }
  445. bool CGuiHandler::isKeyboardShiftDown() const
  446. {
  447. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT];
  448. }
  449. void CGuiHandler::breakEventHandling()
  450. {
  451. eventDispatcher().allowEventHandling(false);
  452. }
  453. const Point & CGuiHandler::getCursorPosition() const
  454. {
  455. return cursorPosition;
  456. }
  457. Point CGuiHandler::screenDimensions() const
  458. {
  459. return Point(screen->w, screen->h);
  460. }
  461. bool CGuiHandler::isMouseButtonPressed() const
  462. {
  463. return mouseButtonsMask > 0;
  464. }
  465. bool CGuiHandler::isMouseButtonPressed(MouseButton button) const
  466. {
  467. static_assert(static_cast<uint32_t>(MouseButton::LEFT) == SDL_BUTTON_LEFT, "mismatch between VCMI and SDL enum!");
  468. static_assert(static_cast<uint32_t>(MouseButton::MIDDLE) == SDL_BUTTON_MIDDLE, "mismatch between VCMI and SDL enum!");
  469. static_assert(static_cast<uint32_t>(MouseButton::RIGHT) == SDL_BUTTON_RIGHT, "mismatch between VCMI and SDL enum!");
  470. static_assert(static_cast<uint32_t>(MouseButton::EXTRA1) == SDL_BUTTON_X1, "mismatch between VCMI and SDL enum!");
  471. static_assert(static_cast<uint32_t>(MouseButton::EXTRA2) == SDL_BUTTON_X2, "mismatch between VCMI and SDL enum!");
  472. uint32_t index = static_cast<uint32_t>(button);
  473. return mouseButtonsMask & SDL_BUTTON(index);
  474. }
  475. void CGuiHandler::drawFPSCounter()
  476. {
  477. static SDL_Rect overlay = { 0, 0, 64, 32};
  478. uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
  479. SDL_FillRect(screen, &overlay, black);
  480. std::string fps = std::to_string(framerateManager().getFramerate());
  481. graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, Colors::YELLOW, Point(10, 10));
  482. }
  483. bool CGuiHandler::amIGuiThread()
  484. {
  485. return inGuiThread.get() && *inGuiThread;
  486. }
  487. void CGuiHandler::pushUserEvent(EUserEvent usercode)
  488. {
  489. pushUserEvent(usercode, nullptr);
  490. }
  491. void CGuiHandler::pushUserEvent(EUserEvent usercode, void * userdata)
  492. {
  493. SDL_Event event;
  494. event.type = SDL_USEREVENT;
  495. event.user.code = static_cast<int32_t>(usercode);
  496. event.user.data1 = userdata;
  497. SDL_PushEvent(&event);
  498. }
  499. IScreenHandler & CGuiHandler::screenHandler()
  500. {
  501. return *screenHandlerInstance;
  502. }
  503. InterfaceEventDispatcher & CGuiHandler::eventDispatcher()
  504. {
  505. return *eventDispatcherInstance;
  506. }
  507. WindowHandler & CGuiHandler::windows()
  508. {
  509. assert(windowHandlerInstance);
  510. return *windowHandlerInstance;
  511. }
  512. std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
  513. {
  514. auto locked = currentStatusBar.lock();
  515. if (!locked)
  516. return fakeStatusBar;
  517. return locked;
  518. }
  519. void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
  520. {
  521. currentStatusBar = newStatusBar;
  522. }
  523. void CGuiHandler::onScreenResize()
  524. {
  525. screenHandler().onScreenResize();
  526. windows().onScreenResize();
  527. }