aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/XCursor.cs
blob: 627e2afdd473aa1cd70bc0580537b3a99fc3672c (plain)
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
//
// XCursor.cs
//
// Author:
//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2013-2017 Jean-Philippe Bruyère
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;

namespace Crow
{
	public class XCursorFile
	{
		const uint XC_TYPE_IMG = 0xfffd0002;

		class toc
		{
			public uint type;
			public uint subtype;
			public uint pos;

			public toc(BinaryReader sr)
			{
				type = sr.ReadUInt32();
				subtype = sr.ReadUInt32();
				pos = sr.ReadUInt32();
			}
		}

		public List<XCursor> Cursors = new List<XCursor>();


		static XCursorFile loadFromStream(Stream s)
		{
			List<toc> tocList = new List<toc> ();
			XCursorFile tmp = new XCursorFile ();

			using (BinaryReader sr = new BinaryReader (s)) {
				byte[] data;
				//magic: CARD32 ’Xcur’ (0x58, 0x63, 0x75, 0x72)
				if (new string (sr.ReadChars (4)) != "Xcur") {
					Debug.WriteLine ("XCursor Load error: Wrong magic");
					return null;
				}
				//header: CARD32 bytes in this header
				uint headerLength = sr.ReadUInt32 ();
				//version: CARD32 file version number
				uint version = sr.ReadUInt32 ();
				//ntoc: CARD32 number of toc entries
				uint nbToc = sr.ReadUInt32 ();
				//toc: LISTofTOC table of contents
				for (uint i = 0; i < nbToc; i++) {
					tocList.Add (new toc (sr));
				}

				foreach (toc t in tocList) {
					if (t.type != XC_TYPE_IMG)
						continue;

					sr.BaseStream.Seek (t.pos, SeekOrigin.Begin);
					tmp.Cursors.Add(imageLoad (sr));						
				}
			}
			return tmp;
		}

		public static XCursorFile Load(string path)
		{
			return loadFromStream (Interface.GetStreamFromPath (path));
		}

		static XCursor imageLoad(BinaryReader sr)
		{
			XCursor tmp = new XCursor();
			//			header: 36 Image headers are 36 bytes
			uint header = sr.ReadUInt32();
			//			type: 0xfffd0002 Image type is 0xfffd0002
			uint type = sr.ReadUInt32();
			//			subtype: CARD32 Image subtype is the nominal size
			uint subtype = sr.ReadUInt32();
			//			version: 1
			uint version = sr.ReadUInt32();
			//			width: CARD32 Must be less than or equal to 0x7fff
			tmp.Width = sr.ReadUInt32();
			//			height: CARD32 Must be less than or equal to 0x7fff
			tmp.Height = sr.ReadUInt32();
			//			xhot: CARD32 Must be less than or equal to width
			tmp.Xhot = sr.ReadUInt32();
			//			yhot: CARD32 Must be less than or equal to height
			tmp.Yhot = sr.ReadUInt32();
			//			delay: CARD32 Delay between animation frames in milliseconds
			tmp.Delay = sr.ReadUInt32();
			//			pixels: LISTofCARD32 Packed ARGB format pixels
			tmp.data = sr.ReadBytes((int)(tmp.Width * tmp.Height * 4));
			return tmp;
		}
	}
	public class XCursor
	{
		public static XCursor Default;
		public static XCursor Cross;
		public static XCursor Arrow;
		public static XCursor Text;
		public static XCursor SW;
		public static XCursor SE;
		public static XCursor NW;
		public static XCursor NE;
		public static XCursor N;
		public static XCursor S;
		public static XCursor V;
		public static XCursor H;

		public uint Width;
		public uint Height;
		public uint Xhot;
		public uint Yhot;
		public uint Delay;
		public byte[] data;

		public XCursor ()
		{
		}
//		public static implicit operator MouseCursor(XCursor xc)
//		{
//			return new MouseCursor((int)xc.Xhot, (int)xc.Yhot, (int)xc.Width, (int)xc.Height,xc.data);
//		}
	}
}