Exemple #1
0
        /// <summary>
        /// Creates a button with a set of state-textures and (optionally) some text.
        /// Any state that is left 'null' will display the up-state texture. Beware that all
        /// state textures should have the same dimensions.
        /// </summary>
        /// <exception cref="ArgumentException">When upState is null</exception>
        public Button(Texture upState, string text = "", Texture downState       = null,
                      Texture overState            = null, Texture disabledState = null)
        {
            if (upState == null)
            {
                throw new ArgumentException("Texture 'upState' cannot be null");
            }

            _upState       = upState;
            _downState     = downState;
            _overState     = overState;
            _disabledState = disabledState;

            _state = ButtonState.Up;
            _body  = new Image(upState);
            _body.PixelSnapping = true;
            ScaleWhenDown       = downState != null ? 1.0f : 0.9f;
            ScaleWhenOver       = AlphaWhenDown = 1.0f;
            AlphaWhenDisabled   = disabledState != null ? 1.0f : 0.5f;
            _enabled            = true;
            _useHandCursor      = true;
            _textBounds         = Rectangle.Create(0, 0, _body.Width, _body.Height);
            _triggerBounds      = Rectangle.Create();

            _contents = new Sprite();
            _contents.AddChild(_body);
            AddChild(_contents);
            Touch += OnTouch;

            TouchGroup = true;
            Text       = text;
        }
Exemple #2
0
        private void OnTouch(TouchEvent evt)
        {
            SparrowSharp.MouseCursor = (_useHandCursor && _enabled && evt.InteractsWith(this)) ? MouseCursor.Hand : MouseCursor.Default;

            if (!_enabled)
            {
                return;
            }
            Touch touch = evt.GetTouch(this);

            if (touch == null)
            {
                State = ButtonState.Up;
            }
            else if (touch.Phase == TouchPhase.Stationary)
            {
                State = ButtonState.Over;
            }
            else if (touch.Phase == TouchPhase.Began && _state != ButtonState.Down)
            {
                _triggerBounds = GetBounds(Stage);
                _triggerBounds.Inflate(MaxDragDist, MaxDragDist);

                State = ButtonState.Down;
            }
            else if (touch.Phase == TouchPhase.Moved)
            {
                var isWithinBounds = _triggerBounds.Contains(touch.GlobalX, touch.GlobalY);

                if (_state == ButtonState.Down && !isWithinBounds)
                {
                    // reset button when finger is moved too far away ...
                    State = ButtonState.Up;
                }
                else if (_state == ButtonState.Up && isWithinBounds)
                {
                    // ... and reactivate when the finger moves back into the bounds.
                    State = ButtonState.Down;
                }
            }
            else if (touch.Phase == TouchPhase.Ended && _state == ButtonState.Down)
            {
                State = ButtonState.Up;
                Triggered?.Invoke(this, evt);
            }
        }