1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 15:27:26 +08:00
osu-lazer/osu.Game/Overlays/Toolbar.cs

277 lines
8.5 KiB
C#
Raw Normal View History

2016-09-30 17:45:27 +08:00
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Drawables;
using OpenTK;
using OpenTK.Graphics;
2016-10-03 19:39:32 +08:00
using osu.Game.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using osu.Game.Configuration;
using System;
using System.Linq;
using osu.Game.GameModes.Play;
using osu.Framework.Extensions;
2016-09-30 17:45:27 +08:00
namespace osu.Game.Overlays
{
public class Toolbar : Container
{
const float height = 50;
2016-10-03 19:39:32 +08:00
private FlowContainer leftFlow;
private FlowContainer rightFlow;
private FlowContainer modeButtons;
public Action OnSettings;
public Action OnHome;
public Action<PlayMode> OnPlayModeChange;
2016-09-30 17:45:27 +08:00
public override void Load()
{
base.Load();
RelativeSizeAxes = Axes.X;
2016-09-30 17:45:27 +08:00
Size = new Vector2(1, height);
modeButtons = new FlowContainer
{
RelativeSizeAxes = Axes.Y,
Direction = FlowDirection.HorizontalOnly
};
foreach (PlayMode m in Enum.GetValues(typeof(PlayMode)))
{
var localMode = m;
modeButtons.Add(new ToolbarModeButton
{
Mode = m,
Action = delegate
{
SetGameMode(localMode);
OnPlayModeChange?.Invoke(localMode);
}
});
}
2016-09-30 17:45:27 +08:00
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
2016-09-30 17:45:27 +08:00
Colour = new Color4(0.1f, 0.1f, 0.1f, 0.9f)
2016-10-03 19:39:32 +08:00
},
leftFlow = new FlowContainer
{
Direction = FlowDirection.HorizontalOnly,
RelativeSizeAxes = Axes.Y,
Children = new []
{
new ToolbarButton
{
Icon = FontAwesome.gear,
Action = OnSettings
},
new ToolbarButton
{
Icon = FontAwesome.home,
Action = OnHome
},
modeButtons
2016-10-03 19:39:32 +08:00
}
},
rightFlow = new FlowContainer
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Direction = FlowDirection.HorizontalOnly,
RelativeSizeAxes = Axes.Y,
Size = new Vector2(0, 1),
Children = new []
{
new ToolbarButton
{
Icon = FontAwesome.search
},
new ToolbarButton
{
Icon = FontAwesome.user,
Text = ((OsuGame)Game).Config.Get<string>(OsuConfig.Username)
},
new ToolbarButton
{
Icon = FontAwesome.bars
},
2016-10-03 19:39:32 +08:00
}
2016-09-30 17:45:27 +08:00
}
};
}
2016-10-03 19:39:32 +08:00
public void SetGameMode(PlayMode mode)
{
foreach (var m in modeButtons.Children.Cast<ToolbarModeButton>())
{
m.Active = m.Mode == mode;
}
}
2016-10-03 19:39:32 +08:00
public class ToolbarModeButton : ToolbarButton
{
private PlayMode mode;
public PlayMode Mode
2016-10-03 19:39:32 +08:00
{
get { return mode; }
set
{
mode = value;
Text = mode.GetDescription();
Icon = getModeIcon(mode);
}
}
public bool Active
{
set
{
Background.Colour = value ? new Color4(100, 100, 100, 140) : new Color4(20, 20, 20, 140);
}
}
private FontAwesome getModeIcon(PlayMode mode)
{
switch (mode)
{
default: return FontAwesome.fa_osu_osu_o;
case PlayMode.Taiko: return FontAwesome.fa_osu_taiko_o;
case PlayMode.Catch: return FontAwesome.fa_osu_fruits_o;
case PlayMode.Mania: return FontAwesome.fa_osu_mania_o;
}
2016-10-03 19:39:32 +08:00
}
public override void Load()
{
base.Load();
DrawableIcon.TextSize = height * 0.7f;
2016-10-03 19:39:32 +08:00
}
}
public class ToolbarButton : FlowContainer
{
public FontAwesome Icon
{
get { return DrawableIcon.Icon; }
set { DrawableIcon.Icon = value; }
}
public string Text
{
get { return DrawableText.Text; }
set
{
DrawableText.Text = value;
paddingIcon.Alpha = string.IsNullOrEmpty(value) ? 0 : 1;
}
}
public Action Action;
protected TextAwesome DrawableIcon;
protected SpriteText DrawableText;
protected Box Background;
protected Box HoverBackground;
2016-10-03 19:39:32 +08:00
private Drawable paddingLeft;
private Drawable paddingRight;
private Drawable paddingIcon;
public new float Padding
{
get { return paddingLeft.Size.X; }
set
{
paddingLeft.Size = new Vector2(value, 1);
paddingRight.Size = new Vector2(value, 1);
}
}
public ToolbarButton()
2016-10-03 19:39:32 +08:00
{
Background = new Box
2016-10-03 19:39:32 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = new Color4(20, 20, 20, 140),
};
HoverBackground = new Box
{
RelativeSizeAxes = Axes.Both,
Additive = true,
Colour = new Color4(20, 20, 20, 0),
Alpha = 0,
};
DrawableIcon = new TextAwesome()
2016-10-03 19:39:32 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
};
DrawableText = new SpriteText
2016-10-03 19:39:32 +08:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
};
paddingLeft = new Container { RelativeSizeAxes = Axes.Y };
paddingRight = new Container { RelativeSizeAxes = Axes.Y };
paddingIcon = new Container
{
Size = new Vector2(5, 0),
Alpha = 0
};
2016-10-03 19:39:32 +08:00
Padding = 10;
}
protected override bool OnClick(InputState state)
{
Action?.Invoke();
return base.OnClick(state);
}
2016-10-03 19:39:32 +08:00
protected override bool OnHover(InputState state)
{
HoverBackground.FadeTo(0.4f, 200);
2016-10-03 19:39:32 +08:00
return base.OnHover(state);
}
protected override void OnHoverLost(InputState state)
{
HoverBackground.FadeTo(0, 200);
2016-10-03 19:39:32 +08:00
base.OnHoverLost(state);
}
public override void Load()
{
base.Load();
RelativeSizeAxes = Axes.Y;
Direction = FlowDirection.HorizontalOnly;
Children = new Drawable[]
{
Background,
HoverBackground,
2016-10-03 19:39:32 +08:00
paddingLeft,
DrawableIcon,
2016-10-03 19:39:32 +08:00
paddingIcon,
DrawableText,
2016-10-03 19:39:32 +08:00
paddingRight,
};
}
}
2016-09-30 17:45:27 +08:00
}
}