Browse Source

pair symbols tool basic imp

jonyrock 11 years ago
parent
commit
a5384bc07b
3 changed files with 168 additions and 4 deletions
  1. 163 4
      scene/gui/text_edit.cpp
  2. 4 0
      scene/gui/text_edit.h
  3. 1 0
      tools/editor/editor_settings.cpp

+ 163 - 4
scene/gui/text_edit.cpp

@@ -44,8 +44,9 @@
 #include "globals.h"
 #include "message_queue.h"
 
-#define TAB_PIXELS
+#include "tools/editor/editor_settings.h"
 
+#define TAB_PIXELS
 
 static bool _is_text_char(CharType c) {
 
@@ -57,6 +58,42 @@ static bool _is_symbol(CharType c) {
 	return c!='_' && ((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~') || c=='\t');
 }
 
+static bool _is_pair_right_symbol(CharType c) {
+	return
+		c == '"'  ||
+		c == '\'' ||
+		c == ')'  ||
+		c == ']'  ||
+		c == '}';
+}
+
+static bool _is_pair_left_symbol(CharType c) {
+	return
+		c == '"'  ||
+		c == '\'' ||
+		c == '('  ||
+		c == '['  ||
+		c == '{';
+}
+
+static bool _is_pair_symbol(CharType c) {
+	return _is_pair_left_symbol(c) || _is_pair_right_symbol(c);
+}
+
+static CharType _get_right_pair_symbol(CharType c) {
+	if(c == '"')
+		return '"';
+	if(c == '\'')
+		return '\'';
+	if(c == '(')
+		return ')';
+	if(c == '[')
+		return ']';
+	if(c == '{')
+		return '}';
+	return 0;
+}
+
 void TextEdit::Text::set_font(const Ref<Font>& p_font) {
 
 	font=p_font;
@@ -707,6 +744,94 @@ void TextEdit::_notification(int p_what) {
 	}
 }
 
+void TextEdit::_consume_pair_symbol(CharType ch) {
+	
+	int cursor_position_to_move = cursor_get_column() + 1;
+	
+	CharType ch_single[2] = {ch, 0};
+	CharType ch_single_pair[2] = {_get_right_pair_symbol(ch), 0};
+	CharType ch_pair[3] = {ch, _get_right_pair_symbol(ch), 0};
+	
+	printf("Selectin if active, %d\n", is_selection_active());
+	if(is_selection_active()) {	
+		
+		int new_column,new_line;
+		
+		_begin_compex_operation();
+		_insert_text(get_selection_from_line(), get_selection_from_column(),
+					 ch_single,
+					 &new_line, &new_column);
+		
+		int to_col_offset = 0;
+		if(get_selection_from_line() == get_selection_to_line()) 
+			to_col_offset = 1;
+		
+		_insert_text(get_selection_to_line(), 
+					 get_selection_to_column() + to_col_offset,
+					 ch_single_pair,
+					 &new_line,&new_column);
+		_end_compex_operation();
+		
+		cursor_set_line(get_selection_to_line());
+		cursor_set_column(get_selection_to_column() + to_col_offset);
+		
+		deselect();
+		update();
+		return;
+	}
+	
+	if( (ch == '\'' || ch == '"') &&
+		cursor_get_column() > 0 &&
+		_is_text_char(text[cursor.line][cursor_get_column() - 1])
+	) {
+		insert_text_at_cursor(ch_single);
+		cursor_set_column(cursor_position_to_move);
+		return;
+	}
+	
+	if(cursor_get_column() < text[cursor.line].length()) {
+		if(_is_text_char(text[cursor.line][cursor_get_column()])) {
+			insert_text_at_cursor(ch_single);
+			cursor_set_column(cursor_position_to_move);
+			return;
+		}
+		if(	_is_pair_right_symbol(ch) && 
+			text[cursor.line][cursor_get_column()] == ch 
+		) {
+			cursor_set_column(cursor_position_to_move);
+			return;
+		}
+	}
+	
+	
+	insert_text_at_cursor(ch_pair);
+	cursor_set_column(cursor_position_to_move);
+	return;
+	
+}
+
+void TextEdit::_consume_backspace_for_pair_symbol(int prev_line, int prev_column) {
+	
+	bool remove_right_symbol = false;
+	
+	if(cursor.column < text[cursor.line].length() && cursor.column > 0) {
+		
+		CharType left_char = text[cursor.line][cursor.column - 1];
+		CharType right_char = text[cursor.line][cursor.column];
+		
+		if(right_char == _get_right_pair_symbol(left_char)) {
+			remove_right_symbol = true;
+		}
+		
+	}
+	if(remove_right_symbol) {
+		_remove_text(prev_line,prev_column,cursor.line,cursor.column + 1);
+	} else {
+		_remove_text(prev_line,prev_column,cursor.line,cursor.column);
+	}
+	
+}
+
 void TextEdit::backspace_at_cursor() {
 
 	if (cursor.column==0 && cursor.line==0)
@@ -714,7 +839,15 @@ void TextEdit::backspace_at_cursor() {
 
 	int prev_line = cursor.column?cursor.line:cursor.line-1;
 	int prev_column = cursor.column?(cursor.column-1):(text[cursor.line-1].length());
-	_remove_text(prev_line,prev_column,cursor.line,cursor.column);
+	bool pair_symbols_tool = EDITOR_DEF("text_editor/pair_symbols_tool", false);
+	if(pair_symbols_tool && 
+		cursor.column > 0 &&
+		_is_pair_left_symbol(text[cursor.line][cursor.column - 1])) {
+		_consume_backspace_for_pair_symbol(prev_line, prev_column);
+	} else {
+		_remove_text(prev_line,prev_column,cursor.line,cursor.column);
+	}
+
 	cursor_set_line(prev_line);
 	cursor_set_column(prev_column);
 
@@ -1113,7 +1246,9 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
 					default:
 						if (k.unicode>=32 && !k.mod.command && !k.mod.alt && !k.mod.meta)
 							clear=true;
-
+						bool pair_symbols_tool=EDITOR_DEF("text_editor/pair_symbols_tool", false);
+						if (pair_symbols_tool && _is_pair_left_symbol(k.unicode))
+							clear=false;
 				}
 
 				if (unselect) {
@@ -1522,14 +1657,38 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
 					if (readonly)
 						break;
 
+					accept_event();
+				} else {
+
+					break;
+				}
+			}
+			
+			if (!scancode_handled && !k.mod.command && !k.mod.alt) {
+			
+				if (k.unicode>=32) {
+
+					if (readonly)
+						break;
+					
+					
+					
 					const CharType chr[2] = {k.unicode, 0};
-					_insert_text_at_cursor(chr);
+					bool pair_symbols_tool = EDITOR_DEF("text_editor/pair_symbols_tool", false);
+					
+					if(pair_symbols_tool && _is_pair_symbol(chr[0])) {
+						_consume_pair_symbol(chr[0]);
+					} else {
+						_insert_text_at_cursor(chr);
+					}
+					
 					accept_event();
 				} else {
 
 					break;
 				}
 			}
+			
 
 			if (!selection.selecting_test) {
 

+ 4 - 0
scene/gui/text_edit.h

@@ -272,6 +272,10 @@ protected:
 	void _insert_text_at_cursor(const String& p_text);
 	void _input_event(const InputEvent& p_input);
 	void _notification(int p_what);
+	
+	void _consume_pair_symbol(CharType ch);
+	void _consume_backspace_for_pair_symbol(int prev_line, int prev_column);
+	
 	static void _bind_methods();
 
 

+ 1 - 0
tools/editor/editor_settings.cpp

@@ -402,6 +402,7 @@ void EditorSettings::_load_defaults() {
 	set("text_editor/create_signal_callbacks",true);
 	set("text_editor/autosave_interval_seconds",60);
 	set("text_editor/font","");
+	set("text_editor/pair_symbols_tool", false);
 	hints["text_editor/font"]=PropertyInfo(Variant::STRING,"text_editor/font",PROPERTY_HINT_GLOBAL_FILE,"*.fnt");