forked from Violet-CLM/MLLE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaletteForm.cs
175 lines (152 loc) · 7.45 KB
/
PaletteForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Linq;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace MLLE
{
public partial class PaletteForm : Form
{
Palette DefaultPalette;
public PaletteForm()
{
InitializeComponent();
}
Palette InitialPalette;
PaletteImage PaletteImage = new PaletteImage(15, 2, false, true);
internal Palette ShowForm(Palette plusPalette, Palette defaultPalette)
{
DefaultPalette = defaultPalette;
InitialPalette = (plusPalette ?? DefaultPalette);
for (int i = 0; i < 10; ++i)
PaletteImage.ColorDisabled[i] = PaletteImage.ColorDisabled[Palette.PaletteSize - 10 + i] = true; //transparency, and default windows colors
PaletteImage.Location = new Point(12, OKButton.Location.Y);
PaletteImage.Palette = InitialPalette;
PaletteImage.MouseMove += PaletteImageMouseMove;
PaletteImage.MouseLeave += PaletteImageMouseLeave;
Controls.Add(PaletteImage);
ShowDialog();
return (!PaletteImage.Palette.Equals(InitialPalette)) ? PaletteImage.Palette : null;
}
private void ResetButton_Click(object sender, System.EventArgs e)
{
PaletteImage.Palette = DefaultPalette;
}
private void ButtonCancel_Click(object sender, System.EventArgs e)
{
PaletteImage.Palette.CopyFrom(InitialPalette);
Close();
}
private void OKButton_Click(object sender, System.EventArgs e)
{
Close();
}
private void select1ToolStripMenuItem_Click(object sender, EventArgs e)
{
select1ToolStripMenuItem.Checked = true;
select8ToolStripMenuItem.Checked = select16ToolStripMenuItem.Checked = false;
PaletteImage.NumberOfColorsToSelectAtATime = 1;
}
private void select8ToolStripMenuItem_Click(object sender, EventArgs e)
{
select8ToolStripMenuItem.Checked = true;
select1ToolStripMenuItem.Checked = select16ToolStripMenuItem.Checked = false;
PaletteImage.NumberOfColorsToSelectAtATime = 8;
}
private void select16ToolStripMenuItem_Click(object sender, EventArgs e)
{
select16ToolStripMenuItem.Checked = true;
select1ToolStripMenuItem.Checked = select8ToolStripMenuItem.Checked = false;
PaletteImage.NumberOfColorsToSelectAtATime = 16;
}
private void selectNoneToolStripMenuItem_Click(object sender, EventArgs e)
{
PaletteImage.SetSelected(PaletteImage.AllPaletteColors, false);
}
void SwapChannels(int a, int b, int c)
{
var selections = PaletteImage.GetSelectedIndices();
foreach (var selectedIndex in selections) {
var paletteEntry = PaletteImage.Palette[selectedIndex];
PaletteImage.Palette[selectedIndex] = new byte[] { paletteEntry[a], paletteEntry[b], paletteEntry[c], byte.MaxValue };
}
PaletteImage.Update(selections);
}
private void swapRedGreenToolStripMenuItem_Click(object sender, EventArgs e) { SwapChannels(1, 0, 2); }
private void swapGreenBlueToolStripMenuItem_Click(object sender, EventArgs e) { SwapChannels(0, 2, 1); }
private void swapBlueRedToolStripMenuItem_Click(object sender, EventArgs e) { SwapChannels(2, 1, 0); }
private void gradientToolStripMenuItem_Click(object sender, EventArgs e) //pretty much copied from JJ2+
{
var selections = PaletteImage.GetSelectedIndices();
var length = selections.Length - 1;
if (selections.Length > 2) //long enough to make a gradient from
{
Color first = Palette.Convert(PaletteImage.Palette[selections[0]]);
Color last = Palette.Convert(PaletteImage.Palette[selections[length]]);
float[] steps = new float[3] { (last.R - first.R) / (float)length, (last.G - first.G) / (float)length, (last.B - first.B) / (float)length };
for (int i = 1; i < length; i++)
PaletteImage.Palette[selections[i]] = new byte[] { (byte)(first.R + (steps[0] * i)), (byte)(first.G + (steps[1] * i)), (byte)(first.B + (steps[2] * i)), byte.MaxValue };
PaletteImage.Update(selections);
}
}
private void LoadButton_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var filepath = openFileDialog1.FileName;
if (Path.GetExtension(filepath) == ".j2t") //J2TFile has zero error checking, so hopefully this works out
{
PaletteImage.Palette = new J2TFile(filepath).Palette;
}
else
using (BinaryReader binreader = new BinaryReader(File.Open(filepath, FileMode.Open, FileAccess.Read), J2TFile.FileEncoding))
{
if (binreader.BaseStream.Length < 1024)
return;
else if (binreader.BaseStream.Length == 1032) //"color table" palette
binreader.BaseStream.Seek(4, SeekOrigin.Begin);
PaletteImage.Palette = new Palette(binreader);
}
}
}
private void toolsToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
{
var count = PaletteImage.NumberOfSelectedColors;
gradientToolStripMenuItem.Enabled =
count >= 3;
swapBlueRedToolStripMenuItem.Enabled = swapGreenBlueToolStripMenuItem.Enabled = swapRedGreenToolStripMenuItem.Enabled =
count > 0;
}
private void toolsToolStripMenuItem_DropDownClosed(object sender, EventArgs e)
{
gradientToolStripMenuItem.Enabled = true; //so that Ctrl+G is always available
}
private void PaletteImageMouseMove(object sender, MouseEventArgs e)
{
if (!(sender as Control).ClientRectangle.Contains(e.Location))
return;
Text = "Level Palette \u2013 " + PaletteImage.getSelectedColor(e);
}
private void PaletteImageMouseLeave(object sender, EventArgs e)
{
Text = "Level Palette";
}
private void SaveButton_Click(object sender, EventArgs e)
{
var levelImageSaveDialog = new SaveFileDialog();
levelImageSaveDialog.DefaultExt = "png";
levelImageSaveDialog.Filter = "Color Table|*.pal";
levelImageSaveDialog.FileName = "My Awesome Palette.pal";
if (levelImageSaveDialog.ShowDialog() == DialogResult.OK)
{
using (BinaryWriter binwriter = new BinaryWriter(File.Open(Path.ChangeExtension(levelImageSaveDialog.FileName, "pal"), FileMode.Create, FileAccess.Write), J2TFile.FileEncoding))
{
binwriter.Write(new byte[] { 0x0, 0x3, 0x0, 0x1 }); //don't really know what these mean... maybe number of channels per color (3), and number of colors (256)? but that would be switching endianness...
foreach (var color in PaletteImage.Palette.Colors)
binwriter.Write(color);
binwriter.Write((UInt32)0); //don't know what this means at all
}
}
}
}
}