Browse Source

Begin work on GUI with Gtk#.

tags/v0.1
Starbeamrainbowlabs 2 years ago
parent
commit
c4ecc51245

+ 131
- 0
SpritePacker-GUI/ListView.cs View File

@@ -0,0 +1,131 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using Gtk;
4
+
5
+namespace SilentorBit
6
+{
7
+	/// <summary>
8
+	/// <para>A single column list, text comes from object.ToString()</para>
9
+	/// <para>For multicolumns, see ListView&lt;ClassType&gt;</para>
10
+	/// <para>By SilentorBit from https://silentorbit.com/notes/2011/05/gtk-sharp-listview/</para>
11
+	/// </summary>
12
+	public class ListView : ListView<object>
13
+	{
14
+		public ListView (string columnTitle) : base(columnTitle)
15
+		{
16
+		}
17
+
18
+		protected override void RenderCell (CellRendererText render, int index, object item)
19
+		{
20
+			render.Text = item.ToString ();
21
+		}
22
+	}
23
+
24
+	/// <summary>
25
+	/// The list only contains one type of object.
26
+	/// To get multiple columns, pass that number of parameters to the constructor
27
+	/// and implement RenderCell accordingly.
28
+	/// </summary>
29
+	public abstract class ListView<T> : TreeView
30
+	{
31
+		/// <summary>
32
+		/// List of all items selected
33
+		/// </summary>
34
+		public T[] SelectedItems { get; private set; }
35
+		public event Action<T[]> ItemSelected;
36
+		public event Action<T> ItemActivated;
37
+
38
+		protected abstract void RenderCell (CellRendererText render, int index, T item);
39
+
40
+		ListStore store = new ListStore (typeof(T));
41
+		Dictionary<T, TreeIter> storeList = new Dictionary<T, TreeIter> ();
42
+		List<TreeViewColumn> columns = new List<TreeViewColumn> ();
43
+
44
+		/// <summary>
45
+		/// Pass one string parameter for every column
46
+		/// </summary>
47
+		/// <param name="columnNames">
48
+		/// Column Titles, one parameter for each column.
49
+		/// </param>
50
+		public ListView (params string[] columnNames)
51
+		{
52
+			this.Model = store;
53
+			foreach (string s in columnNames) {
54
+				TreeViewColumn c = this.AppendColumn (s, new CellRendererText (), this.ColumnCellData);
55
+				columns.Add (c);
56
+			}
57
+
58
+			this.SelectedItems = new T[0];
59
+			this.Selection.Changed += HandleSelectionChanged;
60
+		}
61
+
62
+		private void ColumnCellData (TreeViewColumn column, CellRenderer renderer, ITreeModel model, TreeIter iter)
63
+		{
64
+			T item = (T)model.GetValue (iter, 0);
65
+			CellRendererText textRender = (CellRendererText)renderer;
66
+			int index = columns.IndexOf (column);
67
+			RenderCell (textRender, index, item);
68
+		}
69
+
70
+		#region Add, Remove and Clear Items
71
+
72
+		public void AddItem (T item)
73
+		{
74
+			var iter = store.AppendValues (item);
75
+			storeList.Add (item, iter);
76
+		}
77
+
78
+		public void ClearItems ()
79
+		{
80
+			store.Clear ();
81
+			storeList.Clear ();
82
+		}
83
+
84
+		public void RemoveItem (T item)
85
+		{
86
+			if (! storeList.ContainsKey (item))
87
+				return;
88
+
89
+			TreeIter iter = storeList[item];
90
+			store.Remove (ref iter);
91
+			storeList.Remove (item);
92
+		}
93
+
94
+		#endregion
95
+
96
+		#region Selection and Aktivation triggers
97
+
98
+		void HandleSelectionChanged (object sender, EventArgs e)
99
+		{
100
+			TreeSelection selection = (TreeSelection)sender;
101
+			TreePath[] paths = selection.GetSelectedRows ();
102
+			T[] items = new T[paths.Length];
103
+			for (int n = 0; n < paths.Length; n++) {
104
+				TreeIter iter;
105
+				Model.GetIter (out iter, paths[n]);
106
+				items[n] = (T)Model.GetValue (iter, 0);
107
+			}
108
+
109
+			SelectedItems = items;
110
+
111
+			var itemEvent = ItemSelected;
112
+			if (itemEvent != null)
113
+				itemEvent (items);
114
+		}
115
+
116
+		protected override void OnRowActivated (TreePath path, TreeViewColumn column)
117
+		{
118
+			TreeIter iter;
119
+			Model.GetIter (out iter, path);
120
+			T item = (T)Model.GetValue (iter, 0);
121
+
122
+			var e = ItemActivated;
123
+			if (e != null)
124
+				e (item);
125
+
126
+			base.OnRowActivated (path, column);
127
+		}
128
+
129
+		#endregion
130
+	}
131
+}

+ 68
- 0
SpritePacker-GUI/MainWindow.cs View File

@@ -0,0 +1,68 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+
5
+using Gtk;
6
+using SilentorBit;
7
+
8
+namespace SpritePacker.GUI
9
+{
10
+	public class MainWindow : Window
11
+	{
12
+		private static bool initialised = false;
13
+
14
+		private SpriteListView spriteListDisplay;
15
+
16
+		public MainWindow() : base("SpritePacker GUI")
17
+		{
18
+			if (!initialised)
19
+				throw new InvalidOperationException("Error: Gtk hasn't been initialised yet, so you can't create a new window.");
20
+
21
+			SetDefaultSize(500, 300);
22
+			SetPosition(WindowPosition.Center);
23
+
24
+			// Quit when the close button is pressed
25
+			DeleteEvent += (object o, DeleteEventArgs args) => {
26
+				Application.Quit();
27
+			};
28
+
29
+			setupLayout();
30
+		}
31
+
32
+		/// <summary>
33
+		/// Initilise GTK# to make it ready for use.
34
+		/// </summary>
35
+		public static void Init()
36
+		{
37
+			initialised = true;
38
+			Application.Init();
39
+		}
40
+
41
+		public void Start()
42
+		{
43
+			ShowAll();
44
+			Application.Run();
45
+		}
46
+
47
+		private void setupLayout()
48
+		{
49
+			HBox masterContainer = new HBox(true, 10);
50
+			VBox leftPanel = new VBox(false, 0) { MarginRight = 5 };
51
+			VBox rightPanel = new VBox(false, 0) { MarginLeft = 5 };
52
+			Frame leftPanelFrame = new Frame("Sprites") { Child = leftPanel, Margin = 10, MarginRight = 5 };
53
+			Frame rightPanelFrame = new Frame("Preview") { Child = rightPanel, Margin = 10, MarginLeft = 5 };
54
+
55
+			spriteListDisplay = new SpriteListView() { Margin = 10 };
56
+			spriteListDisplay.AddItem(new Sprite("/home/sbrl/Pictures/Spaghetti.png"));
57
+
58
+			leftPanel.OverrideBackgroundColor(StateFlags.Normal, Gdk.RGBA.Zero);
59
+			leftPanel.PackStart(spriteListDisplay, true, true, 0);
60
+
61
+			masterContainer.PackStart(leftPanelFrame, true, true, 0);
62
+			masterContainer.PackStart(rightPanelFrame, true, true, 0);
63
+
64
+			Add(masterContainer);
65
+		}
66
+	}
67
+}
68
+

+ 14
- 0
SpritePacker-GUI/Program.cs View File

@@ -0,0 +1,14 @@
1
+using System;
2
+
3
+namespace SpritePacker.GUI
4
+{
5
+	class MainClass
6
+	{
7
+		public static void Main(string[] args)
8
+		{
9
+			MainWindow.Init();
10
+			MainWindow mainWindow = new MainWindow();
11
+			mainWindow.Start();
12
+		}
13
+	}
14
+}

+ 27
- 0
SpritePacker-GUI/Properties/AssemblyInfo.cs View File

@@ -0,0 +1,27 @@
1
+using System.Reflection;
2
+using System.Runtime.CompilerServices;
3
+
4
+// Information about this assembly is defined by the following attributes.
5
+// Change them to the values specific to your project.
6
+
7
+[assembly: AssemblyTitle("SpritePacker-GUI")]
8
+[assembly: AssemblyDescription("")]
9
+[assembly: AssemblyConfiguration("")]
10
+[assembly: AssemblyCompany("")]
11
+[assembly: AssemblyProduct("")]
12
+[assembly: AssemblyCopyright("Starbeamrainbowlabs")]
13
+[assembly: AssemblyTrademark("")]
14
+[assembly: AssemblyCulture("")]
15
+
16
+// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
17
+// The form "{Major}.{Minor}.*" will automatically update the build and revision,
18
+// and "{Major}.{Minor}.{Build}.*" will update just the revision.
19
+
20
+[assembly: AssemblyVersion("1.0.*")]
21
+
22
+// The following attributes are used to specify the signing key for the assembly,
23
+// if desired. See the Mono documentation for more information about signing.
24
+
25
+//[assembly: AssemblyDelaySign(false)]
26
+//[assembly: AssemblyKeyFile("")]
27
+

+ 29
- 0
SpritePacker-GUI/SpriteListView.cs View File

@@ -0,0 +1,29 @@
1
+using System;
2
+using SilentorBit;
3
+using System.IO;
4
+
5
+namespace SpritePacker.GUI
6
+{
7
+	public class SpriteListView : ListView<Sprite>
8
+	{
9
+		public SpriteListView() : base("Number", "Filepath")
10
+		{
11
+		}
12
+
13
+		protected override void RenderCell(Gtk.CellRendererText render, int index, Sprite item)
14
+		{
15
+			switch(index)
16
+			{
17
+				case 0:
18
+					render.Text = index.ToString();
19
+					break;
20
+				case 1:
21
+					render.Text = item.Filename;
22
+					break;
23
+				default:
24
+					throw new InvalidDataException($"Invalid column index {index}");
25
+			}
26
+		}
27
+	}
28
+}
29
+

+ 74
- 0
SpritePacker-GUI/SpritePacker-GUI.csproj View File

@@ -0,0 +1,74 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3
+  <PropertyGroup>
4
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6
+    <ProjectGuid>{ECC09EBE-FE69-4DC2-B553-B92DF4992762}</ProjectGuid>
7
+    <OutputType>Exe</OutputType>
8
+    <RootNamespace>SpritePacker.GUI</RootNamespace>
9
+    <AssemblyName>SpritePacker-GUI</AssemblyName>
10
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
11
+  </PropertyGroup>
12
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
13
+    <DebugSymbols>true</DebugSymbols>
14
+    <DebugType>full</DebugType>
15
+    <Optimize>false</Optimize>
16
+    <OutputPath>bin\Debug</OutputPath>
17
+    <DefineConstants>DEBUG;</DefineConstants>
18
+    <ErrorReport>prompt</ErrorReport>
19
+    <WarningLevel>4</WarningLevel>
20
+    <Externalconsole>true</Externalconsole>
21
+    <PlatformTarget>x86</PlatformTarget>
22
+  </PropertyGroup>
23
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
24
+    <DebugType>full</DebugType>
25
+    <Optimize>true</Optimize>
26
+    <OutputPath>bin\Release</OutputPath>
27
+    <ErrorReport>prompt</ErrorReport>
28
+    <WarningLevel>4</WarningLevel>
29
+    <Externalconsole>true</Externalconsole>
30
+    <PlatformTarget>x86</PlatformTarget>
31
+  </PropertyGroup>
32
+  <ItemGroup>
33
+    <Reference Include="System" />
34
+    <Reference Include="atk-sharp">
35
+      <HintPath>..\packages\GtkSharp.3.1.3\lib\net45\atk-sharp.dll</HintPath>
36
+    </Reference>
37
+    <Reference Include="cairo-sharp">
38
+      <HintPath>..\packages\GtkSharp.3.1.3\lib\net45\cairo-sharp.dll</HintPath>
39
+    </Reference>
40
+    <Reference Include="gdk-sharp">
41
+      <HintPath>..\packages\GtkSharp.3.1.3\lib\net45\gdk-sharp.dll</HintPath>
42
+    </Reference>
43
+    <Reference Include="gio-sharp">
44
+      <HintPath>..\packages\GtkSharp.3.1.3\lib\net45\gio-sharp.dll</HintPath>
45
+    </Reference>
46
+    <Reference Include="glib-sharp">
47
+      <HintPath>..\packages\GtkSharp.3.1.3\lib\net45\glib-sharp.dll</HintPath>
48
+    </Reference>
49
+    <Reference Include="gtk-sharp">
50
+      <HintPath>..\packages\GtkSharp.3.1.3\lib\net45\gtk-sharp.dll</HintPath>
51
+    </Reference>
52
+    <Reference Include="pango-sharp">
53
+      <HintPath>..\packages\GtkSharp.3.1.3\lib\net45\pango-sharp.dll</HintPath>
54
+    </Reference>
55
+  </ItemGroup>
56
+  <ItemGroup>
57
+    <Compile Include="Program.cs" />
58
+    <Compile Include="Properties\AssemblyInfo.cs" />
59
+    <Compile Include="MainWindow.cs" />
60
+    <Compile Include="ListView.cs" />
61
+    <Compile Include="SpriteListView.cs" />
62
+  </ItemGroup>
63
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
64
+  <Import Project="..\packages\GtkSharp.3.1.3\build\net45\GtkSharp.targets" Condition="Exists('..\packages\GtkSharp.3.1.3\build\net45\GtkSharp.targets')" />
65
+  <ItemGroup>
66
+    <None Include="packages.config" />
67
+  </ItemGroup>
68
+  <ItemGroup>
69
+    <ProjectReference Include="..\SpritePacker\SpritePacker.csproj">
70
+      <Project>{2D3C4FAA-6D9C-4644-ABC0-361550A1884A}</Project>
71
+      <Name>SpritePacker</Name>
72
+    </ProjectReference>
73
+  </ItemGroup>
74
+</Project>

+ 4
- 0
SpritePacker-GUI/packages.config View File

@@ -0,0 +1,4 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<packages>
3
+  <package id="GtkSharp" version="3.1.3" targetFramework="net45" />
4
+</packages>

+ 6
- 0
SpritePacker.sln View File

@@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
11 11
 EndProject
12 12
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpritePacker", "SpritePacker\SpritePacker.csproj", "{2D3C4FAA-6D9C-4644-ABC0-361550A1884A}"
13 13
 EndProject
14
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpritePacker-GUI", "SpritePacker-GUI\SpritePacker-GUI.csproj", "{ECC09EBE-FE69-4DC2-B553-B92DF4992762}"
15
+EndProject
14 16
 Global
15 17
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
16 18
 		Debug|x86 = Debug|x86
@@ -25,6 +27,10 @@ Global
25 27
 		{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Debug|x86.Build.0 = Debug|x86
26 28
 		{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Release|x86.ActiveCfg = Release|x86
27 29
 		{6EF47B64-1920-4827-BEEF-B262D5A2D214}.Release|x86.Build.0 = Release|x86
30
+		{ECC09EBE-FE69-4DC2-B553-B92DF4992762}.Debug|x86.ActiveCfg = Debug|x86
31
+		{ECC09EBE-FE69-4DC2-B553-B92DF4992762}.Debug|x86.Build.0 = Debug|x86
32
+		{ECC09EBE-FE69-4DC2-B553-B92DF4992762}.Release|x86.ActiveCfg = Release|x86
33
+		{ECC09EBE-FE69-4DC2-B553-B92DF4992762}.Release|x86.Build.0 = Release|x86
28 34
 	EndGlobalSection
29 35
 	GlobalSection(NestedProjects) = preSolution
30 36
 	EndGlobalSection

Loading…
Cancel
Save