Skip to content

Commit

Permalink
Added preview while editing the palette; close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
jefff committed Jul 8, 2013
1 parent 06ac482 commit f947717
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
27 changes: 13 additions & 14 deletions AnimalCrossingQR/AnimalCrossingQR/ColorDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ public partial class ColorDialog : Form
{
public PaletteList Items { get { return paletteControl.Items; } }

public event EventHandler ColorPaletteChanged;


private Brush[] oppositePaletteBrushes;
private Brush[] paletteBrushes;
private string[] paletteLabels;

private int selectedIndex = -1;


private Font textFont;
private StringFormat stringFormat;

Expand Down Expand Up @@ -69,6 +70,12 @@ private void ColorDialog_Load(object sender, EventArgs e)
paletteLabels = new string[AC.Palette.ColorPalette.Length];
}

private void OnColorPaletteChanged()
{
if (ColorPaletteChanged != null)
ColorPaletteChanged(this, EventArgs.Empty);
}

private void DrawFullColorPalette(Graphics graphics)
{
for (int y = 0; y < 4; y++)
Expand Down Expand Up @@ -125,22 +132,14 @@ private void colorPanel_Paint(object sender, PaintEventArgs e)
DrawFullColorPalette(e.Graphics);
}

private void palettePanel_Paint(object sender, PaintEventArgs e)
{

}

private void palettePanel_MouseClick(object sender, MouseEventArgs e)
{
if (e.X > 5 && e.X < 55)
selectedIndex = (e.Y - 5) / 20;
}

private void colorPanel_MouseClick(object sender, MouseEventArgs e)
{
int? color = GetColorClick(e.X, e.Y);
if (color != null)
{
paletteControl.SelectedItem = color.Value;
OnColorPaletteChanged();
}

colorPanel.Invalidate();
}
Expand Down
18 changes: 14 additions & 4 deletions AnimalCrossingQR/AnimalCrossingQR/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,22 @@ private void fromImageURLToolStripMenuItem_Click(object sender, EventArgs e)
private void editColorsButton_Click(object sender, EventArgs e)
{
using (ColorDialog colorDialog = new ColorDialog(paletteControl.Items))
if (colorDialog.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < paletteControl.Items.Length; i++)
paletteControl.Items[i] = colorDialog.Items[i];
patternEditor.BeginPreview();

patternEditor.SetColorPalette(colorDialog.GetAsPalette());
colorDialog.ColorPaletteChanged +=
(o, s) => patternEditor.SetColorPalette(colorDialog.GetAsPalette());
if (colorDialog.ShowDialog() == DialogResult.OK)
{
for (int i = 0; i < paletteControl.Items.Length; i++)
paletteControl.Items[i] = colorDialog.Items[i];

patternEditor.SetColorPalette(colorDialog.GetAsPalette());
}
else
{
patternEditor.EndPreview();
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions AnimalCrossingQR/AnimalCrossingQR/PatternEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public partial class PatternEditor : UserControl

private byte[,] pixels;
private AC.Palette palette;
private byte[] savedPalette;
private Brush[] brushes;

private const int PixelSize = 9;
Expand Down Expand Up @@ -88,6 +89,19 @@ public void SetColorPalette(AC.Palette palette)
Invalidate();
}

// Saves the color palette to be restored with EndPreview
public void BeginPreview()
{
savedPalette = new byte[palette.Colors.Length];
Array.Copy(palette.Colors, savedPalette, savedPalette.Length);
}

public void EndPreview()
{
Array.Copy(savedPalette, palette.Colors, palette.Colors.Length);
Invalidate();
}

private Color FromPaletteColor(AC.Color color)
{
return Color.FromArgb(color.Red, color.Green, color.Blue);
Expand Down

0 comments on commit f947717

Please sign in to comment.