This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
ScreenshotService.cs
executable file
·152 lines (121 loc) · 5.13 KB
/
ScreenshotService.cs
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
//
// Copyright (c) Jesse Freeman, Pixel Vision 8. All rights reserved.
//
// Licensed under the Microsoft Public License (MS-PL) except for a few
// portions of the code. See LICENSE file in the project root for full
// license information. Third-party libraries used by Pixel Vision 8 are
// under their own licenses. Please refer to those libraries for details
// on the license they use.
//
// Contributors
// --------------------------------------------------------
// This is the official list of Pixel Vision 8 contributors:
//
// Jesse Freeman - @JesseFreeman
// Christina-Antoinette Neofotistou @CastPixel
// Christer Kaitila - @McFunkypants
// Pedro Medeiros - @saint11
// Shawn Rakowski - @shwany
//
using Microsoft.Xna.Framework;
using PixelVision8.Player;
using PixelVision8.Workspace;
using System.Collections.Generic;
using System.Linq;
using PixelVision8.Runner;
namespace PixelVision8.Editor
{
public class ScreenshotService : AbstractService
{
private readonly PNGWriter imageExporter;
// private ITextureFactory textureFactory;
private readonly WorkspaceService workspace;
private bool active;
public ScreenshotService(WorkspaceService workspace)
{
// TODO this needs to get teh workspace through the service
// this.textureFactory = textureFactory;
this.workspace = workspace;
imageExporter = new PNGWriter();
}
private WorkspacePath screenshotDir
{
get
{
// var fileSystem = workspace.fileSystem;
try
{
var directoryName =
"Screenshots"; //workspace.ReadBiosData("ScreenshotDir", "Screenshots") as string;
var path = WorkspacePath.Root.AppendDirectory("Tmp").AppendDirectory(directoryName);
try
{
if (workspace.Exists(WorkspacePath.Root.AppendDirectory("Workspace")))
path = WorkspacePath.Root.AppendDirectory("Workspace")
.AppendDirectory(directoryName);
}
catch
{
// Console.WriteLine("Screenshot Error: No workspace found.");
}
// Check to see if a screenshot directory exits
if (!workspace.Exists(path)) workspace.CreateDirectoryRecursive(path);
active = true;
return path;
}
catch
{
// Console.WriteLine("Save Screenshot Error:\n"+e.Message);
}
return WorkspacePath.Root;
}
}
public WorkspacePath GenerateScreenshotName()
{
return workspace.UniqueFilePath(screenshotDir.AppendFile("screenshot.png"));
}
public bool TakeScreenshot(PixelVision engine)
{
// throw new NotImplementedException();
var fileName = GenerateScreenshotName().Path;
if (active == false) return active;
try
{
// var cachedColors = engine.ColorChip.colors;
var cachedColors = ColorUtils.ConvertColors(engine.ColorChip.HexColors, "#FF00FF", true).Select(c=> new ColorData(c.R, c.G, c.B)).ToArray();
var pixels = engine.DisplayChip.Pixels;
var displaySize = engine.GameChip.Display();
var visibleWidth = displaySize.X;
var visibleHeight = displaySize.Y;
var width = engine.DisplayChip.Width;
// Need to crop the image
var newPixels = new ColorData[visibleWidth * visibleHeight];
var totalPixels = pixels.Length;
var newTotalPixels = newPixels.Length;
var index = 0;
for (var i = 0; i < totalPixels; i++)
{
var col = i % width;
if (col < visibleWidth && index < newTotalPixels)
{
newPixels[index] = cachedColors[pixels[i]];
index++;
}
}
// We need to do this manually since the exporter could be active and we don't want to break it for a screenshot
var tmpExporter = new ImageExporter(fileName, imageExporter, newPixels, visibleWidth, visibleHeight);
tmpExporter.CalculateSteps();
// Manually step through the exporter
while (tmpExporter.Completed == false) tmpExporter.NextStep();
workspace.SaveExporterFiles(new Dictionary<string, byte[]> {{tmpExporter.fileName, tmpExporter.Bytes}});
return true;
}
catch
{
// Console.WriteLine("Take Screenshot Error:\n"+e.Message);
// TODO throw some kind of error?
return false;
}
}
}
}