diff --git a/YAFC/Widgets/ObjectTooltip.cs b/YAFC/Widgets/ObjectTooltip.cs index 5f592e1d..62f0a2a9 100644 --- a/YAFC/Widgets/ObjectTooltip.cs +++ b/YAFC/Widgets/ObjectTooltip.cs @@ -432,7 +432,7 @@ private void BuildRecipe(RecipeOrTechnology recipe, ImGui gui) gui.BuildText(DataUtils.FormatAmount(ingredient.amount, UnitOfMeasure.None)); } - gui.allocator = RectAllocator.RemainigRow; + gui.allocator = RectAllocator.RemainingRow; gui.BuildFactorioObjectButtonWithText(technology); } } diff --git a/YAFC/Windows/DependencyExplorer.cs b/YAFC/Windows/DependencyExplorer.cs index 7b38505c..7fa0a78c 100644 --- a/YAFC/Windows/DependencyExplorer.cs +++ b/YAFC/Windows/DependencyExplorer.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.Linq; -using System.Runtime.CompilerServices; using SDL2; using YAFC.Model; using YAFC.UI; @@ -11,8 +10,8 @@ public class DependencyExplorer : PseudoScreen { private static readonly DependencyExplorer Instance = new DependencyExplorer(); - private readonly VerticalScrollCustom dependencies; - private readonly VerticalScrollCustom dependants; + private readonly ScrollArea dependencies; + private readonly ScrollArea dependants; private static readonly Padding listPad = new Padding(0.5f); private readonly List history = new List(); @@ -34,8 +33,8 @@ public class DependencyExplorer : PseudoScreen public DependencyExplorer() : base(60f) { - dependencies = new VerticalScrollCustom(30f, DrawDependencies); - dependants = new VerticalScrollCustom(30f, DrawDependants); + dependencies = new ScrollArea(30f, DrawDependencies); + dependants = new ScrollArea(30f, DrawDependants); } public static void Show(FactorioObject target) diff --git a/YAFC/Windows/ErrorListPanel.cs b/YAFC/Windows/ErrorListPanel.cs index 68f16216..6ad7155e 100644 --- a/YAFC/Windows/ErrorListPanel.cs +++ b/YAFC/Windows/ErrorListPanel.cs @@ -7,12 +7,12 @@ public class ErrorListPanel : PseudoScreen { private static readonly ErrorListPanel Instance = new ErrorListPanel(); private ErrorCollector collector; - private readonly VerticalScrollCustom verticalList; + private readonly ScrollArea verticalList; private (string error, ErrorSeverity severity)[] errors; public ErrorListPanel() : base(60f) { - verticalList = new VerticalScrollCustom(30f, BuildErrorList, default, true); + verticalList = new ScrollArea(30f, BuildErrorList, default, true); } private void BuildErrorList(ImGui gui) diff --git a/YAFC/Windows/NeverEnoughItemsPanel.cs b/YAFC/Windows/NeverEnoughItemsPanel.cs index 5ae0f998..0ca2021e 100644 --- a/YAFC/Windows/NeverEnoughItemsPanel.cs +++ b/YAFC/Windows/NeverEnoughItemsPanel.cs @@ -14,8 +14,8 @@ public class NeverEnoughItemsPanel : PseudoScreen, IComparer recent = new List(); private bool atCurrentMilestones; - private readonly VerticalScrollCustom productionList; - private readonly VerticalScrollCustom usageList; + private readonly ScrollArea productionList; + private readonly ScrollArea usageList; private enum EntryStatus { @@ -62,8 +62,8 @@ public RecipeEntry(Recipe recipe, bool isProduction, Goods currentItem, bool atC public NeverEnoughItemsPanel() : base(76f) { - productionList = new VerticalScrollCustom(40f, BuildItemProduction, new Padding(0.5f)); - usageList = new VerticalScrollCustom(40f, BuildItemUsages, new Padding(0.5f)); + productionList = new ScrollArea(40f, BuildItemProduction, new Padding(0.5f)); + usageList = new ScrollArea(40f, BuildItemUsages, new Padding(0.5f)); } private void SetItem(Goods current) @@ -204,7 +204,7 @@ private void DrawRecipeEntry(ImGui gui, RecipeEntry entry, bool production) if (recipe.products.Length < 3 && recipe.ingredients.Length < 5) gui.AllocateSpacing((3 - entry.recipe.products.Length) * 3f); else if (recipe.products.Length < 3) - gui.allocator = RectAllocator.RemainigRow; + gui.allocator = RectAllocator.RemainingRow; gui.BuildIcon(Icon.ArrowRight, 3f); } } diff --git a/YAFC/Windows/WelcomeScreen.cs b/YAFC/Windows/WelcomeScreen.cs index 4fef0047..debd8d45 100644 --- a/YAFC/Windows/WelcomeScreen.cs +++ b/YAFC/Windows/WelcomeScreen.cs @@ -18,9 +18,9 @@ public class WelcomeScreen : WindowUtility, IProgress<(string, string)> private bool expensive; private string createText; private bool canCreate; - private readonly VerticalScrollCustom errorScroll; - private readonly VerticalScrollCustom recentProjectScroll; - private readonly VerticalScrollCustom languageScroll; + private readonly ScrollArea errorScroll; + private readonly ScrollArea recentProjectScroll; + private readonly ScrollArea languageScroll; private string errorMod; private string errorMessage; private string tip; @@ -69,9 +69,9 @@ public WelcomeScreen() : base(ImGuiUtils.DefaultScreenPadding) RenderingUtils.SetColorScheme(Preferences.Instance.darkMode); var lastProject = Preferences.Instance.recentProjects.FirstOrDefault(); SetProject(lastProject); - errorScroll = new VerticalScrollCustom(20f, BuildError, collapsible:true); - recentProjectScroll = new VerticalScrollCustom(20f, BuildRecentProjectList, collapsible:true); - languageScroll = new VerticalScrollCustom(20f, LanguageSelection, collapsible: true); + errorScroll = new ScrollArea(20f, BuildError, collapsible:true); + recentProjectScroll = new ScrollArea(20f, BuildRecentProjectList, collapsible:true); + languageScroll = new ScrollArea(20f, LanguageSelection, collapsible: true); Create("Welcome to YAFC CE v"+YafcLib.version.ToString(3), 45, null); IconCollection.ClearCustomIcons(); if (tips == null) diff --git a/YAFC/Workspace/ProductionTable/ProductionLinkSummaryScreen.cs b/YAFC/Workspace/ProductionTable/ProductionLinkSummaryScreen.cs index 33289eb6..911b502a 100644 --- a/YAFC/Workspace/ProductionTable/ProductionLinkSummaryScreen.cs +++ b/YAFC/Workspace/ProductionTable/ProductionLinkSummaryScreen.cs @@ -12,22 +12,22 @@ public class ProductionLinkSummaryScreen : PseudoScreen, IComparer<(RecipeRow ro private readonly List<(RecipeRow row, float flow)> input = new List<(RecipeRow, float)>(); private readonly List<(RecipeRow row, float flow)> output = new List<(RecipeRow, float)>(); private float totalInput, totalOutput; - private readonly VerticalScrollCustom scrollArea; + private readonly ScrollArea scrollArea; private ProductionLinkSummaryScreen() { - scrollArea = new VerticalScrollCustom(30, BuildScrollArea); + scrollArea = new ScrollArea(30, BuildScrollArea); } private void BuildScrollArea(ImGui gui) { - gui.BuildText("Production: "+DataUtils.FormatAmount(totalInput, link.goods.flowUnitOfMeasure), Font.subheader); + gui.BuildText("Production: " + DataUtils.FormatAmount(totalInput, link.goods.flowUnitOfMeasure), Font.subheader); BuildFlow(gui, input, totalInput); gui.spacing = 0.5f; - gui.BuildText("Consumption: "+DataUtils.FormatAmount(totalOutput, link.goods.flowUnitOfMeasure), Font.subheader); + gui.BuildText("Consumption: " + DataUtils.FormatAmount(totalOutput, link.goods.flowUnitOfMeasure), Font.subheader); BuildFlow(gui, output, totalOutput); if (link.flags.HasFlags(ProductionLink.Flags.LinkNotMatched) && totalInput != totalOutput) - gui.BuildText((totalInput > totalOutput ? "Overproduction: " : "Overconsumption: ") + DataUtils.FormatAmount(MathF.Abs(totalInput-totalOutput), link.goods.flowUnitOfMeasure), Font.subheader, color:SchemeColor.Error); + gui.BuildText((totalInput > totalOutput ? "Overproduction: " : "Overconsumption: ") + DataUtils.FormatAmount(MathF.Abs(totalInput - totalOutput), link.goods.flowUnitOfMeasure), Font.subheader, color: SchemeColor.Error); } public override void Build(ImGui gui) @@ -71,7 +71,7 @@ private void CalculateFlow(ProductionLink link) { input.Add((recipe, localFlow)); totalInput += localFlow; - } + } else if (localFlow < 0) { output.Add((recipe, -localFlow)); diff --git a/YAFCui/ImGui/ImGui.cs b/YAFCui/ImGui/ImGui.cs index ae05e4dc..836c9ad8 100644 --- a/YAFCui/ImGui/ImGui.cs +++ b/YAFCui/ImGui/ImGui.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Numerics; using System.Runtime.CompilerServices; using SDL2; @@ -47,7 +46,7 @@ public enum RectAllocator Center, LeftRow, RightRow, - RemainigRow, + RemainingRow, FixedRect, HalfRow } @@ -56,17 +55,17 @@ public enum RectAllocator public sealed partial class ImGui : IDisposable, IPanel { - public ImGui(GuiBuilder gui, Padding padding, RectAllocator defaultAllocator = RectAllocator.Stretch, bool clip = false) + public ImGui(GuiBuilder guiBuilder, Padding padding, RectAllocator defaultAllocator = RectAllocator.Stretch, bool clip = false) { - this.gui = gui; - if (gui == null) + this.guiBuilder = guiBuilder; + if (guiBuilder == null) action = ImGuiAction.Build; this.defaultAllocator = defaultAllocator; this.clip = clip; initialPadding = padding; } - public readonly GuiBuilder gui; + public readonly GuiBuilder guiBuilder; public Window window { get; private set; } public ImGui parent { get; private set; } IPanel IPanel.Parent => parent; diff --git a/YAFCui/ImGui/ImGuiBuilding.cs b/YAFCui/ImGui/ImGuiBuilding.cs index d0f74410..cc918a60 100644 --- a/YAFCui/ImGui/ImGuiBuilding.cs +++ b/YAFCui/ImGui/ImGuiBuilding.cs @@ -26,7 +26,7 @@ public void Deconstruct(out Rect rect, out T data, out SchemeColor color) color = this.color; } } - + private readonly List> rects = new List>(); private readonly List> icons = new List>(); private readonly List> renderables = new List>(); @@ -75,10 +75,10 @@ private void ClearDrawCommandList() public void ManualDrawingClear() { - if (gui == null) + if (guiBuilder == null) ClearDrawCommandList(); } - + public readonly ImGuiCache.Cache textCache = new ImGuiCache.Cache(); public FontFile.FontSize GetFontSize(Font font = null) => (font ?? Font.text).GetFontSize(pixelsPerUnit); @@ -108,7 +108,7 @@ public Rect AllocateTextRect(out TextCache cache, string text, Font font = null, } else { - cache = textCache.GetCached((fontSize, text, wrap ? (uint) UnitsToPixels(MathF.Max(width, 5f)) : uint.MaxValue)); + cache = textCache.GetCached((fontSize, text, wrap ? (uint)UnitsToPixels(MathF.Max(width, 5f)) : uint.MaxValue)); rect = AllocateRect(cache.texRect.w / pixelsPerUnit, topOffset + cache.texRect.h / pixelsPerUnit, align); } @@ -141,7 +141,7 @@ public bool BuildTextInput(string text, out string newText, string placeholder, textInputHelper = new ImGuiTextInputHelper(this); return textInputHelper.BuildTextInput(text, out newText, placeholder, GetFontSize(), delayed, icon, padding, alignment, color); } - + public void BuildIcon(Icon icon, float size = 1.5f, SchemeColor color = SchemeColor.None) { if (color == SchemeColor.None) @@ -162,7 +162,7 @@ public void BuildIcon(Icon icon, float size = 1.5f, SchemeColor color = SchemeCo private bool DoGui(ImGuiAction action) { - if (gui == null) + if (guiBuilder == null) return false; this.action = action; ResetLayout(); @@ -170,7 +170,7 @@ private bool DoGui(ImGuiAction action) buildGroupsIndex = -1; using (EnterGroup(initialPadding, defaultAllocator, initialTextColor)) { - gui(this); + guiBuilder(this); } actionParameter = 0; if (action == ImGuiAction.Build) @@ -184,14 +184,14 @@ private bool DoGui(ImGuiAction action) private void BuildGui(float width) { - if (gui == null) + if (guiBuilder == null) return; buildWidth = width; nextRebuildTimer = long.MaxValue; rebuildRequested = false; ClearDrawCommandList(); DoGui(ImGuiAction.Build); - contentSize = new Vector2(buildingWidth, lastRect.Bottom); + contentSize = new Vector2(lastRect.Right, lastRect.Bottom); if (boxColor != SchemeColor.None) { var rect = new Rect(default, contentSize); @@ -252,7 +252,7 @@ public void MouseUp(int button) SDL.SDL_SetCursor(RenderingUtils.cursorHand); cursorSetByMouseDown = false; } - + actionParameter = button; DoGui(ImGuiAction.MouseUp); } @@ -354,7 +354,7 @@ public void SetFocus(Rect rect) mouseDownRect = rect; Rebuild(); } - + public void SetTextInputFocus(Rect rect, string text) { if (textInputHelper != null && InputSystem.Instance.currentKeyboardFocus != textInputHelper) @@ -366,7 +366,6 @@ public void SetTextInputFocus(Rect rect, string text) public void SaveToImage() { - } } } \ No newline at end of file diff --git a/YAFCui/ImGui/ImGuiLayout.cs b/YAFCui/ImGui/ImGuiLayout.cs index 7e21a329..f00e3723 100644 --- a/YAFCui/ImGui/ImGuiLayout.cs +++ b/YAFCui/ImGui/ImGuiLayout.cs @@ -12,7 +12,7 @@ public partial class ImGui public Rect statePosition => new Rect(state.left, state.top, width, 0f); public ref RectAllocator allocator => ref state.allocator; public ref float spacing => ref state.spacing; - public Rect layoutRect => new Rect(state.left, state.top, state.bottom - state.top, state.right - state.left); + public Rect layoutRect => new Rect(state.left, state.top, state.right - state.left, state.bottom - state.top); private void ResetLayout() { @@ -38,7 +38,8 @@ public Rect AllocateRect(float width, float height, float spacing = float.Negati public Rect EncapsulateRect(Rect rect) { - return lastRect = state.EncapsulateRect(rect); + lastRect = state.EncapsulateRect(rect); + return lastRect; } public Rect AllocateRect(float width, float height, RectAlignment alignment, float spacing = float.NegativeInfinity) @@ -54,15 +55,15 @@ public static Rect AlignRect(Rect boundary, RectAlignment alignment, float width switch (alignment) { case RectAlignment.Middle: - return new Rect(boundary.X + (boundary.Width - width) * 0.5f, boundary.Y + (boundary.Height-height) * 0.5f, width, height); + return new Rect(boundary.X + (boundary.Width - width) * 0.5f, boundary.Y + (boundary.Height - height) * 0.5f, width, height); case RectAlignment.MiddleLeft: - return new Rect(boundary.X, boundary.Y + (boundary.Height-height) * 0.5f, width, height); + return new Rect(boundary.X, boundary.Y + (boundary.Height - height) * 0.5f, width, height); case RectAlignment.MiddleRight: - return new Rect(boundary.X, boundary.Y + (boundary.Height-height) * 0.5f, width, height); + return new Rect(boundary.X, boundary.Y + (boundary.Height - height) * 0.5f, width, height); case RectAlignment.UpperCenter: return new Rect(boundary.X + (boundary.Width - width) * 0.5f, boundary.Y, width, height); case RectAlignment.MiddleFullRow: - return new Rect(boundary.X, boundary.Y + (boundary.Height-height) * 0.5f, boundary.Width, height); + return new Rect(boundary.X, boundary.Y + (boundary.Height - height) * 0.5f, boundary.Width, height); default: return boundary; } @@ -71,7 +72,7 @@ public static Rect AlignRect(Rect boundary, RectAlignment alignment, float width public ImGui RemainingRow(float spacing = float.NegativeInfinity) { state.AllocateSpacing(spacing); - allocator = RectAllocator.RemainigRow; + allocator = RectAllocator.RemainingRow; return this; } @@ -102,7 +103,7 @@ public Context EnterFixedPositioning(float width, float height, Padding padding, state.textColor = textColor; return context; } - + private struct CopyableState { public RectAllocator allocator; @@ -121,23 +122,23 @@ public Rect AllocateRect(float width, float height, float spacing) switch (allocator) { case RectAllocator.Stretch: - return new Rect(left, top, right-left, height); + return new Rect(left, top, right - left, height); case RectAllocator.LeftAlign: return new Rect(left, top, width, height); case RectAllocator.RightAlign: - return new Rect(right-width, top, width, height); + return new Rect(right - width, top, width, height); case RectAllocator.Center: - return new Rect((right+left-width) * 0.5f, top, width, height); + return new Rect((right + left - width) * 0.5f, top, width, height); case RectAllocator.LeftRow: return new Rect(left, top, width, rowHeight); case RectAllocator.RightRow: - return new Rect(right-width, top, width, rowHeight); - case RectAllocator.RemainigRow: - return new Rect(left, top, right-left, rowHeight); + return new Rect(right - width, top, width, rowHeight); + case RectAllocator.RemainingRow: + return new Rect(left, top, right - left, rowHeight); case RectAllocator.FixedRect: - return new Rect(left, top, right-left, rowHeight); + return new Rect(left, top, right - left, rowHeight); case RectAllocator.HalfRow: - return new Rect(left, top, (right-left-spacing)/2f, rowHeight); + return new Rect(left, top, (right - left - spacing) / 2f, rowHeight); default: throw new ArgumentOutOfRangeException(); } @@ -166,7 +167,7 @@ public void AllocateSpacing(float amount = float.NegativeInfinity) break; } } - + public Rect EncapsulateRect(Rect rect) { contextRect = hasContent ? Rect.Union(contextRect, rect) : rect; @@ -179,28 +180,28 @@ public Rect EncapsulateRect(Rect rect) rect.Width = right - left; break; case RectAllocator.RightAlign: - top = bottom = MathF.Max(rect.Bottom, top);; + top = bottom = MathF.Max(rect.Bottom, top); rect.Right = right; break; case RectAllocator.LeftAlign: - top = bottom = MathF.Max(rect.Bottom, top);; + top = bottom = MathF.Max(rect.Bottom, top); rect.Left = left; break; case RectAllocator.Center: - top = bottom = MathF.Max(rect.Bottom, top);; + top = bottom = MathF.Max(rect.Bottom, top); break; case RectAllocator.LeftRow: left = rect.Right; - bottom = MathF.Max(rect.Bottom, bottom);; + bottom = MathF.Max(rect.Bottom, bottom); break; case RectAllocator.RightRow: right = rect.Left; - bottom = MathF.Max(rect.Bottom, bottom);; + bottom = MathF.Max(rect.Bottom, bottom); break; case RectAllocator.HalfRow: - allocator = RectAllocator.RemainigRow; + allocator = RectAllocator.RemainingRow; left = rect.Right + spacing; - bottom = MathF.Max(rect.Bottom, bottom);; + bottom = MathF.Max(rect.Bottom, bottom); break; } @@ -237,10 +238,13 @@ public void Dispose() gui.state = state; rect.X -= padding.left; rect.Y -= padding.top; - rect.Width += (padding.left + padding.right); - rect.Height += (padding.top + padding.bottom); + rect.Width += padding.left + padding.right; + rect.Height += padding.top + padding.bottom; if (hasContent) - gui.lastRect = gui.state.EncapsulateRect(rect); + { + gui.state.EncapsulateRect(rect); + gui.lastRect = rect; + } else gui.lastRect = default; } diff --git a/YAFCui/ImGui/ScrollArea.cs b/YAFCui/ImGui/ScrollArea.cs index 82ae7f91..f8659de0 100644 --- a/YAFCui/ImGui/ScrollArea.cs +++ b/YAFCui/ImGui/ScrollArea.cs @@ -77,20 +77,20 @@ public void Build(ImGui gui, float height) { if (horizontal && maxScroll.X > 0f) { - var fullScrollRect = new Rect(rect.X, rect.Bottom-ScrollbarSize, rect.Width, ScrollbarSize); + var fullScrollRect = new Rect(rect.X, rect.Bottom - ScrollbarSize, rect.Width, ScrollbarSize); var scrollRect = new Rect(rect.X + scrollStart.X, fullScrollRect.Y, scrollSize.X, ScrollbarSize); BuildScrollBar(gui, 0, in fullScrollRect, in scrollRect); } if (vertical && maxScroll.Y > 0f) { - var fullScrollRect = new Rect(rect.Right-ScrollbarSize, rect.Y, ScrollbarSize, rect.Height); + var fullScrollRect = new Rect(rect.Right - ScrollbarSize, rect.Y, ScrollbarSize, rect.Height); var scrollRect = new Rect(fullScrollRect.X, rect.Y + scrollStart.Y, ScrollbarSize, scrollSize.Y); BuildScrollBar(gui, 1, in fullScrollRect, in scrollRect); } } } - + private void BuildScrollBar(ImGui gui, int axis, in Rect fullScrollRect, in Rect scrollRect) { switch (gui.action) @@ -132,7 +132,7 @@ public float scroll get => _scroll.Y; set => scroll2d = new Vector2(_scroll.X, value); } - + public float scrollX { get => _scroll.X; @@ -175,17 +175,17 @@ public bool KeyDown(SDL.SDL_Keysym key) public bool TextInput(string input) => false; public bool KeyUp(SDL.SDL_Keysym key) => false; - public void FocusChanged(bool focused) {} + public void FocusChanged(bool focused) { } } - - public abstract class ScrollArea : Scrollable + + public abstract class ScrollAreaBase : Scrollable { - protected readonly ImGui contents; + protected ImGui contents; protected readonly float height; - public ScrollArea(float height, Padding padding, bool collapsible = false, bool vertical = true, bool horizontal = false) : base(vertical, horizontal, collapsible) + public ScrollAreaBase(float height, Padding padding, bool collapsible = false, bool vertical = true, bool horizontal = false) : base(vertical, horizontal, collapsible) { - contents = new ImGui(BuildContents, padding, clip:true); + contents = new ImGui(BuildContents, padding, clip: true); this.height = height; } @@ -197,7 +197,7 @@ protected override void PositionContent(ImGui gui, Rect viewport) public void Build(ImGui gui) => Build(gui, height); protected abstract void BuildContents(ImGui gui); - + public void RebuildContents() { contents.Rebuild(); @@ -209,11 +209,11 @@ protected override Vector2 MeasureContent(Rect rect, ImGui gui) } } - public class VerticalScrollCustom : ScrollArea + public class ScrollArea : ScrollAreaBase { private readonly GuiBuilder builder; - public VerticalScrollCustom(float height, GuiBuilder builder, Padding padding = default, bool collapsible = false) : base(height, padding, collapsible) + public ScrollArea(float height, GuiBuilder builder, Padding padding = default, bool collapsible = false, bool vertical = true, bool horizontal = false) : base(height, padding, collapsible, vertical, horizontal) { this.builder = builder; } @@ -222,7 +222,7 @@ public VerticalScrollCustom(float height, GuiBuilder builder, Padding padding = public void Rebuild() => contents.Rebuild(); } - public class VirtualScrollList : ScrollArea + public class VirtualScrollList : ScrollAreaBase { private readonly Vector2 elementSize; protected readonly int bufferRows;