From 4a5e4346ae5dc471a4fa11d9101013f768bc03f6 Mon Sep 17 00:00:00 2001 From: Kenneth Pouncey Date: Sun, 2 Aug 2015 09:52:38 +0200 Subject: [PATCH] New CCTextField implementation. Implement PlaceHolderText and PlaceHolderTextColor. 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 https://github.com/mono/CocosSharp/issues/55 --- src/text_input_node/CCTextField.cs | 138 +++++- src/text_input_node/MacOSX/IMEKeyboardImpl.cs | 4 +- tests/tests/classes/TestNavigationLayer.cs | 14 +- .../tests/TextInputTest/TextInputTest.cs | 440 ++++++++++++------ .../tests/TextInputTest/TextInputTestScene.cs | 40 +- 5 files changed, 464 insertions(+), 172 deletions(-) diff --git a/src/text_input_node/CCTextField.cs b/src/text_input_node/CCTextField.cs index e02f1f961..0eea730b3 100755 --- a/src/text_input_node/CCTextField.cs +++ b/src/text_input_node/CCTextField.cs @@ -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 + /// + /// Gets or sets a value indicating whether this read only. + /// + /// true if read only; otherwise, false. public bool ReadOnly { get { return readOnly; } @@ -36,6 +42,10 @@ public bool ReadOnly } } + /// + /// Gets or sets a value indicating whether this auto edits. + /// + /// true if auto edit; otherwise, false. public bool AutoEdit { get { return autoEdit; } @@ -46,8 +56,104 @@ public bool AutoEdit } } + /// + /// Gets or sets the place holder text when the Text is empty. + /// + /// The place holder text. + public string PlaceHolderText + { + get { return placeHolderText; } + set + { + if (placeHolderText == value) + return; + + placeHolderText = value; + + if (string.IsNullOrEmpty(Text)) + Text = placeHolderText; + } + } + + /// + /// Gets or sets the color of the place holder text. + /// + /// The color of the place holder text. + 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(); + } + } + } + + /// + /// Gets or sets a value indicating whether this auto repeats. + /// + /// true if auto repeat; otherwise, false. public bool AutoRepeat { get; set; } + /// + /// Gets the character count. + /// + /// The character count. Returns 0 if current text is equal to PlaceHolderText + public int CharacterCount + { + get + { + var currentText = Text; + return currentText == placeHolderText ? 0 : currentText.Length; + } + } + #endregion Properties @@ -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 @@ -160,9 +269,15 @@ public void EndEdit() #endif } + /// + /// Deletes a character from the end of TextField. + /// protected virtual void DeleteBackwards() { var text = Text; + if (text == placeHolderText) + return; + if (string.IsNullOrEmpty(text) || text.Length == 1) { Text = string.Empty; @@ -173,6 +288,11 @@ protected virtual void DeleteBackwards() } } + /// + /// Inserts the text at the end of the TextField + /// + /// Text. + /// Length. protected virtual void InsertText(string text, int len) { @@ -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(); } + /// + /// Replaces the current text of the field + /// + /// Text. + /// Length. protected virtual void ReplaceText(string text, int len) { var insert = new System.Text.StringBuilder(text, len); @@ -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) { @@ -277,7 +405,7 @@ public override void OnExit() CheckTouchState(); if (TextFieldIMEImplementation != null) { - TextFieldIMEImplementation.KeyboardDidHide -= IMEImplementation_KeyboardDidHide; + TextFieldIMEImplementation.KeyboardDidHide -= OnKeyboardDidHide; } } diff --git a/src/text_input_node/MacOSX/IMEKeyboardImpl.cs b/src/text_input_node/MacOSX/IMEKeyboardImpl.cs index 9532c2912..5e93d2f66 100755 --- a/src/text_input_node/MacOSX/IMEKeyboardImpl.cs +++ b/src/text_input_node/MacOSX/IMEKeyboardImpl.cs @@ -242,7 +242,7 @@ void OnKeyPressed(CCEventKeyboard eventKeyboard) } } - CCLog.Log("On Pressed " + lastPressedKey); + //CCLog.Log("On Pressed " + lastPressedKey); if (AutoRepeat && lastPressedKey != CCKeys.None) { StartAutorepeat(); @@ -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 diff --git a/tests/tests/classes/TestNavigationLayer.cs b/tests/tests/classes/TestNavigationLayer.cs index c1e185c6d..656f49c9f 100644 --- a/tests/tests/classes/TestNavigationLayer.cs +++ b/tests/tests/classes/TestNavigationLayer.cs @@ -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); diff --git a/tests/tests/classes/tests/TextInputTest/TextInputTest.cs b/tests/tests/classes/tests/TextInputTest/TextInputTest.cs index f3f3796c5..edd6a2a81 100755 --- a/tests/tests/classes/tests/TextInputTest/TextInputTest.cs +++ b/tests/tests/classes/tests/TextInputTest/TextInputTest.cs @@ -39,7 +39,7 @@ public override string Title { get { - return "text input test"; + return "CCTextField Text Input Test"; } } @@ -47,7 +47,7 @@ public override string Subtitle { get { - return base.Subtitle; + return (notificationLayer != null) ? notificationLayer.Subtitle : string.Empty; } } @@ -55,35 +55,39 @@ public override string Subtitle public class KeyboardNotificationLayer : CCNode { + + CCTextField trackNode; + protected CCPoint beginPosition; + public KeyboardNotificationLayer() { // Register Touch Event var touchListener = new CCEventListenerTouchOneByOne(); touchListener.IsSwallowTouches = true; - touchListener.OnTouchBegan = onTouchBegan; - touchListener.OnTouchEnded = onTouchEnded; + touchListener.OnTouchBegan = OnTouchBegan; + touchListener.OnTouchEnded = OnTouchEnded; AddEventListener(touchListener); } - public virtual string subtitle() + public virtual string Subtitle { - throw new NotFiniteNumberException(); + get { return string.Empty; } } - public virtual void onClickTrackNode(bool bClicked) + public virtual void OnClickTrackNode(bool bClicked) { throw new NotFiniteNumberException(); } - bool onTouchBegan(CCTouch pTouch, CCEvent touchEvent) + bool OnTouchBegan(CCTouch pTouch, CCEvent touchEvent) { - m_beginPos = pTouch.LocationOnScreen; + beginPosition = pTouch.LocationOnScreen; return true; } - void onTouchEnded(CCTouch pTouch, CCEvent touchEvent) + void OnTouchEnded(CCTouch pTouch, CCEvent touchEvent) { if (trackNode == null) { @@ -92,100 +96,140 @@ void onTouchEnded(CCTouch pTouch, CCEvent touchEvent) var endPos = pTouch.LocationOnScreen; - if (trackNode.BoundingBox.ContainsPoint(m_beginPos) && trackNode.BoundingBox.ContainsPoint(endPos)) + if (trackNode.BoundingBox.ContainsPoint(beginPosition) && trackNode.BoundingBox.ContainsPoint(endPos)) { - onClickTrackNode(true); + OnClickTrackNode(true); } else { - onClickTrackNode(false); + OnClickTrackNode(false); } } - protected CCTextField trackNode; - protected CCPoint m_beginPos; - } - - public class TextFieldDefaultTest : KeyboardNotificationLayer - { - public override void onClickTrackNode(bool bClicked) + public override void OnExit() { - if (bClicked && trackNode != null) + base.OnExit(); + if (trackNode != null) { - trackNode.Edit(); + trackNode.EndEdit(); + DetachListeners(); } - else + } + + protected CCTextField TrackNode + { + get { return trackNode; } + set { - if (trackNode != null && trackNode != null) + if (value == null) + { + if (trackNode != null) + { + DetachListeners(); + trackNode = value; + return; + } + } + + if (trackNode != value) { - trackNode.EndEdit(); + DetachListeners(); } + + trackNode = value; + AttachListeners(); } - } - public override void OnEnter() + void AttachListeners () { - base.OnEnter(); - - var s = VisibleBoundsWorldspace.Size; - - var textField = new CCTextField("", - TextInputTestScene.FONT_NAME, - TextInputTestScene.FONT_SIZE, - CCLabelFormat.SpriteFont); - - var imeImplementation = textField.TextFieldIMEImplementation; - imeImplementation.KeyboardDidHide += ImeImplementation_KeyboardDidHide; - imeImplementation.KeyboardDidShow += ImeImplementation_KeyboardDidShow; - imeImplementation.KeyboardWillHide += ImeImplementation_KeyboardWillHide; - imeImplementation.KeyboardWillShow += ImeImplementation_KeyboardWillShow; - - textField.Position = s.Center; - - textField.AutoEdit = true; + // Remember to remove our event listeners. + var imeImplementation = trackNode.TextFieldIMEImplementation; + imeImplementation.KeyboardDidHide += OnKeyboardDidHide; + imeImplementation.KeyboardDidShow += OnKeyboardDidShow; + imeImplementation.KeyboardWillHide += OnKeyboardWillHide; + imeImplementation.KeyboardWillShow += OnKeyboardWillShow; - AddChild(textField); + } - trackNode = textField; + void DetachListeners () + { + if (TrackNode != null) + { + // Remember to remove our event listeners. + var imeImplementation = TrackNode.TextFieldIMEImplementation; + imeImplementation.KeyboardDidHide -= OnKeyboardDidHide; + imeImplementation.KeyboardDidShow -= OnKeyboardDidShow; + imeImplementation.KeyboardWillHide -= OnKeyboardWillHide; + imeImplementation.KeyboardWillShow -= OnKeyboardWillShow; + } } - private void ImeImplementation_KeyboardWillShow(object sender, CCIMEKeyboardNotificationInfo e) + void OnKeyboardWillShow(object sender, CCIMEKeyboardNotificationInfo e) { CCLog.Log("Keyboard will show"); } - private void ImeImplementation_KeyboardWillHide(object sender, CCIMEKeyboardNotificationInfo e) + void OnKeyboardWillHide(object sender, CCIMEKeyboardNotificationInfo e) { CCLog.Log("Keyboard will Hide"); } - private void ImeImplementation_KeyboardDidShow(object sender, CCIMEKeyboardNotificationInfo e) + void OnKeyboardDidShow(object sender, CCIMEKeyboardNotificationInfo e) { CCLog.Log("Keyboard did show"); } - private void ImeImplementation_KeyboardDidHide(object sender, CCIMEKeyboardNotificationInfo e) + void OnKeyboardDidHide(object sender, CCIMEKeyboardNotificationInfo e) { CCLog.Log("Keyboard did hide"); } - public override void OnExit() - { - base.OnExit(); + } - // Remember to remove our event listeners. - var imeImplementation = trackNode.TextFieldIMEImplementation; - imeImplementation.KeyboardDidHide -= ImeImplementation_KeyboardDidHide; - imeImplementation.KeyboardDidShow -= ImeImplementation_KeyboardDidShow; - imeImplementation.KeyboardWillHide -= ImeImplementation_KeyboardWillHide; - imeImplementation.KeyboardWillShow -= ImeImplementation_KeyboardWillShow; + public class TextFieldDefaultTest : KeyboardNotificationLayer + { + public override void OnClickTrackNode(bool bClicked) + { + if (bClicked && TrackNode != null) + { + TrackNode.Edit(); + } + else + { + if (TrackNode != null && TrackNode != null) + { + TrackNode.EndEdit(); + } + } + } + public override void OnEnter() + { + base.OnEnter(); + + var s = VisibleBoundsWorldspace.Size; + + var textField = new CCTextField("", + TextInputTestScene.FONT_NAME, + TextInputTestScene.FONT_SIZE, + CCLabelFormat.SpriteFont); + + textField.Position = s.Center; + + textField.AutoEdit = true; + + AddChild(textField); + + TrackNode = textField; + } - public override string subtitle() + public override string Subtitle { - return "TextField with default behavior test"; + get { + return "TextField with default behavior test"; + } } } @@ -195,31 +239,35 @@ public override string subtitle() public class TextFieldActionTest : KeyboardNotificationLayer { - CCTextField m_pTextField; - CCAction m_pTextFieldAction; - bool m_bAction; - int m_nCharLimit; // the textfield max char limit + CCTextField textField; + CCAction textFieldAction; + CCActionState textFieldActionState; + bool action; + int charLimit; // the textfield max char limit + + const int RANDOM_MAX = 32767; public void callbackRemoveNodeWhenDidAction(CCNode node) { this.RemoveChild(node, true); } - // KeyboardNotificationLayer - public override string subtitle() + public override string Subtitle { - return "CCTextFieldTTF with action and char limit test"; + get { + return "CCTextField with action and char limit test"; + } } - public override void onClickTrackNode(bool bClicked) + public override void OnClickTrackNode(bool bClicked) { if (bClicked) { - trackNode.Edit(); + TrackNode.Edit(); } else { - trackNode.EndEdit(); + TrackNode.EndEdit(); } } @@ -228,78 +276,143 @@ public override void OnEnter() { base.OnEnter(); - m_nCharLimit = 12; + charLimit = 12; - m_pTextFieldAction = new CCRepeatForever( + textFieldAction = new CCRepeatForever( (CCFiniteTimeAction)new CCSequence( new CCFadeOut(0.25f), new CCFadeIn(0.25f))); - m_bAction = false; - - // add CCTextFieldTTF - CCSize s = Layer.VisibleBoundsWorldspace.Size; + action = false; - m_pTextField = new CCTextField("", + textField = new CCTextField("", TextInputTestScene.FONT_NAME, TextInputTestScene.FONT_SIZE, CCLabelFormat.SpriteFont); - AddChild(m_pTextField); - trackNode = m_pTextField; + var imeImplementation = textField.TextFieldIMEImplementation; + imeImplementation.KeyboardDidHide += OnKeyboardDidHide; + imeImplementation.KeyboardDidShow += OnKeyboardDidShow; + imeImplementation.InsertText += OnInsertText; + imeImplementation.DeleteBackward += OnDeleteBackward; + + textField.Position = VisibleBoundsWorldspace.Center; + + AddChild(textField); + + TrackNode = textField; } - // CCTextFieldDelegate - public virtual bool onTextFieldAttachWithIME(CCTextField pSender) + public override void OnExit() { - // if (m_bAction != null) - // { - // m_pTextField.RunAction(m_pTextFieldAction); - // m_bAction = true; - // } - return false; + base.OnExit(); + + // Remember to remove our event listeners. + var imeImplementation = TrackNode.TextFieldIMEImplementation; + imeImplementation.KeyboardDidHide -= OnKeyboardDidHide; + imeImplementation.KeyboardDidShow -= OnKeyboardDidShow; + imeImplementation.InsertText -= OnInsertText; + imeImplementation.DeleteBackward -= OnDeleteBackward; + } - public virtual bool onTextFieldDetachWithIME(CCTextField pSender) + void OnDeleteBackward (object sender, CCIMEKeybardEventArgs e) { - // if (m_bAction != null) - // { - // m_pTextField.StopAction(m_pTextFieldAction); - // m_pTextField.Opacity = 255; - // m_bAction = false; - // } - return false; + var focusedTextField = sender as CCTextField; + + if (focusedTextField == null || string.IsNullOrEmpty(focusedTextField.Text)) + { + e.Cancel = true; + return; + } + + // Just cancel this if we would backspace over the PlaceHolderText as it would just be + // replaced anyway and the Action below should not be executed. + var delText = focusedTextField.Text; + if (delText == focusedTextField.PlaceHolderText) + { + e.Cancel = true; + return; + } + + delText = delText.Substring(delText.Length - 1); + + // create a delete text sprite and do some action + var label = new CCLabel(delText, TextInputTestScene.FONT_NAME, TextInputTestScene.FONT_SIZE + 3, CCLabelFormat.SpriteFont); + this.AddChild(label); + + // move the sprite to fly out + CCPoint beginPos = focusedTextField.Position; + CCSize textfieldSize = focusedTextField.ContentSize; + CCSize labelSize = label.ContentSize; + beginPos.X += (textfieldSize.Width - labelSize.Width) / 2.0f; + + var nextRandom = (float)CCRandom.Next(RANDOM_MAX); + + CCSize winSize = Layer.VisibleBoundsWorldspace.Size; + CCPoint endPos = new CCPoint(-winSize.Width / 4.0f, winSize.Height * (0.5f + nextRandom / (2.0f * RANDOM_MAX))); + + float duration = 1; + float rotateDuration = 0.2f; + int repeatTime = 5; + label.Position = beginPos; + + var delAction = new CCSpawn(new CCMoveTo(duration, endPos), + new CCRepeat( + new CCRotateBy(rotateDuration, (CCRandom.Next() % 2 > 0) ? 360 : -360), + (uint)repeatTime), + new CCFadeOut(duration) + ); + + label.RunActionsAsync(delAction, new CCRemoveSelf(true)); + } - public virtual bool onTextFieldInsertText(CCTextField pSender, string text, int nLen) + void OnInsertText (object sender, CCIMEKeybardEventArgs e) { + var focusedTextField = sender as CCTextField; + + if (focusedTextField == null) + { + e.Cancel = true; + return; + } + + var text = e.Text; + var currentText = focusedTextField.Text; + // if insert enter, treat as default to detach with ime if ("\n" == text) { - return false; + return; } - // if the textfield's char count more than m_nCharLimit, doesn't insert text anymore. - if (pSender.Text.Length >= m_nCharLimit) + // if the textfield's char count is more than charLimit, don't insert text anymore. + if (focusedTextField.CharacterCount >= charLimit) { - return true; + e.Cancel = true; + return; } // create a insert text sprite and do some action var label = new CCLabel(text, TextInputTestScene.FONT_NAME, TextInputTestScene.FONT_SIZE, CCLabelFormat.SpriteFont); this.AddChild(label); - CCColor3B color = new CCColor3B { R = 226, G = 121, B = 7 }; + var color = new CCColor3B { R = 226, G = 121, B = 7 }; label.Color = color; + var inputTextSize = focusedTextField.CharacterCount == 0 ? CCSize.Zero : focusedTextField.ContentSize; + // move the sprite from top to position - CCPoint endPos = pSender.Position; - if (pSender.Text.Length > 0) + var endPos = focusedTextField.Position; + endPos.Y -= inputTextSize.Height; + + if (currentText.Length > 0) { - endPos.X += pSender.ContentSize.Width / 2; + endPos.X += inputTextSize.Width / 2; } - CCSize inputTextSize = label.ContentSize; - CCPoint beginPos = new CCPoint(endPos.X, Layer.VisibleBoundsWorldspace.Size.Height - inputTextSize.Height * 2); - float duration = 0.5f; + var beginPos = new CCPoint(endPos.X, VisibleBoundsWorldspace.Size.Height - inputTextSize.Height * 2); + + var duration = 0.5f; label.Position = beginPos; label.Scale = 8; @@ -308,48 +421,95 @@ public virtual bool onTextFieldInsertText(CCTextField pSender, string text, int new CCMoveTo(duration, endPos), new CCScaleTo(duration, 1), new CCFadeOut(duration)), - new CCCallFuncN(callbackRemoveNodeWhenDidAction)); + new CCRemoveSelf(true)); label.RunAction(seq); - return false; } - public virtual bool onTextFieldDeleteBackward(CCTextFieldTTF pSender, string delText, int nLen) + + void OnKeyboardDidShow(object sender, CCIMEKeyboardNotificationInfo e) { - // create a delete text sprite and do some action - var label = new CCLabel(delText, TextInputTestScene.FONT_NAME, TextInputTestScene.FONT_SIZE, CCLabelFormat.SpriteFont); - this.AddChild(label); + if (!action) + { + textFieldActionState = textField.RunAction(textFieldAction); + action = true; + } + } - // move the sprite to fly out - CCPoint beginPos = pSender.Position; - CCSize textfieldSize = pSender.ContentSize; - CCSize labelSize = label.ContentSize; - beginPos.X += (textfieldSize.Width - labelSize.Width) / 2.0f; + void OnKeyboardDidHide(object sender, CCIMEKeyboardNotificationInfo e) + { + if (action) + { + textField.StopAction(textFieldActionState); + textField.Opacity = 255; + action = false; + } - int RAND_MAX = 32767; - CCRandom rand = new CCRandom(); + } - CCSize winSize = Layer.VisibleBoundsWorldspace.Size; - CCPoint endPos = new CCPoint(-winSize.Width / 4.0f, winSize.Height * (0.5f + (float)CCRandom.Next() / (2.0f * RAND_MAX))); - float duration = 1; - float rotateDuration = 0.2f; - int repeatTime = 5; - label.Position = beginPos; + } - CCAction seq = new CCSequence( - new CCSpawn( - new CCMoveTo(duration, endPos), - new CCRepeat( - new CCRotateBy(rotateDuration, (CCRandom.Next() % 2 > 0) ? 360 : -360), - (uint)repeatTime), - new CCFadeOut(duration)), - new CCCallFuncN(callbackRemoveNodeWhenDidAction)); - label.RunAction(seq); - return false; + public class TextFieldUpperCaseTest : KeyboardNotificationLayer + { + public override void OnClickTrackNode(bool bClicked) + { + if (bClicked && TrackNode != null) + { + TrackNode.Edit(); + } + else + { + if (TrackNode != null && TrackNode != null) + { + TrackNode.EndEdit(); + } + } + + } + + public override void OnEnter() + { + base.OnEnter(); + + var s = VisibleBoundsWorldspace.Size; + + var textField = new CCTextField("", + TextInputTestScene.FONT_NAME, + TextInputTestScene.FONT_SIZE, + CCLabelFormat.SpriteFont); + + var imeImplementation = textField.TextFieldIMEImplementation; + imeImplementation.InsertText += OnInsertText; + textField.Position = s.Center; + + textField.AutoEdit = true; + + AddChild(textField); + + TrackNode = textField; } - public virtual bool onDraw(CCTextField pSender) + public override void OnExit() { - return false; + base.OnExit(); + // Remember to remove our event listeners. + var imeImplementation = TrackNode.TextFieldIMEImplementation; + imeImplementation.InsertText -= OnInsertText; + + } + + void OnInsertText (object sender, CCIMEKeybardEventArgs e) + { + var focusedTextField = sender as CCTextField; + + e.Text = e.Text.ToUpper(); + + } + + public override string Subtitle + { + get { + return "TextField Uppercase test"; + } } } } diff --git a/tests/tests/classes/tests/TextInputTest/TextInputTestScene.cs b/tests/tests/classes/tests/TextInputTest/TextInputTestScene.cs index 4b24d87fe..bbe18fecb 100755 --- a/tests/tests/classes/tests/TextInputTest/TextInputTestScene.cs +++ b/tests/tests/classes/tests/TextInputTest/TextInputTestScene.cs @@ -9,15 +9,18 @@ namespace tests public class TextInputTestScene : TestScene { - int kTextFieldTTFDefaultTest = 0; - int kTextFieldTTFActionTest = 1; - int kTextInputTestsCount = 2; + public static int MAX_TESTS = 0; public static string FONT_NAME = "Thonburi"; public static int FONT_SIZE = 36; public static int testIdx = -1; + public TextInputTestScene() : base() + { + MAX_TESTS = textinputCreateFunctions.Length; + } + public override void runThisTest() { CCLayer pLayer = nextTextInputTest(); @@ -26,24 +29,17 @@ public override void runThisTest() Scene.Director.ReplaceScene(this); } + static Func[] textinputCreateFunctions = + { + () => new TextFieldDefaultTest(), + () => new TextFieldActionTest(), + () => new TextFieldUpperCaseTest(), + + }; + public KeyboardNotificationLayer createTextInputTest(int nIndex) { - switch (nIndex) - { - //case kTextFieldTTFDefaultTest: - // return new TextFieldTTFDefaultTest(); - //case kTextFieldTTFActionTest: - // return new TextFieldTTFActionTest(); - //default: return 0; - - case 0: - return new TextFieldDefaultTest(); - case 1: - return new TextFieldActionTest(); - default: break; - } - - return null; + return textinputCreateFunctions[nIndex](); } protected override void NextTestCase() @@ -61,10 +57,8 @@ protected override void RestTestCase() public CCLayer restartTextInputTest() { TextInputTest pContainerLayer = new TextInputTest(); - //pContainerLayer->autorelease(); KeyboardNotificationLayer pTestLayer = createTextInputTest(testIdx); - //pTestLayer->autorelease(); pContainerLayer.addKeyboardNotificationLayer(pTestLayer); return pContainerLayer; @@ -73,7 +67,7 @@ public CCLayer restartTextInputTest() public CCLayer nextTextInputTest() { testIdx++; - testIdx = testIdx % kTextInputTestsCount; + testIdx = testIdx % MAX_TESTS; return restartTextInputTest(); } @@ -81,7 +75,7 @@ public CCLayer nextTextInputTest() public CCLayer backTextInputTest() { testIdx--; - int total = kTextInputTestsCount; + int total = MAX_TESTS; if (testIdx < 0) testIdx += total;