Skip to content

Commit

Permalink
Add initial online library, component button and form
Browse files Browse the repository at this point in the history
  • Loading branch information
visose committed Jan 19, 2022
1 parent e8d4c52 commit 1b7281e
Show file tree
Hide file tree
Showing 17 changed files with 721 additions and 297 deletions.
97 changes: 97 additions & 0 deletions src/Robots.Grasshopper/ComponentButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Drawing;
using System.Windows.Forms;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Attributes;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;

namespace Robots.Grasshopper;

class ComponentButton : GH_ComponentAttributes
{
const int _buttonSize = 18;

readonly string _label;
readonly Action _action;

RectangleF _buttonBounds;
bool _mouseDown;

public ComponentButton(GH_Component owner, string label, Action action) : base(owner)
{
_label = label;
_action = action;
}

protected override void Layout()
{
base.Layout();

const int margin = 3;

var bounds = GH_Convert.ToRectangle(Bounds);
var button = bounds;

button.X += margin;
button.Width -= margin * 2;
button.Y = bounds.Bottom;
button.Height = _buttonSize;

bounds.Height += _buttonSize + margin;

Bounds = bounds;
_buttonBounds = button;
}

protected override void Render(GH_Canvas canvas, Graphics graphics, GH_CanvasChannel channel)
{
base.Render(canvas, graphics, channel);

if (channel == GH_CanvasChannel.Objects)
{
var prototype = GH_FontServer.StandardAdjusted;
var font = GH_FontServer.NewFont(prototype, 6f / GH_GraphicsUtil.UiScale);
var radius = 3;
var highlight = !_mouseDown ? 8 : 0;

using var button = GH_Capsule.CreateTextCapsule(_buttonBounds, _buttonBounds, GH_Palette.Black, _label, font, radius, highlight);
button.Render(graphics, false, Owner.Locked, false);
}
}

void SetMouseDown(bool value, GH_Canvas canvas, GH_CanvasMouseEvent e)
{
if (Owner.Locked || _mouseDown == value)
return;

if (value && e.Button != MouseButtons.Left)
return;

if (!_buttonBounds.Contains(e.CanvasLocation))
return;

if (_mouseDown && !value)
_action();

_mouseDown = value;
canvas.Invalidate();
}

public override GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, GH_CanvasMouseEvent e)
{
SetMouseDown(true, sender, e);
return base.RespondToMouseDown(sender, e);
}

public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMouseEvent e)
{
SetMouseDown(false, sender, e);
return base.RespondToMouseUp(sender, e);
}

public override GH_ObjectResponse RespondToMouseMove(GH_Canvas sender, GH_CanvasMouseEvent e)
{
SetMouseDown(false, sender, e);
return base.RespondToMouseMove(sender, e);
}
}
46 changes: 46 additions & 0 deletions src/Robots.Grasshopper/ComponentForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.ComponentModel;
using Eto.Drawing;
using Eto.Forms;

namespace Robots.Grasshopper;

class ComponentForm : Form
{
public ComponentForm()
{
Maximizable = false;
Minimizable = false;
Resizable = false;
Topmost = true;
ShowInTaskbar = true;
Owner = Rhino.UI.RhinoEtoApp.MainWindow;
}

public override bool Visible
{
get => base.Visible;
set
{
if (value)
CenterOnMouse();

base.Visible = value;
}
}

void CenterOnMouse()
{
var mousePos = Mouse.Position;
int x = (int)mousePos.X + 20;
int y = (int)mousePos.Y - MinimumSize.Height / 2;
Location = new Point(x, y);
}

protected override void OnClosing(CancelEventArgs e)
{
Visible = false;
e.Cancel = true;

base.OnClosing(e);
}
}
Loading

0 comments on commit 1b7281e

Please sign in to comment.