Added animations and keys (so far, only used by the Navigator Mode)

This commit is contained in:
mtarini 2008-11-12 16:33:40 +00:00
parent 5704acea18
commit 1ad548c24c
2 changed files with 108 additions and 52 deletions

View File

@ -125,7 +125,7 @@ Transform::Transform() {
Trackball::Trackball(): current_button(0), current_mode(NULL), inactive_mode(NULL), Trackball::Trackball(): current_button(0), current_mode(NULL), inactive_mode(NULL),
dragging(false), spinnable(true), spinning(false), dragging(false), spinnable(true), spinning(false),
history_size(10){ history_size(10), last_time(0), fixedTimestepMode(false) {
setDefaultMapping (); setDefaultMapping ();
} }
@ -141,17 +141,24 @@ Trackball::~Trackball()
void Trackball::setDefaultMapping () { void Trackball::setDefaultMapping () {
idle_and_keys_mode = NULL;
inactive_mode = new InactiveMode (); inactive_mode = new InactiveMode ();
modes.clear (); modes.clear ();
modes[0] = NULL; modes[0] = NULL;
modes[BUTTON_MIDDLE | KEY_ALT] =
modes[BUTTON_LEFT] = new SphereMode (); modes[BUTTON_LEFT] = new SphereMode ();
modes[BUTTON_LEFT | KEY_CTRL] = new PanMode (); modes[BUTTON_LEFT | KEY_CTRL] = new PanMode ();
modes[BUTTON_MIDDLE] = new PanMode (); modes[BUTTON_MIDDLE] = new PanMode ();
modes[WHEEL] =
modes[BUTTON_LEFT | KEY_SHIFT] = new ScaleMode (); modes[BUTTON_LEFT | KEY_SHIFT] = new ScaleMode ();
modes[BUTTON_LEFT | KEY_ALT] = new ZMode (); modes[BUTTON_LEFT | KEY_ALT] = new ZMode ();
modes[BUTTON_MIDDLE | KEY_ALT] = new SphereMode ();
modes[WHEEL] = new ScaleMode ();
SetCurrentAction ();
} }
void Trackball::SetIdentity() { void Trackball::SetIdentity() {
@ -171,9 +178,8 @@ void Trackball::DrawPostApply() {
if(current_mode !=NULL){ if(current_mode !=NULL){
current_mode->Draw(this); current_mode->Draw(this);
}else{ }else{
assert(inactive_mode != NULL); if (inactive_mode != NULL) inactive_mode->Draw(this);
inactive_mode->Draw(this); }
}
} }
void Trackball::Apply () { void Trackball::Apply () {
@ -329,8 +335,7 @@ void Trackball::Reset() {
if(mode!=NULL) if(mode!=NULL)
mode->Reset(); mode->Reset();
} }
assert(inactive_mode != NULL); if (inactive_mode != NULL) inactive_mode->Reset();
inactive_mode->Reset();
} }
//interface //interface
@ -358,10 +363,40 @@ void Trackball::MouseMove(int x, int y) {
current_mode->Apply(this, Point3f(float(x), float(y), 0)); current_mode->Apply(this, Point3f(float(x), float(y), 0));
} }
bool Trackball::IsAnimating(unsigned int msec){
bool res;
if(idle_and_keys_mode == NULL) res=false; else res=idle_and_keys_mode->IsAnimating(this);
if (!fixedTimestepMode) {
if (msec==0) msec = clock()*1000/CLOCKS_PER_SEC;
if (!res) {
last_time = msec;
}
}
return res;
}
void Trackball::Sync(unsigned int msec) {
if (!fixedTimestepMode) Animate(msec);
}
void Trackball::Animate(unsigned int msec){
unsigned int delta;
if (fixedTimestepMode) delta=msec;
else {
if (msec==0) msec = clock()*1000/CLOCKS_PER_SEC;
delta = msec -last_time;
last_time = msec;
}
if(idle_and_keys_mode == NULL) return;
idle_and_keys_mode->Animate(delta,this);
}
void Trackball::MouseUp(int /* x */, int /* y */, int button) { void Trackball::MouseUp(int /* x */, int /* y */, int button) {
undo_track = track; undo_track = track;
current_button &= (~button); ButtonUp(vcg::Trackball::Button(button));
SetCurrentAction(); //current_button &= (~button);
//SetCurrentAction();
} }
// it assumes that a notch of 1.0 is a single step of the wheel // it assumes that a notch of 1.0 is a single step of the wheel
@ -373,8 +408,8 @@ void Trackball::MouseWheel(float notch)
SetCurrentAction(); SetCurrentAction();
if (current_mode == NULL) if (current_mode == NULL)
{ {
ScaleMode scalemode; //ScaleMode scalemode;
scalemode.Apply (this, notch); //scalemode.Apply (this, notch);
} }
else else
{ {
@ -399,34 +434,34 @@ void Trackball::MouseWheel(float notch, int button)
SetCurrentAction (); SetCurrentAction ();
} }
void Trackball::ButtonDown(Trackball::Button button) { void Trackball::ButtonDown(Trackball::Button button, unsigned int msec) {
Sync(msec);
bool old_sticky=false, new_sticky=false; bool old_sticky=false, new_sticky=false;
assert (modes.count (0)); assert (modes.count (0));
if ( ( modes.count (current_button) ) && ( modes[current_button] != NULL ) ) {
old_sticky = modes[current_button]->isSticky(); Button b=Button(current_button & MODIFIER_MASK);
} if ( ( modes.count (b) ) && ( modes[b] != NULL ) ) old_sticky = modes[b]->isSticky();
current_button |= button; current_button |= button;
if ( ( modes.count (current_button) ) && ( modes[current_button] != NULL ) ) { b=Button(current_button & MODIFIER_MASK);
new_sticky = modes[current_button]->isSticky(); if ( ( modes.count (b) ) && ( modes[b] != NULL ) ) new_sticky = modes[b]->isSticky();
}
if ( old_sticky || new_sticky) if ( !old_sticky && !new_sticky) SetCurrentAction();
return;
SetCurrentAction();
} }
void Trackball::ButtonUp(Trackball::Button button) { void Trackball::ButtonUp(Trackball::Button button) {
bool old_sticky=false, new_sticky=false; bool old_sticky=false, new_sticky=false;
assert ( modes.count (0) ); assert (modes.count (0));
if ( ( modes.count (current_button) ) && ( modes[current_button] != NULL ) ) {
old_sticky = modes[current_button]->isSticky(); Button b=Button(current_button & MODIFIER_MASK);
} if ( ( modes.count (b) ) && ( modes[b] != NULL ) ) old_sticky = modes[b]->isSticky();
current_button &= (~button); current_button &= (~button);
if ( ( modes.count (current_button) ) && ( modes[current_button] != NULL ) ) { b=Button(current_button & MODIFIER_MASK);
new_sticky = modes[current_button]->isSticky(); if ( ( modes.count (b) ) && ( modes[b] != NULL ) ) new_sticky = modes[b]->isSticky();
}
if ( old_sticky || new_sticky) if ( !old_sticky && !new_sticky) SetCurrentAction();
return;
SetCurrentAction();
} }
void Trackball::Undo(){ void Trackball::Undo(){
@ -457,10 +492,10 @@ void Trackball::SetCurrentAction ()
{ {
//I use strict matching. //I use strict matching.
assert (modes.count (0)); assert (modes.count (0));
if (!modes.count (current_button)) { if (!modes.count (current_button & MODIFIER_MASK)) {
current_mode = NULL; current_mode = NULL;
} else { } else {
current_mode = modes[current_button]; current_mode = modes[current_button & MODIFIER_MASK];
if(current_mode != NULL) if(current_mode != NULL)
current_mode->SetAction(); current_mode->SetAction();
} }

View File

@ -80,6 +80,7 @@ Adding copyright.
#ifndef TRACKBALL_H #ifndef TRACKBALL_H
#define TRACKBALL_H #define TRACKBALL_H
#include <time.h>
#include <vcg/math/similarity.h> #include <vcg/math/similarity.h>
#include <vcg/space/color4.h> #include <vcg/space/color4.h>
#include <wrap/gui/view.h> #include <wrap/gui/view.h>
@ -165,13 +166,9 @@ mesh->Render();
*/ */
class Trackball: public Transform { class Trackball: public Transform {
public: public:
// the drawing code has been moved to the trackmodes
// class DrawingHint {
// DrawingHint DH;
/// The componibile states of the manipulator system. /// The componibile states of the manipulator system.
enum Button { BUTTON_NONE = 0x0000, ///< No mouse button pressed. enum Button { BUTTON_NONE = 0x0000, ///< No button or key pressed.
BUTTON_LEFT = 0x0001, ///< Left mouse button pressed. BUTTON_LEFT = 0x0001, ///< Left mouse button pressed.
BUTTON_MIDDLE = 0x0002, ///< Middle mouse button pressed. BUTTON_MIDDLE = 0x0002, ///< Middle mouse button pressed.
BUTTON_RIGHT = 0x0004, ///< Right mouse button pressed. BUTTON_RIGHT = 0x0004, ///< Right mouse button pressed.
@ -179,7 +176,14 @@ public:
KEY_SHIFT = 0x0010, ///< Shift key pressed. KEY_SHIFT = 0x0010, ///< Shift key pressed.
KEY_CTRL = 0x0020, ///< Ctrl key pressed. KEY_CTRL = 0x0020, ///< Ctrl key pressed.
KEY_ALT = 0x0040, ///< Alt key pressed. KEY_ALT = 0x0040, ///< Alt key pressed.
HANDLE = 0x0080 ///< Application-defined state activated. HANDLE = 0x0080, ///< Application-defined state activated.
MODIFIER_MASK = 0x00FF, ///< (mask to get modifiers only)
KEY_UP = 0x0100, ///< Up directional key
KEY_DOWN = 0x0200, ///< Down directional key
KEY_LEFT = 0x0400, ///< Left directional key
KEY_RIGHT = 0x0800, ///< Right directional key
KEY_PGUP = 0x1000, ///< PageUp directional key
KEY_PGDOWN = 0x2000, ///< PageDown directional key
}; };
/*! /*!
@ -331,7 +335,7 @@ public:
@param button the new state. @param button the new state.
*/ */
void ButtonDown(Button button); void ButtonDown(Button button, unsigned int msec=0);
/*! /*!
@brief Undo function for manipulator system. @brief Undo function for manipulator system.
@ -348,19 +352,23 @@ public:
*/ */
void SetSensitivity(float s); void SetSensitivity(float s);
//spinning interface
/*!
@brief Currently not in use.
@param on Currently not in use.
*/
void SetSpinnable(bool on); void SetSpinnable(bool on);
// returns if it is animating or not
//
bool IsAnimating(unsigned int msec=0);
// Animate: either takes an absolute time (if default not specified, then it is automeasured)
// or a fixed delta
void Animate(unsigned int msec=0);
/*! /*!
@brief Currently not in use. @brief Currently not in use.
@return A meaningless boolean value. @return A meaningless boolean value.
*/ */
bool IsSpinnable(); bool IsSpinnable();
/*! /*!
@brief Currently not in use. @brief Currently not in use.
@ -441,14 +449,16 @@ public:
This function is called automatically when an user action begins. This function is called automatically when an user action begins.
*/ */
void SetCurrentAction(); void SetCurrentAction();
/// Current state composition. /// Current state composition. Note: mask with MODIFIERS to get modifier buttons only
int current_button; int current_button;
/// The selected manipulator. /// The selected manipulator.
TrackMode *current_mode; TrackMode *current_mode;
/// The inactive manipulator. It is drawed when Trackball is inactive. /// The inactive manipulator. It is drawn when Trackball is inactive.
TrackMode *inactive_mode; TrackMode *inactive_mode;
// The manipulator to deal with timer events and key events
TrackMode *idle_and_keys_mode;
/*! /*!
@brief Reset modes to default mapping. @brief Reset modes to default mapping.
@ -482,6 +492,8 @@ public:
/// Currently not in use. /// Currently not in use.
int button_mask; int button_mask;
unsigned int last_time;
/// Currently not in use. /// Currently not in use.
Quaternionf spin; Quaternionf spin;
/// Currently not in use. /// Currently not in use.
@ -494,8 +506,17 @@ public:
/// Currently not in use. /// Currently not in use.
int history_size; int history_size;
void SetFixedTimesteps(bool mode){
fixedTimestepMode=mode;
}
/// Manipulators needs full access to this class. /// Manipulators needs full access to this class.
friend class TrackMode; friend class TrackMode;
private:
void Sync(unsigned int msec);
bool fixedTimestepMode; // if true, animations occurs at fixed time steps
}; };