Example #1
0
        public CCControlStepper(CCSprite minusSprite, CCSprite plusSprite)
        {
            Debug.Assert(minusSprite != null, "Minus sprite must be not nil");
            Debug.Assert(plusSprite != null, "Plus sprite must be not nil");

            IsContinuous = true;
            IgnoreAnchorPointForPosition = false;
            autorepeat   = true;
            minimumValue = 0;
            maximumValue = 100;
            value        = 0;
            stepValue    = 1;
            wraps        = false;

            MinusSprite          = minusSprite;
            MinusSprite.Position = new CCPoint(minusSprite.ContentSize.Width / 2, minusSprite.ContentSize.Height / 2);
            AddChild(MinusSprite);

            MinusLabel          = new CCLabelTtf("-", ControlStepperLabelFont, 38);
            MinusLabel.Color    = ControlStepperLabelColorDisabled;
            MinusLabel.Position = new CCPoint(MinusSprite.ContentSize.Width / 2, MinusSprite.ContentSize.Height / 2);
            MinusSprite.AddChild(MinusLabel);

            PlusSprite          = plusSprite;
            PlusSprite.Position = new CCPoint(minusSprite.ContentSize.Width + plusSprite.ContentSize.Width / 2, minusSprite.ContentSize.Height / 2);
            AddChild(PlusSprite);

            PlusLabel          = new CCLabelTtf("+", ControlStepperLabelFont, 38);
            PlusLabel.Color    = ControlStepperLabelColorEnabled;
            PlusLabel.Position = PlusSprite.ContentSize.Center;
            PlusSprite.AddChild(PlusLabel);

            // Defines the content size
            CCRect maxRect = CCControlUtils.CCRectUnion(MinusSprite.BoundingBox, PlusSprite.BoundingBox);

            ContentSize = new CCSize(MinusSprite.ContentSize.Width + PlusSprite.ContentSize.Height, maxRect.Size.Height);

            // Register Touch Event
            var touchListener = new CCEventListenerTouchOneByOne();

            touchListener.IsSwallowTouches = true;

            touchListener.OnTouchBegan = OnTouchBegan;
            touchListener.OnTouchMoved = OnTouchMoved;
            touchListener.OnTouchEnded = OnTouchEnded;

            AddEventListener(touchListener);
        }
        public CCControlSlider(CCSprite backgroundSprite, CCSprite progressSprite, CCSprite thumbSprite)
        {
            Debug.Assert(backgroundSprite != null, "Background sprite can not be null");
            Debug.Assert(progressSprite != null, "Progress sprite can not be null");
            Debug.Assert(thumbSprite != null, "Thumb sprite can not be null");

            BackgroundSprite             = backgroundSprite;
            ProgressSprite               = progressSprite;
            ThumbSprite                  = thumbSprite;
            minimumValue                 = 0.0f;
            maximumValue                 = 1.0f;
            Value                        = minimumValue;
            IgnoreAnchorPointForPosition = false;

            // Defines the content size
            CCRect maxRect = CCControlUtils.CCRectUnion(backgroundSprite.BoundingBox, thumbSprite.BoundingBox);

            ContentSize = new CCSize(maxRect.Size.Width, maxRect.Size.Height);

            // Add the slider background
            BackgroundSprite.AnchorPoint = CCPoint.AnchorMiddle;
            BackgroundSprite.Position    = ContentSize.Center;
            AddChild(BackgroundSprite);

            // Add the progress bar
            ProgressSprite.AnchorPoint = CCPoint.AnchorMiddleLeft;
            ProgressSprite.PositionX   = 0;
            ProgressSprite.PositionY   = BackgroundSprite.PositionY;
            AddChild(ProgressSprite);

            // Add the slider thumb
            ThumbSprite.Position    = new CCPoint(0, ContentSize.Height / 2);
            ThumbSprite.AnchorPoint = CCPoint.AnchorMiddle;
            ThumbSprite.PositionX   = 0;
            ThumbSprite.PositionY   = BackgroundSprite.PositionY;
            AddChild(ThumbSprite);

            // Register Touch Event
            var touchListener = new CCEventListenerTouchOneByOne();

            touchListener.IsSwallowTouches = true;

            touchListener.OnTouchBegan = OnTouchBegan;
            touchListener.OnTouchMoved = OnTouchMoved;
            touchListener.OnTouchEnded = OnTouchEnded;

            AddEventListener(touchListener);
        }
Example #3
0
        public override void NeedsLayout()
        {
            if (!parentInited)
            {
                return;
            }
            // Hide the background and the label
            if (titleLabel != null)
            {
                titleLabel.Visible = false;
            }
            if (_backgroundSprite != null)
            {
                _backgroundSprite.Visible = false;
            }
            // Update anchor of all labels
            LabelAnchorPoint = labelAnchorPoint;

            // Update the label to match with the current state

            currentTitle      = GetTitleForState(State);
            currentTitleColor = GetTitleColorForState(State);

            TitleLabel = GetTitleLabelForState(State);

            var label = (ICCTextContainer)titleLabel;

            if (label != null && !String.IsNullOrEmpty(currentTitle))
            {
                label.Text = (currentTitle);
            }

            var rgbaLabel = titleLabel;

            if (rgbaLabel != null)
            {
                rgbaLabel.Color = currentTitleColor;
            }
            if (titleLabel != null)
            {
                titleLabel.Position = new CCPoint(ContentSize.Width / 2, ContentSize.Height / 2);
            }

            // Update the background sprite
            BackgroundSprite = GetBackgroundSpriteForState(State);
            if (_backgroundSprite != null)
            {
                _backgroundSprite.Position = new CCPoint(ContentSize.Width / 2, ContentSize.Height / 2);
            }

            // Get the title label size
            CCSize titleLabelSize = CCSize.Zero;

            if (titleLabel != null)
            {
                titleLabelSize = titleLabel.BoundingBox.Size;
            }

            // Adjust the background image if necessary
            if (isAdjustBackgroundImage)
            {
                // Add the margins
                if (_backgroundSprite != null)
                {
                    _backgroundSprite.ContentSize = new CCSize(titleLabelSize.Width + _marginH * 2, titleLabelSize.Height + _marginV * 2);
                }
            }
            else
            {
                //TODO: should this also have margins if one of the preferred sizes is relaxed?
                if (_backgroundSprite != null && _backgroundSprite is CCScale9Sprite)
                {
                    CCSize preferredSize = ((CCScale9Sprite)_backgroundSprite).PreferredSize;
                    if (preferredSize.Width <= 0)
                    {
                        preferredSize.Width = titleLabelSize.Width;
                    }
                    if (preferredSize.Height <= 0)
                    {
                        preferredSize.Height = titleLabelSize.Height;
                    }

                    _backgroundSprite.ContentSize = preferredSize;
                }
            }

            // Set the content size
            CCRect rectTitle = CCRect.Zero;

            if (titleLabel != null)
            {
                rectTitle = titleLabel.BoundingBox;
            }
            CCRect rectBackground = CCRect.Zero;

            if (_backgroundSprite != null)
            {
                rectBackground = _backgroundSprite.BoundingBox;
            }

            CCRect maxRect = CCControlUtils.CCRectUnion(rectTitle, rectBackground);

            ContentSize = new CCSize(maxRect.Size.Width, maxRect.Size.Height);

            if (titleLabel != null)
            {
                titleLabel.Position = new CCPoint(ContentSize.Width / 2, ContentSize.Height / 2);
                // Make visible label
                titleLabel.Visible = true;
            }

            if (_backgroundSprite != null)
            {
                _backgroundSprite.Position = new CCPoint(ContentSize.Width / 2, ContentSize.Height / 2);
                // Make visible the background
                _backgroundSprite.Visible = true;
            }
        }