?? env.cs
字號:
using System;
using System.Drawing;
using System.Collections.Generic;
namespace Skyiv.Ben.PushBox.Common
{
/// <summary>
/// 工作環(huán)境
/// </summary>
sealed class Env : IDisposable
{
DataFile db; // 數(shù)據(jù)文件
ConfigFile cfg; // 配置文件
string errorMsg; // 錯誤信息
string debugMsg; // 調試信息
bool isReplay; // 是否正在回放
Action active; // 模式: 正常 新建 編輯 刪除
byte pen; // 設計時的筆
Bitmap img; // 圖形單元, 橫向被均勻分為八份
Stack<Step> stack; // 歷史路線, 用于后退功能
Size clientSize; // 工作區(qū)域尺寸(以像素為單位)
Size boxSize; // 圖形元素尺寸(以像素為單位)
Point toPixel; // 將要到達的位置(以像素為單位)
Point worker; // 當前工人位置(以單元格為單位)
int pushSteps; // 推動著箱子走的步數(shù)
int levelOem; // 原來的關數(shù),僅用于“菜單 -> 數(shù)據(jù) -> 設計 -> 新建”放棄后恢復現(xiàn)場
public string ErrorMsg { get { return errorMsg; } }
public string DebugMsg { get { return debugMsg; } }
public string[] Groups { get { return cfg.Groups; } }
public int Group { get { return cfg.Group; } set { cfg.Group = value; } }
public int Level { get { return cfg.Levels[Group]; } set { cfg.Levels[Group] = value; } }
public int LeveLOem { get { return levelOem; } }
public int MaxLevel { get { return db.MaxLevel; } }
public string Steps { get { return cfg.Steps; } }
public Size LevelSize { get { return db.LevelSize; } }
public Size ClientSize { set { clientSize = value; } }
public Point ToPixel { set { toPixel = value; } }
public int MaxLevelSize { get { return cfg.MaxLevelSize; } set { cfg.MaxLevelSize = value; } }
public int StepDelay { get { return cfg.StepDelay; } set { cfg.StepDelay = value; } }
public int ReplayDelay { get { return cfg.ReplayDelay; } set { cfg.ReplayDelay = value; } }
public Action Active { get { return active; } set { active = value; } }
public byte Pen { get { return pen; } set { pen = value; } }
public bool HasError { get { return !string.IsNullOrEmpty(errorMsg); } }
public bool HasWorker { get { return db.HasWorker; } }
public bool CanUndo { get { return stack.Count != 0; } }
public bool CanReplay { get { return db.IsFinished && !CanUndo; } }
public bool IsSave { get { return cfg.IsSave; } set { cfg.IsSave = value; } }
public bool IsFinish { get { return db.Tasks == db.Boths; } }
public bool IsReplay { get { return isReplay; } set { isReplay = value; } }
public bool IsDesign { get { return active != Action.None; } }
public Env()
{
stack = new Stack<Step>();
cfg = new ConfigFile();
db = new DataFile();
Init();
}
/// <summary>
/// 狀態(tài)欄信息
/// </summary>
public string StatusMessage
{
get
{
return HasError ? "請點擊“菜單 -> 幫助 -> 錯誤信息”" : string.Format(
"{0} {1}/{2} {3} {4} {5} [{6}] {7}",
(active == Action.Create) ? '+' : (active == Action.Edit) ? '=' : isReplay ? "|/-\\"[stack.Count % 4] : '>',
Level + 1, MaxLevel, Pub.ToString(LevelSize),
IsDesign ? string.Format("{0}={1}", db.Boxs, db.Slots) : string.Format("{0}/{1}", db.Boths, db.Tasks),
IsDesign ? Block.GetPenName(pen) : string.Format("{0}({1})", stack.Count, pushSteps),
IsDesign ? (active == Action.Create ? "新建" : "編輯") : db.IsFinished ?
string.Format("{0}({1})", db.MovedSteps, db.PushedSteps) : string.Empty,
db.GroupName);
}
}
public void Dispose()
{
db.Dispose();
}
public void Init()
{
active = Action.None;
pen = Block.Land;
stack.Clear();
SetExceptionMessage(null);
}
void SetExceptionMessage(Exception ex)
{
errorMsg = Pub.GetMessage(ex, false);
debugMsg = Pub.GetMessage(ex, true);
}
/// <summary>
/// 計算當使用標準箱子尺寸時主窗體客戶區(qū)的尺寸
/// </summary>
/// <param name="statusBarHeight">狀態(tài)條的高度</param>
/// <returns>客戶區(qū)的尺寸</returns>
public Size GetClientSize(int statusBarHeight)
{
int width = (Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox24.Width / 8) * LevelSize.Width;
int height = Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox24.Height * LevelSize.Height + statusBarHeight;
if (width < 240) width = 240;
if (height < 48) height = 48;
if (width > 1008) width = 1008;
if (height > 672) height = 672;
return new Size(width, height);
}
/// <summary>
/// 根據(jù)客戶區(qū)尺寸,計算箱子的尺寸,并相應設定要顯示的圖形單元
/// </summary>
public void SetBoxInfo()
{
if (HasError) return;
if (LevelSize.IsEmpty) return;
int rX = clientSize.Width / LevelSize.Width;
int rY = clientSize.Height / LevelSize.Height;
int r = Math.Min(rX, rY);
if (r >= 24) img = Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox24;
else if (r >= 20) img = Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox20;
else if (r >= 16) img = Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox16;
else img = Skyiv.Ben.PushBoxStd.Properties.Resources.PushBox12;
boxSize = new Size(img.Height, img.Width / 8);
}
/// <summary>
/// 裝入配置文件
/// </summary>
public void LoadConfig()
{
if (HasError) return;
try
{
cfg.LoadConfig();
}
catch (Exception ex)
{
SetExceptionMessage(ex);
}
}
/// <summary>
/// 保存組信息到配置文件
/// </summary>
/// <param name="groups">組信息</param>
public void SaveConfig(string[] groups)
{
if (HasError) return;
try
{
cfg.SaveConfig(groups);
}
catch (Exception ex)
{
SetExceptionMessage(ex);
}
}
/// <summary>
/// 保存當前選項及當前走法到配置文件
/// </summary>
public void SaveConfig()
{
if (HasError) return;
try
{
cfg.SaveConfig(stack.ToArray());
}
catch (Exception ex)
{
SetExceptionMessage(ex);
}
}
/// <summary>
/// 裝入當前組信息
/// </summary>
public void LoadGroup()
{
if (HasError) return;
try
{
db.LoadGroup(Groups[Group]);
}
catch (Exception ex)
{
SetExceptionMessage(ex);
}
}
/// <summary>
/// 裝入當前關信息
/// </summary>
public void LoadLevel()
{
active = Action.None;
if (HasError) return;
try
{
db.LoadLevel(Level);
worker = db.Worker;
stack.Clear();
pushSteps = 0;
isReplay = false;
}
catch (Exception ex)
{
SetExceptionMessage(ex);
}
}
/// <summary>
/// 新建一關
/// </summary>
/// <param name="isCopy">是否復制當前關</param>
/// <param name="size">新建關的尺寸</param>
public void NewLevel(bool isCopy, Size size)
{
if (HasError) return;
try
{
levelOem = Level;
Level = MaxLevel;
db.NewLevel(isCopy, size);
}
catch (Exception ex)
{
SetExceptionMessage(ex);
}
}
/// <summary>
/// 給出通關步驟
/// </summary>
/// <returns>通關步驟</returns>
public string GetSteps()
{
string steps = "";
if (!HasError)
{
try
{
steps = db.GetSteps(Level);
}
catch (Exception ex)
{
SetExceptionMessage(ex);
}
}
return steps;
}
/// <summary>
/// 記錄通關步驟
/// </summary>
public void Record()
{
if (HasError) return;
try
{
db.SaveLevel(Level, stack.ToArray(), pushSteps);
}
catch (Exception ex)
{
SetExceptionMessage(ex);
}
}
/// <summary>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -