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/Description/Types/ChipDescription.cs b/Assets/Scripts/Description/Types/ChipDescription.cs index 1c83b081..f93fac43 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/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/ChipCustomizationMenu.cs b/Assets/Scripts/Graphics/UI/Menus/ChipCustomizationMenu.cs index 959e0c26..42d1fe2e 100644 --- a/Assets/Scripts/Graphics/UI/Menus/ChipCustomizationMenu.cs +++ b/Assets/Scripts/Graphics/UI/Menus/ChipCustomizationMenu.cs @@ -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; @@ -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 hexStringInputValidator = ValidateHexStringInput; @@ -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; 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 1fd8bf26..6d3bbb17 100644 --- a/Assets/Scripts/Graphics/World/DevSceneDrawer.cs +++ b/Assets/Scripts/Graphics/World/DevSceneDrawer.cs @@ -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; @@ -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) { diff --git a/Assets/Scripts/SaveSystem/DescriptionCreator.cs b/Assets/Scripts/SaveSystem/DescriptionCreator.cs index b562b406..33d5cfe7 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); UpdateWireIndicesForDescriptionCreation(chip); @@ -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, 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 3e38f91d..57382ae6 100644 --- a/TestData/Projects/MainTest/ProjectDescription.json +++ b/TestData/Projects/MainTest/ProjectDescription.json @@ -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" }, {