Ejemplo n.º 1
0
        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            clickedPoint = new Point(e.Location.X - Width / 2, e.Location.Y - Height / 2);

            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip.Show(e.X, e.Y);
            }
            else if (e.Button == MouseButtons.Left)
            {
                if (selectedItem != null)
                {
                    float xOffset = 1.0f * (clickedPoint.X - selectedItem.X);
                    float yOffset = 1.0f * (clickedPoint.Y - selectedItem.Y);

                    // Scaling
                    if (Math.Abs(xOffset - ((float)Math.Cos(selectedItem.RotationRadians) * selectedItem.Size.Width / 2 - ImageItem.HandleSize)) < 1.5 * ImageItem.HandleSize &&
                        Math.Abs(yOffset - (float)Math.Sin(selectedItem.RotationRadians) * selectedItem.Size.Width / 2) < 1.5 * ImageItem.HandleSize)
                    {
                        scaling = true;
                        return;
                    }

                    // Rotating
                    if (Math.Abs(xOffset - (float)Math.Cos(selectedItem.RotationRadians) * selectedItem.Size.Width / 4) < 1.5 * ImageItem.HandleSize &&
                        Math.Abs(yOffset - (float)Math.Sin(selectedItem.RotationRadians) * selectedItem.Size.Width / 4) < 1.5 * ImageItem.HandleSize)
                    {
                        rotating = true;
                        return;
                    }

                    // Moving
                    if (Math.Abs(xOffset) < 1.5 * ImageItem.HandleSize && Math.Abs(yOffset) < 1.5 * ImageItem.HandleSize)
                    {
                        moving = true;
                        return;
                    }


                    // Deleting
                    if (Math.Abs(xOffset - ((float)Math.Cos(selectedItem.RotationRadians) * selectedItem.Size.Width / 2 + (float)Math.Sin(selectedItem.RotationRadians) * selectedItem.Size.Height / 2 - ImageItem.HandleSize)) < 1.5 * ImageItem.HandleSize &&
                        Math.Abs(yOffset - ((float)Math.Sin(selectedItem.RotationRadians) * selectedItem.Size.Width / 2 - (float)Math.Cos(selectedItem.RotationRadians) * selectedItem.Size.Height / 2 + ImageItem.HandleSize)) < 1.5 * ImageItem.HandleSize)
                    {
                        if (MessageBox.Show("Delete this image?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            VisionBoard.Current.Items.Remove(selectedItem);
                            selectedItem = null;
                            Invalidate();
                        }
                        return;
                    }
                }

                // Selecting
                selectedItem = null;
                int selectedIndex = -1;
                for (int i = 0; i < VisionBoard.Current.Items.Count; i++) // foreach (ImageItem item in VisionBoard.Items)
                {
                    ImageItem item = (ImageItem)VisionBoard.Current.Items[i];

                    if (VisionBoard.Current.Reordering && VisionBoard.Current.ReorderCurrentIndex - 1 > i)
                    {
                        continue;
                    }

                    if (item.HitTest(clickedPoint.X, clickedPoint.Y))
                    {
                        if (selectedItem == null ||                                                                                // found one
                            item.Distance(clickedPoint.X, clickedPoint.Y) < selectedItem.Distance(clickedPoint.X, clickedPoint.Y)) // found another, so select closest to center
                        {
                            selectedItem  = item;
                            selectedIndex = i;
                        }
                    }
                }

                if (VisionBoard.Current.Reordering && selectedIndex >= 0)
                {
                    // change order
                    if (selectedIndex != VisionBoard.Current.ReorderCurrentIndex - 1)
                    {
                        VisionBoard.Current.Items.RemoveAt(selectedIndex);
                        VisionBoard.Current.Items.Insert(VisionBoard.Current.ReorderCurrentIndex - 1, selectedItem);
                        VisionBoard.Current.IsDirty = true;
                    }

                    // increment ReorderCurrentIndex
                    VisionBoard.Current.ReorderCurrentIndex++;
                    if (VisionBoard.Current.ReorderCurrentIndex > VisionBoard.Current.Items.Count)
                    {
                        VisionBoard.Current.Reordering = false;
                    }

                    selectedItem = null;
                }

                Invalidate();
            }
        }
Ejemplo n.º 2
0
        public static VisionBoard Read(string filename)
        {
            VisionBoard visionBoard = new VisionBoard();

            if (filename.Length == 0)
            {
                return(visionBoard);
            }

            if (!File.Exists(filename))
            {
                MessageBox.Show(filename, "File does not exist");
                return(visionBoard);
            }

            visionBoard.Filename = filename;

            try
            {
                using (XmlTextReader reader = new XmlTextReader(filename))
                {
                    FileInfo fi             = new FileInfo(filename);
                    string   imageDirectory = fi.DirectoryName + "\\" + fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length);

                    while (reader.Read())
                    {
                        reader.MoveToContent();

                        if (reader.NodeType == System.Xml.XmlNodeType.Element)
                        {
                            if (reader.Name == "Image")
                            {
                                ImageItem item = new ImageItem();
                                item.X = int.Parse(reader.GetAttribute("x"));
                                item.Y = int.Parse(reader.GetAttribute("y"));
                                item.RotationDegrees = float.Parse(reader.GetAttribute("rotation"));
                                item.Scale           = float.Parse(reader.GetAttribute("scale"));
                                item.Caption         = reader.GetAttribute("caption");

                                item.Filename = reader.GetAttribute("filename");
                                string path = imageDirectory + "\\" + item.Filename + ".bmp";
                                try
                                {
                                    int index = int.Parse(item.Filename);
                                    if (index >= visionBoard.NextIndex)
                                    {
                                        visionBoard.NextIndex = index + 1;
                                    }
                                }
                                catch (Exception) { }

                                if (File.Exists(path))
                                {
                                    item.Image = Image.FromFile(path);
                                    visionBoard.Items.Add(item);
                                }
                                else
                                {
                                    MessageBox.Show(path, "File does not exist");
                                }
                            }
                        }
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return(visionBoard);
        }
Ejemplo n.º 3
0
        public void PlayAStep(Graphics g, int width, int height)
        {
            if (itemIndex >= Items.Count || itemIndex < 0)
            {
                return;
            }
            ImageItem activeItem = (ImageItem)Items[itemIndex];

            // bitmap to draw to and its bitmapG Graphics object
            Bitmap   bitmap  = new Bitmap(width, height);
            Graphics bitmapG = Graphics.FromImage(bitmap);

            //bitmapG.SetClip(new Rectangle(0, 0, width, height));

            // draw bitmapOfStaticItems of all items except activeItem to bitmapG
            getBitmapOfStaticItems(width, height, activeItem);
            bitmapG.DrawImage(bitmapOfStaticItems, 0, 0);

            bitmapG.TranslateTransform(width / 2, height / 2);                                // center (0,0)
            bitmapG.ScaleTransform(ScreensaverForm.ScaleFactor, ScreensaverForm.ScaleFactor); // Scale down to fit

            // actualStep between original position & zoomed-in position
            int actualStep;

            if (Step < MaxStep / 2 - PauseSteps / 2) // zooming in
            {
                actualStep = Step;
            }
            else if (Step > MaxStep / 2 + PauseSteps / 2) // zooming out
            {
                actualStep = MaxStep - Step;
            }
            else // pausing
            {
                actualStep = MaxStep / 2 - PauseSteps / 2;
            }

            // draw activeItem at its current zoom step to bitmapG

            // offset to accelerate
            //int offset = (int)Math.Round(Math.Sqrt(Math.Pow(HalfMaxStep / 2, 2) - Math.Pow(actualStep - HalfMaxStep / 2, 2))); // offset is in the range of [0..HalfMaxStep], slow moving on the ends and fast moving in the middle

            int x = /*-activeItem.X * offset / HalfMaxStep; //*/ (int)Math.Round(Math.Pow((Math.Pow(Math.Abs(activeItem.X), 0.666) / HalfwayStep * actualStep), 1.5));

            if (activeItem.X > 0)
            {
                x = -x;
            }
            int y = /*-activeItem.Y * offset / HalfMaxStep; //*/ (int)Math.Round(Math.Pow((Math.Pow(Math.Abs(activeItem.Y), 0.33) / HalfwayStep * actualStep), 3)); //-activeItem.Y * actualStep / HalfMaxStep;

            if (activeItem.Y > 0)
            {
                y = -y;
            }
            float rot      = -activeItem.RotationDegrees / HalfwayStep * actualStep;
            float maxScale = Math.Min(width * 0.95f / ScreensaverForm.ScaleFactor / activeItem.Size.Width, height * 0.95f / ScreensaverForm.ScaleFactor / activeItem.Size.Height);
            float scale    = 1 + (maxScale - 1) * actualStep / HalfwayStep;

            if (scale == 0)
            {
                scale = 1;
            }

            activeItem.Draw(bitmapG, x, y, rot, scale);

            // draw the bitmap to the screen
            g.DrawImage(bitmap, 0, 0);

            bitmapG.Dispose();
        }