A single element of the user interface that, itself, can contain sub-elements
Inheritance: Microsoft.Xna.Framework.DrawableGameComponent
Exemple #1
0
        private static GUIElement CreateEmptyRoot(Game game)
        {
            Contract.Ensures(Contract.Result<GUIElement>().Bound.X == 0);
            Contract.Ensures(Contract.Result<GUIElement>().Bound.Y == 0);
            Contract.Ensures(Contract.Result<GUIElement>().Bound.Width == game.GraphicsDevice.Viewport.Width);
            Contract.Ensures(Contract.Result<GUIElement>().Bound.Height == game.GraphicsDevice.Viewport.Height);

            GUIElement result = new GUIElement(game, "root", 0, 0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
            result.SetDrawBackground(false);

            return result;
        }
Exemple #2
0
        /// <summary>
        /// Draws a visualization of a SurgeAbility on a target GUIElement. By using this method
        /// to draw the SurgeAbility, you're sure it's drawn the same way every time.
        /// </summary>
        /// <param name="target">The GUIElement to draw the SurgeAbility on.</param>
        /// <param name="ability">The SurgeAbility to display.</param>
        /// <param name="xPosition">The top-left x-coordinate to start drawing from.</param>
        /// <param name="yPosition">The top-left y-coordinate to start drawing from.</param>
        /// <param name="small">True if the surge ability should be drawn as compact as possible, else false.</param>
        public static void DrawSurgeAbility(GUIElement target, SurgeAbility ability, int xPosition, int yPosition, bool small)
        {
            Contract.Requires(target != null);
            Contract.Requires(ability != null);
            Contract.Requires(target.HasPoint(target.Bound.X + xPosition, target.Bound.Y + yPosition));

            // icons
            int cost = ability.Cost;
            int costX = target.Bound.X + xPosition;
            while (cost > 0)
            {
                Image img = new Image(target.Game.Content.Load<Texture2D>("Images/Other/surge"));
                target.AddDrawable(target.Name, img, new Vector2(costX, target.Bound.Y + yPosition));
                cost--;
                costX += img.Texture.Width + (small ? -5 : +2);
            }

            // text
            costX += 10;
            string s = ability.Ability.ToString();
            if (small) s = s.Replace("Damage", "Dmg");
            target.AddText(target.Name, ":" + s, new Vector2(costX - target.Bound.X, yPosition));
        }
Exemple #3
0
        /// <summary>
        /// Draws a given dice-type to the target GUIElement. By using this method
        /// to draw the EDice, you're sure it's drawn the same way every time.
        /// </summary>
        /// <param name="target">The GUIElement to display the dice in.</param>
        /// <param name="dice">The dice to display.</param>
        /// <param name="x">The top-left x-coordinate to start drawing from.</param>
        /// <param name="y">The top-left y-coordinate to start drawing from.</param>
        /// <param name="size">The width and height in pixels.</param>
        public static void DrawDice(GUIElement target, EDice dice, int x, int y, int size)
        {
            Contract.Requires(target != null);
            Contract.Requires(target.HasPoint(x, y));
            Contract.Requires(size > 0);
            Contract.Requires(x + size < target.Bound.X + target.Bound.Width);

            if (dicetinary == null) LoadDiceTextures(target.Game);

            target.AddDrawable(target.Name, new Image(dicetinary[dice]), new Rectangle(x, y, size, size));
        }
Exemple #4
0
        /// <summary>
        /// Creates a GUIElement that fits the given parameters.
        /// </summary>
        /// <param name="game">The current Game object.</param>
        /// <param name="state">The State to be visualized.</param>
        /// <param name="role">The Role to view the given state from.</param>
        /// <param name="gameState">An object that holds some game properties that is needed for some States.</param>
        /// <returns>A new GUIElement that visualizes the given State.</returns>
        public static GUIElement CreateStateElement(Game game, State state, Role role, GameState gameState)
        {
            Contract.Requires(game != null);
            Contract.Ensures(Contract.Result<GUIElement>() != null);
            Contract.Ensures(Contract.Result<GUIElement>().Bound.X == 0);
            Contract.Ensures(Contract.Result<GUIElement>().Bound.Y == 0);
            Contract.Ensures(Contract.Result<GUIElement>().Bound.Width == game.GraphicsDevice.Viewport.Width);
            Contract.Ensures(Contract.Result<GUIElement>().Bound.Height == game.GraphicsDevice.Viewport.Height);

            Viewport g = game.GraphicsDevice.Viewport;

            GUIElement root = CreateEmptyRoot(game);

            switch (state)
            {
                // TODO: fill the root here for all states that is drawn
                case State.InLobby:
                    {
                        GUIElement box = new GUIElement(game, "players", RelW(g, 10), RelH(g, 10), RelW(g, 80), RelH(g, 80));
                        GUIElement p1 = new GUIElement(game, "player1", RelW(g, 40), RelH(g, 15), RelW(g, 20), RelH(g, 20));
                        GUIElement p2 = new GUIElement(game, "player2", RelW(g, 23), RelH(g, 40), RelW(g, 20), RelH(g, 20));
                        GUIElement p3 = new GUIElement(game, "player3", RelW(g, 56), RelH(g, 40), RelW(g, 20), RelH(g, 20));
                        GUIElement p4 = new GUIElement(game, "player4", RelW(g, 23), RelH(g, 65), RelW(g, 20), RelH(g, 20));
                        GUIElement p5 = new GUIElement(game, "player5", RelW(g, 56), RelH(g, 65), RelW(g, 20), RelH(g, 20));

                        root.AddChild(box);
                        box.AddChild(p1);
                        box.AddChild(p2);
                        box.AddChild(p3);
                        box.AddChild(p4);
                        box.AddChild(p5);

                        Vector2 pos = new Vector2(10, 15);
                        root.AddText("player1", "Overlord:", pos);
                        root.AddText("player2", "Hero:", pos);
                        root.AddText("player3", "Hero:", pos);
                        root.AddText("player4", "Hero:", pos);
                        root.AddText("player5", "Hero:", pos);

                        if (Player.Instance.IsServer)
                        {
                            GUIElement start = new GUIElement(game, "start", RelW(g, 84), RelH(g, 90), RelW(g, 12), RelH(g, 4));
                            root.AddChild(start);
                            root.AddText("start", "Start Game", new Vector2(0, 0));

                            string ipString = "";
                            foreach (string ip in Player.Instance.Connection.Ips) ipString += (ip + "\n");
                            root.AddText(box.Name, ipString, new Vector2(10, 10));
                        }

                        break;
                    }
                case State.AllBuyEquipment:
                case State.BuyEquipment:
                    {
                        GUIElement box = new GUIElement(game, "shop", RelW(g, 5), RelH(g, 5), RelW(g, 90), RelH(g, 80));

                        int startY = RelH(g, 10);
                        int startX = RelW(g, 10);

                        int spacerX = RelW(g, 2);
                        int spacerY = RelH(g, 5);

                        int width = RelW(g, 12);
                        int height = RelH(g, 16);

                        Equipment[] shopContent = gameState.CurrentEquipment.Where(n => n.Rarity == EquipmentRarity.Common).Distinct().ToArray();
                        for (int y = 0; y < 4; y++)
                        {
                            for (int x = 0; x < 6; x++)
                            {
                                if (shopContent.Length > (x + y * 6))
                                {
                                    Equipment current = shopContent[x + y * 6];
                                    EquipmentElement eq = new EquipmentElement(game, startX + x * width + x * spacerX,
                                                                               startY + y * width + y * spacerY, width, height,
                                                                               "", current, x + y * 6 + 1000);
                                    box.AddChild(eq);
                                }
                            }
                        }

                        if (role != Role.Overlord)
                        {
                            GUIElement done = new GUIElement(game, "done", RelW(g, 85), RelH(g, 90), RelW(g, 10), RelH(g, 5));
                            done.AddText(done.Name, "Done", new Vector2(0, 0));

                            root.AddChild(done);
                        }
                        root.AddChild(box);
                        break;
                    }
                case State.AllEquip:
                case State.Equip:
                    {

                        if (role != Role.Overlord)
                        {
                            int startX = RelW(g, 15);
                            int startY = RelH(g, 15);
                            int spacerX = RelW(g, 2);
                            int width = RelW(g, 10);

                            Equipment[] unequipped = gameState.UnequippedEquipment(Player.Instance.Id);
                            int boxes = Math.Max(1, unequipped.Length);

                            for (int x = 0; x < boxes; x++)
                            {
                                Equipment current = (x < unequipped.Length) ? unequipped[x] : null;
                                EquipmentElement eq = GUIElementFactory.CreateEquipmentElement(game, startX + x * width + x * spacerX, startY, "", current, x + 100);
                                root.AddChild(eq);
                            }

                            GUIElement done = new GUIElement(game, "done", RelW(g, 3), RelW(g, 3), RelW(g, 10), RelH(g, 5));
                            done.AddText(done.Name, "Done", new Vector2(0, 0));
                            root.AddChild(done);
                        }
                        break;
                    }
                case State.WaitForChooseSquare: // hero placement
                    {
                        if (role != Role.Overlord)
                        {
                            GUIElement help = new GUIElement(game, "help", RelW(g, 3), RelW(g, 3), RelW(g, 33), RelH(g, 5));

                            root.AddChild(help);
                            root.AddText(help.Name, "Select a square to place your hero", new Vector2(5, 5));
                        }
                        break;
                    }
                case State.WaitForHeroTurn:
                    {
                        if (role != Role.Overlord)
                        {
                            GUIElement takeTurn = new GUIElement(game, "take turn", RelW(g, 3), RelW(g, 3), RelW(g, 15), RelH(g, 5));
                            root.AddChild(takeTurn);
                            root.AddText(takeTurn.Name, "Take Turn", new Vector2(5, 5));
                        }
                        break;
                    }
                case State.WaitForChooseAction:
                    {
                        if (role == Role.ActiveHero)
                        {
                            GUIElement advance = new GUIElement(game, "advance", RelW(g, 3), RelH(g, 3), RelW(g, 10), RelH(g, 5));
                            GUIElement run = new GUIElement(game, "run", RelW(g, 3), RelH(g, 10), RelW(g, 10), RelH(g, 5));
                            GUIElement battle = new GUIElement(game, "battle", RelW(g, 3), RelH(g, 17), RelW(g, 10), RelH(g, 5));

                            Vector2 v = new Vector2(5, 5);
                            advance.AddText(advance.Name, "Advance", v);
                            run.AddText(run.Name, "Run", v);
                            battle.AddText(battle.Name, "Battle", v);

                            root.AddChild(advance);
                            root.AddChild(run);
                            root.AddChild(battle);
                        }
                        break;
                    }
                case State.WaitForPerformAction:
                    {
                        if (role == Role.ActiveHero)
                        {
                            GUIElement end = new GUIElement(game, "end", RelW(g, 3), RelW(g, 3), RelW(g, 15), RelH(g, 5));

                            root.AddChild(end);
                            root.AddText(end.Name, "End Turn", new Vector2(5, 5));
                        }
                        else if (role == Role.Overlord && Player.Instance.StateManager.HasTurn())
                        {
                            GUIElement end = new GUIElement(game, "end", RelW(g, 3), RelW(g, 3), RelW(g, 15), RelH(g, 8));
                            root.AddChild(end);
                            root.AddText(end.Name, "End Monster Turn", new Vector2(5, 5));
                        }
                        break;
                    }
                case State.WaitForOverlordChooseAction:
                    {
                        if (role == Role.Overlord)
                        {
                            GUIElement end = new GUIElement(game, "end", RelW(g, 3), RelW(g, 3), RelW(g, 15), RelH(g, 8));

                            root.AddChild(end);
                            root.AddText(end.Name, "End Overlord Turn", new Vector2(5, 5));
                        }
                        break;
                    }
                case State.WaitForRollDice:
                    {
                        GUIElement box = new GUIElement(game, "box", RelW(g, 10), RelH(g, 10), RelW(g, 80), RelH(g, 60));
                        if (gameState.CurrentPlayer == Player.Instance.Id)
                        {
                            GUIElement roll = new GUIElement(game, "roll", RelW(g, 75), RelH(g, 70), RelW(g, 10), RelH(g, 6));
                            root.AddChild(roll);
                            root.AddText(roll.Name, "Roll", new Vector2(5, 5));
                        }
                        root.AddChild(box);

                        break;
                    }
                case State.WaitForDiceChoice:
                    {
                        GUIElement box = new AttackElement(game, gameState.CurrentAttack, RelW(g, 10), RelH(g, 10), RelW(g, 80), RelH(g, 60));

                        if (gameState.CurrentPlayer == Player.Instance.Id)
                        {
                            GUIElement finish = new GUIElement(game, "finish", RelW(g, 75), RelH(g, 70), RelW(g, 10), RelH(g, 8));
                            root.AddChild(finish);
                            root.AddText(finish.Name, "Inflict Wounds", new Vector2(5, 5));
                        }
                        root.AddChild(box);

                        break;
                    }
                case State.EndGameHeroParty:
                    {
                        GUIElement box = new GUIElement(game, "box", RelW(g, 10), RelH(g, 10), RelW(g, 80), RelH(g, 60));
                        GUIElement inner = new GUIElement(game, "inner", RelW(g, 30), RelH(g, 20), RelW(g, 40), RelH(g, 60));
                        box.AddChild(inner);
                        inner.AddText(inner.Name, "The heroes have triumphed over Joe and won the game!", new Vector2(5, 5));
                        inner.SetDrawBackground(false);

                        root.AddChild(box);
                        break;
                    }
                case State.EndGameOverlord:
                    {
                        GUIElement box = new GUIElement(game, "box", RelW(g, 10), RelH(g, 10), RelW(g, 80), RelH(g, 60));
                        GUIElement inner = new GUIElement(game, "inner", RelW(g, 30), RelH(g, 20), RelW(g, 40), RelH(g, 60));
                        box.AddChild(inner);
                        inner.AddText(inner.Name, "Joe has truimphed over the heroes and won the game!", new Vector2(5, 5));
                        inner.SetDrawBackground(false);

                        root.AddChild(box);
                        break;
                    }
            }

            return root;
        }
Exemple #5
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            GUI.GUI.Font = Content.Load<SpriteFont>("font");
            FullModel.LoadContent(this);

            // creation of elements
            this.gui = new GUI.GUI(this);
            GUIElement root = GUIElementFactory.CreateStateElement(this, Descent.State.State.ActivateMonsters, Descent.Model.Player.Role.Overlord, null);
            GUIElement createGame = new GUIElement(this, "create", (GraphicsDevice.Viewport.Width - 300) / 2, 400, 300, 100);
            GUIElement joinGame = new GUIElement(this, "join", (GraphicsDevice.Viewport.Width - 300) / 2, 550, 300, 100);
            GUIElement changeName = new GUIElement(this, "changeName", (GraphicsDevice.Viewport.Width - 200) / 2, 250, 200, 100);
            InputElement nameInput = new InputElement(this, "nameInput", changeName.Bound.X + 10, changeName.Bound.Y + (changeName.Bound.Height - 30) / 2, changeName.Bound.Width - 20, 30);
            InputElement connectInput = new InputElement(this, "connectInput", joinGame.Bound.X + 10, joinGame.Bound.Y + (joinGame.Bound.Height - 30) / 2, 150, 30);
            GUIElement buttonCreateGame = new GUIElement(this, "doneCreate", createGame.Bound.X + (createGame.Bound.Width - 100) / 2, createGame.Bound.Y + (createGame.Bound.Height - 30) / 2, 120, 30);
            GUIElement buttonJoinGame = new GUIElement(this, "doneJoin", joinGame.Bound.X + 200, joinGame.Bound.Y + (joinGame.Bound.Height - 30) / 2, 80, 30);

            // assembling tree
            root.AddChild(createGame);
            root.AddChild(joinGame);
            root.AddChild(changeName);
            changeName.AddChild(nameInput);
            createGame.AddChild(buttonCreateGame);
            joinGame.AddChild(connectInput);
            joinGame.AddChild(buttonJoinGame);

            // adding visual to tree
            changeName.SetBackground("boxbg");
            joinGame.SetBackground("boxbg");
            createGame.SetBackground("boxbg");
            nameInput.SetBackground("boxbg");
            connectInput.SetBackground("boxbg");
            buttonCreateGame.SetBackground("boxbg");
            buttonJoinGame.SetBackground("boxbg");
            Image logo = new Image(Content.Load<Texture2D>("logo-descent"));
            root.AddDrawable(root.Name, logo, new Vector2((root.Bound.Width - logo.Texture.Bounds.Width) / 2.0f, 50));

            // adding logic to tree
            root.SetDrawBackground(false);

            root.AddText("changeName", "Name:", new Vector2(5, 0));
            root.AddText("doneCreate", "New Game!", new Vector2(5, 0));
            root.AddText("doneJoin", "Join!", new Vector2(25, 0));
            root.SetClickAction("doneCreate", (n, g) =>
            {
                // Start the game. TODO: Try/catch error handling.
                n.StartGame(1337);

                // Set the nickname. Since this is the server, it will be set on id 1 always.
                if (InputElement.GetInputFrom("nameInput").Length > 0)
                {
                    n.Nickname = InputElement.GetInputFrom("nameInput");
                }

                // Create the state manager.
                n.StateManager = new StateManager(gui);
            });

            root.SetClickAction("doneJoin", (n, g) =>
            {
                g.SetClickAction(g.Name, null);
                n.JoinGame(InputElement.GetInputFrom("connectInput"), 1337);

                Player.Instance.EventManager.AcceptPlayerEvent += new AcceptPlayerHandler((sender, eventArgs) =>
                {
                    if (eventArgs.PlayerId == Player.Instance.Id)
                    {
                        if (InputElement.GetInputFrom("nameInput").Length > 0)
                        {
                            n.Nickname = InputElement.GetInputFrom("nameInput");
                        }

                        n.StateManager = new StateManager(gui);

                        Player.Instance.EventManager.QueueEvent(EventType.PlayerJoined, new PlayerJoinedEventArgs(Player.Instance.Id, Player.Instance.Nickname));
                    }
                });
            });

            Player.Instance.EventManager.PlayerJoinedEvent += new PlayerJoinedHandler((sender, eventArgs) =>
            {
                // If the PlayerJoined event is about our local player(the id will be set just before this, so

            });

            // placing the root in the gui
            gui.ChangeStateGUI(root);
        }
Exemple #6
0
        /// <summary>
        /// Adds the given GUIElement to the set of children within
        /// this one.
        /// </summary>
        /// <param name="child">The new child</param>
        public void AddChild(GUIElement child)
        {
            Contract.Requires(child != null);
            Contract.Requires(child.Bound.X >= Bound.X);
            Contract.Requires(child.Bound.Y >= Bound.Y);
            Contract.Ensures(Contract.OldValue<int>(children.Count) + 1 == children.Count);

            children.Add(child);
        }