Skip to content

Commit

Permalink
Merge pull request #98 from philipbelesky/feature/#88-inline-buttons
Browse files Browse the repository at this point in the history
Add MenuButton widget and basic layout for reset/calibrate buttons
  • Loading branch information
mariuszhermansdorfer authored Feb 3, 2022
2 parents 472a7b4 + 9ade1f0 commit 2c26a27
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 7 deletions.
9 changes: 5 additions & 4 deletions SandWorm/Components/SandWormComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public class SandWormComponent : GH_ExtendableComponent
public static List<string> stats;
protected Stopwatch timer;

// Boolean controls
public bool reset;

#endregion
Expand All @@ -84,7 +83,7 @@ public SandWormComponent()

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddBooleanParameter("Reset", "reset", "Hitting this button will reset everything to defaults.", GH_ParamAccess.item, reset);
pManager.AddBooleanParameter("Reset", "reset", "Re-initialise the camera stream and its processing. Use if your output is strange or behaving unexpectedly. ", GH_ParamAccess.item, reset);
pManager.AddColourParameter("Color list", "color list", "Provide a list of custom colors to define a gradient for the elevation analysis.", GH_ParamAccess.list);
pManager.AddGenericParameter("Mesh", "mesh", "Provide a base mesh for the Cut & Fill analysis mode.", GH_ParamAccess.item);

Expand Down Expand Up @@ -113,10 +112,12 @@ protected override void OnComponentLoaded()
protected override void SolveInstance(IGH_DataAccess DA)
{
DA.GetData(0, ref reset);
if (_calibrate.Active)
_sensorElevation.Value = Calibrate((KinectTypes)_sensorType.Value);
ResetDataArrays();

if (reset || _reset)
{
if (_calibrate.Active)
_sensorElevation.Value = Calibrate((KinectTypes)_sensorType.Value);

if ((KinectTypes)_sensorType.Value == KinectTypes.KinectForWindows)
KinectForWindows.RemoveRef();
Expand Down
8 changes: 5 additions & 3 deletions SandWorm/ComponentsUI/SandWormComponentUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class SandWormComponentUI
public static MenuDropDown _sensorType;
public static MenuDropDown _refreshRate;
public static MenuSlider _sensorElevation;
public static MenuCheckBox _calibrate;
public static MenuButton _calibrate;
public static MenuButton _resetSettings;
public static MenuSlider _leftColumns;
public static MenuSlider _rightColumns;
public static MenuSlider _topRows;
Expand Down Expand Up @@ -69,9 +70,10 @@ public static void MainComponentUI(GH_ExtendableComponentAttributes attr)
_refreshRate.AddItem("1 FPS", "1 FPS");
_refreshRate.AddItem("0.2 FPS", "0.2 FPS");

_calibrate = new MenuButton(111121, "Calibrate Elevation"); // "Initiate the automatic calibration process to determine elevation."

MenuStaticText sensorElevationHeader = new MenuStaticText("Sensor elevation", "Distance between the sensor and the table. \nInput should be in millimeters.\nTo automatically estimate this value, check the 'Calibrate' checkbox and reset.");
_sensorElevation = new MenuSlider(sensorElevationHeader, 1, 0, 1500, 1000, 0);
_calibrate = new MenuCheckBox(10001, "Calibrate", "Calibrate");

MenuStaticText leftColumnsHeader = new MenuStaticText("Left columns", "Number of pixels to trim from the left.");
_leftColumns = new MenuSlider(leftColumnsHeader, 2, 0, 200, 50, 0);
Expand All @@ -96,9 +98,9 @@ public static void MainComponentUI(GH_ExtendableComponentAttributes attr)
optionsMenuPanel.AddControl(_sensorType);
optionsMenuPanel.AddControl(refreshRateHeader);
optionsMenuPanel.AddControl(_refreshRate);
optionsMenuPanel.AddControl(_calibrate);
optionsMenuPanel.AddControl(sensorElevationHeader);
optionsMenuPanel.AddControl(_sensorElevation);
optionsMenuPanel.AddControl(_calibrate);
optionsMenuPanel.AddControl(leftColumnsHeader);
optionsMenuPanel.AddControl(_leftColumns);
optionsMenuPanel.AddControl(rightColumnsHeader);
Expand Down
120 changes: 120 additions & 0 deletions SandWorm/CustomComponent/MenuButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using GH_IO.Serialization;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;

namespace SandWorm
{
public class MenuButton : GH_Attr_Widget
{
private bool _active;
private int _buttonHeight = 20;
private Rectangle _buttonBounds;
private int _verticalPadding = 8;

public bool Active
{
get
{
return _active;
}
set
{
_active = value;
}
}

public override string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

public MenuButton(int index, string id) : base(index, id)
{
Name = id;
}

public override SizeF ComputeMinSize()
{
float height = TextRenderer.MeasureText("1", WidgetServer.Instance.SliderValueTagFont).Height;
return new System.Drawing.SizeF(20f, _buttonHeight + _verticalPadding);
}

public override void PostUpdateBounds(out float outHeight)
{
outHeight = ComputeMinSize().Height;
}

public override void Layout()
{
_buttonBounds = new Rectangle(
(int)base.CanvasPivot.X, (int)base.CanvasPivot.Y + (int)(_verticalPadding / 2),
(int)base.Width, _buttonHeight + _verticalPadding);
}

public override void Render(WidgetRenderArgs args)
{
Graphics graphics = args.Canvas.Graphics;
GH_Capsule button;
var buttonBox = _buttonBounds;
buttonBox.Height -= _verticalPadding;

if (_active)
button = GH_Capsule.CreateTextCapsule(buttonBox, buttonBox, GH_Palette.Grey, "Calibrating...", 1, 0);
else
button = GH_Capsule.CreateTextCapsule(buttonBox, buttonBox, GH_Palette.Black, this.Name, 1, 0);

button.Render(graphics, _active, false, false);
button.Dispose();
}

public override void TooltipSetup(System.Drawing.PointF canvasPoint, GH_TooltipDisplayEventArgs e)
{
e.Icon = null;
e.Title = _name + " (Button)";
e.Text = "Automatically estimates the Elevation distance based on recent height data from the depth camera.";
}

public override GH_Attr_Widget IsTtipPoint(PointF pt)
{
if (_buttonBounds.Contains((int)pt.X, (int)pt.Y))
{
return this;
}
return null;
}

public override GH_ObjectResponse RespondToMouseMove(GH_Canvas sender, GH_CanvasMouseEvent e)
{
return GH_ObjectResponse.Capture;
}

public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMouseEvent e)
{
if (base.CanvasBounds.Contains(e.CanvasLocation))
{
_active = true;
}
return GH_ObjectResponse.Release;
}

public override GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, GH_CanvasMouseEvent e)
{
// Checking if it's a left click, and if it's in the button's area
if (e.Button == System.Windows.Forms.MouseButtons.Left && ((RectangleF)_buttonBounds).Contains(e.CanvasLocation))
{
return GH_ObjectResponse.Handled;
}
return GH_ObjectResponse.Ignore;
}
}
}
1 change: 1 addition & 0 deletions SandWorm/SandWorm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
<Compile Include="CustomComponent\GH_SwitchAction.cs" />
<Compile Include="CustomComponent\GH_SwitcherComponent.cs" />
<Compile Include="CustomComponent\GH_SwitcherComponentAttributes.cs" />
<Compile Include="CustomComponent\MenuButton.cs" />
<Compile Include="CustomComponent\MenuCheckBox.cs" />
<Compile Include="CustomComponent\MenuDropDown.cs" />
<Compile Include="CustomComponent\MenuPanel.cs" />
Expand Down

0 comments on commit 2c26a27

Please sign in to comment.