add scaffolding for EventFilters (to be implemented in Engine-specific code)

This commit is contained in:
Michael Becker 2022-01-15 07:59:07 -05:00
parent d58213bf78
commit 75c2dcca65
No known key found for this signature in database
GPG Key ID: 98C333A81F18C22C
5 changed files with 203 additions and 0 deletions

View File

@ -63,6 +63,8 @@ namespace MBS.Framework
/// </value> /// </value>
public static Application Instance { get; set; } = null; public static Application Instance { get; set; } = null;
public EventFilter.EventFilterCollection EventFilters { get; } = new EventFilter.EventFilterCollection();
protected virtual Command FindCommandInternal(string commandID) protected virtual Command FindCommandInternal(string commandID)
{ {
return null; return null;
@ -486,5 +488,30 @@ namespace MBS.Framework
Stopping = false; Stopping = false;
} }
private Dictionary<Guid, object> _settings = new Dictionary<Guid, object>();
public T GetSetting<T>(Guid id, T defaultValue = default(T))
{
object value = GetSetting(id, (object)defaultValue);
if (value is T)
{
return (T)value;
}
else if (value is string && ((string)value).TryParse(typeof(T), out object val))
{
return (T)val;
}
return defaultValue;
}
public object GetSetting(Guid id, object defaultValue = null)
{
if (_settings.ContainsKey(id))
return _settings[id];
return defaultValue;
}
public void SetSetting<T>(Guid id, T value)
{
_settings[id] = value;
}
} }
} }

View File

@ -0,0 +1,109 @@
//
// EventFilter.cs - provides a way to hook application-wide events
//
// Author:
// Michael Becker <alcexhim@gmail.com>
//
// Copyright (c) 2021 Mike Becker's Software
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace MBS.Framework
{
public class EventFilter
{
public class EventFilterCollection
: System.Collections.ObjectModel.Collection<EventFilter>
{
}
public EventFilterType EventType { get; private set; } = EventFilterType.None;
protected EventFilter(EventFilterType eventType)
{
EventType = eventType;
}
private EventFilterDelegate<EventArgs> _processAction = null;
/// <summary>
/// Initializes a new instance of the <see cref="EventFilter" /> class
/// with the given process function.
/// </summary>
/// <param name="processAction">
/// The function to call when the <see cref="Process" /> function is
/// called.
/// </param>
public EventFilter(EventFilterDelegate<EventArgs> processAction, EventFilterType eventType = EventFilterType.All)
{
_processAction = processAction;
EventType = eventType;
}
/// <summary>
/// Calls the <see cref="EventFilter" />'s internal process function and
/// returns its value.
/// </summary>
/// <returns>
/// A value indicating whether the event was successfully processed.
/// </returns>
/// <param name="e">
/// The <see cref="EventArgs" /> to pass to the underlying process function.
/// </param>
public bool Process(ref EventArgs e, EventFilterType type)
{
if (_processAction != null)
return _processAction(ref e, type);
return true;
}
}
/// <summary>
/// Provides a way to hook application-wide events.
/// </summary>
public class EventFilter<T> : EventFilter where T : EventArgs
{
private EventFilterDelegate<T> _processAction = null;
/// <summary>
/// Initializes a new instance of the <see cref="EventFilter" /> class
/// with the given process function.
/// </summary>
/// <param name="processAction">
/// The function to call when the <see cref="Process" /> function is
/// called.
/// </param>
public EventFilter(EventFilterDelegate<T> processAction, EventFilterType eventType = EventFilterType.All) : base(eventType)
{
_processAction = processAction;
}
/// <summary>
/// Calls the <see cref="EventFilter" />'s internal process function and
/// returns its value.
/// </summary>
/// <returns>
/// A value indicating whether the event was successfully processed.
/// </returns>
/// <param name="e">
/// The <see cref="EventArgs" /> to pass to the underlying process function.
/// </param>
public bool Process(ref T e, EventFilterType type)
{
if (_processAction != null)
return _processAction(ref e, type);
return true;
}
}
}

View File

@ -0,0 +1,25 @@
//
// EventFilterDelegate.cs
//
// Author:
// Michael Becker <alcexhim@gmail.com>
//
// Copyright (c) 2021 Mike Becker's Software
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace MBS.Framework
{
public delegate bool EventFilterDelegate<T>(ref T e, EventFilterType type) where T : EventArgs;
}

View File

@ -0,0 +1,39 @@
//
// EventFilterType.cs
//
// Author:
// Michael Becker <alcexhim@gmail.com>
//
// Copyright (c) 2021 Mike Becker's Software
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace MBS.Framework
{
[Flags()]
public enum EventFilterType
{
None = 0,
KeyDown,
KeyUp,
MouseDown,
MouseMove,
MouseUp,
MouseWheel,
GotFocus,
LostFocus,
All = KeyDown | KeyUp | MouseDown | MouseMove | MouseUp | MouseWheel
| GotFocus | LostFocus
}
}

View File

@ -124,6 +124,9 @@
<Compile Include="Collections\Generic\HandleDictionary.cs" /> <Compile Include="Collections\Generic\HandleDictionary.cs" />
<Compile Include="Collections\ExtensionMethods.cs" /> <Compile Include="Collections\ExtensionMethods.cs" />
<Compile Include="Collections\Generic\AppendOnlyLinkedList.cs" /> <Compile Include="Collections\Generic\AppendOnlyLinkedList.cs" />
<Compile Include="EventFilter.cs" />
<Compile Include="EventFilterType.cs" />
<Compile Include="EventFilterDelegate.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Logic\" /> <Folder Include="Logic\" />