00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "OpenGLVisualizerControl.h"
00047
00048 #include "Interfaces/MainWindowInterface.h"
00049 #include "OpenGLVisualizer.h"
00050
00051
00052
00053
00054
00055
00056
00057 COpenGLVisualizerControl::COpenGLVisualizerControl(WIDGET_HANDLE widget, int width, int height, COpenGLVisualizer *visualizer, CMainWindowInterface *main_window, CMainWindowEventInterface *event_callback)
00058 : m_widget(widget), m_visualizer(visualizer), m_main_window(main_window), m_event_callback(event_callback), m_width(width), m_height(height), m_mouse_mode(0)
00059 {
00060 ResetViewMatrix();
00061 }
00062
00063
00064
00065
00066
00067
00068 void COpenGLVisualizerControl::SetViewMatrix(const Transformation3d &transformation)
00069 {
00070 m_ViewMatrix = transformation;
00071
00072 if (m_visualizer)
00073 m_visualizer->SetViewMatrix(m_ViewMatrix);
00074 }
00075
00076 const Transformation3d &COpenGLVisualizerControl::GetViewMatrix()
00077 {
00078 return m_ViewMatrix;
00079 }
00080
00081 void COpenGLVisualizerControl::ResetViewMatrix()
00082 {
00083 m_ViewMatrix.rotation = Math3d::unit_mat;
00084 m_ViewMatrix.translation = Math3d::zero_vec;
00085
00086 if (m_visualizer)
00087 m_visualizer->SetViewMatrix(m_ViewMatrix);
00088 }
00089
00090
00091 void COpenGLVisualizerControl::RectSelected(WIDGET_HANDLE widget, int x0, int y0, int x1, int y1)
00092 {
00093 if (m_event_callback)
00094 m_event_callback->RectSelected(widget, x0, y0, x1, y1);
00095 }
00096
00097 void COpenGLVisualizerControl::PointClicked(WIDGET_HANDLE widget, int x, int y)
00098 {
00099 if (m_event_callback)
00100 m_event_callback->PointClicked(widget, x, y);
00101 }
00102
00103
00104 void COpenGLVisualizerControl::MouseDown(WIDGET_HANDLE widget, int button, int x, int y)
00105 {
00106 if (widget == m_widget)
00107 {
00108 int mod = 0;
00109 if (m_main_window)
00110 mod = m_main_window->GetModifierKeyState();
00111
00112 if (button == IVT_LEFT_BUTTON)
00113 {
00114 if (mod == 0)
00115 m_mouse_mode = 1;
00116 else if (mod & IVT_SHIFT_KEY)
00117 m_mouse_mode = 3;
00118 else if (mod & IVT_CONTROL_KEY || mod & IVT_ALT_KEY)
00119 m_mouse_mode = 4;
00120 }
00121 else if (button == IVT_RIGHT_BUTTON)
00122 {
00123 m_mouse_mode = 2;
00124 }
00125 else if (button == IVT_MIDDLE_BUTTON)
00126 {
00127 m_mouse_mode = 3;
00128 }
00129
00130 m_mouse_old_x = x;
00131 m_mouse_old_y = y;
00132
00133 return;
00134 }
00135
00136 if (m_event_callback)
00137 m_event_callback->MouseDown(widget, button, x, y);
00138 }
00139
00140 void COpenGLVisualizerControl::MouseUp(WIDGET_HANDLE widget, int button, int x, int y)
00141 {
00142 if (widget == m_widget)
00143 {
00144 m_mouse_mode = 0;
00145
00146 m_mouse_old_x = x;
00147 m_mouse_old_y = y;
00148
00149 return;
00150 }
00151
00152 if (m_event_callback)
00153 m_event_callback->MouseUp(widget, button, x, y);
00154 }
00155
00156 void COpenGLVisualizerControl::MouseMove(WIDGET_HANDLE widget, int x, int y)
00157 {
00158 if (widget == m_widget && m_mouse_mode != 0)
00159 {
00160 m_mouse_old_x = x;
00161 m_mouse_old_y = y;
00162
00163 return;
00164 }
00165
00166 if (m_event_callback)
00167 m_event_callback->MouseMove(widget, x, y);
00168 }
00169
00170 void COpenGLVisualizerControl::KeyDown(WIDGET_HANDLE widget, int key)
00171 {
00172 if (m_event_callback)
00173 m_event_callback->KeyDown(widget, key);
00174 }
00175
00176 void COpenGLVisualizerControl::KeyUp(WIDGET_HANDLE widget, int key)
00177 {
00178 if (m_event_callback)
00179 m_event_callback->KeyUp(widget, key);
00180 }
00181
00182
00183 void COpenGLVisualizerControl::ButtonPushed(WIDGET_HANDLE widget)
00184 {
00185 if (m_event_callback)
00186 m_event_callback->ButtonPushed(widget);
00187 }
00188
00189
00190
00191 void COpenGLVisualizerControl::ValueChanged(WIDGET_HANDLE widget, int value)
00192 {
00193 if (m_event_callback)
00194 m_event_callback->ValueChanged(widget, value);
00195 }
00196
00197 #define ROT_SPEED 0.1f
00198 #define TRANS_SPEED 100.0f
00199
00200 void COpenGLVisualizerControl::Update()
00201 {
00202 float fx = (float)(m_mouse_old_x - (m_width / 2)) / (float)(m_width / 2);
00203 float fy = (float)(m_mouse_old_y - (m_height / 2)) / (float)(m_height / 2);
00204
00205 if (m_mouse_mode == 1)
00206 {
00207 Mat3d m1, m2;
00208
00209 Math3d::SetRotationMatX(m1, ROT_SPEED * fy);
00210 Math3d::SetRotationMatY(m2, ROT_SPEED * -fx);
00211
00212 Math3d::MulMatMat(m1, m2, m1);
00213 Math3d::MulMatMat(m1, m_ViewMatrix.rotation, m_ViewMatrix.rotation);
00214
00215 if (m_visualizer)
00216 m_visualizer->SetViewMatrix(m_ViewMatrix);
00217 }
00218 else if (m_mouse_mode == 2)
00219 {
00220 m_ViewMatrix.translation.z += TRANS_SPEED * fy;
00221
00222 if (m_visualizer)
00223 m_visualizer->SetViewMatrix(m_ViewMatrix);
00224 }
00225 else if (m_mouse_mode == 3)
00226 {
00227 m_ViewMatrix.translation.x += TRANS_SPEED * fx;
00228 m_ViewMatrix.translation.y += TRANS_SPEED * fy;
00229
00230 if (m_visualizer)
00231 m_visualizer->SetViewMatrix(m_ViewMatrix);
00232 }
00233 else if (m_mouse_mode == 4)
00234 {
00235 Mat3d m1;
00236
00237 Math3d::SetRotationMatZ(m1, ROT_SPEED * fy);
00238
00239 Math3d::MulMatMat(m1, m_ViewMatrix.rotation, m_ViewMatrix.rotation);
00240
00241 if (m_visualizer)
00242 m_visualizer->SetViewMatrix(m_ViewMatrix);
00243 }
00244 }
00245
00246 #undef ROT_SPEED
00247 #undef TRANS_SPEED