Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate HandleInput #1242

Merged
merged 10 commits into from Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions osu.Framework.Tests/Visual/TestCaseDrawablePath.cs
Expand Up @@ -105,8 +105,6 @@ public TestCaseDrawablePath() : base(2, 2)

private class UserDrawnPath : Path
{
public override bool HandleInput => true;

private Vector2 oldPos;

protected override bool OnDragStart(InputState state)
Expand Down
2 changes: 0 additions & 2 deletions osu.Framework.Tests/Visual/TestCaseInputResampler.cs
Expand Up @@ -183,8 +183,6 @@ public ArcPath(bool raw, bool keepFraction, InputResampler inputResampler, Textu

private class UserDrawnPath : SmoothedPath
{
public override bool HandleInput => true;

public SpriteText DrawText;

protected virtual void AddUserVertex(Vector2 v) => AddRawVertex(v);
Expand Down
1 change: 0 additions & 1 deletion osu.Framework.Tests/Visual/TestCaseTriangles.cs
Expand Up @@ -172,7 +172,6 @@ private void addCornerMarkers(Container box, int size = 50, Color4? colour = nul
internal class DraggableTriangle : Triangle
{
public bool AllowDrag = true;
public override bool HandleInput => true;

protected override bool OnDrag(InputState state)
{
Expand Down
57 changes: 57 additions & 0 deletions osu.Framework/Caching/HandleInputCache.cs
@@ -0,0 +1,57 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE

using System;
using System.Collections.Concurrent;
using System.Reflection;
using osu.Framework.Graphics;

namespace osu.Framework.Caching
{
public class HandleInputCache

This comment was marked as off-topic.

This comment was marked as off-topic.

{
private readonly ConcurrentDictionary<Type, bool> cachedValues = new ConcurrentDictionary<Type, bool>();

private readonly string[] inputMethods = {
"OnHover",

This comment was marked as off-topic.

"OnHoverLost",
"OnMouseDown",
"OnMouseUp",
"OnClick",
"OnDoubleClick",
"OnDragStart",
"OnDrag",
"OnDragEnd",
"OnWheel",
"OnFocus",
"OnFocusLost",
"OnKeyDown",
"OnKeyUp",
"OnMouseMove"
};

public bool Get(Type type)
{
if (!type.IsSubclassOf(typeof(Drawable)))
throw new ArgumentException();

var cached = cachedValues.TryGetValue(type, out var value);

if (!cached)
{
foreach (var inputMethod in inputMethods)
{
var isOverridden = type.GetMethod(inputMethod, BindingFlags.Instance | BindingFlags.NonPublic).DeclaringType != typeof(Drawable);
if (isOverridden)
{
cachedValues.TryAdd(type, true);
return true;
}
}
cachedValues.TryAdd(type, value = false);
}

return value;
}
}
}
2 changes: 0 additions & 2 deletions osu.Framework/Graphics/Containers/TextFlowContainer.cs
Expand Up @@ -118,8 +118,6 @@ public string Text
}
}

public override bool HandleInput => false;

public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true)
{
if ((invalidation & Invalidation.DrawSize) > 0)
Expand Down
10 changes: 9 additions & 1 deletion osu.Framework/Graphics/Drawable.cs
Expand Up @@ -206,6 +206,12 @@ internal void Load(IFrameBasedClock clock, IReadOnlyDependencyContainer dependen
}
}

[BackgroundDependencyLoader]
private void load(HandleInputCache handleInputCache)
{
this.handleInputCache = handleInputCache;
}

/// <summary>
/// Runs once on the update thread after loading has finished.
/// </summary>
Expand Down Expand Up @@ -1830,10 +1836,12 @@ protected virtual void OnFocusLost(InputState state)
/// is propagated up the scene graph to the next eligible Drawable.</returns>
protected virtual bool OnMouseMove(InputState state) => false;

private HandleInputCache handleInputCache;

/// <summary>
/// This drawable only receives input events if HandleInput is true.
/// </summary>
public virtual bool HandleInput => false;
public virtual bool HandleInput => handleInputCache.Get(GetType());

/// <summary>
/// Check whether we have active focus.
Expand Down
2 changes: 0 additions & 2 deletions osu.Framework/Graphics/Performance/FrameStatisticsDisplay.cs
Expand Up @@ -482,8 +482,6 @@ public TimeBar(TextureAtlas atlas)

Sprite.Texture = atlas.Add(WIDTH, HEIGHT);
}

public override bool HandleInput => false;
}

private class CounterBar : Container
Expand Down
4 changes: 1 addition & 3 deletions osu.Framework/Graphics/Sprites/SpriteText.cs
Expand Up @@ -22,7 +22,7 @@ namespace osu.Framework.Graphics.Sprites
/// </summary>
public class SpriteText : FillFlowContainer, IHasCurrentValue<string>, IHasLineBaseHeight, IHasText, IHasFilterTerms
{
public IEnumerable<string> FilterTerms => new [] { Text };
public IEnumerable<string> FilterTerms => new[] { Text };

private static readonly char[] default_fixed_width_exceptions = { '.', ':', ',' };

Expand Down Expand Up @@ -121,8 +121,6 @@ public float LineBaseHeight

private FontStore store;

public override bool HandleInput => false;

/// <summary>
/// Creates a new sprite text. <see cref="Container{T}.AutoSizeAxes"/> is set to <see cref="Axes.Both"/> by default.
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions osu.Framework/Graphics/Visualisation/LogOverlay.cs
Expand Up @@ -145,8 +145,6 @@ internal class DrawableLogEntry : Container

private const float font_size = 14;

public override bool HandleInput => false;

public DrawableLogEntry(LogEntry entry)
{
RelativeSizeAxes = Axes.X;
Expand Down
14 changes: 14 additions & 0 deletions osu.Framework/Platform/GameHost.cs
Expand Up @@ -14,6 +14,7 @@
using OpenTK.Graphics.ES30;
using OpenTK.Input;
using osu.Framework.Allocation;
using osu.Framework.Caching;
using osu.Framework.Configuration;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
Expand Down Expand Up @@ -157,6 +158,7 @@ protected GameHost(string gameName = @"")

Dependencies.Cache(this);
Dependencies.Cache(Storage = GetStorage(gameName));
prepareHandleInputCache();

Name = gameName;
Logger.GameIdentifier = gameName;
Expand All @@ -180,6 +182,18 @@ protected GameHost(string gameName = @"")
Environment.CurrentDirectory = path;
}

private void prepareHandleInputCache()
{
var handleInputCache = new HandleInputCache();

foreach (var type in AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Where(t => t.IsSubclassOf(typeof(Drawable))))
{
handleInputCache.Get(type);
}

Dependencies.Cache(handleInputCache);
}

private void exceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
var exception = (Exception)e.ExceptionObject;
Expand Down
1 change: 1 addition & 0 deletions osu.Framework/osu.Framework.csproj
Expand Up @@ -192,6 +192,7 @@
<Compile Include="Graphics\Cursor\CursorEffectContainer.cs" />
<Compile Include="Graphics\Cursor\IHasCustomTooltip.cs" />
<Compile Include="Graphics\Cursor\ITooltip.cs" />
<Compile Include="Caching\HandleInputCache.cs" />
<Compile Include="Graphics\Primitives\RectangleI.cs" />
<Compile Include="Graphics\Primitives\Vector2I.cs" />
<Compile Include="Graphics\Shapes\Circle.cs" />
Expand Down