Skip to content

Instantly share code, notes, and snippets.

@teocomi
Last active February 26, 2022 14:53
Show Gist options
  • Save teocomi/f08d29144f60b7ecef7aaa090287d500 to your computer and use it in GitHub Desktop.
Save teocomi/f08d29144f60b7ecef7aaa090287d500 to your computer and use it in GitHub Desktop.
Grasshopper GUI Button mouseover
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Attributes;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
namespace MetaHopper
{
internal class MH_ButtonObject_Attributes : GH_ComponentAttributes
{
private bool mouseOver;
private bool mouseDown;
private RectangleF buttonArea;
private RectangleF textArea;
public MH_ButtonObject_Attributes(MH_SelectButtonComponent owner)
: base(owner)
{
mouseOver = false;
mouseDown = false;
// PerformLayout();
}
protected override void Layout()
{
Bounds = RectangleF.Empty;
base.Layout();
buttonArea = new RectangleF(Bounds.Left, Bounds.Bottom, Bounds.Width, 20);
textArea = buttonArea;
// textArea.Inflate(-3, -3);
Bounds = RectangleF.Union(Bounds, buttonArea);
}
protected override void Render(Grasshopper.GUI.Canvas.GH_Canvas canvas, System.Drawing.Graphics graphics, Grasshopper.GUI.Canvas.GH_CanvasChannel channel)
{
base.Render(canvas, graphics, channel);
if (channel == GH_CanvasChannel.Objects)
{
GH_PaletteStyle style = GH_CapsuleRenderEngine.GetImpliedStyle(GH_Palette.Black, Selected, Owner.Locked, true);
GH_Capsule button = GH_Capsule.CreateTextCapsule(buttonArea, textArea, GH_Palette.Black, "Select", GH_FontServer.Small, 1, 9);
button.RenderEngine.RenderBackground(graphics, canvas.Viewport.Zoom, style);
if (!mouseDown)
{
button.RenderEngine.RenderHighlight(graphics);
}
button.RenderEngine.RenderOutlines(graphics, canvas.Viewport.Zoom, style);
if (mouseOver)
{
button.RenderEngine.RenderBackground_Alternative(graphics, Color.FromArgb(50,Color.Blue), false);
MH_SelectButtonComponent buttonComp = Owner as MH_SelectButtonComponent;
Pen highlightPen = new Pen(Color.Blue,4);
foreach (IGH_DocumentObject ido in buttonComp.ActiveObjects.Union(buttonComp.InactiveObjects))
{
RectangleF boundsInflated = ido.Attributes.Bounds;
boundsInflated.Inflate(4, 4);
graphics.DrawRectangle(highlightPen, GH_Convert.ToRectangle(boundsInflated));
}
}
button.RenderEngine.RenderText(graphics, Color.White);
button.Dispose();
}
}
public override GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, Grasshopper.GUI.GH_CanvasMouseEvent e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right && sender.Viewport.Zoom >= 0.5f && buttonArea.Contains(e.CanvasLocation))
{
mouseDown = true;
//Owner.ButtonDown = true;
MH_SelectButtonComponent owner = Owner as MH_SelectButtonComponent;
owner.SelectSelected();
return GH_ObjectResponse.Capture;
}
if (e.Button == System.Windows.Forms.MouseButtons.Left && sender.Viewport.Zoom >= 0.5f && buttonArea.Contains(e.CanvasLocation))
{
mouseDown = true;
//Owner.ButtonDown = true;
Owner.RecordUndoEvent("Update Selection",new Grasshopper.Kernel.Undo.Actions.GH_GenericObjectAction(Owner));
MH_SelectButtonComponent owner = Owner as MH_SelectButtonComponent;
owner.UpdateSelection();
Owner.ExpireSolution(true);
return GH_ObjectResponse.Capture;
}
return base.RespondToMouseDown(sender, e);
}
public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMouseEvent e)
{
if (!buttonArea.Contains(e.CanvasLocation))
{
mouseOver = false;
}
if (mouseDown)
{
mouseDown = false;
sender.Invalidate();
return GH_ObjectResponse.Release;
}
return base.RespondToMouseUp(sender, e);
}
public override GH_ObjectResponse RespondToMouseMove(GH_Canvas sender, GH_CanvasMouseEvent e)
{
System.Drawing.Point pt = GH_Convert.ToPoint(e.CanvasLocation);
if (e.Button != System.Windows.Forms.MouseButtons.None)
{
return base.RespondToMouseMove(sender, e);
}
if (buttonArea.Contains(pt))
{
if (mouseOver != true)
{
mouseOver = true;
sender.Invalidate();
}
return GH_ObjectResponse.Capture;
}
if (mouseOver != false)
{
mouseOver = false;
sender.Invalidate();
}
return GH_ObjectResponse.Release;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment