comparison Orchestland/Assets/LeapMotion/Widgets/Scripts/Interfaces/DataBinder.cs @ 1:f7675884f2a1

Add Orchestland project
author Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:09:20 +0900
parents
children
comparison
equal deleted inserted replaced
0:347d21cdfc22 1:f7675884f2a1
1 using UnityEngine;
2 using System;
3 using System.Collections.Generic;
4
5 namespace LMWidgets {
6
7 // Interface to define an object that can be a data provider to a widget.
8 public abstract class DataBinder<WidgetType, PayloadType> : MonoBehaviour where WidgetType : IDataBoundWidget<WidgetType, PayloadType> {
9 [SerializeField]
10 private List<WidgetType> m_widgets;
11
12 private PayloadType m_lastDataValue;
13
14 // Fires when the data is updated with the most recent data as the payload
15 public event EventHandler<EventArg<PayloadType>> DataChangedHandler;
16
17 /// <summary>
18 /// Returns the current system value of the data.
19 /// </summary>
20 /// <remarks>
21 /// In the default implementation of the data-binder this is called every frame (in Update) so it's best to keep
22 /// this implementation light weight.
23 /// </remarks>
24 abstract public PayloadType GetCurrentData();
25
26 /// <summary>
27 /// Set the current system value of the data.
28 /// </summary>
29 abstract protected void setDataModel(PayloadType value);
30
31 // Directly set the current value of the data-model and send out the relevant updates.
32 public void SetCurrentData(PayloadType value) {
33 setDataModel (value);
34 updateLinkedWidgets ();
35 fireDataChangedEvent (GetCurrentData ());
36 m_lastDataValue = GetCurrentData ();
37 }
38
39 // Itterate through the linked widgets and update their values.
40 private void updateLinkedWidgets() {
41 foreach(WidgetType widget in m_widgets) {
42 widget.SetWidgetValue(GetCurrentData());
43 }
44 }
45
46 // Register all assigned widgets with the data-binder.
47 virtual protected void Awake() {
48 foreach (WidgetType widget in m_widgets) {
49 widget.RegisterDataBinder(this);
50 }
51 }
52
53 // Grab the inital value for GetCurrentData
54 virtual protected void Start() {
55 m_lastDataValue = GetCurrentData();
56 }
57
58 // Checks for change in data.
59 // We need this in addition to SetCurrentData as the data we're linked to
60 // could be modified by an external source.
61 void Update() {
62 PayloadType currentData = GetCurrentData();
63 if (!compare (m_lastDataValue, currentData)) {
64 updateLinkedWidgets ();
65 fireDataChangedEvent (currentData);
66 }
67 m_lastDataValue = currentData;
68 }
69
70 // Fire the data changed event.
71 // Wrapping this in a function allows child classes to call it and fire the event.
72 protected void fireDataChangedEvent(PayloadType currentData) {
73 EventHandler<EventArg<PayloadType>> handler = DataChangedHandler;
74 if ( handler != null ) { handler(this, new EventArg<PayloadType>(currentData)); }
75 }
76
77 // Handles proper comparison of generic types.
78 private bool compare(PayloadType x, PayloadType y)
79 {
80 return EqualityComparer<PayloadType>.Default.Equals(x, y);
81 }
82 }
83
84 public abstract class DataBinderSlider : DataBinder<SliderBase, float> {};
85 public abstract class DataBinderToggle : DataBinder<ButtonToggleBase, bool> {};
86 public abstract class DataBinderDial : DataBinder<DialGraphics, string> {};
87 }