00001 #include <wx/wx.h>
00002 #include <iostream.h>
00003 #include "vuBasicUtility.h"
00004 #include "../vuMainWindow.h"
00005 #include "../wxUIElements/vuKeyFramerDialog/vuKeyFramerDialog.h"
00006 #include "../wxUIElements/vuTransferDialog.h"
00007 #include "vuArcBall.h"
00008
00009
00010
00011
00012
00013 BEGIN_EVENT_TABLE(vuBasicGLCanvas, vuGLCanvas)
00014 EVT_MOUSE_EVENTS(vuBasicGLCanvas::OnMouse)
00015 EVT_CHAR(vuBasicGLCanvas::OnChar)
00016 END_EVENT_TABLE();
00017
00018
00019 vuBasicGLCanvas::vuBasicGLCanvas(vuBasicUtility *parent)
00020 : vuGLCanvas(parent), m_Parent(parent)
00021
00022 {
00023
00024 };
00025
00026 void vuBasicUtility::notifyDataChanged ()
00027
00028 {
00029 }
00030
00031 bool vuBasicGLCanvas::glInit()
00032 {
00033 return m_Parent->glInit();
00034 }
00035
00036 void vuBasicGLCanvas::render()
00037 {
00038 m_Parent->glRender();
00039 };
00040
00041 void vuBasicGLCanvas::resize()
00042 {
00043 m_Parent->glResize();
00044 };
00045
00046 void vuBasicGLCanvas::OnMouse(wxMouseEvent &ev)
00047 {
00048 m_Parent->glOnMouse(ev);
00049 };
00050
00051 void vuBasicGLCanvas::OnChar(wxKeyEvent& event)
00052 {
00053 m_Parent->OnChar(event);
00054 }
00055
00056
00057
00058
00059
00060 vuBasicUtility::vuBasicUtility() : m_helpPanel(this)
00061 {
00062 m_Main = 0;
00063 m_glCanvas = NULL;
00064 m_camera = NULL;
00065 m_DrawPreview = true;
00066 m_keyframer = NULL;
00067 }
00068
00069
00070
00071
00072
00073 vuBasicUtility::~vuBasicUtility()
00074 {
00075
00076
00077
00078
00079
00080 if (m_Main!=0) m_Main->notifyClosed(this);
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 void vuBasicUtility::close()
00101 {
00102
00103 m_Main = NULL;
00104
00105 Close();
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 const vuBasicGLCanvas *vuBasicUtility::getCanvas()
00131 {
00132 return m_glCanvas;
00133 }
00134
00135
00136
00137
00138
00139 bool vuBasicUtility::init(vuMainWindow *main,const char* DataFile)
00140 {
00141 m_Main=main;
00142
00143
00144
00145
00146 bool success = Create((wxPARENTWINDOW*)m_Main,-1,"Utility",
00147 wxPoint(-1,-1),wxSize(-1,-1),wxDEFAULT_FRAME_STYLE
00148 |wxRESIZE_BORDER,"Utility");
00149
00150 if (!success) return false;
00151 Show(false);
00152
00153
00154
00155
00156 m_glCanvas = new vuBasicGLCanvas(this);
00157
00158
00159
00160 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
00161 wxBoxSizer *topSizer = new wxBoxSizer(wxHORIZONTAL);
00162 wxBoxSizer *midSizer = new wxBoxSizer(wxHORIZONTAL);
00163 wxBoxSizer *botSizer = new wxBoxSizer(wxHORIZONTAL);
00164 wxBoxSizer *lefSizer = new wxBoxSizer(wxVERTICAL);
00165 wxBoxSizer *cenSizer = new wxBoxSizer(wxVERTICAL);
00166 wxBoxSizer *rigSizer = new wxBoxSizer(wxVERTICAL);
00167
00168 m_glCanvas->SetSize(-1,-1,512,512);
00169 cenSizer->Add(m_glCanvas,1,wxEXPAND);
00170
00171 addTop(topSizer);
00172 addBottom(botSizer);
00173 addLeft(lefSizer);
00174 addRight(rigSizer);
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 midSizer->Add(lefSizer,0,wxGROW);
00185 midSizer->Add(cenSizer,1,wxEXPAND);
00186 midSizer->Add(rigSizer,0,wxGROW);
00187 mainSizer->Add(topSizer,0,wxGROW);
00188 mainSizer->Add(midSizer,1,wxEXPAND);
00189 mainSizer->Add(botSizer,0,wxGROW);
00190
00191 SetSizer(mainSizer);
00192 SetAutoLayout(true);
00193 mainSizer->SetSizeHints( this );
00194
00195
00196 success = init(DataFile);
00197 if (success) Show(true);
00198 return success;
00199 };
00200
00201 void vuBasicUtility::useOpenGL(bool yesorno)
00202 {
00203 if(m_glCanvas)
00204 if(yesorno)
00205 m_glCanvas->enableOpenGL();
00206 else
00207 m_glCanvas->disableOpenGL();
00208 }
00209
00210 bool vuBasicUtility::IsReRendering ()
00211
00212 {
00213 return false;
00214 }
00215
00216 void vuBasicUtility::setIsReRendering (bool isit)
00217 {
00218 }
00219
00220
00221
00222
00223
00224
00237 void vuBasicUtility::glOnMouse(wxMouseEvent &event)
00238 {
00239 if (event.LeftDown() || event.RightDown())
00240 storeMousePosition(event);
00241 else if (event.LeftIsDown() && event.Moving())
00242 onMouseLeftMoving(event);
00243 else if (event.RightIsDown() && event.Moving())
00244 onMouseRightMoving(event);
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 else
00255 onMouse(event);
00256 }
00257
00258
00260 void vuBasicUtility::onMouse(wxMouseEvent &event)
00261 {
00262
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00297 void vuBasicUtility::onMouseRightMoving(wxMouseEvent &event)
00298 {
00299 vu1 *volume = getVolume();
00300
00301 if (volume != NULL) {
00302 vuCamera *camera = volume->getCameraPtr();
00303
00304 if (camera) {
00305 float s = ((float)event.GetY() - m_MouseY)/1.0f;
00306 camera->translateXYZ(0.0f, 0.0f, -s);
00307 glResize();
00308 volume->setIsReRendering(true);
00309 m_glCanvas->redraw();
00310 }
00311 else
00312 cerr << "vuBasicUtility::onMouseRightMoving: camera is not set" << endl;
00313 }
00314
00315 storeMousePosition(event);
00316 }
00317
00321 void vuBasicUtility::onMouseLeftMoving(wxMouseEvent &event)
00322 {
00323 vu1 *volume = getVolume();
00324
00325 if (volume) {
00326 vuCamera *camera = volume->getCameraPtr();
00327
00328 if (camera) {
00329 vuVector t = camera->getPosition() - volume->getCenter();
00330 float d = t.norm();
00331 camera->translateXYZ(0.0f, 0.0f, d);
00332 t = camera->getPosition();
00333
00334 vuArcBall ball;
00335 ball.attachCamera(volume->getCamera());
00336 ball.setWinSize(int (camera->getWidth()), int (camera->getHeight()));
00337 ball.turn(m_MouseX, m_MouseY, event.GetX(), event.GetY());
00338 camera->translateXYZ(0.0f, 0.0f, -d);
00339 camera->init();
00340 volume->setIsReRendering(true);
00341 m_glCanvas->redraw();
00342 }
00343 else
00344 cerr << "vuBasicUtility::onMouseLeftMoving: camera is not set" << endl;
00345 }
00346
00347 storeMousePosition(event);
00348 }
00349
00351 void vuBasicUtility::storeMousePosition(wxMouseEvent &event)
00352 {
00353 m_MouseX = (int) event.GetX();
00354 m_MouseY = (int) event.GetY();
00355 }
00356
00357
00358
00359
00360
00361
00362
00376 void vuBasicUtility::OnChar(wxKeyEvent& event)
00377 {
00378 switch(event.GetKeyCode()) {
00379 case '?':
00380 onKeyboardHelp(event);
00381 break;
00382 case 'k':
00383 onKeyboardKeyframer(event);
00384 break;
00385 case 13: {
00386 SetStatusText("Rendering...");
00387 Refresh();
00388 vu1 *volume = getVolume();
00389 if (volume) volume->setIsReRendering(true);
00390 m_DrawPreview = false;
00391 m_glCanvas->redraw();
00392 break;
00393 }
00394 case 316:
00395 case 317:
00396 case 318:
00397 case 319:
00398 onKeyboardRotate(event);
00399 break;
00400 case '-':
00401 case '+':
00402 onKeyboardZoom(event);
00403 break;
00404 default:
00405 return onKeyboard(event);
00406 }
00407 }
00408
00410 void vuBasicUtility::onKeyboard(wxKeyEvent &event)
00411 {
00412
00413 }
00414
00416 void vuBasicUtility::onKeyboardHelp(wxKeyEvent& event)
00417 {
00418 m_helpPanel.setTitle("Help");
00419 m_helpPanel.setHelpText(helpText());
00420 m_helpPanel.Show(true);
00421 }
00422
00424 void vuBasicUtility::onKeyboardKeyframer(wxKeyEvent& event)
00425 {
00426 vu1 *volume = getVolume();
00427
00428 if (volume != NULL) {
00429 vuCamera *camera = volume->getCameraPtr();
00430
00431 if (camera != NULL) {
00432 if (m_keyframer == NULL) {
00433 m_keyframer = new vuKeyFramerDialog(this, camera, this);
00434 m_keyframer->setup(camera, this);
00435 }
00436 m_keyframer->Show(true);
00437 }
00438 }
00439 else {
00440 cerr << "Warning! No camera defined. Can't start the keyframer." << endl;
00441 }
00442 }
00443
00447 void vuBasicUtility::onKeyboardZoom(wxKeyEvent& event)
00448 {
00449 vu1 *volume = getVolume();
00450
00451 if (volume != NULL) {
00452 vuCamera *camera = volume->getCameraPtr();
00453
00454 if (camera) {
00455 float speed = (event.GetKeyCode() == '+') ? 10 : -10;
00456 camera->translateXYZ(0.0f, 0.0f, speed);
00457 glResize();
00458 m_glCanvas->redraw();
00459 }
00460 }
00461 }
00462
00466 void vuBasicUtility::onKeyboardRotate(wxKeyEvent& event)
00467 {
00468 vu1 *volume = getVolume();
00469
00470 if (volume) {
00471 vuCamera *camera = volume->getCameraPtr();
00472
00473 if (camera) {
00474 vuVector t = camera->getPosition() - volume->getCenter();
00475 float d = t.norm();
00476 camera->translateXYZ(0.0f, 0.0f, d);
00477 t = camera->getPosition();
00478
00479 float speed = (event.m_shiftDown) ? 0.2 : 2;
00480
00481 switch (event.GetKeyCode()) {
00482 case 316:
00483 camera->rotateAboutUp(speed);
00484 break;
00485 case 317:
00486 camera->rotateAboutRight(speed);
00487 break;
00488 case 318:
00489 camera->rotateAboutUp(-speed);
00490 break;
00491 case 319:
00492 camera->rotateAboutRight(-speed);
00493 break;
00494 }
00495
00496 volume->setIsReRendering();
00497 camera->translateXYZ(0.0f, 0.0f, -d);
00498 camera->init();
00499 m_glCanvas->redraw();
00500 }
00501 }
00502 }
00503
00504
00505
00506
00507
00508 void vuBasicUtility::glRender()
00509 {
00510 vu1 *volume = getVolume();
00511
00512 if (volume == NULL) {
00513 cerr << "Warning: Can't draw preview. ";
00514 cerr << "Probably the utility's getVolume() was not overwritten. ";
00515 cerr << endl;
00516 onRender();
00517 }
00518 else if(m_DrawPreview) {
00519 volume->preview();
00520 SetStatusText(wxString("Press '?' for Help!"));
00521 }
00522 else {
00523 onRender();
00524 m_DrawPreview = true;
00525 }
00526 };
00527
00528 void vuBasicUtility::onRender()
00529 {
00530
00531
00532
00533 }
00534
00535
00536
00537
00538
00539 wxString vuBasicUtility::helpText()
00540 {
00541 wxString str("");
00542
00543 str +=
00544 "\n Keyboard bindings:\n\n"
00545 "\t ?\t\t\t this help window\n"
00546 "\t k\t\t\t Keyframer\n"
00547 "\t +\t\t\t zoom in\n"
00548 "\t -\t\t\t zoom out\n"
00549 "\t enter\t\t\t render volume\n"
00550 "\t right\t\t\t rotate right\n"
00551 "\t left\t\t\t rotate left\n"
00552 "\t up\t\t rotate up\n"
00553 "\t down\t\t rotate down\n"
00554 "\t shift-right\t rotate slowly right\n"
00555 "\t shift-left\t\t rotate slowly left\n"
00556 "\t shift-up\t\t rotate slowly up\n"
00557 "\t shift-down\t rotate slowly down\n";
00558 return str;
00559 }
00560
00561
00562
00563
00564
00565 vu1 *vuBasicUtility::getVolume()
00566 {
00567 cerr << "Warning: the utility's getVolume() was not overwritten. " << endl;
00568 return NULL;
00569 }