📄 mainform.cs
字号:
////////////////////////////////////////////////
//
// Project: Lines.NET
// Version: 1.1
// Author: Vladimir L.
//
// homepage: http://www.boomsoft.org
// e-mail: support@boomsoft.org
//
// Copyright (c) 2003-2004, Boomsoft.org
//
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using Lines.GUI;
using Lines.Core;
using Lines.Utils;
namespace Lines
{
/// <summary>
/// Describes the main form class of Lines.NET game.
/// </summary>
/// <remarks>
/// This class bears three major functions in this project:
/// <list type="number">
/// <item>
/// <description>This class contains the main entry point for Lines.NET application:
/// <see cref="Main()"/>.</description>
/// </item>
/// <item>
/// <description>This class represents the main form of this game, where the <see cref="IndicatorCtrl"/>
/// and <see cref="BoardCtrl"/> are placed on and rendered. It also supports user menu to provide
/// an interface for additional game capabilites.</description>
/// </item>
/// <item>
/// <description>This class takes a role of controller from the point of view of MVC
/// (Model-View-Controller) pattern. In this case <b>Model</b> is represented by the classes from
/// <see cref="Lines.Core"/> namespace, <b>View</b> is located in the <see cref="Lines.GUI"/>
/// namespace, and all intercommunication between model and view is implemented in this class.
/// The <b>Model</b> classes are absolutely independent of <b>View</b> and <b>Controller</b>
/// and interacts with <b>Controller</b> via events mechanism.</description>
/// </item>
/// </list>
/// To use this class to launch a game simply create an instance of this class and call
/// <see cref="Application.Run(Form)"/> method with a reference to the created instance as it is shown
/// in an example.
/// </remarks>
/// <example>
/// <code>
/// ...
/// MainForm gameForm = new MainForm();
/// gameForm.Bounds = Screen.PrimaryScreen.WorkingArea;
/// Application.Run(gameForm);
/// </code>
/// </example>
public class MainForm : Form
{
#region Application menu objects
/// <summary>
/// The main menu of application.
/// </summary>
private MainMenu mainMenu;
/// <summary>
/// The 'New game' submenu item of 'Game' menu.
/// </summary>
private MenuItem menuItemNew;
/// <summary>
/// The 'Exit' submenu item of 'Game' menu.
/// </summary>
private MenuItem menuItemExit;
/// <summary>
/// The '?' menu.
/// </summary>
private MenuItem menuItemHelp;
/// <summary>
/// The 'About' submenu item of '?' menu.
/// </summary>
private MenuItem menuItemAbout;
/// <summary>
/// The delimiter in the 'Game' menu.
/// </summary>
private MenuItem menuItemDelimiter1;
/// <summary>
/// The 'Hi score' submenu item of 'Game' menu.
/// </summary>
private MenuItem menuItemRecords;
/// <summary>
/// The 'Game' menu.
/// </summary>
private MenuItem menuItemGame;
/// <summary>
/// The 'Options' menu.
/// </summary>
private MenuItem menuItemOptions;
/// <summary>
/// The 'Sound' submenu item of 'Options' menu.
/// </summary>
private MenuItem menuItemSound;
/// <summary>
/// The 'One-by-one destruction' submenu item of 'Options' menu.
/// </summary>
private MenuItem menuItemOneByOne;
/// <summary>
/// The 'Refresh view' submenu item of 'Options' menu.
/// </summary>
private MenuItem menuItemRefresh;
#endregion
/// <summary>
/// Referes to the indicator control that displays the current game status.
/// </summary>
private IndicatorCtrl indicatorCtrl;
/// <summary>
/// Refers to the board control that renders the game board on the screen.
/// </summary>
private BoardCtrl boardCtrl;
/// <summary>
/// This timer is used to draw jumping ball.
/// </summary>
private Timer ballJump;
/// <summary>
/// This timer is an ad hoc for threading problem in .NET Compact Framework.
/// </summary>
/// <remarks>
/// This timer is taking care of tracking the game over event that will be fired
/// by the instance of <see cref="Game"/> class.
/// </remarks>
private Timer gameOverWatchDog;
/// <summary>
/// Refers to the <see cref="Game"/> object of Lines.NET game.
/// </summary>
private Game game = new Game();
/// <summary>
/// The graphics and color preferences of this game.
/// </summary>
private Preferences preferences = new Preferences();
/// <summary>
/// Holds the constant of a menu size.
/// </summary>
/// <remarks>
/// <p>It is an important constant for application if it runs under Windows CE.NET 4.x operation
/// system since the menu is placed on the top of a dialog and isn't excluded from the
/// <see cref="Screen.WorkingArea"/> property. For Pocket PC version this constant
/// isn't needed. See the meaning of <see cref="AppSettings.MagicPadding"/>.</p>
/// <p>Because of the lack of capability to detect the size of menu in .NET Compact Framework
/// the size of menu is stored in application settings property
/// <see cref="AppSettings.MagicPadding"/>.</p>
/// </remarks>
private int menuSize;
/// <summary>
/// The move ball sound.
/// </summary>
private Sound soundMove;
/// <summary>
/// The destroy ball sound.
/// </summary>
private Sound soundDestroy;
/// <summary>
/// The wrong way sound.
/// </summary>
private Sound soundBlocked;
/// <summary>
/// Creates an instance of MainForm dialog.
/// </summary>
public MainForm()
{
menuSize = AppSettings.Instance.MagicPadding;
//
// Required for Windows Form Designer support
//
ExtraInitializeComponent();
InitializeComponent();
InitializeSounds();
indicatorCtrl.Game = game;
indicatorCtrl.Preferences = preferences;
boardCtrl.Game = game;
boardCtrl.Preferences = preferences;
game.BallAdd += new Game.BallEventHandler(game_BallAdd);
game.BallDelete += new Game.BallEventHandler(game_BallDelete);
game.BallsDelete += new Game.BallsEventHandler(game_BallsDelete);
game.BallMove += new Game.BallMoveEventHandler(game_BallMove);
game.BallMoved += new Game.BallEventHandler(game_BallMoved);
game.NewGame();
// Additional initialization from properties
menuItemOneByOne.Checked = AppSettings.Instance.OneByOneDestroy;
game.OneByOneDestruction = AppSettings.Instance.OneByOneDestroy;
menuItemSound.Checked = AppSettings.Instance.UseSound;
if (menuItemSound.Checked)
{
game.DestroyBallPause = AppSettings.Instance.PauseDestroyBall;
}
else
{
game.DestroyBallPause = 0;
}
}
/// <summary>
/// Creates <see cref="Sound"/> objects for each of all different sounds that are supported by
/// Lines.NET game.
/// </summary>
/// <remarks>
/// The file location for each sound might be changed in <b>settings.xml</b> (See
/// <see cref="AppSettings"/> for more detailes). Sound file must be recorded in *.wav format.
/// </remarks>
private void InitializeSounds()
{
soundMove = new Sound(PathHelper.GetAppFile(AppSettings.Instance.SoundMoveFile));
soundDestroy = new Sound(PathHelper.GetAppFile(AppSettings.Instance.SoundDestroyFile));
soundBlocked = new Sound(PathHelper.GetAppFile(AppSettings.Instance.SoundBlockedFile));
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.mainMenu = new System.Windows.Forms.MainMenu();
this.menuItemGame = new System.Windows.Forms.MenuItem();
this.menuItemNew = new System.Windows.Forms.MenuItem();
this.menuItemRecords = new System.Windows.Forms.MenuItem();
this.menuItemDelimiter1 = new System.Windows.Forms.MenuItem();
this.menuItemExit = new System.Windows.Forms.MenuItem();
this.menuItemOptions = new System.Windows.Forms.MenuItem();
this.menuItemSound = new System.Windows.Forms.MenuItem();
this.menuItemOneByOne = new System.Windows.Forms.MenuItem();
this.menuItemHelp = new System.Windows.Forms.MenuItem();
this.menuItemAbout = new System.Windows.Forms.MenuItem();
this.ballJump = new System.Windows.Forms.Timer();
this.gameOverWatchDog = new System.Windows.Forms.Timer();
this.menuItemRefresh = new System.Windows.Forms.MenuItem();
//
// mainMenu
//
this.mainMenu.MenuItems.Add(this.menuItemGame);
this.mainMenu.MenuItems.Add(this.menuItemOptions);
this.mainMenu.MenuItems.Add(this.menuItemHelp);
//
// menuItemGame
//
this.menuItemGame.MenuItems.Add(this.menuItemNew);
this.menuItemGame.MenuItems.Add(this.menuItemRecords);
this.menuItemGame.MenuItems.Add(this.menuItemDelimiter1);
this.menuItemGame.MenuItems.Add(this.menuItemExit);
this.menuItemGame.Text = "&Game";
//
// menuItemNew
//
this.menuItemNew.Text = "&New game";
this.menuItemNew.Click += new System.EventHandler(this.menuItemNew_Click);
//
// menuItemRecords
//
this.menuItemRecords.Text = "&Hi Score";
this.menuItemRecords.Click += new System.EventHandler(this.menuItemRecords_Click);
//
// menuItemDelimiter1
//
this.menuItemDelimiter1.Text = "-";
//
// menuItemExit
//
this.menuItemExit.Text = "E&xit";
this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
//
// menuItemOptions
//
this.menuItemOptions.MenuItems.Add(this.menuItemSound);
this.menuItemOptions.MenuItems.Add(this.menuItemOneByOne);
this.menuItemOptions.MenuItems.Add(this.menuItemRefresh);
this.menuItemOptions.Text = "&Options";
//
// menuItemSound
//
this.menuItemSound.Text = "&Sound";
this.menuItemSound.Click += new System.EventHandler(this.menuItemSound_Click);
//
// menuItemOneByOne
//
this.menuItemOneByOne.Text = "&One-by-one destruction";
this.menuItemOneByOne.Click += new System.EventHandler(this.menuItemOneByOne_Click);
//
// menuItemHelp
//
this.menuItemHelp.MenuItems.Add(this.menuItemAbout);
this.menuItemHelp.Text = "&?";
//
// menuItemAbout
//
this.menuItemAbout.Text = "&About";
this.menuItemAbout.Click += new System.EventHandler(this.menuItemAbout_Click);
//
// ballJump
//
this.ballJump.Enabled = true;
this.ballJump.Tick += new System.EventHandler(this.ballJump_Tick);
//
// gameOverWatchDog
//
this.gameOverWatchDog.Enabled = true;
this.gameOverWatchDog.Tick += new System.EventHandler(this.gameOverWatchDog_Tick);
//
// menuItemRefresh
//
this.menuItemRefresh.Text = "&Refresh view";
this.menuItemRefresh.Click += new System.EventHandler(this.menuItemRefresh_Click);
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(442, 830);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Menu = this.mainMenu;
this.Text = "Lines.NET";
this.Resize += new System.EventHandler(this.MainForm_Resize);
this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
this.Load += new System.EventHandler(this.MainForm_Load);
}
private void ExtraInitializeComponent()
{
this.boardCtrl = new BoardCtrl();
this.indicatorCtrl = new IndicatorCtrl();
//
// Indicator
//
Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
int minDimension = workingArea.Height < workingArea.Width ? workingArea.Height : workingArea.Width;
this.indicatorCtrl.Location = new System.Drawing.Point(0, menuSize);
this.indicatorCtrl.Height = minDimension * 10 / 100; // 10% of min. dimension
//
// Field
//
this.boardCtrl.Location = new System.Drawing.Point(0, menuSize + this.indicatorCtrl.Height);
this.boardCtrl.CellClick += new Lines.GUI.BoardCtrl.CellEventHandler(boardCtrl_CellClick);
this.boardCtrl.BallClick += new Lines.GUI.BoardCtrl.BallEventHandler(boardCtrl_BallClick);
this.Controls.Add(this.indicatorCtrl);
this.Controls.Add(this.boardCtrl);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
MainForm mainForm = new MainForm();
mainForm.Bounds = workingArea;
Application.Run(mainForm);
}
/// <summary>
/// Handles <b>Load</b> event of a form.
/// </summary>
/// <param name="sender"></param>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -