Toggle with one click

Thank you very much , now looks better with highlight.

image

using System;
using System.Collections.Generic;
using Grasshopper.Kernel;
using System.Drawing;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel.Types;

namespace singleClick  //adapt to your project
{

    public class SpecialIntegerObject : GH_Param<GH_Boolean>
    {
        public SpecialIntegerObject() :
          base(new GH_InstanceDescription("Toggle", "Toggle", "True/False", "Params", "Input"))
        { }

        public override void CreateAttributes()
        {
            m_attributes = new SpecialIntegerAttributes(this);
        }

        protected override Bitmap Icon
        {
            get
            {
                return Toggle_new.Properties.Resources.tog;

            }
        }
        public override GH_Exposure Exposure
        {
            get
            {
                return GH_Exposure.primary | GH_Exposure.obscure;
            }
        }
        public override System.Guid ComponentGuid
        {
            get { return new Guid("{520F0976-35D1-4CC4-925B-2A07BFF17007}"); }
        }

        //private int m_value;
        private bool m_value;
        public bool Value
        {
            get { return m_value; }
            set { m_value = value; }
        }
        protected override void CollectVolatileData_Custom()
        {
            VolatileData.Clear();
            AddVolatileData(new Grasshopper.Kernel.Data.GH_Path(0), 0, new GH_Boolean(Value));
        }

        public override bool Write(GH_IO.Serialization.GH_IWriter writer)
        {
            writer.SetBoolean("SpecialInteger", m_value);
            return base.Write(writer);
        }
        public override bool Read(GH_IO.Serialization.GH_IReader reader)
        {
            reader.TryGetBoolean("SpecialInteger", ref m_value);
            return base.Read(reader);
        }
    }

    public class SpecialIntegerAttributes : GH_Attributes<SpecialIntegerObject>
    {

        public SpecialIntegerAttributes(SpecialIntegerObject owner)
          : base(owner)
        {

        }

        public override bool HasInputGrip { get { return false; } }
        public override bool HasOutputGrip { get { return true; } }

        private const int ButtonSize = 34;

        public bool bo;

        public Font font;
        
        //Our object is always the same size, but it needs to be anchored to the pivot.
        protected override void Layout()
        {
            //Lock this object to the pixel grid. 
            //I.e., do not allow it to be position in between pixels.
            Pivot = GH_Convert.ToPoint(Pivot);
            Bounds = new RectangleF(Pivot, new SizeF((float)(ButtonSize * 2.5), ButtonSize));
        }
        /// <summary>
        /// This method returns the button at the given column and row offsets.
        /// </summary>
        private RectangleF TextBound()
        {
            int x = Convert.ToInt32(Pivot.X);
            int y = Convert.ToInt32(Pivot.Y);
            return new RectangleF(new PointF(x + 27, y), new SizeF(55, ButtonSize));
        }
        private Rectangle Button()
        {
            int x = Convert.ToInt32(Pivot.X);
            int y = Convert.ToInt32(Pivot.Y);
            return new Rectangle(x + 4 , y + 4, ButtonSize - 8, ButtonSize - 8);
        }
        /// <summary>
        /// Gets the value for the given button.
        /// </summary>
        public override GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, GH_CanvasMouseEvent e)
        {
            //On one click we'll set the owner value.
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                RectangleF button = Button();
                if (button.Contains(e.CanvasLocation))
                {
                    bo = !bo;
                    Owner.Value = bo;
                    Owner.ExpireSolution(true);
                    return GH_ObjectResponse.Handled;
                }

            }
            return base.RespondToMouseDown(sender, e);

        }
        public override void SetupTooltip(PointF point, GH_TooltipDisplayEventArgs e)
        {
            base.SetupTooltip(point, e);
            e.Description = "Toggle";
        }

        /// <summary>
        /// This object is rendered as a 1x3 grid of capsules.
        /// </summary>

        protected override void Render(GH_Canvas canvas, Graphics graphics, GH_CanvasChannel channel)
        {
            if (channel == GH_CanvasChannel.Objects)
            {
                //Render output grip.
                GH_CapsuleRenderEngine.RenderOutputGrip(graphics, canvas.Viewport.Zoom, OutputGrip, true);

                Rectangle button = Button();
                RectangleF bound = Bounds;
                RectangleF text = TextBound();
                font = new Font("Verdana", 9);
                Color color;

                if (Owner.Value == true)
                {
                    bo = true;
                    color = Color.YellowGreen;
                }
                else
                {
                    bo = false;
                    color = Color.LightGray;
                }

                GH_Capsule capsule = GH_Capsule.CreateTextCapsule(bound, text, GH_Palette.Transparent, Owner.Value.ToString().ToUpper(), font, 17, 1) ;
                capsule.HighlightShape.Reset();
                capsule.HighlightShape.AddPath(capsule.OutlineShape, true);
                capsule.Render(graphics, Color.DarkGray);

                capsule.Dispose();

                GH_Capsule buttonb = GH_Capsule.CreateCapsule(button, GH_Palette.Transparent, 16, 1);
                
                buttonb.HighlightShape.Reset();
                buttonb.HighlightShape.AddPie(button, 180f, 180f);
                buttonb.Render(graphics, color);

                buttonb.Dispose();


            }
        }
    }
}
1 Like