//---------------------------------------------------------------------------------------------------------------------------------------------------
//
// Copyright (C)2007 DarkWynter Studios. All rights reserved.
//
//---------------------------------------------------------------------------------------------------------------------------------------------------
// {Contact : darkwynter.com for licensing information
//---------------------------------------------------------------------------------------------------------------------------------------------------
#define DEBUG_MODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Windows.Forms;
namespace DarkWynter.App
{
public partial class GameObjectControl : UserControl
{
int zoomRate = 5;
OpenFileDialog fileDialog;
public GameObjectControl()
{
InitializeComponent();
// Set up Drag Drop to terrain
this.terrain.AllowDrop = true;
this.terrain.DragEnter += new DragEventHandler(terrain_DragEnter);
this.terrain.DragDrop += new DragEventHandler(DragDrop_Receive);
this.MouseWheel += new MouseEventHandler(TerrainZoom);
ScalePictureBoxToFit();
}
#region Level Map
///
/// Resize picture box on resize event
///
private void splitContainer2_SplitterMoved(object sender, SplitterEventArgs e)
{
ScalePictureBoxToFit();
}
///
/// Resize picture box on resize event
///
private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
{
ScalePictureBoxToFit();
}
///
/// Smooth Scaling by reinterpolating the image
///
///
///
///
///
private void ResizeImage(String Image2Resize, String Image2Resize2, int poSizeWidth, int poSizeHeight)
{
Image poImage = Image.FromFile(Image2Resize);
Image Original = poImage.Clone() as Image;
if (Original.Height > Original.Width)
{
int TempValue;
TempValue = poSizeWidth;
poSizeWidth = poSizeHeight;
poSizeHeight = TempValue;
}
Image ResizedImage = new Bitmap(poSizeWidth, poSizeHeight, Original.PixelFormat);
Graphics oGraphic = Graphics.FromImage(ResizedImage);
Rectangle oRectangle = new Rectangle(0, 0, poSizeWidth, poSizeHeight);
oGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
oGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
oGraphic.DrawImage(Original, oRectangle);
oGraphic.Dispose();
Original.Dispose();
// ResizedImage.Save(Image2Resize2, System.Drawing.Imaging.ImageFormat.Jpeg);
}
///
/// Scale picture box to fit to current control size and image size
///
private void ScalePictureBoxToFit()
{
if (this.terrain == null)
{
// set size of picture box the same as client size
}
else if (//this.pictureBoxSizeMode == PictureBoxSizeMode.Zoom ||
(this.terrain.Width <= this.splitContainer2.Panel2.Width &&
this.terrain.Height <= this.splitContainer2.Panel2.Height))
{
// set size of picture box as not bigger
// than client size according to current image
this.terrain.Width = this.splitContainer2.Panel2.Width;
this.terrain.Height = this.splitContainer2.Panel2.Height;
//this.BackgroundImageLayout = ImageLayout.Stretch;
}
else
{
// set size of picture box according
// to current scale percent selected
this.splitContainer2.Panel2.AutoScroll = true; // let the control scrollable
}
// set cursor for picture box
this.terrain.Invalidate();
}
private void TerrainZoom(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.BackgroundImageLayout = ImageLayout.Center;
int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
int diffZoom = numberOfTextLinesToMove * zoomRate;
if (diffZoom != 0)
{
this.terrain.Width += diffZoom;
this.terrain.Height += diffZoom;
}
this.Invalidate();
}
///
/// Copy the new image
///
///
///
void terrain_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
#endregion
private void buttonSelectFileModel_Click(object sender, EventArgs e)
{
fileDialog.Title = "Select Model File";
fileDialog.Filter = "X model File (*.x)|*.x|All files (*.*)|*.*";
fileDialog.FilterIndex = 1;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
//textBoxModelFile.Text = fileDialog.FileName;
}
}
private void DragDrop_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//this.listView_LevelIndex.DoDragDrop(this.waypoint.Image, DragDropEffects.All);
}
if (e.Button == MouseButtons.Right)
{
// this is where we needtodothe arrow change direction thing
}
}
void DragDrop_Receive(object sender, DragEventArgs e)
{
Bitmap bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
//Get graphics from the Image in the PictureBox1 which is the drop destination.
Graphics g = Graphics.FromImage(this.terrain.Image);
Point location = this.terrain.PointToClient(new Point(e.X, e.Y));
//Draw the draged image onto the destinational image
g.DrawImage(bmp, location);
//Force the PictureBox to redraw himself
this.terrain.Invalidate();
}
}
///
/// A collection of Levels, or acting stages.
/// Each level contains it's own library of objects.
///
public class LevelNode
{
public List objectNodes;
#region Interface
private string _name;
private string _xmlFile;
private string _description;
public string name { get { return _name; } set { _name = value; } }
public string xmlFile { get { return _xmlFile; } set { _xmlFile = value; } }
public string description { get { return _description; } set { _description = value; } }
#endregion
///
/// Load level description info from the Level Index Node.
/// Then open each Level.xml file and load ObjectNodes for this level into objectNodesList.
///
///
public LevelNode(XmlNode levelIndexNode)
{
// Process Level Index Properties
name = levelIndexNode.Attributes["name"].Value;
xmlFile = levelIndexNode.Attributes["xmlFile"].Value;
description = levelIndexNode.Attributes["description"].Value;
// Read the Level File
try
{
XmlDocument reader = new XmlDocument();
reader.Load(xmlFile);
// Get all outer nodes (incl xml version info and level)
foreach (XmlNode xmlLevelNode in reader.ChildNodes)
{
// If level definition
if (xmlLevelNode.Name == "level")
{
// Create GameObject from Xml
objectNodes = new List();
for (int nodeCount = 0; nodeCount < xmlLevelNode.ChildNodes.Count; nodeCount++)
{
XmlNode gameObjectNode = xmlLevelNode.ChildNodes[nodeCount];
if (gameObjectNode.Attributes != null)
{
// Load GameObjectNode and add to List.
objectNodes.Add(new ObjectNode(gameObjectNode));
}
}
}
}
}
catch
{
System.Diagnostics.Debug.WriteLine("Error reading xml: " + xmlFile);
// throw new Exception("Error reading XML");
}
}
public string ToXml()
{
string xml =
"";
return xml;
}
}
///
/// A collection of GameObjects.
/// Each GameObject contains it's own Properties
///
public class ObjectNode
{
public PropertyNode propertyNode;
#region Interface
#endregion
public ObjectNode(XmlNode gameObjectNode)
{
// Create new Properties node and add to List.
propertyNode = new PropertyNode(gameObjectNode);
}
public string ToXml()
{
string xml =
"";
return xml;
}
}
///
/// A collection of Properties making up a GameObject.
///
public class PropertyNode
{
#region Interface
///
/// Defines the coordinate system the object is working in.
///
public enum GridType { unit, coord, tile };
private string _name;
private string _type;
private string _id;
private string _x;
private string _z;
private GridType _grid;
private string _typeID;
private string _model;
private string _texture;
private string _bumpTexture;
private string _maxScale;
private string _animated;
public string name { get { return _name; } set { _name = value; } }
public string type { get { return _type; } set { _type = value; } }
public string id { get { return _id; } set { _id = value; } }
public string x { get { return _x; } set { _x = value; } }
public string z { get { return _z; } set { _z = value; } }
public GridType grid { get { return _grid; } set { _grid = value; } }
public string typeID { get { return _typeID; } set { _typeID = value; } }
public string model { get { return _model; } set { _model = value; } }
public string texture { get { return _texture; } set { _texture = value; } }
public string bumpTexture { get { return _bumpTexture; } set { _bumpTexture = value; } }
public string maxScale { get { return _maxScale; } set { _maxScale = value; } }
public string animated { get { return _animated; } set { _animated = value; } }
#endregion
public PropertyNode(XmlNode gameObjectNode)
{
if (gameObjectNode.Name != null)
{type = gameObjectNode.Name;}
if (gameObjectNode.Attributes["name"] != null)
{name = gameObjectNode.Attributes["name"].Value;}
if (gameObjectNode.Attributes["id"] != null)
{ id = gameObjectNode.Attributes["id"].Value; }
if (gameObjectNode.Attributes["x"] != null)
{ x = gameObjectNode.Attributes["x"].Value; }
if (gameObjectNode.Attributes["z"] != null)
{ z = gameObjectNode.Attributes["z"].Value; }
if (gameObjectNode.Attributes["grid"] != null)
{
switch (gameObjectNode.Attributes["grid"].Value)
{
case "unit":
grid = PropertyNode.GridType.unit;
break;
case "tile":
grid = PropertyNode.GridType.tile;
break;
case "coord":
grid = PropertyNode.GridType.coord;
break;
default:
grid = PropertyNode.GridType.unit;
break;
}
}
if (gameObjectNode.Attributes["typeID"] != null)
{ typeID = gameObjectNode.Attributes["typeID"].Value; }
if (gameObjectNode.Attributes["model"] != null)
{ model = gameObjectNode.Attributes["model"].Value; }
if (gameObjectNode.Attributes["texture"] != null)
{ texture = gameObjectNode.Attributes["texture"].Value; }
if (gameObjectNode.Attributes["bumpTexture"] != null)
{ bumpTexture = gameObjectNode.Attributes["bumpTexture"].Value; }
if (gameObjectNode.Attributes["maxScale"] != null)
{ maxScale = gameObjectNode.Attributes["maxScale"].Value; }
if (gameObjectNode.Attributes["animated"] != null)
{ animated = gameObjectNode.Attributes["animated"].Value; }
}
public string ToXml()
{
string xml =
"<" + type+ " "+
" name=\"" + name + "\"" +
" id=\"" + id + "\"" +
" x=\"" + x + "\"" +
" z=\"" + z + "\"" +
" grid=\"" + grid.ToString() + "\"" +
" typeID=\"" + typeID + "\"\n" +
" model=\"" + model + "\"" +
" texture=\"" + texture + "\"" +
" bumpTexture=\"" + bumpTexture + "\"\n" +
" maxScale=\"" + maxScale + "\"" +
" animated=\"" + animated + "\"" +
"/>";
return xml;
}
}
}