Add mouse info to events

This commit is contained in:
Frank A. Krueger 2017-07-07 22:55:04 -07:00
parent 0d1d5c6490
commit 906bb498c6
6 changed files with 43 additions and 31 deletions

View File

@ -25,67 +25,67 @@ namespace Ooui
set => SetProperty (ref hidden, value, "hidden");
}
public event EventHandler Clicked {
public event TargetEventHandler Clicked {
add => AddEventListener ("click", value);
remove => RemoveEventListener ("click", value);
}
public event EventHandler DoubleClicked {
public event TargetEventHandler DoubleClicked {
add => AddEventListener ("dblclick", value);
remove => RemoveEventListener ("dblclick", value);
}
public event EventHandler KeyDown {
public event TargetEventHandler KeyDown {
add => AddEventListener ("keydown", value);
remove => RemoveEventListener ("keydown", value);
}
public event EventHandler KeyPressed {
public event TargetEventHandler KeyPressed {
add => AddEventListener ("keypress", value);
remove => RemoveEventListener ("keypress", value);
}
public event EventHandler KeyUp {
public event TargetEventHandler KeyUp {
add => AddEventListener ("keyup", value);
remove => RemoveEventListener ("keyup", value);
}
public event EventHandler MouseDown {
public event TargetEventHandler MouseDown {
add => AddEventListener ("mousedown", value);
remove => RemoveEventListener ("mousedown", value);
}
public event EventHandler MouseEntered {
public event TargetEventHandler MouseEntered {
add => AddEventListener ("mouseenter", value);
remove => RemoveEventListener ("mouseenter", value);
}
public event EventHandler MouseLeft {
public event TargetEventHandler MouseLeft {
add => AddEventListener ("mouseleave", value);
remove => RemoveEventListener ("mouseleave", value);
}
public event EventHandler MouseMoved {
public event TargetEventHandler MouseMoved {
add => AddEventListener ("mousemove", value);
remove => RemoveEventListener ("mousemove", value);
}
public event EventHandler MouseOut {
public event TargetEventHandler MouseOut {
add => AddEventListener ("mouseout", value);
remove => RemoveEventListener ("mouseout", value);
}
public event EventHandler MouseOver {
public event TargetEventHandler MouseOver {
add => AddEventListener ("mouseover", value);
remove => RemoveEventListener ("mouseover", value);
}
public event EventHandler MouseUp {
public event TargetEventHandler MouseUp {
add => AddEventListener ("mouseup", value);
remove => RemoveEventListener ("mouseup", value);
}
public event EventHandler Wheeled {
public event TargetEventHandler Wheeled {
add => AddEventListener ("wheel", value);
remove => RemoveEventListener ("wheel", value);
}

View File

@ -9,8 +9,8 @@ namespace Ooui
{
readonly List<Message> stateMessages = new List<Message> ();
readonly Dictionary<string, List<EventHandler>> eventListeners =
new Dictionary<string, List<EventHandler>> ();
readonly Dictionary<string, List<TargetEventHandler>> eventListeners =
new Dictionary<string, List<TargetEventHandler>> ();
public string Id { get; private set; } = GenerateId ();
@ -45,17 +45,17 @@ namespace Ooui
return null;
}
public void AddEventListener (string eventType, EventHandler handler)
public void AddEventListener (string eventType, TargetEventHandler handler)
{
if (eventType == null) return;
if (handler == null) return;
var sendListen = false;
List<EventHandler> handlers;
List<TargetEventHandler> handlers;
lock (eventListeners) {
if (!eventListeners.TryGetValue (eventType, out handlers)) {
handlers = new List<EventHandler> ();
handlers = new List<TargetEventHandler> ();
eventListeners[eventType] = handlers;
sendListen = true;
}
@ -70,12 +70,12 @@ namespace Ooui
});
}
public void RemoveEventListener (string eventType, EventHandler handler)
public void RemoveEventListener (string eventType, TargetEventHandler handler)
{
if (eventType == null) return;
if (handler == null) return;
List<EventHandler> handlers;
List<TargetEventHandler> handlers;
lock (eventListeners) {
if (eventListeners.TryGetValue (eventType, out handlers)) {
handlers.Remove (handler);
@ -176,15 +176,19 @@ namespace Ooui
if (message.TargetId != Id)
return false;
List<EventHandler> handlers = null;
List<TargetEventHandler> handlers = null;
lock (eventListeners) {
List<EventHandler> hs;
List<TargetEventHandler> hs;
if (eventListeners.TryGetValue (message.Key, out hs)) {
handlers = new List<EventHandler> (hs);
handlers = new List<TargetEventHandler> (hs);
}
}
if (handlers != null) {
var args = EventArgs.Empty;
var args = new TargetEventArgs ();
if (message.Value is Newtonsoft.Json.Linq.JObject o) {
args.OffsetX = (double)o["offsetX"];
args.OffsetY = (double)o["offsetY"];
}
foreach (var h in handlers) {
h.Invoke (this, args);
}
@ -212,4 +216,12 @@ namespace Ooui
return typeof (EventTarget).IsAssignableFrom (objectType);
}
}
public delegate void TargetEventHandler (object sender, TargetEventArgs e);
public class TargetEventArgs : EventArgs
{
public double OffsetX { get; set; }
public double OffsetY { get; set; }
}
}

View File

@ -10,12 +10,12 @@ namespace Ooui
set => SetProperty (ref action, value ?? "", "action");
}
public event EventHandler Submitted {
public event TargetEventHandler Submitted {
add => AddEventListener ("submit", value);
remove => RemoveEventListener ("submit", value);
}
public event EventHandler Reset {
public event TargetEventHandler Reset {
add => AddEventListener ("reset", value);
remove => RemoveEventListener ("reset", value);
}

View File

@ -30,12 +30,12 @@ namespace Ooui
}
}
public event EventHandler Changed {
public event TargetEventHandler Changed {
add => AddEventListener ("change", value);
remove => RemoveEventListener ("change", value);
}
public event EventHandler Inputted {
public event TargetEventHandler Inputted {
add => AddEventListener ("input", value);
remove => RemoveEventListener ("input", value);
}

View File

@ -10,7 +10,7 @@ namespace Ooui
set => SetProperty (ref val, value ?? "", "value");
}
public event EventHandler Changed {
public event TargetEventHandler Changed {
add => AddEventListener ("change", value);
remove => RemoveEventListener ("change", value);
}

View File

@ -4,12 +4,12 @@ namespace Ooui
{
public class TextArea : FormControl
{
public event EventHandler Changed {
public event TargetEventHandler Changed {
add => AddEventListener ("change", value);
remove => RemoveEventListener ("change", value);
}
public event EventHandler Inputted {
public event TargetEventHandler Inputted {
add => AddEventListener ("input", value);
remove => RemoveEventListener ("input", value);
}