Просмотр исходного кода

Added UITextInput::shiftText(), which allows blocks of text to be shifted left and right

Ethan M 12 лет назад
Родитель
Сommit
2effa2380f

+ 7 - 0
Modules/Contents/UI/Include/PolyUITextInput.h

@@ -154,6 +154,10 @@ namespace Polycode {
 			UIScrollContainer *getScrollContainer();
 			
 			bool useStrongHinting;
+        
+            void shiftText(bool left=false);
+            void convertIndentToTabs();
+            void convertIndentToSpaces();
 		
 		protected:
 		
@@ -263,6 +267,9 @@ namespace Polycode {
 			vector<ScreenLabel*> numberLines;
 			
 			Core *core;
+        
+			enum indentTypes { INDENT_SPACE, INDENT_TAB } indentType;
+			int indentSpacing;
 			
 	};
 }

+ 105 - 18
Modules/Contents/UI/Source/PolyUITextInput.cpp

@@ -193,7 +193,10 @@ UITextInput::UITextInput(bool multiLine, Number width, Number height) : UIElemen
 	core->addEventListener(this, Core::EVENT_CUT);
 	core->addEventListener(this, Core::EVENT_UNDO);
 	core->addEventListener(this, Core::EVENT_REDO);
-	core->addEventListener(this, Core::EVENT_SELECT_ALL);	
+	core->addEventListener(this, Core::EVENT_SELECT_ALL);
+	
+	indentSpacing = 4;
+	indentType = INDENT_TAB;
 }
 
 void UITextInput::checkBufferLines() {
@@ -1088,7 +1091,7 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
 		
 //	Logger::log("UCHAR: %d\n", charCode);	
 	
-	CoreInput *input = CoreServices::getInstance()->getCore()->getInput();	
+	CoreInput *input = CoreServices::getInstance()->getCore()->getInput();
 	
 	if(key == KEY_LEFT) {
 		if(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER)) {
@@ -1297,22 +1300,35 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
 	
 	bool _changedText = false;
 		
-	if((charCode > 31 && charCode < 127) || charCode > 127) {	
-		if(!isNumberOnly || (isNumberOnly && ((charCode > 47 && charCode < 58) || (charCode == '.' || charCode == '-')))) {
-			if(!isNumberOrCharacter(charCode)) { 
-				saveUndoState();
-			} else if (!isTypingWord) {
-				saveUndoState();
-				isTypingWord = 1;
+	if((charCode > 31 && charCode < 127) || charCode > 127) {
+		
+		// indent/shift text
+		if (multiLine && (key == KEY_LEFTBRACKET || key == KEY_RIGHTBRACKET) &&
+			(input->getKeyState(KEY_LSUPER) || input->getKeyState(KEY_RSUPER) ||
+				input->getKeyState(KEY_LCTRL) || input->getKeyState(KEY_RCTRL))) {
+					shiftText( (key == KEY_RIGHTBRACKET) ? false : true );
+					return;
+		}
+		
+		else {
+		
+			if(!isNumberOnly || (isNumberOnly && ((charCode > 47 && charCode < 58) || (charCode == '.' || charCode == '-')))) {
+				if(!isNumberOrCharacter(charCode)) { 
+					saveUndoState();
+				} else if (!isTypingWord) {
+					saveUndoState();
+					isTypingWord = 1;
+				}
+				if(hasSelection)
+					deleteSelection();
+				ctext = lines[lineOffset];
+				String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
+				ctext = ctext.substr(0,caretPosition);
+				ctext += charCode + text2;
+				caretPosition++;
+				_changedText = true;
 			}
-			if(hasSelection)
-				deleteSelection();
-			ctext = lines[lineOffset];
-			String text2 = ctext.substr(caretPosition, ctext.length()-caretPosition);
-			ctext = ctext.substr(0,caretPosition);
-			ctext += charCode + text2;
-			caretPosition++;
-			_changedText = true;
+			
 		}
 	}
 	
@@ -1519,4 +1535,75 @@ void UITextInput::handleEvent(Event *event) {
 		}
 	}
 	
-}
+}
+
+void UITextInput::shiftText(bool left) {
+	if (multiLine && (hasSelection || lines[lineOffset] != "")) {
+		saveUndoState();
+		
+		String t = (wchar_t)'\t';
+		
+		if (hasSelection) {
+			for (int i = selectionTop; i <= selectionBottom; i++) {
+				if (i == selectionBottom && selectionCaretPosition <= 0)
+					// at least one character of bottom line needs to be selected before indenting, so...
+					break;
+				if (indentType == INDENT_TAB) {
+					if (left) {
+						if (lines[i].substr(0,1) == t) {
+							lines[i] = lines[i].substr(1, lines[i].length()-1);
+							caretPosition--;
+						}
+					} else {
+						lines[i] = t + lines[i];
+						caretPosition++;
+					}
+				} else if (indentType == INDENT_SPACE) {
+					if (left) {
+						// TODO
+					} else {
+						// TODO
+					}
+				}
+			}
+		}
+		else {
+			if (indentType == INDENT_TAB) {
+				if (left) {
+					if (lines[lineOffset].substr(0,1) == t) {
+						lines[lineOffset] = lines[lineOffset].substr(1, lines[lineOffset].length()-1);
+						caretPosition--;
+					}
+				} else {
+					lines[lineOffset] = t + lines[lineOffset];
+					caretPosition++;
+				}
+			} else if (indentType == INDENT_SPACE) {
+				if (left) {
+					// TODO
+				} else {
+					// TODO
+				}
+			}
+		}
+		
+		changedText();
+		updateCaretPosition();
+	}
+}
+
+void UITextInput::convertIndentToSpaces() {
+	if (indentType == INDENT_TAB) {
+		indentType = INDENT_SPACE;
+		
+		//TODO
+	}
+}
+
+void UITextInput::convertIndentToTabs() {
+	if (indentType == INDENT_SPACE) {
+		indentType = INDENT_TAB;
+		
+		//TODO
+	}
+}