Skip to content

Commit

Permalink
Merge pull request #11 from DanielAbalde/GroupNicknames
Browse files Browse the repository at this point in the history
v1.3.0
  • Loading branch information
DanielAbalde authored May 16, 2021
2 parents 4eab77d + 1a2adb6 commit 8485bb5
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 162 deletions.
327 changes: 234 additions & 93 deletions Drawing.cs

Large diffs are not rendered by default.

Binary file added Media/DisplayOnlyNicknames.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Media/GroupNicknames.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Media/menu1.3.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
131 changes: 82 additions & 49 deletions MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,9 @@ public SunglassesMenuItem()
{
Name = "SunglassesMenuItem";
Size = new Size(200, 30);
ToolTipText = "Draw the name above the component, so that it can be used in icon mode.";
ToolTipText = "Draw the name or nickname above the component, so that it can be used in icon mode.\n\rVersion " + typeof(SunglassesMenuItem).Assembly.GetName().Version;
Checked = Settings.DisplayNames;

if (Rhino.Runtime.HostUtils.RunningOnWindows)
{
GH_DocumentObject.Menu_AppendItem(DropDown, "Select font", SelectFontHandler);
}
else
{
var slider = new Grasshopper.GUI.GH_DigitScroller
{
MinimumValue = 3,
MaximumValue = 256,
DecimalPlaces = 1,
Value = (decimal)Settings.Font.Size,
Size = new Size(200, 24)
};
slider.ValueChanged += Slider_ValueChanged;
GH_DocumentObject.Menu_AppendCustomItem(DropDown, slider);
}


_menuNicknames = GH_DocumentObject.Menu_AppendItem(DropDown, "Display nicknames",
DisplayNicknamesHandler, true, Settings.DisplayNicknames);
_menuNicknames.ToolTipText = "Draw the nickname instead of the name of the objects.";
Expand All @@ -61,7 +43,7 @@ public SunglassesMenuItem()
GH_DocumentObject.Menu_AppendItem(_menuFilter.DropDown, "Graphics", FilterGraphicHandler, true, Settings.FilterGraphic)
.ToolTipText = "If checked, draw the name of graphic objects, like Group, Scribble, Jump...";
var customFilter = GH_DocumentObject.Menu_AppendItem(_menuFilter.DropDown, "Exclusions");
customFilter.ToolTipText = "Insert the names of the objects, separated by commas, that you want to exclude.";
customFilter.ToolTipText = "Insert the names or nicknames of the objects, separated by commas, that you want to exclude.";
//GH_DocumentObject.Menu_AppendTextItem(customFilter.DropDown, Utils.FilterCustom, FilterCustomKeyDownHandler, FilterCustomTextChangedHandler, true);
var textBox = new ToolStripTextBox()
{
Expand All @@ -73,13 +55,39 @@ public SunglassesMenuItem()
customFilter.DropDownItems.Add(textBox);

GH_DocumentObject.Menu_AppendSeparator(DropDown);

var riched = GH_DocumentObject.Menu_AppendItem(DropDown, "Riched capsules", DisplayRichedAttributesHandler, true, Settings.DisplayRichedCapsules);
riched.ToolTipText = "Shows all the information within a component when zoomed in. ";

GH_DocumentObject.Menu_AppendItem(DropDown, "Hide on low zoom",
HideOnLowZoomHandler, true, Settings.HideOnLowZoom)
GH_DocumentObject.Menu_AppendItem(DropDown, "Hide on low zoom", HideOnLowZoomHandler, true, Settings.HideOnLowZoom)
.ToolTipText = "If checked, the name disappears when the canvas zoom is very low.";

GH_DocumentObject.Menu_AppendItem(DropDown, "Big nicknames on groups", GroupsOnLowZoomHandler, true, Settings.GroupsOnLowZoom)
.ToolTipText = "If checked, the nickname of the groups is drawn with low zoom.";

GH_DocumentObject.Menu_AppendSeparator(DropDown);

if (Rhino.Runtime.HostUtils.RunningOnWindows)
{
GH_DocumentObject.Menu_AppendItem(DropDown, "Select font", SelectFontHandler);
}
else
{
var slider = new Grasshopper.GUI.GH_DigitScroller
{
MinimumValue = 3,
MaximumValue = 256,
DecimalPlaces = 1,
Value = (decimal)Settings.Font.Size,
Size = new Size(200, 24),
Name = "Font size"
};
slider.ValueChanged += Slider_ValueChanged;
GH_DocumentObject.Menu_AppendCustomItem(DropDown, slider);
}



UpdateCustomNicknameMenuEntryState();
}

Expand Down Expand Up @@ -203,7 +211,30 @@ private bool IsValidFilterCustom(string text)
.Where(t => !string.IsNullOrWhiteSpace(t))
.ToArray();

return split.All(t => Grasshopper.Instances.ComponentServer.FindObjectByName(t, true, true) != null);
return split.All(t => Exists(t));

bool Exists(string key)
{
foreach(var p in Grasshopper.Instances.ComponentServer.ObjectProxies)
{
if (p.Exposure == GH_Exposure.hidden)
continue;
if (p.Desc.Name.Equals(key, StringComparison.OrdinalIgnoreCase) ||
p.Desc.NickName.Equals(key, StringComparison.OrdinalIgnoreCase))
return true;
}
if (Grasshopper.Instances.ActiveCanvas.IsDocument)
{
foreach (var obj in Grasshopper.Instances.ActiveCanvas.Document.Objects)
{
if (obj.Name.Equals(key, StringComparison.OrdinalIgnoreCase) ||
obj.NickName.Equals(key, StringComparison.OrdinalIgnoreCase))
return true;
}
}

return false;
}
}

private void UpdateCustomNicknameMenuEntryState()
Expand Down Expand Up @@ -242,6 +273,11 @@ private void HideOnLowZoomHandler(object sender, EventArgs e)
((ToolStripMenuItem)sender).Checked = Settings.HideOnLowZoom = !Settings.HideOnLowZoom;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private void GroupsOnLowZoomHandler(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked = Settings.GroupsOnLowZoom = !Settings.GroupsOnLowZoom;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}

private void Slider_ValueChanged(object sender, Grasshopper.GUI.Base.GH_DigitScrollerEventArgs e)
{
Expand All @@ -258,24 +294,14 @@ private static void ClickHandler(object sender, EventArgs e)
const string NameOfClusterComponent = "Cluster";
private bool ObjectFilter(IGH_DocumentObject obj)
{
// Hide nickname for panels
if (Settings.DisplayNicknames || Settings.DisplayCustomNicknames)
{
// Hide group's nickname for now

if (obj is GH_Panel || obj is GH_Group || obj is GH_NumberSlider)
return false;

// TODO: Draw Group's nickname at the largest font size
// in the group's container box to be able to read it when the zoom is too low.
}

// Move filter logic ahead to enable filter on custom nicknames.

// Do not split the string in every paint call. Cache them in Settings.
// String manipulation & IEnumerable.Contains are time-costly.
if (Settings.IsFilterCustomEnabled &&
Settings.ShouldExcludeObject(obj.Name))
Settings.ShouldExcludeObject(obj))
return false;

if (!Settings.FilterComponents &&
Expand All @@ -296,9 +322,14 @@ private bool ObjectFilter(IGH_DocumentObject obj)

if (Settings.DisplayCustomNicknames)
{
if(obj is IGH_Param param && param.Kind == GH_ParamKind.floating &&
(param.IconDisplayMode == GH_IconDisplayMode.name ||
param.IconDisplayMode == GH_IconDisplayMode.application && !Grasshopper.CentralSettings.CanvasObjectIcons))
{
return false;
}
var code = string.Join(".", obj.Category, obj.SubCategory, obj.Name);

// Reduce string manipulation to improve performance
var id = obj.ComponentGuid;

if (!_nicknamesCache.TryGetValue(code, out var defNick))
Expand All @@ -322,14 +353,9 @@ private bool ObjectFilter(IGH_DocumentObject obj)
{
if (obj is GH_Cluster)
{
// A newly-created cluster, or a cluster which is not an existing user object.
// You cannot really know its original nickname
// Show the nickname if it's not 'Cluster' - take it as newly-created

return obj.NickName != NameOfClusterComponent;
}

Rhino.RhinoApp.WriteLine(obj.Name);

return true;
}
}
Expand All @@ -340,11 +366,9 @@ private bool ObjectFilter(IGH_DocumentObject obj)
return true;
}

// This method is created to cache results and improve performance.
private static Dictionary<Guid, bool> _specialObjectGuids = new Dictionary<Guid, bool>();
private static bool IsSpecialObject(IGH_DocumentObject obj)
{
// Cluster is regarded as a component, although it lies in the 'Special' namespace.
if (obj is GH_Cluster)
return false;

Expand Down Expand Up @@ -406,11 +430,20 @@ private void Canvas_CanvasPrePaintObjects(Grasshopper.GUI.Canvas.GH_Canvas sende
}
private void Canvas_CanvasPostPaintObjects(Grasshopper.GUI.Canvas.GH_Canvas sender)
{
if (!Settings.DisplayRichedCapsules || !sender.IsDocument)
return;
if (_visibleObjects == null || _visibleObjects.Length == 0)
return;
Drawing.PaintRichedCapsules(sender, _visibleObjects);
if (!sender.IsDocument || _visibleObjects == null || _visibleObjects.Length == 0)
return;
if(Settings.GroupsOnLowZoom && sender.Viewport.Zoom <= 0.5f)
{
var groups = _visibleObjects.OfType<GH_Group>();
if (Settings.IsFilterCustomEnabled)
{
groups = groups.Where(g => !Settings.ShouldExcludeObject(g));
}
Drawing.PaintGroupNickname(sender, groups);
}

if (Settings.DisplayRichedCapsules)
Drawing.PaintRichedCapsules(sender, _visibleObjects);
}

private Guid galapagosID = new Guid("E6DD2904-14BC-455b-8376-948BF2E3A7BC");
Expand Down
10 changes: 6 additions & 4 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ private void Instances_CanvasCreated(Grasshopper.GUI.Canvas.GH_Canvas canvas)
{
Rhino.RhinoApp.WriteLine("Sunglasses cannot find Display menu.");
return;
}
((ToolStripMenuItem)menu[0]).DropDownItems.Insert(3, new SunglassesMenuItem());
}
var items = ((ToolStripMenuItem)menu[0]).DropDownItems;
if(items.Find("Sunglasses", false).Length == 0)
items.Insert(3, new SunglassesMenuItem());

}

Expand All @@ -46,7 +48,7 @@ public class SunglassesInfo : GH_AssemblyInfo
public override Guid Id => new Guid("194607e9-d4d6-4e5a-836f-a65774231315");
public override string AuthorName => "Daniel Gonzalez Abalde";
public override string AuthorContact => "https://discord.gg/75tP9hBnk8";
public override GH_LibraryLicense License => GH_LibraryLicense.opensource;
public override string Version => "1.2.0";
public override GH_LibraryLicense License => GH_LibraryLicense.opensource;
public override string AssemblyVersion => base.AssemblyVersion;
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

<h1 align="center"> Sunglasses </h1>
<p align="center"> <img align="center" border="1px" width="50%" src="Media/sunglassesBIG.jpg" alt="Img"></p>
<h6 align="center"> v1.3.0 </h6>
<p align="center">Draw the name (or nickname) of Grasshopper objects with Draw Icons mode on. It also enriches the capsules when you zoom in to understand the component and visualise its data at a quick glance.</p>

## 😎 Overview
Expand All @@ -11,6 +12,11 @@ The second release (v1.1.0) includes the option **Riched capsules**. This is ver
<p align="center"> <img align="center" border="1px" src="Media/RichedCapsulesDispatchGIF.gif" alt="Img"></p>

The third release (v1.2.0) adds a new option **Display custom nicknames** where only user defined nicknames are drawn.
<p align="center"> <img align="center" border="1px" src="Media/DisplayOnlyNicknames.png" alt="Img"></p>

The fourth release (v1.3.0) adds a new option **Big nicknames on groups** to display the nickname of the groups when the zoom is too low.
<p align="center"> <img align="center" border="1px" src="Media/GroupNicknames.png" alt="Img"></p>


## 🔌 Installation

Expand All @@ -22,19 +28,20 @@ From food4Rhino:
2) Copy and paste it in the Grasshopper Libraries folder, usually: C:\Users\\<YourUser\>\AppData\Roaming\Grasshopper\Libraries.
3) Restart Rhinoceros and Grasshopper.
4) Access from Grasshopper Toolbar > Display > Sunglasses.


<!---
From YAK (for Rhino 7 and above):
0) Type <ins>PackageManager</ins> in the rhino command prompt.
1) In the Online tab, type "sunglasses" in the search box.
2) Install last version.
3) Restart Rhino and Grasshopper.
--->

## 📖 Usage
This plugin does not contain any components. Access the user settings from <ins>Grasshopper Toolbar > Display > Sunglasses</ins> to enable or disable, change the font, draw the nickname instead of name, and include or exclude objects to display the name.
<p align="center"> <img align="center" border="1px" src="Media/menu1.2.0.PNG" alt="Img"></p>
<p align="center"> <img align="center" border="1px" src="Media/menu1.3.0.PNG" alt="Img"></p>

From version 1.1.0, there is another feature to visualise the content of components and parameters by zooming in on the capsules, when Display > Draw Icons is activated. Enable it from <ins>Grasshopper Toolbar >Display > Sunglasses > Riched capsules</ins>.
<p align="center"> <img align="center" border="1px" src="Media/RichedCapsulesListItemGIF.gif" alt="Img"></p>
Expand All @@ -54,6 +61,6 @@ This program is distributed in the hope that it will be useful, but WITHOUT ANY
You can use my Discord for [assitance](https://discord.gg/b6URGyAQxY) or [feedback](https://discord.gg/b6URGyAQxY) or create an issue using github.


## 🍻 Donation
## ☕ Support me!

You can invite me to a beer using [Ko-fi](https://ko-fi.com/daniga) or [Paypal](https://www.paypal.com/paypalme/danielabalde). Thank you!
Contribute to thank or further develop using [Ko-fi](https://ko-fi.com/daniga) or [Paypal](https://www.paypal.com/paypalme/danielabalde). Thank you!
29 changes: 20 additions & 9 deletions Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class Settings
public static string Key_FilterGraphic => "Sunglasses.FilterGraphic";
public static string Key_FilterCustom => "Sunglasses.FilterCustom";
public static string Key_HideOnLowZoom => "Sunglasses.HideOnLowZoom ";
public static string Key_GroupsOnLowZoom => "Sunglasses.GroupsOnLowZoom ";

#region Fonts
private static Font _font;
Expand Down Expand Up @@ -96,15 +97,13 @@ internal static Font FontCapsuleParameterData

}
#endregion

// Cache frequently-used values to improve performance
// ??= isn't used to preserve compatibility with C# 7.3


private static bool? m_DisplayNames;
private static bool? m_DisplayNicknames;
private static bool? m_DisplayCustomNicknames;
private static bool? m_DisplayRichedCapsules;
private static bool? m_HideOnLowZoom;
private static bool? m_GroupsOnLowZoom;
private static bool? m_FilterComponents;
private static bool? m_FilterParameters;
private static bool? m_FilterSpecial;
Expand Down Expand Up @@ -182,6 +181,20 @@ public static bool HideOnLowZoom
Grasshopper.Instances.Settings.SetValue(Key_HideOnLowZoom, value);
}
}
public static bool GroupsOnLowZoom
{
get
{
return m_GroupsOnLowZoom ??
(m_GroupsOnLowZoom =
Grasshopper.Instances.Settings.GetValue(Key_GroupsOnLowZoom, false)).Value;
}
set
{
m_GroupsOnLowZoom = value;
Grasshopper.Instances.Settings.SetValue(Key_GroupsOnLowZoom, value);
}
}
public static bool FilterComponents
{
get
Expand Down Expand Up @@ -256,12 +269,12 @@ public static string FilterCustom

public static bool IsFilterCustomEnabled => !string.IsNullOrEmpty(FilterCustom);

public static bool ShouldExcludeObject(string objectName)
public static bool ShouldExcludeObject(IGH_DocumentObject obj)
{
if (_customFilters == null)
UpdateFilterHashset();

return _customFilters.Contains(objectName);
return _customFilters.Contains(obj.Name) || _customFilters.Contains(obj.NickName);
}

private static void UpdateFilterHashset()
Expand All @@ -288,9 +301,7 @@ public static void ResetFonts()
_fontCapsuleParameterData = null;
}
public static void Dispose()
{
// For better readibility

{
_font?.Dispose();
_fontCapsuleInfoName?.Dispose();
_fontCapsuleDescription?.Dispose();
Expand Down

0 comments on commit 8485bb5

Please sign in to comment.