Panda3D Manual: Mouse SupportPanda3D has mouse support built in. In C++, you need to do the following if you want the mouse to control the camera: window->setup_trackball(); You don't need to do this to enable the mouse itself, only to enable a task that drives the camera around. You can still get the position of the mouse, as well as the mouse clicks, even if you don't enable this "trackball mode". To get the position: if (mouseWatcher->has_mouse()){ The mouse clicks generate "events." To understand what events are, and how to process them, you will need to read the Event Handling section. The names of the events generated are:
If you want to hide the mouse cursor, you want the line: "cursor hidden #t" in your Config.prc or this section of code: Re-enabling mouse control If you need to re-enable the mouse control of the camera, you have to adjust mouseInterfaceNode to the current camera transformation : Otherwise the camera would be placed back to the last position when the mouse control was enabled. Relative mode Sometimes it is desirable for the mouse cursor to stay in place but still get movement events, like in a first person game. // To set relative mode and hide the cursor:
Multiple Mice If you have multiple mice connected to a single machine, it is possible to get mouse movements and buttons for each individual mouse. This is called raw mouse input. It is really only useful if you are building an arcade machine that has lots of trackballs or spinners. In order to use raw mouse input, you first need to enable it. To do so, add the following line to your panda configuration file: read-raw-mice #t This causes the panda main window to be created with the "raw_mice" window property. That window property, in turn, causes the window to track and store the positions and buttons of the raw mice. Then, that data is extracted from the main window by objects of class MouseWatcher. The application program can fetch the mouse data from the MouseWatchers. The global variable The first MouseWatcher on the list always represents the system mouse pointer - a virtual mouse that moves around whenever any of the physical mice do. Usually, you do not want to use this virtual mouse. If you're accessing raw mice, you usually want to access the real, physical mice. The list So to print out the positions of the mice, use this: Each mouse will have a name-string, which might be something along the lines of "Micrologic High-Precision Gaming Mouse 2.0 #20245/405". The name is the only way to tell the various mice apart. If you have two different mice of different brands, you can easily tell them apart by the names. If you have two mice of the same make and manufacture, then their names will be very similar, but still unique. This is not because the mice contain serial numbers, but rather because they are uniquefied based on the USB port into which they are plugged. That means that if you move a mouse from one USB port to another, it will have a new name. For all practical purposes, that means that you will need to store a config file that maps mouse name to intended purpose. Raw mouse buttons generate events. The event names are similar to the ones for the system mouse, except that they have a "mousedevX" prefix. Ie, an example event might be Multiple Mice under Linux To use raw mouse input under Linux, the panda program needs to open the device files /dev/input/event*. On many Linux distributions, the permission bits are set such that this is not possible. This is a flaw in these distributions. It is not a good idea to just change the permission bits. Doing so introduces a huge security hole in which any logged in user can monitor the mice, the joysticks, and the keyboard --- including any passwords that may be typed. The correct solution is to change the ownership of the input devices whenever a user sits down at the console. There is a module, pam_console, that does this, but it is now obsoleted, and has been removed from several distros. The Fedora pam_console removal page states that ACLs set by the HAL should replace pam_console's functionality. Currently, since it does not seem that HAL provides this yet, the best course of action is to make an 'input' group as described on the Gizmod wiki. If you are building a stand-alone arcade machine that does not allow remote login and probably doesn't even have a net connection, then changing the permission bits isn't going to hurt you. © Carnegie Mellon University 2010 |