using System;
using System.Collections.Generic;
using GH_IO.Serialization;
using Grasshopper.Kernel;
using Rhino.Geometry;
using System.Drawing;
using System.Windows.Forms;
using Grasshopper.Kernel.Attributes;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel.Data;
// In order to load the result of this wizard, you will also need to
// add the output bin/ folder of this project to the list of loaded
// folder in Grasshopper.
// You can use the _GrasshopperDeveloperSettings Rhino command for that.
namespace ExtendGUI
{
public class ExtendGUIComponent : GH_Component
{
///
/// Each implementation of GH_Component must provide a public
/// constructor without any arguments.
/// Category represents the Tab in which the component will appear,
/// Subcategory the panel. If you use non-existing tab or panel names,
/// new tabs/panels will automatically be created.
///
public ExtendGUIComponent()
: base("ExtendGUI", "ExGUI",
"test",
"User", "tests")
{
Num = 0;
}
public override void CreateAttributes()
{
m_attributes = new Attributes_Custom(this);
}
///
/// Registers all the input parameters for this component.
///
protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddNumberParameter("0", "", "", GH_ParamAccess.item, 0);
}
///
/// Registers all the output parameters for this component.
///
protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddNumberParameter("Num", "N", "Number.", GH_ParamAccess.item);
}
///
/// This is the method that actually does the work.
///
/// The DA object can be used to retrieve data from input parameters and
/// to store data in output parameters.
protected override void SolveInstance(IGH_DataAccess DA)
{
double num_0 = 0;
if (!DA.GetData(0, ref num_0)) return;
Params.Input[0].NickName = num_0.ToString();
if (Params.Input.Count >= 2)
{
if(Num == 0)
{
double num_1 = 0.0;
if (!DA.GetData(1, ref num_1)) return;
DA.SetData(0, num_0 * num_1);
}
else if(Num == 1)
{
double num_1 = 0;
if (!DA.GetData(1, ref num_1)) return;
DA.SetData(0, num_0 / num_1);
}
else
{
DA.SetData(0, 0);
}
}
}
private void Params_ParameterChanged(object sender, GH_ParamServerEventArgs e)
{
throw new NotImplementedException();
}
public int Num
{
get; set;
}
public override bool Write(GH_IO.Serialization.GH_IWriter writer)
{
writer.SetInt32("Num", Num);
return base.Write(writer);
}
public override bool Read(GH_IO.Serialization.GH_IReader reader)
{
Num = reader.GetInt32("Num");
return base.Read(reader);
}
///
/// Provides an Icon for every component that will be visible in the User Interface.
/// Icons need to be 24x24 pixels.
///
protected override System.Drawing.Bitmap Icon
{
get
{
// You can add image files to your project resources and access them like this:
//return Resources.IconForThisComponent;
return null;
}
}
///
/// Each component must have a unique Guid to identify it.
/// It is vital this Guid doesn't change otherwise old ghx files
/// that use the old ID will partially fail during loading.
///
public override Guid ComponentGuid
{
get { return new Guid("6c03b95c-dbcc-47ae-8c24-8b7d57997691"); }
}
}
public class Attributes_Custom : GH_ComponentAttributes
{
public Attributes_Custom(ExtendGUIComponent owner) : base(owner) { }
protected override void Layout()
{
base.Layout();
Rectangle rec0 = GH_Convert.ToRectangle(Bounds/*コンポーネントの下の余白範囲*/); /*余白をRectangleに変更し編集できるように*/
rec0.Height += 44;
Rectangle rec1 = rec0;
rec1.Y = rec0.Bottom - 44;
rec1.Height = 22;
rec1.Inflate(-2, -2); /*指定サイズだけオフセット*/
Rectangle rec2 = rec0;
rec2.Y = rec0.Bottom - 22;
rec2.Height = 22;
rec2.Inflate(-2, -2);
Bounds = rec0; /*余白にrec0を代入(Boundsはgetset機能がある)*/
ButtonBounds_0 = rec1; /*ButtonBoundsにrec1を代入(ButtunBoundsは下でgetset機能を作っている)*/
ButtonBounds_1 = rec2;
}
private Rectangle ButtonBounds_0 { get; set; } /*ButtonBoundsにgetset機能を追加*/
private Rectangle ButtonBounds_1 { get; set; }
private GH_Capsule button_0 { get; set; }
private GH_Capsule button1 { get; set; }
protected override void Render(GH_Canvas canvas, Graphics graphics, GH_CanvasChannel channel)
{
switch (channel)
{
case GH_CanvasChannel.Objects:
base.RenderComponentCapsule(canvas, graphics, true, false, false, true, true, true);
ExtendGUIComponent comp = Owner as ExtendGUIComponent;
button_0 = GH_Capsule.CreateTextCapsule(ButtonBounds_0/*ボタン*/, ButtonBounds_0/*テキストボックス*/, comp.Num == 0 ? GH_Palette.Black/*ボタンの色*/: GH_Palette.White, "Multiplied", 5/*ボタンの角のR*/, 0);
button_0.Render(graphics, this.Selected, Owner.Locked, Owner.Hidden);
button_0.Dispose();
button1 = GH_Capsule.CreateTextCapsule(ButtonBounds_1/*ボタン*/, ButtonBounds_1/*テキストボックス*/, comp.Num == 1 ? GH_Palette.Black/*ボタンの色*/: GH_Palette.White, "Divided", 5/*ボタンの角のR*/, 0);
button1.Render(graphics, this.Selected, Owner.Locked, Owner.Hidden);
button1.Dispose();
break;
default:
base.Render(canvas, graphics, channel);
break;
}
}
public override GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, GH_CanvasMouseEvent e) /*GH_ObjectResponce RespondToMouseDown でWeb検索するとその他の挙動も出てくる*/
{
ExtendGUIComponent extendGUI = Owner as ExtendGUIComponent;
if (e.Button/*e.Buttunでどちらの左右どちらがクリックされているかをgetする*/ == MouseButtons.Left)
{
RectangleF rec = ButtonBounds_0;
if (rec.Contains(e.CanvasLocation)) //もしButtonの範囲内にe(マウス)があったら
{
if (extendGUI.Num == 0)
{
return GH_ObjectResponse.Handled;
}
else
{
extendGUI.RecordUndoEvent("button");
extendGUI.Num = 0;
extendGUI.ExpireSolution(true);
//Inputの変更
Grasshopper.Kernel.Parameters.Param_Number input_0 = new Grasshopper.Kernel.Parameters.Param_Number();
input_0.NickName = "*";
input_0.Optional = true;
for (int i = 1; i < extendGUI.Params.Input.Count;)
{
extendGUI.Params.UnregisterInputParameter(extendGUI.Params.Input[i]);
}
extendGUI.Params.RegisterInputParam(input_0, 1);
extendGUI.OnSolutionExpired(true);
extendGUI.Params.OnParametersChanged();
return GH_ObjectResponse.Handled; /*.Handled以外にも選択肢はあるがどれでも変わらなかった*/
}
}
}
if (e.Button == MouseButtons.Left)
{
RectangleF rec = ButtonBounds_1;
if (rec.Contains(e.CanvasLocation)) //もしButtonの範囲内にe(マウス)があったら
{
if (extendGUI.Num == 1)
{
return GH_ObjectResponse.Handled;
}
else
{
extendGUI.RecordUndoEvent("button");
extendGUI.Num = 1;
extendGUI.ExpireSolution(true);
//Inputの変更
Grasshopper.Kernel.Parameters.Param_Number input_0 = new Grasshopper.Kernel.Parameters.Param_Number();
input_0.NickName = "/";
input_0.Optional = true;
//input_0.AddVolatileData(gH_Path, 0, 10);
for(int i =1;i< extendGUI.Params.Input.Count;)
{
extendGUI.Params.UnregisterInputParameter(extendGUI.Params.Input[i]);
}
extendGUI.Params.RegisterInputParam(input_0, 1);
extendGUI.OnSolutionExpired(true);
extendGUI.Params.OnParametersChanged();
return GH_ObjectResponse.Handled; /*.Handled以外にも選択肢はあるがどれでも変わらなかった*/
}
}
}
return base.RespondToMouseDown(sender, e);
}
}
}