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