Skip to content

Commit f9d0260

Browse files
committed
Image Background
1 parent 242c4d5 commit f9d0260

38 files changed

Lines changed: 389 additions & 135 deletions

Bloxstrap/Enums/BackgroundMode.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Bloxstrap.Enums
2+
{
3+
public enum BackgroundMode
4+
{
5+
[EnumName(StaticName = "Gradient")]
6+
Gradient,
7+
8+
[EnumName(StaticName = "Image")]
9+
Image
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Bloxstrap.Enums
2+
{
3+
public enum BackgroundStretch
4+
{
5+
[EnumName(StaticName = "None")]
6+
None,
7+
8+
[EnumName(StaticName = "Fill")]
9+
Fill,
10+
11+
[EnumName(StaticName = "Uniform")]
12+
Uniform,
13+
14+
[EnumName(StaticName = "Uniform To Fill")]
15+
UniformToFill
16+
}
17+
}

Bloxstrap/Models/GradientStops.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
public class GradientStops
44
{
55
public double Offset { get; set; } = 1.0;
6-
public string Color { get; set; } = "#3A7A3F";
6+
public string Color { get; set; } = "#000000";
77
}
88
}

Bloxstrap/Models/Persistable/Settings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public class Settings
6868
new GradientStops { Offset = 1.0, Color = "#252A30" }
6969
};
7070
public double GradientAngle { get; set; } = 0;
71+
public BackgroundMode BackgroundType { get; set; } = BackgroundMode.Gradient;
72+
public string? BackgroundImagePath { get; set; }
73+
public BackgroundStretch BackgroundStretch { get; set; } = BackgroundStretch.UniformToFill;
74+
public double BackgroundOpacity { get; set; } = 1.0;
7175
public string BootstrapperTitle { get; set; } = App.ProjectName;
7276
public string BootstrapperIconCustomLocation { get; set; } = "";
7377
public string DownloadingStringFormat { get; set; } = Strings.Bootstrapper_Status_Downloading + " {0} - {1}MB / {2}MB";

Bloxstrap/UI/Elements/About/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
ExtendsContentIntoTitleBar="True"
1717
WindowBackdropType="Mica"
1818
WindowStartupLocation="CenterScreen">
19-
<Grid Background="{DynamicResource WindowBackgroundGradient}">
19+
<Grid Background="{DynamicResource ApplicationBackground}">
2020
<Grid.RowDefinitions>
2121
<RowDefinition Height="Auto" />
2222
<RowDefinition Height="*" />

Bloxstrap/UI/Elements/AccountManager/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
ExtendsContentIntoTitleBar="True"
1717
WindowBackdropType="Mica"
1818
WindowStartupLocation="CenterScreen">
19-
<Grid Background="{DynamicResource WindowBackgroundGradient}">
19+
<Grid Background="{DynamicResource ApplicationBackground}">
2020
<Grid.RowDefinitions>
2121
<RowDefinition Height="Auto" />
2222
<RowDefinition Height="*" />

Bloxstrap/UI/Elements/Base/WpfUiWindow.cs

Lines changed: 105 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Windows;
22
using System.Windows.Interop;
33
using System.Windows.Media;
4+
using System.Windows.Media.Imaging;
45
using Wpf.Ui.Appearance;
56
using Wpf.Ui.Controls;
67
using Wpf.Ui.Mvvm.Contracts;
@@ -26,55 +27,26 @@ public void ApplyTheme()
2627
_themeService.SetTheme(finalTheme == Enums.Theme.Light ? ThemeType.Light : ThemeType.Dark);
2728
_themeService.SetSystemAccent();
2829

29-
if (finalTheme == Enums.Theme.Custom)
30-
{
31-
Application.Current.Resources["WindowBackgroundGradient"] = null;
32-
33-
double angle = App.Settings.Prop.GradientAngle;
34-
double angleRad = angle * Math.PI / 180.0;
30+
Application.Current.Resources["ApplicationBackground"] = null;
3531

36-
double startX = 0.5 + 0.5 * Math.Cos(angleRad + Math.PI);
37-
double startY = 0.5 + 0.5 * Math.Sin(angleRad + Math.PI);
38-
double endX = 0.5 + 0.5 * Math.Cos(angleRad);
39-
double endY = 0.5 + 0.5 * Math.Sin(angleRad);
32+
this.Background = null;
4033

41-
var customBrush = new LinearGradientBrush
34+
if (finalTheme == Enums.Theme.Custom)
35+
{
36+
if (App.Settings.Prop.BackgroundType == BackgroundMode.Gradient)
4237
{
43-
StartPoint = new Point(startX, startY),
44-
EndPoint = new Point(endX, endY)
45-
};
46-
47-
foreach (var stop in App.Settings.Prop.CustomGradientStops.OrderBy(s => s.Offset))
38+
ApplyGradientBackground();
39+
}
40+
else if (App.Settings.Prop.BackgroundType == BackgroundMode.Image)
4841
{
49-
try
50-
{
51-
var color = (Color)ColorConverter.ConvertFromString(stop.Color);
52-
customBrush.GradientStops.Add(new GradientStop(color, stop.Offset));
53-
}
54-
catch { }
42+
ApplyImageBackground();
5543
}
5644

57-
Application.Current.Resources["WindowBackgroundGradient"] = customBrush;
58-
59-
Application.Current.Resources["NewTextEditorBackground"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#59000000"));
60-
Application.Current.Resources["NewTextEditorForeground"] = new SolidColorBrush(Colors.White);
61-
Application.Current.Resources["NewTextEditorLink"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#3A9CEA"));
62-
Application.Current.Resources["PrimaryBackgroundColor"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#19000000"));
63-
Application.Current.Resources["NormalDarkAndLightBackground"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0FFFFFFF"));
64-
Application.Current.Resources["ControlFillColorDefault"] = (Color)ColorConverter.ConvertFromString("#19000000");
45+
ApplyCustomThemeResources();
6546
}
6647
else
6748
{
68-
var dict = new ResourceDictionary { Source = new Uri($"pack://application:,,,/UI/Style/{Enum.GetName(finalTheme)}.xaml") };
69-
Application.Current.Resources.MergedDictionaries[customThemeIndex] = dict;
70-
71-
Application.Current.Resources["WindowBackgroundGradient"] = null;
72-
Application.Current.Resources.Remove("NewTextEditorBackground");
73-
Application.Current.Resources.Remove("NewTextEditorForeground");
74-
Application.Current.Resources.Remove("NewTextEditorLink");
75-
Application.Current.Resources.Remove("PrimaryBackgroundColor");
76-
Application.Current.Resources.Remove("NormalDarkAndLightBackground");
77-
Application.Current.Resources.Remove("ControlFillColorDefault");
49+
ApplyStandardTheme(finalTheme, customThemeIndex);
7850
}
7951

8052
#if QA_BUILD
@@ -83,6 +55,99 @@ public void ApplyTheme()
8355
#endif
8456
}
8557

58+
private void ApplyGradientBackground()
59+
{
60+
double angle = App.Settings.Prop.GradientAngle;
61+
double angleRad = angle * Math.PI / 180.0;
62+
63+
double startX = 0.5 + 0.5 * Math.Cos(angleRad + Math.PI);
64+
double startY = 0.5 + 0.5 * Math.Sin(angleRad + Math.PI);
65+
double endX = 0.5 + 0.5 * Math.Cos(angleRad);
66+
double endY = 0.5 + 0.5 * Math.Sin(angleRad);
67+
68+
var customBrush = new LinearGradientBrush
69+
{
70+
StartPoint = new Point(startX, startY),
71+
EndPoint = new Point(endX, endY)
72+
};
73+
74+
foreach (var stop in App.Settings.Prop.CustomGradientStops.OrderBy(s => s.Offset))
75+
{
76+
try
77+
{
78+
var color = (Color)ColorConverter.ConvertFromString(stop.Color);
79+
customBrush.GradientStops.Add(new GradientStop(color, stop.Offset));
80+
}
81+
catch { }
82+
}
83+
84+
Application.Current.Resources["ApplicationBackground"] = customBrush;
85+
}
86+
87+
private void ApplyImageBackground()
88+
{
89+
if (string.IsNullOrEmpty(App.Settings.Prop.BackgroundImagePath) || !File.Exists(App.Settings.Prop.BackgroundImagePath))
90+
{
91+
App.Settings.Prop.BackgroundType = BackgroundMode.Gradient;
92+
ApplyTheme();
93+
return;
94+
}
95+
96+
try
97+
{
98+
var imageSource = new BitmapImage();
99+
imageSource.BeginInit();
100+
imageSource.CacheOption = BitmapCacheOption.OnLoad;
101+
imageSource.UriSource = new Uri(App.Settings.Prop.BackgroundImagePath);
102+
imageSource.EndInit();
103+
imageSource.Freeze();
104+
105+
var imageBrush = new ImageBrush
106+
{
107+
ImageSource = imageSource,
108+
Stretch = App.Settings.Prop.BackgroundStretch switch
109+
{
110+
BackgroundStretch.None => Stretch.None,
111+
BackgroundStretch.Fill => Stretch.Fill,
112+
BackgroundStretch.Uniform => Stretch.Uniform,
113+
BackgroundStretch.UniformToFill => Stretch.UniformToFill,
114+
_ => Stretch.UniformToFill
115+
},
116+
Opacity = App.Settings.Prop.BackgroundOpacity
117+
};
118+
119+
Application.Current.Resources["ApplicationBackground"] = imageBrush;
120+
}
121+
catch (Exception)
122+
{
123+
App.Settings.Prop.BackgroundType = BackgroundMode.Gradient;
124+
ApplyTheme();
125+
}
126+
}
127+
128+
private void ApplyCustomThemeResources()
129+
{
130+
Application.Current.Resources["NewTextEditorBackground"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#59000000"));
131+
Application.Current.Resources["NewTextEditorForeground"] = new SolidColorBrush(Colors.White);
132+
Application.Current.Resources["NewTextEditorLink"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#3A9CEA"));
133+
Application.Current.Resources["PrimaryBackgroundColor"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#19000000"));
134+
Application.Current.Resources["NormalDarkAndLightBackground"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0FFFFFFF"));
135+
Application.Current.Resources["ControlFillColorDefault"] = (Color)ColorConverter.ConvertFromString("#19000000");
136+
}
137+
138+
private void ApplyStandardTheme(Enums.Theme finalTheme, int customThemeIndex)
139+
{
140+
var dict = new ResourceDictionary { Source = new Uri($"pack://application:,,,/UI/Style/{Enum.GetName(finalTheme)}.xaml") };
141+
Application.Current.Resources.MergedDictionaries[customThemeIndex] = dict;
142+
143+
Application.Current.Resources.Remove("NewTextEditorBackground");
144+
Application.Current.Resources.Remove("NewTextEditorForeground");
145+
Application.Current.Resources.Remove("NewTextEditorLink");
146+
Application.Current.Resources.Remove("PrimaryBackgroundColor");
147+
Application.Current.Resources.Remove("NormalDarkAndLightBackground");
148+
Application.Current.Resources.Remove("ControlFillColorDefault");
149+
}
150+
86151
protected override void OnSourceInitialized(EventArgs e)
87152
{
88153
base.OnSourceInitialized(e);

Bloxstrap/UI/Elements/ContextMenu/GameInformation.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
ExtendsContentIntoTitleBar="True"
2121
WindowStartupLocation="CenterScreen">
2222

23-
<Grid Background="{DynamicResource WindowBackgroundGradient}">
23+
<Grid Background="{DynamicResource ApplicationBackground}">
2424
<Grid.RowDefinitions>
2525
<RowDefinition Height="Auto" />
2626
<RowDefinition Height="*" />

Bloxstrap/UI/Elements/ContextMenu/ServerHistory.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
ExtendsContentIntoTitleBar="True"
2121
WindowStartupLocation="CenterScreen"
2222
mc:Ignorable="d">
23-
<Grid Background="{DynamicResource WindowBackgroundGradient}">
23+
<Grid Background="{DynamicResource ApplicationBackground}">
2424
<Grid.RowDefinitions>
2525
<RowDefinition Height="Auto" />
2626
<RowDefinition Height="Auto" />

Bloxstrap/UI/Elements/ContextMenu/ServerInformation.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Background="{ui:ThemeResource ApplicationBackgroundBrush}"
2020
ExtendsContentIntoTitleBar="True"
2121
WindowStartupLocation="CenterScreen">
22-
<Grid Background="{DynamicResource WindowBackgroundGradient}">
22+
<Grid Background="{DynamicResource ApplicationBackground}">
2323
<Grid.RowDefinitions>
2424
<RowDefinition Height="Auto" />
2525
<RowDefinition Height="*" />

0 commit comments

Comments
 (0)