Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NesMenuFolder.cs - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 67299141576154decded85bfc5fa88cf0455bb85 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using com.clusterrr.hakchi_gui.Properties;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Resources;

namespace com.clusterrr.hakchi_gui
{
    public class NesMenuFolder : INesMenuElement
    {
        static Random rnd = new Random();
        static ResourceManager rm = Resources.ResourceManager;
        public static readonly string FolderImagesDirectory = Path.Combine(Program.BaseDirectoryExternal, "folder_images");

        private int childIndex = 0;

        public int ChildIndex
        {
            get { return childIndex; }
            set { childIndex = value; }
        }

        public enum Priority
        {
            Leftmost = 0,
            Left = 1,
            Right = 3,
            Rightmost = 4,
            Back = 5
        }
        private Priority position;

        public string Code
        {
            get { return string.Format("CLV-S-{0:D5}", childIndex); }
        }
        private string name = null;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                if (!string.IsNullOrEmpty(name))
                    nameParts = new string[] { name };
                else
                    nameParts = new string[0];
            }
        }
        private string[] nameParts;
        public string[] NameParts
        {
            get { return nameParts; }
            set
            {
                nameParts = value;
                if (value != null)
                    name = string.Join(" - ", nameParts);
                else
                    name = null;
            }
        }
        public NesMenuCollection ChildMenuCollection = new NesMenuCollection();
        public string Initial = "";
        private Image image;
        private string imageId;

        byte Players = 2;
        byte Simultaneous = 1;
        string ReleaseDate = "0000-00-00";
        string Publisher = new String('!', 10);

        // It's workaround for sorting
        public Priority Position
        {
            set
            {
                // Sort to left
                position = value;
                switch (position)
                {
                    case Priority.Leftmost:
                        Players = 2;
                        Simultaneous = 1;
                        ReleaseDate = "0000-00-00";
                        Publisher = new String((char)1, 10);
                        break;
                    case Priority.Left:
                        Players = 2;
                        Simultaneous = 1;
                        ReleaseDate = "1111-11-11";
                        Publisher = new String((char)2, 10);
                        break;
                    case Priority.Right:
                        Players = 1;
                        Simultaneous = 0;
                        ReleaseDate = "7777-77-77";
                        Publisher = new String('Z', 9) + "X";
                        break;
                    case Priority.Rightmost:
                        Players = 1;
                        Simultaneous = 0;
                        ReleaseDate = "8888-88-88";
                        Publisher = new String('Z', 9) + "Y";
                        break;
                    case Priority.Back:
                        Players = 1;
                        Simultaneous = 0;
                        ReleaseDate = "9999-99-99";
                        Publisher = new String('Z', 10);
                        break;
                }
            }
            get
            {
                return position;
            }
        }

        public NesMenuFolder(string name = "Folder", string imageId = "folder")
        {
            Name = name;
            Position = Priority.Right;
            ImageId = imageId;
        }

        public Image Image
        {
            set
            {
                if (value == null)
                    ImageId = "folder";
                else
                {
                    image = Image;
                    imageId = null;
                }
            }
            get
            {
                Bitmap outImage;
                Graphics gr;
                if (image == null)
                    ImageId = "folder";
                // Just keep aspect ratio
                int maxX = 204;
                int maxY = 204;
                if (ConfigIni.ConsoleType == MainForm.ConsoleType.SNES || ConfigIni.ConsoleType == MainForm.ConsoleType.SuperFamicom)
                {
                    maxX = 228;
                    maxY = 204;
                }
                if (image.Width <= maxX && image.Height <= maxY) // Do not upscale
                    return image;
                if ((double)image.Width / (double)image.Height > (double)maxX / (double)maxY)
                    outImage = new Bitmap(maxX, (int)((double)maxX * (double)image.Height / (double)image.Width));
                else
                    outImage = new Bitmap((int)(maxY * (double)image.Width / (double)image.Height), maxY);
                gr = Graphics.FromImage(outImage);
                gr.DrawImage(image, new Rectangle(0, 0, outImage.Width, outImage.Height),
                                    new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                gr.Flush();
                return outImage;
            }
        }

        public Image ImageThumbnail
        {
            get
            {
                Bitmap outImage;
                Graphics gr;
                if (image == null)
                    ImageId = "folder";
                // Just keep aspect ratio
                const int maxX = 40;
                const int maxY = 40;
                if (image.Width <= maxX && image.Height <= maxY) // Do not upscale
                    return image;
                if ((double)image.Width / (double)image.Height > (double)maxX / (double)maxY)
                    outImage = new Bitmap(maxX, (int)((double)maxX * (double)image.Height / (double)image.Width));
                else
                    outImage = new Bitmap((int)(maxY * (double)image.Width / (double)image.Height), maxY);
                gr = Graphics.FromImage(outImage);
                gr.DrawImage(image, new Rectangle(0, 0, outImage.Width, outImage.Height),
                                    new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                gr.Flush();
                return outImage;
            }
        }

        public string ImageId
        {
            get { return imageId; }
            set
            {
                var folderImagesDirectory = Path.Combine(Program.BaseDirectoryExternal, "folder_images");
                var filePath = Path.Combine(folderImagesDirectory, value + ".png");
                if (File.Exists(filePath))
                    image = NesMiniApplication.LoadBitmap(filePath);
                else
                    image = (Image)rm.GetObject(value);
                imageId = value;
            }
        }

        public long Save(string path)
        {
            Directory.CreateDirectory(path);
            var ConfigPath = Path.Combine(path, Code + ".desktop");
            var IconPath = Path.Combine(path, Code + ".png");
            var ThumnnailIconPath = Path.Combine(path, Code + "_small.png");
            char prefix;
            switch (position)
            {
                case Priority.Leftmost:
                    prefix = (char)1;
                    break;
                default:
                case Priority.Left:
                    prefix = (char)2;
                    break;
                case Priority.Right:
                    prefix = 'Э';
                    break;
                case Priority.Rightmost:
                    prefix = 'Ю';
                    break;
                case Priority.Back:
                    prefix = 'Я';
                    break;
            }
            File.WriteAllText(ConfigPath, string.Format(
                 "[Desktop Entry]\n" +
                 "Type=Application\n" +
                 "Exec=/bin/chmenu {1:D3} {8}\n" +
                 "Path=/var/lib/clover/profiles/0/FOLDER\n" +
                 "Name={2}\n" +
                 "Icon={9}/{0}/{0}.png\n\n" +
                 "[X-CLOVER Game]\n" +
                 "Code={0}\n" +
                 "TestID=777\n" +
                 "ID=0\n" +
                 "Players={3}\n" +
                 "Simultaneous={7}\n" +
                 "ReleaseDate={4}\n" +
                 "SaveCount=0\n" +
                 "SortRawTitle={5}\n" +
                 "SortRawPublisher={6}\n" +
                 "Copyright=hakchi2 ©2017 Alexey 'Cluster' Avdyukhin\n",
                 Code, ChildIndex, Name ?? Code, Players, ReleaseDate,
                 prefix + (Name ?? Code).ToLower(), (Publisher ?? "").ToUpper(),
                 Simultaneous, Initial, NesMiniApplication.GamesCloverPath)
                 );
            Image.Save(IconPath, ImageFormat.Png);
            ImageThumbnail.Save(ThumnnailIconPath, ImageFormat.Png);
            return new FileInfo(ConfigPath).Length + new FileInfo(IconPath).Length + new FileInfo(ThumnnailIconPath).Length;
        }

        public override string ToString()
        {
            return Name ?? Code;
        }
    }
}