Browse Source

API for custom events no longer requires SDL access

Ivan Savenko 2 years ago
parent
commit
e1bd0d2a04

+ 3 - 3
client/CMT.cpp

@@ -54,7 +54,7 @@
 #include <SDL.h>
 
 #ifdef VCMI_WINDOWS
-#include "SDL_syswm.h"
+#include <SDL_syswm.h>
 #endif
 #ifdef VCMI_ANDROID
 #include "lib/CAndroidVMHelper.h"
@@ -949,7 +949,7 @@ static void handleEvent(SDL_Event & ev)
 	}
 	else if(ev.type == SDL_USEREVENT)
 	{
-		switch(ev.user.code)
+		switch(static_cast<EUserEvent>(ev.user.code))
 		{
 		case EUserEvent::FORCE_QUIT:
 			{
@@ -1050,7 +1050,7 @@ static void handleEvent(SDL_Event & ev)
 static void mainLoop()
 {
 	SettingsListener resChanged = settings.listen["video"]["fullscreen"];
-	resChanged([](const JsonNode &newState){  CGuiHandler::pushSDLEvent(SDL_USEREVENT, EUserEvent::FULLSCREEN_TOGGLED); });
+	resChanged([](const JsonNode &newState){  CGuiHandler::pushUserEvent(EUserEvent::FULLSCREEN_TOGGLED); });
 
 	inGuiThread.reset(new bool(true));
 	GH.mainFPSmng->init();

+ 2 - 7
client/CPlayerInterface.cpp

@@ -2139,12 +2139,7 @@ void CPlayerInterface::requestReturningToMainMenu(bool won)
 	if(won && cb->getStartInfo()->campState)
 		CSH->startCampaignScenario(cb->getStartInfo()->campState);
 	else
-		sendCustomEvent(EUserEvent::RETURN_TO_MAIN_MENU);
-}
-
-void CPlayerInterface::sendCustomEvent( int code )
-{
-	CGuiHandler::pushSDLEvent(SDL_USEREVENT, code);
+		GH.pushUserEvent(EUserEvent::RETURN_TO_MAIN_MENU);
 }
 
 void CPlayerInterface::askToAssembleArtifact(const ArtifactLocation &al)
@@ -2268,7 +2263,7 @@ void CPlayerInterface::waitForAllDialogs(bool unlockPim)
 
 void CPlayerInterface::proposeLoadingGame()
 {
-	showYesNoDialog(CGI->generaltexth->allTexts[68], [this](){ sendCustomEvent(EUserEvent::RETURN_TO_MENU_LOAD); }, nullptr);
+	showYesNoDialog(CGI->generaltexth->allTexts[68], [this](){ GH.pushUserEvent(EUserEvent::RETURN_TO_MENU_LOAD); }, nullptr);
 }
 
 CPlayerInterface::SpellbookLastSetting::SpellbookLastSetting()

+ 0 - 1
client/CPlayerInterface.h

@@ -240,7 +240,6 @@ public:
 	void tryDiggging(const CGHeroInstance *h);
 	void showShipyardDialogOrProblemPopup(const IShipyard *obj); //obj may be town or shipyard;
 	void requestReturningToMainMenu(bool won);
-	void sendCustomEvent(int code);
 	void proposeLoadingGame();
 
 	// Ambient sounds

+ 2 - 2
client/CServerHandler.cpp

@@ -657,7 +657,7 @@ void CServerHandler::startCampaignScenario(std::shared_ptr<CCampaignState> cs)
 {
 	SDL_Event event;
 	event.type = SDL_USEREVENT;
-	event.user.code = EUserEvent::CAMPAIGN_START_SCENARIO;
+	event.user.code = static_cast<int32_t>(EUserEvent::CAMPAIGN_START_SCENARIO);
 	if(cs)
 		event.user.data1 = CMemorySerializer::deepCopy(*cs.get()).release();
 	else
@@ -824,7 +824,7 @@ void CServerHandler::threadHandleConnection()
 			if(client)
 			{
 				state = EClientState::DISCONNECTING;
-				CGuiHandler::pushSDLEvent(SDL_USEREVENT, EUserEvent::RETURN_TO_MAIN_MENU);
+				CGuiHandler::pushUserEvent(EUserEvent::RETURN_TO_MAIN_MENU);
 			}
 			else
 			{

+ 1 - 1
client/adventureMap/CAdvMapInt.cpp

@@ -792,7 +792,7 @@ void CAdvMapInt::keyDown(const SDL_Keycode & key)
 		if(isActive() && GH.isKeyboardCtrlDown())
 		{
 			LOCPLINT->showYesNoDialog(CGI->generaltexth->translate("vcmi.adventureMap.confirmRestartGame"),
-				[](){ LOCPLINT->sendCustomEvent(EUserEvent::RESTART_GAME); }, nullptr);
+				[](){ GH.pushUserEvent(EUserEvent::RESTART_GAME); }, nullptr);
 		}
 		return;
 	case SDLK_SPACE: //space - try to revisit current object with selected hero

+ 6 - 6
client/gui/CGuiHandler.cpp

@@ -118,7 +118,7 @@ void CGuiHandler::popInt(std::shared_ptr<IShowActivatable> top)
 		listInt.front()->activate();
 	totalRedraw();
 
-	pushSDLEvent(SDL_USEREVENT, EUserEvent::INTERFACE_CHANGED);
+	pushUserEvent(EUserEvent::INTERFACE_CHANGED);
 }
 
 void CGuiHandler::pushInt(std::shared_ptr<IShowActivatable> newInt)
@@ -137,7 +137,7 @@ void CGuiHandler::pushInt(std::shared_ptr<IShowActivatable> newInt)
 	objsToBlit.push_back(newInt);
 	totalRedraw();
 
-	pushSDLEvent(SDL_USEREVENT, EUserEvent::INTERFACE_CHANGED);
+	pushUserEvent(EUserEvent::INTERFACE_CHANGED);
 }
 
 void CGuiHandler::popInts(int howMany)
@@ -160,7 +160,7 @@ void CGuiHandler::popInts(int howMany)
 	}
 	fakeMouseMove();
 
-	pushSDLEvent(SDL_USEREVENT, EUserEvent::INTERFACE_CHANGED);
+	pushUserEvent(EUserEvent::INTERFACE_CHANGED);
 }
 
 std::shared_ptr<IShowActivatable> CGuiHandler::topInt()
@@ -825,11 +825,11 @@ bool CGuiHandler::amIGuiThread()
 	return inGuiThread.get() && *inGuiThread;
 }
 
-void CGuiHandler::pushSDLEvent(int type, int usercode)
+void CGuiHandler::pushUserEvent(EUserEvent usercode)
 {
 	SDL_Event event;
-	event.type = type;
-	event.user.code = usercode;	// not necessarily used
+	event.type = SDL_USEREVENT;
+	event.user.code = static_cast<int32_t>(usercode);
 	SDL_PushEvent(&event);
 }
 

+ 2 - 2
client/gui/CGuiHandler.h

@@ -32,7 +32,7 @@ class IShowActivatable;
 class IShowable;
 
 // TODO: event handling need refactoring
-enum EUserEvent
+enum class EUserEvent
 {
 	/*CHANGE_SCREEN_RESOLUTION = 1,*/
 	RETURN_TO_MAIN_MENU = 2,
@@ -176,7 +176,7 @@ public:
 	static bool isNumKey(SDL_Keycode key, bool number = true); //checks if key is on numpad (numbers - check only for numpad digits)
 	static bool isArrowKey(SDL_Keycode key);
 	static bool amIGuiThread();
-	static void pushSDLEvent(int type, int usercode = 0);
+	static void pushUserEvent(EUserEvent usercode);
 
 	CondSh<bool> * terminate_cond; // confirm termination
 };

+ 1 - 1
client/lobby/CBonusSelection.cpp

@@ -454,7 +454,7 @@ void CBonusSelection::restartMap()
 	close();
 	LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [=]()
 	{
-		LOCPLINT->sendCustomEvent(EUserEvent::RESTART_GAME);
+		GH.pushUserEvent(EUserEvent::RESTART_GAME);
 	}, 0);
 }
 

+ 6 - 6
client/windows/GUIClasses.cpp

@@ -63,7 +63,7 @@
 #include "../lib/NetPacksBase.h"
 #include "../lib/StartInfo.h"
 
-#include <SDL_events.h>
+#include <SDL_video.h>
 
 using namespace CSDL_Ext;
 
@@ -610,7 +610,7 @@ void CSystemOptionsWindow::setGameRes(int index)
 
 void CSystemOptionsWindow::bquitf()
 {
-	LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::FORCE_QUIT); }, 0);
+	LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(EUserEvent::FORCE_QUIT); }, 0);
 }
 
 void CSystemOptionsWindow::breturnf()
@@ -620,7 +620,7 @@ void CSystemOptionsWindow::breturnf()
 
 void CSystemOptionsWindow::bmainmenuf()
 {
-	LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::RETURN_TO_MAIN_MENU); }, 0);
+	LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(EUserEvent::RETURN_TO_MAIN_MENU); }, 0);
 }
 
 void CSystemOptionsWindow::bloadf()
@@ -637,13 +637,13 @@ void CSystemOptionsWindow::bsavef()
 
 void CSystemOptionsWindow::brestartf()
 {
-	LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this](){ closeAndPushEvent(SDL_USEREVENT, EUserEvent::RESTART_GAME); }, 0);
+	LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this](){ closeAndPushEvent(EUserEvent::RESTART_GAME); }, 0);
 }
 
-void CSystemOptionsWindow::closeAndPushEvent(int eventType, int code)
+void CSystemOptionsWindow::closeAndPushEvent(EUserEvent code)
 {
 	close();
-	GH.pushSDLEvent(eventType, code);
+	GH.pushUserEvent(code);
 }
 
 CTavernWindow::CTavernWindow(const CGObjectInstance * TavernObj)

+ 3 - 1
client/windows/GUIClasses.h

@@ -43,6 +43,8 @@ class CTextBox;
 class CResDataBar;
 class CHeroWithMaybePickedArtifact;
 
+enum class EUserEvent;
+
 /// Recruitment window where you can recruit creatures
 class CRecruitmentWindow : public CStatusbarWindow
 {
@@ -234,7 +236,7 @@ private:
 
 	void selectGameRes();
 	void setGameRes(int index);
-	void closeAndPushEvent(int eventType, int code = 0);
+	void closeAndPushEvent(EUserEvent code);
 
 public:
 	CSystemOptionsWindow();