QSDK 1.1 Documentation
Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

Listener Struct Reference

Inheritance diagram for Listener:

ApplicationListener KeyboardListener List of all members.

Public Member Functions

 Listener (const Q::Group &objGrp, const Q::Light &light)
void onTick (double time)
void onWindowClose ()
void onKeyPress (Q::Layer &l, unsigned int code)
void onKeyRelease (Q::Layer &l, unsigned int code)
void onKeyFocus (Q::Layer &l, bool gainedFocus)
void onKeyValue (Q::Layer &l, char character)

Private Types

enum  Move {
  Left = 0,
  Right,
  Forwards,
  Backwards,
  SpinLeft,
  SpinRight,
  SpinForwards,
  SpinBackwards,
  LastMove
}

Private Member Functions

void translate (const Maths::Vec3f &dp)
void rotate (const Maths::Vec3f &axis, float angle)
void setLightColor (const Maths::Color4b &)

Private Attributes

Q::Group objGrp_
Q::Light light_
int moves_ [LastMove]
double lastTime_

Static Private Attributes

const float translateRate_ = 1.0f
const float rotateRate_ = 45.0f

Detailed Description

Listener implements the ApplicationListener, for performing a per frame call and for closing down the application and a KeyboardListener to implement keyboard controls.

Definition at line 21 of file ptut02.cpp.


Member Enumeration Documentation

enum Listener::Move [private]
 

Enumeration values:
Left 
Right 
Forwards 
Backwards 
SpinLeft 
SpinRight 
SpinForwards 
SpinBackwards 
LastMove 

Definition at line 60 of file ptut02.cpp.

00060               {
00061         Left = 0,
00062         Right,
00063         Forwards,
00064         Backwards,
00065         SpinLeft,
00066         SpinRight,
00067         SpinForwards,
00068         SpinBackwards,
00069         LastMove
00070     };


Constructor & Destructor Documentation

Listener::Listener const Q::Group objGrp,
const Q::Light light
 

Store the Group so it can be moved and the Light so we can change its color.

Definition at line 89 of file ptut02.cpp.

References LastMove, lastTime_, and moves_.

00091     : objGrp_(objGrp),
00092       light_(light)
00093 {
00094     // Set all movement axes to non-moving
00095     for (unsigned int i=0; i<LastMove; ++i) {
00096         moves_[i] = 0;
00097     }
00098 
00099     // initialise the start time.
00100     lastTime_ = 0;
00101 }


Member Function Documentation

void Listener::onKeyFocus Q::Layer l,
bool  gainedFocus
[inline]
 

Definition at line 40 of file ptut02.cpp.

00041                                                      {}

void Listener::onKeyPress Q::Layer l,
unsigned int  code
 

For each key press that moves the group, just mark the fact that it has been pressed. For the other keys, that adjust the light color, call the functions immeadiately. This is so that key repeat can be implemented for the move controls.

Definition at line 110 of file ptut02.cpp.

References Application::app(), Backwards, Forwards, Left, moves_, Right, setLightColor(), SpinBackwards, SpinForwards, SpinLeft, and SpinRight.

00111 {
00112     switch (code) {
00113     case Draw::Key::q:
00114         Q::Application::app().shutdown();
00115         break;
00116 
00117     // Start Movement in a given axis
00118     case Draw::Key::Up:
00119         moves_[Forwards] = 1;
00120         break;
00121     case Draw::Key::Down:
00122         moves_[Backwards] = 1;
00123         break;
00124     case Draw::Key::Left:
00125         moves_[Left] = 1;
00126         break;
00127     case Draw::Key::Right:
00128         moves_[Right] = 1;
00129         break;
00130     case Draw::Key::a:
00131         moves_[SpinLeft] = 1;
00132         break;
00133     case Draw::Key::s:
00134         moves_[SpinRight] = 1;
00135         break;
00136     case Draw::Key::e:
00137         moves_[SpinForwards] = 1;
00138         break;
00139     case Draw::Key::d:
00140         moves_[SpinBackwards] = 1;
00141         break;
00142 
00143     // change the light color
00144     case Draw::Key::r:
00145         setLightColor(Maths::Color4b(255, 0, 0));
00146         break;
00147     case Draw::Key::g:
00148         setLightColor(Maths::Color4b(0, 255, 0));
00149         break;
00150     case Draw::Key::b:
00151         setLightColor(Maths::Color4b(0, 0, 255));
00152         break;
00153     case Draw::Key::w:
00154         setLightColor(Maths::Color4b(255, 255, 255));
00155         break;
00156     }
00157 }

void Listener::onKeyRelease Q::Layer l,
unsigned int  code
 

Only the move controls need to be detected for key release, so that we can unmark them from the list of pressed keys.

Definition at line 164 of file ptut02.cpp.

References Backwards, Forwards, Left, moves_, Right, SpinBackwards, SpinForwards, SpinLeft, and SpinRight.

00165 {
00166     switch (code) {
00167     
00168     // Stop movement in a given axis
00169     case Draw::Key::Up:
00170         moves_[Forwards] = 0;
00171         break;
00172     case Draw::Key::Down:
00173         moves_[Backwards] = 0;
00174         break;
00175     case Draw::Key::Left:
00176         moves_[Left] = 0;
00177         break;
00178     case Draw::Key::Right:
00179         moves_[Right] = 0;
00180         break;
00181     case Draw::Key::a:
00182         moves_[SpinLeft] = 0;
00183         break;
00184     case Draw::Key::s:
00185         moves_[SpinRight] = 0;
00186         break;
00187     case Draw::Key::e:
00188         moves_[SpinForwards] = 0;
00189         break;
00190     case Draw::Key::d:
00191         moves_[SpinBackwards] = 0;
00192         break;
00193     }
00194 }

void Listener::onKeyValue Q::Layer l,
char  character
[inline]
 

Definition at line 42 of file ptut02.cpp.

00043                                                    {}

void Listener::onTick double  time  )  [virtual]
 

Process the pressed keys to move the group by an amount calculated by the time since the last call to onTick. Thus making the results independent of the frame rate.

Implements ApplicationListener.

Definition at line 202 of file ptut02.cpp.

References Backwards, Forwards, lastTime_, Left, moves_, Right, rotate(), rotateRate_, SpinBackwards, SpinForwards, SpinLeft, SpinRight, translate(), translateRate_, and Vec3f.

00203 {
00204     // calculate the time difference since the last tick.
00205     double dt = time - lastTime_;
00206     lastTime_ = time;
00207     
00208     // work out the change in position
00209     Maths::Vec3f dp = Maths::Vec3f(0, 0, 0);
00210     dp += Maths::Vec3f(1, 0, 0) * (float)(moves_[Right]-moves_[Left]);
00211     dp += Maths::Vec3f(0, 0, 1) * (float)(moves_[Forwards]-moves_[Backwards]);
00212     dp *= translateRate_ * (float)dt;
00213     translate(dp);
00214 
00215     // and the change in angle
00216     float spinConv = rotateRate_ / 180.0f * Maths::PI * (float)dt;
00217     float dtheta = (moves_[SpinLeft]-moves_[SpinRight]) * spinConv;
00218     rotate(Maths::Vec3f(0, 1, 0), dtheta);
00219 
00220     dtheta = (moves_[SpinForwards]-moves_[SpinBackwards]) * spinConv;
00221     rotate(Maths::Vec3f(1, 0, 0), dtheta);
00222 }

void Listener::onWindowClose  )  [inline, virtual]
 

Implements ApplicationListener.

Definition at line 33 of file ptut02.cpp.

References Application::app().

00033 { Q::Application::app().shutdown(); }

void Listener::rotate const Maths::Vec3f axis,
float  angle
[private]
 

Rotate the cube angle degrees about the axis, in world space.

Definition at line 238 of file ptut02.cpp.

References objGrp_, Group::orientation(), Group::setOrientation(), and Group::zone().

Referenced by onTick().

00239 {
00240     Q::Group root = objGrp_.zone().root();
00241     Maths::Quat4f rot(axis, angle);
00242     objGrp_.setOrientation(root, rot * objGrp_.orientation(root)); 
00243 }

void Listener::setLightColor const Maths::Color4b col  )  [private]
 

Change the color of the stored light.

Definition at line 249 of file ptut02.cpp.

References light_, and Light::setColor().

Referenced by onKeyPress().

00250 {
00251     light_.setColor(col);
00252 }

void Listener::translate const Maths::Vec3f dp  )  [private]
 

Translate the cube in world space by position dp.

Definition at line 228 of file ptut02.cpp.

References objGrp_, Group::position(), Group::setPosition(), and Group::zone().

Referenced by onTick().

00229 {
00230     Q::Group root = objGrp_.zone().root();
00231     objGrp_.setPosition(root, objGrp_.position(root) + dp);
00232 }


Member Data Documentation

double Listener::lastTime_ [private]
 

The time of the previous frame.

Definition at line 80 of file ptut02.cpp.

Referenced by Listener(), and onTick().

Q::Light Listener::light_ [private]
 

The scene light.

Definition at line 58 of file ptut02.cpp.

Referenced by setLightColor().

int Listener::moves_[LastMove] [private]
 

The set of current actions.

Definition at line 75 of file ptut02.cpp.

Referenced by Listener(), onKeyPress(), onKeyRelease(), and onTick().

Q::Group Listener::objGrp_ [private]
 

The group that holds the cube.

Definition at line 53 of file ptut02.cpp.

Referenced by rotate(), and translate().

const float Listener::rotateRate_ = 45.0f [static, private]
 

Definition at line 87 of file ptut02.cpp.

Referenced by onTick().

const float Listener::translateRate_ = 1.0f [static, private]
 

Definition at line 86 of file ptut02.cpp.

Referenced by onTick().

Return to QSDK documentation Contents page. Contact details for support, information and fault-reporting.
Qube Software Limited © 2000-2004