📄 mapviewer.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace GrapeCity.Competition.TreasureHouse
{
class MapViewer : Control
{
public MapViewer()
{
this.ResizeRedraw = true;
this.DoubleBuffered = true;
}
private Map _map;
public Map Map
{
get
{
return this._map;
}
set
{
if (this._map != value)
{
if (this._map != null)
{
this._map.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(_map_PropertyChanged);
}
this._map = value;
if (this._map != null)
{
this._map.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_map_PropertyChanged);
}
this.Invalidate();
}
}
}
void _map_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
this.Invalidate();
}
public Size MapSize
{
get
{
if (this.Map != null)
{
return this.Map.Size;
}
else
{
return Size.Empty;
}
}
}
public Size UnitSize
{
get
{
if (this.MapSize == Size.Empty)
{
return Size.Empty;
}
else
{
Size size = this.ClientSize;
return new Size(size.Width / this.MapSize.Width, size.Height / this.MapSize.Height);
}
}
}
private bool _showGridLine;
public bool ShowGridLine
{
get
{
return this._showGridLine;
}
set
{
this._showGridLine = value;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
if (!this.MapSize.IsEmpty)
{
for (int x = 0; x < this.MapSize.Width; x++)
{
for (int y = 0; y < this.MapSize.Height; y++)
{
Rectangle area = this.GetArea(new Point(x, y));
if (e.ClipRectangle.IntersectsWith(area))
{
Brush background = null;
int index = -1;
if (this.Positions != null)
{
index = this.Positions.IndexOf(new Point(x, y));
}
if (this._isReplaying)
{
if (this.Positions[_currentStep].X == x && this.Positions[_currentStep].Y == y)
{
background = Brushes.Pink;
}
else if (x == 0 && y == 0)
{
background = Brushes.LightGreen;
}
else if (index >= 0 && index < _currentStep)
{
background = Brushes.LightBlue;
}
}
else
{
if (index >= 0)
{
background = Brushes.LightBlue;
}
if (x == 0 && y == 0)
{
background = Brushes.LightGreen;
}
}
this.DrawUnit(e.Graphics, area, this.Map[x, y], background);
}
}
}
}
base.OnPaint(e);
}
private void DrawUnit(Graphics graphics, Rectangle area, Tag tag, Brush background)
{
if (background != null)
{
graphics.FillRectangle(background, area);
}
if (this.ShowGridLine)
{
graphics.DrawRectangle(Pens.Gray, area);
}
switch (tag)
{
case GrapeCity.Competition.TreasureHouse.Tag.Path:
break;
case GrapeCity.Competition.TreasureHouse.Tag.Wall:
using (HatchBrush hb = new HatchBrush(HatchStyle.HorizontalBrick, Color.Black, Color.White))
{
graphics.FillRectangle(hb, area);
}
break;
case GrapeCity.Competition.TreasureHouse.Tag.Coin:
area.Inflate(-area.Width / 3, -area.Height / 3);
graphics.DrawImage(GrapeCity.Competition.TreasureHouse.Properties.Resources.Coin, area);
break;
case GrapeCity.Competition.TreasureHouse.Tag.Gem:
area.Inflate(-area.Width / 4, -area.Height / 4);
graphics.DrawImage(GrapeCity.Competition.TreasureHouse.Properties.Resources.Gem, area);
break;
case GrapeCity.Competition.TreasureHouse.Tag.HolyA:
area.Inflate(-area.Width / 4, -area.Height / 4);
graphics.DrawImage(GrapeCity.Competition.TreasureHouse.Properties.Resources.HolyA, area);
break;
case GrapeCity.Competition.TreasureHouse.Tag.HolyB:
area.Inflate(-area.Width / 4, -area.Height / 4);
graphics.DrawImage(GrapeCity.Competition.TreasureHouse.Properties.Resources.HolyB, area);
break;
case GrapeCity.Competition.TreasureHouse.Tag.HolyC:
area.Inflate(-area.Width / 4, -area.Height / 4);
graphics.DrawImage(GrapeCity.Competition.TreasureHouse.Properties.Resources.HolyC, area);
break;
default:
break;
}
}
private static StringFormat _textFormat;
private static StringFormat TextFormat
{
get
{
if (_textFormat == null)
{
_textFormat = new StringFormat();
_textFormat.Alignment = StringAlignment.Center;
_textFormat.LineAlignment = StringAlignment.Center;
}
return _textFormat;
}
}
public Point HitTest(Point mouse)
{
Size unit = this.UnitSize;
if (unit.IsEmpty)
{
return Point.Empty;
}
if (mouse.X < 0 || mouse.X >= unit.Width * this.Map.Size.Width
|| mouse.Y < 0 || mouse.Y >= unit.Height * this.Map.Size.Height)
{
return Point.Empty;
}
return new Point(mouse.X / unit.Width, mouse.Y / unit.Height);
}
public Rectangle GetArea(Point position)
{
Size unit = this.UnitSize;
return new Rectangle(new Point(position.X * unit.Width, position.Y * unit.Height), unit);
}
private List<Point> _positions;
public List<Point> Positions
{
get
{
return this._positions;
}
set
{
if (this._positions != value)
{
this._positions = value;
this.Invalidate();
}
}
}
public void Replay()
{
if (this.Positions == null)
{
return;
}
_isReplaying = true;
for (this._currentStep = 0; this._currentStep < this.Positions.Count; this._currentStep++)
{
this.Refresh();
System.Threading.Thread.Sleep(100);
}
_isReplaying = false;
}
private bool _isReplaying = false;
private int _currentStep = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -