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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using buttonstate = Microsoft.Xna.Framework.Input.ButtonState;
using keys = Microsoft.Xna.Framework.Input.Keys;
namespace PictureJoinerSplitter
{
[Serializable]
public class Img
{
[XmlIgnore]
public Texture2D Texture;
public string Name;
public Rectangle sourceRectangle;
[XmlIgnore]
public bool NoneIntersection;
public Rectangle rect_m { get { return new Rectangle(sourceRectangle.X, sourceRectangle.Y, Texture.Width, Texture.Height); } }
[XmlIgnore]
public MouseState MST, lastMST;
[XmlIgnore]
public bool Dragged = false;
[XmlIgnore]
public bool Colored = false;
private void CheckIntersection()
{
NoneIntersection = true;
Rectangle main_rect = new Rectangle(0,0,Game1.scale*512,Game1.scale*512);
Colored = true;
if (!main_rect.Contains(rect_m))
{
Colored = false;
if (Keyboard.GetState().IsKeyDown(keys.LeftShift))
{
NoneIntersection = false;
return;
}
}
for (int i = 0; i < Game1.Images.Count; i++)
{
if (Game1.Images[i].Name != this.Name)
{
if (rect_m.Intersects(Game1.Images[i].rect_m))
{
NoneIntersection = false;
return;
}
}
}
}
public void Draw(SpriteBatch sb)
{
sb.Draw(Texture,
new Vector2(sourceRectangle.Left / Game1.scale + 5, sourceRectangle.Top / Game1.scale + 5),
new Rectangle(0,0,Texture.Width,Texture.Height),
(!NoneIntersection || !Colored)? Color.Red : Color.White,0,Vector2.Zero,1f/(float)Game1.scale,SpriteEffects.None,1f);
}
public void DrawFull(SpriteBatch sb)
{
sb.Draw(Texture,
new Vector2(sourceRectangle.Left, sourceRectangle.Top),
new Rectangle(0,0,Texture.Width,Texture.Height),
Color.White,0,Vector2.Zero,1f,SpriteEffects.None,1f);
}
public void Update()
{
lastMST = MST;
MST = Mouse.GetState();
KeyboardState KBS = Keyboard.GetState();
CheckIntersection();
if (MST.LeftButton == buttonstate.Pressed &&
rect_m.Contains(new Point(Game1.scale*(MST.X)-5, Game1.scale*(MST.Y)-5))&&Game1.candragother)
{
Dragged = true;
Game1.candragother = false;
}
if (MST.LeftButton == buttonstate.Released &&
Dragged)
{
Dragged = false;
Game1.candragother = true;
}
if (Dragged)
{
Point point = sourceRectangle.Location;
sourceRectangle.Location = new Point(sourceRectangle.Location.X + MST.X - lastMST.X,
sourceRectangle.Location.Y);
CheckIntersection();
if (!KBS.IsKeyDown(keys.LeftControl)) if (!NoneIntersection) sourceRectangle.Location = point;
point = sourceRectangle.Location;
sourceRectangle.Location = new Point(sourceRectangle.Location.X,
sourceRectangle.Location.Y + MST.Y - lastMST.Y);
CheckIntersection();
if (!KBS.IsKeyDown(keys.LeftControl)) if (!NoneIntersection) sourceRectangle.Location = point;
}
}
}
public class Button
{
public readonly Texture2D texture;
public string Text = "";
public Point position;
public MouseState MST;
public MouseState lastMST;
public Button(Texture2D texture, Point position, string Text)
{
this.Text = Text;
this.texture = texture;
this.position = position;
}
private bool CheckCursor(Vector2 cursor)
{
return (new Rectangle(position.X, position.Y, texture.Width, texture.Height).Contains(new Point((int)cursor.X, (int)cursor.Y)));
}
public void Update(Action action)
{
lastMST = MST;
MST = Mouse.GetState();
if (CheckCursor(new Vector2(MST.X,MST.Y)) && (MST.LeftButton == buttonstate.Pressed) &&
(lastMST.LeftButton == buttonstate.Released))
action();
}
public void Draw(SpriteBatch sb)
{
sb.Draw(texture, new Vector2(position.X, position.Y), CheckCursor(new Vector2(MST.X, MST.Y)) ? Color.Orange : Color.LightBlue);
sb.DrawString(Game1.font, Text, new Vector2(position.X + (float)(texture.Width - Game1.font.MeasureString(Text).X) / 2f, position.Y + (float)(texture.Height - Game1.font.MeasureString(Text).Y) / 2f), Color.White);
}
}
public class Game1 : Microsoft.Xna.Framework.Game
{
Button button_new,
button_add,
button_save,
button_split,
button_exit,
button_2048;
public static SpriteFont font;
public static int scale = 2;
public static bool candragother = true;
public static List<Img> Images = new List<Img>();
private Texture2D p;
private KeyboardState kbs, lastKBS;
private SpriteBatch spriteBatch;
private RenderTarget2D RT2D;
GraphicsDeviceManager graphics;
private bool click(keys k)
{
return kbs.IsKeyDown(k) && lastKBS.IsKeyUp(k);
}
public Game1()
{
this.IsMouseVisible = true;
graphics = new GraphicsDeviceManager(this);
graphics.SynchronizeWithVerticalRetrace = true;
graphics.PreferredBackBufferWidth = 629;// GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
graphics.PreferredBackBufferHeight = 524;// GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
graphics.IsFullScreen = !true;
graphics.ApplyChanges();
}
protected override void Initialize()
{
Content.RootDirectory = "Content";
MessageBox.Show(@"
удерживать Left Shift - для того, чтобы картинка не выходила за пределы ограничивающего прямоугольника.
удерживать Left Сtrl - для того, чтобы иметь возможность перетаскивать картинку даже в случае наложения.
автор: za5
");
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Services.AddService(typeof(SpriteBatch), spriteBatch);
Texture2D button_texture = Content.Load<Texture2D>("button");
button_new = new Button(button_texture, new Point(524,5),"new");
button_add = new Button(button_texture, new Point(524, 35), "add");
button_save = new Button(button_texture, new Point(524, 65), "join!");
button_exit = new Button(button_texture, new Point(524, 125), "exit");
button_split = new Button(button_texture, new Point(524, 95), "split!");
button_2048 = new Button(button_texture, new Point(524, 185), "1024");
p = Content.Load<Texture2D>("p");
font = Content.Load<SpriteFont>("font");
}
private void FileOk(object sender, CancelEventArgs e)
{
OpenFileDialog f = sender as OpenFileDialog;
string[] files_to_load = f.FileNames;
string[] names = f.SafeFileNames;
for (int i = 0; i < files_to_load.Length; i++)
{
using (FileStream fs = File.OpenRead(files_to_load[i]))
{
Texture2D tex = Texture2D.FromStream(GraphicsDevice, fs);
Images.Add(new Img()
{
Texture = tex,
Name = names[i].Remove(names[i].Length - 4),
sourceRectangle = new Rectangle(0, 0, tex.Width, tex.Height)
});
fs.Dispose();
}
}
}
public Point Max
{
get
{
Point max = new Point(Game1.Images.Max(o => o.sourceRectangle.Right),
Game1.Images.Max(o => o.sourceRectangle.Bottom));
return new Point(max.X > Game1.scale * 512 ? Game1.scale * 512 : max.X,
max.Y > Game1.scale * 512 ? Game1.scale * 512 : max.Y);
}
}
private void FileOkSplit(object sender, CancelEventArgs e)
{
OpenFileDialog f = sender as OpenFileDialog;
string[] files_to_load = f.FileNames;
string[] names = f.SafeFileNames;
string nameee = names[0].Remove(names[0].Length - 3)+"png";
Images.Clear();
Texture2D tex = Texture2D.FromStream(GraphicsDevice, File.OpenRead(nameee));
LoadAllFromFile(files_to_load[0]);
string str = Path.GetDirectoryName(Application.ExecutablePath) +"\\"+ DateTime.Now.Ticks + "\\";
for (int i = 0; i < Game1.Images.Count; i++)
{
Game1.Images[i].Texture = SelectiveRTt2D(spriteBatch, Game1.Images[i].sourceRectangle, tex);
Directory.CreateDirectory(str);
using (FileStream fs = File.OpenWrite(str + Game1.Images[i].Name + ".png"))
{
Game1.Images[i].Texture.SaveAsPng(fs, Game1.Images[i].Texture.Width, Game1.Images[i].Texture.Height);
fs.Dispose();
}
}
MessageBox.Show("Save OK");
}
public RenderTarget2D GetRenderTarget2D(SpriteBatch sB, Point size)
{
RenderTarget2D renderTarget = new RenderTarget2D(GraphicsDevice, size.X,
size.Y, false, SurfaceFormat.Color, DepthFormat.Depth24, 0, RenderTargetUsage.DiscardContents);
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(Color.Transparent);
//sB.Begin();
sB.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, DepthStencilState.Default, null);
foreach (Img img in Images)
{
img.DrawFull(spriteBatch);
}
//sB.DrawString(font, str, Vector2.Zero, color);
sB.End();
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Transparent);
return renderTarget;
}
public Texture2D SelectiveRTt2D(SpriteBatch sB, Rectangle size, Texture2D FULL)
{
RenderTarget2D renderTarget = new RenderTarget2D(GraphicsDevice, size.Width,
size.Height, false, SurfaceFormat.Color, DepthFormat.Depth24, 0, RenderTargetUsage.DiscardContents);
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(Color.Transparent);
sB.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, DepthStencilState.Default, null);
sB.Draw(FULL, new Vector2(-size.Left,-size.Top), Color.White);
sB.End();
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Transparent);
return renderTarget;
}
private void FileOkSave(object sender, CancelEventArgs e)
{
SaveFileDialog f = sender as SaveFileDialog;
string path = f.FileName;
string s = path.Remove(path.Length - 3);
using (XmlTextWriter w = new XmlTextWriter(s + "zzz", System.Text.Encoding.UTF8))
{
w.Formatting = Formatting.Indented;
new XmlSerializer(typeof(List<Img>)).Serialize(w, Images);
}
using (FileStream fs = File.OpenWrite(path))
{
GetRenderTarget2D(spriteBatch, Max).SaveAsPng(fs, Max.X, Max.Y);
fs.Dispose();
}
}
public static void LoadAllFromFile(string path)
{
using (XmlTextReader r = new XmlTextReader(path))
Images = (List<Img>)new XmlSerializer(typeof(List<Img>)).Deserialize(r);
}
public void Increase()
{
if (Game1.scale == 1) { Game1.scale = 2; button_2048.Text = (Game1.scale * 512).ToString(); return; }
if (Game1.scale == 2) { Game1.scale = 4; button_2048.Text = (Game1.scale * 512).ToString(); return; }
if (Game1.scale == 4) { Game1.scale = 1; button_2048.Text = (Game1.scale * 512).ToString(); return; }
}
public void button_add_click()
{
OpenFileDialog f = new OpenFileDialog();
f.Filter = "Image Files(*.PNG;.JPG;*.GIF)|*.PNG;*.JPG;*.GIF";
f.FilterIndex = 1 ;
f.FileOk += new System.ComponentModel.CancelEventHandler(FileOk);
f.ShowDialog();
}
public void button_split_click()
{
OpenFileDialog f = new OpenFileDialog();
f.Multiselect = false;
f.Filter = "Image Files(*.zzz;)|*.zzz";
f.FilterIndex = 1;
f.FileOk += new System.ComponentModel.CancelEventHandler(FileOkSplit);
f.ShowDialog();
}
public void button_save_click()
{
SaveFileDialog f = new SaveFileDialog();
f.Filter = "PNG Files(*.PNG;)|*.PNG";
f.FilterIndex = 1;
f.FileOk += new System.ComponentModel.CancelEventHandler(FileOkSave);
f.ShowDialog();
}
protected override void Update(GameTime gameTime)
{
if (Game1.candragother)
{
button_add.Update(button_add_click);
button_save.Update(button_save_click);
button_split.Update(button_split_click);
button_exit.Update(Exit);
button_new.Update(Images.Clear);
button_2048.Update(Increase);
}
lastKBS = kbs;
kbs = Keyboard.GetState();
foreach (var item in Images)
{
item.Update();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
foreach (Img img in Images)
{
img.Draw(spriteBatch);
}
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise);
button_add.Draw(spriteBatch);
button_new.Draw(spriteBatch);
button_save.Draw(spriteBatch);
button_split.Draw(spriteBatch);
button_exit.Draw(spriteBatch);
button_2048.Draw(spriteBatch);
spriteBatch.Draw(p, new Vector2(5), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
} |