Пример #1
0
        public override void DrawInteriorWithFrame(CGRect cellFrame, NSView inView)
        {
            var frame = cellFrame;
            var data  = ObjectValue as MacImageData;

            if (data != null)
            {
                if (data.Image != null)
                {
                    var imageSize = data.Image.Size;
                    if (imageSize.Width > 0 && imageSize.Height > 0)
                    {
                        var newHeight = (nfloat)Math.Min(imageSize.Height, frame.Height);
                        var newWidth  = (nfloat)(imageSize.Width * newHeight / imageSize.Height);

                        var context = NSGraphicsContext.CurrentContext;
                        var g       = context.GraphicsPort;

                        if (DrawsBackground && !Highlighted)
                        {
                            BackgroundColor.SetFill();
                            g.FillRect(new CGRect(frame.X, frame.Y, newWidth + ImagePadding, frame.Height));
                        }

                        var imageRect = new CGRect(frame.X, frame.Y, newWidth, newHeight);
                        imageRect.Y += (frame.Height - newHeight) / 2;

                        const float alpha = 1;                         //Enabled ? 1 : (nfloat)0.5;

                        context.ImageInterpolation = ImageInterpolation;

                        data.Image.Draw(imageRect, new CGRect(CGPoint.Empty, data.Image.Size), NSCompositingOperation.SourceOver, alpha, true, null);
                        frame.Width -= newWidth + ImagePadding;
                        frame.X     += newWidth + ImagePadding;
                    }
                }
            }

            var titleSize = AttributedStringValue.Size;

            // test to see if the text height is bigger then the cell, if it is,
            // don't try to center it or it will be pushed up out of the cell!
            if (titleSize.Height < frame.Size.Height)
            {
                //frame.Y = frame.Y + (frame.Size.Height - titleSize.Height) / 2;
            }

            if (UseTextShadow)
            {
                var str = new NSMutableAttributedString(StringValue);
                str.AddAttribute(NSStringAttributeKey.Shadow, Highlighted ? TextHighlightShadow : TextShadow, new NSRange(0, (int)str.Length));
                AttributedStringValue = str;
            }

            base.DrawInteriorWithFrame(frame, inView);
        }
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);

            var context = UIGraphics.GetCurrentContext();

            context.ClearRect(this.Bounds);

            BackgroundColor.SetFill();
            context.FillRect(rect);



            for (nuint i = 0; i < this.NumberOfWaves; i++)
            {
                context = UIGraphics.GetCurrentContext();

                context.SetLineWidth(i == 0 ? this.PrimaryWaveLineWidth : this.SecondaryWaveLineWidth);

                var halfHeight = this.Bounds.Height / 2.0f;
                var width      = this.Bounds.Width;
                var mid        = width / 2.0f;

                var maxAmplitude = halfHeight - 4.0f;                 // 4 corresponds to twice the stroke width

                // Progress is a value between 1.0 and -0.5, determined by the current wave idx, which is used to alter the wave's amplitude.
                var progress        = 1.0f - ((float)i / (float)this.NumberOfWaves);
                var normedAmplitude = (1.5f * progress - 0.5f) * this.Amplitude;

                var multiplier = Math.Min(1.0, (progress / 3.0f * 2.0f) + (1.0f / 3.0f));

                this.WaveColor.ColorWithAlpha((nfloat)multiplier * this.WaveColor.CGColor.Alpha).SetStroke();

                for (float x = 0; x < width + this.Density; x += this.Density)
                {
                    // We use a parable to scale the sinus wave, that has its peak in the middle of the view.
                    var scaling = -Math.Pow(1.0f / mid * (x - mid), 2.0f) + 1.0f;

                    var y = scaling * maxAmplitude * normedAmplitude * Math.Sin(2 * Math.PI * (x / width) * this.Frequency + this.Phase) + halfHeight;

                    y = y + i;

                    if (x == 0)
                    {
                        context.MoveTo((nfloat)x, (nfloat)y);
                    }
                    else
                    {
                        context.AddLineToPoint((nfloat)x, (nfloat)y);
                    }
                }

                context.StrokePath();
            }
        }
Пример #3
0
            public override void DrawBorderAndBackground(CGRect cellFrame, NSView controlView)
            {
                if (DrawsBackground)
                {
                    var nscontext = NSGraphicsContext.CurrentContext;
                    var context   = nscontext.GraphicsPort;

                    BackgroundColor.SetFill();
                    context.FillRect(cellFrame);
                }

                base.DrawBorderAndBackground(cellFrame, controlView);
            }
        public override void Draw(CGRect rect)
        {
            if (BackgroundColor != null)
            {
                BackgroundColor.SetFill();
            }

            SelectionIndicatorArrowLayer.BackgroundColor = SelectionIndicatorColor.CGColor;
            SelectionIndicatorStripLayer.BackgroundColor = SelectionIndicatorColor.CGColor;

            SelectionIndicatorBoxLayer.BackgroundColor = SelectionIndicatorColor.CGColor;
            SelectionIndicatorBoxLayer.BorderColor     = SelectionIndicatorColor.CGColor;

            scrollView.Layer.Sublayers = new DisposableCALayer[0];
            ClearScrollViewSubLayers();
            var oldRect = rect;

            if (type == HMSegmentedControlType.Text)
            {
                for (int idx = 0; idx < sectionTitles.Count; idx++)
                {
                    var    size = MeasureTitle(idx);
                    var    stringWidth = size.Width;
                    var    stringHeight = size.Height;
                    CGRect rectangle, rectDiv, rectFull;

                    var locationUp           = SelectionIndicatorLocation == HMSegmentedControlIndicatorLocation.Up;
                    var selectionStyleNotBox = SelectionStyle != HMSegmentedControlSelectionStyle.Box;
                    var y = (float)Math.Round(((Frame.Height - (selectionStyleNotBox ? 1 : 0 * SelectionIndicatorHeight)) / 2) - (stringHeight / 2) + (SelectionIndicatorHeight * (locationUp ? 1 : 0)));

                    if (segmentWidthStyle == HMSegmentedControlWidthStyle.Fixed)
                    {
                        rectangle = new CGRect((segmentWidth * idx) + (segmentWidth - stringWidth) / 2, y, stringWidth, stringHeight);
                        rectDiv   = new CGRect((segmentWidth * idx) + (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4));
                        rectFull  = new CGRect(segmentWidth * idx, 0, segmentWidth, oldRect.Size.Height);
                    }
                    else
                    {
                        var xOffset = 0.0f;
                        var i       = 0;
                        foreach (var width in segmentWidths)
                        {
                            if (idx == i)
                            {
                                break;
                            }
                            xOffset += width;
                            i++;
                        }

                        var widthForIndex = segmentWidths[idx];
                        rectangle = new CGRect(xOffset, y, widthForIndex, stringHeight);
                        rectFull  = new CGRect(segmentWidth * idx, 0, widthForIndex, oldRect.Size.Height);
                        rectDiv   = new CGRect(xOffset - (VerticalDividerWidth / 2),
                                               SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4));
                    }

                    rectangle = new CGRect((float)Math.Ceiling(rectangle.X), (float)Math.Ceiling(rectangle.Y), (float)Math.Ceiling(rectangle.Size.Width), (float)Math.Ceiling(rectangle.Size.Height));

                    var titleLayer = new DisposableCATextLayer
                    {
                        Frame            = rectangle,
                        AlignmentMode    = CATextLayer.AlignmentCenter,
                        TruncationMode   = CATextLayer.TruncantionEnd,
                        AttributedString = AttributedTitle(idx),
                        ContentsScale    = UIScreen.MainScreen.Scale
                    };
                    AddScrollViewSubLayer(titleLayer);

                    if (VerticalDividerEnabled)
                    {
                        var verticalDividerLayer = new DisposableCALayer {
                            Frame = rectDiv, BackgroundColor = VerticalDividerColor.CGColor
                        };
                        AddScrollViewSubLayer(verticalDividerLayer);
                    }

                    AddBackgroundAndBorderLayer(rectFull);
                }
            }
            else if (type == HMSegmentedControlType.Image)
            {
                for (int idx = 0; idx < sectionTitles.Count; idx++)
                {
                    var icon        = sectionImages[idx];
                    var imageWidth  = icon.Size.Width;
                    var imageHeight = icon.Size.Height;
                    var y           = (float)Math.Round(Frame.Height - SelectionIndicatorHeight) / 2 - imageHeight / 2 + (SelectionIndicatorLocation == HMSegmentedControlIndicatorLocation.Up ? SelectionIndicatorHeight : 0);
                    var x           = segmentWidth * idx + (segmentWidth - imageWidth) / 2.0f;
                    var rectNew     = new CGRect(x, y, imageWidth, imageHeight);

                    var imageLayer = new DisposableCALayer {
                        Frame = rectNew
                    };
                    if (SelectedIndex == idx && sectionSelectedImages != null)
                    {
                        imageLayer.Contents = sectionSelectedImages[idx].CGImage;
                    }
                    else
                    {
                        imageLayer.Contents = icon.CGImage;
                    }

                    AddScrollViewSubLayer(imageLayer);

                    if (VerticalDividerEnabled && idx > 0)
                    {
                        var verticalDividerLayer = new DisposableCALayer
                        {
                            Frame           = new CGRect((segmentWidth * idx) - (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4)),
                            BackgroundColor = VerticalDividerColor.CGColor
                        };
                        AddScrollViewSubLayer(verticalDividerLayer);
                    }

                    AddBackgroundAndBorderLayer(rectNew);
                }
            }
            else if (type == HMSegmentedControlType.TextAndImage)
            {
                for (int idx = 0; idx < sectionTitles.Count; idx++)
                {
                    var icon        = sectionImages[idx];
                    var imageWidth  = icon.Size.Width;
                    var imageHeight = icon.Size.Height;

                    var stringHeight = MeasureTitle(idx).Height;
                    var yOffset      = (float)Math.Round(((Frame.Height - SelectionIndicatorHeight) / 2) - (stringHeight / 2));

                    var imageXOffset = SegmentEdgeInset.Left;
                    var textXOffset  = SegmentEdgeInset.Left;
                    var textWidth    = (nfloat)0.0f;

                    if (segmentWidthStyle == HMSegmentedControlWidthStyle.Fixed)
                    {
                        imageXOffset = (segmentWidth * idx) + (segmentWidth / 2.0f) - (imageWidth / 2.0f);
                        textXOffset  = segmentWidth * idx;
                        textWidth    = segmentWidth;
                    }
                    else
                    {
                        var xOffset = 0.0f;
                        var i       = 0;
                        foreach (var width in segmentWidths)
                        {
                            if (idx == i)
                            {
                                break;
                            }
                            xOffset += width;
                            i++;
                        }

                        imageXOffset = xOffset + (segmentWidths[idx] / 2.0f) - (imageWidth / 2.0f);
                        textXOffset  = xOffset;
                        textWidth    = segmentWidths[idx];
                    }

                    var imageYOffset = (float)Math.Round((Frame.Height - SelectionIndicatorHeight) / 2.0f);
                    var imageRect    = new CGRect(imageXOffset, imageYOffset, imageWidth, imageHeight);
                    var textRect     = new CGRect((float)Math.Ceiling(textXOffset), (float)Math.Ceiling(yOffset), (float)Math.Ceiling(textWidth), (float)Math.Ceiling(stringHeight));

                    var titleLayer = new DisposableCATextLayer
                    {
                        Frame            = textRect,
                        AlignmentMode    = CATextLayer.AlignmentCenter,
                        AttributedString = AttributedTitle(idx),
                        TruncationMode   = CATextLayer.TruncantionEnd,
                        ContentsScale    = UIScreen.MainScreen.Scale
                    };

                    var imageLayer = new DisposableCALayer
                    {
                        Frame    = imageRect,
                        Contents = (SelectedIndex == idx && sectionSelectedImages != null) ? sectionSelectedImages[idx].CGImage : icon.CGImage
                    };

                    AddScrollViewSubLayer(imageLayer);
                    AddScrollViewSubLayer(titleLayer);

                    AddBackgroundAndBorderLayer(imageRect);
                }
            }

            if (SelectedIndex != -1)
            {
                if (SelectionStyle == HMSegmentedControlSelectionStyle.Arrow)
                {
                    SetArrowFrame();
                    AddScrollViewSubLayer(SelectionIndicatorArrowLayer);
                }
                else
                {
                    if (SelectionIndicatorStripLayer.SuperLayer == null)
                    {
                        SelectionIndicatorStripLayer.Frame = FrameForSelectionIndicator();
                        AddScrollViewSubLayer(SelectionIndicatorStripLayer);

                        if (SelectionStyle == HMSegmentedControlSelectionStyle.Box && SelectionIndicatorBoxLayer.SuperLayer == null)
                        {
                            SelectionIndicatorBoxLayer.Frame = FrameForFillerSelectionIndicator();
                            InsertScrollViewSubLayer(SelectionIndicatorBoxLayer, 0);
                        }
                    }
                }
            }
        }
 public override void DrawRect(System.Drawing.RectangleF dirtyRect)
 {
     // TODO: Why doesn't the background fill compiled under 64-bit Mono?
     BackgroundColor.SetFill();
     NSBezierPath.FillRect(dirtyRect);
 }
        public override void Draw(CGRect rect)
        {
            if (SectionTitles == null || SectionTitles.Count == 0)
            {
                return;
            }

            if (BackgroundColor != null)
            {
                BackgroundColor.SetFill();
            }

            SelectionIndicatorArrowLayer.BackgroundColor = SelectionIndicatorColor.CGColor;
            SelectionIndicatorStripLayer.BackgroundColor = SelectionIndicatorColor.CGColor;

            SelectionIndicatorBoxLayer.BackgroundColor = SelectionIndicatorColor.CGColor;
            SelectionIndicatorBoxLayer.BorderColor     = SelectionIndicatorColor.CGColor;

            _scrollView.Layer.Sublayers = new DisposableCALayer[0];
            ClearScrollViewSubLayers();
            var oldRect = rect;

            if (_controlType == DHSegmentedControlType.Text)
            {
                for (int idx = 0; idx < _sectionTitles.Count; idx++)
                {
                    var    size = MeasureTitle(idx);
                    var    stringWidth = size.Width;
                    var    stringHeight = size.Height;
                    CGRect newRect, rectDiv, rectFull;

                    var locationUp           = SelectionIndicatorLocation == DHSegmentedControlLocation.Up;
                    var selectionStyleNotBox = SelectionStyle != DHSegmentedControlSelectionStyle.Box;

                    var y = (float)Math.Round(((this.Frame.Height - (selectionStyleNotBox ? 1 : 0 * SelectionIndicatorHeight)) / 2) - (stringHeight / 2) + (SelectionIndicatorHeight * (locationUp ? 1 : 0)));

                    if (_segmentWidthStyle == DHSegmentedControlWidthStyle.Fixed && !UserDraggable)
                    {
                        stringWidth = _segmentWidth - LabelPaddingInset.Right - LabelPaddingInset.Left;

                        newRect  = new CGRect((_segmentWidth * idx) + (_segmentWidth - stringWidth) / 2, 0, stringWidth, oldRect.Height - LabelPaddingInset.Top - LabelPaddingInset.Bottom);
                        rectDiv  = new CGRect((_segmentWidth * idx) + (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4));
                        rectFull = new CGRect(_segmentWidth * idx, 0, _segmentWidth, oldRect.Height - SelectionIndicatorHeight);
                    }
                    else if (_segmentWidthStyle == DHSegmentedControlWidthStyle.Fixed && UserDraggable)
                    {
                        newRect  = new CGRect((_segmentWidth * idx) + (_segmentWidth - stringWidth) / 2, y, stringWidth, stringHeight);
                        rectDiv  = new CGRect((_segmentWidth * idx) + (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4));
                        rectFull = new CGRect(_segmentWidth * idx, 0, _segmentWidth, oldRect.Size.Height);
                    }
                    else
                    {
                        var xOffset = GetSelectedSegmentOffset(idx);

                        var widthForIndex = _segmentWidths[idx];
                        newRect  = new CGRect(xOffset, y, widthForIndex, stringHeight);
                        rectFull = new CGRect(_segmentWidth * idx, 0, widthForIndex, oldRect.Size.Height);
                        rectDiv  = new CGRect(xOffset - (VerticalDividerWidth / 2),
                                              SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4));
                    }

                    newRect = new CGRect((float)Math.Ceiling(newRect.X), (float)Math.Ceiling(newRect.Y), (float)Math.Ceiling(newRect.Size.Width), (float)Math.Ceiling(newRect.Size.Height));

                    var titleLabel = _titleLabels[idx];
                    titleLabel.AttributedText = AttributedTitle(idx);
                    titleLabel.Frame          = newRect;

                    AddScrollViewSubLayer(titleLabel.Layer);

                    if (VerticalDividerEnabled)
                    {
                        var verticalDividerLayer = new DisposableCALayer {
                            Frame = rectDiv, BackgroundColor = VerticalDividerColor.CGColor
                        };
                        AddScrollViewSubLayer(verticalDividerLayer);
                    }

                    AddBackgroundAndBorderLayer(rectFull);
                }
            }


            if (_selectedIndex != -1)
            {
                if (SelectionIndicatorStripLayer.SuperLayer == null)
                {
                    SelectionIndicatorStripLayer.Frame = FrameForSelectionIndicator();
                    AddScrollViewSubLayer(SelectionIndicatorStripLayer);

                    if (SelectionStyle == DHSegmentedControlSelectionStyle.Box && SelectionIndicatorBoxLayer.SuperLayer == null)
                    {
                        SelectionIndicatorBoxLayer.Frame = FrameForFillerSelectionIndicator();
                        InsertScrollViewSubLayer(SelectionIndicatorBoxLayer, 0);
                    }
                }
            }
        }