Skip to content

Commit

Permalink
Dev with documentation canvas (#1)
Browse files Browse the repository at this point in the history
* Initial set up

* Remove implementation of assembly priority
  • Loading branch information
gshinohara authored Sep 14, 2024
1 parent dabc2ff commit 8dc3b68
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
9 changes: 9 additions & 0 deletions WireEventImplementor/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"profiles": {
"WireEventImplementor": {
"commandName": "Executable",
"executablePath": "C:\\Program Files\\Rhino 7\\System\\Rhino.exe",
"commandLineArgs": ""
}
}
}
19 changes: 19 additions & 0 deletions WireEventImplementor/WireEventImplementor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<Version>1.0</Version>
<Title>WireEventImplementor</Title>
<Description>Description of WireEventImplementor</Description>
<TargetExt>.dll</TargetExt>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grasshopper" Version="7.13.21348.13001" IncludeAssets="compile;build" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Windows.Forms" />
</ItemGroup>

</Project>
79 changes: 79 additions & 0 deletions WireEventImplementor/WireInstances.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Grasshopper.GUI.Canvas;
using Grasshopper.GUI.Canvas.Interaction;
using Grasshopper.Kernel;
using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WireEventImplementor
{
public static class WireInstances
{

public delegate void PreWiredEventHandler(WireStatus wireStatus);

public delegate void WiringEventHandler(WireStatus wireStatus);

public delegate void PostWiredEventHandler(WireStatus wireStatus);

/// <summary>
/// Invoked when your wiring cursor is floating.
/// </summary>
public static event WiringEventHandler Wiring;

/// <summary>
/// Invoked when your wiring cursor is on the grip.
/// </summary>
public static event PreWiredEventHandler PreWired;

/// <summary>
/// Invoked when your wire connects grips.
/// </summary>
public static event PostWiredEventHandler PostWired;

private static TaskCompletionSource<WireStatus> m_source = new TaskCompletionSource<WireStatus>();

/// <summary>
/// Call when canvas created.
/// </summary>
/// <param name="canvas">Applied canvas.</param>
public static void SetUp(GH_Canvas canvas)
{
canvas.MouseMove += Canvas_MouseMove;
canvas.MouseUp += Canvas_MouseUp;
}

private static void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (sender is GH_Canvas canvas && canvas.ActiveInteraction is GH_WireInteraction wireInteraction)
{
wireInteraction.GetWireParameters(out WireStatus status);

if (e.Button == MouseButtons.Left)
{
m_source = new TaskCompletionSource<WireStatus>();

if (status.WireTarget == null)
Wiring?.Invoke(status);
else
{
PreWired?.Invoke(status);
m_source.SetResult(status);
}
}

canvas.Refresh();
}
}

private static async void Canvas_MouseUp(object sender, MouseEventArgs e)
{
WireStatus status = await m_source.Task;
m_source = new TaskCompletionSource<WireStatus>();

PostWired?.Invoke(status);

(sender as GH_Canvas).Refresh();
}
}
}
31 changes: 31 additions & 0 deletions WireEventImplementor/WireStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Grasshopper.Kernel;

namespace WireEventImplementor
{
public class WireStatus
{
public IGH_Param WireSource { get; }

public IGH_Param WireTarget { get; }

public LinkMode? LinkMode { get; }

public bool? IsDragFromInput { get; }

public IGH_Param PreviousSideParam => (bool)IsDragFromInput ? WireTarget : WireSource;

public IGH_Param SubsequentSideParam => (bool)IsDragFromInput ? WireSource : WireTarget;

private WireStatus()
{
}

internal WireStatus(IGH_Param wireSource, IGH_Param wireTarget, LinkMode? linkMode, bool? isDragFromInput)
{
WireSource = wireSource;
WireTarget = wireTarget;
LinkMode = linkMode;
IsDragFromInput = isDragFromInput;
}
}
}
29 changes: 29 additions & 0 deletions WireEventImplementor/WireUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Grasshopper.GUI.Canvas.Interaction;
using Grasshopper.Kernel;
using System;
using System.Reflection;

namespace WireEventImplementor
{
public enum LinkMode
{
Replace,
Add,
Remove
}

public static class WireUtil
{
internal static void GetWireParameters(this GH_WireInteraction self, out WireStatus wireStatus)
{
Func<string, object> getPrivateParam = (name) => typeof(GH_WireInteraction).GetField(name, BindingFlags.NonPublic | BindingFlags.Instance).GetValue(self);

IGH_Param source = getPrivateParam("m_source") as IGH_Param;
IGH_Param target = getPrivateParam("m_target") as IGH_Param;
LinkMode mode = (LinkMode)Enum.Parse(typeof(LinkMode), getPrivateParam("m_mode").ToString());
bool dragFromInput = (bool)getPrivateParam("m_dragfrominput");

wireStatus = new WireStatus(source, target, mode, dragFromInput);
}
}
}

0 comments on commit 8dc3b68

Please sign in to comment.