1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 20:47:51 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/DialogButton.cs

285 lines
9.8 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-06 03:16:40 +08:00
using OpenTK;
2017-01-27 17:24:49 +08:00
using OpenTK.Graphics;
2017-01-28 02:18:57 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Shapes;
2017-01-28 02:18:57 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Sprites;
using osu.Framework.Extensions.Color4Extensions;
using osu.Game.Graphics.Containers;
using osu.Framework.Configuration;
2017-12-18 15:38:15 +08:00
using osu.Framework.Input;
2017-01-27 17:24:49 +08:00
2017-02-27 11:35:13 +08:00
namespace osu.Game.Graphics.UserInterface
2017-01-28 02:18:57 +08:00
{
public class DialogButton : OsuClickableContainer
2017-01-27 17:24:49 +08:00
{
2017-02-07 15:15:45 +08:00
private const float hover_width = 0.9f;
private const float hover_duration = 500;
private const float glow_fade_duration = 250;
private const float click_duration = 200;
2017-01-27 17:24:49 +08:00
public readonly BindableBool Selected = new BindableBool();
2017-02-27 11:35:13 +08:00
private readonly Container backgroundContainer;
private readonly Container colourContainer;
private readonly Container glowContainer;
private readonly Box leftGlow;
private readonly Box centerGlow;
private readonly Box rightGlow;
private readonly Box background;
private readonly SpriteText spriteText;
2017-02-28 09:30:10 +08:00
private Vector2 hoverSpacing => new Vector2(3f, 0f);
2017-01-28 02:18:57 +08:00
2017-02-27 11:35:13 +08:00
public DialogButton()
2017-01-27 17:39:15 +08:00
{
2017-02-27 11:35:13 +08:00
RelativeSizeAxes = Axes.X;
Children = new Drawable[]
2017-01-28 02:18:57 +08:00
{
backgroundContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Width = 1f,
Children = new Drawable[]
{
2017-02-27 11:35:13 +08:00
background = new Box
2017-01-28 02:18:57 +08:00
{
RelativeSizeAxes = Axes.Both,
2017-02-27 11:35:13 +08:00
Colour = backgroundColour,
},
},
2017-01-28 02:18:57 +08:00
},
glowContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Width = 1f,
Alpha = 0f,
Children = new Drawable[]
{
2017-01-30 20:19:44 +08:00
leftGlow = new Box
2017-01-28 02:18:57 +08:00
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.TopLeft,
Anchor = Anchor.TopLeft,
2017-02-27 11:35:13 +08:00
Width = 0.125f,
},
2017-01-30 20:19:44 +08:00
centerGlow = new Box
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
2017-02-27 11:35:13 +08:00
Width = 0.75f,
2017-01-28 02:18:57 +08:00
},
2017-01-30 20:19:44 +08:00
rightGlow = new Box
2017-01-28 02:18:57 +08:00
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.TopRight,
Anchor = Anchor.TopRight,
2017-02-27 11:35:13 +08:00
Width = 0.125f,
},
},
2017-01-28 02:18:57 +08:00
},
new Container
{
2017-01-30 16:43:06 +08:00
RelativeSizeAxes = Axes.Both,
2017-01-28 02:18:57 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Masking = true,
Children = new Drawable[]
{
colourContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Width = 0.8f,
2017-01-28 02:18:57 +08:00
Masking = true,
MaskingSmoothness = 2,
2017-06-12 11:48:47 +08:00
EdgeEffect = new EdgeEffectParameters
2017-01-28 02:18:57 +08:00
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.2f),
2017-02-27 11:35:13 +08:00
Radius = 5,
2017-01-28 02:18:57 +08:00
},
2017-02-28 09:38:25 +08:00
Colour = ButtonColour,
Shear = new Vector2(0.2f, 0),
2017-01-28 02:18:57 +08:00
Children = new Drawable[]
{
new Box
{
EdgeSmoothness = new Vector2(2, 0),
2017-02-27 11:35:13 +08:00
RelativeSizeAxes = Axes.Both,
2017-01-28 02:18:57 +08:00
},
new Container
2017-01-28 02:18:57 +08:00
{
RelativeSizeAxes = Axes.Both,
Masking = true,
MaskingSmoothness = 0,
Children = new[]
{
new Triangles
{
RelativeSizeAxes = Axes.Both,
TriangleScale = 4,
ColourDark = OsuColour.Gray(0.88f),
2017-02-27 11:35:13 +08:00
Shear = new Vector2(-0.2f, 0),
},
},
},
2017-02-27 11:35:13 +08:00
},
},
},
2017-01-28 02:18:57 +08:00
},
spriteText = new OsuSpriteText
2017-01-28 02:18:57 +08:00
{
Text = Text,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
TextSize = 28,
2017-01-28 02:18:57 +08:00
Font = "Exo2.0-Bold",
Shadow = true,
ShadowColour = new Color4(0, 0, 0, 0.1f),
2017-02-27 11:35:13 +08:00
Colour = Color4.White,
},
};
2017-01-30 20:19:44 +08:00
2017-01-31 21:17:47 +08:00
updateGlow();
Selected.ValueChanged += selectionChanged;
}
private Color4 buttonColour;
public Color4 ButtonColour
{
get
{
return buttonColour;
}
set
{
buttonColour = value;
updateGlow();
colourContainer.Colour = value;
}
}
private Color4 backgroundColour = OsuColour.Gray(34);
public Color4 BackgroundColour
{
get
{
return backgroundColour;
}
set
{
backgroundColour = value;
background.Colour = value;
}
}
private string text;
public string Text
{
get
{
return text;
}
set
{
text = value;
spriteText.Text = Text;
}
}
private float textSize = 28;
public float TextSize
{
get
{
return textSize;
}
set
{
textSize = value;
spriteText.TextSize = value;
}
}
public override bool ReceiveMouseInputAt(Vector2 screenSpacePos) => backgroundContainer.ReceiveMouseInputAt(screenSpacePos);
2017-12-18 15:38:15 +08:00
protected override bool OnClick(InputState state)
{
colourContainer.ResizeTo(new Vector2(1.5f, 1f), click_duration, Easing.In);
flash();
this.Delay(click_duration).Schedule(delegate
{
colourContainer.ResizeTo(new Vector2(0.8f, 1f));
spriteText.Spacing = Vector2.Zero;
glowContainer.FadeOut();
});
return base.OnClick(state);
}
2017-12-18 15:38:15 +08:00
protected override bool OnHover(InputState state)
{
base.OnHover(state);
Selected.Value = true;
return true;
}
2017-12-18 15:38:15 +08:00
protected override void OnHoverLost(InputState state)
{
base.OnHoverLost(state);
Selected.Value = false;
}
private void selectionChanged(bool isSelected)
{
if (isSelected)
{
spriteText.TransformSpacingTo(hoverSpacing, hover_duration, Easing.OutElastic);
colourContainer.ResizeTo(new Vector2(hover_width, 1f), hover_duration, Easing.OutElastic);
glowContainer.FadeIn(glow_fade_duration, Easing.Out);
}
2017-12-18 15:36:11 +08:00
else
{
colourContainer.ResizeTo(new Vector2(0.8f, 1f), hover_duration, Easing.OutElastic);
spriteText.TransformSpacingTo(Vector2.Zero, hover_duration, Easing.OutElastic);
glowContainer.FadeOut(glow_fade_duration, Easing.Out);
}
}
private void flash()
{
var flash = new Box
{
RelativeSizeAxes = Axes.Both
};
colourContainer.Add(flash);
flash.Colour = ButtonColour;
flash.Blending = BlendingMode.Additive;
flash.Alpha = 0.3f;
flash.FadeOutFromOne(click_duration);
flash.Expire();
}
private void updateGlow()
{
leftGlow.Colour = ColourInfo.GradientHorizontal(new Color4(ButtonColour.R, ButtonColour.G, ButtonColour.B, 0f), ButtonColour);
centerGlow.Colour = ButtonColour;
rightGlow.Colour = ColourInfo.GradientHorizontal(ButtonColour, new Color4(ButtonColour.R, ButtonColour.G, ButtonColour.B, 0f));
2017-01-28 02:18:57 +08:00
}
}
}