From 771e3a8917aefbd100c28aa5427534a73ab269fc Mon Sep 17 00:00:00 2001 From: firecerne <105079722+firecerne@users.noreply.github.com> Date: Tue, 15 Apr 2025 23:12:56 +0200 Subject: [PATCH 1/2] Hozriontal name alignment Adds the customization option of aligning names horizontaly. It is fully compatible with save files that don't have the functionnality. --- Assets/Scripts/Description/Types/ChipDescription.cs | 11 +++++++++++ .../Graphics/UI/Menus/ChipCustomizationMenu.cs | 11 +++++++++++ Assets/Scripts/Graphics/World/DevSceneDrawer.cs | 8 ++++++++ 3 files changed, 30 insertions(+) diff --git a/Assets/Scripts/Description/Types/ChipDescription.cs b/Assets/Scripts/Description/Types/ChipDescription.cs index 4b8637f8..c60d9bd1 100644 --- a/Assets/Scripts/Description/Types/ChipDescription.cs +++ b/Assets/Scripts/Description/Types/ChipDescription.cs @@ -1,5 +1,7 @@ using System; using UnityEngine; +using System.ComponentModel; + namespace DLS.Description { @@ -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; @@ -33,4 +37,11 @@ public enum NameDisplayLocation Top, Hidden } + + public enum NameAlignment + { + Centre, + Right, + Left + } } \ No newline at end of file diff --git a/Assets/Scripts/Graphics/UI/Menus/ChipCustomizationMenu.cs b/Assets/Scripts/Graphics/UI/Menus/ChipCustomizationMenu.cs index 9f4c6421..e8139a56 100644 --- a/Assets/Scripts/Graphics/UI/Menus/ChipCustomizationMenu.cs +++ b/Assets/Scripts/Graphics/UI/Menus/ChipCustomizationMenu.cs @@ -24,12 +24,20 @@ public static class ChipCustomizationMenu "Name: Hidden" }; + static readonly string[] nameAlignmentOptions = + { + "Center Aligned", + "Right Aligned", + "Left Aligned" + }; + static SubChipInstance[] subChipsWithDisplays; static string displayLabelString; static readonly UIHandle ID_DisplaysScrollView = new("CustomizeMenu_DisplaysScroll"); static readonly UIHandle ID_ColourPicker = new("CustomizeMenu_ChipCol"); static readonly UIHandle ID_NameDisplayOptions = new("CustomizeMenu_NameDisplayOptions"); + static readonly UIHandle ID_NameAlignmentOptions = new("CustomizeMenu_NameAlignmentOptions"); static readonly UI.ScrollViewDrawElementFunc drawDisplayScrollEntry = DrawDisplayScroll; public static void OnMenuOpened() @@ -65,6 +73,9 @@ public static void DrawMenu() int nameDisplayMode = UI.WheelSelector(ID_NameDisplayOptions, nameDisplayOptions, NextPos(), new Vector2(pw, 3), 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; + Color newCol = UI.DrawColourPicker(ID_ColourPicker, NextPos(), pw, Anchor.TopLeft); ChipSaveMenu.ActiveCustomizeDescription.Colour = newCol; diff --git a/Assets/Scripts/Graphics/World/DevSceneDrawer.cs b/Assets/Scripts/Graphics/World/DevSceneDrawer.cs index cfd832a2..6147fe39 100644 --- a/Assets/Scripts/Graphics/World/DevSceneDrawer.cs +++ b/Assets/Scripts/Graphics/World/DevSceneDrawer.cs @@ -301,6 +301,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) { From c491d343bd663d5696ac949f36c3861652a021c5 Mon Sep 17 00:00:00 2001 From: firecerne <105079722+firecerne@users.noreply.github.com> Date: Wed, 16 Apr 2025 00:16:27 +0200 Subject: [PATCH 2/2] Appropriate padding depending on name alignment Adds padding depending on the name alignment --- .vsconfig | 6 +++++ .../Scripts/Game/Elements/SubChipInstance.cs | 22 ++++++++++++++----- .../Scripts/Graphics/UI/Menus/ChipSaveMenu.cs | 2 +- .../Scripts/Graphics/World/DevSceneDrawer.cs | 2 +- .../Scripts/SaveSystem/DescriptionCreator.cs | 3 ++- .../MainTest/Chips/7-SEGMENT DRIVER.json | 7 +++--- .../Projects/MainTest/ProjectDescription.json | 6 ++--- 7 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 .vsconfig diff --git a/.vsconfig b/.vsconfig new file mode 100644 index 00000000..f019fd0a --- /dev/null +++ b/.vsconfig @@ -0,0 +1,6 @@ +{ + "version": "1.0", + "components": [ + "Microsoft.VisualStudio.Workload.ManagedGame" + ] +} diff --git a/Assets/Scripts/Game/Elements/SubChipInstance.cs b/Assets/Scripts/Game/Elements/SubChipInstance.cs index 6baa58ad..acbf79f1 100644 --- a/Assets/Scripts/Game/Elements/SubChipInstance.cs +++ b/Assets/Scripts/Game/Elements/SubChipInstance.cs @@ -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); @@ -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); @@ -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; @@ -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); @@ -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, ' '); @@ -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"); diff --git a/Assets/Scripts/Graphics/UI/Menus/ChipSaveMenu.cs b/Assets/Scripts/Graphics/UI/Menus/ChipSaveMenu.cs index 53b191ea..e80d215d 100644 --- a/Assets/Scripts/Graphics/UI/Menus/ChipSaveMenu.cs +++ b/Assets/Scripts/Graphics/UI/Menus/ChipSaveMenu.cs @@ -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; } diff --git a/Assets/Scripts/Graphics/World/DevSceneDrawer.cs b/Assets/Scripts/Graphics/World/DevSceneDrawer.cs index 6147fe39..3c50c469 100644 --- a/Assets/Scripts/Graphics/World/DevSceneDrawer.cs +++ b/Assets/Scripts/Graphics/World/DevSceneDrawer.cs @@ -291,7 +291,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; diff --git a/Assets/Scripts/SaveSystem/DescriptionCreator.cs b/Assets/Scripts/SaveSystem/DescriptionCreator.cs index 542fbcf5..31829515 100644 --- a/Assets/Scripts/SaveSystem/DescriptionCreator.cs +++ b/Assets/Scripts/SaveSystem/DescriptionCreator.cs @@ -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); // Store wire's current index in wire for convenient access @@ -38,6 +38,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, diff --git a/TestData/Projects/MainTest/Chips/7-SEGMENT DRIVER.json b/TestData/Projects/MainTest/Chips/7-SEGMENT DRIVER.json index 16595c77..da700748 100644 --- a/TestData/Projects/MainTest/Chips/7-SEGMENT DRIVER.json +++ b/TestData/Projects/MainTest/Chips/7-SEGMENT DRIVER.json @@ -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": { @@ -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":[] } \ No newline at end of file diff --git a/TestData/Projects/MainTest/ProjectDescription.json b/TestData/Projects/MainTest/ProjectDescription.json index d6a80e9a..82f992b6 100644 --- a/TestData/Projects/MainTest/ProjectDescription.json +++ b/TestData/Projects/MainTest/ProjectDescription.json @@ -1,9 +1,9 @@ { "ProjectName": "MainTest", - "DLSVersion_LastSaved": "2.0.4", + "DLSVersion_LastSaved": "2.1.0", "DLSVersion_EarliestCompatible": "2.0.0", "CreationTime": "2025-03-14T18:23:30.404+01:00", - "LastSaveTime": "2025-04-15T00:40:26.898+02:00", + "LastSaveTime": "2025-04-16T00:14:12.658+02:00", "Prefs_MainPinNamesDisplayMode": 0, "Prefs_ChipPinNamesDisplayMode": 1, "Prefs_SimPaused": false, @@ -109,7 +109,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" }, {