Skip to content

Commit 0e1881e

Browse files
committed
adding settings for grid colors
1 parent f1e7a4f commit 0e1881e

File tree

8 files changed

+129
-26
lines changed

8 files changed

+129
-26
lines changed

PixelArtTool/App.config

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
3+
<configSections>
4+
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
5+
<section name="PixelArtTool.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
6+
</sectionGroup>
7+
</configSections>
38
<startup>
49
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
510
</startup>
11+
<userSettings>
12+
<PixelArtTool.Properties.Settings>
13+
<setting name="gridLightColor" serializeAs="String">
14+
<value>White</value>
15+
</setting>
16+
<setting name="gridDarkColor" serializeAs="String">
17+
<value>Black</value>
18+
</setting>
19+
</PixelArtTool.Properties.Settings>
20+
</userSettings>
621
</configuration>

PixelArtTool/MainWindow.xaml.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public partial class MainWindow : Window, INotifyPropertyChanged
3535
int paletteScaleY = 1;
3636
int dpiX = 96;
3737
int dpiY = 96;
38-
byte gridAlpha = 32;
3938

4039
// simple undo
4140
Stack<WriteableBitmap> undoStack = new Stack<WriteableBitmap>();
@@ -45,6 +44,9 @@ public partial class MainWindow : Window, INotifyPropertyChanged
4544
// colors
4645
PixelColor currentColor;
4746
PixelColor[] palette;
47+
PixelColor lightColor;
48+
PixelColor darkColor;
49+
byte gridAlpha = 32;
4850

4951
int currentColorIndex = 0;
5052
byte opacity = 255;
@@ -131,10 +133,21 @@ void Start()
131133
// needed for binding
132134
DataContext = this;
133135

136+
// TODO read settings
137+
lightColor.Red = 255;
138+
lightColor.Green = 255;
139+
lightColor.Blue = 255;
140+
lightColor.Alpha = gridAlpha;
141+
142+
darkColor.Red = 0;
143+
darkColor.Green = 0;
144+
darkColor.Blue = 0;
145+
darkColor.Alpha = gridAlpha;
146+
134147
// setup background grid
135148
gridBitmap = new WriteableBitmap(canvasResolutionX, canvasResolutionY, dpiX, dpiY, PixelFormats.Bgra32, null);
136149
gridImage.Source = gridBitmap;
137-
DrawBackgroundGrid(gridBitmap, canvasResolutionX, canvasResolutionY, gridAlpha);
150+
DrawBackgroundGrid(gridBitmap, canvasResolutionX, canvasResolutionY, lightColor, darkColor);
138151

139152
// build drawing area
140153
canvasBitmap = new WriteableBitmap(canvasResolutionX, canvasResolutionY, dpiX, dpiY, PixelFormats.Bgra32, null);
@@ -1363,6 +1376,19 @@ private void btnSettings_Click(object sender, RoutedEventArgs e)
13631376
switch (result)
13641377
{
13651378
case true: // ok
1379+
// TODO: update things using settings
1380+
1381+
var b1 = (SolidColorBrush)dlg.settingsLightColor.Fill;
1382+
lightColor.Red = b1.Color.R;
1383+
lightColor.Green = b1.Color.G;
1384+
lightColor.Blue = b1.Color.B;
1385+
1386+
var b2 = (SolidColorBrush)dlg.settingsDarkColor.Fill;
1387+
darkColor.Red = b2.Color.R;
1388+
darkColor.Green = b2.Color.G;
1389+
darkColor.Blue = b2.Color.B;
1390+
1391+
DrawBackgroundGrid(gridBitmap, canvasResolutionX, canvasResolutionY, lightColor, darkColor);
13661392
break;
13671393
case false: // cancelled
13681394
break;

PixelArtTool/PixelArtTool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<ItemGroup>
4242
<Reference Include="System" />
4343
<Reference Include="System.Data" />
44+
<Reference Include="System.Drawing" />
4445
<Reference Include="System.Xml" />
4546
<Reference Include="Microsoft.CSharp" />
4647
<Reference Include="System.Core" />

PixelArtTool/Properties/Settings.Designer.cs

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
3-
<Profiles>
4-
<Profile Name="(Default)" />
5-
</Profiles>
6-
<Settings />
2+
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="PixelArtTool.Properties" GeneratedClassName="Settings">
3+
<Profiles />
4+
<Settings>
5+
<Setting Name="gridLightColor" Type="System.Drawing.Color" Scope="User">
6+
<Value Profile="(Default)">White</Value>
7+
</Setting>
8+
<Setting Name="gridDarkColor" Type="System.Drawing.Color" Scope="User">
9+
<Value Profile="(Default)">Black</Value>
10+
</Setting>
11+
</Settings>
712
</SettingsFile>

PixelArtTool/Settings.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:local="clr-namespace:PixelArtTool"
77
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="PixelArtTool.Settings"
88
mc:Ignorable="d"
9-
Title="Settings" Height="330" Width="300" Background="#FF252525">
9+
Title="Settings" Height="330" Width="300" Background="#FF252525" Closing="Window_Closing">
1010
<Grid>
1111
<StackPanel>
1212
<Separator Height="20" Margin="0"/>
@@ -25,6 +25,8 @@
2525

2626
<Separator Height="20" Margin="0"/>
2727
<Rectangle x:Name="defaultColor" Fill="#FF000000" HorizontalAlignment="Left" Height="28" Stroke="Black" VerticalAlignment="Top" Width="28"/>
28+
<Button x:Name="okButton" IsDefault="True" Content="OK" Click="OnOkButtonClick" Margin="80,0,137,0" Height="50"/>
29+
<Button x:Name="cancelButton" IsCancel="True" Content="Cancel" Margin="0,0,217,0" Height="50"/>
2830
</StackPanel>
2931
</Grid>
3032
</Window>

PixelArtTool/Settings.xaml.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62
using System.Windows;
7-
using System.Windows.Controls;
8-
using System.Windows.Data;
9-
using System.Windows.Documents;
103
using System.Windows.Input;
114
using System.Windows.Media;
12-
using System.Windows.Media.Imaging;
13-
using System.Windows.Shapes;
5+
using static PixelArtTool.Tools;
146

157
namespace PixelArtTool
168
{
@@ -22,6 +14,19 @@ public partial class Settings : Window
2214
public Settings()
2315
{
2416
InitializeComponent();
17+
Start();
18+
}
19+
20+
void Start()
21+
{
22+
// TODO load all current settings
23+
settingsLightColor.Fill = ConvertSystemDrawingColorToSolidColorBrush(Properties.Settings.Default.gridLightColor);
24+
settingsDarkColor.Fill = ConvertSystemDrawingColorToSolidColorBrush(Properties.Settings.Default.gridDarkColor);
25+
}
26+
27+
private void OnOkButtonClick(object sender, RoutedEventArgs e)
28+
{
29+
this.DialogResult = true;
2530
}
2631

2732
private void settingsLightColor_MouseDown(object sender, MouseButtonEventArgs e)
@@ -31,7 +36,9 @@ private void settingsLightColor_MouseDown(object sender, MouseButtonEventArgs e)
3136
var result = dlg.ShowDialog();
3237
switch (result)
3338
{
34-
case true: // ok
39+
case true: // ok
40+
// get values from color picker
41+
settingsLightColor.Fill = dlg.rectCurrentColor.Fill;
3542
break;
3643
case false: // cancelled
3744
break;
@@ -49,6 +56,7 @@ private void settingsDarkColor_MouseDown(object sender, MouseButtonEventArgs e)
4956
switch (result)
5057
{
5158
case true: // ok
59+
settingsDarkColor .Fill = dlg.rectCurrentColor.Fill;
5260
break;
5361
case false: // cancelled
5462
break;
@@ -57,5 +65,13 @@ private void settingsDarkColor_MouseDown(object sender, MouseButtonEventArgs e)
5765
break;
5866
}
5967
}
60-
}
68+
69+
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
70+
{
71+
// TODO save to settings
72+
Properties.Settings.Default.gridDarkColor = ConvertBrushToSystemDrawingColor(settingsDarkColor.Fill);
73+
Properties.Settings.Default.gridLightColor = ConvertBrushToSystemDrawingColor(settingsLightColor.Fill);
74+
Properties.Settings.Default.Save();
75+
}
76+
} // class
6177
}

PixelArtTool/Tools.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,22 @@ public static int Repeat(int val, int max)
191191
return result;
192192
}
193193

194-
public static void DrawBackgroundGrid(WriteableBitmap targetBitmap, int canvasResolutionX, int canvasResolutionY, byte gridAlpha)
194+
public static void DrawBackgroundGrid(WriteableBitmap targetBitmap, int canvasResolutionX, int canvasResolutionY, PixelColor c1, PixelColor c2)
195195
{
196+
Console.WriteLine(123);
196197
PixelColor c = new PixelColor();
197198
for (int x = 0; x < canvasResolutionX; x++)
198199
{
199200
for (int y = 0; y < canvasResolutionY; y++)
200201
{
201-
c.Alpha = gridAlpha;
202-
byte v = (byte)(((x % 2) == (y % 2)) ? 255 : 0);
203-
c.Red = v;
204-
c.Green = v;
205-
c.Blue = v;
206-
SetPixel(targetBitmap, x, y, (int)c.ColorBGRA);
202+
// c.Alpha = gridAlpha;
203+
// byte v = (byte)(((x % 2) == (y % 2)) ? 255 : 0);
204+
var v = ((x % 2) == (y % 2)) ? c1 : c2;
205+
// c.Red = v;
206+
// c.Green = v;
207+
// c.Blue = v;
208+
//v.Alpha = 255;
209+
SetPixel(targetBitmap, x, y, (int)v.ColorBGRA);
207210
}
208211
}
209212
}
@@ -519,6 +522,17 @@ public static BitmapImage ConvertWriteableBitmapToBitmapImage(WriteableBitmap wb
519522
return bmImage;
520523
}
521524

525+
public static SolidColorBrush ConvertSystemDrawingColorToSolidColorBrush(System.Drawing.Color c)
526+
{
527+
return new SolidColorBrush(Color.FromArgb(c.A, c.R, c.G, c.B));
528+
}
529+
530+
public static System.Drawing.Color ConvertBrushToSystemDrawingColor(Brush c)
531+
{
532+
var bc = ((SolidColorBrush)c);
533+
var newc = System.Drawing.Color.FromArgb(bc.Color.A, bc.Color.R, bc.Color.G, bc.Color.B);
534+
return newc;
535+
}
522536

523537
} // class
524538
} // namespace

0 commit comments

Comments
 (0)