00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef VMDVRPN
00025 #include "quat.h"
00026 #endif
00027
00028 #include "TextEvent.h"
00029 #include "P_UIVR.h"
00030 #include "Molecule.h"
00031 #include "MoleculeList.h"
00032 #include "P_CmdTool.h"
00033 #include "VMDApp.h"
00034 #include "CommandQueue.h"
00035 #include "Scene.h"
00036 #include "PickList.h"
00037 #include "P_Tool.h"
00038 #include "P_RotateTool.h"
00039 #include "P_JoystickTool.h"
00040 #include "P_GrabTool.h"
00041 #include "P_PrintTool.h"
00042 #include "P_TugTool.h"
00043 #include "SpringTool.h"
00044 #include "P_PinchTool.h"
00045 #include "MaterialList.h"
00046 #include "P_SensorConfig.h"
00047 #include "P_Tracker.h"
00048 #include "P_Feedback.h"
00049 #include "P_Buttons.h"
00050 #include "CmdLabel.h"
00051 #include "Inform.h"
00052
00053 #ifdef VMDVRPN
00054 #include "vrpn_Button.h"
00055 #include "vrpn_Tracker.h"
00056 #include "vrpn_ForceDevice.h"
00057 #endif
00058
00059 #include "P_Tracker.h"
00060 #include "P_Buttons.h"
00061 #include "P_Feedback.h"
00062
00063 #ifdef VMDCAVE
00064 #include "P_CaveButtons.h"
00065 #include "P_CaveTracker.h"
00066 #endif
00067
00068 #ifdef VMDFREEVR
00069 #include "P_FreeVRButtons.h"
00070 #include "P_FreeVRTracker.h"
00071 #endif
00072
00073 #ifdef VMDVRPN
00074 #include "P_VRPNTracker.h"
00075 #include "P_VRPNButtons.h"
00076 #include "P_VRPNFeedback.h"
00077 #endif
00078
00079 #ifdef WINGMAN
00080 #include "wingforce.h"
00081 #include "P_JoystickButtons.h"
00082 #endif
00083
00084 #include "SpaceballButtons.h"
00085 #include "SpaceballTracker.h"
00086
00087 UIVR::~UIVR() {
00088 int i;
00089 for (i=0;i<tools.num();i++)
00090 delete tools[i];
00091 for (i=0; i<trackerList.num(); i++)
00092 delete trackerList.data(i);
00093 for (i=0; i<feedbackList.num(); i++)
00094 delete feedbackList.data(i);
00095 for (i=0; i<buttonList.num(); i++)
00096 delete buttonList.data(i);
00097
00098 }
00099
00100 UIVR::UIVR(VMDApp *vmdapp) : UIObject(vmdapp) {
00101 command_wanted(Command::TOOL_OFFSET);
00102 command_wanted(Command::TOOL_REP);
00103 command_wanted(Command::TOOL_ADD_DEVICE);
00104 command_wanted(Command::TOOL_DELETE_DEVICE);
00105 command_wanted(Command::TOOL_CALLBACK);
00106
00107 tool_serialno = 0;
00108
00109 tool_types.add_name("grab", 0);
00110 tool_types.add_name("rotate", 1);
00111 tool_types.add_name("joystick", 2);
00112 tool_types.add_name("tug", 3);
00113 tool_types.add_name("pinch", 4);
00114 tool_types.add_name("spring", 5);
00115 tool_types.add_name("print", 6);
00116
00117 update_device_lists();
00118 reset();
00119 }
00120
00121 Tool *UIVR::create_tool(const char *type) {
00122 Displayable *parent = &(app->scene->root);
00123 Tool *newtool = NULL;
00124 if(!strupncmp(type, "grab", 10))
00125 newtool = new GrabTool(tool_serialno++, app, parent);
00126 else if(!strupncmp(type, "joystick", 10))
00127 newtool = new JoystickTool(tool_serialno++, app, parent);
00128 else if(!strupncmp(type, "tug", 10))
00129 newtool = new TugTool(tool_serialno++,app, parent);
00130 else if(!strupncmp(type, "pinch", 10))
00131 newtool = new PinchTool(tool_serialno++,app, parent);
00132 else if(!strupncmp(type, "spring", 10))
00133 newtool = new SpringTool(tool_serialno++,app, parent);
00134 else if(!strupncmp(type, "print", 10))
00135 newtool = new PrintTool(tool_serialno++, app, parent);
00136
00137 #ifdef VMDVRPN
00138
00139 else if(!strupncmp(type, "rotate", 10))
00140 newtool = new RotateTool(tool_serialno++,app, parent);
00141 #endif
00142 else {
00143 msgErr << "Unrecognized tool type " << type << sendmsg;
00144 msgErr << "possiblities are:";
00145 for(int i=0;i<num_tool_types();i++) msgErr << " " << tool_types.name(i);
00146 msgErr << sendmsg;
00147 return NULL;
00148 }
00149 newtool->On();
00150 newtool->grabs = 0;
00151 return newtool;
00152 }
00153
00154 int UIVR::add_tool(const char *type) {
00155 Tool *newtool = create_tool(type);
00156 if (!newtool) return -1;
00157 tools.append(newtool);
00158 return tools.num()-1;
00159 }
00160
00161 int UIVR::add_tool_with_USL(const char *type, int argc, const char **USL) {
00162 int num = add_tool(type);
00163 if(num==-1) return FALSE;
00164 Tool *tool = tools[num];
00165 for (int i=0; i<argc; i++)
00166 add_device_to_tool(USL[i], tool);
00167 return TRUE;
00168 }
00169
00170 int UIVR::change_type(int toolnum, const char *type) {
00171 if (toolnum < 0 || toolnum >= tools.num()) return FALSE;
00172 Tool *newtool = create_tool(type);
00173 if (!newtool) return FALSE;
00174 Tool *oldtool = tools[toolnum];
00175 newtool->steal_sensor(oldtool);
00176 delete oldtool;
00177 tools[toolnum] = newtool;
00178 return TRUE;
00179 }
00180
00181 int UIVR::remove_tool(int i) {
00182 if(i<0 || i >= tools.num()) return FALSE;
00183 Tool *deadtool = tools[i];
00184 delete deadtool;
00185 tools.remove(i);
00186 return TRUE;
00187 }
00188
00189 int UIVR::check_event() {
00190
00191
00192
00193
00194 int i;
00195 for(i=0;i<tools.num();i++) {
00196
00197 if(!tools[i]->alive()) {
00198
00199 remove_tool(i);
00200 i--;
00201 continue;
00202 }
00203
00204 if(tools[i]->orientation()==NULL)
00205 continue;
00206
00207
00208 if(tools[i]->isgrabbing()) {
00209 tools[i]->dograb();
00210 tools[i]->grabs = 1;
00211 } else {
00212 if(tools[i]->grabs) tools[i]->ungrab();
00213 tools[i]->grabs = 0;
00214 }
00215 }
00216
00217
00218 return TRUE;
00219 }
00220
00221 Tool *UIVR::gettool(int i) {
00222 if(i<0) return NULL;
00223 if(i>=tools.num()) return NULL;
00224 return tools[i];
00225 }
00226
00227 int UIVR::set_position_scale(int toolnum, float newval) {
00228 if (newval >= 0) {
00229 Tool *tool = gettool(toolnum);
00230 if (tool) {
00231 tool->setscale(newval);
00232 return TRUE;
00233 }
00234 }
00235 return FALSE;
00236 }
00237 int UIVR::set_force_scale(int toolnum, float newval) {
00238 if (newval >= 0) {
00239 Tool *tool = gettool(toolnum);
00240 if (tool) {
00241 tool->setforcescale(newval);
00242 return TRUE;
00243 }
00244 }
00245 return FALSE;
00246 }
00247 int UIVR::set_spring_scale(int toolnum, float newval) {
00248 if (newval >= 0) {
00249 Tool *tool = gettool(toolnum);
00250 if (tool) {
00251 tool->setspringscale(newval);
00252 return TRUE;
00253 }
00254 }
00255 return FALSE;
00256 }
00257
00258 int UIVR::act_on_command(int type, Command *c) {
00259 switch(type) {
00260 default: return FALSE;
00261 case Command::TOOL_OFFSET:
00262 {
00263 Tool *tool = gettool(((CmdToolScaleSpring *)c)->num);
00264 if (!tool) return FALSE;
00265 tool->setoffset(((CmdToolOffset *)c)->offset);
00266 }
00267 break;
00268 case Command::TOOL_ADD_DEVICE:
00269 {
00270 CmdToolAddDevice *cmd = (CmdToolAddDevice *)c;
00271 Tool *tool = gettool(cmd->num);
00272 return add_device_to_tool(cmd->name, tool);
00273 }
00274 break;
00275 case Command::TOOL_DELETE_DEVICE:
00276 {
00277 CmdToolAddDevice *cmd = (CmdToolAddDevice *)c;
00278 Tool *tool = gettool(cmd->num);
00279 if (!tool) return FALSE;
00280 if (!cmd->name) return FALSE;
00281 tool->remove_device(cmd->name);
00282 }
00283 break;
00284 case Command::TOOL_CALLBACK:
00285 {
00286 CmdToolCallback *cmd = (CmdToolCallback *)c;
00287 set_callbacks(cmd->on);
00288 break;
00289 }
00290 case Command::TOOL_REP:
00291 {
00292 CmdToolRep *cmd = (CmdToolRep *)c;
00293 Tool *tool = gettool(cmd->toolnum);
00294 if (!tool) return FALSE;
00295 if (cmd->molid < 0 || cmd->repnum < 0) {
00296 tool->clear_rep();
00297 } else {
00298 tool->assign_rep(cmd->molid, cmd->repnum);
00299 }
00300 }
00301 break;
00302 }
00303 return TRUE;
00304 }
00305
00306
00307 int UIVR::add_device_to_tool(const char *device, Tool *tool) {
00308
00309 if (!tool) return FALSE;
00310 if (!device) return FALSE;
00311
00312
00313
00314 SensorConfig config(device);
00315 if (!config.getUSL()[0]) return FALSE;
00316
00317
00318
00319
00320
00321
00322 const char *devtype = config.gettype();
00323 if (trackerList.typecode(devtype) >= 0) {
00324 tool->add_tracker(trackerList.data(devtype)->clone(), &config);
00325 } else if (feedbackList.typecode(devtype) >= 0) {
00326 tool->add_feedback(feedbackList.data(devtype)->clone(), &config);
00327 } else if (buttonList.typecode(devtype) >= 0) {
00328 tool->add_buttons(buttonList.data(devtype)->clone(), &config);
00329 } else {
00330 msgErr << "Device '" << device << "' of type '" << devtype << "' not available." << sendmsg;
00331 }
00332 return TRUE;
00333 }
00334
00335 int UIVR::set_tracker(int toolnum, const char *device) {
00336 Tool *tool = gettool(toolnum);
00337 if (!tool) return FALSE;
00338 if (device == NULL) {
00339 tool->add_tracker(NULL, NULL);
00340 return TRUE;
00341 }
00342 return add_device_to_tool(device, tool);
00343 }
00344 int UIVR::set_feedback(int toolnum, const char *device) {
00345 Tool *tool = gettool(toolnum);
00346 if (!tool) return FALSE;
00347 if (device == NULL) {
00348 tool->add_feedback(NULL, NULL);
00349 return TRUE;
00350 }
00351 return add_device_to_tool(device, tool);
00352 }
00353 int UIVR::set_buttons(int toolnum, const char *device) {
00354 Tool *tool = gettool(toolnum);
00355 if (!tool) return FALSE;
00356 if (device == NULL) {
00357 tool->add_buttons(NULL, NULL);
00358 return TRUE;
00359 }
00360 return add_device_to_tool(device, tool);
00361 }
00362
00363 ResizeArray<JString *> *UIVR::get_device_names() {
00364 return SensorConfig::getnames();
00365 }
00366
00367 template<class T>
00368 ResizeArray<JString *> *generic_get_names(const T &devList) {
00369
00370 ResizeArray<JString *> *names = SensorConfig::getnames();
00371 ResizeArray<JString *> *devnames = new ResizeArray<JString *>;
00372 for (int i=0; i<names->num(); i++) {
00373 JString *jstr = (*names)[i];
00374 SensorConfig config(*jstr);
00375 const char *type = config.gettype();
00376 if (devList.typecode(type) >= 0)
00377 devnames->append(jstr);
00378 else
00379 delete jstr;
00380 }
00381 delete names;
00382 return devnames;
00383 }
00384
00385 ResizeArray<JString *> *UIVR::get_tracker_names() {
00386 return generic_get_names(trackerList);
00387 }
00388 ResizeArray<JString *> *UIVR::get_feedback_names() {
00389 return generic_get_names(feedbackList);
00390 }
00391 ResizeArray<JString *> *UIVR::get_button_names() {
00392 return generic_get_names(buttonList);
00393 }
00394
00395 void UIVR::update_device_lists() {
00396 VMDTracker *tracker = NULL;
00397 Buttons *buttons = NULL;
00398
00399 tracker = new SpaceballTracker(app);
00400 trackerList.add_name(tracker->device_name(), tracker);
00401 buttons = new SpaceballButtons(app);
00402 buttonList.add_name(buttons->device_name(), buttons);
00403
00404 #ifdef VMDVRPN
00405 Feedback *feedback = NULL;
00406 tracker = new VRPNTracker;
00407 trackerList.add_name(tracker->device_name(), tracker);
00408 feedback = new VRPNFeedback;
00409 feedbackList.add_name(feedback->device_name(), feedback);
00410 buttons = new VRPNButtons;
00411 buttonList.add_name(buttons->device_name(), buttons);
00412 #endif
00413
00414 #ifdef VMDCAVE
00415 tracker = new CaveTracker;
00416 trackerList.add_name(tracker->device_name(), tracker);
00417 buttons = new CaveButtons;
00418 buttonList.add_name(buttons->device_name(), buttons);
00419 #endif
00420
00421 #ifdef VMDFREEVR
00422 tracker = new FreeVRTracker;
00423 trackerList.add_name(tracker->device_name(), tracker);
00424 buttons = new FreeVRButtons;
00425 buttonList.add_name(buttons->device_name(), buttons);
00426 #endif
00427
00428 #ifdef WINGMAN
00429 buttons = new JoystickButtons;
00430 buttonList.add_name(buttons->device_name(), buttons);
00431 #endif
00432 }