Пример #1
0
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }


            string xrml_current = "";

            if (!string.IsNullOrEmpty(_xrml_text))
            {
                using (TextReader reader = XmlTool.OpenTextReader("Text2.XRML"))
                {
                    xrml_current = reader.ReadToEnd();
                }


                if (_xrml_text != xrml_current)
                {
                    try
                    {
                        using (XmlReader xmlReader = XmlTool.OpenXmlReader("Text2.XRML"))
                        {
                            RemoveContainer();
                            RootContainer          = (IContainer)XmlTool.Deserialize(typeof(Grid), xmlReader);
                            RootContainer.Position = new Point(568, 100);
                            RootContainer.Initialize();
                            RootContainer.Setup();

                            List <Slot <UIObject> > backup = new List <Slot <UIObject> >();
                            backup = RootContainer.Slots.ToList();

                            // Temporary workaround to add items in the same order as it was saved
                            // However it is not perfect yet
                            backup = backup.OrderBy(s => s, Comparer <Slot <UIObject> > .Create((x, y) =>
                                                                                                (Point)x.Position > y.Position ? 1 :
                                                                                                (Point)x.Position < y.Position ? -1 :
                                                                                                0)).ToList();

                            for (int i = backup.Count - 1; i >= 0; i--)
                            {
                                backup[i].Item.Editable = true;
                                backup[i].Item.Locked   = false;
                                RootContainer.Insert(backup[i]);
                            }

                            RootContainer.AddSpriteRenderer(_spriteBatch);
                            RootContainer.AddStringRenderer(_spriteBatch);
                            RootContainer.Update(gameTime);
                            RootContainer.UpdateLayout();

                            AddContainer();
                        }
                        _xrml_text = xrml_current;
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }



            if (Singleton.Input.KeyReleased(Keys.Escape))
            {
                Exit();
            }
            if (Singleton.Input.KeyReleased(Keys.Delete))
            {
                RootContainer.RemoveItem(CurrentObject);
                RootContainer.RemoveSlot(CurrentObject);
                RootContainer.Update(gameTime);
                RootContainer.UpdateLayout();
            }



            for (int i = _guiList.Count - 1; i >= 0; i--)
            {
                _guiList[i].Update(gameTime);
            }
            _fileMenu.Update(gameTime);

            Singleton.Input.Update();


            MouseGUI.HitObject = null;

            for (int i = 0; i < _guiList.Count; i++)
            {
                MouseGUI.HitObject = _guiList[i].HitTest(MouseGUI.Position);
                if (MouseGUI.HitObject != null)
                {
                    break;
                }
            }

            if (MouseGUI.HitObject != null)
            {
                if (MouseGUI.LeftWasPressed)
                {
                    MouseGUI.Focus      = MouseGUI.HitObject;
                    MouseGUI.DragOffset = MouseGUI.Focus.Position - MouseGUI.Position;

                    _fromContainer = RootContainer.Contains(MouseGUI.Focus) ? RootContainer : UIToolShelf;


                    if (MouseGUI.Focus.Editable && RootContainer.Contains(MouseGUI.Focus))
                    {
                        RootContainer.RemoveItem(MouseGUI.Focus);
                    }
                }
                else
                {
                    if (MouseGUI.Focus != null)
                    {
                        if (MouseGUI.Focus.Editable && MouseGUI.Focus.MoveState == MoveOption.DYNAMIC)
                        {
                            MouseGUI.Focus.Position = MouseGUI.Position + MouseGUI.DragOffset;
                            MouseGUI.Focus.Update(gameTime);

                            // TODO: Make sure this is inside the MainWindowFrame bounds
                            if (MouseGUI.LeftIsPressed && RootContainer.Contains(MouseGUI.Position))
                            {
                                Point newItemPosition = RootContainer.SimulateInsert(MouseGUI.Focus.Position - RootContainer.Position, MouseGUI.Focus);
                                newItemPosition += RootContainer.Position;

                                Point rightBarPosition  = new Point(newItemPosition.X + MouseGUI.Focus.Width, newItemPosition.Y);
                                Point leftBarPosition   = new Point(newItemPosition.X, newItemPosition.Y);
                                Point topBarPosition    = new Point(newItemPosition.X, newItemPosition.Y);
                                Point bottomBarPosition = new Point(newItemPosition.X, newItemPosition.Y + MouseGUI.Focus.Height);

                                Selection.UpdatePosition(rightBarPosition, leftBarPosition, topBarPosition, bottomBarPosition);
                                Selection.UpdateSize(MouseGUI.Focus);

                                Selection.Show();
                            }
                        }


                        if (MouseGUI.LeftWasReleased)
                        {
                            Selection.Hide();
                            if (MouseGUI.Focus.Editable && RootContainer.Contains(MouseGUI.Focus) && _fromContainer != UIToolShelf)
                            {
                                CurrentObject = MouseGUI.Focus;
                                RootContainer.RemoveSlot(MouseGUI.Focus);
                                RootContainer.AddItem(MouseGUI.Focus.Position - RootContainer.Position, MouseGUI.Focus);
                                RootContainer.Update(gameTime);
                                RootContainer.UpdateLayout();
                            }

                            if (MouseGUI.Focus is UIObject && !MouseGUI.Focus.Editable)
                            {
                                MouseGUI.Focus.MouseEvent.Click();
                            }


                            if (MouseGUI.Focus.Editable && RootContainer.Contains(MouseGUI.Focus.Position) && _fromContainer == UIToolShelf)
                            {
                                CurrentObject = UIFactory <UIObject, IContainer> .CreateObject(MouseGUI.Focus.Position, MouseGUI.Focus, RootContainer);

                                CurrentObject.InitPropertyPanel();
                                CurrentObject.AddSpriteRenderer(_mainBatch);
                                CurrentObject.AddStringRenderer(_mainBatch);
                                CurrentObject.AddPropertyRenderer(_mainBatch);
                                RootContainer.Update(gameTime);
                                RootContainer.UpdateLayout();
                            }
                            if (MouseGUI.Focus.Locked)
                            {
                                CurrentObject = MouseGUI.Focus;
                            }

                            MouseGUI.ClearFocus();
                        }
                    }
                }
            }
            if (MouseGUI.Focus != null && !MouseGUI.Focus.Locked)
            {
                if (MouseGUI.LeftIsPressed && _fromContainer != UIToolShelf)
                {
                    if (MouseGUI.Position.X <= RootContainer.Rect.Left)
                    {
                        MouseGUI.Focus.Position =
                            new Point(RootContainer.Rect.Left, MouseGUI.Position.Y);
                    }
                    if (MouseGUI.Position.X + MouseGUI.Focus.Width >= RootContainer.Rect.Right)
                    {
                        MouseGUI.Focus.Position =
                            new Point(RootContainer.Rect.Right - MouseGUI.Focus.Width, MouseGUI.Position.Y);
                    }
                    if (MouseGUI.Position.Y + MouseGUI.Focus.Height >= RootContainer.Rect.Bottom)
                    {
                        MouseGUI.Focus.Position =
                            new Point(MouseGUI.Position.X, RootContainer.Rect.Bottom - MouseGUI.Focus.Height);
                    }
                    if (MouseGUI.Position.Y - MouseGUI.Focus.Height <= RootContainer.Rect.Top)
                    {
                        MouseGUI.Focus.Position =
                            new Point(MouseGUI.Position.X, RootContainer.Rect.Top);
                    }

                    MouseGUI.Focus.Update(gameTime);
                }
            }

            MouseGUI.Update();
            base.Update(gameTime);
        }