Showing the changes to interface/src/scripting/ControllerScriptingInterface.cpp (1 of 44 files). Show all
Unified · Split
| @@ -0,0 +1,257 @@ | ||
| 1 | +// | |
| 2 | +// ControllerScriptingInterface.h | |
| 3 | +// hifi | |
| 4 | +// | |
| 5 | +// Created by Brad Hefta-Gaub on 12/17/13 | |
| 6 | +// Copyright (c) 2013 HighFidelity, Inc. All rights reserved. | |
| 7 | +// | |
| 8 | + | |
| 9 | +#include <HandData.h> | |
| 10 | +#include "Application.h" | |
| 11 | +#include "devices/SixenseManager.h" | |
| 12 | +#include "ControllerScriptingInterface.h" | |
| 13 | + | |
| 14 | +ControllerScriptingInterface::ControllerScriptingInterface() : | |
| 15 | + _mouseCaptured(false), | |
| 16 | + _touchCaptured(false), | |
| 17 | + _wheelCaptured(false) | |
| 18 | +{ | |
| 19 | +} | |
| 20 | + | |
| 21 | + | |
| 22 | +const PalmData* ControllerScriptingInterface::getPrimaryPalm() const { | |
| 23 | + int leftPalmIndex, rightPalmIndex; | |
| 24 | + | |
| 25 | + const HandData* handData = Application::getInstance()->getAvatar()->getHandData(); | |
| 26 | + handData->getLeftRightPalmIndices(leftPalmIndex, rightPalmIndex); | |
| 27 | + | |
| 28 | + if (rightPalmIndex != -1) { | |
| 29 | + return &handData->getPalms()[rightPalmIndex]; | |
| 30 | + } | |
| 31 | + | |
| 32 | + return NULL; | |
| 33 | +} | |
| 34 | + | |
| 35 | +int ControllerScriptingInterface::getNumberOfActivePalms() const { | |
| 36 | + const HandData* handData = Application::getInstance()->getAvatar()->getHandData(); | |
| 37 | + int numberOfPalms = handData->getNumPalms(); | |
| 38 | + int numberOfActivePalms = 0; | |
| 39 | + for (int i = 0; i < numberOfPalms; i++) { | |
| 40 | + if (getPalm(i)->isActive()) { | |
| 41 | + numberOfActivePalms++; | |
| 42 | + } | |
| 43 | + } | |
| 44 | + return numberOfActivePalms; | |
| 45 | +} | |
| 46 | + | |
| 47 | +const PalmData* ControllerScriptingInterface::getPalm(int palmIndex) const { | |
| 48 | + const HandData* handData = Application::getInstance()->getAvatar()->getHandData(); | |
| 49 | + return &handData->getPalms()[palmIndex]; | |
| 50 | +} | |
| 51 | + | |
| 52 | +const PalmData* ControllerScriptingInterface::getActivePalm(int palmIndex) const { | |
| 53 | + const HandData* handData = Application::getInstance()->getAvatar()->getHandData(); | |
| 54 | + int numberOfPalms = handData->getNumPalms(); | |
| 55 | + int numberOfActivePalms = 0; | |
| 56 | + for (int i = 0; i < numberOfPalms; i++) { | |
| 57 | + if (getPalm(i)->isActive()) { | |
| 58 | + if (numberOfActivePalms == palmIndex) { | |
| 59 | + return &handData->getPalms()[i]; | |
| 60 | + } | |
| 61 | + numberOfActivePalms++; | |
| 62 | + } | |
| 63 | + } | |
| 64 | + return NULL; | |
| 65 | +} | |
| 66 | + | |
| 67 | +bool ControllerScriptingInterface::isPrimaryButtonPressed() const { | |
| 68 | + const PalmData* primaryPalm = getPrimaryPalm(); | |
| 69 | + if (primaryPalm) { | |
| 70 | + if (primaryPalm->getControllerButtons() & BUTTON_FWD) { | |
| 71 | + return true; | |
| 72 | + } | |
| 73 | + } | |
| 74 | + | |
| 75 | + return false; | |
| 76 | +} | |
| 77 | + | |
| 78 | +glm::vec2 ControllerScriptingInterface::getPrimaryJoystickPosition() const { | |
| 79 | + const PalmData* primaryPalm = getPrimaryPalm(); | |
| 80 | + if (primaryPalm) { | |
| 81 | + return glm::vec2(primaryPalm->getJoystickX(), primaryPalm->getJoystickY()); | |
| 82 | + } | |
| 83 | + | |
| 84 | + return glm::vec2(0); | |
| 85 | +} | |
| 86 | + | |
| 87 | +int ControllerScriptingInterface::getNumberOfButtons() const { | |
| 88 | + return getNumberOfActivePalms() * NUMBER_OF_BUTTONS_PER_PALM; | |
| 89 | +} | |
| 90 | + | |
| 91 | +bool ControllerScriptingInterface::isButtonPressed(int buttonIndex) const { | |
| 92 | + int palmIndex = buttonIndex / NUMBER_OF_BUTTONS_PER_PALM; | |
| 93 | + int buttonOnPalm = buttonIndex % NUMBER_OF_BUTTONS_PER_PALM; | |
| 94 | + const PalmData* palmData = getActivePalm(palmIndex); | |
| 95 | + if (palmData) { | |
| 96 | + switch (buttonOnPalm) { | |
| 97 | + case 0: | |
| 98 | + return palmData->getControllerButtons() & BUTTON_0; | |
| 99 | + case 1: | |
| 100 | + return palmData->getControllerButtons() & BUTTON_1; | |
| 101 | + case 2: | |
| 102 | + return palmData->getControllerButtons() & BUTTON_2; | |
| 103 | + case 3: | |
| 104 | + return palmData->getControllerButtons() & BUTTON_3; | |
| 105 | + case 4: | |
| 106 | + return palmData->getControllerButtons() & BUTTON_4; | |
| 107 | + case 5: | |
| 108 | + return palmData->getControllerButtons() & BUTTON_FWD; | |
| 109 | + } | |
| 110 | + } | |
| 111 | + return false; | |
| 112 | +} | |
| 113 | + | |
| 114 | +int ControllerScriptingInterface::getNumberOfTriggers() const { | |
| 115 | + return getNumberOfActivePalms() * NUMBER_OF_TRIGGERS_PER_PALM; | |
| 116 | +} | |
| 117 | + | |
| 118 | +float ControllerScriptingInterface::getTriggerValue(int triggerIndex) const { | |
| 119 | + // we know there's one trigger per palm, so the triggerIndex is the palm Index | |
| 120 | + int palmIndex = triggerIndex; | |
| 121 | + const PalmData* palmData = getActivePalm(palmIndex); | |
| 122 | + if (palmData) { | |
| 123 | + return palmData->getTrigger(); | |
| 124 | + } | |
| 125 | + return 0.0f; | |
| 126 | +} | |
| 127 | + | |
| 128 | +int ControllerScriptingInterface::getNumberOfJoysticks() const { | |
| 129 | + return getNumberOfActivePalms() * NUMBER_OF_JOYSTICKS_PER_PALM; | |
| 130 | +} | |
| 131 | + | |
| 132 | +glm::vec2 ControllerScriptingInterface::getJoystickPosition(int joystickIndex) const { | |
| 133 | + // we know there's one joystick per palm, so the joystickIndex is the palm Index | |
| 134 | + int palmIndex = joystickIndex; | |
| 135 | + const PalmData* palmData = getActivePalm(palmIndex); | |
| 136 | + if (palmData) { | |
| 137 | + return glm::vec2(palmData->getJoystickX(), palmData->getJoystickY()); | |
| 138 | + } | |
| 139 | + return glm::vec2(0); | |
| 140 | +} | |
| 141 | + | |
| 142 | +int ControllerScriptingInterface::getNumberOfSpatialControls() const { | |
| 143 | + return getNumberOfActivePalms() * NUMBER_OF_SPATIALCONTROLS_PER_PALM; | |
| 144 | +} | |
| 145 | + | |
| 146 | +glm::vec3 ControllerScriptingInterface::getSpatialControlPosition(int controlIndex) const { | |
| 147 | + int palmIndex = controlIndex / NUMBER_OF_SPATIALCONTROLS_PER_PALM; | |
| 148 | + int controlOfPalm = controlIndex % NUMBER_OF_SPATIALCONTROLS_PER_PALM; | |
| 149 | + const PalmData* palmData = getActivePalm(palmIndex); | |
| 150 | + if (palmData) { | |
| 151 | + switch (controlOfPalm) { | |
| 152 | + case PALM_SPATIALCONTROL: | |
| 153 | + return palmData->getPosition(); | |
| 154 | + case TIP_SPATIALCONTROL: | |
| 155 | + return palmData->getTipPosition(); | |
| 156 | + } | |
| 157 | + } | |
| 158 | + return glm::vec3(0); // bad index | |
| 159 | +} | |
| 160 | + | |
| 161 | +glm::vec3 ControllerScriptingInterface::getSpatialControlVelocity(int controlIndex) const { | |
| 162 | + int palmIndex = controlIndex / NUMBER_OF_SPATIALCONTROLS_PER_PALM; | |
| 163 | + int controlOfPalm = controlIndex % NUMBER_OF_SPATIALCONTROLS_PER_PALM; | |
| 164 | + const PalmData* palmData = getActivePalm(palmIndex); | |
| 165 | + if (palmData) { | |
| 166 | + switch (controlOfPalm) { | |
| 167 | + case PALM_SPATIALCONTROL: | |
| 168 | + return palmData->getVelocity(); | |
| 169 | + case TIP_SPATIALCONTROL: | |
| 170 | + return palmData->getTipVelocity(); | |
| 171 | + } | |
| 172 | + } | |
| 173 | + return glm::vec3(0); // bad index | |
| 174 | +} | |
| 175 | + | |
| 176 | +glm::quat ControllerScriptingInterface::getSpatialControlRawRotation(int controlIndex) const { | |
| 177 | + int palmIndex = controlIndex / NUMBER_OF_SPATIALCONTROLS_PER_PALM; | |
| 178 | + int controlOfPalm = controlIndex % NUMBER_OF_SPATIALCONTROLS_PER_PALM; | |
| 179 | + const PalmData* palmData = getActivePalm(palmIndex); | |
| 180 | + if (palmData) { | |
| 181 | + switch (controlOfPalm) { | |
| 182 | + case PALM_SPATIALCONTROL: | |
| 183 | + return palmData->getRawRotation(); | |
| 184 | + case TIP_SPATIALCONTROL: | |
| 185 | + return palmData->getRawRotation(); // currently the tip doesn't have a unique rotation, use the palm rotation | |
| 186 | + } | |
| 187 | + } | |
| 188 | + return glm::quat(); // bad index | |
| 189 | +} | |
| 190 | + | |
| 191 | +glm::vec3 ControllerScriptingInterface::getSpatialControlNormal(int controlIndex) const { | |
| 192 | + int palmIndex = controlIndex / NUMBER_OF_SPATIALCONTROLS_PER_PALM; | |
| 193 | + int controlOfPalm = controlIndex % NUMBER_OF_SPATIALCONTROLS_PER_PALM; | |
| 194 | + const PalmData* palmData = getActivePalm(palmIndex); | |
| 195 | + if (palmData) { | |
| 196 | + switch (controlOfPalm) { | |
| 197 | + case PALM_SPATIALCONTROL: | |
| 198 | + return palmData->getNormal(); | |
| 199 | + case TIP_SPATIALCONTROL: | |
| 200 | + return palmData->getNormal(); // currently the tip doesn't have a unique normal, use the palm normal | |
| 201 | + } | |
| 202 | + } | |
| 203 | + return glm::vec3(0); // bad index | |
| 204 | +} | |
| 205 | + | |
| 206 | +bool ControllerScriptingInterface::isKeyCaptured(QKeyEvent* event) const { | |
| 207 | + return isKeyCaptured(KeyEvent(*event)); | |
| 208 | +} | |
| 209 | + | |
| 210 | +bool ControllerScriptingInterface::isKeyCaptured(const KeyEvent& event) const { | |
| 211 | + // if we've captured some combination of this key it will be in the map | |
| 212 | + if (_capturedKeys.contains(event.key, event)) { | |
| 213 | + return true; | |
| 214 | + } | |
| 215 | + return false; | |
| 216 | +} | |
| 217 | + | |
| 218 | +void ControllerScriptingInterface::captureKeyEvents(const KeyEvent& event) { | |
| 219 | + // if it's valid | |
| 220 | + if (event.isValid) { | |
| 221 | + // and not already captured | |
| 222 | + if (!isKeyCaptured(event)) { | |
| 223 | + // then add this KeyEvent record to the captured combos for this key | |
| 224 | + _capturedKeys.insert(event.key, event); | |
| 225 | + } | |
| 226 | + } | |
| 227 | +} | |
| 228 | + | |
| 229 | +void ControllerScriptingInterface::releaseKeyEvents(const KeyEvent& event) { | |
| 230 | + if (event.isValid) { | |
| 231 | + // and not already captured | |
| 232 | + if (isKeyCaptured(event)) { | |
| 233 | + _capturedKeys.remove(event.key, event); | |
| 234 | + } | |
| 235 | + } | |
| 236 | +} | |
| 237 | + | |
| 238 | +bool ControllerScriptingInterface::isJoystickCaptured(int joystickIndex) const { | |
| 239 | + return _capturedJoysticks.contains(joystickIndex); | |
| 240 | +} | |
| 241 | + | |
| 242 | +void ControllerScriptingInterface::captureJoystick(int joystickIndex) { | |
| 243 | + if (!isJoystickCaptured(joystickIndex)) { | |
| 244 | + _capturedJoysticks.insert(joystickIndex); | |
| 245 | + } | |
| 246 | +} | |
| 247 | + | |
| 248 | +void ControllerScriptingInterface::releaseJoystick(int joystickIndex) { | |
| 249 | + if (isJoystickCaptured(joystickIndex)) { | |
| 250 | + _capturedJoysticks.remove(joystickIndex); | |
| 251 | + } | |
| 252 | +} | |
| 253 | + | |
| 254 | +glm::vec2 ControllerScriptingInterface::getViewportDimensions() const { | |
| 255 | + QGLWidget* widget = Application::getInstance()->getGLWidget(); | |
| 256 | + return glm::vec2(widget->width(), widget->height()); | |
| 257 | +} | |