Panda3D
 All Classes Functions Variables Enumerations
mouseInterfaceNode.cxx
00001 // Filename: mouseInterfaceNode.cxx
00002 // Created by:  drose (11Jun04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 #include "trackball.h"
00017 #include "buttonEvent.h"
00018 #include "buttonEventList.h"
00019 #include "dataNodeTransmit.h"
00020 #include "mouseData.h"
00021 
00022 TypeHandle MouseInterfaceNode::_type_handle;
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //     Function: MouseInterfaceNode::Constructor
00026 //       Access: Public
00027 //  Description:
00028 ////////////////////////////////////////////////////////////////////
00029 MouseInterfaceNode::
00030 MouseInterfaceNode(const string &name) :
00031   DataNode(name)
00032 {
00033   _button_events_input = define_input("button_events", ButtonEventList::get_class_type());
00034 }
00035 
00036 ////////////////////////////////////////////////////////////////////
00037 //     Function: MouseInterfaceNode::Destructor
00038 //       Access: Public, Virtual
00039 //  Description:
00040 ////////////////////////////////////////////////////////////////////
00041 MouseInterfaceNode::
00042 ~MouseInterfaceNode() {
00043 }
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //     Function: MouseInterfaceNode::require_button
00047 //       Access: Published
00048 //  Description: Indicates that the indicated button must be in the
00049 //               required state (either up or down) in order for this
00050 //               particular MouseInterfaceNode to do anything.  For
00051 //               instance, this may be called to make a Trackball
00052 //               object respect mouse input only when the control key
00053 //               is held down.
00054 ////////////////////////////////////////////////////////////////////
00055 void MouseInterfaceNode::
00056 require_button(const ButtonHandle &button, bool is_down) {
00057   _required_buttons_mask.add_button(button);
00058   _required_buttons_state.set_button_list(_required_buttons_mask);
00059   _current_button_state.set_button_list(_required_buttons_mask);
00060 
00061   _required_buttons_mask.button_down(button);
00062   if (is_down) {
00063     _required_buttons_state.button_down(button);
00064   } else {
00065     _required_buttons_state.button_up(button);
00066   }
00067 }
00068 
00069 ////////////////////////////////////////////////////////////////////
00070 //     Function: MouseInterfaceNode::clear_button
00071 //       Access: Published
00072 //  Description: Removes any requirement on the indicated button set
00073 //               by an earlier call to require_button().
00074 ////////////////////////////////////////////////////////////////////
00075 void MouseInterfaceNode::
00076 clear_button(const ButtonHandle &button) {
00077   _required_buttons_mask.button_up(button);
00078   _required_buttons_state.button_up(button);
00079 
00080   // The _required_buttons_mask and state must always keep the buttons
00081   // that are listed in _watched_buttons.
00082 
00083   if (!_watched_buttons.has_button(button)) {
00084     _required_buttons_mask.remove_button(button);
00085     _required_buttons_state.set_button_list(_required_buttons_mask);
00086     _current_button_state.set_button_list(_required_buttons_mask);
00087   }
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //     Function: MouseInterfaceNode::clear_all_button
00092 //       Access: Published
00093 //  Description: Removes all requirements on buttons set by an earlier
00094 //               call to require_button().
00095 ////////////////////////////////////////////////////////////////////
00096 void MouseInterfaceNode::
00097 clear_all_buttons() {
00098   _required_buttons_mask.all_buttons_up();
00099   _required_buttons_state.all_buttons_up();
00100 
00101   _required_buttons_mask.set_button_list(_watched_buttons);
00102   _required_buttons_state.set_button_list(_watched_buttons);
00103   _current_button_state.set_button_list(_watched_buttons);
00104 }
00105 
00106 ////////////////////////////////////////////////////////////////////
00107 //     Function: MouseInterfaceNode::watch_button
00108 //       Access: Protected
00109 //  Description: Indicates that the derived class would like to know
00110 //               the state of the given button.
00111 ////////////////////////////////////////////////////////////////////
00112 void MouseInterfaceNode::
00113 watch_button(const ButtonHandle &button) {
00114   _watched_buttons.add_button(button);
00115 
00116   // We also add the button to _required_buttons_mask and
00117   // _required_buttons_state, but it's left 'up' in these two.
00118   _required_buttons_mask.add_button(button);
00119   _required_buttons_state.set_button_list(_required_buttons_mask);
00120   _current_button_state.set_button_list(_required_buttons_mask);
00121 }
00122 
00123 ////////////////////////////////////////////////////////////////////
00124 //     Function: MouseInterfaceNode::check_button_events
00125 //       Access: Protected
00126 //  Description: Gets the button events from the data graph and
00127 //               updates the ModifierButtons objects appropriately.
00128 //
00129 //               Sets required_buttons_match to true if the required
00130 //               combination of buttons are being held down, or false
00131 //               otherwise.
00132 //
00133 //               The return value is the list of button events
00134 //               processed this frame, or NULL if there are no button
00135 //               events.
00136 ////////////////////////////////////////////////////////////////////
00137 const ButtonEventList *MouseInterfaceNode::
00138 check_button_events(const DataNodeTransmit &input,
00139                     bool &required_buttons_match) {
00140   const ButtonEventList *button_events = NULL;
00141 
00142   if (input.has_data(_button_events_input)) {
00143     DCAST_INTO_R(button_events, input.get_data(_button_events_input).get_ptr(), false);
00144     button_events->update_mods(_current_button_state);
00145   }
00146 
00147   required_buttons_match = 
00148     (_current_button_state & _required_buttons_mask) == _required_buttons_state;
00149 
00150   return button_events;
00151 }
 All Classes Functions Variables Enumerations