Skip to content

Commit

Permalink
Merge pull request #4 from mitevpi/web-view-2
Browse files Browse the repository at this point in the history
Creation Components
  • Loading branch information
mitevpi authored Mar 23, 2021
2 parents d20a36d + 72a9903 commit 55261b3
Show file tree
Hide file tree
Showing 27 changed files with 2,624 additions and 460 deletions.
83 changes: 83 additions & 0 deletions GHUI/BuildHtmlUiComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.IO;
using Grasshopper.Kernel;

namespace GHUI
{
public class BuildHtmlUiComponent : GH_Component
{
private string _oldString = null;

/// <summary>
/// Component for building a Vue.js UI into a HTML file within
/// Grasshopper.
/// </summary>
public BuildHtmlUiComponent()
: base("Build HTML UI", "HTML UI",
"Build a Web UI to a HTML file.",
"UI", "Main")
{
}

protected override void RegisterInputParams(GH_InputParamManager pManager)
{
pManager.AddTextParameter("HTML Path", "path", "Where to create the Vue HTML interface.",
GH_ParamAccess.item);
pManager.AddTextParameter("HTML String", "html", "The HTML string to write to a file.",
GH_ParamAccess.item);
}

protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddBooleanParameter("Success", "out", "Status of HTML file creation.", GH_ParamAccess.item);
pManager.AddTextParameter("Path", "path", "The path at which the HTML file has been written.", GH_ParamAccess.item);
}

protected override void SolveInstance(IGH_DataAccess da)
{
// get input from gh component inputs
string path = null;
string htmlString = null;

if (!da.GetData(0, ref path)) return;
if (!da.GetData(1, ref htmlString)) return;

// if the html string is the same, do nothing
if (_oldString == htmlString)
{
}
// otherwise try writing to the html file and inform the user
// of the successful write
else
{
try
{
File.WriteAllText(path, htmlString);
_oldString = htmlString;
}
catch (Exception e)
{
da.SetData(0, false);
da.SetData(1, "");
}
}

// set status message and path output if component executed successfully
da.SetData(0, true);
da.SetData(1, path);

GH_Document doc = OnPingDocument();
doc?.ScheduleSolution(500, ScheduleCallback);
}


private void ScheduleCallback(GH_Document document)
{
ExpireSolution(false);
}

protected override System.Drawing.Bitmap Icon => Properties.Resources.web_window;

public override Guid ComponentGuid => new Guid("1c7a41f6-2e46-4a7b-a67d-b7621dc312b4");
}
}
77 changes: 77 additions & 0 deletions GHUI/BuildSliderComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.IO;
using Grasshopper.Kernel;

namespace GHUI
{
public class BuildSliderComponent : GH_Component
{
private string _oldString = null;

/// <summary>
/// Component for building a Vue.js UI into a HTML file within
/// Grasshopper.
/// </summary>
public BuildSliderComponent()
: base("Create Slider", "Slider",
"Create a HTML Slider Input.",
"UI", "Create")
{
}

protected override void RegisterInputParams(GH_InputParamManager pManager)
{
pManager.AddTextParameter("Name", "name", "The name of the slider input component.", GH_ParamAccess.item,
"slider");
pManager.AddTextParameter("ID", "id", "The id of the slider input component.", GH_ParamAccess.item,
"slider");
pManager.AddNumberParameter("Value", "val", "The starting value of the slider input component.",
GH_ParamAccess.item, 50);
pManager.AddNumberParameter("Min", "min", "The min value of the slider input component.",
GH_ParamAccess.item, 0);
pManager.AddNumberParameter("Max", "max", "The max value of the slider input component.",
GH_ParamAccess.item, 100);
}

protected override void RegisterOutputParams(GH_OutputParamManager pManager)
{
pManager.AddTextParameter("HTML", "html", "The HTML code for the created slider input.",
GH_ParamAccess.list);
}

protected override void SolveInstance(IGH_DataAccess da)
{
// get input from gh component inputs
string name = null;
string id = null;
double value = 50;
double min = 0;
double max = 100;

da.GetData(0, ref name);
da.GetData(1, ref id);
da.GetData(2, ref value);
da.GetData(3, ref min);
da.GetData(4, ref max);

// create a valid HTML string from the inputs for our slider
string sliderString =
$"<input type='range' id='{id}' name='{name}' value='{value}' min='{min}' max='{max}'>";

da.SetData(0, sliderString);

GH_Document doc = OnPingDocument();
doc?.ScheduleSolution(500, ScheduleCallback);
}


private void ScheduleCallback(GH_Document document)
{
ExpireSolution(false);
}

protected override System.Drawing.Bitmap Icon => Properties.Resources.slider;

public override Guid ComponentGuid => new Guid("1c2a47f6-2e46-2a7b-a17d-b2629dc312b3");
}
}
13 changes: 13 additions & 0 deletions GHUI/Classes/Models/DomInputModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace GHUI.Classes.Models
{
public class DomInputModel
{
public string id;
public string value;
public string name;
public string max;
public string min;
public string type;
public string isChecked;
}
}
Loading

0 comments on commit 55261b3

Please sign in to comment.