-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.cs
More file actions
156 lines (138 loc) · 5.37 KB
/
Block.cs
File metadata and controls
156 lines (138 loc) · 5.37 KB
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
using System;
using System.Drawing;
namespace DLMSoft.MySweeper {
class Block : IDisposable {
#region Static members
public static void LoadResources(Image icons) {
number_icons_ = new Image[8];
for (var i = 0; i < 8; i++) {
var x = i * 16;
var result = new Bitmap(16, 16);
var g = Graphics.FromImage(result);
g.Clear(Color.Transparent);
g.DrawImage(icons, new Rectangle(0, 0, 16, 16), x, 0, 16, 16, GraphicsUnit.Pixel);
g.Dispose();
number_icons_[i] = result;
}
status_images_ = new Image[5];
for (var i = 0; i < 5; i++) {
var x = i * 16;
var result = new Bitmap(16, 16);
var g = Graphics.FromImage(result);
g.Clear(Color.Transparent);
g.DrawImage(icons, new Rectangle(0, 0, 16, 16), x, 48, 16, 16, GraphicsUnit.Pixel);
g.Dispose();
status_images_[i] = result;
}
block_cover_brush_ = new SolidBrush(Color.Gray);
block_hover_brush_ = new SolidBrush(Color.Silver);
block_flipped_brush_ = new SolidBrush(Color.White);
}
static Image[] status_images_;
static Image[] number_icons_;
static Brush block_cover_brush_;
static Brush block_hover_brush_;
static Brush block_flipped_brush_;
public static int OffsetX { get; set; }
public static int OffsetY { get; set; }
public static int CellSize { get; set; }
#endregion
#region Properties
public bool IsMine { get; set; }
public BlockStatus Status { get; set; }
public int X { get; private set; }
public int Y { get; private set; }
public uint Number { get; set; }
public bool IsDisposed { get; private set; }
public bool Hovered { get; set; }
public bool ForceShow { get; set; }
#endregion
#region Constructors
public Block(int x, int y, bool is_mine) {
IsDisposed = false;
IsMine = is_mine;
Status = BlockStatus.Normal;
X = x;
Y = y;
block_rectangle_ = new Rectangle(X * (CellSize + 2) + OffsetX + 1, Y * (CellSize + 2) + OffsetY + 1, CellSize, CellSize);
Number = 0;
}
#endregion
#region Dispose
public void Dispose() {
if (IsDisposed)
return;
IsDisposed = true;
}
#endregion
#region HitTest
public bool HitTest(int x, int y) {
return block_rectangle_.Contains(new Point(x, y));
}
#endregion
#region Render
public void Render(Graphics g) {
if (IsDisposed)
return;
switch (Status) {
case BlockStatus.Normal :
if (Hovered) {
g.FillRectangle(block_hover_brush_, block_rectangle_);
}
else {
g.FillRectangle(block_cover_brush_, block_rectangle_);
}
if (ForceShow) {
if (IsMine) {
g.DrawImage(status_images_[0], block_rectangle_);
}
if (Number > 0)
g.DrawImage(number_icons_[Number - 1], block_rectangle_);
}
return;
case BlockStatus.Flipped :
g.FillRectangle(block_flipped_brush_, block_rectangle_);
if (Number == 0) {
return;
}
g.DrawImage(number_icons_[Number - 1], block_rectangle_);
return;
case BlockStatus.Exploded :
g.FillRectangle(block_flipped_brush_, block_rectangle_);
g.DrawImage(status_images_[1], block_rectangle_);
return;
case BlockStatus.Marked :
if (Hovered) {
g.FillRectangle(block_hover_brush_, block_rectangle_);
}
else {
g.FillRectangle(block_cover_brush_, block_rectangle_);
}
g.DrawImage(status_images_[2], block_rectangle_);
return;
case BlockStatus.WrongMark :
if (Hovered) {
g.FillRectangle(block_hover_brush_, block_rectangle_);
}
else {
g.FillRectangle(block_cover_brush_, block_rectangle_);
}
g.DrawImage(status_images_[3], block_rectangle_);
return;
case BlockStatus.Question :
if (Hovered) {
g.FillRectangle(block_hover_brush_, block_rectangle_);
}
else {
g.FillRectangle(block_cover_brush_, block_rectangle_);
}
g.DrawImage(status_images_[4], block_rectangle_);
return;
}
}
#endregion
#region Fields
Rectangle block_rectangle_;
#endregion
}
}