15 #include "trackball.h" 16 #include "buttonEvent.h" 17 #include "buttonEventList.h" 18 #include "dataNodeTransmit.h" 19 #include "compose_matrix.h" 20 #include "mouseData.h" 21 #include "modifierButtons.h" 22 #include "linmath_events.h" 23 #include "mouseButton.h" 24 #include "keyboardButton.h" 25 #include "config_tform.h" 40 Trackball(
const string &name) :
43 _pixel_xy_input = define_input(
"pixel_xy", EventStoreVec2::get_class_type());
45 _transform_output = define_output(
"transform", TransformState::get_class_type());
47 _transform = TransformState::make_identity();
53 _lastx = _lasty = 0.5f;
56 _translation.set(0.0f, 0.0f, 0.0f);
60 _cs = get_default_coordinate_system();
61 _control_mode = CM_default;
68 if (trackball_use_alt_keys) {
71 watch_button(KeyboardButton::control());
72 watch_button(KeyboardButton::meta());
73 watch_button(KeyboardButton::alt());
94 _translation.set(0.0f, 0.0f, 0.0f);
120 _fwdscale = fwdscale;
134 PN_stdfloat Trackball::
136 return _translation[0];
139 PN_stdfloat Trackball::
141 return _translation[1];
144 PN_stdfloat Trackball::
146 return _translation[2];
162 set_pos(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z) {
163 _translation.set(x, y, z);
168 set_x(PN_stdfloat x) {
174 set_y(PN_stdfloat y) {
180 set_z(PN_stdfloat z) {
194 decompose_matrix(_rotation, scale, shear, hpr, translate);
198 PN_stdfloat Trackball::
201 decompose_matrix(_rotation, scale, shear, hpr, translate);
205 PN_stdfloat Trackball::
208 decompose_matrix(_rotation, scale, shear, hpr, translate);
212 PN_stdfloat Trackball::
215 decompose_matrix(_rotation, scale, shear, hpr, translate);
227 LVecBase3 scale, shear, old_hpr, translate;
228 decompose_matrix(_rotation, scale, shear, old_hpr, translate);
229 compose_matrix(_rotation, scale, shear, hpr, translate);
234 set_hpr(PN_stdfloat h, PN_stdfloat p, PN_stdfloat r) {
236 decompose_matrix(_rotation, scale, shear, hpr, translate);
238 compose_matrix(_rotation, scale, shear, hpr, translate);
243 set_h(PN_stdfloat h) {
245 decompose_matrix(_rotation, scale, shear, hpr, translate);
247 compose_matrix(_rotation, scale, shear, hpr, translate);
252 set_p(PN_stdfloat p) {
254 decompose_matrix(_rotation, scale, shear, hpr, translate);
256 compose_matrix(_rotation, scale, shear, hpr, translate);
261 set_r(PN_stdfloat r) {
263 decompose_matrix(_rotation, scale, shear, hpr, translate);
265 compose_matrix(_rotation, scale, shear, hpr, translate);
281 _translation.set(0.0f, 0.0f, 0.0f);
354 _control_mode = control_mode;
364 return _control_mode;
430 _mat = invert(_orig);
471 apply(
double x,
double y,
int button) {
472 if (button && !_rel_to.
is_empty()) {
478 if (button == B1_MASK && _control_mode != CM_default) {
482 switch (_control_mode) {
496 button = B2_MASK | B3_MASK;
505 if (button == B1_MASK) {
512 }
else if (button == (B2_MASK | B3_MASK)) {
520 }
else if ((button == B2_MASK) || (button == (B1_MASK | B3_MASK))) {
529 }
else if ((button == B3_MASK) || (button == (B1_MASK | B2_MASK))) {
577 _mat = invert(_orig);
601 bool required_buttons_match;
602 check_button_events(input, required_buttons_match);
605 if (required_buttons_match && input.
has_data(_pixel_xy_input)) {
609 PN_stdfloat this_x = p[0];
610 PN_stdfloat this_y = p[1];
614 if (is_down(KeyboardButton::alt())) {
616 this_button |= B2_MASK;
617 if (is_down(KeyboardButton::meta()) || is_down(KeyboardButton::control())) {
618 this_button |= B3_MASK;
621 }
else if (is_down(KeyboardButton::meta()) || is_down(KeyboardButton::control())) {
623 this_button |= B3_MASK;
627 this_button |= B1_MASK;
631 this_button |= B2_MASK;
634 this_button |= B3_MASK;
637 PN_stdfloat x = this_x - _lastx;
638 PN_stdfloat y = this_y - _lasty;
640 if (this_button == _last_button) {
641 apply(x, y, this_button);
644 _last_button = this_button;
652 _transform = TransformState::make_mat(_mat);
static const LMatrix4f & ident_mat()
Returns an identity matrix.
const LMatrix4 & get_trans_mat() const
Returns the actual transform that will be applied to the scene graph.
This is the base class for all three-component vectors and points.
void set_mat(const LMatrix4 &mat)
Stores the indicated transform in the trackball.
void set_control_mode(ControlMode control_mode)
Sets the control mode.
static LMatrix4f translate_mat(const LVecBase3f &trans)
Returns a matrix that applies the indicated translation.
An optional parameter associated with an event.
bool is_empty() const
Returns true if the NodePath contains no nodes.
void set_invert(bool flag)
Sets the invert flag.
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
bool has_data(int index) const
Returns true if the indicated parameter has been stored, false otherwise.
A handy class object for storing simple values (like integers or strings) passed along with an Event ...
CoordinateSystem get_coordinate_system() const
Returns the coordinate system of the Trackball.
void set_coordinate_system(CoordinateSystem cs)
Sets the coordinate system of the Trackball.
static LVector3f forward(CoordinateSystem cs=CS_default)
Returns the forward vector for the given coordinate system.
const LPoint3 & get_pos() const
**** Translation ****
PN_stdfloat get_forward_scale() const
Returns the scale factor applied to forward and backward motion.
ControlMode
**** Misc ****
void set_data(int index, const EventParameter &data)
Sets the data for the indicated parameter.
ControlMode get_control_mode() const
Returns the control mode.
LPoint3 get_origin() const
Returns the current center of rotation.
const NodePath & get_rel_to() const
Returns the NodePath that all trackball manipulations are relative to, or the empty path...
This is a 4-by-4 transform matrix.
void reset()
Reinitializes all transforms to identity.
This is the base class for all two-component vectors and points.
void move_origin(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z)
Moves the center of rotation by the given amount.
LVecBase3 get_hpr() const
**** Rotation ****
const Type & get_value() const
Retrieves the value stored in the parameter.
static LVector3f right(CoordinateSystem cs=CS_default)
Returns the right vector for the given coordinate system.
const LMatrix4 & get_mat() const
Returns the matrix represented by the trackball rotation.
This is the base class for some classes that monitor the mouse and keyboard input and perform some ac...
TypedWritableReferenceCount * get_ptr() const
Retrieves a pointer to the actual value stored in the parameter.
static LVector3f down(CoordinateSystem cs=CS_default)
Returns the down vector for the given coordinate system.
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...
bool get_invert() const
Returns the invert flag.
void set_origin(const LVecBase3 &origin)
Directly sets the center of rotation.
static LMatrix4f rotate_mat_normaxis(float angle, const LVecBase3f &axis, CoordinateSystem cs=CS_default)
Returns a matrix that rotates by the given angle in degrees counterclockwise about the indicated vect...
void set_rel_to(const NodePath &rel_to)
Sets the NodePath that all trackball manipulations are to be assumed to be relative to...
void set_row(int row, const LVecBase4f &v)
Replaces the indicated row of the matrix.
TypeHandle is the identifier used to differentiate C++ class types.
const TransformState * get_transform(Thread *current_thread=Thread::get_current_thread()) const
Returns the complete transform object set on this node.
void set_hpr(const LVecBase3 &hpr)
Directly set the mover's orientation.
void set_forward_scale(PN_stdfloat fwdscale)
Changes the scale factor applied to forward and backward motion.
LVecBase3f get_row3(int row) const
Retrieves the row column of the matrix as a 3-component vector, ignoring the last column...
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
static LVector3f up(CoordinateSystem cs=CS_default)
Returns the up vector for the given coordinate system.
void set_pos(const LVecBase3 &vec)
Directly set the offset from the rotational origin.
Encapsulates the data generated from (or sent into) any particular DataNode.
void reset_origin_here()
**** Origin of Rotation ****
This object supervises the traversal of the data graph and the moving of data from one DataNode to it...