CGuiHandler.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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::updateTime()
  78. {
  79. eventDispatcher().updateTime(framerateManager().getElapsedMilliseconds());
  80. }
  81. void CGuiHandler::handleEvents()
  82. {
  83. //player interface may want special event handling
  84. if(nullptr != LOCPLINT && LOCPLINT->capturedAllEvents())
  85. return;
  86. boost::unique_lock<boost::mutex> lock(eventsM);
  87. while(!SDLEventsQueue.empty())
  88. {
  89. continueEventHandling = true;
  90. SDL_Event currentEvent = SDLEventsQueue.front();
  91. if (currentEvent.type == SDL_MOUSEMOTION)
  92. {
  93. cursorPosition = Point(currentEvent.motion.x, currentEvent.motion.y);
  94. mouseButtonsMask = currentEvent.motion.state;
  95. }
  96. SDLEventsQueue.pop();
  97. // In a sequence of mouse motion events, skip all but the last one.
  98. // This prevents freezes when every motion event takes longer to handle than interval at which
  99. // the events arrive (like dragging on the minimap in world view, with redraw at every event)
  100. // so that the events would start piling up faster than they can be processed.
  101. if ((currentEvent.type == SDL_MOUSEMOTION) && !SDLEventsQueue.empty() && (SDLEventsQueue.front().type == SDL_MOUSEMOTION))
  102. continue;
  103. handleCurrentEvent(currentEvent);
  104. }
  105. }
  106. void CGuiHandler::convertTouchToMouse(SDL_Event * current)
  107. {
  108. int rLogicalWidth, rLogicalHeight;
  109. SDL_RenderGetLogicalSize(mainRenderer, &rLogicalWidth, &rLogicalHeight);
  110. int adjustedMouseY = (int)(current->tfinger.y * rLogicalHeight);
  111. int adjustedMouseX = (int)(current->tfinger.x * rLogicalWidth);
  112. current->button.x = adjustedMouseX;
  113. current->motion.x = adjustedMouseX;
  114. current->button.y = adjustedMouseY;
  115. current->motion.y = adjustedMouseY;
  116. }
  117. void CGuiHandler::fakeMoveCursor(float dx, float dy)
  118. {
  119. int x, y, w, h;
  120. SDL_Event event;
  121. SDL_MouseMotionEvent sme = {SDL_MOUSEMOTION, 0, 0, 0, 0, 0, 0, 0, 0};
  122. sme.state = SDL_GetMouseState(&x, &y);
  123. SDL_GetWindowSize(mainWindow, &w, &h);
  124. sme.x = CCS->curh->position().x + (int)(GH.pointerSpeedMultiplier * w * dx);
  125. sme.y = CCS->curh->position().y + (int)(GH.pointerSpeedMultiplier * h * dy);
  126. vstd::abetween(sme.x, 0, w);
  127. vstd::abetween(sme.y, 0, h);
  128. event.motion = sme;
  129. SDL_PushEvent(&event);
  130. }
  131. void CGuiHandler::fakeMouseMove()
  132. {
  133. fakeMoveCursor(0, 0);
  134. }
  135. void CGuiHandler::startTextInput(const Rect & whereInput)
  136. {
  137. #ifdef VCMI_APPLE
  138. dispatch_async(dispatch_get_main_queue(), ^{
  139. #endif
  140. // TODO ios: looks like SDL bug actually, try fixing there
  141. auto renderer = SDL_GetRenderer(mainWindow);
  142. float scaleX, scaleY;
  143. SDL_Rect viewport;
  144. SDL_RenderGetScale(renderer, &scaleX, &scaleY);
  145. SDL_RenderGetViewport(renderer, &viewport);
  146. #ifdef VCMI_IOS
  147. const auto nativeScale = iOS_utils::screenScale();
  148. scaleX /= nativeScale;
  149. scaleY /= nativeScale;
  150. #endif
  151. SDL_Rect rectInScreenCoordinates;
  152. rectInScreenCoordinates.x = (viewport.x + whereInput.x) * scaleX;
  153. rectInScreenCoordinates.y = (viewport.y + whereInput.y) * scaleY;
  154. rectInScreenCoordinates.w = whereInput.w * scaleX;
  155. rectInScreenCoordinates.h = whereInput.h * scaleY;
  156. SDL_SetTextInputRect(&rectInScreenCoordinates);
  157. if (SDL_IsTextInputActive() == SDL_FALSE)
  158. {
  159. SDL_StartTextInput();
  160. }
  161. #ifdef VCMI_APPLE
  162. });
  163. #endif
  164. }
  165. void CGuiHandler::stopTextInput()
  166. {
  167. #ifdef VCMI_APPLE
  168. dispatch_async(dispatch_get_main_queue(), ^{
  169. #endif
  170. if (SDL_IsTextInputActive() == SDL_TRUE)
  171. {
  172. SDL_StopTextInput();
  173. }
  174. #ifdef VCMI_APPLE
  175. });
  176. #endif
  177. }
  178. void CGuiHandler::fakeMouseButtonEventRelativeMode(bool down, bool right)
  179. {
  180. SDL_Event event;
  181. SDL_MouseButtonEvent sme = {SDL_MOUSEBUTTONDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  182. if(!down)
  183. {
  184. sme.type = SDL_MOUSEBUTTONUP;
  185. }
  186. sme.button = right ? SDL_BUTTON_RIGHT : SDL_BUTTON_LEFT;
  187. sme.x = CCS->curh->position().x;
  188. sme.y = CCS->curh->position().y;
  189. float xScale, yScale;
  190. int w, h, rLogicalWidth, rLogicalHeight;
  191. SDL_GetWindowSize(mainWindow, &w, &h);
  192. SDL_RenderGetLogicalSize(mainRenderer, &rLogicalWidth, &rLogicalHeight);
  193. SDL_RenderGetScale(mainRenderer, &xScale, &yScale);
  194. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  195. moveCursorToPosition( Point(
  196. (int)(sme.x * xScale) + (w - rLogicalWidth * xScale) / 2,
  197. (int)(sme.y * yScale + (h - rLogicalHeight * yScale) / 2)));
  198. SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
  199. event.button = sme;
  200. SDL_PushEvent(&event);
  201. }
  202. void CGuiHandler::handleCurrentEvent( SDL_Event & current )
  203. {
  204. switch (current.type)
  205. {
  206. case SDL_KEYDOWN:
  207. return handleEventKeyDown(current);
  208. case SDL_KEYUP:
  209. return handleEventKeyUp(current);
  210. case SDL_MOUSEMOTION:
  211. return handleEventMouseMotion(current);
  212. case SDL_MOUSEBUTTONDOWN:
  213. return handleEventMouseButtonDown(current);
  214. case SDL_MOUSEWHEEL:
  215. return handleEventMouseWheel(current);
  216. case SDL_TEXTINPUT:
  217. return handleEventTextInput(current);
  218. case SDL_TEXTEDITING:
  219. return handleEventTextEditing(current);
  220. case SDL_MOUSEBUTTONUP:
  221. return handleEventMouseButtonUp(current);
  222. case SDL_FINGERMOTION:
  223. return handleEventFingerMotion(current);
  224. case SDL_FINGERDOWN:
  225. return handleEventFingerDown(current);
  226. case SDL_FINGERUP:
  227. return handleEventFingerUp(current);
  228. }
  229. }
  230. void CGuiHandler::handleEventKeyDown(SDL_Event & current)
  231. {
  232. SDL_KeyboardEvent key = current.key;
  233. if(key.repeat != 0)
  234. return; // ignore periodic event resends
  235. assert(key.state == SDL_PRESSED);
  236. if(current.type == SDL_KEYDOWN && key.keysym.sym >= SDLK_F1 && key.keysym.sym <= SDLK_F15 && settings["session"]["spectate"].Bool())
  237. {
  238. //TODO: we need some central place for all interface-independent hotkeys
  239. Settings s = settings.write["session"];
  240. switch(key.keysym.sym)
  241. {
  242. case SDLK_F5:
  243. if(settings["session"]["spectate-locked-pim"].Bool())
  244. LOCPLINT->pim->unlock();
  245. else
  246. LOCPLINT->pim->lock();
  247. s["spectate-locked-pim"].Bool() = !settings["session"]["spectate-locked-pim"].Bool();
  248. break;
  249. case SDLK_F6:
  250. s["spectate-ignore-hero"].Bool() = !settings["session"]["spectate-ignore-hero"].Bool();
  251. break;
  252. case SDLK_F7:
  253. s["spectate-skip-battle"].Bool() = !settings["session"]["spectate-skip-battle"].Bool();
  254. break;
  255. case SDLK_F8:
  256. s["spectate-skip-battle-result"].Bool() = !settings["session"]["spectate-skip-battle-result"].Bool();
  257. break;
  258. default:
  259. break;
  260. }
  261. return;
  262. }
  263. auto shortcutsVector = shortcutsHandler().translateKeycode(key.keysym.sym);
  264. eventDispatcher().dispatchShortcutPressed(shortcutsVector);
  265. }
  266. void CGuiHandler::handleEventKeyUp(SDL_Event & current)
  267. {
  268. SDL_KeyboardEvent key = current.key;
  269. if(key.repeat != 0)
  270. return; // ignore periodic event resends
  271. assert(key.state == SDL_RELEASED);
  272. auto shortcutsVector = shortcutsHandler().translateKeycode(key.keysym.sym);
  273. eventDispatcher().dispatchShortcutReleased(shortcutsVector);
  274. }
  275. void CGuiHandler::handleEventMouseMotion(SDL_Event & current)
  276. {
  277. eventDispatcher().dispatchMouseMoved(Point(current.motion.x, current.motion.y));
  278. }
  279. void CGuiHandler::handleEventMouseButtonDown(SDL_Event & current)
  280. {
  281. switch(current.button.button)
  282. {
  283. case SDL_BUTTON_LEFT:
  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. : lastClick(-500, -500)
  407. , lastClickTime(0)
  408. , defActionsDef(0)
  409. , captureChildren(false)
  410. , multifinger(false)
  411. , mouseButtonsMask(0)
  412. , continueEventHandling(true)
  413. , curInt(nullptr)
  414. , fakeStatusBar(std::make_shared<EmptyStatusBar>())
  415. , terminate_cond (new CondSh<bool>(false))
  416. {
  417. }
  418. CGuiHandler::~CGuiHandler()
  419. {
  420. delete terminate_cond;
  421. }
  422. ShortcutHandler & CGuiHandler::shortcutsHandler()
  423. {
  424. assert(shortcutsHandlerInstance);
  425. return *shortcutsHandlerInstance;
  426. }
  427. FramerateManager & CGuiHandler::framerateManager()
  428. {
  429. assert(framerateManagerInstance);
  430. return *framerateManagerInstance;
  431. }
  432. void CGuiHandler::moveCursorToPosition(const Point & position)
  433. {
  434. SDL_WarpMouseInWindow(mainWindow, position.x, position.y);
  435. }
  436. bool CGuiHandler::isKeyboardCtrlDown() const
  437. {
  438. #ifdef VCMI_MAC
  439. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LGUI] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RGUI];
  440. #else
  441. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  442. #endif
  443. }
  444. bool CGuiHandler::isKeyboardAltDown() const
  445. {
  446. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT];
  447. }
  448. bool CGuiHandler::isKeyboardShiftDown() const
  449. {
  450. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT];
  451. }
  452. void CGuiHandler::breakEventHandling()
  453. {
  454. continueEventHandling = false;
  455. }
  456. const Point & CGuiHandler::getCursorPosition() const
  457. {
  458. return cursorPosition;
  459. }
  460. Point CGuiHandler::screenDimensions() const
  461. {
  462. return Point(screen->w, screen->h);
  463. }
  464. bool CGuiHandler::isMouseButtonPressed() const
  465. {
  466. return mouseButtonsMask > 0;
  467. }
  468. bool CGuiHandler::isMouseButtonPressed(MouseButton button) const
  469. {
  470. static_assert(static_cast<uint32_t>(MouseButton::LEFT) == SDL_BUTTON_LEFT, "mismatch between VCMI and SDL enum!");
  471. static_assert(static_cast<uint32_t>(MouseButton::MIDDLE) == SDL_BUTTON_MIDDLE, "mismatch between VCMI and SDL enum!");
  472. static_assert(static_cast<uint32_t>(MouseButton::RIGHT) == SDL_BUTTON_RIGHT, "mismatch between VCMI and SDL enum!");
  473. static_assert(static_cast<uint32_t>(MouseButton::EXTRA1) == SDL_BUTTON_X1, "mismatch between VCMI and SDL enum!");
  474. static_assert(static_cast<uint32_t>(MouseButton::EXTRA2) == SDL_BUTTON_X2, "mismatch between VCMI and SDL enum!");
  475. uint32_t index = static_cast<uint32_t>(button);
  476. return mouseButtonsMask & SDL_BUTTON(index);
  477. }
  478. void CGuiHandler::drawFPSCounter()
  479. {
  480. static SDL_Rect overlay = { 0, 0, 64, 32};
  481. uint32_t black = SDL_MapRGB(screen->format, 10, 10, 10);
  482. SDL_FillRect(screen, &overlay, black);
  483. std::string fps = std::to_string(framerateManager().getFramerate());
  484. graphics->fonts[FONT_BIG]->renderTextLeft(screen, fps, Colors::YELLOW, Point(10, 10));
  485. }
  486. bool CGuiHandler::amIGuiThread()
  487. {
  488. return inGuiThread.get() && *inGuiThread;
  489. }
  490. void CGuiHandler::pushUserEvent(EUserEvent usercode)
  491. {
  492. pushUserEvent(usercode, nullptr);
  493. }
  494. void CGuiHandler::pushUserEvent(EUserEvent usercode, void * userdata)
  495. {
  496. SDL_Event event;
  497. event.type = SDL_USEREVENT;
  498. event.user.code = static_cast<int32_t>(usercode);
  499. event.user.data1 = userdata;
  500. SDL_PushEvent(&event);
  501. }
  502. IScreenHandler & CGuiHandler::screenHandler()
  503. {
  504. return *screenHandlerInstance;
  505. }
  506. InterfaceEventDispatcher & CGuiHandler::eventDispatcher()
  507. {
  508. return *eventDispatcherInstance;
  509. }
  510. WindowHandler & CGuiHandler::windows()
  511. {
  512. assert(windowHandlerInstance);
  513. return *windowHandlerInstance;
  514. }
  515. std::shared_ptr<IStatusBar> CGuiHandler::statusbar()
  516. {
  517. auto locked = currentStatusBar.lock();
  518. if (!locked)
  519. return fakeStatusBar;
  520. return locked;
  521. }
  522. void CGuiHandler::setStatusbar(std::shared_ptr<IStatusBar> newStatusBar)
  523. {
  524. currentStatusBar = newStatusBar;
  525. }
  526. void CGuiHandler::onScreenResize()
  527. {
  528. screenHandler().onScreenResize();
  529. windows().onScreenResize();
  530. }