Skip to content

Hozriontal name alignment #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .vsconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": "1.0",
"components": [
"Microsoft.VisualStudio.Workload.ManagedGame"
]
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Description/Types/ChipDescription.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using UnityEngine;
using System.ComponentModel;


namespace DLS.Description
{
Expand All @@ -12,6 +14,8 @@ public class ChipDescription
// ---- Data ----
public string Name;
public NameDisplayLocation NameLocation;
[DefaultValue(NameAlignment.Centre)]
public NameAlignment NameAlignment;
public ChipType ChipType;
public Vector2 Size;
public Color Colour;
Expand All @@ -33,4 +37,11 @@ public enum NameDisplayLocation
Top,
Hidden
}

public enum NameAlignment
{
Centre,
Right,
Left
}
}
22 changes: 16 additions & 6 deletions Assets/Scripts/Game/Elements/SubChipInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public SubChipInstance(ChipDescription description, SubChipDescription subChipDe
ID = subChipDesc.ID;
Label = subChipDesc.Label;
IsBus = ChipTypeHelper.IsBusType(ChipType);
MultiLineName = CreateMultiLineName(description.Name);
MinSize = CalculateMinChipSize(description.InputPins, description.OutputPins, description.Name);
MultiLineName = CreateMultiLineName(description.Name, description.NameAlignment);
MinSize = CalculateMinChipSize(description.InputPins, description.OutputPins, description.Name, description.NameAlignment);

InputPins = CreatePinInstances(description.InputPins, true);
OutputPins = CreatePinInstances(description.OutputPins, false);
Expand Down Expand Up @@ -281,10 +281,10 @@ Bounds2D CreateBoundingBox(float pad)
return Bounds2D.CreateFromCentreAndSize(Position + Vector2.right * offsetX, Size + padFinal);
}

public static Vector2 CalculateMinChipSize(PinDescription[] inputPins, PinDescription[] outputPins, string unformattedName)
public static Vector2 CalculateMinChipSize(PinDescription[] inputPins, PinDescription[] outputPins, string unformattedName, NameAlignment alignment)
{
float minHeightForPins = MinChipHeightForPins(inputPins, outputPins);
string multiLineName = CreateMultiLineName(unformattedName);
string multiLineName = CreateMultiLineName(unformattedName, alignment);
bool hasMultiLineName = multiLineName != unformattedName;
float minNameHeight = DrawSettings.GridSize * (hasMultiLineName ? 4 : 3);

Expand All @@ -309,7 +309,7 @@ public static float PinHeightFromBitCount(PinBitCount bitCount)
}

// Split chip name into two lines (if contains a space character)
static string CreateMultiLineName(string name)
static string CreateMultiLineName(string name, NameAlignment alignment)
{
// If name is short, or contains no spaces, then just keep on single line
if (name.Length <= 6 || !name.Contains(' ')) return name;
Expand All @@ -333,7 +333,11 @@ static string CreateMultiLineName(string name)
}
}

return PadName(lines, alignment) ;
}

static string PadName(string[] lines, NameAlignment alignment)
{
// Pad lines with spaces to centre justify
string formatted = "";
int longestLine = lines.Max(l => l.Length);
Expand All @@ -342,7 +346,8 @@ static string CreateMultiLineName(string name)
{
string line = lines[i];
int numPadChars = longestLine - line.Length;
int numPadLeft = numPadChars / 2;
int numPadLeft = (alignment == NameAlignment.Centre) ? numPadChars / 2 :
(alignment == NameAlignment.Right) ? numPadChars : 0;
int numPadRight = numPadChars - numPadLeft;
line = line.PadLeft(line.Length + numPadLeft, ' ');
line = line.PadRight(line.Length + numPadRight, ' ');
Expand All @@ -360,6 +365,11 @@ static string CreateMultiLineName(string name)
return formatted;
}

public string GetUpdatedMultilineName()
{
return CreateMultiLineName(Description.Name, Description.NameAlignment);
}

public void FlipBus()
{
if (!IsBus) throw new Exception("Can't flip non-bus type");
Expand Down
14 changes: 12 additions & 2 deletions Assets/Scripts/Graphics/UI/Menus/ChipCustomizationMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ public static class ChipCustomizationMenu
"Name: Hidden"
};


// ---- State ----
static readonly string[] nameAlignmentOptions =
{
"Center Aligned",
"Right Aligned",
"Left Aligned"
};

static SubChipInstance[] subChipsWithDisplays;
static string displayLabelString;
static string colHexCodeString;
Expand All @@ -29,6 +34,7 @@ public static class ChipCustomizationMenu
static readonly UIHandle ID_ColourPicker = new("CustomizeMenu_ChipCol");
static readonly UIHandle ID_ColourHexInput = new("CustomizeMenu_ChipColHexInput");
static readonly UIHandle ID_NameDisplayOptions = new("CustomizeMenu_NameDisplayOptions");
static readonly UIHandle ID_NameAlignmentOptions = new("CustomizeMenu_NameAlignmentOptions");
static readonly UI.ScrollViewDrawElementFunc drawDisplayScrollEntry = DrawDisplayScroll;
static readonly Func<string, bool> hexStringInputValidator = ValidateHexStringInput;

Expand Down Expand Up @@ -61,7 +67,11 @@ public static void DrawMenu()
int nameDisplayMode = UI.WheelSelector(ID_NameDisplayOptions, nameDisplayOptions, NextPos(), new Vector2(pw, DrawSettings.ButtonHeight), theme.OptionsWheel, Anchor.TopLeft);
ChipSaveMenu.ActiveCustomizeDescription.NameLocation = (NameDisplayLocation)nameDisplayMode;

int nameAlignmentMode = UI.WheelSelector(ID_NameAlignmentOptions, nameAlignmentOptions, NextPos(), new Vector2(pw, 3), theme.OptionsWheel, Anchor.TopLeft);
ChipSaveMenu.ActiveCustomizeDescription.NameAlignment = (NameAlignment)nameAlignmentMode;

// ---- Chip colour UI ----

Color newCol = UI.DrawColourPicker(ID_ColourPicker, NextPos(), pw, Anchor.TopLeft);
InputFieldTheme inputTheme = MenuHelper.Theme.ChipNameInputField;
inputTheme.fontSize = MenuHelper.Theme.FontSizeRegular;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Graphics/UI/Menus/ChipSaveMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static void DrawMenu()
if (ActiveCustomizeDescription.Name != newName)
{
ActiveCustomizeDescription.Name = newName;
Vector2 minChipSize = SubChipInstance.CalculateMinChipSize(ActiveCustomizeDescription.InputPins, ActiveCustomizeDescription.OutputPins, newName);
Vector2 minChipSize = SubChipInstance.CalculateMinChipSize(ActiveCustomizeDescription.InputPins, ActiveCustomizeDescription.OutputPins, newName, NameAlignment.Centre);
Vector2 chipSizeNew = Vector2.Max(minChipSize, ActiveCustomizeDescription.Size);
ActiveCustomizeDescription.Size = chipSizeNew;
}
Expand Down
10 changes: 9 additions & 1 deletion Assets/Scripts/Graphics/World/DevSceneDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public static void DrawSubChip(SubChipInstance subchip, SimChip sim = null)
if (isButton || desc.NameLocation != NameDisplayLocation.Hidden)
{
// Display on single line if name fits comfortably, otherwise use 'formatted' version (split across multiple lines)
string displayName = isButton ? subchip.activationKeyString : subchip.MultiLineName;
string displayName = isButton ? subchip.activationKeyString : subchip.GetUpdatedMultilineName();
if (Draw.CalculateTextBoundsSize(subchip.Description.Name, FontSizeChipName, FontBold).x < subchip.Size.x - PinRadius * 2.5f)
{
displayName = subchip.Description.Name;
Expand All @@ -316,6 +316,14 @@ public static void DrawSubChip(SubChipInstance subchip, SimChip sim = null)
Anchor textAnchor = nameCentre ? Anchor.TextCentre : Anchor.CentreTop;
Vector2 textPos = nameCentre ? pos : pos + Vector2.up * (subchip.Size.y / 2 - GridSize / 2);

if (desc.NameAlignment != NameAlignment.Centre)
{
int mult = desc.NameAlignment == NameAlignment.Right ? 1 : -1;
TextRenderer.BoundingBox textBounds = Draw.CalculateTextBounds(displayName, FontBold, FontSizeChipName, textPos, textAnchor);
textPos.x += (pos.x + desc.Size.x / 2 - textBounds.BoundsMax.x) * mult;
}


// Draw background band behind text if placed at top (so it doesn't look out of place..)
if (desc.NameLocation == NameDisplayLocation.Top)
{
Expand Down
3 changes: 2 additions & 1 deletion Assets/Scripts/SaveSystem/DescriptionCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static ChipDescription CreateChipDescription(DevChipInstance chip)
PinDescription[] inputPins = OrderPins(chip.GetInputPins()).Select(CreatePinDescription).ToArray();
PinDescription[] outputPins = OrderPins(chip.GetOutputPins()).Select(CreatePinDescription).ToArray();
SubChipDescription[] subchips = chip.GetSubchips().Select(CreateSubChipDescription).ToArray();
Vector2 minChipsSize = SubChipInstance.CalculateMinChipSize(inputPins, outputPins, name);
Vector2 minChipsSize = SubChipInstance.CalculateMinChipSize(inputPins, outputPins, name, hasSavedDesc ? descOld.NameAlignment : NameAlignment.Centre);
size = Vector2.Max(minChipsSize, size);

UpdateWireIndicesForDescriptionCreation(chip);
Expand All @@ -34,6 +34,7 @@ public static ChipDescription CreateChipDescription(DevChipInstance chip)
{
Name = name,
NameLocation = hasSavedDesc ? descOld.NameLocation : NameDisplayLocation.Centre,
NameAlignment = hasSavedDesc ? descOld.NameAlignment : NameAlignment.Centre,
Size = size,
Colour = col,

Expand Down
7 changes: 4 additions & 3 deletions TestData/Projects/MainTest/Chips/7-SEGMENT DRIVER.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"Name": "7-SEGMENT DRIVER",
"NameLocation": 0,
"NameAlignment": 1,
"ChipType": 0,
"Size": {
"x": 1.7,
"x": 2.23443,
"y": 1.75
},
"Colour": {
Expand Down Expand Up @@ -809,6 +811,5 @@
"Points":[{"x":0.0,"y":0.0},{"x":-2.625,"y":1.5},{"x":-2.625,"y":2.375},{"x":0.0,"y":0.0}]
}
],
"Displays":[],
"ChipType": 0
"Displays":[]
}
2 changes: 1 addition & 1 deletion TestData/Projects/MainTest/ProjectDescription.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
},
{
"Chips":["D-LATCH","FLIP-FLOP","OR-8","MEM-1","NOT-8","AND(8,1)","MUX-8","PC","BUF-8","ALU-8","DECODE-3","AND-3","CONTROL UNIT","TOGGLE","FLAGS","DISP-7","demo","7-SEGMENT DRIVER","DABBLE","LSB","LSHIFT-8","DOUBLE DABBLE","ALU","BUS BUFFER","MEM-256","REGISTER-8","XNOR","EQUALS-8","ADDER-4","DECODER-2","ADDER-8","ADDER","MEM-16","REGISTER-1","AND-8","RAM-256×8 (async)","ROM 256×16"],
"IsToggledOpen":false,
"IsToggledOpen":true,
"Name":"KEEP"
},
{
Expand Down