This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSurface.cs
165 lines (140 loc) · 4.2 KB
/
Surface.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
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace OpenRA.TilesetBuilder
{
class Surface : Control
{
public Bitmap Image;
public int[,] TerrainTypes;
public List<Template> Templates = new List<Template>();
public string InputMode;
public Bitmap[] Icon;
public int TileSize;
public int TilesPerRow;
public event Action<int, int, int> UpdateMouseTilePosition = (x, y, t) => { };
Template currentTemplate;
ImageList imagesListControl;
bool showTerrainTypes;
public bool ShowTerrainTypes
{
get { return showTerrainTypes; }
set { showTerrainTypes = value; }
}
public ImageList ImagesList
{
get { return imagesListControl; }
set { imagesListControl = value; }
}
public Surface()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
UpdateStyles();
}
Brush currentBrush = new SolidBrush(Color.FromArgb(60, Color.White));
protected override void OnPaint(PaintEventArgs e)
{
if (Image == null || TerrainTypes == null || Templates == null)
return;
/* draw the background */
e.Graphics.DrawImage(Image, 0, 0, Image.Width, Image.Height);
/* draw terrain type overlays */
if (ShowTerrainTypes)
{
for (var i = 0; i <= TerrainTypes.GetUpperBound(0); i++)
for (var j = 0; j <= TerrainTypes.GetUpperBound(1); j++)
if (TerrainTypes[i, j] != 0)
{
////e.Graphics.FillRectangle(Brushes.Black, TileSize * i + 8, TileSize * j + 8, 16, 16);
e.Graphics.DrawImage(Icon[TerrainTypes[i, j]], TileSize * i + 8, TileSize * j + 8, 16, 16);
////e.Graphics.DrawString(TerrainTypes[i, j].ToString(),
////Font, Brushes.LimeGreen, TileSize * i + 10, TileSize * j + 10);
}
}
/* draw template outlines */
foreach (var t in Templates)
{
var pen = Pens.White;
foreach (var c in t.Cells.Keys)
{
if (currentTemplate == t)
e.Graphics.FillRectangle(currentBrush, TileSize * c.X, TileSize * c.Y, TileSize, TileSize);
if (!t.Cells.ContainsKey(c + new int2(-1, 0)))
{
var a = TileSize * c;
var b = TileSize * (c + new int2(0, 1));
e.Graphics.DrawLine(pen, new Point(a.X, a.Y), new Point(b.X, b.Y));
}
if (!t.Cells.ContainsKey(c + new int2(+1, 0)))
{
var a = TileSize * (c + new int2(1, 0));
var b = TileSize * (c + new int2(1, 1));
e.Graphics.DrawLine(pen, new Point(a.X, a.Y), new Point(b.X, b.Y));
}
if (!t.Cells.ContainsKey(c + new int2(0, +1)))
{
var a = TileSize * (c + new int2(0, 1));
var b = TileSize * (c + new int2(1, 1));
e.Graphics.DrawLine(pen, new Point(a.X, a.Y), new Point(b.X, b.Y));
}
if (!t.Cells.ContainsKey(c + new int2(0, -1)))
{
var a = TileSize * c;
var b = TileSize * (c + new int2(1, 0));
e.Graphics.DrawLine(pen, new Point(a.X, a.Y), new Point(b.X, b.Y));
}
}
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
var pos = new int2(e.X / TileSize, e.Y / TileSize);
if (InputMode == null)
{
if (e.Button == MouseButtons.Left)
{
currentTemplate = Templates.FirstOrDefault(t => t.Cells.ContainsKey(pos));
if (currentTemplate == null)
Templates.Add(currentTemplate = new Template { Cells = new Dictionary<int2, bool> { { pos, true } } });
Invalidate();
}
if (e.Button == MouseButtons.Right)
{
Templates.RemoveAll(t => t.Cells.ContainsKey(pos));
currentTemplate = null;
Invalidate();
}
}
else
{
TerrainTypes[pos.X, pos.Y] = int.Parse(InputMode);
Invalidate();
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
var pos = new int2(e.X / TileSize, e.Y / TileSize);
if (InputMode == null)
{
if (e.Button == MouseButtons.Left && currentTemplate != null)
{
if (!currentTemplate.Cells.ContainsKey(pos))
{
currentTemplate.Cells[pos] = true;
Invalidate();
}
}
}
UpdateMouseTilePosition(pos.X, pos.Y, (pos.Y * TilesPerRow) + pos.X);
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
}
}