SetBackground() public method

Changes the background to the given asset and enables background drawing.
public SetBackground ( string assetName ) : void
assetName string The name of the asset in the Content project.
return void
示例#1
0
文件: Game1.cs 项目: nezbo/Descent
        /// <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);
        }