Panda3D
mouseInterfaceNode.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 mouseInterfaceNode.cxx
10  * @author drose
11  * @date 2004-06-11
12  */
13 
14 #include "trackball.h"
15 #include "buttonEvent.h"
16 #include "buttonEventList.h"
17 #include "dataNodeTransmit.h"
18 #include "mouseData.h"
19 
20 TypeHandle MouseInterfaceNode::_type_handle;
21 
22 /**
23  *
24  */
25 MouseInterfaceNode::
26 MouseInterfaceNode(const std::string &name) :
27  DataNode(name)
28 {
29  _button_events_input = define_input("button_events", ButtonEventList::get_class_type());
30 }
31 
32 /**
33  *
34  */
35 MouseInterfaceNode::
36 ~MouseInterfaceNode() {
37 }
38 
39 /**
40  * Indicates that the indicated button must be in the required state (either
41  * up or down) in order for this particular MouseInterfaceNode to do anything.
42  * For instance, this may be called to make a Trackball object respect mouse
43  * input only when the control key is held down.
44  */
46 require_button(const ButtonHandle &button, bool is_down) {
47  _required_buttons_mask.add_button(button);
48  _required_buttons_state.set_button_list(_required_buttons_mask);
49  _current_button_state.set_button_list(_required_buttons_mask);
50 
51  _required_buttons_mask.button_down(button);
52  if (is_down) {
53  _required_buttons_state.button_down(button);
54  } else {
55  _required_buttons_state.button_up(button);
56  }
57 }
58 
59 /**
60  * Removes any requirement on the indicated button set by an earlier call to
61  * require_button().
62  */
64 clear_button(const ButtonHandle &button) {
65  _required_buttons_mask.button_up(button);
66  _required_buttons_state.button_up(button);
67 
68  // The _required_buttons_mask and state must always keep the buttons that
69  // are listed in _watched_buttons.
70 
71  if (!_watched_buttons.has_button(button)) {
72  _required_buttons_mask.remove_button(button);
73  _required_buttons_state.set_button_list(_required_buttons_mask);
74  _current_button_state.set_button_list(_required_buttons_mask);
75  }
76 }
77 
78 /**
79  * Removes all requirements on buttons set by an earlier call to
80  * require_button().
81  */
84  _required_buttons_mask.all_buttons_up();
85  _required_buttons_state.all_buttons_up();
86 
87  _required_buttons_mask.set_button_list(_watched_buttons);
88  _required_buttons_state.set_button_list(_watched_buttons);
89  _current_button_state.set_button_list(_watched_buttons);
90 }
91 
92 /**
93  * Indicates that the derived class would like to know the state of the given
94  * button.
95  */
96 void MouseInterfaceNode::
97 watch_button(const ButtonHandle &button) {
98  _watched_buttons.add_button(button);
99 
100  // We also add the button to _required_buttons_mask and
101  // _required_buttons_state, but it's left 'up' in these two.
102  _required_buttons_mask.add_button(button);
103  _required_buttons_state.set_button_list(_required_buttons_mask);
104  _current_button_state.set_button_list(_required_buttons_mask);
105 }
106 
107 /**
108  * Gets the button events from the data graph and updates the ModifierButtons
109  * objects appropriately.
110  *
111  * Sets required_buttons_match to true if the required combination of buttons
112  * are being held down, or false otherwise.
113  *
114  * The return value is the list of button events processed this frame, or NULL
115  * if there are no button events.
116  */
117 const ButtonEventList *MouseInterfaceNode::
118 check_button_events(const DataNodeTransmit &input,
119  bool &required_buttons_match) {
120  const ButtonEventList *button_events = nullptr;
121 
122  if (input.has_data(_button_events_input)) {
123  DCAST_INTO_R(button_events, input.get_data(_button_events_input).get_ptr(), nullptr);
124  button_events->update_mods(_current_button_state);
125  }
126 
127  required_buttons_match =
128  (_current_button_state & _required_buttons_mask) == _required_buttons_state;
129 
130  return button_events;
131 }
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:52
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.
bool has_button(ButtonHandle button) const
Returns true if the indicated button is in the set of buttons being monitored, false otherwise.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
bool has_data(int index) const
Returns true if the indicated parameter has been stored, false otherwise.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:26
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypedWritableReferenceCount * get_ptr() const
Retrieves a pointer to the actual value stored in the parameter.
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...
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 set_button_list(const ModifierButtons &other)
Sets the list of buttons to watch to be the same as that of the other ModifierButtons object.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.