Sensor is derived from Resource and has an initial reference count of 1. Sensors ought never be deleted directly. Instead, an application should call Unref(sensor) on any sensor which it created or referenced. This should be done after the final use of the sensor. InterViews Plus will take care of deletion when the reference count has reached zero.
Several sensors are predefined as variables for use by all interactors:
Sensor Type | Description |
allEvents | |
updownEvents | |
onoffEvents | |
noEvents |
Class HierarchySensor->Resource ConstructorsSensor()Creates a new sensor. Initially, this sensor will not catch any events. Sensor is a Resource with an initial reference count of 1. Sensor(const Sensor&)Creates a copy of the given sensor. Sensor(const Sensor*)Creates a sensor that points to the same data as the passed sensor. Public Operationsvoid Catch(EventType)Expresses interest in a particular type of event. Valid event types (from <ivplus/event.h>) are: Event TypeDescription MotionEventDownEventUpEventKeyEventEnterEventLeaveEventFocusInEventFocusOutEvent Two other event types, DeleteWindowEventand CheckpointEventhave been added for window manager interaction. These are caught by all sensors, but are usually handled by a special window handler. void CatchButton(EventType, int)Expresses interest in a particular type of button event for a specific mouse button. The mouse button is passed as an int using the mouse button types LEFTMOUSE, MIDDLEMOUSE and RIGHTMOUSE. virtual boolean Caught(const Event&)Returns whether or not the sensor is catching the given event. void Ignore(EventType)Removes interest in a particular event type. void IgnoreButton(EventType, int)Removes interest in a particular type of button event for a specific mouse button. The mouse button is passed as an int using the mouse button types LEFTMOUSE, MIDDLEMOUSE and RIGHTMOUSE. static void init()Initializes the predefined sensors: allEvents, updownEvents, onoffEvents, noEvents. This is called by world at application startup and is not for use by applications in general. virtual Sensor& operator=(const Sensor&)Overloaded operator = for copying Sensors. Protected Operationsint ButtonFlag(unsigned long b) constReturns the flag needed to indicate interest in the indicated button. b is the button field retrieved from an X event. Used by Catch() and Ignore() to determine appropriate flag to set the internal variables up and down. int ButtonIndex(unsigned long b) constReturns the button index of the indicated button field retrieved from an X event. Used by Catch() and Ignore() to index into the internal variables up and down. boolean ButtonIsSet(unsigned long a[], unsigned long b) constReturns whether or not interest has been expressed in the type of event represented by a (up or down) for the indicated button. b is the button field retrieved from an X event. void ClearButton(unsigned long a[], unsigned long b) {Removes interest in the type of event represented by a (up or down) for the button indicated by b. a is one of the internal variables up or down. b is the button field retrieved from an X event. void ClearMouseButtons(unsigned long a[])Removes interest in the type of event indicated by a (up or down) for all buttons. boolean MouseButtons(unsigned long a[]) constReturns whether interest in the type of event represented by a (up or down) is set for mouse buttons. void SetButton(unsigned long a[], unsigned long b)Expresses interest in the type of event represented by a (up or down) for the button indicated by b. a is one of the internal variables up or down. b is the button field retrieved from an X event. void SetMouseButtons(unsigned long a[])Expresses interest in the type of event indicated by a (up or down) for all buttons. X ResourcesNone. Examples // Sensor Example #include <ivplus/event.h> #include <ivplus/frame.h> #include <ivplus/message.h> #include <ivplus/sensor.h> #include <ivplus/world.h> #include <iostream.h> // MySample has 2 sensors, Interactor is asked to listen to sensor1 whenever // a down event is encountered. Interactor is asked to listen to sensor2 // whenever up event is encountered. This is an illustration using a sensor. class MySample : public Message { public: MySample(const char* text) : Message(text) { Init(); } void MySample::Handle(Event& e); void MySample::Sensor1On() { Listen(sensor1);}; void MySample::Sensor2On() { Listen(sensor2);}; private: void Init(); Sensor *sensor1, *sensor2; }; void MySample::Init() { // Create a sensor that listens to enter, leave, up and down events. sensor1 = new Sensor(); sensor1->Catch(EnterEvent); sensor1->Catch(LeaveEvent); sensor1->Catch(DownEvent); sensor1->Catch(UpEvent); // By default, make sensor1 active. Sensor1On(); // Create a sensor that listen only to down events for the right // mouse button. sensor2 = new Sensor(); sensor2->CatchButton(DownEvent, RIGHTMOUSE); }; void MySample::Handle(Event& e) { if (e.eventType == DownEvent && e.target == this) { // Listen to sensor1. Sensor1On(); cout << " Encountered Down Event" << endl; } if (e.eventType == UpEvent && e.target == this) { // Listen to sensor2. Sensor2On(); cout << " Encountered Up Event" << endl; } if (e.eventType == EnterEvent) cout << " Encountered Enter Event" << endl; if (e.eventType == LeaveEvent) cout << " Encountered Leave Event" << endl; }; int main(int argc, char **argv) { // Create the world to get the connection with the display. World* world = new World("MyApp", argc, argv); MySample* sample = new MySample("Use mouse button3 down event for Sensor1\n Use mouse button3 up event for Sensor2"); // Map the window. world->InsertApplication(new Frame(sample, 1, BevelOut)); // Enter the event loop. world->Run(); return 0; }
sensor.h