//--------------------------------------------------------------------------------------------------------------------------------------------------- // // Copyright (C)2007 DarkWynter Studios. All rights reserved. // //--------------------------------------------------------------------------------------------------------------------------------------------------- // {Contact : darkwynter.com for licensing information //--------------------------------------------------------------------------------------------------------------------------------------------------- #define DEBUG_MODE namespace DarkWynter.App { 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 System.Xml.Schema; using System.Net; public partial class LevelEditorControl : UserControl { OpenFileDialog fileDialog; private static string contentFileName = "Content.contentproj"; private static string zipFileName = "G2L.zip"; private static string zipPassWord = "darkwynter"; private static string server_Zip = "http://darkwynter.com/_Episodes/G2L/"; private static string server_Files = "http://www.darkwynter.com/Beta2/Trunk/"; private static string userName = "darkwynter"; private static string password = "wynter1"; private static string localPath = ""; // Calced in CTOR private static ProgressBar progress; private static Label progressLabel; private static ListBox localEpisodes; enum NodeListType { GameObjects, Events, List }; NodeListType nodeListType; List listViewObjects = new List(); List traceObjects = new List(); public LevelEditorControl() { InitializeComponent(); this.Dock = DockStyle.Fill; // Kill the PropertyGrid's bottom Help bar this.propertyGrid_Properties.HelpVisible = false; // Prep file dialog fileDialog = new OpenFileDialog(); fileDialog.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop); fileDialog.RestoreDirectory = true; // Drag Drop GameObjects to Map this.listView_ObjectIndex.MouseDown += new System.Windows.Forms.MouseEventHandler(DragDrop_MouseDown); // Notify us of Game project changes Engine.DarkWynterEngine.onGameChange += new DarkWynter.Engine.DarkWynterEngine.OnGameChange(LevelIndex_Load); // Notify us of level changes Engine.DarkWynterEngine.onLevelChange += new DarkWynter.Engine.DarkWynterEngine.OnLevelChange(this.Load_OnLevelChange); // Notify us of update changes Engine.DarkWynterEngine.onUpdateComplete += new DarkWynter.Engine.DarkWynterEngine.OnUpdateComplete(this.PropertyViewer_OnUpdateComplete); // Notify us of list changes Stream.UIInterfacing.ListPropertyEditor.listUpdates += new Stream.UIInterfacing.ListPropertyEditor.ListUpdates(ViewList); // Notify us of property grid changes Stream.UIInterfacing.ObjectPropertyEditor.propertyGridUpdates += new Stream.UIInterfacing.ObjectPropertyEditor.PropertyGridUpdates(ViewChildObjectProperties); // Calc local Content folder location localPath = System.IO.Directory.GetCurrentDirectory() + "\\"; progress = this.progressBarZipDownload; progressLabel = this.labelProgress; localEpisodes = this.localEposodes_listBox; //LevelIndex_Load(); } #region Download Episode private void DownloadContentToolStripMenuItem_Click(object sender, EventArgs e) { progress.Minimum = 0; progress.Maximum = 100; progress.BackColor = Color.LightBlue; progress.Invalidate(); progressLabel.Text = "Contacting Server.."; progressLabel.Invalidate(); DownLoadFileInBackground( server_Zip + zipFileName, localPath + zipFileName ); } public static void DownLoadFileInBackground(string serverAddress, string localAddress) { WebClient client = new WebClient(); Uri uri = new Uri(serverAddress); // Specify that the DownloadFileCallback method gets called // when the download completes. client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCallback); // Specify a progress notification handler. client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback); try { client.DownloadFileAsync(uri, localAddress); } catch { progressLabel.Text = "Error Connecting to Server. Please Try Again..."; } } private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e) { if (e.ProgressPercentage < 99) { progress.Value = e.ProgressPercentage; // Displays the operation identifier, and the transfer progress. progressLabel.Text = (string)e.UserState + " Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive + " bytes. " + e.ProgressPercentage + " % complete... "; } else { progress.Value = 85; progressLabel.Text = "Unzipping Files"; } } private static void DownloadFileCallback(object sender, AsyncCompletedEventArgs e) { progress.BackColor = Color.LightGreen; // Displays the operation identifier, and the transfer progress. if (e.Error != null) { throw new Exception("Error Downloading File"); } progressLabel.Text = "Download Complete"; progress.Value = 85; progressLabel.Text = "Unzipping Files"; // Unzip the content DarkWynter.Engine.Utilities.ZipUtility.UnZipFiles( localPath + zipFileName, localPath, zipPassWord, true ); progressLabel.Text = "Installation Complete... Please Restart This Application."; progress.Value = 100; } #endregion #region Level Selection private static void LevelIndex_Load() { localEpisodes.Items.Clear(); if (DarkWynter.Engine.Globals.Statics_Engine.levelInfo != null) { // Get Xml Level Info Nodes for (int i = 0; i < DarkWynter.Engine.Globals.Statics_Engine.levelInfo.Count; i++) { localEpisodes.Items.Add(DarkWynter.Engine.Globals.Statics_Engine.levelInfo[i].name); } } } private void Level_SelectedIndexChanged(object sender, EventArgs e) { DarkWynter.Engine.Globals.Statics_Engine.levelIndex = localEposodes_listBox.SelectedIndex; DarkWynter.Engine.Globals.Statics_Engine.GameSettings.LoadLevel = true; if (DarkWynter.Engine.Globals.Statics_Engine.levelIndex == 2) { DarkWynter.Engine.Globals.Statics_Engine.SystemSettings.gameState = DarkWynter.Engine.Globals.Enums_Engine.EngineState.FINALIZE_G2LSTUFF; } if (DarkWynter.Engine.Globals.Statics_Engine.levelIndex == 1 || DarkWynter.Engine.Globals.Statics_Engine.levelIndex == 0) { DarkWynter.Engine.Globals.Statics_Engine.SystemSettings.gameState = DarkWynter.Engine.Globals.Enums_Engine.EngineState.INSTRUCTION_SCREEN; } } public void Load_OnLevelChange(EventArgs e) { // Updating index to reflect changes in the engine. ObjLib_toolStripMenuItem_Click(new object(), new EventArgs()); } #endregion #region Old Loading Code - ListView // /// // /// Load Level Index from file; Contains a link to all Levels in the Game. // /// // /// // /// // private void Load_Project(object sender, EventArgs e) // { // string levelXmlFileName = ""; // fileDialog.Title = "Select Level Xml File"; // fileDialog.Filter = "Level Description File (*.xml)|*.xml|All files (*.*)|*.*"; // fileDialog.FilterIndex = 1; // if (fileDialog.ShowDialog() == DialogResult.OK) // { // levelXmlFileName = fileDialog.FileName; // } // else // { //#if DEBUG_MODE // levelXmlFileName = Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory).ToString();; // levelXmlFileName += "\\Darkwynter\\POCs\\G2L\\__Content\\_xml\\LevelEdit\\_Levels.xml"; //#else // return; //#endif // } // // Load Level Index Data // try // { // // Open selected Level Index xml-file // XmlDocument reader = new XmlDocument(); // reader.Load(levelXmlFileName); // // Clear levels; then add new levels to the data-structure // levelNodes.Clear(); // foreach (XmlNode xmlLevelNode in reader.ChildNodes) // { // // Check for a Level-Set definition // if (xmlLevelNode.Name == "levels") // { // for (int count = 0; count < xmlLevelNode.ChildNodes.Count; count++) // { // XmlNode node = xmlLevelNode.ChildNodes[count]; // // Check for a Level-Node definition // if (node.Name == "level") // { // // Create Level and add to list // levelNodes.Add(new LevelNode(node)); // } // } // } // } // } // catch // { // System.Diagnostics.Debug.WriteLine("Error Loading Level-Index Xml file: " + levelXmlFileName); // // throw new Exception("Error reading XML"); // } // } //private void LevelIndex_SelectIndexChanged(object sender, EventArgs e) //{ // ListView LevelIndex = sender as ListView; // // Get Selected Level // for (int l_Index = 0; l_Index < LevelIndex.Items.Count; l_Index++) // { // if (LevelIndex.Items[l_Index].Selected == true) // { // // Load the selected Level's ObjectNodes into ListView // this.listView_ObjectIndex.Items.Clear(); // for (int objectIndex = 0; objectIndex < levelNodes[l_Index].objectNodes.Count; objectIndex++) // { // // Create a ListViewItem containing the Level Summary Info // // Add Item to ObjectIndex ListView // this.listView_ObjectIndex.Items.Add( // new ListViewItem(new string[] { // levelNodes[l_Index].objectNodes[objectIndex].propertyNode.name, // levelNodes[l_Index].objectNodes[objectIndex].propertyNode.type // })); // } // return; // } // } //} #endregion #region ListView - MenuStrip Items private void EngineToolStripMenuItem_Click(object sender, EventArgs e) { this.propertyGrid_Properties.SelectedObject = DarkWynter.Engine.DarkWynterEngine.engine; } private void HUD_toolStripMenuItem_Click(object sender, EventArgs e) { } private void ObjLib_toolStripMenuItem_Click(object sender, EventArgs e) { nodeListType = NodeListType.GameObjects; this.listView_ObjectIndex.Items.Clear(); // Get Names and add to list List names = DarkWynter.Engine.DarkWynterEngine.engine.objectLibrary.objectNames; for (int i = 0; i < names.Count; i++) { // Add Item to ListView this.listView_ObjectIndex.Items.Add(new ListViewItem(names[i])); } } private void Events_toolStripMenuItem_Click(object sender, EventArgs e) { nodeListType = NodeListType.Events; this.listView_ObjectIndex.Items.Clear(); // Current Game Conditions if (DarkWynter.Engine.EventControl.GameEventHandler.CurrentGameConditions != null) { this.listView_ObjectIndex.Items.Add( new ListViewItem(new string[] { DarkWynter.Engine.EventControl.GameEventHandler.CurrentGameConditions.ToString(), })); } if (DarkWynter.Engine.EventControl.GameEventHandler.GameEvents != null) { for (int eventIndex = 0; eventIndex < DarkWynter.Engine.EventControl.GameEventHandler.GameEvents.Count; eventIndex++) { // Add Item to ListView this.listView_ObjectIndex.Items.Add( new ListViewItem(new string[] { DarkWynter.Engine.EventControl.GameEventHandler.GameEvents[eventIndex]._trigger_ID.ToString(), DarkWynter.Engine.EventControl.GameEventHandler.GameEvents[eventIndex]._typeID.ToString() })); } } } private void Code_toolStripMenuItem_Click(object sender, EventArgs e) { } private void Color_toolStripMenuItem_Click(object sender, EventArgs e) { propertyGrid_Properties.SelectedObject = DarkWynter.Shell.UCAD_Control.settings; } private void Camera_toolStripMenuItem_Click(object sender, EventArgs e) { propertyGrid_Properties.SelectedObject = DarkWynter.Stream.Statics_Stream.RenderSettings.cameraList[0]; } #endregion #region ListView private void ListView_Click(object sender, EventArgs e) { //ListView.SelectedIndexCollection selectedIndices = this.listView_ObjectIndex.SelectedIndices; //for (int o_Index = 0; o_Index < selectedIndices.Count; o_Index++) //{ // // Display the Object's Properties // int index = selectedIndices[o_Index]; // propertyGrid_Properties.SelectedObject = listViewObjects[index]; //} if (nodeListType == NodeListType.GameObjects) { for (int o_Index = 0; o_Index < listView_ObjectIndex.Items.Count; o_Index++) { if (listView_ObjectIndex.Items[o_Index].Selected) { // Display the Object's Properties propertyGrid_Properties.SelectedObject = DarkWynter.Engine.DarkWynterEngine.engine.objectLibrary.GetGameObject( listView_ObjectIndex.Items[o_Index].Text ); return; } } } if (nodeListType == NodeListType.Events) { for (int o_Index = 0; o_Index < listView_ObjectIndex.Items.Count; o_Index++) { if (listView_ObjectIndex.Items[o_Index].Selected) { if (o_Index == 0) { propertyGrid_Properties.SelectedObject = DarkWynter.Engine.EventControl.GameEventHandler.CurrentGameConditions; } else { // Display the Object's Properties propertyGrid_Properties.SelectedObject = DarkWynter.Engine.EventControl.GameEventHandler.GameEvents[o_Index - 1]; //DarkWynter.Engine.EventControl.GameEventHandler.GetEvent(int.Parse(listView_ObjectIndex.Items[o_Index].Text)); } return; } } } } #endregion #region Properties Grid public bool refreshGrid = true; public System.Diagnostics.Stopwatch propertyViewerTimer = new System.Diagnostics.Stopwatch(); public void ViewList(object[] list) { object objectList = this.propertyGrid_Properties.SelectedGridItem.Value; // Load the selected Level's ObjectNodes into ListView this.listView_ObjectIndex.Items.Clear(); Type type = objectList.GetType(); //type. //List objList = (List)objectList; for (int i = 0; i < list.Length; i++) { // Add Object to Active list this.listViewObjects.Add(list[i]); // Add Object to List this.listView_ObjectIndex.Items.Add(i.ToString()); } } public void ViewChildObjectProperties(object childObject) { // BreadCrumb MenuItem ToolStripMenuItem BreadCrumbMenuItem = new ToolStripMenuItem(); BreadCrumbMenuItem.Name = "BreadCrumbMenuItem"; BreadCrumbMenuItem.Size = new System.Drawing.Size(78, 20); BreadCrumbMenuItem.Click += new System.EventHandler(this.ViewParentObjectProperties); // Parse Button text from Object Type string buttonText = this.propertyGrid_Properties.SelectedObject.GetType().ToString(); int shortName = buttonText.LastIndexOf("."); BreadCrumbMenuItem.Text = buttonText.Substring(shortName + 1); // Add Button to Breadcrumb MenuStrip this.menuStrip_Properties.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { BreadCrumbMenuItem }); // Store selected object in breadcrumb list traceObjects.Add(this.propertyGrid_Properties.SelectedObject); // Load new object into property grid this.propertyGrid_Properties.SelectedObject = childObject; } private void ViewParentObjectProperties(object sender, EventArgs e) { this.propertyGrid_Properties.SelectedObject = this.traceObjects[0]; this.traceObjects.Remove(this.traceObjects[0]); this.menuStrip_Properties.Items.RemoveAt(0); } private void PropertyViewer_OnUpdateComplete(object sender, EventArgs e) { if (!propertyViewerTimer.IsRunning) propertyViewerTimer.Start(); if (Engine.Globals.Statics_Engine.SystemSettings.WINDOW_IN_FOCUS) { if (propertyViewerTimer.ElapsedMilliseconds > 100) { propertyViewerTimer.Reset(); propertyViewerTimer.Start(); this.propertyGrid_Properties.Refresh(); } } } private void PropertyViewer_SelectedItemChanged(object sender, EventArgs e) { // Callback for Property Grid } private void PropertyViewer_MouseEnter(object sender, EventArgs e) { this.refreshGrid = false; } private void PropertyViewer_MouseLeave(object sender, EventArgs e) { this.refreshGrid = true; } #endregion #region Drag/Drop 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(); } #endregion #region Save Library private void saveLevelToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "Xml Level File (*.xml)|*.xml|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; saveFileDialog1.InitialDirectory = System.IO.Directory.GetCurrentDirectory() + "\\Content"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { XmlDocument levelXml = DarkWynter.Engine.DarkWynterEngine.engine.objectLibrary.ToXml(); try { levelXml.Save(saveFileDialog1.FileName); //I've chosen the c:\ for the resulting file pavel.xml } catch (Exception exception) { Console.WriteLine(exception.Message); } } //// CHECK AGAINST SCHEMA //XmlReaderSettings booksSettings = new XmlReaderSettings(); //booksSettings.Schemas.Add(null, "ValidationSchema_Level.xsd"); //booksSettings.ValidationType = ValidationType.Schema; //booksSettings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); //XmlReader books = XmlReader.Create("Medulla.xml", booksSettings); //XmlReader books = XmlReader.Create(LevelXml, booksSettings); //while (books.Read()) { } } private static void ValidationCallBack(object sender, ValidationEventArgs e) { Console.WriteLine("Validation Error: {0}", e.Message); } #endregion private void loadGameToolStripMenuItem_Click(object sender, EventArgs e) { } } }