Browse Source

Change textinput event, it now takes an UTF-8 encoded string as its parameter with the key 'text'.

Michael Ragazzon 6 years ago
parent
commit
acad54bfc2
4 changed files with 21 additions and 24 deletions
  1. 1 1
      Include/RmlUi/Core/Context.h
  2. 10 4
      Source/Controls/WidgetTextInput.cpp
  3. 9 18
      Source/Core/Context.cpp
  4. 1 1
      readme.md

+ 1 - 1
Include/RmlUi/Core/Context.h

@@ -179,7 +179,7 @@ public:
 	/// Sends a single ascii character as text input into this context.
 	bool ProcessTextInput(char character);
 	/// Sends a string of text as text input into this context.
-	/// @param[in] string The UTF8 string to send into this context.
+	/// @param[in] string The UTF-8 string to send into this context.
 	/// @return True if the event was not consumed (ie, was prevented from propagating by an element), false if it was.
 	bool ProcessTextInput(const String& string);
 

+ 10 - 4
Source/Controls/WidgetTextInput.cpp

@@ -378,8 +378,8 @@ void WidgetTextInput::ProcessEvent(Core::Event& event)
 				// @performance: Can be made heaps faster.
 				for (auto it = Core::StringIteratorU8(clipboard_text); it; ++it)
 				{
-					Core::Character code = *it;
-					AddCharacter(code);
+					Core::Character character = *it;
+					AddCharacter(character);
 				}
 			}
 		}
@@ -404,8 +404,14 @@ void WidgetTextInput::ProcessEvent(Core::Event& event)
 			event.GetParameter< int >("alt_key", 0) == 0 &&
 			event.GetParameter< int >("meta_key", 0) == 0)
 		{
-			Rml::Core::Character character = event.GetParameter("data", Rml::Core::Character::Null);
-			AddCharacter(character);
+			Core::String text = event.GetParameter("text", Core::String{});
+
+			// @performance: Can be made heaps faster.
+			for (auto it = Core::StringIteratorU8(text); it; ++it)
+			{
+				Core::Character character = *it;
+				AddCharacter(character);
+			}
 		}
 
 		ShowCursor(true);

+ 9 - 18
Source/Core/Context.cpp

@@ -500,6 +500,9 @@ bool Context::ProcessKeyUp(Input::KeyIdentifier key_identifier, int key_modifier
 
 bool Context::ProcessTextInput(char character)
 {
+	// Only the standard ASCII character set is a valid subset of UTF-8.
+	if (character < 0 || character > 127)
+		return false;
 	return ProcessTextInput(static_cast<Character>(character));
 }
 
@@ -507,31 +510,19 @@ bool Context::ProcessTextInput(char character)
 bool Context::ProcessTextInput(Character character)
 {
 	// Generate the parameters for the key event.
-	Dictionary parameters;
-	parameters["data"] = character;
-
-	if (focus)
-		return focus->DispatchEvent(EventId::Textinput, parameters);
-	else
-		return root->DispatchEvent(EventId::Textinput, parameters);
+	String text = StringUtilities::ToUTF8(character);
+	return ProcessTextInput(text);
 }
 
 // Sends a string of text as text input into RmlUi.
 bool Context::ProcessTextInput(const String& string)
 {
-	bool consumed = true;
+	Element* target = (focus ? focus : root.get());
 
-	for (size_t i = 0; i < string.size(); ++i)
-	{
-		// Generate the parameters for the key event.
-		Dictionary parameters;
-		parameters["data"] = string[i];
+	Dictionary parameters;
+	parameters["text"] = string;
 
-		if (focus)
-			consumed = focus->DispatchEvent(EventId::Textinput, parameters) && consumed;
-		else
-			consumed = root->DispatchEvent(EventId::Textinput, parameters) && consumed;
-	}
+	bool consumed = target->DispatchEvent(EventId::Textinput, parameters);
 
 	return consumed;
 }

+ 1 - 1
readme.md

@@ -435,7 +435,7 @@ Various changes:
 - `Element::DispatchEvent` can now optionally take an `EventId` instead of a `String`.
 - The `resize` event now only applies to the document size, not individual elements.
 - The `scrollchange` event has been replaced by a function call. To capture scroll changes, instead use the `scroll` event.
-
+- The `textinput` event now sends a `String` in UTF-8 instead of a UCS-2 character, possibly with multiple characters. The parameter key name is changed from "data" to "text".
 
 
 ## RmlUi 2.0 features