Skip to content

Commit

Permalink
[191] Button gemacht! Mit Hover!! Hurra
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Wendel committed Jul 9, 2015
1 parent 526a77f commit fc8f882
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 8 deletions.
47 changes: 47 additions & 0 deletions OctoAwesome/OctoAwesome.Client/Components/Hud/ButtonControl.cs
@@ -0,0 +1,47 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OctoAwesome.Client.Components.Hud
{
internal class ButtonControl : Control
{
public Brush Background { get; set; }

public Brush Hovered { get; set; }

public string Text { get; set; }

public SpriteFont Font { get; set; }

public Color Color { get; set; }

public ButtonControl(IScreenManager screenManager) : base(screenManager) { }

public override void Draw(SpriteBatch batch, GameTime gameTime)
{
if (IsHovered)
{
if (Hovered != null)
Hovered.Draw(batch, new Rectangle(Position.X, Position.Y, Size.X, Size.Y));
else if (Background != null)
Background.Draw(batch, new Rectangle(Position.X, Position.Y, Size.X, Size.Y));
}
else
{
if (Background != null)
Background.Draw(batch, new Rectangle(Position.X, Position.Y, Size.X, Size.Y));
}

batch.Begin();
Vector2 textSize = Font.MeasureString(Text);
batch.DrawString(Font, Text, new Vector2(
Position.X + ((Size.X - textSize.X) / 2),
Position.Y + ((Size.Y - textSize.Y) / 2)), Color);
batch.End();
}
}
}
2 changes: 2 additions & 0 deletions OctoAwesome/OctoAwesome.Client/Components/Hud/Control.cs
Expand Up @@ -17,6 +17,8 @@ internal abstract class Control : UiElement

public bool Visible { get; set; }

public bool IsHovered { get; internal set; }

public Control(IScreenManager screenManager) : base(screenManager)
{
Enabled = true;
Expand Down
15 changes: 15 additions & 0 deletions OctoAwesome/OctoAwesome.Client/Components/Hud/InventoryScreen.cs
Expand Up @@ -13,6 +13,7 @@ internal class InventoryScreen : Screen

private PanelControl panel;
private LabelControl headline;
private ButtonControl closeButton;

public InventoryScreen(IScreenManager screenManager) : base(screenManager)
{
Expand Down Expand Up @@ -43,6 +44,20 @@ public override void LoadContent()
};
Controls.Add(headline);

closeButton = new ButtonControl(ScreenManager) {
Background = new SolidColorBrush(ScreenManager) { Color = Color.DarkBlue },
Hovered = new SolidColorBrush(ScreenManager) { Color = Color.Blue },
Font = ScreenManager.NormalText,
Text = "Close",
Color = Color.White,
Position = new Index2(
((ScreenManager.ScreenSize.X - 600) / 2) + 100,
((ScreenManager.ScreenSize.Y - 400) / 2) + 170),
Size = new Index2(200, 50),
};

Controls.Add(closeButton);

foreach (var control in Controls)
control.LoadContent();
}
Expand Down
2 changes: 1 addition & 1 deletion OctoAwesome/OctoAwesome.Client/Components/Hud/Screen.cs
Expand Up @@ -11,7 +11,7 @@ internal abstract class Screen : UiElement

private List<Control> controls = new List<Control>();

protected List<Control> Controls
public List<Control> Controls
{
get
{
Expand Down
Expand Up @@ -14,6 +14,7 @@ internal class KeyboardScreenInput : IScreenInputSet
public Index2 PointerPosition
{
get { return Index2.Zero; }
set { }
}

public void Update()
Expand Down
Expand Up @@ -8,12 +8,21 @@ namespace OctoAwesome.Client.Components.Input
{
internal class MouseScreenInput : IScreenInputSet
{
public Index2 PointerPosition { get; private set; }
private Index2 mousePointer;

public Index2 PointerPosition
{
get { return mousePointer; }
set
{
Mouse.SetPosition(value.X, value.Y);
}
}

public void Update()
{
MouseState state = Mouse.GetState();
PointerPosition = new Index2(state.X, state.Y);
mousePointer = new Index2(state.X, state.Y);
}

public event OnKeyChange OnKeyDown;
Expand Down
11 changes: 9 additions & 2 deletions OctoAwesome/OctoAwesome.Client/Components/InputComponent.cs
Expand Up @@ -15,6 +15,8 @@ internal sealed class InputComponent : GameComponent, IInputSet, IScreenInputSet
private List<IInputSet> inputDevices;
private List<IScreenInputSet> screenInputDevices;

private Index2 mousePointer;

private MouseInput mouse;
private KeyboardInput keyboard;
private GamePadInput gamepad;
Expand All @@ -23,7 +25,12 @@ internal sealed class InputComponent : GameComponent, IInputSet, IScreenInputSet
private KeyboardScreenInput screenKeyboard;

public bool ScreenMode { get; set; }
public Index2 PointerPosition { get; private set; }
public Index2 PointerPosition
{
get { return mousePointer; }
set { screenMouse.PointerPosition = value; }
}

public float MoveX { get; private set; }
public float MoveY { get; private set; }
public float HeadX { get; private set; }
Expand Down Expand Up @@ -89,7 +96,7 @@ public override void Update(GameTime gameTime)
Game.IsMouseVisible = true;
screenMouse.Update();
screenKeyboard.Update();
PointerPosition = screenMouse.PointerPosition;
mousePointer = screenMouse.PointerPosition;
}
else
{
Expand Down
Expand Up @@ -32,12 +32,17 @@ public ScreenManagerComponent(Game game, InputComponent input)

void input_OnKeyUp(Keys key)
{
throw new NotImplementedException();
if (key == Keys.Escape)
{
ActiveScreen = null;
input.PointerPosition = ScreenSize / 2;
input.ScreenMode = false;
}
}

void input_OnKeyDown(Keys key)
{
throw new NotImplementedException();
// throw new NotImplementedException();
}

protected override void LoadContent()
Expand All @@ -60,6 +65,17 @@ public override void Update(GameTime gameTime)
input.ScreenMode = true;
}

if (ActiveScreen != null)
{
foreach (var control in ActiveScreen.Controls)
{
control.IsHovered = (input.PointerPosition.X >= control.Position.X &&
input.PointerPosition.X <= control.Position.X + control.Size.X &&
input.PointerPosition.Y >= control.Position.Y &&
input.PointerPosition.Y <= control.Position.Y + control.Size.Y);
}
}

base.Update(gameTime);
}

Expand Down
1 change: 1 addition & 0 deletions OctoAwesome/OctoAwesome.Client/OctoAwesome.Client.csproj
Expand Up @@ -48,6 +48,7 @@
<Compile Include="Components\ChunkRenderer.cs" />
<Compile Include="Components\CameraComponent.cs" />
<Compile Include="Components\Hud\Brush.cs" />
<Compile Include="Components\Hud\ButtonControl.cs" />
<Compile Include="Components\Hud\Compass.cs" />
<Compile Include="Components\Hud\Control.cs" />
<Compile Include="Components\Hud\DebugInfos.cs" />
Expand Down
2 changes: 1 addition & 1 deletion OctoAwesome/OctoAwesome/IScreenInputSet.cs
Expand Up @@ -8,7 +8,7 @@ namespace OctoAwesome
{
public interface IScreenInputSet
{
Index2 PointerPosition { get; }
Index2 PointerPosition { get; set; }

event OnKeyChange OnKeyDown;

Expand Down

0 comments on commit fc8f882

Please sign in to comment.