Panda3D
mouseSubregion.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file mouseSubregion.cxx
10  * @author drose
11  * @date 2005-05-13
12  */
13 
14 #include "mouseSubregion.h"
15 #include "dataNodeTransmit.h"
16 
17 TypeHandle MouseSubregion::_type_handle;
18 
19 /**
20  *
21  */
22 MouseSubregion::
23 MouseSubregion(const std::string &name) :
24  MouseInterfaceNode(name)
25 {
26  _pixel_xy_input = define_input("pixel_xy", EventStoreVec2::get_class_type());
27  _pixel_size_input = define_input("pixel_size", EventStoreVec2::get_class_type());
28  _xy_input = define_input("xy", EventStoreVec2::get_class_type());
29  _button_events_input = define_input("button_events", ButtonEventList::get_class_type());
30 
31  _pixel_xy_output = define_output("pixel_xy", EventStoreVec2::get_class_type());
32  _pixel_size_output = define_output("pixel_size", EventStoreVec2::get_class_type());
33  _xy_output = define_output("xy", EventStoreVec2::get_class_type());
34  _button_events_output = define_output("button_events", ButtonEventList::get_class_type());
35 
36  _pixel_xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
37  _pixel_size = new EventStoreVec2(LPoint2(0.0f, 0.0f));
38  _xy = new EventStoreVec2(LPoint2(0.0f, 0.0f));
39  _button_events = new ButtonEventList;
40 }
41 
42 /**
43  *
44  */
45 MouseSubregion::
46 ~MouseSubregion() {
47 }
48 
49 /**
50  * The virtual implementation of transmit_data(). This function receives an
51  * array of input parameters and should generate an array of output
52  * parameters. The input parameters may be accessed with the index numbers
53  * returned by the define_input() calls that were made earlier (presumably in
54  * the constructor); likewise, the output parameters should be set with the
55  * index numbers returned by the define_output() calls.
56  */
57 void MouseSubregion::
58 do_transmit_data(DataGraphTraverser *, const DataNodeTransmit &input,
59  DataNodeTransmit &output) {
60  bool has_mouse = false;
61 
62  if (input.has_data(_xy_input)) {
63  const EventStoreVec2 *xy;
64  DCAST_INTO_V(xy, input.get_data(_xy_input).get_ptr());
65  const LVecBase2 &p = xy->get_value();
66 
67  // Scale the old value into the new range.
68  LVecBase2 n((p[0] - _minx) * _scalex - 1.0f, (p[1] - _miny) * _scaley - 1.0f);
69 
70  // If the mouse is indeed within the display region, pass it down.
71  if (n[0] >= -1.0f && n[0] <= 1.0f &&
72  n[1] >= -1.0f && n[1] <= 1.0f) {
73  _xy->set_value(n);
74  output.set_data(_xy_output, EventParameter(_xy));
75 
76  // Also compute the pixel coordinates, based on the supplied pixel_size.
77  if (input.has_data(_pixel_size_input)) {
78  const EventStoreVec2 *pixel_size;
79  DCAST_INTO_V(pixel_size, input.get_data(_pixel_size_input).get_ptr());
80  const LVecBase2 &s = pixel_size->get_value();
81 
82  PN_stdfloat xf = (1.0f + n[0]) * 0.5f * s[0];
83  PN_stdfloat yf = (1.0f - n[1]) * 0.5f * s[1];
84 
85  _pixel_xy->set_value(LPoint2(xf, yf));
86  output.set_data(_pixel_xy_output, EventParameter(_pixel_xy));
87  }
88 
89  has_mouse = true;
90  }
91  }
92 
93  if (has_mouse) {
94  // If we have the mouse, send all of the mouse buttons.
95  output.set_data(_button_events_output, input.get_data(_button_events_input));
96  } else {
97  // Otherwise, send only the button-up events.
98  _button_events->clear();
99 
100  if (input.has_data(_button_events_input)) {
101  const ButtonEventList *button_events;
102  DCAST_INTO_V(button_events, input.get_data(_button_events_input).get_ptr());
103  int num_events = button_events->get_num_events();
104  for (int i = 0; i < num_events; i++) {
105  const ButtonEvent &be = button_events->get_event(i);
106  if (be._type == ButtonEvent::T_up) {
107  // Don't suppress this button event; pass it through.
108  _button_events->add_event(be);
109  }
110  }
111  }
112 
113  if (_button_events->get_num_events() != 0) {
114  output.set_data(_button_events_output, EventParameter(_button_events));
115  }
116  }
117 
118 
119  // Now scale the window size.
120  if (input.has_data(_pixel_size_input)) {
121  const EventStoreVec2 *pixel_size;
122  DCAST_INTO_V(pixel_size, input.get_data(_pixel_size_input).get_ptr());
123  const LVecBase2 &s = pixel_size->get_value();
124 
125  LVecBase2 n(s[0] * (_r - _l), s[1] * (_t - _b));
126  _pixel_size->set_value(n);
127  output.set_data(_pixel_size_output, EventParameter(_pixel_size));
128  }
129 }
An optional parameter associated with an event.
get_value
Retrieves the value stored in the parameter.
Definition: paramValue.h:115
int get_num_events() const
Returns the number of events in the list.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Records a button event of some kind.
Definition: buttonEvent.h:46
bool has_data(int index) const
Returns true if the indicated parameter has been stored, false otherwise.
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
Definition: paramValue.h:103
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Records a set of button events that happened recently.
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
This is the base class for some classes that monitor the mouse and keyboard input and perform some ac...
TypedWritableReferenceCount * get_ptr() const
Retrieves a pointer to the actual value stored in the parameter.
const EventParameter & get_data(int index) const
Extracts the data for the indicated index, if it has been stored, or the empty parameter if it has no...
const ButtonEvent & get_event(int n) const
Returns the nth event in the list.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
Encapsulates the data generated from (or sent into) any particular DataNode.
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...