summaryrefslogtreecommitdiffhomepage
path: root/SixenseSDK/include/sixense_utils/event_triggers.hpp
blob: 53454ef8e21e8d4821de3b211f0495b28d5a3261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 *
 * SIXENSE CONFIDENTIAL
 *
 * Copyright (C) 2011 Sixense Entertainment Inc.
 * All Rights Reserved
 *
 */

#ifndef EVENT_TRIGGERS_HPP
#define EVENT_TRIGGERS_HPP

#pragma warning(push)
#pragma warning( disable:4251 )

#include <sixense_math.hpp>

namespace sixenseUtils {

		// Classes for triggering events based on joystick or trigger positions

	// These are classes that call the supplied EventTriggerBase's 'trigger()' events when triggered. They can be 
	// created and bound to a switch.

	class EventTriggerBase {
	public:
		virtual void trigger() const = 0;
	};

	// Do nothing
	class NullEventTrigger : public EventTriggerBase {
	public:
		virtual void trigger() const {};
	};



	// These classes monitor a floating point value, and trigger their events when the value crosses
	// a threshold, both rising and falling.

	class EventSwitchBase { 
	public:
		EventSwitchBase( float thresh, const EventTriggerBase *positive_transition_event, const EventTriggerBase *negative_transition_event ) : _last_state(false), _thresh(thresh), _positive_transition_event(positive_transition_event), _negative_transition_event(negative_transition_event) {}
		~EventSwitchBase() {
			delete _positive_transition_event;
			delete _negative_transition_event;
		}
		void test( float val ) {
			if( val > _thresh  &&  !_last_state ) {
				if( _positive_transition_event ) {
					_positive_transition_event->trigger();
				}
				_last_state = true;
			}
			if( val < _thresh  &&  _last_state ) {
				if( _negative_transition_event ) {
					_negative_transition_event->trigger();
				}
				_last_state = false;
			}
		}

		bool getLastState( void ) {
			return _last_state;
		}

	protected:
		bool _last_state;
		float _thresh;
		const EventTriggerBase *_positive_transition_event;
		const EventTriggerBase *_negative_transition_event;

	private:
		EventSwitchBase() {}
	};

	class PlaneCrossEventSwitch : public EventSwitchBase {
	public:
		PlaneCrossEventSwitch( sixenseMath::Plane plane, float thresh, const EventTriggerBase *positive_transition_event, const EventTriggerBase *negative_transition_event ) : EventSwitchBase( thresh, positive_transition_event, negative_transition_event ),
			_plane(plane) {}

		void test( sixenseMath::Vector3 pt ) {
			EventSwitchBase::test( -1.0f * (float)_plane.whichSide( pt ) );
		}
	private:
		sixenseMath::Plane _plane;
	};

	class BinaryEventSwitch : public EventSwitchBase {
	public:
		BinaryEventSwitch( const EventTriggerBase *positive_transition_event, const EventTriggerBase *negative_transition_event ) : EventSwitchBase( 0.5f, positive_transition_event, negative_transition_event ) {}

		void test( bool val ) {
			EventSwitchBase::test( val ? 1.0f : 0.0f );
		}
	};

	class ValuatorEventSwitch : public EventSwitchBase {
	public:
		ValuatorEventSwitch( float thresh, const EventTriggerBase *positive_transition_event, const EventTriggerBase *negative_transition_event ) : EventSwitchBase( thresh, positive_transition_event, negative_transition_event ){}
		void test( float val ) {
			EventSwitchBase::test( val );
		}
	};


}
#pragma warning(pop)

#endif