Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Version 1.3.0
Browse files Browse the repository at this point in the history
Added nickname feature, bug fixes and workarounds
  • Loading branch information
lin-ycv authored May 14, 2021
1 parent 371541d commit 308846a
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 59 deletions.
33 changes: 25 additions & 8 deletions src/MultilingualGH/MultilingualComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public MultilingualComp()
"Params", "Util")
{
}
public override GH_Exposure Exposure => GH_Exposure.primary | GH_Exposure.obscure;
public override GH_Exposure Exposure => GH_Exposure.primary;// | GH_Exposure.obscure;

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
Expand Down Expand Up @@ -53,7 +53,7 @@ public override void AddedToDocument(GH_Document document)
MultilingualMenu.mghDropdown.ToolTipText = UI.MenuDisabled;
document.ObjectsDeleted += RemovedMe;
IGH_Param inputParam = base.Params.Input[0];
if (mgh.excludeUser != string.Empty && inputParam.SourceCount == 0)
if (mgh.excludeUser != "" && inputParam.SourceCount == 0)
{
var exPanel = new GH_Panel();
exPanel.CreateAttributes();
Expand Down Expand Up @@ -84,7 +84,6 @@ public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
{
mgh.textLabel = !mgh.textLabel;
((ToolStripComboBox)items["Method"]).SelectedIndex = mgh.textLabel ? 1 : 0;
MultilingualInstance.EventHandler(canvas, mgh);
}, true, mgh.textLabel);
if (mgh.textLabel)
{
Expand All @@ -102,9 +101,26 @@ public override void AppendAdditionalMenuItems(ToolStripDropDown menu)
if (inputSize > textSize.Maximum) inputSize = textSize.Maximum;
else if (inputSize < textSize.Minimum) inputSize = textSize.Minimum;
mgh.size = Convert.ToInt32(inputSize);
canvas.Refresh();
};
Menu_AppendCustomItem(menu, textSize);
}
Menu_AppendItem(menu, UI.NicknamePreferred, (s, e) =>
{
mgh.nickname = (byte) (((ToolStripMenuItem)s).Checked ? 0 : 1);
((ToolStripComboBox)items["Nickname"]).SelectedIndex = mgh.nickname;
}, mgh.nickname!=2, mgh.nickname==1);
Menu_AppendItem(menu, UI.NicknameOnly, (s, e) =>
{
mgh.nickname = (byte)(((ToolStripMenuItem)s).Checked ? 0 : 2);
((ToolStripComboBox)items["Nickname"]).SelectedIndex = mgh.nickname;
}, true, mgh.nickname == 2);
Menu_AppendItem(menu, UI.ShowEnglish, (s, e) =>
{
mgh.showeng = !mgh.showeng;
((ToolStripMenuItem)items["ShowEnglish"]).Checked = mgh.showeng;
MultilingualInstance.EventHandler(canvas, mgh);
}, true, mgh.showeng);
Menu_AppendItem(menu, UI.UseDefaultExclusions, (s, e) =>
{
mgh.excludeDefault = !mgh.excludeDefault;
Expand Down Expand Up @@ -138,7 +154,7 @@ internal void LangSelection(string lang)
Message = lang;
if (mgh.prevLang != lang)
{
Translation.Clear(Grasshopper.Instances.ActiveCanvas.Document);
Translation.Clear(OnPingDocument());
mgh.prevLang = lang;
ExpireSolution(true);
}
Expand All @@ -158,7 +174,7 @@ internal void PluginSelection(string plugin)
if (mgh.enabled)
{
if (clean)
Translation.Clear(Grasshopper.Instances.ActiveCanvas.Document);
Translation.Clear(OnPingDocument());
ExpireSolution(true);
MultilingualInstance.EventHandler(Grasshopper.Instances.ActiveCanvas, mgh);
}
Expand All @@ -179,7 +195,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
}
else
{
mgh.excludeUser = string.Empty;
mgh.excludeUser = "";
((ToolStripMenuItem)MultilingualMenu.mghDropdown.DropDownItems["User"]).Checked = false;
}
Message = mgh.enabled ? mgh.language : UI.Disabled;
Expand Down Expand Up @@ -207,9 +223,10 @@ internal void RemovedMe(object sender, GH_DocObjectEventArgs e) //sender = docum
{
ghDoc.ObjectsDeleted -= RemovedMe;
ghDoc.ObjectsAdded -= Translation.CompAdded;
if (!mgh.keep) Translation.Clear(ghDoc);
if (mgh.enabled) mgh.enabled = false;
mgh.compGuid = Guid.Empty;
MultilingualMenu.mghDropdown.ToolTipText = string.Empty;
MultilingualMenu.mghDropdown.ToolTipText = "";
MultilingualMenu.mghDropdown.Enabled = true;
MultilingualMenu.UpdateMenu(mgh);
}
Expand Down Expand Up @@ -263,7 +280,7 @@ public override bool Write(GH_IO.Serialization.GH_IWriter writer)
}
public override bool Read(GH_IO.Serialization.GH_IReader reader)
{
var ghDoc = Grasshopper.Instances.ActiveCanvas.Document;
var ghDoc = OnPingDocument();
if (ghDoc != null)
{
MultilingualInstance.documents.TryGetValue(ghDoc.DocumentID, out mgh);
Expand Down
8 changes: 7 additions & 1 deletion src/MultilingualGH/MultilingualInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class MultilingualInstance
internal bool excludeDefault = true;
internal string excludeUser = string.Empty;
internal bool keep = false;
internal bool showeng = true;
internal byte nickname = 0;
internal string language = "English";
internal string extras = string.Empty;
internal string prevLang;
Expand All @@ -27,6 +29,8 @@ internal MultilingualInstance()
excludeDefault = Grasshopper.Instances.Settings.GetValue("MGHUseDe", true);
excludeUser = Grasshopper.Instances.Settings.GetValue("MGHUseUe", string.Empty);
keep = Grasshopper.Instances.Settings.GetValue("MGHKeepAnno", false);
showeng = Grasshopper.Instances.Settings.GetValue("MGHShowEng", true);
nickname = Grasshopper.Instances.Settings.GetValue("MGHNickname", (byte)0);
language = Grasshopper.Instances.Settings.GetValue("MGHLangSel", "English");
extras = Grasshopper.Instances.Settings.GetValue("MGHExtras", string.Empty);
size = Grasshopper.Instances.Settings.GetValue("MGHTextSize", 8);
Expand All @@ -38,6 +42,8 @@ static internal void SaveSettings(MultilingualInstance mgh)
Grasshopper.Instances.Settings.SetValue("MGHUseDe", mgh.excludeDefault);
Grasshopper.Instances.Settings.SetValue("MGHUseUe", mgh.excludeUser);
Grasshopper.Instances.Settings.SetValue("MGHKeepAnno", mgh.keep);
Grasshopper.Instances.Settings.SetValue("MGHShowEng", mgh.showeng);
Grasshopper.Instances.Settings.SetValue("MGHNickname", mgh.nickname);
Grasshopper.Instances.Settings.SetValue("MGHLangSel", mgh.language);
Grasshopper.Instances.Settings.SetValue("MGHExtras", mgh.extras);
Grasshopper.Instances.Settings.SetValue("MGHTextSize", mgh.size);
Expand All @@ -53,7 +59,7 @@ static internal void EventHandler(GH_Canvas sender, MultilingualInstance mgh)
}
else
{
Translation.CompAdded(sender.Document, 0);
Translation.CompAdded(sender.Document, null);
sender.Document.ObjectsAdded += Translation.CompAdded;
}
}
Expand Down
39 changes: 34 additions & 5 deletions src/MultilingualGH/MultilingualMenu.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
Expand All @@ -12,6 +13,7 @@ public class MultilingualMenu : GH_AssemblyPriority
static MultilingualInstance mgh;
static internal ToolStripMenuItem mghDropdown = new ToolStripMenuItem();
static private ToolStripItemCollection options = null;


public override GH_LoadingInstruction PriorityLoad()
{
Expand Down Expand Up @@ -56,8 +58,8 @@ static void Setup(GH_Canvas canvas)
{
UpdateMenu(mgh);
}

};

}
static void CreateMenu(GH_DocumentEditor docEditor)
{
Expand Down Expand Up @@ -92,6 +94,14 @@ static void SetupMenu()
mgh.keep = !mgh.keep;
keepOption.Checked = mgh.keep;
};
ToolStripMenuItem showEng = new ToolStripMenuItem { Name = "ShowEnglish", Text = UI.ShowEnglish, Checked = mgh.showeng };
showEng.Click += (s, e) =>
{
mgh.showeng = !mgh.showeng;
showEng.Checked = mgh.showeng;
if (!mgh.textLabel) Translation.Clear(canvas.Document);
MultilingualInstance.EventHandler(canvas, mgh);
};
NumericUpDown textSize = new NumericUpDown
{
Minimum = 1,
Expand All @@ -106,6 +116,7 @@ static void SetupMenu()
if (inputSize > textSize.Maximum) inputSize = textSize.Maximum;
else if (inputSize < textSize.Minimum) inputSize = textSize.Minimum;
mgh.size = Convert.ToInt32(inputSize);
canvas.Refresh();
};
textSize.Enabled = mgh.textLabel;
ToolStripControlHost numericSize = new ToolStripControlHost(textSize, "TextSize");
Expand All @@ -126,7 +137,22 @@ static void SetupMenu()
keepOption.Checked = false;
}
mgh.textLabel = !basic;
if (mgh.compGuid == Guid.Empty) MultilingualInstance.EventHandler(canvas, mgh);
MultilingualInstance.EventHandler(canvas, mgh);
};
ToolStripComboBox nicknameOnly = new ToolStripComboBox { Name = "Nickname", DropDownStyle = ComboBoxStyle.DropDownList, FlatStyle = FlatStyle.Flat };
nicknameOnly.Items.AddRange(new object[] {
UI.NoNickname,
UI.NicknamePreferred,
UI.NicknameOnly});
nicknameOnly.SelectedIndex = 0;
nicknameOnly.SelectedIndexChanged += (s, e) =>
{
mgh.nickname = (byte) nicknameOnly.SelectedIndex;
if (mgh.enabled)
{
if(!mgh.textLabel) Translation.Clear(canvas.Document);
MultilingualInstance.EventHandler(canvas, mgh);
}
};
defaultOption.Click += (s, e) =>
{
Expand All @@ -135,7 +161,7 @@ static void SetupMenu()
canvas.Refresh();
defaultOption.Checked = mgh.excludeDefault;
};
ToolStripMenuItem userOption = new ToolStripMenuItem { Name = "User", Text = UI.CustomExclusions, Checked = mgh.excludeUser != string.Empty };
ToolStripMenuItem userOption = new ToolStripMenuItem { Name = "User", Text = UI.CustomExclusions, Checked = mgh.excludeUser != "" };
userOption.Click += (s, e) =>
{
UserForm form = new UserForm();
Expand All @@ -145,7 +171,7 @@ static void SetupMenu()
MultilingualInstance.EventHandler(canvas, mgh);
canvas.Refresh();
}
userOption.Checked = mgh.excludeUser != string.Empty;
userOption.Checked = mgh.excludeUser != "";
};
ToolStripMenuItem saveSettings = new ToolStripMenuItem { Name = "Save", Text = UI.SaveAsDefault, ToolTipText = UI.SaveDeTooltip };
saveSettings.Click += (s, e) =>
Expand All @@ -158,6 +184,7 @@ static void SetupMenu()
$"Default Exclusions: {mgh.excludeDefault}\r\n" +
$"User Exclusions: {mgh.excludeUser.Length > 0}\r\n" +
$"Keep Annotations: {mgh.keep}\r\n" +
$"Show English: {mgh.showeng}\r\n" +
$"Language: {mgh.language}\r\n" +
$"Plugins: {mgh.extras.Length > 0}\r\n");
};
Expand All @@ -167,13 +194,15 @@ static void SetupMenu()
List<ToolStripItem> menuOptions = new List<ToolStripItem> {
toggle,
translationMethod,
nicknameOnly,
numericSize,
new ToolStripSeparator(),
saveSettings,
new ToolStripSeparator(),
defaultOption,
userOption,
keepOption,
showEng,
new ToolStripSeparator(),
langOption
};
Expand Down Expand Up @@ -301,7 +330,7 @@ static internal void UpdateMenu(MultilingualInstance mgh)
}
((NumericUpDown)((ToolStripControlHost)options["TextSize"]).Control).Value = mgh.size;
((ToolStripMenuItem)options["Default"]).Checked = mgh.excludeDefault;
((ToolStripMenuItem)options["User"]).Checked = mgh.excludeUser != string.Empty;
((ToolStripMenuItem)options["User"]).Checked = mgh.excludeUser != "";
bool isEng = mgh.language == "English";
((ToolStripMenuItem)options["English"]).Checked = isEng;
foreach (var lang in Translation.files)
Expand Down
4 changes: 2 additions & 2 deletions src/MultilingualGH/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
// 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.*")]
[assembly: AssemblyVersion("1.3.0.*")]
#pragma warning disable CS7035 // The specified version string does not conform to the recommended format - major.minor.build.revision
[assembly: AssemblyFileVersion("1.2.0.*")]
[assembly: AssemblyFileVersion("1.3.0.*")]
#pragma warning restore CS7035 // The specified version string does not conform to the recommended format - major.minor.build.revision
[assembly: NeutralResourcesLanguage("en")]
76 changes: 44 additions & 32 deletions src/MultilingualGH/Translation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static internal void GetFiles()
var translationDictionary = new Dictionary<string, string>();
string nameOnly = Path.GetFileNameWithoutExtension(file);
ParseFile(file, ref translationDictionary);
if(!nameOnly.StartsWith("UILang"))
if (!nameOnly.StartsWith("UILang"))
{
files.Add(nameOnly);
translations.Add(nameOnly, translationDictionary);
Expand Down Expand Up @@ -125,31 +125,35 @@ static internal void CompAdded(object sender, object e)
var shouldRemove = new ConcurrentDictionary<IGH_DocumentObject, byte>();
var newlyAdded = new ConcurrentDictionary<IGH_DocumentObject, byte>();
var inGroup = new ConcurrentDictionary<IGH_DocumentObject, byte>();
Parallel.ForEach(ghDoc.Objects, docObject =>
{
if (docObject is GH_Group ghGroup)
{
var objectsInGroup = ghGroup.Objects();
if (objectsInGroup.Count == 1 && ghGroup.Description == objectsInGroup[0].Name)
{
if (IsExclusion(objectsInGroup[0], exclusions))
{
shouldRemove.TryAdd(docObject, temp);
}
inGroup.TryAdd(objectsInGroup[0], temp);
newlyAdded.TryRemove(objectsInGroup[0], out temp);
return;
}
else return;
}
else
{
if (IsExclusion(docObject, exclusions) || inGroup.ContainsKey(docObject))
return;
else
newlyAdded.TryAdd(docObject, temp);
}
});
Parallel.ForEach(e is GH_DocObjectEventArgs newObj ? newObj.Objects : ghDoc.Objects, docObject =>
{
if (docObject is GH_Group ghGroup)
{
var objectsInGroup = ghGroup.Objects();
if (objectsInGroup.Count == 1 && ghGroup.Description == objectsInGroup[0].Name)
{
if (IsExclusion(objectsInGroup[0], exclusions))
{
shouldRemove.TryAdd(docObject, temp);
}
inGroup.TryAdd(objectsInGroup[0], temp);
newlyAdded.TryRemove(objectsInGroup[0], out temp);
return;
}
else return;
}
else
{
if (IsExclusion(docObject, exclusions) ||
inGroup.ContainsKey(docObject) ||
(mgh.nickname == 2 &&
(docObject.NickName == Grasshopper.Instances.ComponentServer.EmitObjectProxy(docObject.ComponentGuid).Desc.NickName ||
docObject.NickName == "")))
return;
else
newlyAdded.TryAdd(docObject, temp);
}
});
foreach (var comp in newlyAdded.Keys)
{
GH_Group annotation = new GH_Group
Expand All @@ -173,14 +177,20 @@ static internal void Paint(GH_Canvas sender)
{
var exclusions = ExclusionSetup(mgh);
bool ZUI = GH_Canvas.ZoomFadeHigh == 255;
float size = (float)(ZUI ? mgh.size*0.5 : mgh.size);
float size = (float)(ZUI ? mgh.size * 0.5 : mgh.size);
Font font = new Font("sans-serif", size);
SolidBrush brush = new SolidBrush(Color.Black);
StringFormat alignment = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Far };
foreach (var comp in sender.Document.Objects)
{
RectangleF bnd = comp.Attributes.Bounds;
if (!sender.Viewport.IsVisible(ref bnd, 20) || IsExclusion(comp, exclusions) || comp is GH_Group) continue;
if (!sender.Viewport.IsVisible(ref bnd, 20) ||
IsExclusion(comp, exclusions) ||
comp is GH_Group ||
(mgh.nickname == 2 &&
(comp.NickName == Grasshopper.Instances.ComponentServer.EmitObjectProxy(comp.ComponentGuid).Desc.NickName ||
comp.NickName == "")))
continue;
RectangleF anchor = comp.Attributes.Bounds;
float x = anchor.X + 0.5f * anchor.Width;
float y = anchor.Y - 0.1f * size;
Expand Down Expand Up @@ -217,14 +227,16 @@ static bool IsExclusion(IGH_DocumentObject obj, ConcurrentDictionary<string, byt
}
static string Alias(MultilingualInstance mgh, IGH_DocumentObject comp)
{
if (mgh.language == "English") return comp.Name;
string subfix = mgh.nickname>=1 ? comp.NickName : comp.Name;
if (mgh.language == "English") goto End;
if (translations.TryGetValue(mgh.language, out Dictionary<string, string> index))
if (index.TryGetValue(comp.Name + comp.Category, out string translated) || index.TryGetValue(comp.Name, out translated))
return translated;
return translated + (mgh.showeng ? Environment.NewLine + subfix : "");
if (mgh.extras.Contains(comp.Category) && extraTranslations.TryGetValue(comp.Category, out Dictionary<string, string> eIndex))
if (eIndex.TryGetValue(comp.Name, out string eTranslated))
return eTranslated;
return comp.Name;
return eTranslated + (mgh.showeng ? Environment.NewLine + subfix : "");
End:
return subfix;
}

static internal void Clear(GH_Document ghDoc)
Expand Down
Loading

0 comments on commit 308846a

Please sign in to comment.