diff --git a/MBS.Framework/Application.cs b/MBS.Framework/Application.cs index f7ff8cf..3b3b3ef 100644 --- a/MBS.Framework/Application.cs +++ b/MBS.Framework/Application.cs @@ -63,6 +63,8 @@ namespace MBS.Framework /// public static Application Instance { get; set; } = null; + public EventFilter.EventFilterCollection EventFilters { get; } = new EventFilter.EventFilterCollection(); + protected virtual Command FindCommandInternal(string commandID) { return null; @@ -486,5 +488,30 @@ namespace MBS.Framework Stopping = false; } + private Dictionary _settings = new Dictionary(); + public T GetSetting(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(Guid id, T value) + { + _settings[id] = value; + } } } diff --git a/MBS.Framework/EventFilter.cs b/MBS.Framework/EventFilter.cs new file mode 100644 index 0000000..04d67b6 --- /dev/null +++ b/MBS.Framework/EventFilter.cs @@ -0,0 +1,109 @@ +// +// EventFilter.cs - provides a way to hook application-wide events +// +// Author: +// Michael Becker +// +// 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 . +using System; +namespace MBS.Framework +{ + public class EventFilter + { + public class EventFilterCollection + : System.Collections.ObjectModel.Collection + { + + } + + public EventFilterType EventType { get; private set; } = EventFilterType.None; + + protected EventFilter(EventFilterType eventType) + { + EventType = eventType; + } + + private EventFilterDelegate _processAction = null; + /// + /// Initializes a new instance of the class + /// with the given process function. + /// + /// + /// The function to call when the function is + /// called. + /// + public EventFilter(EventFilterDelegate processAction, EventFilterType eventType = EventFilterType.All) + { + _processAction = processAction; + EventType = eventType; + } + + /// + /// Calls the 's internal process function and + /// returns its value. + /// + /// + /// A value indicating whether the event was successfully processed. + /// + /// + /// The to pass to the underlying process function. + /// + public bool Process(ref EventArgs e, EventFilterType type) + { + if (_processAction != null) + return _processAction(ref e, type); + + return true; + } + } + /// + /// Provides a way to hook application-wide events. + /// + public class EventFilter : EventFilter where T : EventArgs + { + private EventFilterDelegate _processAction = null; + /// + /// Initializes a new instance of the class + /// with the given process function. + /// + /// + /// The function to call when the function is + /// called. + /// + public EventFilter(EventFilterDelegate processAction, EventFilterType eventType = EventFilterType.All) : base(eventType) + { + _processAction = processAction; + } + + /// + /// Calls the 's internal process function and + /// returns its value. + /// + /// + /// A value indicating whether the event was successfully processed. + /// + /// + /// The to pass to the underlying process function. + /// + public bool Process(ref T e, EventFilterType type) + { + if (_processAction != null) + return _processAction(ref e, type); + + return true; + } + } +} diff --git a/MBS.Framework/EventFilterDelegate.cs b/MBS.Framework/EventFilterDelegate.cs new file mode 100644 index 0000000..361ce94 --- /dev/null +++ b/MBS.Framework/EventFilterDelegate.cs @@ -0,0 +1,25 @@ +// +// EventFilterDelegate.cs +// +// Author: +// Michael Becker +// +// 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 . +using System; +namespace MBS.Framework +{ + public delegate bool EventFilterDelegate(ref T e, EventFilterType type) where T : EventArgs; +} diff --git a/MBS.Framework/EventFilterType.cs b/MBS.Framework/EventFilterType.cs new file mode 100644 index 0000000..18a050e --- /dev/null +++ b/MBS.Framework/EventFilterType.cs @@ -0,0 +1,39 @@ +// +// EventFilterType.cs +// +// Author: +// Michael Becker +// +// 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 . +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 + } +} diff --git a/MBS.Framework/MBS.Framework.csproj b/MBS.Framework/MBS.Framework.csproj index 458fb2b..9d6d295 100644 --- a/MBS.Framework/MBS.Framework.csproj +++ b/MBS.Framework/MBS.Framework.csproj @@ -124,6 +124,9 @@ + + +