using System; using System.Collections.Generic; using System.Linq; using Gtk; using Ext.SilentorBit; using System.Drawing.Imaging; using System.Runtime.ExceptionServices; namespace SpritePacker.GUI { public class MainWindow : Window { private static bool initialised = false; private SpriteListView spriteListDisplay; private FileChooserDialog spriteListSelector; private FileFilter spriteListSelectorFilter; private Button openImageButton; private Button removeImageButton; private SpritePacker spritePacker = new SpritePacker(); public MainWindow() : base("SpritePacker GUI") { if (!initialised) throw new InvalidOperationException("Error: Gtk hasn't been initialised yet, so you can't create a new window."); SetDefaultSize(500, 300); SetPosition(WindowPosition.Center); // Quit when the close button is pressed DeleteEvent += (object o, DeleteEventArgs args) => { Application.Quit(); }; setupWidgets(); } /// /// Initilise GTK# to make it ready for use. /// public static void Init() { initialised = true; Application.Init(); } public void Start() { ShowAll(); Application.Run(); } private void setupWidgets() { // Create the layout containers HBox masterContainer = new HBox(false, 10); VBox leftPanel = new VBox(false, 0) { MarginRight = 5 }; HBox controlButtonRow = new HBox(); VBox rightPanel = new VBox(false, 0) { MarginLeft = 5 }; Frame leftPanelFrame = new Frame("Sprites") { Child = leftPanel, Margin = 10, MarginRight = 5 }; Frame rightPanelFrame = new Frame("Preview") { Child = rightPanel, Margin = 10, MarginLeft = 5 }; // Create the file selector filter spriteListSelectorFilter = new FileFilter() { Name = "Images" }; foreach (ImageCodecInfo currentDecoder in ImageCodecInfo.GetImageDecoders()) spriteListSelectorFilter.AddMimeType(currentDecoder.MimeType); // Create the file selector dialog spriteListSelector = new FileChooserDialog("Add Sprite", this, FileChooserAction.Open) { Margin = 10, PreviewWidgetActive = true, Filter = spriteListSelectorFilter }; spriteListSelector.SelectMultiple = true; spriteListSelector.AddButton("Add Sprites", ResponseType.Ok); spriteListSelector.AddButton("Cancel", ResponseType.Cancel); // Create the ListView to display the added sprites spriteListDisplay = new SpriteListView() { Margin = 10, MarginTop = 5 }; spriteListDisplay.EnableSearch = true; openImageButton = new Button("Add Sprites") { Margin = 10, MarginTop = 0, MarginBottom = 0 }; removeImageButton = new Button("Remove Selected Sprites") { Margin = 10, MarginTop = 0, MarginBottom = 0 }; openImageButton.Released += (object sender, EventArgs e) => { ResponseType response = (ResponseType)spriteListSelector.Run(); spriteListSelector.Hide(); switch(response) { case ResponseType.Ok: Action_AddSprite(spriteListSelector.Filenames); break; case ResponseType.DeleteEvent: case ResponseType.Cancel: Console.Error.WriteLine("Ignoring sprite file chooser response {0}.", response); break; default: throw new Exception($"Unknown sprite file chooser response type {response}."); } }; removeImageButton.Released += (object sender, EventArgs e) => { foreach(Sprite currentSprite in spriteListDisplay.SelectedItems) spritePacker.Remove(currentSprite); spriteListDisplay.ClearItems(); foreach(Sprite currentSprite in spritePacker.CurrentSprites) spriteListDisplay.AddItem(currentSprite); }; // Populate the control button row controlButtonRow.PackStart(openImageButton, true, false, 0); controlButtonRow.PackStart(removeImageButton, true, false, 0); // Populate the left panel leftPanel.PackStart(controlButtonRow, false, false, 5); leftPanel.PackStart(spriteListDisplay, true, true, 0); // Pack the master container masterContainer.PackStart(leftPanelFrame, true, true, 0); masterContainer.PackStart(rightPanelFrame, true, true, 0); // Add the master container to the window itself Add(masterContainer); } void Action_AddSprite(string[] selectedFilenames) { foreach (string filename in selectedFilenames) { Sprite nextSprite = new Sprite(filename); spritePacker.Add(nextSprite); spriteListDisplay.AddItem(nextSprite); } } } }