-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canvas.cs
122 lines (102 loc) · 3.63 KB
/
Canvas.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
using GBJamGame.Enums;
using GBJamGame.Globals;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace GBJamGame
{
internal class Canvas
{
private readonly int _height;
private readonly RenderTarget2D _result;
private readonly LinkedList<ColorIndex[]> _snapshots;
private readonly int _width;
private bool _isDirty;
private ColorIndex[] _pixels;
public Canvas(GraphicsDevice graphicsDevice, int width, int height)
{
_width = width;
_height = height;
_result = new RenderTarget2D(graphicsDevice, width, height);
_snapshots = new LinkedList<ColorIndex[]>();
Clear();
}
public void Clear()
{
_pixels = new ColorIndex[_width * _height];
_isDirty = true;
}
public void Snapshot()
{
var copy = new ColorIndex[_width * _height];
Array.Copy(_pixels, copy, _pixels.Length);
_snapshots.AddFirst(copy);
if (_snapshots.Count > 20)
_snapshots.RemoveLast();
}
public bool Undo()
{
if (_snapshots.Count > 0)
{
_pixels = _snapshots.First.Value;
_snapshots.RemoveFirst();
_isDirty = true;
return true;
}
return false;
}
public void SetFromTexture(Texture2D texture)
{
var data = new Color[_width * _height];
texture.GetData(data);
for (var i = 0; i < data.Length; i++)
if (data[i] == Constants.ToneColor1)
SetPixel(i % _width, i / _width, ColorIndex.Color1);
else if (data[i] == Constants.ToneColor2)
SetPixel(i % _width, i / _width, ColorIndex.Color2);
else if (data[i] == Constants.ToneColor3)
SetPixel(i % _width, i / _width, ColorIndex.Color3);
else if (data[i] == Constants.ToneColor4)
SetPixel(i % _width, i / _width, ColorIndex.Color4);
else
SetPixel(i % _width, i / _width, ColorIndex.Color1);
}
public ColorIndex GetPixel(int x, int y)
{
if (x < 0 || x >= _width || y < 0 || y >= _height)
return ColorIndex.Transparent;
return _pixels[x + y * _width];
}
public bool SetPixel(int x, int y, ColorIndex color)
{
if (GetPixel(x, y) == color)
return false;
if (x < 0 || x >= _width || y < 0 || y >= _height)
return false;
_pixels[x + y * _width] = color;
_isDirty = true;
return true;
}
public Texture2D GetScreenTexture()
{
if (_isDirty)
{
var copy = new Color[_width * _height];
for (var i = 0; i < _pixels.Length; i++)
copy[i] = _pixels[i] switch
{
ColorIndex.Transparent => Color.Transparent,
ColorIndex.Color1 => Constants.ToneColor1,
ColorIndex.Color2 => Constants.ToneColor2,
ColorIndex.Color3 => Constants.ToneColor3,
ColorIndex.Color4 => Constants.ToneColor4,
_ => Constants.ToneColor1
};
_result.SetData(copy);
}
_isDirty = false;
return _result;
}
}
}