Skip to content

Instantly share code, notes, and snippets.

@phrohdoh
Created February 5, 2015 19:58
Show Gist options
  • Save phrohdoh/86e3449564a2b2e79db3 to your computer and use it in GitHub Desktop.
Save phrohdoh/86e3449564a2b2e79db3 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using Eto;
using Eto.Forms;
using Eto.Drawing;
namespace ShpSharp
{
public class PalEditorForm : Form
{
public readonly Drawable Drawable;
PaletteData palette;
public PalEditorForm()
{
Title = "Palette Editor";
ClientSize = new Size(400, 300);
Drawable = new Drawable();
Drawable.Paint += HandlePaint;
Drawable.Width = 256; // Explicit is bad, but we'll do it for now
Drawable.Visible = true;
#region Open files
var openFileDlg = new OpenFileDialog
{
CheckFileExists = true,
MultiSelect = false,
Filters = GetFilters(),
Directory = new Uri(EtoEnvironment.GetFolderPath(EtoSpecialFolder.ApplicationResources))
};
var openFileCmd = new Command
{
MenuText = "Open Pal...",
ToolBarText = "Open Pal",
ToolTip = "Open a palette for editing",
Shortcut = Application.Instance.CommonModifier | Keys.O
};
openFileCmd.Executed += (sender, e) =>
{
var result = openFileDlg.ShowDialog(this);
if (result == DialogResult.Cancel)
return;
var file = openFileDlg.FileName;
MessageBox.Show(this, "TODO: Open pal: " + file);
};
#endregion
var newPalCmd = new Command
{
MenuText = "New Pal",
ToolBarText = "New Pal",
ToolTip = "Create a new palette",
};
newPalCmd.Executed += (sender, e) =>
{
Console.WriteLine("newPalCmd");
palette = new PaletteData();
palette.Entries = new PaletteEntry[256];
Drawable.Invalidate();
};
var invalidateBtn = new Button
{
Text = "Invalidate Drawable",
Visible = true,
};
invalidateBtn.Click += (sender, e) => Drawable.Invalidate();
ToolBar = new ToolBar
{
Items =
{
openFileCmd,
newPalCmd
}
};
var layout = new TableLayout
(
Drawable,
invalidateBtn
);
Content = layout;
}
void HandlePaint(object sender, PaintEventArgs e)
{
Console.WriteLine("HandlePaint from " + sender.ToString());
var g = e.Graphics;
var entries = palette.Entries;
for (var i = 0; i < entries.Length; i++)
{
var c = Color.FromArgb
(
entries[i].R,
entries[i].G,
entries[i].B
);
var rect = RectForEntry(i);
g.DrawPolygon(c, rect);
Console.WriteLine("Color ({0}), Rect ({1}, {2}, {3}, {4})",
c, rect[0], rect[1], rect[2], rect[3]);
}
}
PointF[] RectForEntry(int i)
{
var w = 32;
var h = 16;
var rect = new PointF[]
{
new PointF(i * w, i * h), // top-left
new PointF(i * w + w, i * h), // top-right
new PointF(i * w + w, i * h + h), // bottom-right
new PointF(i * w, i * h + h) // bottom-left
};
// Console.WriteLine("Rect ({0}, {1}, {2}, {3})", rect[0], rect[1], rect[2], rect[3]);
return rect;
}
IEnumerable<IFileDialogFilter> GetFilters()
{
yield return new FileDialogFilter("Palettes (.pal)", "pal");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment