Browse Source

Working text input (again)

Marko Pintera 12 years ago
parent
commit
961055185e

+ 3 - 3
BansheeEngine/Include/BsGUIKeyEvent.h

@@ -20,16 +20,16 @@ namespace BansheeEngine
 
 
 		GUIKeyEventType getType() const { return mType; }
 		GUIKeyEventType getType() const { return mType; }
 		CM::KeyCode getKey() const { return mKey; }
 		CM::KeyCode getKey() const { return mKey; }
-		const CM::WString& getInputString() const { return mInputString; }
+		const CM::UINT32& getInputChar() const { return mInputChar; }
 	private:
 	private:
 		friend class GUIManager;
 		friend class GUIManager;
 
 
 		GUIKeyEventType mType;
 		GUIKeyEventType mType;
 		CM::KeyCode mKey;
 		CM::KeyCode mKey;
-		CM::WString mInputString;
+		CM::UINT32 mInputChar;
 
 
 		void setKeyDownData(CM::KeyCode key);
 		void setKeyDownData(CM::KeyCode key);
 		void setKeyUpData(CM::KeyCode key);
 		void setKeyUpData(CM::KeyCode key);
-		void setTextInputData(const CM::WString& string);
+		void setTextInputData(CM::UINT32 inputChar);
 	};
 	};
 }
 }

+ 2 - 3
BansheeEngine/Include/BsGUIManager.h

@@ -67,10 +67,9 @@ namespace BansheeEngine
 		boost::signals::connection mOnKeyUpConn;
 		boost::signals::connection mOnKeyUpConn;
 
 
 		void updateMeshes();
 		void updateMeshes();
-		void updateInput();
 
 
-		void onKeyDown(CM::KeyCode keyCode);
-		void onKeyUp(CM::KeyCode keyCode);
+		void onKeyDown(const CM::KeyEvent& event);
+		void onKeyUp(const CM::KeyEvent& event);
 
 
 		void onMouseMoved(const CM::MouseEvent& event);
 		void onMouseMoved(const CM::MouseEvent& event);
 		void onMouseDown(const CM::MouseEvent& event, CM::MouseButton buttonID);
 		void onMouseDown(const CM::MouseEvent& event, CM::MouseButton buttonID);

+ 1 - 1
BansheeEngine/Source/BsGUIInputBox.cpp

@@ -236,7 +236,7 @@ namespace BansheeEngine
 		if(ev.getType() == GUIKeyEventType::TextInput)
 		if(ev.getType() == GUIKeyEventType::TextInput)
 		{
 		{
 			// TODO - How are backspace, caps and enter handled?
 			// TODO - How are backspace, caps and enter handled?
-			mText += ev.getInputString();
+			mText += ev.getInputChar();
 
 
 			markAsDirty();
 			markAsDirty();
 
 

+ 4 - 4
BansheeEngine/Source/BsGUIKeyEvent.cpp

@@ -14,20 +14,20 @@ namespace BansheeEngine
 	{
 	{
 		mType = GUIKeyEventType::KeyDown;
 		mType = GUIKeyEventType::KeyDown;
 		mKey = key;
 		mKey = key;
-		mInputString = "";
+		mInputChar = 0;
 	}
 	}
 
 
 	void GUIKeyEvent::setKeyUpData(KeyCode key)
 	void GUIKeyEvent::setKeyUpData(KeyCode key)
 	{
 	{
 		mType = GUIKeyEventType::KeyUp;
 		mType = GUIKeyEventType::KeyUp;
 		mKey = key;
 		mKey = key;
-		mInputString = "";
+		mInputChar = 0;
 	}
 	}
 
 
-	void GUIKeyEvent::setTextInputData(const WString& string)
+	void GUIKeyEvent::setTextInputData(UINT32 inputChar)
 	{
 	{
 		mType = GUIKeyEventType::TextInput;
 		mType = GUIKeyEventType::TextInput;
 		mKey = KC_0;
 		mKey = KC_0;
-		mInputString = string;
+		mInputChar = inputChar;
 	}
 	}
 }
 }

+ 30 - 15
BansheeEngine/Source/BsGUIManager.cpp

@@ -111,7 +111,6 @@ namespace BansheeEngine
 		}
 		}
 
 
 		updateMeshes();
 		updateMeshes();
-		updateInput();
 	}
 	}
 
 
 	void GUIManager::render(ViewportPtr& target, RenderContext& renderContext)
 	void GUIManager::render(ViewportPtr& target, RenderContext& renderContext)
@@ -405,31 +404,35 @@ namespace BansheeEngine
 		}
 		}
 	}
 	}
 
 
-	void GUIManager::updateInput()
+	void GUIManager::onKeyDown(const KeyEvent& event)
 	{
 	{
+		if(event.isUsed())
+			return;
 
 
-	}
-
-	void GUIManager::onKeyDown(KeyCode keyCode)
-	{
-		//if(mKeyboardFocusElement != nullptr)
-		//{
-		//	mKeyEvent = GUIKeyEvent();
-
-		//	// TODO - Handle KeyUp/KeyDown states
+		if(mKeyboardFocusElement != nullptr)
+		{
+			mKeyEvent = GUIKeyEvent();
 
 
-		//	mKeyEvent.setTextInputData(gInput().getInputString());
-		//	mKeyboardFocusWidget->_keyEvent(mKeyboardFocusElement, mKeyEvent);
-		//}
+			mKeyEvent.setTextInputData(event.textChar);
+			mKeyboardFocusWidget->_keyEvent(mKeyboardFocusElement, mKeyEvent);
+		}
+		
+		event.markAsUsed();
 	}
 	}
 
 
-	void GUIManager::onKeyUp(KeyCode keyCode)
+	void GUIManager::onKeyUp(const KeyEvent& event)
 	{
 	{
+		if(event.isUsed())
+			return;
 
 
+		event.markAsUsed();
 	}
 	}
 
 
 	void GUIManager::onMouseMoved(const MouseEvent& event)
 	void GUIManager::onMouseMoved(const MouseEvent& event)
 	{
 	{
+		if(event.isUsed())
+			return;
+
 #if CM_DEBUG_MODE
 #if CM_DEBUG_MODE
 		// Checks if all referenced windows actually exist
 		// Checks if all referenced windows actually exist
 		vector<RenderWindow*>::type activeWindows = RenderWindowManager::instance().getRenderWindows();
 		vector<RenderWindow*>::type activeWindows = RenderWindowManager::instance().getRenderWindows();
@@ -566,10 +569,15 @@ namespace BansheeEngine
 
 
 		mMouseOverElement = topMostElement;
 		mMouseOverElement = topMostElement;
 		mMouseOverWidget = widgetInFocus;
 		mMouseOverWidget = widgetInFocus;
+
+		event.markAsUsed();
 	}
 	}
 
 
 	void GUIManager::onMouseDown(const MouseEvent& event, MouseButton buttonID)
 	void GUIManager::onMouseDown(const MouseEvent& event, MouseButton buttonID)
 	{
 	{
+		if(event.isUsed())
+			return;
+
 		// TODO - Maybe avoid querying these for every event separately?
 		// TODO - Maybe avoid querying these for every event separately?
 		bool buttonStates[(int)MB_Count];
 		bool buttonStates[(int)MB_Count];
 		for(int i = 0; i < MB_Count; i++)
 		for(int i = 0; i < MB_Count; i++)
@@ -604,10 +612,15 @@ namespace BansheeEngine
 
 
 		mKeyboardFocusElement = mMouseOverElement;
 		mKeyboardFocusElement = mMouseOverElement;
 		mKeyboardFocusWidget = mMouseOverWidget;
 		mKeyboardFocusWidget = mMouseOverWidget;
+
+		event.markAsUsed();
 	}
 	}
 
 
 	void GUIManager::onMouseUp(const MouseEvent& event, MouseButton buttonID)
 	void GUIManager::onMouseUp(const MouseEvent& event, MouseButton buttonID)
 	{
 	{
+		if(event.isUsed())
+			return;
+
 		// TODO - Maybe avoid querying these for every event separately?
 		// TODO - Maybe avoid querying these for every event separately?
 		bool buttonStates[(int)MB_Count];
 		bool buttonStates[(int)MB_Count];
 		for(int i = 0; i < MB_Count; i++)
 		for(int i = 0; i < MB_Count; i++)
@@ -641,6 +654,8 @@ namespace BansheeEngine
 			mActiveWidget = nullptr;
 			mActiveWidget = nullptr;
 			mActiveMouseButton = 0;
 			mActiveMouseButton = 0;
 		}	
 		}	
+
+		event.markAsUsed();
 	}
 	}
 
 
 	Int2 GUIManager::getWidgetRelativePos(const GUIWidget& widget, const Int2& screenPos)
 	Int2 GUIManager::getWidgetRelativePos(const GUIWidget& widget, const Int2& screenPos)

+ 4 - 10
CamelotCore/Include/CmInput.h

@@ -13,14 +13,13 @@ namespace CamelotFramework
 		Input();
 		Input();
 		~Input();
 		~Input();
 
 
-		boost::signal<void(KeyCode)> onKeyDown;
-		boost::signal<void(KeyCode)> onKeyUp;
+		boost::signal<void(const KeyEvent&)> onKeyDown;
+		boost::signal<void(const KeyEvent&)> onKeyUp;
 
 
 		boost::signal<void(const MouseEvent&)> onMouseMoved;
 		boost::signal<void(const MouseEvent&)> onMouseMoved;
 		boost::signal<void(const MouseEvent&, MouseButton)> onMouseDown;
 		boost::signal<void(const MouseEvent&, MouseButton)> onMouseDown;
 		boost::signal<void(const MouseEvent&, MouseButton)> onMouseUp;
 		boost::signal<void(const MouseEvent&, MouseButton)> onMouseUp;
 
 
-
 		void initClipRect(Rect& clipRect);
 		void initClipRect(Rect& clipRect);
 		void registerInputHandler(InputHandlerPtr inputHandler);
 		void registerInputHandler(InputHandlerPtr inputHandler);
 
 
@@ -46,11 +45,6 @@ namespace CamelotFramework
 		bool isButtonDown(MouseButton button) const;
 		bool isButtonDown(MouseButton button) const;
 		bool isKeyDown(KeyCode keyCode) const;
 		bool isKeyDown(KeyCode keyCode) const;
 
 
-		/**
-		 * @brief	Gets all the characters inputted since the last frame.
-		 */
-		WString getInputString() const { return mInputHandler->getInputString(); }
-
 	private:
 	private:
 		InputHandlerPtr mInputHandler;
 		InputHandlerPtr mInputHandler;
 
 
@@ -69,8 +63,8 @@ namespace CamelotFramework
 		bool mMouseButtonState[MB_Count];
 		bool mMouseButtonState[MB_Count];
 		bool mKeyState[KC_Count];
 		bool mKeyState[KC_Count];
 
 
-		void keyDown(KeyCode keyCode);
-		void keyUp(KeyCode keyCode);
+		void keyDown(const KeyEvent& event);
+		void keyUp(const KeyEvent& event);
 
 
 		void mouseMoved(const MouseEvent& event);
 		void mouseMoved(const MouseEvent& event);
 		void mouseDown(const MouseEvent& event, MouseButton buttonID);
 		void mouseDown(const MouseEvent& event, MouseButton buttonID);

+ 29 - 7
CamelotCore/Include/CmInputHandler.h

@@ -169,13 +169,40 @@ namespace CamelotFramework
 		MB_Button18, MB_Button19, MB_Button20, MB_Count
 		MB_Button18, MB_Button19, MB_Button20, MB_Count
 	};
 	};
 
 
+	struct KeyEvent
+	{
+	public:
+		KeyEvent()
+			:mIsUsed(false)
+		{ }
+
+		KeyCode keyCode;
+		UINT32 textChar;
+
+		bool isUsed() const { return mIsUsed; }
+		void markAsUsed() const { mIsUsed = true; }
+	private:
+		mutable bool mIsUsed;
+	};
+
 	struct MouseEvent
 	struct MouseEvent
 	{
 	{
+	public:
+		MouseEvent()
+			:mIsUsed(false)
+		{ }
+
 		Int2 coords;
 		Int2 coords;
 		Int2 relCoords;
 		Int2 relCoords;
 
 
 		int z;
 		int z;
 		int relZ;
 		int relZ;
+
+		bool isUsed() const { return mIsUsed; }
+		void markAsUsed() const { mIsUsed = true; }
+
+	private:
+		mutable bool mIsUsed;
 	};
 	};
 
 
 	/**
 	/**
@@ -188,8 +215,8 @@ namespace CamelotFramework
 		InputHandler() {}
 		InputHandler() {}
 		virtual ~InputHandler() {}
 		virtual ~InputHandler() {}
 
 
-		boost::signal<void(KeyCode)> onKeyDown;
-		boost::signal<void(KeyCode)> onKeyUp;
+		boost::signal<void(const KeyEvent&)> onKeyDown;
+		boost::signal<void(const KeyEvent&)> onKeyUp;
 
 
 		boost::signal<void(const MouseEvent&)> onMouseMoved;
 		boost::signal<void(const MouseEvent&)> onMouseMoved;
 		boost::signal<void(const MouseEvent&, MouseButton)> onMouseDown;
 		boost::signal<void(const MouseEvent&, MouseButton)> onMouseDown;
@@ -199,10 +226,5 @@ namespace CamelotFramework
 		 * @brief	Called every frame by InputManager. Capture input here if needed.
 		 * @brief	Called every frame by InputManager. Capture input here if needed.
 		 */
 		 */
 		virtual void update() {}
 		virtual void update() {}
-
-		/**
-		 * @brief	Gets all the characters inputted since the last frame.
-		 */
-		virtual const WString& getInputString() const = 0;
 	};
 	};
 }
 }

+ 6 - 6
CamelotCore/Source/CmInput.cpp

@@ -78,16 +78,16 @@ namespace CamelotFramework
 		updateSmoothInput();
 		updateSmoothInput();
 	}
 	}
 
 
-	void Input::keyDown(KeyCode keyCode)
+	void Input::keyDown(const KeyEvent& event)
 	{
 	{
-		mKeyState[keyCode] = true;
-		onKeyDown(keyCode);
+		mKeyState[event.keyCode] = true;
+		onKeyDown(event);
 	}
 	}
 
 
-	void Input::keyUp(KeyCode keyCode)
+	void Input::keyUp(const KeyEvent& event)
 	{
 	{
-		mKeyState[keyCode] = false;
-		onKeyUp(keyCode);
+		mKeyState[event.keyCode] = false;
+		onKeyUp(event);
 	}
 	}
 
 
 	void Input::mouseMoved(const MouseEvent& event)
 	void Input::mouseMoved(const MouseEvent& event)

+ 0 - 2
CamelotOISInput/Include/CmInputHandlerOIS.h

@@ -16,12 +16,10 @@ namespace CamelotFramework
 		InputHandlerOIS(unsigned int hWnd);
 		InputHandlerOIS(unsigned int hWnd);
 		virtual ~InputHandlerOIS();
 		virtual ~InputHandlerOIS();
 
 
-		const WString& getInputString() const { return mInputString; }
 	private:
 	private:
 		OIS::InputManager*	mInputManager;
 		OIS::InputManager*	mInputManager;
 		OIS::Mouse*			mMouse;
 		OIS::Mouse*			mMouse;
 		OIS::Keyboard*		mKeyboard;
 		OIS::Keyboard*		mKeyboard;
-		WString				mInputString;
 
 
 		virtual bool keyPressed(const OIS::KeyEvent& arg);
 		virtual bool keyPressed(const OIS::KeyEvent& arg);
 		virtual bool keyReleased(const OIS::KeyEvent& arg);
 		virtual bool keyReleased(const OIS::KeyEvent& arg);

+ 12 - 12
CamelotOISInput/Source/CmInputHandlerOIS.cpp

@@ -56,27 +56,27 @@ namespace CamelotFramework
 
 
 	void InputHandlerOIS::update()
 	void InputHandlerOIS::update()
 	{
 	{
-		mInputString = "";
-
 		mMouse->capture();
 		mMouse->capture();
 		mKeyboard->capture();
 		mKeyboard->capture();
 	}
 	}
 
 
 	bool InputHandlerOIS::keyPressed(const OIS::KeyEvent &arg)
 	bool InputHandlerOIS::keyPressed(const OIS::KeyEvent &arg)
 	{
 	{
-		if(arg.text != 0)
-		{
-			mInputString += arg.text;
-		}
-
-		onKeyDown((KeyCode)(int)arg.key);
+		KeyEvent event;
+		event.keyCode = (KeyCode)(int)arg.key;
+		event.textChar = arg.text;
 
 
+		onKeyDown(event);
 		return true;
 		return true;
 	}
 	}
 
 
 	bool InputHandlerOIS::keyReleased(const OIS::KeyEvent& arg)
 	bool InputHandlerOIS::keyReleased(const OIS::KeyEvent& arg)
 	{
 	{
-		onKeyUp((KeyCode)(int)arg.key);
+		KeyEvent event;
+		event.keyCode = (KeyCode)(int)arg.key;
+		event.textChar = arg.text;
+
+		onKeyUp(event);
 		return true;
 		return true;
 	}
 	}
 
 
@@ -87,8 +87,8 @@ namespace CamelotFramework
 		event.relCoords = Int2(arg.state.X.rel, arg.state.Y.rel);
 		event.relCoords = Int2(arg.state.X.rel, arg.state.Y.rel);
 		event.z = arg.state.Z.abs;
 		event.z = arg.state.Z.abs;
 		event.relZ = arg.state.Z.rel;
 		event.relZ = arg.state.Z.rel;
-		onMouseMoved(event);
 
 
+		onMouseMoved(event);
 		return true;
 		return true;
 	}
 	}
 
 
@@ -99,8 +99,8 @@ namespace CamelotFramework
 		event.relCoords = Int2(arg.state.X.rel, arg.state.Y.rel);
 		event.relCoords = Int2(arg.state.X.rel, arg.state.Y.rel);
 		event.z = arg.state.Z.abs;
 		event.z = arg.state.Z.abs;
 		event.relZ = arg.state.Z.rel;
 		event.relZ = arg.state.Z.rel;
-		onMouseDown(event, (MouseButton)(int)id);
 
 
+		onMouseDown(event, (MouseButton)(int)id);
 		return true;
 		return true;
 	}
 	}
 
 
@@ -110,8 +110,8 @@ namespace CamelotFramework
 		event.coords = Int2(arg.state.X.abs, arg.state.Y.abs);
 		event.coords = Int2(arg.state.X.abs, arg.state.Y.abs);
 		event.relCoords = Int2(0, 0);
 		event.relCoords = Int2(0, 0);
 		event.z = arg.state.Z.abs;
 		event.z = arg.state.Z.abs;
-		onMouseUp(event, (MouseButton)(int)id);
 
 
+		onMouseUp(event, (MouseButton)(int)id);
 		return true;
 		return true;
 	}
 	}
 }
 }

+ 0 - 1
TODO.txt

@@ -22,7 +22,6 @@ I call waitUntilLoaded too many times. Sometimes 5-6 times in a single function.
 GUIWidget::updateMeshes leaks. If I leave the game running I can see memory continously going up
 GUIWidget::updateMeshes leaks. If I leave the game running I can see memory continously going up
 
 
 IMMEDIATE:
 IMMEDIATE:
- - OIS provides me only with main window cursor coordinates, while I need screen coordinates. Modify OIS project and correct that. Merging it with my plugin is probably a bad idea.
  - Add window-management command to DeferredRenderContext - I already have resource management commands
  - Add window-management command to DeferredRenderContext - I already have resource management commands
    there, few window related ones won't hurt. I can always split the class if needed.
    there, few window related ones won't hurt. I can always split the class if needed.
      - Possibly add guards to render target classes so they aren't accidentaly accessed from the render thread (similar to how Texture is handled)
      - Possibly add guards to render target classes so they aren't accidentaly accessed from the render thread (similar to how Texture is handled)