00001
00017 #ifndef _TRANSFORMCONTROL_H
00018 #define _TRANSFORMCONTROL_H
00019
00020 #include <Transform.H>
00021 #include <UserInterface.H>
00022 #include <View.H>
00023 #include <Mouse.H>
00024
00025 namespace xchen
00026 {
00027 class UI::KeyBoard;
00028
00029 class UI::TransformControl
00030 {
00031 enum TransformMode {ROTATE, TRANSLATE };
00032 public:
00033 TransformControl();
00034
00035 int GenerateMenu(KeyBoard* kb);
00036
00037 void SetTransformMode(TransformMode mode) { rot_or_trans = mode; }
00038 void Reset();
00039 void ResetRotate();
00040 void MouseMove(int button, int dx, int dy);
00041
00042 const fVector3D& GetTranslate() const { return translate; }
00043 const Transform& GetRotateTransform() const { return rotate; }
00044
00046 void FrontView() { ResetRotate(); }
00047 void BackView () { ResetRotate(); rotateY(180.0); }
00048 void LeftView () { ResetRotate(); rotateY(90.0); }
00049 void RightView() { ResetRotate(); rotateY(-90.0); }
00050 void TopView () { ResetRotate(); rotateX(90.0); }
00051 void BottomView() { ResetRotate(); rotateX(-90.0); }
00053
00054 void FlipMicroTransform() { micro_ctrl = ! micro_ctrl; }
00055
00056 void IncMicroFactor();
00057 void DecMicroFactor();
00058
00059 private:
00060 fVector3D translate;
00061 Transform rotate;
00062
00063 int rot_or_trans;
00064
00065 double micro_factor;
00066 bool micro_ctrl;
00067
00068 void IncTranslateX(double dx) { translate[0] += micro_ctrl? micro_factor*dx : dx; }
00069 void IncTranslateY(double dy) { translate[1] += micro_ctrl? micro_factor*dy : dy; }
00070 void IncTranslateZ(double dz);
00071 void rotateX(double dx) { rotate.RotateX(micro_ctrl? micro_factor*dx : dx); }
00072 void rotateY(double dy) { rotate.RotateY(micro_ctrl? micro_factor*dy : dy); }
00073 void rotateZ(double dz) { rotate.RotateZ(micro_ctrl? micro_factor*dz : dz); }
00074
00075
00076 static TransformControl& _getTctrl()
00077 {
00078 return *(View :: GetInteractiveUI().trans);
00079 }
00080 static void set2translate_cb(ConvertedKeyValue, int, int) { _getTctrl().SetTransformMode(TRANSLATE); }
00081 static void set2rotate_cb(ConvertedKeyValue, int, int) { _getTctrl().SetTransformMode(ROTATE); }
00082
00083 static void set_left_view_cb(ConvertedKeyValue, int, int) { _getTctrl().LeftView(); glutPostRedisplay(); }
00084 static void set_back_view_cb(ConvertedKeyValue, int, int) { _getTctrl().BackView(); glutPostRedisplay(); }
00085 static void set_right_view_cb(ConvertedKeyValue, int, int) { _getTctrl().RightView(); glutPostRedisplay(); }
00086 static void set_front_view_cb(ConvertedKeyValue, int, int) { _getTctrl().FrontView(); glutPostRedisplay(); }
00087 static void set_top_view_cb(ConvertedKeyValue, int, int) { _getTctrl().TopView(); glutPostRedisplay(); }
00088 static void set_bottom_view_cb(ConvertedKeyValue, int, int) { _getTctrl().BottomView(); glutPostRedisplay(); }
00089 static void reset_rotate_cb(ConvertedKeyValue, int, int) { _getTctrl().ResetRotate(); glutPostRedisplay(); }
00090 static void reset_transform_cb(ConvertedKeyValue, int, int) { _getTctrl().Reset(); glutPostRedisplay(); }
00091
00092 static void flip_micro_ctrl_cb(ConvertedKeyValue, int, int) { _getTctrl().FlipMicroTransform(); glutPostRedisplay(); }
00093 static void dec_micro_factor_cb(ConvertedKeyValue, int, int) { _getTctrl().DecMicroFactor(); glutPostRedisplay(); }
00094 static void inc_micro_factor_cb(ConvertedKeyValue, int, int) { _getTctrl().IncMicroFactor(); glutPostRedisplay(); }
00095 };
00096
00097 inline void UI::TransformControl::Reset()
00098 {
00099 ResetRotate();
00100 translate = fVector3D(0.0);
00101 }
00102 inline void UI::TransformControl::ResetRotate()
00103 {
00104 rotate.SetIdentity();
00105 }
00106 inline void UI::TransformControl::MouseMove(int button, int dx, int dy)
00107 {
00108 if(button == Mouse::NoButton)
00109 return;
00110
00111 if(rot_or_trans == ROTATE) {
00112 if(button == Mouse::LeftButton)
00113 rotateY(.05 * (dx));
00114 else if(button == Mouse::MiddleButton)
00115 rotateX(.05 * (dy));
00116 else
00117 rotateZ(.05 * (dx));
00118 }
00119 else if(rot_or_trans == TRANSLATE) {
00120 if(button == Mouse::LeftButton){
00121 IncTranslateX(.001 * (dx));
00122 IncTranslateY(-.001 * (dy));
00123 }
00124 else
00125 IncTranslateZ(.001 * (dx));
00126 }
00127 }
00128 inline void UI::TransformControl::IncMicroFactor()
00129 {
00130 if(micro_factor > 0.002)
00131 micro_factor += 0.002;
00132 else
00133 micro_factor *= 1.5;
00134 }
00135 inline void UI::TransformControl::DecMicroFactor()
00136 {
00137 if(micro_factor > 0.002)
00138 micro_factor -= 0.002;
00139 else
00140 micro_factor *= 0.5;
00141 }
00142 inline void UI::TransformControl::IncTranslateZ(double dz)
00143 {
00144 translate[2] += dz;
00145 }
00146
00147 }
00148
00149
00150 #endif