Example #1
0
        public void TestBasicInvalidations()
        {
            var services = new ServiceRegistry();
            services.AddService(typeof(IGame), new Game());

            var edit = new EditText();
            edit.UIElementServices = new UIElementServices { Services = services };

            // - test the properties that are supposed to invalidate the object measurement
            UIElementLayeringTests.TestMeasureInvalidation(edit, () => edit.Font = null);
            UIElementLayeringTests.TestMeasureInvalidation(edit, () => edit.MaxLines = 34);
            UIElementLayeringTests.TestMeasureInvalidation(edit, () => edit.MinLines = 34);
            UIElementLayeringTests.TestMeasureInvalidation(edit, () => edit.SelectedText = "toto");
            UIElementLayeringTests.TestMeasureInvalidation(edit, () => edit.Text = "titi");
            UIElementLayeringTests.TestMeasureInvalidation(edit, () => edit.MaxLength = 3); // text is modified

            // - test the properties that are not supposed to invalidate the object layout state
            UIElementLayeringTests.TestNoInvalidation(edit, () => edit.IsReadOnly = !edit.IsReadOnly);
            UIElementLayeringTests.TestNoInvalidation(edit, () => edit.ActiveImage = (SpriteFromTexture)new Sprite());
            UIElementLayeringTests.TestNoInvalidation(edit, () => edit.InactiveImage = (SpriteFromTexture)new Sprite());
            UIElementLayeringTests.TestNoInvalidation(edit, () => edit.TextColor = new Color(1, 2, 3, 4));
            UIElementLayeringTests.TestNoInvalidation(edit, () => edit.SelectionColor = new Color(1, 2, 3, 4));
            UIElementLayeringTests.TestNoInvalidation(edit, () => edit.CaretColor = new Color(1, 7, 3, 4));
            UIElementLayeringTests.TestNoInvalidation(edit, () => edit.CaretPosition = 34);
            UIElementLayeringTests.TestNoInvalidation(edit, () => edit.SelectionLength = 2);
            UIElementLayeringTests.TestNoInvalidation(edit, () => edit.SelectionStart = 0);
            UIElementLayeringTests.TestNoInvalidation(edit, () => edit.MaxLength = 34); // text is not modified
        }
        private void ActivateEditTextImpl()
        {
            var pane = Windows.UI.ViewManagement.InputPane.GetForCurrentView();
            if (pane.TryShow())
            {
                var game = GetGame();
                if (game == null)
                    throw new ArgumentException("Provided services need to contain a provider for the IGame interface.");

                Debug.Assert(game.Context is GameContextUWP, "There is only one possible descendant of GameContext for Windows Store.");

                gameContext = (GameContextUWP)game.Context;
                var swapChainPanel = gameContext.Control;

                // Detach previous EditText (if any)
                if (activeEditText != null)
                    activeEditText.IsSelectionActive = false;
                activeEditText = this;

                // Make sure it doesn't have a parent (another text box being edited)
                editText = gameContext.EditTextBox;
                editText.Text = text;
                swapChainPanel.Children.Add(new Windows.UI.Xaml.Controls.StackPanel { Children = { editText } });

                editText.TextChanged += EditText_TextChanged;
                editText.KeyDown += EditText_KeyDown;

                // Focus
                editText.Focus(Windows.UI.Xaml.FocusState.Programmatic);
            }
        }
        protected override async Task LoadContent()
        {
            await base.LoadContent();

            var background = new Entity { new BackgroundComponent { Texture = Content.Load<Texture>("XenkoBackground") } };
            Scene.Entities.Add(background);

            var text1 = new TextBlock { Text = "text block button 1", Font = Content.Load<SpriteFont>("CourierNew12"), SynchronousCharacterGeneration = true };
            ApplyTextBlockDefaultStyle(text1);
            button1 = new Button { Content = text1 };
            ApplyButtonDefaultStyle(button1);
            button1.SetCanvasRelativePosition(new Vector3(0.025f, 0.05f, 0f));

            edit1 = new EditText() { Text = "Edit text 1", Font = Content.Load<SpriteFont>("CourierNew12"), SynchronousCharacterGeneration = true, };
            ApplyEditTextDefaultStyle(edit1);
            edit1.SetCanvasRelativePosition(new Vector3(0.025f, 0.15f, 0f));

            var text2 = new TextBlock { Text = "text block button 2", Font = Content.Load<SpriteFont>("MicrosoftSansSerif15"), SynchronousCharacterGeneration = true };
            ApplyTextBlockDefaultStyle(text2);
            button2 = new Button { Content = text2 };
            ApplyButtonDefaultStyle(button2);
            edit2 = new EditText() { Text = "Edit text 2", Font = Content.Load<SpriteFont>("MicrosoftSansSerif15"), };
            ApplyEditTextDefaultStyle(edit2);

            stackPanel = new StackPanel
            {
                Children = { button2, edit2 }, 
                HorizontalAlignment = HorizontalAlignment.Center, 
                VerticalAlignment = VerticalAlignment.Center, 
                Orientation = Orientation.Horizontal
            };
            stackPanel.SetCanvasRelativePosition(new Vector3(0.5f, 0.5f, 0f));
            stackPanel.SetCanvasPinOrigin(new Vector3(.5f));

            canvas = new Canvas { Children = {button1, edit1, stackPanel}, CanBeHitByUser = true };

            button1.MouseOverStateChanged += (sender, args) => { triggeredButton1 = true; oldValueButton1 = args.OldValue; newValueButton1 = args.NewValue;};
            button2.MouseOverStateChanged += (sender, args) => { triggeredButton2 = true;};
            edit1.MouseOverStateChanged += (sender, args) => { triggeredEdit1 = true;};
            edit2.MouseOverStateChanged += (sender, args) => { triggeredEdit2 = true;};
            canvas.MouseOverStateChanged += (sender, args) => { triggeredCanvas = true;};
            stackPanel.MouseOverStateChanged += (sender, args) => { triggeredStackPanel = true;};

            canvas.UIElementServices = new UIElementServices { Services = this.Services };

            UIComponent.Page = new Engine.UIPage { RootElement = canvas };
        }
Example #4
0
 private void DeactivateEditTextImpl()
 {
     attachedTextField.EditingChanged -= TextFieldOnValueChanged;
     attachedTextField.ShouldChangeCharacters -= ShouldChangeCharacters;
     attachedTextField.SecureTextEntry = false;
     attachedTextField.ResignFirstResponder();
     attachedTextField = null;
     currentActiveEditText = null;
 }
Example #5
0
        private void ActivateEditTextImpl()
        {
            currentActiveEditText = this;
            attachedTextField = textField;

            UpdateInputTypeImpl();
            attachedTextField.Text = text;
            attachedTextField.EditingChanged += TextFieldOnValueChanged;
            attachedTextField.ShouldChangeCharacters += ShouldChangeCharacters;
            attachedTextField.BecomeFirstResponder();
        }
        private void DeactivateEditTextImpl()
        {
            if (activeEditText == null)
                throw new Exception("Internal error: Can not deactivate the EditText, it is already nullified");

            // remove callbacks
            editText.EditorAction -= AndroidEditTextOnEditorAction;
            editText.AfterTextChanged -= AndroidEditTextOnAfterTextChanged;

            editText.ClearFocus();

            editText = null;
            activeEditText = null;

            // remove the edit text from the layout and hide the layout
            GetGameContext().EditTextLayout.Visibility = ViewStates.Gone;

            // deactivate the ime (hide the keyboard)
            if (staticEditText != null) // staticEditText can be null if window have already been detached.
                inputMethodManager.HideSoftInputFromWindow(staticEditText.WindowToken, HideSoftInputFlags.None);
            inputMethodManager = null;

            FocusedElement = null;
        }
        private void ActivateEditTextImpl()
        {
            if(activeEditText != null)
                throw new Exception("Internal error: Can not activate edit text, another edit text is already active");

            EnsureStaticEditText();

            activeEditText = this;
            editText = staticEditText;
            
            // set up the initial state of the android EditText
            UpdateInputTypeImpl();

            editText.SetMaxLines(MaxLines);
            editText.SetMinLines(MinLines);

            UpdateTextToEditImpl();
            UpdateSelectionToEditImpl();

            // add callbacks
            editText.EditorAction += AndroidEditTextOnEditorAction;
            editText.AfterTextChanged += AndroidEditTextOnAfterTextChanged;

            // add the edit to the overlay layout and show the layout
            GetGameContext().EditTextLayout.Visibility = ViewStates.Visible;

            // set the focus to the edit box
            editText.RequestFocus();

            // activate the ime (show the keyboard)
            inputMethodManager = (InputMethodManager)PlatformAndroid.Context.GetSystemService(Context.InputMethodService);
            inputMethodManager.ShowSoftInput(staticEditText, ShowFlags.Forced);
        }
        private void DeactivateEditTextImpl()
        {
            if (editText != null)
            {
                // Remove text box
                editText.TextChanged -= EditText_TextChanged;
                editText.KeyDown -= EditText_KeyDown;
                var stackPanel = (Windows.UI.Xaml.Controls.Panel)editText.Parent;
                stackPanel.Children.Remove(editText);
                gameContext.Control.Children.Remove(stackPanel);

                editText = null;
                activeEditText = null;
            }

            FocusedElement = null;
        }
Example #9
0
        protected override async Task LoadContent()
        {
            await base.LoadContent();

            var middleOfScreen = new Vector3(UIComponent.VirtualResolution.X, UIComponent.VirtualResolution.Y, 0) / 2;

            edit1 = new EditText(Services)
            {
                Name = "TestEdit1",
                Font = Asset.Load<SpriteFont>("MSMincho10"),
                MinimumWidth = 100,
                Text = "Sample Text1",
                MaxLength = 35,
                TextSize = 20,
                SynchronousCharacterGeneration = true
            };
            edit1.DependencyProperties.Set(Canvas.PinOriginPropertyKey, 0.5f * Vector3.One);
            edit1.DependencyProperties.Set(Canvas.AbsolutePositionPropertyKey, new Vector3(middleOfScreen.X, 100, 0));
            edit1.TextChanged += Edit1OnTextChanged;

            edit2 = new EditText(Services)
            {
                Name = "TestEdit2",
                Font = Asset.Load<SpriteFont>("MicrosoftSansSerif15"),
                MinimumWidth = 100,
                Text = "Sample2 Text2",
                MaxLength = 10,
                CharacterFilterPredicate = IsLetter,
                SynchronousCharacterGeneration = true
            };
            edit2.DependencyProperties.Set(Canvas.PinOriginPropertyKey, 0.5f * Vector3.One);
            edit2.DependencyProperties.Set(Canvas.AbsolutePositionPropertyKey, new Vector3(middleOfScreen.X, 200, 0));
            edit2.TextChanged += Edit2OnTextChanged;

            edit3 = new EditText(Services)
            {
                Name = "TestEdit3",
                Font = Asset.Load<SpriteFont>("MSMincho10"),
                MinimumWidth = 100,
                Text = "secret",
                MaxLength = 15,
                TextSize = 24,
                InputType = EditText.InputTypeFlags.Password,
                SynchronousCharacterGeneration = true
            };
            edit3.DependencyProperties.Set(Canvas.PinOriginPropertyKey, 0.5f * Vector3.One);
            edit3.DependencyProperties.Set(Canvas.AbsolutePositionPropertyKey, new Vector3(middleOfScreen.X, 300, 0));

            edit4 = new EditText(Services)
            {
                Name = "TestEdit4",
                Font = Asset.Load<SpriteFont>("MicrosoftSansSerif15"),
                MinimumWidth = 200,
                Text = "aligned text",
                TextSize = 24,
                SynchronousCharacterGeneration = true
            };
            edit4.DependencyProperties.Set(Canvas.PinOriginPropertyKey, 0.5f * Vector3.One);
            edit4.DependencyProperties.Set(Canvas.AbsolutePositionPropertyKey, new Vector3(middleOfScreen.X, 400, 0));

            var canvas = new Canvas();
            canvas.Children.Add(edit1);
            canvas.Children.Add(edit2);
            canvas.Children.Add(edit3);
            canvas.Children.Add(edit4);

            UIComponent.RootElement = canvas;
        }
        protected void ApplyEditTextDefaultStyle(EditText editText)
        {
            if (editText == null) throw new ArgumentNullException(nameof(editText));

            editText.TextColor = editTextTextColor;
            editText.SelectionColor = editTextSelectionColor;
            editText.CaretColor = editTextCaretColor;
            editText.ActiveImage = editTextActiveImage;
            editText.InactiveImage = editTextInactiveImage;
            editText.MouseOverImage = editTextMouseOverImage;
        }