Skip to content

Commit

Permalink
Add a dark mode checkbox to the preferences. (#333)
Browse files Browse the repository at this point in the history
The obvious change in Yafc/Windows/PreferencesScreen.cs is about 80% of
the job, but with just that, the pseudoscreen shadows disappear until
you close the main window. (Either by exiting or returning to the
Welcome window.) The rest of the work links the style change to the
surface replacement, so the shadows reappear. (And the background
changes color properly.)
  • Loading branch information
shpaass authored Oct 28, 2024
2 parents 15b89fb + 7509db3 commit 812dc48
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Yafc.UI/Core/Ui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,10 @@ public static void CloseWidowOfType(Type type) {
}
}
}

internal static void ColorSchemeChanged() {
foreach (Window window in windows.Values) {
window.DarkModeChanged();
}
}
}
2 changes: 2 additions & 0 deletions Yafc.UI/Core/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public void SetNextRepaint(long nextRepaintTime) {
}
}

internal virtual void DarkModeChanged() { }

public void ShowTooltip(Tooltip tooltip) {
this.tooltip = tooltip;
Rebuild();
Expand Down
12 changes: 10 additions & 2 deletions Yafc.UI/Core/WindowMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
namespace Yafc.UI;

// Main window is resizable and hardware-accelerated unless forced to render via software by caller
public abstract class WindowMain(Padding padding) : Window(padding) {
protected void Create(string title, int display, float initialWidth, float initialHeight, bool maximized, bool forceSoftwareRenderer) {
public abstract class WindowMain(Padding padding, bool forceSoftwareRenderer) : Window(padding) {
protected void Create(string title, int display, float initialWidth, float initialHeight, bool maximized) {
if (visible) {
return;
}
Expand Down Expand Up @@ -38,6 +38,14 @@ protected void Create(string title, int display, float initialWidth, float initi
base.Create();
}

internal override void DarkModeChanged() {
if (surface != null) {
// Replace surface to (1) replace circleTexture, which is used for drawing shadows, and (2) invalidate the faded background from FadeDrawer.
surface.Dispose();
surface = new MainWindowDrawingSurface(this, forceSoftwareRenderer);
}
}

protected override void BuildContents(ImGui gui) {
BuildContent(gui);
gui.SetContextRect(new Rect(default, size));
Expand Down
1 change: 1 addition & 0 deletions Yafc.UI/Rendering/RenderingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static class RenderingUtils {
public static void SetColorScheme(bool darkMode) {
RenderingUtils.darkMode = darkMode;
SchemeColors = darkMode ? DarkModeScheme : LightModeScheme;
Ui.ColorSchemeChanged();
byte col = darkMode ? (byte)0 : (byte)255;
_ = SDL.SDL_SetSurfaceColorMod(CircleSurface, col, col, col);
}
Expand Down
4 changes: 2 additions & 2 deletions Yafc/Windows/MainScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public partial class MainScreen : WindowMain, IKeyboardFocus, IProgress<(string,
private readonly Dictionary<Type, ProjectPageView> registeredPageViews = [];
private readonly Dictionary<Type, ProjectPageView> secondaryPageViews = [];

public MainScreen(int display, Project project) : base(default) {
public MainScreen(int display, Project project) : base(default, Preferences.Instance.forceSoftwareRenderer) {
summaryView = new SummaryView(project);
RegisterPageView<ProductionTable>(new ProductionTableView());
RegisterPageView<AutoPlanner>(new AutoPlannerView());
Expand All @@ -58,7 +58,7 @@ public MainScreen(int display, Project project) : base(default) {
allPages = new VirtualScrollList<ProjectPage>(30, new Vector2(0f, 2f), BuildPage, collapsible: true);

Create("Yet Another Factorio Calculator CE v" + YafcLib.version.ToString(3), display, Preferences.Instance.initialMainScreenWidth,
Preferences.Instance.initialMainScreenHeight, Preferences.Instance.maximizeMainScreen, Preferences.Instance.forceSoftwareRenderer);
Preferences.Instance.initialMainScreenHeight, Preferences.Instance.maximizeMainScreen);
SetProject(project);
}

Expand Down
6 changes: 6 additions & 0 deletions Yafc/Windows/PreferencesScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ public override void Build(ImGui gui) {
}
}
}

if (gui.BuildCheckBox("Dark mode", Preferences.Instance.darkMode, out bool newValue)) {
Preferences.Instance.darkMode = newValue;
Preferences.Instance.Save();
RenderingUtils.SetColorScheme(newValue);
}
}

gui.AllocateSpacing();
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Version: 2.x
Date:
Features:
- Focus the built count and fixed count edit boxes when first opening them.
- Add a dark mode checkbox to the preferences.
Bugfixes:
- Recipes no longer have excessive question marks in their names
- Moved mining and research bonuses have to the Preferences screen.
Expand Down

0 comments on commit 812dc48

Please sign in to comment.