Panda3D
 All Classes Functions Variables Enumerations
mouseInterfaceNode.cxx
1 // Filename: mouseInterfaceNode.cxx
2 // Created by: drose (11Jun04)
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 
16 #include "trackball.h"
17 #include "buttonEvent.h"
18 #include "buttonEventList.h"
19 #include "dataNodeTransmit.h"
20 #include "mouseData.h"
21 
22 TypeHandle MouseInterfaceNode::_type_handle;
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: MouseInterfaceNode::Constructor
26 // Access: Public
27 // Description:
28 ////////////////////////////////////////////////////////////////////
29 MouseInterfaceNode::
30 MouseInterfaceNode(const string &name) :
31  DataNode(name)
32 {
33  _button_events_input = define_input("button_events", ButtonEventList::get_class_type());
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: MouseInterfaceNode::Destructor
38 // Access: Public, Virtual
39 // Description:
40 ////////////////////////////////////////////////////////////////////
41 MouseInterfaceNode::
42 ~MouseInterfaceNode() {
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: MouseInterfaceNode::require_button
47 // Access: Published
48 // Description: Indicates that the indicated button must be in the
49 // required state (either up or down) in order for this
50 // particular MouseInterfaceNode to do anything. For
51 // instance, this may be called to make a Trackball
52 // object respect mouse input only when the control key
53 // is held down.
54 ////////////////////////////////////////////////////////////////////
56 require_button(const ButtonHandle &button, bool is_down) {
57  _required_buttons_mask.add_button(button);
58  _required_buttons_state.set_button_list(_required_buttons_mask);
59  _current_button_state.set_button_list(_required_buttons_mask);
60 
61  _required_buttons_mask.button_down(button);
62  if (is_down) {
63  _required_buttons_state.button_down(button);
64  } else {
65  _required_buttons_state.button_up(button);
66  }
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function: MouseInterfaceNode::clear_button
71 // Access: Published
72 // Description: Removes any requirement on the indicated button set
73 // by an earlier call to require_button().
74 ////////////////////////////////////////////////////////////////////
76 clear_button(const ButtonHandle &button) {
77  _required_buttons_mask.button_up(button);
78  _required_buttons_state.button_up(button);
79 
80  // The _required_buttons_mask and state must always keep the buttons
81  // that are listed in _watched_buttons.
82 
83  if (!_watched_buttons.has_button(button)) {
84  _required_buttons_mask.remove_button(button);
85  _required_buttons_state.set_button_list(_required_buttons_mask);
86  _current_button_state.set_button_list(_required_buttons_mask);
87  }
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: MouseInterfaceNode::clear_all_button
92 // Access: Published
93 // Description: Removes all requirements on buttons set by an earlier
94 // call to require_button().
95 ////////////////////////////////////////////////////////////////////
98  _required_buttons_mask.all_buttons_up();
99  _required_buttons_state.all_buttons_up();
100 
101  _required_buttons_mask.set_button_list(_watched_buttons);
102  _required_buttons_state.set_button_list(_watched_buttons);
103  _current_button_state.set_button_list(_watched_buttons);
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function: MouseInterfaceNode::watch_button
108 // Access: Protected
109 // Description: Indicates that the derived class would like to know
110 // the state of the given button.
111 ////////////////////////////////////////////////////////////////////
112 void MouseInterfaceNode::
113 watch_button(const ButtonHandle &button) {
114  _watched_buttons.add_button(button);
115 
116  // We also add the button to _required_buttons_mask and
117  // _required_buttons_state, but it's left 'up' in these two.
118  _required_buttons_mask.add_button(button);
119  _required_buttons_state.set_button_list(_required_buttons_mask);
120  _current_button_state.set_button_list(_required_buttons_mask);
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: MouseInterfaceNode::check_button_events
125 // Access: Protected
126 // Description: Gets the button events from the data graph and
127 // updates the ModifierButtons objects appropriately.
128 //
129 // Sets required_buttons_match to true if the required
130 // combination of buttons are being held down, or false
131 // otherwise.
132 //
133 // The return value is the list of button events
134 // processed this frame, or NULL if there are no button
135 // events.
136 ////////////////////////////////////////////////////////////////////
137 const ButtonEventList *MouseInterfaceNode::
138 check_button_events(const DataNodeTransmit &input,
139  bool &required_buttons_match) {
140  const ButtonEventList *button_events = NULL;
141 
142  if (input.has_data(_button_events_input)) {
143  DCAST_INTO_R(button_events, input.get_data(_button_events_input).get_ptr(), NULL);
144  button_events->update_mods(_current_button_state);
145  }
146 
147  required_buttons_match =
148  (_current_button_state & _required_buttons_mask) == _required_buttons_state;
149 
150  return button_events;
151 }
bool button_down(ButtonHandle button)
Records that a particular button has been pressed.
The fundamental type of node for the data graph.
Definition: dataNode.h:64
void clear_all_buttons()
Removes all requirements on buttons set by an earlier call to require_button().
bool remove_button(ButtonHandle button)
Removes the indicated button from the set of buttons being monitored.
void all_buttons_up()
Marks all monitored buttons as being in the "up" state.
bool button_up(ButtonHandle button)
Records that a particular button has been released.
Records a set of button events that happened recently.
void update_mods(ModifierButtons &mods) const
Updates the indicated ModifierButtons object with all of the button up/down transitions indicated in ...
A ButtonHandle represents a single button from any device, including keyboard buttons and mouse butto...
Definition: buttonHandle.h:28
void clear_button(const ButtonHandle &button)
Removes any requirement on the indicated button set by an earlier call to require_button().
bool add_button(ButtonHandle button)
Adds the indicated button to the set of buttons that will be monitored for upness and downness...
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...
void require_button(const ButtonHandle &button, bool is_down)
Indicates that the indicated button must be in the required state (either up or down) in order for th...
TypedWritableReferenceCount * get_ptr() const
Retrieves a pointer to the actual value stored in the parameter.
void set_button_list(const ModifierButtons &other)
Sets the list of buttons to watch to be the same as that of the other ModifierButtons object...
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.
bool has_button(ButtonHandle button) const
Returns true if the indicated button is in the set of buttons being monitored, false otherwise...
Encapsulates the data generated from (or sent into) any particular DataNode.