浏览代码

input modi detection

Laserlicht 1 年之前
父节点
当前提交
5dae5ed30e

+ 8 - 2
client/battle/BattleInterfaceClasses.cpp

@@ -427,7 +427,7 @@ QuickSpellPanel::QuickSpellPanel(BattleInterface & owner)
 {
 	OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
 
-	addUsedEvents(LCLICK | SHOW_POPUP | MOVE);
+	addUsedEvents(LCLICK | SHOW_POPUP | MOVE | INPUT_MODI_CHANGE);
 
 	pos = Rect(0, 0, 52, 600);
 	background = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), pos);
@@ -483,7 +483,8 @@ void QuickSpellPanel::create()
 		{
 			buttonsDisabled.push_back(std::make_shared<TransparentFilledRectangle>(Rect(2, 7 + 50 * i, 48, 36), ColorRGBA(0, 0, 0, 172)));
 		}
-		labels.push_back(std::make_shared<CLabel>(7, 10 + 50 * i, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, config["keyboard"]["battleSpellShortcut" + std::to_string(i)].String()));
+		if(GH.input().getCurrentInputModi() == InputModi::MOUSE)
+			labels.push_back(std::make_shared<CLabel>(7, 10 + 50 * i, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, config["keyboard"]["battleSpellShortcut" + std::to_string(i)].String()));
 
 		buttons.push_back(button);
 	}
@@ -495,6 +496,11 @@ void QuickSpellPanel::show(Canvas & to)
 	CIntObject::show(to);
 }
 
+void QuickSpellPanel::inputModiChanged(InputModi modi)
+{
+	create();
+}
+
 HeroInfoBasicPanel::HeroInfoBasicPanel(const InfoAboutHero & hero, Point * position, bool initializeBackground)
 	: CIntObject(0)
 {

+ 1 - 0
client/battle/BattleInterfaceClasses.h

@@ -167,6 +167,7 @@ public:
 	void create();
 
 	void show(Canvas & to) override;
+	void inputModiChanged(InputModi modi) override;
 };
 
 class HeroInfoBasicPanel : public CIntObject //extracted from InfoWindow to fit better as non-popup embed element

+ 22 - 0
client/eventsSDL/InputHandler.cpp

@@ -37,6 +37,7 @@ InputHandler::InputHandler()
 	: enableMouse(settings["input"]["enableMouse"].Bool())
 	, enableTouch(settings["input"]["enableTouch"].Bool())
 	, enableController(settings["input"]["enableController"].Bool())
+	, currentInputModi(InputModi::MOUSE)
 	, mouseHandler(std::make_unique<InputSourceMouse>())
 	, keyboardHandler(std::make_unique<InputSourceKeyboard>())
 	, fingerHandler(std::make_unique<InputSourceTouch>())
@@ -60,7 +61,10 @@ void InputHandler::handleCurrentEvent(const SDL_Event & current)
 #ifndef VCMI_EMULATE_TOUCHSCREEN_WITH_MOUSE
 		case SDL_MOUSEMOTION:
 			if (enableMouse)
+			{
+				setCurrentInputModi(InputModi::MOUSE);
 				mouseHandler->handleEventMouseMotion(current.motion);
+			}
 			return;
 		case SDL_MOUSEBUTTONDOWN:
 			if (enableMouse)
@@ -83,7 +87,10 @@ void InputHandler::handleCurrentEvent(const SDL_Event & current)
 			return;
 		case SDL_FINGERMOTION:
 			if (enableTouch)
+			{
+				setCurrentInputModi(InputModi::TOUCH);
 				fingerHandler->handleEventFingerMotion(current.tfinger);
+			}
 			return;
 		case SDL_FINGERDOWN:
 			if (enableTouch)
@@ -95,7 +102,10 @@ void InputHandler::handleCurrentEvent(const SDL_Event & current)
 			return;
 		case SDL_CONTROLLERAXISMOTION:
 			if (enableController)
+			{
+				setCurrentInputModi(InputModi::CONTROLLER);
 				gameControllerHandler->handleEventAxisMotion(current.caxis);
+			}
 			return;
 		case SDL_CONTROLLERBUTTONDOWN:
 			if (enableController)
@@ -108,6 +118,18 @@ void InputHandler::handleCurrentEvent(const SDL_Event & current)
 	}
 }
 
+void InputHandler::setCurrentInputModi(InputModi modi)
+{
+	if(currentInputModi != modi)
+		GH.events().dispatchInputModiChanged(modi);
+	currentInputModi = modi;
+}
+
+InputModi InputHandler::getCurrentInputModi()
+{
+	return currentInputModi;
+}
+
 std::vector<SDL_Event> InputHandler::acquireEvents()
 {
 	boost::unique_lock<boost::mutex> lock(eventsMutex);

+ 12 - 0
client/eventsSDL/InputHandler.h

@@ -23,6 +23,13 @@ class InputSourceTouch;
 class InputSourceText;
 class InputSourceGameController;
 
+enum class InputModi
+{
+	MOUSE,
+	TOUCH,
+	CONTROLLER
+};
+
 class InputHandler
 {
 	std::vector<SDL_Event> eventsQueue;
@@ -34,6 +41,9 @@ class InputHandler
 	const bool enableTouch;
 	const bool enableController;
 
+	InputModi currentInputModi;
+	void setCurrentInputModi(InputModi modi);
+
 	std::vector<SDL_Event> acquireEvents();
 
 	void preprocessEvent(const SDL_Event & event);
@@ -91,4 +101,6 @@ public:
 	bool isKeyboardCmdDown() const;
 	bool isKeyboardCtrlDown() const;
 	bool isKeyboardShiftDown() const;
+
+	InputModi getCurrentInputModi();
 };

+ 10 - 0
client/gui/EventDispatcher.cpp

@@ -19,6 +19,7 @@
 
 #include "../../lib/CConfigHandler.h"
 #include "../../lib/Rect.h"
+#include "../eventsSDL/InputHandler.h"
 
 template<typename Functor>
 void EventDispatcher::processLists(ui16 activityFlag, const Functor & cb)
@@ -40,6 +41,7 @@ void EventDispatcher::processLists(ui16 activityFlag, const Functor & cb)
 	processList(AEventsReceiver::DOUBLECLICK, doubleClickInterested);
 	processList(AEventsReceiver::TEXTINPUT, textInterested);
 	processList(AEventsReceiver::GESTURE, panningInterested);
+	processList(AEventsReceiver::INPUT_MODI_CHANGE, inputModiChangeInterested);
 }
 
 void EventDispatcher::activateElement(AEventsReceiver * elem, ui16 activityFlag)
@@ -316,6 +318,14 @@ void EventDispatcher::dispatchTextEditing(const std::string & text)
 	}
 }
 
+void EventDispatcher::dispatchInputModiChanged(const InputModi & modi)
+{
+	for(auto it : inputModiChangeInterested)
+	{
+		it->inputModiChanged(modi);
+	}
+}
+
 void EventDispatcher::dispatchGesturePanningStarted(const Point & initialPosition)
 {
 	auto copied = panningInterested;

+ 4 - 0
client/gui/EventDispatcher.h

@@ -16,6 +16,7 @@ VCMI_LIB_NAMESPACE_END
 class AEventsReceiver;
 enum class MouseButton;
 enum class EShortcut;
+enum class InputModi;
 
 /// Class that receives events from event producers and dispatches it to UI elements that are interested in this event
 class EventDispatcher
@@ -34,6 +35,7 @@ class EventDispatcher
 	EventReceiversList doubleClickInterested;
 	EventReceiversList textInterested;
 	EventReceiversList panningInterested;
+	EventReceiversList inputModiChangeInterested;
 
 	void handleLeftButtonClick(const Point & position, int tolerance, bool isPressed);
 	void handleDoubleButtonClick(const Point & position, int tolerance);
@@ -76,4 +78,6 @@ public:
 	/// Text input events
 	void dispatchTextInput(const std::string & text);
 	void dispatchTextEditing(const std::string & text);
+
+	void dispatchInputModiChanged(const InputModi & modi);
 };

+ 4 - 0
client/gui/EventsReceiver.h

@@ -16,6 +16,7 @@ VCMI_LIB_NAMESPACE_END
 
 class EventDispatcher;
 enum class EShortcut;
+enum class InputModi;
 
 /// Class that is capable of subscribing and receiving input events
 /// Acts as base class for all UI elements
@@ -75,6 +76,8 @@ public:
 
 	virtual void tick(uint32_t msPassed) {}
 
+	virtual void inputModiChanged(InputModi modi) {}
+
 public:
 	AEventsReceiver();
 	virtual ~AEventsReceiver() = default;
@@ -94,6 +97,7 @@ public:
 		TEXTINPUT = 512,
 		GESTURE = 1024,
 		DRAG = 2048,
+		INPUT_MODI_CHANGE = 4096
 	};
 
 	/// Returns true if element is currently hovered by mouse