/// <summary> /// Start the next animation once the sequence was over. /// </summary> /// <param name="animationID">ID of the animation.</param> /// <param name="where">Where the pet is "walking"</param> /// <returns>ID of the next animation to play. -1 if there is no animation.</returns> public int SetNextSequenceAnimation(int animationID, TNextAnimation.TOnly where) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.info, "animation is over"); return(SetNextGeneralAnimation(SheepAnimations[animationID].EndAnimation, where)); }
/// <summary> /// Start the next animation once the gravity was detected. /// </summary> /// <param name="animationID">ID of the animation.</param> /// <param name="where">Where the pet is "walking"</param> /// <returns>ID of the next animation to play. -1 if there is no animation.</returns> public int SetNextGravityAnimation(int animationID, TNextAnimation.TOnly where) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.info, "gravity detected"); return(SetNextGeneralAnimation(SheepAnimations[animationID].EndGravity, where)); }
static void Main(string[] args) { int iMutexIndex = 0; string resource1 = "DesktopPet.Portable.NAudio.dll"; EmbeddedAssembly.Load(resource1, "NAudio.dll"); AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); Application.EnableVisualStyles(); // if you like to wait a few seconds in case that the instance is just // shutting down try { if (!mutex.WaitOne(TimeSpan.FromSeconds(1), false)) { iMutexIndex = 1; try { if (!mutex2.WaitOne(TimeSpan.FromSeconds(1), false)) { MessageBox.Show("Application is already running! Only 2 instances are allowed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } catch (Exception) { } } } catch (Exception ex) { MessageBox.Show("Can't execute application: " + ex.Message); return; } // Check and parse the arguments string SearchStringLocalXml = "localxml="; string SearchStringWebXml = "webxml="; string SearchStringInstall = "install="; foreach (string s in args) { if (s.IndexOf(SearchStringLocalXml) >= 0) { ArgumentLocalXML = s.Substring(s.IndexOf(SearchStringLocalXml) + SearchStringLocalXml.Length); if (ArgumentLocalXML.IndexOf(" ") >= 0) { ArgumentLocalXML = ArgumentLocalXML.Substring(0, ArgumentLocalXML.IndexOf(" ")); } } else if (s.IndexOf(SearchStringWebXml) >= 0) { ArgumentWebXML = s.Substring(s.IndexOf(SearchStringWebXml) + SearchStringWebXml.Length); if (ArgumentWebXML.IndexOf(" ") >= 0) { ArgumentWebXML = ArgumentWebXML.Substring(0, ArgumentWebXML.IndexOf(" ")); } } else if (s.IndexOf(SearchStringInstall) >= 0) { ArgumentInstall = s.Substring(s.IndexOf(SearchStringInstall) + SearchStringInstall.Length); if (ArgumentInstall.IndexOf(" ") >= 0) { ArgumentInstall = ArgumentInstall.Substring(0, ArgumentInstall.IndexOf(" ")); } } } Application.SetCompatibleTextRenderingDefault(false); // Show the system tray icon. using (ProcessIcon pi = new ProcessIcon()) { pi.Display(); Mainthread = new StartUp(pi); // Make sure the application runs! Application.Run(); } if (iMutexIndex == 0) { mutex.ReleaseMutex(); } else if (iMutexIndex == 1) { mutex2.ReleaseMutex(); } }
/// <summary> /// Add another spawn to the spawn dictionary. Spawns are defined in the XML. /// <seealso cref="AddAnimation(int, string)"/> /// <seealso cref="AddChild(int)"/> /// </summary> /// <param name="ID">Spawn unique ID.</param> /// <param name="probability">Probability this spawn will be taken.</param> /// <returns></returns> public TSpawn AddSpawn(int ID, int probability) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.info, "adding spawn: " + ID.ToString()); SheepSpawn.Add(ID, new TSpawn(probability)); return(SheepSpawn[ID]); }
/// <summary> /// Read images from XML file and store them in the application. /// </summary> private void readImages() { MemoryStream imageStream = null; try { if (Properties.Settings.Default.Images.Length < 2) { throw new InvalidDataException(); } imageStream = new MemoryStream(Convert.FromBase64String(Properties.Settings.Default.Images)); // only decode once so dont need to keep the source string for image Properties.Settings.Default.Images = string.Empty; StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.info, "user images loaded"); } catch (Exception) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.warning, "user images not found, loading defaults"); try { Properties.Settings.Default.Images = AnimationXML.Image.Png; int mod4 = Properties.Settings.Default.Images.Length % 4; if (mod4 > 0) { Properties.Settings.Default.Images += new string('=', 4 - mod4); } Properties.Settings.Default.Save(); } catch (Exception ex) { MessageBox.Show(ex.Message); } try { imageStream = new MemoryStream(Convert.FromBase64String(Properties.Settings.Default.Images)); // only decode once so dont need to keep the source string for image Properties.Settings.Default.Images = string.Empty; } catch (Exception ex) { MessageBox.Show(ex.Message); } } try { if (Properties.Settings.Default.Icon.Length < 100) { throw new InvalidDataException(); } bitmapIcon = new MemoryStream(Convert.FromBase64String(Properties.Settings.Default.Icon)); StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.info, "user icon loaded"); } catch (Exception) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.warning, "no user icon, loading default"); try { Properties.Settings.Default.Icon = AnimationXML.Header.Icon; int mod4 = Properties.Settings.Default.Icon.Length % 4; if (mod4 > 0) { Properties.Settings.Default.Icon += new string('=', 4 - mod4); } Properties.Settings.Default.Save(); } catch (Exception ex) { MessageBox.Show(ex.Message); } try { bitmapIcon = new MemoryStream(Convert.FromBase64String(Properties.Settings.Default.Icon)); } catch (Exception ex) { MessageBox.Show(ex.Message); } } var image = new Bitmap(imageStream); // no longer need stream imageStream.Close(); spriteWidth = image.Width / AnimationXML.Image.TilesX; spriteHeight = image.Height / AnimationXML.Image.TilesY; sprites = BuildSprites(image); // have sprites no longer need source sheet image.Dispose(); }
/// <summary> /// Add another Child to the Child dictionary. Childs are defined in the XML. /// <seealso cref="AddAnimation(int, string)"/> /// <seealso cref="AddSpawn(int, int)"/> /// </summary> /// <param name="ID">Child unique ID.</param> /// <returns></returns> public TChild AddChild(int ID) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.info, "adding child (ani." + ID.ToString() + ")"); SheepChild.Add(ID, new TChild()); return(SheepChild[ID]); }
/// <summary> /// This function will load the XML. If something can't be loaded as expected, the default XML will be loaded. /// </summary> /// <returns>true, if the XML was loaded successfully.</returns> public bool readXML() { bool bError = false; // Construct an instance of the XmlSerializer with the type // of object that is being deserialized. XmlSerializer mySerializer = new XmlSerializer(typeof(RootNode)); // To read the file, create a FileStream. MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); // Try to load local XML try { if (File.Exists(Application.StartupPath + "\\installpet.xml")) { string sXML = System.Text.Encoding.Default.GetString(File.ReadAllBytes(Application.StartupPath + "\\installpet.xml")); File.Delete(Application.StartupPath + "\\installpet.xml"); writer.Write(sXML); AnimationXMLString = sXML; Properties.Settings.Default.xml = sXML; Properties.Settings.Default.Save(); } else if (Program.ArgumentLocalXML != "") { string sXML = System.Text.Encoding.Default.GetString(File.ReadAllBytes(Program.ArgumentLocalXML)); writer.Write(sXML); AnimationXMLString = sXML; } else if (Program.ArgumentWebXML != "") { System.Net.WebClient client = new System.Net.WebClient(); string sXML = client.DownloadString(Program.ArgumentWebXML); writer.Write(sXML); AnimationXMLString = sXML; } else { writer.Write(Properties.Settings.Default.xml); AnimationXMLString = Properties.Settings.Default.xml; } // Don't load personal pets anymore Program.ArgumentLocalXML = ""; Program.ArgumentWebXML = ""; //writer.Write(Properties.Resources.animations); writer.Flush(); stream.Position = 0; // Call the Deserialize method and cast to the object type. AnimationXML = (RootNode)mySerializer.Deserialize(stream); stream.Close(); Properties.Settings.Default.Images = AnimationXML.Image.Png; Properties.Settings.Default.Icon = AnimationXML.Header.Icon; } catch (Exception ex) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.warning, "User XML error: " + ex.ToString()); if (Properties.Settings.Default.xml.Length > 100) { MessageBox.Show("Error parsing animation XML:" + ex.ToString(), "XML error", MessageBoxButtons.OK, MessageBoxIcon.Error); } writer.Write(Properties.Resources.animations); writer.Flush(); AnimationXMLString = Properties.Resources.animations; stream.Position = 0; // Call the Deserialize method and cast to the object type. AnimationXML = (RootNode)mySerializer.Deserialize(stream); Properties.Settings.Default.Images = AnimationXML.Image.Png; Properties.Settings.Default.Icon = AnimationXML.Header.Icon; } finally { // if the images were loaded from external make some memory available. // don't need it again as its in Properties.Settings.Default.Images AnimationXML.Image.Png = string.Empty; // don't need it again as its in Properties.Settings.Default.Icon AnimationXML.Header.Icon = string.Empty; try { readImages(); if (AnimationXML.Header.Petname.Length > 16) { AnimationXML.Header.Petname = AnimationXML.Header.Petname.Substring(0, 16); } } catch (Exception ex) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.error, "Error reading XML: " + ex.Message); bError = true; } } if (bError) { MessageBox.Show("Error, can't load animations file. The original pet will be loaded", "XML error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(!bError); }
/// <summary> /// Load the animations (read them from XML file) /// </summary> /// <param name="animations">Animation class where the animations should be saved</param> public void loadAnimations(Animations animations) { if (AnimationXML.Animations == null) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.error, "No animations for this pet"); return; } // for each animation foreach (AnimationNode node in AnimationXML.Animations.Animation) { TAnimation ani = animations.AddAnimation(node.Id, node.Id.ToString()); ani.Border = node.Border != null; ani.Gravity = node.Gravity != null; ani.Name = node.Name; switch (ani.Name) { case "fall": animations.AnimationFall = node.Id; break; case "drag": animations.AnimationDrag = node.Id; break; case "kill": animations.AnimationKill = node.Id; break; case "sync": animations.AnimationSync = node.Id; break; } ani.Start.X = getXMLCompute(node.Start.X, "animation " + node.Id + ": node.start.X"); ani.Start.Y = getXMLCompute(node.Start.Y, "animation " + node.Id + ": node.start.Y"); ani.Start.Interval = getXMLCompute(node.Start.Interval, "animation " + node.Id + ": node.start.Interval"); ani.Start.OffsetY = node.Start.OffsetY; ani.Start.Opacity = node.Start.Opacity; ani.End.X = getXMLCompute(node.End.X, "animation " + node.Id + ": node.end.X"); ani.End.Y = getXMLCompute(node.End.Y, "animation " + node.Id + ": node.end.Y"); ani.End.Interval = getXMLCompute(node.End.Interval, "animation " + node.Id + ": node.end.Interval"); ani.End.OffsetY = node.End.OffsetY; ani.End.Opacity = node.End.Opacity; ani.Sequence.RepeatFrom = node.Sequence.RepeatFromFrame; ani.Sequence.Action = node.Sequence.Action; ani.Sequence.Repeat = getXMLCompute(node.Sequence.RepeatCount, "animation " + node.Id + ": node.sequence.Repeat"); ani.Sequence.Frames.AddRange(node.Sequence.Frame); if (ani.Sequence.RepeatFrom > 0) { ani.Sequence.TotalSteps = ani.Sequence.Frames.Count + (ani.Sequence.Frames.Count - ani.Sequence.RepeatFrom - 1) * ani.Sequence.Repeat.Value; } else { ani.Sequence.TotalSteps = ani.Sequence.Frames.Count + ani.Sequence.Frames.Count * ani.Sequence.Repeat.Value; } if (node.Sequence.Next != null) { foreach (NextNode nextNode in node.Sequence.Next) { TNextAnimation.TOnly where; switch (nextNode.OnlyFlag) { case "taskbar": where = TNextAnimation.TOnly.TASKBAR; break; case "window": where = TNextAnimation.TOnly.WINDOW; break; case "horizontal": where = TNextAnimation.TOnly.HORIZONTAL; break; case "horizontal+": where = TNextAnimation.TOnly.HORIZONTAL_; break; case "vertical": where = TNextAnimation.TOnly.VERTICAL; break; default: where = TNextAnimation.TOnly.NONE; break; } ani.EndAnimation.Add( new TNextAnimation( nextNode.Value, nextNode.Probability, where ) ); } } if (ani.Border) { foreach (NextNode nextNode in node.Border.Next) { TNextAnimation.TOnly where; switch (nextNode.OnlyFlag) { case "taskbar": where = TNextAnimation.TOnly.TASKBAR; break; case "window": where = TNextAnimation.TOnly.WINDOW; break; case "horizontal": where = TNextAnimation.TOnly.HORIZONTAL; break; case "horizontal+": where = TNextAnimation.TOnly.HORIZONTAL_; break; case "vertical": where = TNextAnimation.TOnly.VERTICAL; break; default: where = TNextAnimation.TOnly.NONE; break; } ani.Border = true; ani.EndBorder.Add( new TNextAnimation( nextNode.Value, nextNode.Probability, where ) ); } } if (ani.Gravity) { foreach (NextNode nextNode in node.Gravity.Next) { TNextAnimation.TOnly where; switch (nextNode.OnlyFlag) { case "taskbar": where = TNextAnimation.TOnly.TASKBAR; break; case "window": where = TNextAnimation.TOnly.WINDOW; break; case "horizontal": where = TNextAnimation.TOnly.HORIZONTAL; break; case "horizontal+": where = TNextAnimation.TOnly.HORIZONTAL_; break; case "vertical": where = TNextAnimation.TOnly.VERTICAL; break; default: where = TNextAnimation.TOnly.NONE; break; } ani.Gravity = true; ani.EndGravity.Add( new TNextAnimation( nextNode.Value, nextNode.Probability, where ) ); } } animations.SaveAnimation(ani, node.Id); } // for each spawn if (AnimationXML.Spawns.Spawn != null) { foreach (SpawnNode node in AnimationXML.Spawns.Spawn) { TSpawn ani = animations.AddSpawn( node.Id, node.Probability); ani.Start.X = getXMLCompute(node.X, "spawn " + node.Id + ": node.X"); ani.Start.Y = getXMLCompute(node.Y, "spawn " + node.Id + ": node.X"); ani.Next = node.Next.Value; animations.SaveSpawn(ani, node.Id); } } // for each child if (AnimationXML.Childs.Child != null) { foreach (ChildNode node in AnimationXML.Childs.Child) { TChild aniChild = animations.AddChild(node.Id); aniChild.AnimationID = node.Id; aniChild.Position.X = getXMLCompute(node.X, "child " + node.Id + ": node.X"); aniChild.Position.Y = getXMLCompute(node.Y, "child " + node.Id + ": node.Y"); aniChild.Next = node.Next; animations.SaveChild(aniChild, node.Id); } } // for each sound if (AnimationXML.Sounds != null && AnimationXML.Sounds.Sound != null) { foreach (SoundNode node in AnimationXML.Sounds.Sound) { animations.AddSound(node.Id, node.Probability, node.Loop, node.Base64); } } }
/// <summary> /// Picture box fills the form, so mouse events are managed by this object: mouse pressed = pick pet. /// </summary> /// <param name="sender">The caller object.</param> /// <param name="e">Mouse event values.</param> private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && Name.IndexOf("child") < 0) { hwndWindow = (IntPtr)0; // Remove window handles TopMost = false; TopMost = true; // Set again the topmost bDragging = true; // Flag it as dragging pet SetNewAnimation(Animations.AnimationDrag); // Set the dragging animation (if present) } else if (e.Button == MouseButtons.Right && StartUp.IsDebugActive()) { ContextMenu cm = new ContextMenu(); cm.MenuItems.Add("ID." + CurrentAnimation.ID + " - " + CurrentAnimation.Name).Enabled = false; cm.MenuItems.Add("-"); MenuItem menuNext = cm.MenuItems.Add("Next"); MenuItem menuBorder = cm.MenuItems.Add("Border"); MenuItem menuGravity = cm.MenuItems.Add("Gravity"); cm.MenuItems.Add("-"); MenuItem menuSpawn = cm.MenuItems.Add("Spawns"); List <TNextAnimation> list = Animations.GetNextAnimations(CurrentAnimation.ID, true, false, false); foreach (TNextAnimation ani in list) { MenuItem menu = menuNext.MenuItems.Add("ID." + ani.ID + " - " + Animations.SheepAnimations[ani.ID].Name + "\t (Prob: " + ani.Probability + ") only:" + ani.only.ToString()); menu.Click += (ms, me) => { SetNewAnimation(ani.ID); }; } if (list.Count == 0) { menuNext.Enabled = false; } list = Animations.GetNextAnimations(CurrentAnimation.ID, false, true, false); foreach (TNextAnimation ani in list) { MenuItem menu = menuBorder.MenuItems.Add("ID." + ani.ID + " - " + Animations.SheepAnimations[ani.ID].Name + "\t (Prob: " + ani.Probability + ") only: " + ani.only.ToString()); menu.Click += (ms, me) => { SetNewAnimation(ani.ID); }; } if (list.Count == 0) { menuBorder.Enabled = false; } list = Animations.GetNextAnimations(CurrentAnimation.ID, false, false, true); foreach (TNextAnimation ani in list) { MenuItem menu = menuGravity.MenuItems.Add("ID." + ani.ID + " - " + Animations.SheepAnimations[ani.ID].Name + "\t (Prob: " + ani.Probability + ") only:" + ani.only.ToString()); menu.Click += (ms, me) => { SetNewAnimation(ani.ID); }; } if (list.Count == 0) { menuGravity.Enabled = false; } List <TSpawn> listS = Animations.GetNextSpawns(); foreach (TSpawn spa in listS) { MenuItem menu = menuSpawn.MenuItems.Add("ID." + spa.Next + " - " + Animations.SheepAnimations[spa.Next].Name + "\t (Prob: " + spa.Probability + ")"); menu.Click += (ms, me) => { Top = spa.Start.Y.GetValue(); Left = spa.Start.X.GetValue(); dPosX = Left; dPosY = Top; dOffsetY = 0.0; SetNewAnimation(spa.Next); }; } timer1.Enabled = false; cm.Collapse += (ms, me) => { timer1.Interval = 1; timer1.Enabled = true; }; pictureBox1.ContextMenu = cm; pictureBox1.ContextMenu.Show(pictureBox1, new Point(0, this.Top > 500 ? 0 : this.Height)); } }
/// <summary> /// The most important function. Each movement step is managed by this function:<br /> /// Will calculate how much and where a pet should be positioned in the next step.<br /> /// This function is called from <see cref="timer1_Tick(object, EventArgs)"/>. /// </summary> private void NextStep() { // If there is no repeat, we don't need to calculate the frame index. if (iAnimationStep < CurrentAnimation.Sequence.Frames.Count) { pictureBox1.Image = imageList1.Images[CurrentAnimation.Sequence.Frames[iAnimationStep]]; } else { int index = ((iAnimationStep - CurrentAnimation.Sequence.Frames.Count + CurrentAnimation.Sequence.RepeatFrom) % (CurrentAnimation.Sequence.Frames.Count - CurrentAnimation.Sequence.RepeatFrom)) + CurrentAnimation.Sequence.RepeatFrom; pictureBox1.Image = imageList1.Images[CurrentAnimation.Sequence.Frames[index]]; } // Get interval, opacity and offset interpolated from START and END values. timer1.Interval = CurrentAnimation.Start.Interval.Value + ((CurrentAnimation.End.Interval.Value - CurrentAnimation.Start.Interval.Value) * iAnimationStep / CurrentAnimation.Sequence.TotalSteps); Opacity = CurrentAnimation.Start.Opacity + (CurrentAnimation.End.Opacity - CurrentAnimation.Start.Opacity) * iAnimationStep / CurrentAnimation.Sequence.TotalSteps; dOffsetY = CurrentAnimation.Start.OffsetY + (double)(CurrentAnimation.End.OffsetY - CurrentAnimation.Start.OffsetY * iAnimationStep / CurrentAnimation.Sequence.TotalSteps); // If dragging is enabled, move the pet to the mouse position. if (bDragging) { dPosX = Left = Cursor.Position.X - Width / 2; dPosY = Top = Cursor.Position.Y - 2; return; } double x = CurrentAnimation.Start.X.Value; double y = CurrentAnimation.Start.Y.Value; // if TotalSteps is more than 1, we have to interpolate START and END values) if (CurrentAnimation.Sequence.TotalSteps > 1) { x += ((CurrentAnimation.End.X.Value - CurrentAnimation.Start.X.Value) * (double)iAnimationStep / (CurrentAnimation.Sequence.TotalSteps - 1.0)); y += ((CurrentAnimation.End.Y.Value - CurrentAnimation.Start.Y.Value) * (double)iAnimationStep / (CurrentAnimation.Sequence.TotalSteps - 1.0)); } // If a new animation need to be started bool bNewAnimation = false; // If the pet is "flipped", mirror the movement if (!bMoveLeft) { x = -x; } if (x < 0) // moving left (detect left borders) { if (hwndWindow == (IntPtr)0) { if (dPosX + x < 0) // left screen border! { int iBorderAnimation = Animations.SetNextBorderAnimation(CurrentAnimation.ID, TNextAnimation.TOnly.VERTICAL); if (iBorderAnimation >= 0) { x = 0; SetNewAnimation(iBorderAnimation); bNewAnimation = true; } } } else { NativeMethods.RECT rct; if (NativeMethods.GetWindowRect(new HandleRef(this, hwndWindow), out rct)) { if (dPosX + x < rct.Left) // left window border! { int iBorderAnimation = Animations.SetNextBorderAnimation(CurrentAnimation.ID, TNextAnimation.TOnly.WINDOW); if (iBorderAnimation >= 0) { x = rct.Left; SetNewAnimation(iBorderAnimation); bNewAnimation = true; } else { // not anymore on the window hwndWindow = (IntPtr)0; } } } } } else if (x > 0) // moving right (detect right borders) { if (hwndWindow == (IntPtr)0) { if (dPosX + x + Width > Screen.PrimaryScreen.WorkingArea.Width) // right screen border! { int iBorderAnimation = Animations.SetNextBorderAnimation(CurrentAnimation.ID, TNextAnimation.TOnly.VERTICAL); if (iBorderAnimation >= 0) { x = Screen.PrimaryScreen.WorkingArea.Width - Width; SetNewAnimation(iBorderAnimation); bNewAnimation = true; } } } else { NativeMethods.RECT rct; if (NativeMethods.GetWindowRect(new HandleRef(this, hwndWindow), out rct)) { if (dPosX + x + Width > rct.Right) // right window border! { int iBorderAnimation = Animations.SetNextBorderAnimation(CurrentAnimation.ID, TNextAnimation.TOnly.WINDOW); if (iBorderAnimation >= 0) { x = rct.Right - Width; SetNewAnimation(iBorderAnimation); bNewAnimation = true; } else { // not anymore on the window hwndWindow = (IntPtr)0; } } } } } if (y > 0) // moving down (detect taskbar and windows) { if (CurrentAnimation.EndBorder.Count > 0) { int bottomY = Screen.PrimaryScreen.WorkingArea.Height + Screen.PrimaryScreen.WorkingArea.Y; if (dPosY + y > bottomY - Height) // border detected! { y = bottomY - (int)(dPosY + Height); dOffsetY = 0; SetNewAnimation(Animations.SetNextBorderAnimation(CurrentAnimation.ID, TNextAnimation.TOnly.TASKBAR)); bNewAnimation = true; } else { int iWindowTop = FallDetect((int)y); if (iWindowTop > 0) { y = iWindowTop - dPosY - Height; dOffsetY = 0; SetNewAnimation(Animations.SetNextBorderAnimation(CurrentAnimation.ID, TNextAnimation.TOnly.WINDOW)); bNewAnimation = true; if (CurrentAnimation.Start.Y.Value != 0) { hwndWindow = (IntPtr)0; } } } } } else if (y < 0) // moving up, detect upper screen border { if (CurrentAnimation.EndBorder.Count > 0) { if (dPosY < Screen.PrimaryScreen.WorkingArea.Y) // border detected! { y = 0; SetNewAnimation(Animations.SetNextBorderAnimation(CurrentAnimation.ID, TNextAnimation.TOnly.HORIZONTAL)); bNewAnimation = true; } } } if (iAnimationStep >= CurrentAnimation.Sequence.TotalSteps) // animation over { int iNextAni = -1; if (CurrentAnimation.Sequence.Action == "flip") { // flip all images bMoveLeft = !bMoveLeft; for (int i = 0; i < imageList1.Images.Count; i++) { Image im = imageList1.Images[i]; im.RotateFlip(RotateFlipType.RotateNoneFlipX); imageList1.Images[i] = im; } } if (hwndWindow != (IntPtr)0) { iNextAni = Animations.SetNextSequenceAnimation(CurrentAnimation.ID, TNextAnimation.TOnly.WINDOW); } else { // If pet is outside the borders, spawn it again. if (Left < -Width || Left > Screen.PrimaryScreen.Bounds.Width) { iNextAni = -1; } else { iNextAni = Animations.SetNextSequenceAnimation(CurrentAnimation.ID, dPosY + Height + y >= Screen.PrimaryScreen.WorkingArea.Height - 2 ? TNextAnimation.TOnly.TASKBAR : TNextAnimation.TOnly.NONE); } } if (CurrentAnimation.ID == Animations.AnimationKill) { double op; if (timer1.Tag == null || !double.TryParse(timer1.Tag.ToString(), out op)) { timer1.Tag = 1.0; } op = double.Parse(timer1.Tag.ToString()); timer1.Tag = op - 0.1; Opacity = op; if (op <= 0.1) { Close(); } } else if (iNextAni >= 0) { SetNewAnimation(iNextAni); bNewAnimation = true; } else { // Child doesn't have a spawn, they will be closed once the animation is over. if (Name.IndexOf("child") == 0) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.info, "removing child"); Close(); } else { Play(false); } } } // If there is a Gravity-Next animation, check if gravity is present. else if (CurrentAnimation.Gravity) { if (hwndWindow == (IntPtr)0) { if (dPosY + y < Screen.PrimaryScreen.WorkingArea.Height - Height) { if (dPosY + y + 3 >= Screen.PrimaryScreen.WorkingArea.Height - Height) // allow 3 pixels to move without fall { y = Screen.PrimaryScreen.WorkingArea.Height - (int)dPosY - Height; } else { SetNewAnimation(Animations.SetNextGravityAnimation(CurrentAnimation.ID, TNextAnimation.TOnly.NONE)); bNewAnimation = true; } } } else { if (iAnimationStep > 0 && CheckTopWindow(true)) { hwndWindow = (IntPtr)0; SetNewAnimation(Animations.SetNextGravityAnimation(CurrentAnimation.ID, TNextAnimation.TOnly.WINDOW)); bNewAnimation = true; } } } // If a new animation was started, set the interval and the first animation frame image. if (bNewAnimation) { timer1.Interval = 1; // execute immediately the first step of the next animation. x = 0; // don't move the pet, if a new animation must be started //y = 0; // if falling, set the pet to the new position pictureBox1.Image = imageList1.Images[CurrentAnimation.Sequence.Frames[0]]; } // Set the new pet position (and offset) in the screen. dPosX += x; dPosY += y; Left = (int)dPosX; Top = (int)(dPosY + dOffsetY); }
/// <summary> /// This function will load the XML. If something can't be loaded as expected, the default XML will be loaded. /// </summary> /// <returns>true, if the XML was loaded successfully.</returns> public bool readXML() { bool bError = false; // Construct an instance of the XmlSerializer with the type // of object that is being deserialized. XmlSerializer mySerializer = new XmlSerializer(typeof(XmlData.RootNode)); // To read the file, create a FileStream. MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); // Try to load local XML try { writer.Write(Program.MyData.GetXml()); AnimationXMLString = Program.MyData.GetXml(); //writer.Write(Properties.Resources.animations); writer.Flush(); stream.Position = 0; // Call the Deserialize method and cast to the object type. AnimationXML = (XmlData.RootNode)mySerializer.Deserialize(stream); stream.Close(); Program.MyData.SetImages(AnimationXML.Image.Png); Program.MyData.SetIcon(AnimationXML.Header.Icon); } catch (Exception ex) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.warning, "User XML error: " + ex.ToString()); if (Program.MyData.GetXml().Length > 100) { MessageBox.Show("Error parsing animation XML:" + ex.ToString(), "XML error", MessageBoxButtons.OK, MessageBoxIcon.Error); } stream.Flush(); stream.Position = 0; writer.Write(Properties.Resources.animations); writer.Flush(); AnimationXMLString = Properties.Resources.animations; stream.Position = 0; // Call the Deserialize method and cast to the object type. AnimationXML = (XmlData.RootNode)mySerializer.Deserialize(stream); Program.MyData.SetXml(Properties.Resources.animations, "esheep64"); Program.MyData.SetImages(AnimationXML.Image.Png); Program.MyData.SetIcon(AnimationXML.Header.Icon); } finally { // if the images were loaded from external make some memory available. // don't need it again as its in Properties.Settings.Default.Images AnimationXML.Image.Png = string.Empty; // don't need it again as its in Properties.Settings.Default.Icon AnimationXML.Header.Icon = string.Empty; try { readImages(); if (AnimationXML.Header.Petname.Length > 16) { AnimationXML.Header.Petname = AnimationXML.Header.Petname.Substring(0, 16); } } catch (Exception ex) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.error, "Error reading XML: " + ex.Message); bError = true; } } if (bError) { MessageBox.Show("Error, can't load animations file. The original pet will be loaded", "XML error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(!bError); }
/// <summary> /// Read images from XML file and store them in the application. /// </summary> private void readImages() { try { if (Properties.Settings.Default.Images.Length < 2) { throw new InvalidDataException(); } images.bitmapImages = new MemoryStream(Convert.FromBase64String(Properties.Settings.Default.Images)); StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.info, "user images loaded"); } catch (Exception) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.warning, "user images not found, loading defaults"); try { Properties.Settings.Default.Images = AnimationXML.Image.Png; int mod4 = Properties.Settings.Default.Images.Length % 4; if (mod4 > 0) { Properties.Settings.Default.Images += new string('=', 4 - mod4); } Properties.Settings.Default.Save(); } catch (Exception ex) { MessageBox.Show(ex.Message); } try { images.bitmapImages = new MemoryStream(Convert.FromBase64String(Properties.Settings.Default.Images)); } catch (Exception ex) { MessageBox.Show(ex.Message); } } try { if (Properties.Settings.Default.Icon.Length < 100) { throw new InvalidDataException(); } bitmapIcon = new MemoryStream(Convert.FromBase64String(Properties.Settings.Default.Icon)); StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.info, "user icon loaded"); } catch (Exception) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.warning, "no user icon, loading default"); try { Properties.Settings.Default.Icon = AnimationXML.Header.Icon; int mod4 = Properties.Settings.Default.Icon.Length % 4; if (mod4 > 0) { Properties.Settings.Default.Icon += new string('=', 4 - mod4); } Properties.Settings.Default.Save(); } catch (Exception ex) { MessageBox.Show(ex.Message); } try { bitmapIcon = new MemoryStream(Convert.FromBase64String(Properties.Settings.Default.Icon)); } catch (Exception ex) { MessageBox.Show(ex.Message); } } FullImage = new Bitmap(images.bitmapImages); }
/// <summary> /// This function will load the XML. If something can't be loaded as expected, the default XML will be loaded. /// </summary> /// <returns>true, if the XML was loaded successfully.</returns> public bool readXML() { bool bError = false; // Construct an instance of the XmlSerializer with the type // of object that is being deserialized. XmlSerializer mySerializer = new XmlSerializer(typeof(RootNode)); // To read the file, create a FileStream. MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); // Try to load local XML try { writer.Write(Properties.Settings.Default.xml); //writer.Write(Properties.Resources.animations); writer.Flush(); stream.Position = 0; // Call the Deserialize method and cast to the object type. AnimationXML = (RootNode)mySerializer.Deserialize(stream); Properties.Settings.Default.Images = AnimationXML.Image.Png; Properties.Settings.Default.Icon = AnimationXML.Header.Icon; } catch (Exception ex) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.warning, "User XML error: " + ex.ToString()); if (Properties.Settings.Default.xml.Length > 100) { MessageBox.Show("Error parsing animation XML:" + ex.ToString(), "XML error", MessageBoxButtons.OK, MessageBoxIcon.Error); } writer.Write(Properties.Resources.animations); writer.Flush(); stream.Position = 0; // Call the Deserialize method and cast to the object type. AnimationXML = (RootNode)mySerializer.Deserialize(stream); Properties.Settings.Default.Images = AnimationXML.Image.Png; Properties.Settings.Default.Icon = AnimationXML.Header.Icon; } finally { try { readImages(); if (AnimationXML.Header.Petname.Length > 16) { AnimationXML.Header.Petname = AnimationXML.Header.Petname.Substring(0, 16); } } catch (Exception ex) { StartUp.AddDebugInfo(StartUp.DEBUG_TYPE.error, "Error reading XML: " + ex.Message); bError = true; } } if (bError) { MessageBox.Show("Error, can't load animations file. The original pet will be loaded", "XML error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(!bError); }