From 299c4923c3781040d3b65cfe472d1f0e7ede7333 Mon Sep 17 00:00:00 2001 From: default0 Date: Sat, 4 Feb 2017 22:03:39 +0100 Subject: [PATCH] Implement Beatmap Import via drag&drop --- osu-framework | 2 +- osu.Desktop/OsuGameDesktop.cs | 55 ++++++++++++++++++++++++++++++++++ osu.Desktop/Program.cs | 2 +- osu.Desktop/osu.Desktop.csproj | 3 ++ osu.Game/OsuGame.cs | 8 ++++- 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 osu.Desktop/OsuGameDesktop.cs diff --git a/osu-framework b/osu-framework index f9627494e4..a642501e72 160000 --- a/osu-framework +++ b/osu-framework @@ -1 +1 @@ -Subproject commit f9627494e444d8b35eb20d418539ada15f258c4d +Subproject commit a642501e72bb0c062835e75151eac45df1ad481d diff --git a/osu.Desktop/OsuGameDesktop.cs b/osu.Desktop/OsuGameDesktop.cs new file mode 100644 index 0000000000..e5abb763a7 --- /dev/null +++ b/osu.Desktop/OsuGameDesktop.cs @@ -0,0 +1,55 @@ +using osu.Game; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using osu.Framework.Platform; +using osu.Framework.Desktop.Platform; +using osu.Game.Database; + +namespace osu.Desktop +{ + class OsuGameDesktop : OsuGame + { + public OsuGameDesktop(string[] args = null) + : base(args) + { + + } + + public override void SetHost(BasicGameHost host) + { + base.SetHost(host); + var desktopWindow = host.Window as DesktopGameWindow; + if (desktopWindow != null) + { + desktopWindow.DragEnter += dragEnter; + desktopWindow.DragDrop += dragDrop; + } + } + + private void dragDrop(DragEventArgs e) + { + // this method will only be executed if e.Effect in dragEnter gets set to something other that None. + var dropData = e.Data.GetData(DataFormats.FileDrop) as object[]; + var filePaths = dropData.Select(f => f.ToString()).ToArray(); + ImportBeatmaps(filePaths); + } + + private void dragEnter(DragEventArgs e) + { + // dragDrop will only be executed if e.Effect gets set to something other that None in this method. + bool isFile = e.Data.GetDataPresent(DataFormats.FileDrop); + if (isFile) + { + var paths = (e.Data.GetData(DataFormats.FileDrop) as object[]).Select(f => f.ToString()).ToArray(); + if (paths.Any(p => !p.EndsWith(".osz"))) + e.Effect = DragDropEffects.None; + else + e.Effect = DragDropEffects.Copy; + } + } + } +} diff --git a/osu.Desktop/Program.cs b/osu.Desktop/Program.cs index ef2a4b8ba1..62100f9b36 100644 --- a/osu.Desktop/Program.cs +++ b/osu.Desktop/Program.cs @@ -51,7 +51,7 @@ namespace osu.Desktop Ruleset.Register(new ManiaRuleset()); Ruleset.Register(new CatchRuleset()); - BaseGame osu = new OsuGame(args); + BaseGame osu = new OsuGameDesktop(args); host.Add(osu); host.Run(); } diff --git a/osu.Desktop/osu.Desktop.csproj b/osu.Desktop/osu.Desktop.csproj index ce0e39b6d9..9df4148ac0 100644 --- a/osu.Desktop/osu.Desktop.csproj +++ b/osu.Desktop/osu.Desktop.csproj @@ -81,7 +81,9 @@ + + @@ -152,6 +154,7 @@ + diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index ede86db1db..7bbdc1e952 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -22,6 +22,7 @@ using osu.Game.Overlays.Toolbar; using osu.Game.Screens; using osu.Game.Screens.Menu; using OpenTK; +using System.Linq; namespace osu.Game { @@ -63,13 +64,18 @@ namespace osu.Game } if (args?.Length > 0) - Schedule(delegate { Dependencies.Get().Import(args); }); + ImportBeatmaps(args); Dependencies.Cache(this); PlayMode = LocalConfig.GetBindable(OsuConfig.PlayMode); } + public void ImportBeatmaps(params string[] paths) + { + Schedule(delegate { Dependencies.Get().Import(paths); }); + } + protected override void LoadComplete() { base.LoadComplete();