using System; using System.Collections.Generic; using System.ComponentModel; //using System.Drawing; using System.Data; using System.Text; using System.Xml; using System.Windows.Forms; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework; using Nuclex; namespace OperationDrawDown { public partial class OperationDrawDown : Nuclex.GameControl { private SceneGraph sceneGraph; private Actors actors; public static SpriteBatch spriteBatch; public static ContentManager content; private enum GameMode { Menu, Game }; private GameMode gameMode; List selectedCells; bool isMouseSelecting = false; bool isSelectionMoving = false; SelectionRectangle rectangleA; SelectionRectangle rectangleB; Point lastMousePosition = Point.Zero; public OperationDrawDown() { InitializeComponent(); //graphics = new GraphicsDeviceManager(this); } protected override void Initialize() { sceneGraph = new SceneGraph(); actors = new Actors(); content = new ContentManager(Services); content.RootDirectory = "Content"; spriteBatch = new SpriteBatch(this.graphics.GraphicsDevice); selectedCells = new List(); rectangleA = new SelectionRectangle(); rectangleA.Color_Set(0.0f, 1.0f, 0.0f, 0.5f); rectangleB = new SelectionRectangle(); rectangleB.Color_Set(1.0f, 0.0f, 0.0f, 0.5f); LoadScene("_xml/Level1.xml"); gameMode = GameMode.Game; base.Initialize(); } private void LoadScene(string xmlHotZoneFilePath) { XmlDocument reader = new XmlDocument(); reader.Load(xmlHotZoneFilePath); foreach (XmlNode hotZoneNode in reader.ChildNodes) { if (hotZoneNode.Name == "HotZone") { // Get World Scale Cell.scale = int.Parse(hotZoneNode.Attributes["glyphPixelScale"].Value); // Unpack Content/Assets foreach (XmlNode node in hotZoneNode.ChildNodes) { if (node.Name == "SceneGraph") { // Create SceneGraph sceneGraph.Load(node); } else if (node.Name == "Players") { // Create List of Actors actors.Load(node); } } } } } protected override void Update(Microsoft.Xna.Framework.GameTime gameTime) { //if (gameMode == GameMode.Menu) //{ //} //else if (gameMode == GameMode.Game) //{ //} base.Update(gameTime); } protected override void Draw(Microsoft.Xna.Framework.GameTime gameTime) { graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Graphics.Color.Black); Viewport viewport = graphics.GraphicsDevice.Viewport; if (this.gameMode == GameMode.Menu) { } else if (this.gameMode == GameMode.Game) { OperationDrawDown.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None); sceneGraph.Draw(); actors.Draw(); for (int i = 0; i < selectedCells.Count; i++) { selectedCells[i].Draw(); } rectangleA.Draw(); rectangleB.Draw(); OperationDrawDown.spriteBatch.End(); } base.Draw(gameTime); } private void MouseDown_Click(object sender, MouseEventArgs args) { // Left Mouse Button if (args.Button == MouseButtons.Left) { // If user clicked w/in selection if (this.rectangleA.Contains(args.X,args.Y)) { // Flag that this blocks are moving this.isSelectionMoving = true; // Init Rectangle B, containing the eventual moveTo position this.rectangleB.SetStartPoint(this.rectangleA.StartX, this.rectangleA.StartY); this.rectangleB.SetEndPoint(this.rectangleA.EndX, this.rectangleA.EndY); } else { // Flag the user is selecting blocks this.isMouseSelecting = true; // Set first point in selection rectangle this.rectangleA.SetStartPoint(args.X,args.Y); this.rectangleA.SetEndPoint(args.X, args.Y); this.rectangleB.SetStartPoint(0, 0); this.rectangleB.SetEndPoint(0, 0); } } // Right Mouse Button if (args.Button == MouseButtons.Right) { // Launch Forms right click context menu contextMenuStrip1.SetBounds( args.X, args.Y, contextMenuStrip1.Size.Width, contextMenuStrip1.Size.Height); contextMenuStrip1.Show(); } } private void MouseUp_Click(object sender, MouseEventArgs args) { if (this.isMouseSelecting) { this.isMouseSelecting = false; // Snap selection to block boundaries rectangleA.DescretizeSelection(Cell.scale); // Add Selected Cells to List selectedCells.Clear(); } if (this.isSelectionMoving) { this.isSelectionMoving = false; sceneGraph.CopyCells_ByScreenCoord( this.rectangleA, rectangleB.StartX - rectangleA.StartX, rectangleB.StartY - rectangleA.StartY ); this.selectedCells.Clear(); } } private void MouseMotion(object sender, MouseEventArgs args) { if (this.isMouseSelecting) { // Set Selection Block Width and Height this.rectangleA.SetEndPoint(args.X,args.Y); } if (this.isSelectionMoving) { // Move Selection Rectangle this.rectangleB.MoveTo(args.X, args.Y); } } } }