/// <summary> /// Update the Environment each frame. /// </summary> /// <param name="elapsedTime">Time since last Update() call.</param> public override void Update(float elapsedTime) { MouseState mouse = Mouse.GetState(); Vector2 mousePos = new Vector2(mouse.X, mouse.Y); bool mousePressed = (mouse.LeftButton == ButtonState.Pressed); List<Widget> collidingButtons = Buttons.FindAll(b => b.Visible && b.Collides(mousePos)); Widget button = null; if (collidingButtons.Count > 0) button = collidingButtons.OrderByDescending(b => b.Zindex).First(); // We switched to a new button. if (m_activeButton != button) { // Unset current button. if (m_activeButton != null) { m_activeButton.DispatchOnMouseOut(); if (m_mouseWasPressed) m_activeButton.DispatchOnMouseUp(false); } // Activate new one. m_activeButton = button; if (m_activeButton != null) { m_activeButton.DispatchOnMouseOver(); if (mousePressed && !m_mouseWasPressed) { m_activeButton.DispatchOnMouseDown(); } } } else if (m_activeButton != null) { // Button is the same. Update it. if (m_mouseWasPressed && !mousePressed) { m_activeButton.DispatchOnMouseUp(true); } else if (!m_mouseWasPressed && mousePressed) { m_activeButton.DispatchOnMouseDown(); } } m_mouseWasPressed = mousePressed; base.Update(elapsedTime); }
private Widget CreateSprite(string tex) { Widget widget = new Widget(this); widget.LoadTexture(contentManager, tex); widget.Registration = new Vector2(widget.Texture.Width * 0.5f, widget.Texture.Height * 0.5f); widget.Zindex = 0.5f; return widget; }
private void CreateButton(Widget widget, int height = 70) { widget.Registration = new Vector2(widget.Texture.Width * 0.5f, widget.Texture.Height * 0.5f); int width = (int) Math.Round((double) widget.Texture.Width); widget.CreateButton(new Rectangle(0, 0, width, height)); widget.Zindex = 0.5f; }
public MainMenu(Controller ctrl) : base(ctrl) { Sound.StopAll(true); Sound.PlayCue("main_theme"); Controller.IsMouseVisible = true; // Background. m_background = new Widget(this); m_background.LoadTexture(contentManager, "space-desktop"); m_background.PositionPercent = new Vector2(0.5f, 0.5f); m_background.Zindex = 1.0f; m_background.Registration = new Vector2(m_background.Texture.Width, m_background.Texture.Height) * 0.5f; AddChild(m_background); // Logo. m_logo = new Widget(this); m_logo.LoadTexture(contentManager, "logo_sputnik"); m_logo.PositionPercent = new Vector2(0.5f, 0.3f); m_logo.Position = new Vector2(-75.0f, 0.0f); m_logo.Zindex = 0.8f; m_logo.Registration = new Vector2(375.0f, 230.0f); m_mainMenu.Add(m_logo); AddChild(m_logo); // Logo text. m_logoText = new Widget(this); m_logoText.LoadTexture(contentManager, "logo"); m_logoText.PositionPercent = new Vector2(0.5f, 0.3f); m_logoText.Position = new Vector2(-75.0f, 25.0f); m_logoText.Registration = new Vector2(375.0f, 230.0f); m_logoText.Zindex = 0.7f; m_mainMenu.Add(m_logoText); AddChild(m_logoText); // TeamQ badge. m_mainTeamQ = CreateSprite("teamq"); m_mainTeamQ.PositionPercent = new Vector2(0.0f, 1.0f); m_mainTeamQ.Position = new Vector2(50.0f, -50.0f); m_mainMenu.Add(m_mainTeamQ); AddChild(m_mainTeamQ); //// Vector2 k_buttonPos = new Vector2(0.5f, 0.5f); const float k_buttonSpacing = 60.0f; float ypos = 50.0f; ImageButton startLevel = new ImageButton(this, "main_start_level", "main_start_level1"); startLevel.PositionPercent = k_buttonPos; startLevel.Position = new Vector2(0.0f, ypos); CreateButton(startLevel); startLevel.OnActivate += () => { ShowDifficulty(); }; m_mainMenu.Add(startLevel); AddChild(startLevel); ypos += k_buttonSpacing; ImageButton credits = new ImageButton(this, "main_credits", "main_credits1"); credits.PositionPercent = k_buttonPos; credits.Position = new Vector2(0.0f, ypos); CreateButton(credits); credits.OnActivate += () => { ShowCredits(); }; m_mainMenu.Add(credits); AddChild(credits); ypos += k_buttonSpacing; ImageButton quit = new ImageButton(this, "main_quit", "main_quit1"); quit.PositionPercent = k_buttonPos; quit.Position = new Vector2(0.0f, ypos); CreateButton(quit); quit.OnActivate += () => { Controller.Exit(); }; m_mainMenu.Add(quit); AddChild(quit); //////////////////////////////////////////////////////////////////////////// ///// Difficulty select. Widget sprite = CreateSprite("difficulty_bg"); sprite.Visible = false; sprite.PositionPercent = new Vector2(0.5f, 0.5f); sprite.Zindex = 0.2f; m_difficultySelect.Add(sprite); AddChild(sprite); sprite = CreateSprite("choose_difficulty"); sprite.Visible = false; sprite.PositionPercent = new Vector2(0.5f, 0.5f); sprite.Zindex = 0.1f; m_difficultySelect.Add(sprite); AddChild(sprite); ImageButton diffNormal = new ImageButton(this, "diff_normal", "diff_normal1"); CreateButton(diffNormal, 50); diffNormal.Visible = false; diffNormal.PositionPercent = new Vector2(0.5f, 0.5f); diffNormal.Position = new Vector2(0.0f, -20.0f); diffNormal.Zindex = 0.1f; diffNormal.OnActivate += () => { Controller.ChangeEnvironment(new Level1Environment(Controller)); }; m_difficultySelect.Add(diffNormal); AddChild(diffNormal); ImageButton diffFrost = new ImageButton(this, "diff_frost", "diff_frost1"); CreateButton(diffFrost, 50); diffFrost.Visible = false; diffFrost.PositionPercent = new Vector2(0.5f, 0.5f); diffFrost.Position = new Vector2(0.0f, 40.0f); diffFrost.Zindex = 0.1f; diffFrost.OnActivate += () => { Level1Environment env = new Level1Environment(Controller); env.isFrostMode = true; Controller.ChangeEnvironment(env); }; m_difficultySelect.Add(diffFrost); AddChild(diffFrost); //////////////////////////////////////////////////////////////////////////// ///// Create credits. sprite = CreateSprite("credits"); sprite.Visible = false; sprite.PositionPercent = new Vector2(0.5f, 0.5f); m_credits.Add(sprite); AddChild(sprite); // TeamQ badge. sprite = CreateSprite("teamq"); sprite.PositionPercent = new Vector2(0.5f, 0.225f); sprite.Visible = false; m_credits.Add(sprite); AddChild(sprite); sprite = CreateSprite("programmers"); sprite.Visible = false; sprite.PositionPercent = new Vector2(0.5f, 0.5f); m_credits.Add(sprite); AddChild(sprite); sprite = CreateSprite("programmer_names"); sprite.Visible = false; sprite.PositionPercent = new Vector2(0.5f, 0.5f); m_credits.Add(sprite); AddChild(sprite); sprite = CreateSprite("art_sound"); sprite.Visible = false; sprite.PositionPercent = new Vector2(0.5f, 0.5f); m_credits.Add(sprite); AddChild(sprite); sprite = CreateSprite("kayu"); sprite.Visible = false; sprite.PositionPercent = new Vector2(0.5f, 0.5f); m_credits.Add(sprite); AddChild(sprite); sprite = CreateSprite("special_thanks"); sprite.Visible = false; sprite.PositionPercent = new Vector2(0.5f, 0.5f); m_credits.Add(sprite); AddChild(sprite); sprite = CreateSprite("special"); sprite.Visible = false; sprite.PositionPercent = new Vector2(0.5f, 0.5f); m_credits.Add(sprite); AddChild(sprite); // Credits return button. Widget creditsReturn = new Widget(this); creditsReturn.Visible = false; creditsReturn.CreateButton(new Rectangle(0, 0, 5000, 5000)); creditsReturn.Zindex = 0.1f; creditsReturn.PositionPercent = new Vector2(0.0f, 0.0f); creditsReturn.OnActivate += () => { HideCredits(); }; m_credits.Add(creditsReturn); AddChild(creditsReturn); }
public HUD(GameEnvironment env) : base(env.Controller) { Environment = env; // Cursor. Cursor = new Widget(this); Cursor.LoadTexture(contentManager, "bullet_arrow"); Cursor.Registration = new Vector2(Cursor.Texture.Width, Cursor.Texture.Height) * 0.5f; Cursor.Scale = 0.2f; AddChild(Cursor); // Arrow pointing towards boss. BossDirection = new Widget(this); BossDirection.LoadTexture(contentManager, "arrow"); BossDirection.Registration = new Vector2(BossDirection.Texture.Width, BossDirection.Texture.Height) * 0.5f; BossDirection.PositionPercent = new Vector2(1.0f, 1.0f); BossDirection.Position = new Vector2(-75.0f, -75.0f); AddChild(BossDirection); ///// // Boss health meter. BossHealth = new RectangleWidget(this, ScreenSize.X * 0.75f - 4.0f, 20.0f - 4.0f); BossHealth.PositionPercent = new Vector2(0.5f, 0.0f); BossHealth.Position = new Vector2(0.0f, 20.0f); BossHealth.VertexColor = Color.DarkRed; BossHealth.Zindex = 0.0f; AddChild(BossHealth); // Boss health background. BossHealthBG = new RectangleWidget(this, ScreenSize.X * 0.75f, 20.0f); BossHealthBG.PositionPercent = new Vector2(0.5f, 0.0f); BossHealthBG.Position = new Vector2(0.0f, 20.0f); BossHealthBG.VertexColor = Color.Black; BossHealthBG.Zindex = 1.0f; AddChild(BossHealthBG); ///// // Ship health meter. ShipHealth = new RectangleWidget(this, ScreenSize.X * 0.25f - 4.0f, 20.0f - 4.0f); ShipHealth.PositionPercent = new Vector2(0.0f, 1.0f); ShipHealth.Position = new Vector2(10.0f + ScreenSize.X * 0.25f / 2, -20.0f); ShipHealth.VertexColor = Color.Green; ShipHealth.Zindex = 0.5f; ShipHealth.Alpha = 0.0f; AddChild(ShipHealth); // Ship health shield. ShipHealthShield = new RectangleWidget(this, ScreenSize.X * 0.25f - 4.0f, 20.0f - 4.0f); ShipHealthShield.PositionPercent = new Vector2(0.0f, 1.0f); ShipHealthShield.Position = new Vector2(10.0f + ScreenSize.X * 0.25f / 2, -20.0f); ShipHealthShield.VertexColor = Color.LightBlue; ShipHealthShield.Zindex = 0.0f; ShipHealthShield.Alpha = 0.0f; AddChild(ShipHealthShield); // Ship health background. ShipHealthBG = new RectangleWidget(this, ScreenSize.X * 0.25f, 20.0f); ShipHealthBG.PositionPercent = new Vector2(0.0f, 1.0f); ShipHealthBG.Position = new Vector2(10.0f + ScreenSize.X * 0.25f / 2, -20.0f); ShipHealthBG.VertexColor = Color.Black; ShipHealthBG.Zindex = 1.0f; ShipHealthBG.Alpha = 0.0f; AddChild(ShipHealthBG); ///// // Sputnik time left. SputnikTimeLeft = new RectangleWidget(this, 100.0f - 4.0f, 10.0f - 4.0f); SputnikTimeLeft.VertexColor = Color.White; SputnikTimeLeft.Zindex = 0.0f; SputnikTimeLeft.Alpha = 0.0f; AddChild(SputnikTimeLeft); // Sputnik time left background. SputnikTimeLeftBG = new RectangleWidget(this, 100.0f, 10.0f); SputnikTimeLeftBG.VertexColor = Color.Black; SputnikTimeLeftBG.Zindex = 1.0f; SputnikTimeLeftBG.Alpha = 0.0f; AddChild(SputnikTimeLeftBG); }