1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 11:12:57 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuButton.cs

127 lines
3.7 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-02-07 12:52:19 +08:00
using System.Collections.Generic;
2017-02-07 12:52:19 +08:00
using OpenTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Extensions.Color4Extensions;
2017-02-07 12:52:19 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2017-02-07 12:52:19 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
2017-01-31 19:32:36 +08:00
using osu.Framework.Input;
2017-02-07 12:52:19 +08:00
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics.UserInterface
{
public class OsuButton : Button, IFilterable
2017-02-07 12:52:19 +08:00
{
private Box hover;
private SampleChannel sampleClick;
private SampleChannel sampleHover;
protected Triangles Triangles;
2017-02-07 12:52:19 +08:00
public OsuButton()
{
Height = 40;
}
protected override SpriteText CreateText() => new OsuSpriteText
{
Depth = -1,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
2017-01-31 18:58:45 +08:00
Font = @"Exo2.0-Bold",
};
public override bool HandleInput => Action != null;
2017-02-07 12:52:19 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours, AudioManager audio)
2017-02-07 12:52:19 +08:00
{
if (Action == null)
Colour = OsuColour.Gray(0.5f);
BackgroundColour = colours.BlueDark;
2017-01-31 19:32:36 +08:00
2017-02-07 12:52:19 +08:00
Content.Masking = true;
Content.CornerRadius = 5;
2017-07-11 21:58:06 +08:00
AddRange(new Drawable[]
{
Triangles = new Triangles
2017-01-31 19:32:36 +08:00
{
RelativeSizeAxes = Axes.Both,
ColourDark = colours.BlueDarker,
ColourLight = colours.Blue,
},
hover = new Box
{
RelativeSizeAxes = Axes.Both,
2017-09-07 21:46:21 +08:00
Blending = BlendingMode.Additive,
2017-01-31 19:32:36 +08:00
Colour = Color4.White.Opacity(0.1f),
Alpha = 0,
},
2017-02-07 12:52:19 +08:00
});
sampleClick = audio.Sample.Get(@"UI/generic-click");
sampleHover = audio.Sample.Get(@"UI/generic-hover");
Enabled.ValueChanged += enabled_ValueChanged;
Enabled.TriggerChange();
}
private void enabled_ValueChanged(bool enabled)
{
this.FadeColour(enabled ? Color4.White : Color4.Gray, 200, Easing.OutQuint);
}
protected override bool OnClick(InputState state)
{
sampleClick?.Play();
return base.OnClick(state);
2017-02-07 12:52:19 +08:00
}
2017-01-31 19:32:36 +08:00
protected override bool OnHover(InputState state)
{
sampleHover?.Play();
2017-01-31 19:32:36 +08:00
hover.FadeIn(200);
return base.OnHover(state);
2017-02-07 12:52:19 +08:00
}
2017-01-31 19:32:36 +08:00
protected override void OnHoverLost(InputState state)
{
hover.FadeOut(200);
base.OnHoverLost(state);
2017-02-07 12:52:19 +08:00
}
2017-01-31 19:32:36 +08:00
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
{
2017-07-23 02:50:25 +08:00
Content.ScaleTo(0.9f, 4000, Easing.OutQuint);
2017-01-31 19:32:36 +08:00
return base.OnMouseDown(state, args);
2017-02-07 12:52:19 +08:00
}
2017-01-31 19:32:36 +08:00
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
{
2017-07-23 02:50:25 +08:00
Content.ScaleTo(1, 1000, Easing.OutElastic);
2017-01-31 19:32:36 +08:00
return base.OnMouseUp(state, args);
2017-02-07 12:52:19 +08:00
}
public IEnumerable<string> FilterTerms => new[] { Text };
public bool MatchingFilter
{
set
{
this.FadeTo(value ? 1 : 0);
}
}
2017-02-07 12:52:19 +08:00
}
}