osu/osu.Game/Screens/Edit/Editor.cs

327 lines
12 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 09:19:50 +00:00
using System;
2018-11-20 07:51:59 +00:00
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Screens;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics;
using osu.Game.Screens.Edit.Components.Timelines.Summary;
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.UserInterface;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2018-06-19 11:19:52 +00:00
using osu.Framework.Platform;
2018-04-13 09:19:50 +00:00
using osu.Framework.Timing;
using osu.Game.Graphics.UserInterface;
using osu.Game.Screens.Edit.Components;
2018-11-06 09:28:22 +00:00
using osu.Game.Screens.Edit.Components.Menus;
2018-11-07 04:04:17 +00:00
using osu.Game.Screens.Edit.Design;
using osuTK.Input;
using System.Collections.Generic;
using osu.Framework;
2019-06-30 10:31:31 +00:00
using osu.Framework.Input.Bindings;
using osu.Game.Graphics.Cursor;
2019-06-30 10:31:31 +00:00
using osu.Game.Input.Bindings;
2019-10-09 07:04:58 +00:00
using osu.Game.Screens.Edit.Compose;
using osu.Game.Screens.Edit.Setup;
using osu.Game.Screens.Edit.Timing;
2019-12-12 04:04:32 +00:00
using osu.Game.Screens.Play;
using osu.Game.Users;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Screens.Edit
{
2019-12-12 04:04:32 +00:00
public class Editor : ScreenWithBeatmapBackground, IKeyBindingHandler<GlobalAction>
2018-04-13 09:19:50 +00:00
{
2019-11-06 09:11:56 +00:00
public override float BackgroundParallaxAmount => 0.1f;
2019-06-25 07:55:49 +00:00
public override bool AllowBackButton => false;
public override bool HideOverlaysOnEnter => true;
2019-02-01 06:42:15 +00:00
public override bool DisallowExternalBeatmapRulesetChanges => true;
2018-04-13 09:19:50 +00:00
private Box bottomBackground;
private Container screenContainer;
private EditorScreen currentScreen;
private readonly BindableBeatDivisor beatDivisor = new BindableBeatDivisor();
private EditorClock clock;
private DependencyContainer dependencies;
2018-06-19 11:19:52 +00:00
private GameHost host;
2018-04-13 09:19:50 +00:00
protected override UserActivity InitialActivity => new UserActivity.Editing(Beatmap.Value.BeatmapInfo);
2018-07-11 08:07:14 +00:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
=> dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
2018-04-13 09:19:50 +00:00
[BackgroundDependencyLoader]
2018-06-19 11:19:52 +00:00
private void load(OsuColour colours, GameHost host)
2018-04-13 09:19:50 +00:00
{
2018-06-19 11:19:52 +00:00
this.host = host;
2019-11-08 08:12:47 +00:00
beatDivisor.Value = Beatmap.Value.BeatmapInfo.BeatDivisor;
beatDivisor.BindValueChanged(divisor => Beatmap.Value.BeatmapInfo.BeatDivisor = divisor.NewValue);
// Todo: should probably be done at a DrawableRuleset level to share logic with Player.
2018-04-13 09:19:50 +00:00
var sourceClock = (IAdjustableClock)Beatmap.Value.Track ?? new StopwatchClock();
clock = new EditorClock(Beatmap.Value, beatDivisor) { IsCoupled = false };
2018-04-13 09:19:50 +00:00
clock.ChangeSource(sourceClock);
dependencies.CacheAs<IFrameBasedClock>(clock);
dependencies.CacheAs<IAdjustableClock>(clock);
dependencies.Cache(beatDivisor);
EditorMenuBar menuBar;
var fileMenuItems = new List<MenuItem>();
2019-04-01 03:16:05 +00:00
if (RuntimeInfo.IsDesktop)
{
fileMenuItems.Add(new EditorMenuItem("Export", MenuItemType.Standard, exportBeatmap));
fileMenuItems.Add(new EditorMenuItemSpacer());
}
2019-02-28 09:58:52 +00:00
fileMenuItems.Add(new EditorMenuItem("Exit", MenuItemType.Standard, this.Exit));
InternalChild = new OsuContextMenuContainer
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Both,
Children = new[]
2018-04-13 09:19:50 +00:00
{
new Container
2018-04-13 09:19:50 +00:00
{
Name = "Screen container",
2018-04-13 09:19:50 +00:00
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Top = 40, Bottom = 60 },
Child = screenContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true
}
},
new Container
2018-04-13 09:19:50 +00:00
{
Name = "Top bar",
RelativeSizeAxes = Axes.X,
Height = 40,
Child = menuBar = new EditorMenuBar
2018-04-13 09:19:50 +00:00
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
RelativeSizeAxes = Axes.Both,
Items = new[]
2018-04-13 09:19:50 +00:00
{
new MenuItem("File")
{
Items = fileMenuItems
}
2018-04-13 09:19:50 +00:00
}
}
},
new Container
2018-04-13 09:19:50 +00:00
{
Name = "Bottom bar",
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
Height = 60,
Children = new Drawable[]
2018-04-13 09:19:50 +00:00
{
bottomBackground = new Box { RelativeSizeAxes = Axes.Both },
new Container
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Vertical = 5, Horizontal = 10 },
Child = new GridContainer
2018-04-13 09:19:50 +00:00
{
RelativeSizeAxes = Axes.Both,
ColumnDimensions = new[]
2018-04-13 09:19:50 +00:00
{
new Dimension(GridSizeMode.Absolute, 220),
new Dimension(),
new Dimension(GridSizeMode.Absolute, 220)
},
Content = new[]
{
new Drawable[]
2018-04-13 09:19:50 +00:00
{
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = 10 },
Child = new TimeInfoContainer { RelativeSizeAxes = Axes.Both },
},
new SummaryTimeline
{
RelativeSizeAxes = Axes.Both,
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Left = 10 },
Child = new PlaybackControl { RelativeSizeAxes = Axes.Both },
}
2018-04-13 09:19:50 +00:00
},
}
},
}
2018-04-13 09:19:50 +00:00
}
},
}
2018-04-13 09:19:50 +00:00
};
menuBar.Mode.ValueChanged += onModeChanged;
bottomBackground.Colour = colours.Gray2;
}
protected override void Update()
{
base.Update();
clock.ProcessFrame();
}
protected override bool OnKeyDown(KeyDownEvent e)
2018-04-13 09:19:50 +00:00
{
switch (e.Key)
2018-04-13 09:19:50 +00:00
{
case Key.Left:
seek(e, -1);
return true;
2019-04-01 03:16:05 +00:00
case Key.Right:
seek(e, 1);
return true;
2018-04-13 09:19:50 +00:00
}
return base.OnKeyDown(e);
2018-04-13 09:19:50 +00:00
}
private double scrollAccumulation;
2018-10-02 03:02:47 +00:00
protected override bool OnScroll(ScrollEvent e)
2018-04-13 09:19:50 +00:00
{
scrollAccumulation += (e.ScrollDelta.X + e.ScrollDelta.Y) * (e.IsPrecise ? 0.1 : 1);
const int precision = 1;
while (Math.Abs(scrollAccumulation) > precision)
{
if (scrollAccumulation > 0)
seek(e, -1);
else
seek(e, 1);
scrollAccumulation = scrollAccumulation < 0 ? Math.Min(0, scrollAccumulation + precision) : Math.Max(0, scrollAccumulation - precision);
}
2018-04-13 09:19:50 +00:00
return true;
}
2019-06-30 10:31:31 +00:00
public bool OnPressed(GlobalAction action)
{
if (action == GlobalAction.Back)
{
// as we don't want to display the back button, manual handling of exit action is required.
2019-06-30 10:31:31 +00:00
this.Exit();
return true;
}
return false;
}
public bool OnReleased(GlobalAction action) => action == GlobalAction.Back;
2019-01-23 11:52:00 +00:00
public override void OnResuming(IScreen last)
2018-04-13 09:19:50 +00:00
{
base.OnResuming(last);
2019-07-10 15:22:40 +00:00
Beatmap.Value.Track?.Stop();
2018-04-13 09:19:50 +00:00
}
2019-01-23 11:52:00 +00:00
public override void OnEntering(IScreen last)
2018-04-13 09:19:50 +00:00
{
base.OnEntering(last);
2019-07-10 15:22:40 +00:00
2019-12-12 04:04:32 +00:00
// todo: temporary. we want to be applying dim using the UserDimContainer eventually.
2018-04-13 09:19:50 +00:00
Background.FadeColour(Color4.DarkGray, 500);
2019-12-12 04:04:32 +00:00
Background.EnableUserDim.Value = false;
Background.BlurAmount.Value = 0;
resetTrack(true);
2018-04-13 09:19:50 +00:00
}
2019-01-23 11:52:00 +00:00
public override bool OnExiting(IScreen next)
2018-04-13 09:19:50 +00:00
{
Background.FadeColour(Color4.White, 500);
2019-07-10 15:22:40 +00:00
resetTrack();
2019-07-10 08:43:02 +00:00
2018-04-13 09:19:50 +00:00
return base.OnExiting(next);
}
2019-07-10 15:22:40 +00:00
private void resetTrack(bool seekToStart = false)
2019-07-10 15:22:40 +00:00
{
Beatmap.Value.Track?.ResetSpeedAdjustments();
Beatmap.Value.Track?.Stop();
if (seekToStart)
{
double targetTime = 0;
if (Beatmap.Value.Beatmap.HitObjects.Count > 0)
{
// seek to one beat length before the first hitobject
targetTime = Beatmap.Value.Beatmap.HitObjects[0].StartTime;
targetTime -= Beatmap.Value.Beatmap.ControlPointInfo.TimingPointAt(targetTime).BeatLength;
}
clock.Seek(Math.Max(0, targetTime));
}
2019-07-10 15:22:40 +00:00
}
private void exportBeatmap() => host.OpenFileExternally(Beatmap.Value.Save());
2019-02-21 09:56:34 +00:00
private void onModeChanged(ValueChangedEvent<EditorScreenMode> e)
{
currentScreen?.Exit();
2019-02-21 09:56:34 +00:00
switch (e.NewValue)
{
case EditorScreenMode.SongSetup:
currentScreen = new SetupScreen();
break;
case EditorScreenMode.Compose:
currentScreen = new ComposeScreen();
break;
2019-04-01 03:16:05 +00:00
case EditorScreenMode.Design:
currentScreen = new DesignScreen();
break;
2019-04-01 03:16:05 +00:00
case EditorScreenMode.Timing:
currentScreen = new TimingScreen();
break;
}
LoadComponentAsync(currentScreen, screenContainer.Add);
}
private void seek(UIEvent e, int direction)
{
double amount = e.ShiftPressed ? 2 : 1;
if (direction < 1)
clock.SeekBackward(!clock.IsRunning, amount);
else
clock.SeekForward(!clock.IsRunning, amount);
}
2018-04-13 09:19:50 +00:00
}
}