Example #1
0
        private void cvMap_MouseMove(object sender, MouseEventArgs e)
        {
            int X, Y;        // tọa độ của chuột

            if (isAutoAlign) // tự đọng căn, vẽ tọa độ tương đối
            {
                X = (int)(e.GetPosition(this.cvMap).X / MainWindow.OBJECT_WIDTH) * MainWindow.OBJECT_WIDTH;
                Y = (int)(e.GetPosition(this.cvMap).Y / MainWindow.OBJECT_HEIGHT) * MainWindow.OBJECT_HEIGHT;
            }
            else // vẽ bằng tay, tọa độ tuyệt đối
            {
                X = (int)e.GetPosition(this.cvMap).X;
                Y = (int)e.GetPosition(this.cvMap).Y;
            }

            if (selectedImage == null)
            {
                this.tbX.Text = X.ToString();
                this.tbY.Text = Y.ToString();
            }
            else
            {
                this.tbX.Text = ((int)Canvas.GetLeft(selectedImage)).ToString();
                this.tbY.Text = ((int)Canvas.GetTop(selectedImage)).ToString();
            }

            // remove line căn nếu đã add
            if (this.cvMap.Children.Count > 0)
            {
                List <UIElement> lines = new List <UIElement>();
                foreach (UIElement item in this.cvMap.Children)
                {
                    if (item is Line)
                    {
                        Line l = item as Line;
                        if (l.Tag != null && l.Tag.ToString() == "Align")
                        {
                            lines.Add(item);
                        }
                    }
                }

                if (lines.Count > 0)
                {
                    foreach (var item in lines)
                    {
                        this.cvMap.Children.Remove(item);
                    }
                }
            }

            // tạo hình giả theo chuột
            if (this._image != null)
            {
                if (lastImage != null && lastImage.Opacity == 0.5f)
                {
                    this.cvMap.Children.Remove(lastImage);
                }

                curImage = new Image();
                int w, h;

                Factory.ConvertToStandardImage(this._image, out w, out h);
                curImage.Width   = w;
                curImage.Height  = h;
                curImage.Source  = this._image.Source;
                curImage.Opacity = 0.5f; // dùng để nhận biết và phân biệt với image đc vẽ trong canvas

                Image rel_X, rel_Y;
                Alignment.Direction dir;
                Point p = Alignment.GetAutoAlignOffset(this.cvMap, X, Y, w, h, 3, 3, out rel_X, out rel_Y, out dir);

                if (p != (new Point(0, 0)))
                {
                    X += (int)p.X;
                    Y += (int)p.Y;
                    Alignment.AddAlignLine(this.cvMap, X, Y, w, h, rel_X, rel_Y, dir);
                }

                Canvas.SetLeft(curImage, X);
                Canvas.SetTop(curImage, Y);

                this.cvMap.Children.Add(curImage);
                lastImage = curImage;
                curImage  = null;

                curMouseState = e.LeftButton;
                if (curMouseState == MouseButtonState.Pressed && preMouseState == MouseButtonState.Released) // nhấn chuột trái
                {
                    Management.AddObjectToCanvas(this.cvMap, this._image, X, Y, i_MouseLeftButtonDown, i_MouseLeftButtonUp, canOverlap);
                    _isUnsaved = true;
                    _begin_X   = X;
                    _begin_Y   = Y;
                }
                else if (curMouseState == MouseButtonState.Pressed && e.RightButton == MouseButtonState.Pressed)
                {
                    if (e.GetPosition(this.cvMap).X > _begin_X)
                    {
                        int iWidth, iHeight;
                        Factory.ConvertToStandardImage(this._image, out iWidth, out iHeight);

                        while (_begin_X + iWidth < e.GetPosition(this.cvMap).X)
                        {
                            _begin_X += iWidth;
                            if (Management.HasAlreadyOverLap(this.cvMap, _begin_X, _begin_Y) == false)
                            {
                                Management.AddObjectToCanvas(this.cvMap, this._image, _begin_X, _begin_Y, i_MouseLeftButtonDown, i_MouseLeftButtonUp, canOverlap);
                            }
                        }
                    }
                    else if (e.GetPosition(this.cvMap).X < _begin_X)
                    {
                        int iWidth, iHeight;
                        Factory.ConvertToStandardImage(this._image, out iWidth, out iHeight);

                        while (_begin_X - iWidth > e.GetPosition(this.cvMap).X)
                        {
                            _begin_X -= iWidth;
                            if (Management.HasAlreadyOverLap(this.cvMap, _begin_X, _begin_Y) == false)
                            {
                                Management.AddObjectToCanvas(this.cvMap, this._image, _begin_X, _begin_Y, i_MouseLeftButtonDown, i_MouseLeftButtonUp, canOverlap);
                            }
                        }
                    }
                }

                preMouseState = curMouseState;
            }
            else if (this.selectedImage != null)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    isDragDrop = true;

                    selected_Offset_X = (int)e.GetPosition(this.cvMap).X - (selected_X + delta_X);
                    // selected_Offset_Y = (selected_Y + delta_Y)- (int)e.GetPosition(this.cvMap).Y;
                    selected_Offset_Y = (int)e.GetPosition(this.cvMap).Y - (selected_Y + delta_Y);
                    Image rel_X, rel_Y;
                    Alignment.Direction dir;
                    Point p = Alignment.GetAutoAlignOffset(
                        this.cvMap,
                        (int)e.GetPosition(this.cvMap).X - delta_X,
                        (int)e.GetPosition(this.cvMap).Y - delta_Y,
                        (int)selectedImage.Width, (int)selectedImage.Height, 3, 3, out rel_X, out rel_Y, out dir);

                    if (p != (new Point(0, 0)))
                    {
                        selected_Offset_X += (int)p.X;
                        selected_Offset_Y -= (int)p.Y;
                        Alignment.AddAlignLine(this.cvMap, selected_X + selected_Offset_X, selected_Y - selected_Offset_Y, (int)selectedImage.Width, (int)selectedImage.Height, rel_X, rel_Y, dir);
                    }

                    //this.tbOffsetX.Text = selected_Offset_X.ToString();
                    //this.tbOffsetY.Text = selected_Offset_Y.ToString();
                }
                else
                {
                    this.i_MouseLeftButtonUp(null, null);
                }
            }
        }
Example #2
0
        // thêm đối tượng vào canvas
        public static void AddObjectToCanvas(
            Canvas cvMap,
            Image image,
            int X, int Y,
            MouseButtonEventHandler mouseDown, MouseButtonEventHandler mouseUp,
            bool canOverlap)
        {
            if (image != null)
            {
                #region Kiểm tra đảm bảo chỉ có duy nhất một nhân vật Mario trong map

                if ((EObjectName)(int)image.Tag == EObjectName.SMALL_MARIO)
                {
                    if (HasMarioAdded(cvMap))
                    {
                        MessageBox.Show("Mario Object has already been added into map!");
                        return;
                    }
                }
                //else if ((EObjectName)(int)image.Tag == EObjectName.CREEP_UP_PIPE_1)
                //{
                //    //if (HasCreepUp(cvMap, 0))
                //    //{
                //    //    MessageBox.Show("Creep Up Pipe 1 Object has already been added into map!");
                //    //    return;
                //    //}
                //}
                //else if ((EObjectName)(int)image.Tag == EObjectName.CREEP_UP_PIPE_2)
                //{
                //    //if (HasCreepUp(cvMap, 1))
                //    //{
                //    //    MessageBox.Show("Creep Up Pipe 2 Object has already been added into map!");
                //    //    return;
                //    //}
                //}
                #endregion

                #region Thêm image vào canvas
                Image i = new Image();
                int   w, h;

                Factory.ConvertToStandardImage(image, out w, out h);
                i.Width  = w;
                i.Height = h;
                i.Source = image.Source;
                i.Tag    = image.Tag;
                i.MouseLeftButtonDown += mouseDown;
                i.MouseLeftButtonUp   += mouseUp;

                if (!canOverlap)
                {
                    if (cvMap.Children.Count > 0)
                    {
                        foreach (var item in cvMap.Children)
                        {
                            // nếu đã tồn tại đối tượng tại vị trí (X, Y) thì k add vào canvas, return khỏi hàm
                            if (item is Image && ((Image)item).Opacity != 0.5f)
                            {
                                if ((int)Canvas.GetLeft((Image)item) == X && (int)Canvas.GetTop((Image)item) == Y)
                                {
                                    MessageBox.Show("This coordinate has a object!");
                                    return;
                                }
                            }
                        }
                    }
                }

                Image rel_X, rel_Y;
                Alignment.Direction dir;
                Point p = Alignment.GetAutoAlignOffset(cvMap, X, Y, w, h, 3, 3, out rel_X, out rel_Y, out dir);

                if (p != (new Point(0, 0)))
                {
                    X += (int)p.X;
                    Y += (int)p.Y;
                }

                Canvas.SetLeft(i, X);
                Canvas.SetTop(i, Y);

                if (Factory.CheckObjectCollisionAbility((EObjectName)i.Tag) == false)
                {
                    //if ((EObjectName)i.Tag == EObjectName.FLAG_POSITION)
                    //{
                    //    cvMap.Children.Add(i);
                    //}
                    //else
                    //{
                    cvMap.Children.Insert(0, i);
                    //}
                }
                else
                {
                    if ((EObjectName)i.Tag == EObjectName.MISC_CASTLE)
                    {
                        cvMap.Children.Insert(0, i);
                    }
                    else
                    {
                        cvMap.Children.Add(i);
                    }
                }
                #endregion
            }
        }