Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
New CCTextField implementation. Implement PlaceHolderText and PlaceHo…
Browse files Browse the repository at this point in the history
…lderTextColor.

Fix TextFieldActionTest to work with CCTextField implementation
Add new TextFieldUppercaseTest to show how to implement overriding field implementation.  This will change all all characters to uppercase.

See Issue #55
  • Loading branch information
kjpou1 committed Aug 2, 2015
1 parent ad57fd5 commit 4a5e434
Show file tree
Hide file tree
Showing 5 changed files with 464 additions and 172 deletions.
138 changes: 133 additions & 5 deletions src/text_input_node/CCTextField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ public class CCTextField : CCLabel
bool autoEdit;
bool touchHandled;



public ICCIMEDelegate TextFieldIMEImplementation { get; set; }
string placeHolderText = string.Empty;

CCColor4B defaultTextColor = CCColor4B.White;
CCColor4B placeholderTextColor = CCColor4B.Gray;

public event CCTextFieldDelegate BeginEditing;
public event CCTextFieldDelegate EndEditing;

#region Properties

/// <summary>
/// Gets or sets a value indicating whether this <see cref="CocosSharp.CCTextField"/> read only.
/// </summary>
/// <value><c>true</c> if read only; otherwise, <c>false</c>.</value>
public bool ReadOnly
{
get { return readOnly; }
Expand All @@ -36,6 +42,10 @@ public bool ReadOnly
}
}

/// <summary>
/// Gets or sets a value indicating whether this <see cref="CocosSharp.CCTextField"/> auto edits.
/// </summary>
/// <value><c>true</c> if auto edit; otherwise, <c>false</c>.</value>
public bool AutoEdit
{
get { return autoEdit; }
Expand All @@ -46,8 +56,104 @@ public bool AutoEdit
}
}

/// <summary>
/// Gets or sets the place holder text when the Text is empty.
/// </summary>
/// <value>The place holder text.</value>
public string PlaceHolderText
{
get { return placeHolderText; }
set
{
if (placeHolderText == value)
return;

placeHolderText = value;

if (string.IsNullOrEmpty(Text))
Text = placeHolderText;
}
}

/// <summary>
/// Gets or sets the color of the place holder text.
/// </summary>
/// <value>The color of the place holder text.</value>
public CCColor4B PlaceHolderTextColor
{
get { return placeholderTextColor; }
set {
placeholderTextColor = value;
updateColors();
}
}

void updateColors()
{
if (Text == placeHolderText)
{
if (Color.R != PlaceHolderTextColor.R ||
Color.G != PlaceHolderTextColor.G ||
Color.B != PlaceHolderTextColor.B ||
Opacity != PlaceHolderTextColor.A)
{
Color = new CCColor3B(placeholderTextColor);
Opacity = placeholderTextColor.A;
}
}
else
{
if (Color.R != defaultTextColor.R ||
Color.G != defaultTextColor.G ||
Color.B != defaultTextColor.B ||
Opacity != defaultTextColor.A)
{
Color = new CCColor3B(defaultTextColor);
Opacity = defaultTextColor.A;
}
}
}

public override string Text
{
get
{
return base.Text;
}
set
{
if (string.IsNullOrEmpty(value))
{
base.Text = placeHolderText;
updateColors();
}
else
{
base.Text = value;
updateColors();
}
}
}

/// <summary>
/// Gets or sets a value indicating whether this <see cref="CocosSharp.CCTextField"/> auto repeats.
/// </summary>
/// <value><c>true</c> if auto repeat; otherwise, <c>false</c>.</value>
public bool AutoRepeat { get; set; }

/// <summary>
/// Gets the character count.
/// </summary>
/// <value>The character count. Returns 0 if current text is equal to PlaceHolderText</value>
public int CharacterCount
{
get
{
var currentText = Text;
return currentText == placeHolderText ? 0 : currentText.Length;
}
}

#endregion Properties


Expand Down Expand Up @@ -84,7 +190,10 @@ public CCTextField(string text, string fontName, float fontSize, CCSize dimensio
this.HorizontalAlignment = hAlignment;
this.VerticalAlignment = vAlignment;
AutoRepeat = true;
placeHolderText = text;
updateColors();
TextFieldIMEImplementation = IMEKeyboardImpl.SharedInstance;

}

#endregion Constructors
Expand Down Expand Up @@ -160,9 +269,15 @@ public void EndEdit()
#endif
}

/// <summary>
/// Deletes a character from the end of TextField.
/// </summary>
protected virtual void DeleteBackwards()
{
var text = Text;
if (text == placeHolderText)
return;

if (string.IsNullOrEmpty(text) || text.Length == 1)
{
Text = string.Empty;
Expand All @@ -173,6 +288,11 @@ protected virtual void DeleteBackwards()
}
}

/// <summary>
/// Inserts the text at the end of the TextField
/// </summary>
/// <param name="text">Text.</param>
/// <param name="len">Length.</param>
protected virtual void InsertText(string text, int len)
{

Expand All @@ -188,13 +308,21 @@ protected virtual void InsertText(string text, int len)

if (len > 0)
{
Text += insert.ToString();
if (Text == placeHolderText)
Text = text;
else
Text += insert.ToString();
return;
}

EndEdit();
}

/// <summary>
/// Replaces the current text of the field
/// </summary>
/// <param name="text">Text.</param>
/// <param name="len">Length.</param>
protected virtual void ReplaceText(string text, int len)
{
var insert = new System.Text.StringBuilder(text, len);
Expand Down Expand Up @@ -244,7 +372,7 @@ public override void OnEnter()
CheckTouchState();
}

private void IMEImplementation_KeyboardDidHide(object sender, CCIMEKeyboardNotificationInfo e)
void OnKeyboardDidHide(object sender, CCIMEKeyboardNotificationInfo e)
{
if (TextFieldIMEImplementation != null)
{
Expand Down Expand Up @@ -277,7 +405,7 @@ public override void OnExit()
CheckTouchState();
if (TextFieldIMEImplementation != null)
{
TextFieldIMEImplementation.KeyboardDidHide -= IMEImplementation_KeyboardDidHide;
TextFieldIMEImplementation.KeyboardDidHide -= OnKeyboardDidHide;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/text_input_node/MacOSX/IMEKeyboardImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ void OnKeyPressed(CCEventKeyboard eventKeyboard)
}
}

CCLog.Log("On Pressed " + lastPressedKey);
//CCLog.Log("On Pressed " + lastPressedKey);
if (AutoRepeat && lastPressedKey != CCKeys.None)
{
StartAutorepeat();
Expand All @@ -251,7 +251,7 @@ void OnKeyPressed(CCEventKeyboard eventKeyboard)

void OnKeyReleased(CCEventKeyboard eventKeyboard)
{
CCLog.Log("On Released " + eventKeyboard.Keys);
//CCLog.Log("On Released " + eventKeyboard.Keys);
if (eventKeyboard.Keys == CCKeys.Back)
OnDeleteBackward();
else
Expand Down
14 changes: 12 additions & 2 deletions tests/tests/classes/TestNavigationLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,18 @@ public override void OnEnter()

TitleLabel.Position = new CCPoint(visibleRect.Center.X, visibleRect.Top().Y - 30);

if (!string.IsNullOrEmpty(Subtitle))
SubtitleLabel.Text = Subtitle;
string subtitleStr = Subtitle;

if (!string.IsNullOrEmpty(subtitleStr) && SubtitleLabel == null)
{
SubtitleLabel = new CCLabel(subtitleStr, "arial", 16, CCLabelFormat.SpriteFont);
SubtitleLabel.AnchorPoint = CCPoint.AnchorMiddleTop;
SubtitleLabel.HorizontalAlignment = CCTextAlignment.Center;
AddChild(SubtitleLabel, TestScene.TITLE_LEVEL);
}
else
if (!string.IsNullOrEmpty(Subtitle))
SubtitleLabel.Text = Subtitle;

if(SubtitleLabel != null)
SubtitleLabel.Position = new CCPoint(visibleRect.Center.X, visibleRect.Top().Y - 60);
Expand Down
Loading

0 comments on commit 4a5e434

Please sign in to comment.