-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial online library, component button and form
- Loading branch information
Showing
17 changed files
with
721 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.