Allowable characters for GUIEditBox

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Allowable characters for GUIEditBox

Post by Ion Dune »

For one of my projects, I wanted to be able to limit the characters which can be inputted into an EditBox (for example, if its a user name input box, I might want to limit the box to just letters and no spaces, or something to that effect). I made a patch which allows this behavior by keeping an array of characters and checking to see if inputted characters are in this array before placing them in the edit box. I've taken care of input by typing and pasting, which I think covers all the bases. Here's the patch for it (based off rev. 2428):

Code: Select all

Index: include/IGUIEditBox.h
===================================================================
--- include/IGUIEditBox.h	(revision 2428)
+++ include/IGUIEditBox.h	(working copy)
@@ -7,6 +7,7 @@
 
 #include "IGUIElement.h"
 #include "SColor.h"
+#include "irrArray.h"
 
 namespace irr
 {
@@ -90,6 +91,18 @@
 		//! Returns true if the edit box is currently a password box.
 		virtual bool isPasswordBox() const = 0;
 
+		//! Sets whether or not this edit box will use a list of allowed characters when receiving input.
+		virtual void setUseAllowableCharacters(bool useAllowableCharacters, irr::core::array< wchar_t > const & allowedCharacters) = 0;
+
+		//! Sets whether or not this edit box will use a list of allowed characters when receiving input.
+		virtual void setUseAllowableCharacters(bool useAllowableCharacters) = 0;
+
+		//! Returns true if the edit box is currently limiting the characters inputted into this box.
+		virtual bool isUsingAllowableCharacters() const = 0;
+
+		//! Returns the array of characters currently allowed for this edit box.
+		virtual irr::core::array< wchar_t > const & getAllowedCharacters() = 0;
+
 		//! Gets the size area of the text in the edit box
 		/** \return The size in pixels of the text */
 		virtual core::dimension2du getTextDimension() = 0;
Index: source/Irrlicht/CGUIEditBox.cpp
===================================================================
--- source/Irrlicht/CGUIEditBox.cpp	(revision 2428)
+++ source/Irrlicht/CGUIEditBox.cpp	(working copy)
@@ -37,7 +37,7 @@
 	Operator(0), BlinkStartTime(0), CursorPos(0), HScrollPos(0), VScrollPos(0), Max(0),
 	WordWrap(false), MultiLine(false), AutoScroll(true), PasswordBox(false),
 	PasswordChar(L'*'), HAlign(EGUIA_UPPERLEFT), VAlign(EGUIA_CENTER),
-	CurrentTextRect(0,0,1,1), FrameRect(rectangle)
+	CurrentTextRect(0,0,1,1), FrameRect(rectangle), UseAllowableCharacters(false)
 {
 	#ifdef _DEBUG
 	setDebugName("CGUIEditBox");
@@ -185,6 +185,29 @@
 }
 
 
+void CGUIEditBox::setUseAllowableCharacters(bool useAllowableCharacters, irr::core::array< wchar_t > const & allowedCharacters)
+{
+	UseAllowableCharacters=useAllowableCharacters;
+	AllowedCharacters=allowedCharacters;
+}
+
+void CGUIEditBox::setUseAllowableCharacters(bool useAllowableCharacters)
+{
+	UseAllowableCharacters=useAllowableCharacters;
+}
+
+
+bool CGUIEditBox::isUsingAllowableCharacters() const
+{
+	return UseAllowableCharacters;
+}
+
+irr::core::array< wchar_t > const & CGUIEditBox::getAllowedCharacters()
+{
+	return AllowedCharacters;
+}
+
+
 //! Sets text justification
 void CGUIEditBox::setTextAlignment(EGUI_ALIGNMENT horizontal, EGUI_ALIGNMENT vertical)
 {
@@ -311,7 +334,20 @@
 					{
 						// insert text
 						core::stringw s = Text.subString(0, CursorPos);
-						s.append(p);
+						if (UseAllowableCharacters)
+						{
+							core::stringw paste = p;
+							for ( u32 i = 0 ; i < paste.size() ;)
+							{
+								if ( AllowedCharacters.binary_search(paste[i]) == -1 )
+									paste.erase(i);
+								else
+									++i;
+							}
+							s.append(paste);
+						}
+						else
+							s.append(p);
 						s.append( Text.subString(CursorPos, Text.size()-CursorPos) );
 
 						if (!Max || s.size()<=Max) // thx to Fish FH for fix
@@ -326,7 +362,20 @@
 						// replace text
 
 						core::stringw s = Text.subString(0, realmbgn);
-						s.append(p);
+						if (UseAllowableCharacters)
+						{
+							core::stringw paste = p;
+							for ( u32 i = 0 ; i < paste.size() ;)
+							{
+								if ( AllowedCharacters.binary_search(paste[i]) == -1 )
+									paste.erase(i);
+								else
+									++i;
+							}
+							s.append(paste);
+						}
+						else
+							s.append(p);
 						s.append( Text.subString(realmend, Text.size()-realmend) );
 
 						if (!Max || s.size()<=Max)  // thx to Fish FH for fix
@@ -1252,7 +1301,7 @@
 	if (!IsEnabled)
 		return;
 
-	if (c != 0)
+	if (c != 0 && ( ! UseAllowableCharacters || AllowedCharacters.binary_search( c ) != -1 ) )
 	{
 		if (Text.size() < Max || Max == 0)
 		{
Index: source/Irrlicht/CGUIEditBox.h
===================================================================
--- source/Irrlicht/CGUIEditBox.h	(revision 2428)
+++ source/Irrlicht/CGUIEditBox.h	(working copy)
@@ -97,6 +97,18 @@
 		//! Returns true if the edit box is currently a password box.
 		virtual bool isPasswordBox() const;
 
+		//! Sets whether or not this edit box will use a list of allowed characters when receiving input.
+		virtual void setUseAllowableCharacters(bool useAllowableCharacters, irr::core::array< wchar_t > const & allowedCharacters);
+
+		//! Sets whether or not this edit box will use a list of allowed characters when receiving input.
+		virtual void setUseAllowableCharacters(bool useAllowableCharacters);
+
+		//! Returns true if the edit box is currently limiting the characters inputted into this box.
+		virtual bool isUsingAllowableCharacters() const;
+
+		//! Returns the array of characters currently allowed for this edit box.
+		virtual irr::core::array< wchar_t > const & getAllowedCharacters();
+
 		//! Updates the absolute position, splits text if required
 		virtual void updateAbsolutePosition();
 
@@ -141,12 +153,13 @@
 		s32 HScrollPos, VScrollPos; // scroll position in characters
 		u32 Max;
 
-		bool WordWrap, MultiLine, AutoScroll, PasswordBox;
+		bool WordWrap, MultiLine, AutoScroll, PasswordBox, UseAllowableCharacters;
 		wchar_t PasswordChar;
 		EGUI_ALIGNMENT HAlign, VAlign;
 
 		core::array< core::stringw > BrokenText;
 		core::array< s32 > BrokenTextPositions;
+		core::array< wchar_t > AllowedCharacters;
 
 		core::rect<s32> CurrentTextRect, FrameRect; // temporary values
 	};
as an example of usage, I made a patch for the gui example (example 5):

Code: Select all

Index: examples/05.UserInterface/main.cpp
===================================================================
--- examples/05.UserInterface/main.cpp	(revision 2428)
+++ examples/05.UserInterface/main.cpp	(working copy)
@@ -244,6 +244,12 @@
 	env->addStaticText(L"Logging ListBox:", rect<s32>(50,110,250,130), true);
 	IGUIListBox * listbox = env->addListBox(rect<s32>(50, 140, 250, 210));
 	env->addEditBox(L"Editable Text", rect<s32>(350, 80, 550, 100));
+	IGUIEditBox * editbox = env->addEditBox(L"Editable Text", rect<s32>(350, 110, 550, 130));
+	wchar_t * warray = L"abc123";
+	irr::core::array< wchar_t > limitchars;
+	limitchars.set_pointer(warray, 6);
+	limitchars.set_free_when_destroyed(false);
+	editbox->setUseAllowableCharacters(true , limitchars);
 
 	// Store the appropriate data in a context structure.
 	SAppContext context;
I found this to be very useful, and have submitted it to the svn here:

https://sourceforge.net/tracker/?func=d ... tid=540678
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post by Ulf »

I only skimmed the code quickly, but, just a quick question:
Ion Dune wrote: For one of my projects, I wanted to be able to limit the characters which can be inputted into an EditBox
Your code isn't actually limiting the input, it's just removing the unwanted characters from the string after it has been input, isn't it?

Not really much use like that.
*EDIT* Not in general anyway.
I can hear birds chirping
:twisted:

I live in the Eye of Insanity.
Post Reply