osu/osu.Game/Graphics/Cursor/MenuCursor.cs

192 lines
7.1 KiB
C#
Raw Normal View History

2017-03-16 13:41:07 +00:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using osu.Game.Configuration;
using System;
using osu.Framework.Graphics.Textures;
2017-04-02 16:19:59 +00:00
using osu.Framework.Threading;
using System.Linq;
2017-03-16 13:41:07 +00:00
namespace osu.Game.Graphics.Cursor
{
public class MenuCursor : CursorContainer
{
protected override Drawable CreateCursor() => new Cursor();
2017-04-02 16:19:59 +00:00
[BackgroundDependencyLoader]
private void load(OsuGameBase game)
{
this.game = game;
}
private bool dragging;
2017-04-02 16:19:59 +00:00
private ScheduledDelegate show;
private OsuGameBase game;
2017-04-13 22:18:17 +00:00
private IHasOverhangingTooltip overhang;
2017-04-02 16:19:59 +00:00
2017-03-18 11:47:49 +00:00
protected override bool OnMouseMove(InputState state)
{
2017-04-13 22:18:17 +00:00
Tooltip tooltip = ((Cursor)ActiveCursor).Tooltip;
if (overhang?.Overhanging ?? false)
tooltip.TooltipText = overhang.Tooltip;
else if (state.Mouse.Position != state.Mouse.LastPosition)
2017-04-02 16:19:59 +00:00
{
show?.Cancel();
tooltip.TooltipText = string.Empty;
IHasTooltip hasTooltip = null;
if (game.InternalChildren.OfType<IContainer>().Any(child => (hasTooltip = searchTooltip(child as IContainerEnumerable<Drawable>)) != null))
2017-04-02 16:19:59 +00:00
{
IHasDelayedTooltip delayedTooltip = hasTooltip as IHasDelayedTooltip;
2017-04-13 22:18:17 +00:00
overhang = hasTooltip as IHasOverhangingTooltip;
show = Scheduler.AddDelayed(delegate
{
tooltip.TooltipText = hasTooltip.Tooltip;
}, delayedTooltip?.Delay ?? 250);
}
2017-04-02 16:19:59 +00:00
}
2017-04-13 22:18:17 +00:00
else if(overhang != null)
{
overhang = null;
tooltip.TooltipText = string.Empty;
}
2017-04-02 16:19:59 +00:00
if (dragging)
2017-03-18 11:47:49 +00:00
{
2017-03-18 13:23:53 +00:00
Vector2 offset = state.Mouse.Position - state.Mouse.PositionMouseDown ?? state.Mouse.Delta;
2017-03-18 11:47:49 +00:00
float degrees = (float)MathHelper.RadiansToDegrees(Math.Atan2(-offset.X, offset.Y)) + 24.3f;
// Always rotate in the direction of least distance
float diff = (degrees - ActiveCursor.Rotation) % 360;
if (diff < -180) diff += 360;
if (diff > 180) diff -= 360;
degrees = ActiveCursor.Rotation + diff;
ActiveCursor.RotateTo(degrees, 600, EasingTypes.OutQuint);
}
return base.OnMouseMove(state);
}
private IHasTooltip searchTooltip(IContainerEnumerable<Drawable> children)
2017-04-02 16:19:59 +00:00
{
Drawable next = children.InternalChildren.OrderBy(drawable => drawable.Depth).FirstOrDefault(drawable => drawable.Hovering && !(drawable is CursorContainer));
2017-04-02 16:19:59 +00:00
IHasTooltip tooltipText = next as IHasTooltip;
if (tooltipText != null) return tooltipText;
if (next is IContainer)
return searchTooltip(next as IContainerEnumerable<Drawable>);
return null;
2017-04-02 16:19:59 +00:00
}
protected override bool OnDragStart(InputState state)
{
dragging = true;
return base.OnDragStart(state);
}
2017-03-16 13:41:07 +00:00
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
{
ActiveCursor.Scale = new Vector2(1);
ActiveCursor.ScaleTo(0.90f, 800, EasingTypes.OutQuint);
((Cursor)ActiveCursor).AdditiveLayer.Alpha = 0;
((Cursor)ActiveCursor).AdditiveLayer.FadeInFromZero(800, EasingTypes.OutQuint);
return base.OnMouseDown(state, args);
}
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
{
if (!state.Mouse.HasMainButtonPressed)
{
dragging = false;
2017-03-16 13:41:07 +00:00
((Cursor)ActiveCursor).AdditiveLayer.FadeOut(500, EasingTypes.OutQuint);
2017-03-18 13:37:50 +00:00
ActiveCursor.RotateTo(0, 600 * (1 + Math.Abs(ActiveCursor.Rotation / 720)), EasingTypes.OutElasticHalf);
2017-03-16 13:41:07 +00:00
ActiveCursor.ScaleTo(1, 500, EasingTypes.OutElastic);
}
return base.OnMouseUp(state, args);
}
protected override bool OnClick(InputState state)
{
((Cursor)ActiveCursor).AdditiveLayer.FadeOutFromOne(500, EasingTypes.OutQuint);
return base.OnClick(state);
}
2017-03-16 14:58:36 +00:00
protected override void PopIn()
{
ActiveCursor.FadeTo(1, 250, EasingTypes.OutQuint);
ActiveCursor.ScaleTo(1, 400, EasingTypes.OutQuint);
2017-03-16 14:58:36 +00:00
}
protected override void PopOut()
{
ActiveCursor.FadeTo(0, 900, EasingTypes.OutQuint);
2017-03-16 14:58:36 +00:00
ActiveCursor.ScaleTo(0, 500, EasingTypes.In);
}
2017-03-16 13:41:07 +00:00
public class Cursor : Container
{
private Container cursorContainer;
2017-04-02 16:19:59 +00:00
public Tooltip Tooltip;
2017-03-16 13:41:07 +00:00
private Bindable<double> cursorScale;
public Sprite AdditiveLayer;
public Cursor()
{
Size = new Vector2(42);
}
[BackgroundDependencyLoader]
2017-03-17 12:04:46 +00:00
private void load(OsuConfigManager config, TextureStore textures, OsuColour colour)
2017-03-16 13:41:07 +00:00
{
Children = new Drawable[]
{
cursorContainer = new Container
{
2017-03-17 12:04:46 +00:00
Size = new Vector2(32),
2017-03-16 13:41:07 +00:00
Children = new Drawable[]
{
new Sprite
{
FillMode = FillMode.Fit,
Texture = textures.Get(@"Cursor/menu-cursor"),
},
AdditiveLayer = new Sprite
{
FillMode = FillMode.Fit,
BlendingMode = BlendingMode.Additive,
2017-03-17 12:04:46 +00:00
Colour = colour.Pink,
2017-03-16 13:41:07 +00:00
Alpha = 0,
2017-03-17 12:04:46 +00:00
Texture = textures.Get(@"Cursor/menu-cursor-additive"),
2017-03-16 13:41:07 +00:00
},
}
2017-04-02 16:19:59 +00:00
},
Tooltip = new Tooltip
{
Alpha = 0,
},
2017-03-16 13:41:07 +00:00
};
cursorScale = config.GetBindable<double>(OsuConfig.MenuCursorSize);
cursorScale.ValueChanged += newScale => cursorContainer.Scale = new Vector2((float)newScale);
cursorScale.ValueChanged += newScale => Tooltip.Y = cursorContainer.Height * (float)newScale;
cursorScale.TriggerChange();
2017-03-16 13:41:07 +00:00
}
}
}
}