Skip to content

Commit

Permalink
Merge branch 'bugfix/GSAGH-561-Contour-legend-in-Rhino-is-cropped' in…
Browse files Browse the repository at this point in the history
…to refactorContourClasses
  • Loading branch information
DominikaLos committed Dec 5, 2024
2 parents 37f6af6 + 0e0fbf4 commit 767ed9d
Show file tree
Hide file tree
Showing 11 changed files with 531 additions and 40 deletions.
2 changes: 1 addition & 1 deletion GsaGH/GsaGH.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<RepositoryUrl>https://github.com/arup-group/GSA-Grasshopper</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>GSALogo128.png</PackageIcon>
<Version>1.5.0</Version>
<Version>1.5.1</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
<LangVersion>8.0</LangVersion>
Expand Down
2 changes: 1 addition & 1 deletion GsaGH/GsaGHInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal readonly struct GsaVersionRequired {
public const string ProductName = "GSA";
internal const string TermsConditions
= "Oasys terms and conditions apply. See https://www.oasys-software.com/terms-conditions for details. ";
internal const string GrasshopperVersion = "1.5.0";
internal const string GrasshopperVersion = "1.5.1";

internal static string disclaimer = $"{PluginName} is pre-release and under active development, "
+ "including further testing to be undertaken. It is provided \"as-is\" and you bear the risk of using it. "
Expand Down
33 changes: 8 additions & 25 deletions GsaGH/Helpers/GH/Legend/ContourLegend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
namespace GsaGH.Helpers.GH {

internal class ContourLegend : IContourLegend {
private readonly IContourLegendConfiguration _configuration;

private const int DefaultTextHeight = 12;
private const int DefaultBitmapWidth = 110;

private int _textHeight = DefaultTextHeight;
private readonly IContourLegendConfiguration _configuration;
private int _leftBitmapEdge;
private bool _isDrawLegendCalled = false;
private int _textHeight = DefaultTextHeight;
internal bool IsInvalidConfiguration = false; //only for tests

public ContourLegend(IContourLegendConfiguration configuration) {
_configuration = configuration;
Expand All @@ -31,10 +29,10 @@ public void DrawLegendRectangle(
IGH_PreviewArgs args, string title, string bottomText,
List<(int startY, int endY, Color gradientColor)> gradients) {
if (!_configuration.IsLegendDisplayable()) {
IsInvalidConfiguration = true;
return;
}

_isDrawLegendCalled = true;
InitializeDimensions(args.Viewport.Bounds.Right);

// Step 1: Apply all gradients to the bitmap
Expand All @@ -47,8 +45,6 @@ public void DrawLegendRectangle(
DrawTitle(args, title);
DrawValues(args);
DrawBottomText(args, bottomText);

_isDrawLegendCalled = false;
}

public void DrawGradientLegend(int startY, int endY, Color gradientColor) {
Expand All @@ -63,24 +59,21 @@ public void DrawGradientLegend(int startY, int endY, Color gradientColor) {
}
}

public void DrawBitmap(IGH_PreviewArgs args) {
EnsureDimensionsInitialized(args);
private void DrawBitmap(IGH_PreviewArgs args) {
const int TopOffset = 20;
int topPosition = CalculateScaledOffset(TopOffset);

args.Display.DrawBitmap(new DisplayBitmap(_configuration.Bitmap), _leftBitmapEdge, topPosition);
}

public void DrawTitle(IGH_PreviewArgs args, string title) {
EnsureDimensionsInitialized(args);
private void DrawTitle(IGH_PreviewArgs args, string title) {
const int TopOffset = 7;
int topPosition = CalculateScaledOffset(TopOffset);

args.Display.Draw2dText(title, Color.Black, new Point2d(_leftBitmapEdge, topPosition), false, _textHeight);
}

public void DrawValues(IGH_PreviewArgs args) {
EnsureDimensionsInitialized(args);
private void DrawValues(IGH_PreviewArgs args) {
const int LeftOffset = 25;
int leftEdge = _leftBitmapEdge + CalculateScaledOffset(LeftOffset);
var zippedLists = _configuration.Values.Zip(_configuration.ValuePositionsY, (value, positionY) => new {
Expand All @@ -92,8 +85,7 @@ public void DrawValues(IGH_PreviewArgs args) {
}
}

public void DrawBottomText(IGH_PreviewArgs args, string bottomText) {
EnsureDimensionsInitialized(args);
private void DrawBottomText(IGH_PreviewArgs args, string bottomText) {
const int BottomOffset = 145;
const int ExtraOffset = 20;
int bitmapWidth = CalculateScaledOffset(DefaultBitmapWidth);
Expand All @@ -109,15 +101,6 @@ private void InitializeDimensions(int viewportEdge) {
_leftBitmapEdge = viewportEdge - CalculateScaledOffset(DefaultBitmapWidth);
}

/// <summary>
/// Ensures dimensions are initialized for individual drawing calls.
/// </summary>
private void EnsureDimensionsInitialized(IGH_PreviewArgs args) {
if (!_isDrawLegendCalled) {
InitializeDimensions(args.Viewport.Bounds.Right);
}
}

private string WrapText(string bottomText, int width) {
return TextWrapper.WrapText(bottomText, width, _textHeight);
}
Expand Down
16 changes: 11 additions & 5 deletions GsaGH/Helpers/GH/Legend/ContourLegendConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ public class ContourLegendConfiguration : IContourLegendConfiguration {
public double Scale { get; private set; } = 1.0;
public bool IsVisible { get; private set; } = true;

private const int DefaultWidth = 15;
private const int DefaultHeight = 120;
private const string ScaleKey = "legendScale";
private const string VisibilityKey = "legend";
public readonly int DefaultWidth = 15;
public readonly int DefaultHeight = 120;
/// <summary>
/// Key used to de/serialise scale of the legend
/// </summary>
public static string ScaleKey => "legendScale";
/// <summary>
/// Key used to de/serialise visibility of the legend
/// </summary>
public static string VisibilityKey => "legend";

public ContourLegendConfiguration() {
ScaleBitmap();
Expand Down Expand Up @@ -56,7 +62,7 @@ private void ScaleBitmap() {
}

public bool IsLegendDisplayable() {
return Values.Any() && IsVisible;
return Values.Any() && ValuePositionsY.Any() && IsVisible;
}

/// <summary>
Expand Down
13 changes: 5 additions & 8 deletions GsaGH/Helpers/GH/TextWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static string WrapText(string text, int maxWidth, int fontSize) {

if (testLineWidth > maxWidth) {
lines.Add(currentLine);
textWidthCache[(text, font.Size)] = testLineWidth;
currentLine = word;
} else {
currentLine = testLine;
Expand Down Expand Up @@ -63,21 +64,17 @@ private static float GetCachedTextWidth(string text, Font font, int maxWidth) {

var graphics = System.Drawing.Graphics.FromImage(new Bitmap(maxWidth, 1)); //we care only about width
float width = graphics.MeasureString(text, font).Width;
textWidthCache[(text, font.Size)] = width;

return width;
}

public static string GetFontName() {
string fontName = "Arial";
public static string GetFontName(RhinoDoc testPurpose = null) {
try {
RhinoDoc activeRhino = RhinoDoc.ActiveDoc;
fontName = activeRhino.DimStyles.Current.Font.LogfontName;
RhinoDoc activeRhino = testPurpose ?? RhinoDoc.ActiveDoc;
return activeRhino.DimStyles.Current.Font.FamilyName;
} catch { //will be catched only by tests
return fontName;
return "Arial";
}

return fontName;
}
}
}
1 change: 1 addition & 0 deletions GsaGHTests/GsaGHTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<None Remove="TestHelpers\Steel_Design_Complex.gwb.lok" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Rhino.Inside" Version="7.0.0" />
<PackageReference Include="xunit" Version="2.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
Expand Down
Loading

0 comments on commit 767ed9d

Please sign in to comment.