Why the button is behind the capsule?


i add a button on a param component,but,the button is behind the main capsule…help…
the code in custom attribute:

  protected override void Layout()
    {
        base.Layout();
        Rectangle r = GH_Convert.ToRectangle(this.Bounds);
        r.Height = 40;
        rec2 = new RectangleF(Pivot.X - ((this.Bounds.Width - 4) / 2) + 10, Pivot.Y + 27, this.Bounds.Width - 4, 25);
        this.Bounds = r;
    }

    private RectangleF rec2 { get; set; }

    public override Grasshopper.GUI.Canvas.GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, Grasshopper.GUI.GH_CanvasMouseEvent e)
    {
        if (((e.Button == MouseButtons.Left) && this.rec2.Contains(e.CanvasLocation)) && (this.Owner != null))
        {
            (Owner as ComponentTest2).IsBtnDown = true;
            Owner.ExpireSolution(true);
            return GH_ObjectResponse.Handled;
        }
        return base.RespondToMouseDown(sender, e);
    }
    public override Grasshopper.GUI.Canvas.GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, Grasshopper.GUI.GH_CanvasMouseEvent e)
    {
        if ((Owner as ComponentTest2).IsBtnDown)
        {
            (Owner as ComponentTest2).IsBtnDown = false;
            Owner.ExpireSolution(true);
            return GH_ObjectResponse.Release;
        }
        return base.RespondToMouseUp(sender, e);
    }

    protected override void Render(GH_Canvas canvas, Graphics graphics, GH_CanvasChannel channel)
    {
        GH_Capsule capsule = GH_Capsule.CreateCapsule(Bounds, GH_Palette.Normal);
        capsule.Render(graphics, this.Selected, Owner.Locked, true);
        capsule.Dispose();
        Font font = new Font("MS UI Gothic", 3, FontStyle.Italic);
        if (channel == GH_CanvasChannel.Objects)
        {
            bool isbd = (Owner as ComponentTest2).IsBtnDown;
            GH_Capsule gH_Capsule;
            if (isbd)
            {
                gH_Capsule = GH_Capsule.CreateTextCapsule(rec2, rec2, GH_Palette.Black, "On", 2, 0);
                gH_Capsule.AddOutputGrip(this.OutputGrip.X, this.OutputGrip.Y);
                gH_Capsule.Render(graphics, this.Selected, base.Owner.Locked, false);
                gH_Capsule.Dispose();
            }
            else
            {
                gH_Capsule = GH_Capsule.CreateTextCapsule(rec2, rec2, GH_Palette.White, "Off", 2, 0);
                gH_Capsule.AddOutputGrip(this.OutputGrip.X, this.OutputGrip.Y);
                gH_Capsule.Render(graphics, this.Selected, base.Owner.Locked, false);
                gH_Capsule.Dispose();
            }
        }
    }

Thank you.

Because you are drawing “capsule” (the button?) first. Put that fragment after of “gh_capsule” and also inside the object channel.

1 Like

Thankyou Dani,it works:
image
but how to put the little circle under the BigCapsule?

Thank you

Code:

 protected override void Render(GH_Canvas canvas, Graphics graphics, GH_CanvasChannel channel)
    {
        if (channel == GH_CanvasChannel.Objects)
        {
            GH_Capsule BigCapsule = GH_Capsule.CreateCapsule(Bounds, GH_Palette.Normal);
            BigCapsule.Render(graphics, this.Selected, Owner.Locked, false);

            bool isbd = (Owner as ComponentTest2).IsBtnDown;
            GH_Capsule btn_Capsule;
            if (isbd)
            {
                btn_Capsule = GH_Capsule.CreateTextCapsule(rec2, rec2, GH_Palette.Black, "On", 2, 0);
                btn_Capsule.AddOutputGrip(this.OutputGrip.X, this.OutputGrip.Y);
            }
            else
            {
                btn_Capsule = GH_Capsule.CreateTextCapsule(rec2, rec2, GH_Palette.Black, "Off", 2, 9);
                btn_Capsule.AddOutputGrip(this.OutputGrip.X, this.OutputGrip.Y);
            }                
            btn_Capsule.Render(graphics, this.Selected, base.Owner.Locked, false);

            btn_Capsule.Dispose();
            BigCapsule.Dispose();
            
        }
    }

Try removing this line:

btn_Capsule.AddOutputGrip(this.OutputGrip.X, this.OutputGrip.Y);

and before of:

BigCapsule.Render(graphics, this.Selected, Owner.Locked, false);

write this:

GH_CapsuleRenderEngine.RenderOutputGrip(graphics, canvas.Viewport.Zoom, this.OutputGrip, false);

EDIT: You’re assigning the grip to the wrong capsule, maybe that’s the problem.

1 Like

Thank you Dani,it works!

Hi @andrealu2012

When the component has tree input param, Whey the Size of this.InputGrip is none not 3?