Ver Fonte

Refactored selection code a bit

Marko Pintera há 12 anos atrás
pai
commit
2716b1c7bd
2 ficheiros alterados com 173 adições e 162 exclusões
  1. 3 0
      BansheeEngine/Include/BsGUIInputBox.h
  2. 170 162
      BansheeEngine/Source/BsGUIInputBox.cpp

+ 3 - 0
BansheeEngine/Include/BsGUIInputBox.h

@@ -80,6 +80,7 @@ namespace BansheeEngine
 
 
 		virtual bool mouseEvent(const GUIMouseEvent& ev);
 		virtual bool mouseEvent(const GUIMouseEvent& ev);
 		virtual bool keyEvent(const GUIKeyEvent& ev);
 		virtual bool keyEvent(const GUIKeyEvent& ev);
+
 		virtual bool commandEvent(const GUICommandEvent& ev);
 		virtual bool commandEvent(const GUICommandEvent& ev);
 
 
 		Sprite* renderElemToSprite(CM::UINT32 renderElemIdx, CM::UINT32& localRenderElemIdx) const;
 		Sprite* renderElemToSprite(CM::UINT32 renderElemIdx, CM::UINT32& localRenderElemIdx) const;
@@ -90,6 +91,8 @@ namespace BansheeEngine
 
 
 		void showSelection(CM::UINT32 startChar);
 		void showSelection(CM::UINT32 startChar);
 		void clearSelection();
 		void clearSelection();
+		void moveSelectionLeft(bool skipNewline);
+		void moveSelectionRight(bool skipnewLine);
 		CM::UINT32 getCaretSelectionCharIdx(SelectionDir dir) const;
 		CM::UINT32 getCaretSelectionCharIdx(SelectionDir dir) const;
 		bool isNewlineChar(CM::UINT32 charIdx) const;
 		bool isNewlineChar(CM::UINT32 charIdx) const;
 		CM::Vector<CM::Rect>::type getSelectionRects() const;
 		CM::Vector<CM::Rect>::type getSelectionRects() const;

+ 170 - 162
BansheeEngine/Source/BsGUIInputBox.cpp

@@ -449,85 +449,7 @@ namespace BansheeEngine
 						showSelection(getCaretSelectionCharIdx(SelectionDir::Left));
 						showSelection(getCaretSelectionCharIdx(SelectionDir::Left));
 					}
 					}
 
 
-					if(mSelectionAnchor == mSelectionEnd)
-					{
-						UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-						if(mInputCaret->getCaretPos() > 0)
-						{
-							mInputCaret->moveCaretLeft();
-							charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-
-							if(!caretMovedDueToNewline) // Only move if we didn't already move due to newline, so we don't move twice
-							{
-								if (isNewlineChar(charIdx) && mInputCaret->getCaretPos() > 0)
-								{
-									mInputCaret->moveCaretLeft();
-
-									charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-									// Reverse caret movement if previous char was a newline, and this one is as well
-									// so we don't skip a line
-									if (isNewlineChar(charIdx)) 
-									{
-										mInputCaret->moveCaretRight();
-									}
-								} 
-							}
-							else
-							{
-								// Reverse caret movement if previous char was a newline, and this one is as well
-								// so we don't skip a line
-								if (isNewlineChar(charIdx)) 
-								{
-									mInputCaret->moveCaretRight();
-								}
-							}
-						}
-
-						charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-						mSelectionStart = std::min(mSelectionEnd, charIdx);
-					}
-					else
-					{
-						UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-						if(mInputCaret->getCaretPos() > 0)
-						{
-							mInputCaret->moveCaretLeft();
-							charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-
-							if(!caretMovedDueToNewline) // Only move if we didn't already move due to newline, so we don't move twice
-							{
-								if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right)) 
-									&& mInputCaret->getCaretPos() > 0)
-								{
-									mInputCaret->moveCaretLeft();
-
-									charIdx = getCaretSelectionCharIdx(SelectionDir::Right);
-									// Reverse caret movement if previous char was a newline, and this one is as well
-									// so we don't skip a line
-									if (isNewlineChar(charIdx)) 
-									{
-										mInputCaret->moveCaretRight();
-									}
-								} 
-							}
-							else
-							{
-								// Reverse caret movement if previous char was a newline, and this one is as well
-								// so we don't skip a line
-								if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right))) 
-								{
-									mInputCaret->moveCaretRight();
-								}
-							}
-						}
-
-						charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-						mSelectionEnd = std::max(mSelectionStart, charIdx);
-					}
-
-					if(mSelectionStart == mSelectionEnd)
-						clearSelection();
-
+					moveSelectionLeft(caretMovedDueToNewline);
 					scrollTextToCaret();
 					scrollTextToCaret();
 
 
 					markAsDirty();
 					markAsDirty();
@@ -560,89 +482,7 @@ namespace BansheeEngine
 						showSelection(getCaretSelectionCharIdx(SelectionDir::Left));
 						showSelection(getCaretSelectionCharIdx(SelectionDir::Left));
 					}
 					}
 						
 						
-					if(mSelectionAnchor == mSelectionStart)
-					{
-						UINT32 maxCaretPos = mInputCaret->getMaxCaretPos();
-						UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-
-						if(mInputCaret->getCaretPos() < maxCaretPos)
-						{
-							mInputCaret->moveCaretRight();
-							charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-
-							if(!caretMovedDueToNewline) // Only move if we didn't already move due to newline, so we don't move twice
-							{
-								if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right)) && mInputCaret->getCaretPos() < maxCaretPos)
-								{
-									mInputCaret->moveCaretRight();
-
-									charIdx = getCaretSelectionCharIdx(SelectionDir::Right);
-									// Reverse caret movement if previous char was a newline, and this one is as well
-									// so we don't skip a line
-									if (isNewlineChar(charIdx)) 
-									{
-										mInputCaret->moveCaretLeft();
-									}
-								} 
-							}
-							else
-							{
-								// Reverse caret movement if previous char was a newline, and this one is as well
-								// so we don't skip a line
-								if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right))) 
-								{
-									mInputCaret->moveCaretLeft();
-								}
-							}
-						}
-
-						charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-						mSelectionEnd = std::max(mSelectionStart, charIdx);
-					}
-					else
-					{
-						UINT32 maxCaretPos = mInputCaret->getMaxCaretPos();
-						UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-
-						if(mInputCaret->getCaretPos() < maxCaretPos)
-						{
-							mInputCaret->moveCaretRight();
-							charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-
-							if(!caretMovedDueToNewline) // Only move if we didn't already move due to newline, so we don't move twice
-							{
-							
-								if (isNewlineChar(charIdx) && mInputCaret->getCaretPos() < maxCaretPos)
-								{
-									mInputCaret->moveCaretRight();
-
-									charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-									// Reverse caret movement if previous char was a newline, and this one is as well
-									// so we don't skip a line
-									if (isNewlineChar(charIdx)) 
-									{
-										mInputCaret->moveCaretLeft();
-									}
-								} 
-							}
-							else
-							{
-								// Reverse caret movement if previous char was a newline, and this one is as well
-								// so we don't skip a line
-								if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right))) 
-								{
-									mInputCaret->moveCaretLeft();
-								}
-							}
-						}
-
-						charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
-						mSelectionStart = std::min(mSelectionEnd, charIdx);
-					}
-
-					if(mSelectionStart == mSelectionEnd)
-						clearSelection();
-
+					moveSelectionRight(caretMovedDueToNewline);
 					scrollTextToCaret();
 					scrollTextToCaret();
 
 
 					markAsDirty();
 					markAsDirty();
@@ -861,6 +701,174 @@ namespace BansheeEngine
 		return false;
 		return false;
 	}
 	}
 
 
+	void GUIInputBox::moveSelectionLeft(bool skipNewline) 
+	{
+		if(mSelectionAnchor == mSelectionEnd)
+		{
+			UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+			if(mInputCaret->getCaretPos() > 0)
+			{
+				mInputCaret->moveCaretLeft();
+				charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+
+				if(!skipNewline) // Move one more if we moved to a new line
+				{
+					if (isNewlineChar(charIdx) && mInputCaret->getCaretPos() > 0)
+					{
+						mInputCaret->moveCaretLeft();
+
+						charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+						// Reverse caret movement if previous char was a newline, and this one is as well
+						// so we don't skip a line
+						if (isNewlineChar(charIdx)) 
+						{
+							mInputCaret->moveCaretRight();
+						}
+					} 
+				}
+				else
+				{
+					// Reverse caret movement if previous char was a newline, and this one is as well
+					// so we don't skip a line
+					if (isNewlineChar(charIdx)) 
+					{
+						mInputCaret->moveCaretRight();
+					}
+				}
+			}
+
+			charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+			mSelectionStart = std::min(mSelectionEnd, charIdx);
+		}
+		else
+		{
+			UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+			if(mInputCaret->getCaretPos() > 0)
+			{
+				mInputCaret->moveCaretLeft();
+				charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+
+				if(!skipNewline) // Move one more if we moved to a new line
+				{
+					if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right)) 
+						&& mInputCaret->getCaretPos() > 0)
+					{
+						mInputCaret->moveCaretLeft();
+
+						charIdx = getCaretSelectionCharIdx(SelectionDir::Right);
+						// Reverse caret movement if previous char was a newline, and this one is as well
+						// so we don't skip a line
+						if (isNewlineChar(charIdx)) 
+						{
+							mInputCaret->moveCaretRight();
+						}
+					} 
+				}
+				else
+				{
+					// Reverse caret movement if previous char was a newline, and this one is as well
+					// so we don't skip a line
+					if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right))) 
+					{
+						mInputCaret->moveCaretRight();
+					}
+				}
+			}
+
+			charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+			mSelectionEnd = std::max(mSelectionStart, charIdx);
+		}
+
+		if(mSelectionStart == mSelectionEnd)
+			clearSelection();
+	}
+
+	void GUIInputBox::moveSelectionRight(bool skipNewline) 
+	{
+		if(mSelectionAnchor == mSelectionStart)
+		{
+			UINT32 maxCaretPos = mInputCaret->getMaxCaretPos();
+			UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+
+			if(mInputCaret->getCaretPos() < maxCaretPos)
+			{
+				mInputCaret->moveCaretRight();
+				charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+
+				if(!skipNewline) // Move one more if we moved to a new line
+				{
+					if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right)) && mInputCaret->getCaretPos() < maxCaretPos)
+					{
+						mInputCaret->moveCaretRight();
+
+						charIdx = getCaretSelectionCharIdx(SelectionDir::Right);
+						// Reverse caret movement if previous char was a newline, and this one is as well
+						// so we don't skip a line
+						if (isNewlineChar(charIdx)) 
+						{
+							mInputCaret->moveCaretLeft();
+						}
+					} 
+				}
+				else
+				{
+					// Reverse caret movement if previous char was a newline, and this one is as well
+					// so we don't skip a line
+					if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right))) 
+					{
+						mInputCaret->moveCaretLeft();
+					}
+				}
+			}
+
+			charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+			mSelectionEnd = std::max(mSelectionStart, charIdx);
+		}
+		else
+		{
+			UINT32 maxCaretPos = mInputCaret->getMaxCaretPos();
+			UINT32 charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+
+			if(mInputCaret->getCaretPos() < maxCaretPos)
+			{
+				mInputCaret->moveCaretRight();
+				charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+
+				if(!skipNewline) // Move one more if we moved to a new line
+				{
+
+					if (isNewlineChar(charIdx) && mInputCaret->getCaretPos() < maxCaretPos)
+					{
+						mInputCaret->moveCaretRight();
+
+						charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+						// Reverse caret movement if previous char was a newline, and this one is as well
+						// so we don't skip a line
+						if (isNewlineChar(charIdx)) 
+						{
+							mInputCaret->moveCaretLeft();
+						}
+					} 
+				}
+				else
+				{
+					// Reverse caret movement if previous char was a newline, and this one is as well
+					// so we don't skip a line
+					if (isNewlineChar(getCaretSelectionCharIdx(SelectionDir::Right))) 
+					{
+						mInputCaret->moveCaretLeft();
+					}
+				}
+			}
+
+			charIdx = getCaretSelectionCharIdx(SelectionDir::Left);
+			mSelectionStart = std::min(mSelectionEnd, charIdx);
+		}
+
+		if(mSelectionStart == mSelectionEnd)
+			clearSelection();
+	}
+
 	Vector<Rect>::type GUIInputBox::getSelectionRects() const
 	Vector<Rect>::type GUIInputBox::getSelectionRects() const
 	{
 	{
 		Vector<Rect>::type selectionRects;
 		Vector<Rect>::type selectionRects;