My Project
AnnHandController.cpp
Go to the documentation of this file.
1 // This is an open source non-commercial project. Dear PVS-Studio, please check it.
2 // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3 
4 #include "AnnHandController.hpp"
5 #include "AnnGetter.hpp"
6 #include "AnnLogger.hpp"
7 #include "AnnException.hpp"
8 
9 using namespace Annwvyn;
10 
11 AnnHandController::AnnHandController(const std::string& type, Ogre::SceneNode* handNode, AnnHandControllerID controllerID, AnnHandControllerSide controllerSide) :
12  controllerTypeString(type),
13  controllerTypeHash(AnnGetStringUtility()->hash(type)),
14  id(controllerID),
15  side(controllerSide),
16  node(handNode),
17  grabbed(nullptr),
18  model(nullptr),
19  tracked(false),
20  trackedAngularSpeed(AnnVect3::ZERO),
21  trackedLinearSpeed(AnnVect3::ZERO),
22  invalidAxis("INVALID", 0),
23  gestureNotAvailableHash(AnnGetStringUtility()->hash(gestureNotAvailableString))
24 {
26 
27  std::cerr << "HandController ID : " << id << " created";
28  std::cerr << "For side : " << getSideAsString(side);
29  std::cerr << "Of type : " << controllerTypeString;
30 
31  //Let this variable not initialized
32  capabilites = None;
33 }
34 
36 {
37  if(s == leftHandController) return "Left Hand";
38  return "Right Hand";
39 }
40 
41 void AnnHandController::_attachModelItem(Ogre::Item* handModel)
42 {
43  if(model) node->detachObject(model);
44  model = handModel;
45  node->attachObject(model);
46 }
47 
49 {
50  Ogre::v1::MeshPtr v1;
51  Ogre::MeshPtr v2;
52  v2 = AnnGetGameObjectManager()->getAndConvertFromV1Mesh(name.c_str(), v1, v2);
53  auto oldModel = getHandModel();
54  _attachModelItem(AnnGetEngine()->getSceneManager()->createItem(v2));
55  if(oldModel)
56  {
57  AnnGetEngine()->getSceneManager()->destroyItem(oldModel);
58  }
59 }
60 
62 {
63  if(model) node->detachObject(model);
64  model = nullptr;
65 }
66 
68 {
69  return model;
70 }
71 
73 {
74  return node->getPosition();
75 }
76 
78 {
79  return node->getOrientation();
80 }
81 
83 {
84  return trackedAngularSpeed;
85 }
86 
88 {
89  return trackedLinearSpeed;
90 }
91 
93 {
94  return node->getOrientation() * AnnVect3::NEGATIVE_UNIT_Z;
95 }
96 
97 void AnnHandController::attachNode(Ogre::SceneNode* grabbedObject)
98 {
99  if(grabbedObject->getParentSceneNode())
100  grabbedObject->getParentSceneNode()->removeChild(grabbedObject);
101  node->addChild(grabbedObject);
102  grabbed = grabbedObject;
103 }
104 
106 {
107  tracked = true;
108  node->setPosition(position);
109 }
110 
112 {
113  tracked = true;
114  node->setOrientation(static_cast<Ogre::Quaternion>(orientation));
115 }
116 
118 {
119  tracked = true;
121 }
122 
124 {
125  tracked = true;
127 }
128 
130 {
131  node->setVisible(!AnnGetVRRenderer()->shouldHideHands());
132 }
133 
135 {
136  return tracked;
137 }
138 
139 bool AnnHandController::getButtonState(uint8_t buttonIndex)
140 {
141  return buttonsState[buttonIndex] != 0;
142 }
143 
145 {
146  return buttonsState.size();
147 }
148 
149 bool AnnHandController::hasBeenPressed(uint8_t buttonIndex)
150 {
151  for(auto button : pressedButtons)
152  if(button == buttonIndex) return true;
153  return false;
154 }
155 
156 bool AnnHandController::hasBeenReleased(uint8_t buttonIndex)
157 {
158  for(auto button : releasedButtons)
159  if(button == buttonIndex) return true;
160  return false;
161 }
162 
164 {
165  return axes.size();
166 }
167 
169 {
170  if(index < axes.size()) return axes[index];
171  return invalidAxis;
172 }
173 
175 {
176  return side;
177 }
178 
180 {
181  return controllerTypeString;
182 }
183 
185 {
186  return axes;
187 }
188 
190 {
191  return buttonsState;
192 }
193 
195 {
196  return pressedButtons;
197 }
198 
200 {
201  return releasedButtons;
202 }
203 
204 AnnHandControllerAxis::AnnHandControllerAxis(const std::string& AxisName, float normalizedValue) :
205  name(AxisName),
206  value(0)
207 {
208  updateValue(normalizedValue);
209 }
210 
212 float AnnHandControllerAxis::getValue() const { return value; }
213 
214 void AnnHandControllerAxis::updateValue(float normalizedValue)
215 {
216  if(isInRange(normalizedValue))
217  value = normalizedValue;
218 }
219 
220 bool AnnHandControllerAxis::isInRange(float v) { return (v >= -1 && v <= 1); }
221 
223 {
224  return controllerTypeHash;
225 }
226 
228 {
229  AnnDebug() << "rumbleStart not specialized on this controller...";
230 }
231 
233 {
234  AnnDebug() << "rumbleStop not specialized on this controller...";
235 }
236 
238 {
239  return capabilites;
240 }
241 
243 {
245 }
246 
248 {
250 }
std::string controllerTypeString
Type of the controller, Can be string like "Vive controller" or "Oculus Touch Controller".
AnnVect3 getPointingDirection() const
Get a vector aligned with the pointing direction.
virtual void rumbleStart(float factor)
Start vibrating the controller with a set amount of "strength". Result will vary between VR systems....
void setTrackedOrientation(AnnQuaternion orientation)
Set the orientation of the hand.
bool isTracked() const
Return true if the hand controller object has revived updates from the tracking system.
virtual void rumbleStop()
Stop vibrating, if the controller was vibrating in the first place...
AnnHandControllerSide side
Side of the controller, right hand? left hand? Who knows!
AnnEngine * AnnGetEngine()
Get the current instance of AnnEngine.
Definition: AnnGetter.cpp:10
static std::string getSideAsString(AnnHandControllerSide s)
Get the side type as a std::string.
const AnnHandControllerGestureHash gestureNotAvailableHash
The hash of the "N/A" string.
float value
Value of the axis.
std::vector< uint8_t > pressedButtons
buttons that has been pressed or released
HandControllerCapabilites_t getCapabilities() const
Get the capabilities (bit test again the capability flags)
void updateVisibility() const
Check if controller is visible or not.
AnnHandControllerSide getSide() const
Get the "hand side" of this particular controller.
bool hasBeenReleased(uint8_t buttonIndex)
size_t getNbAxes() const
Return the number of analog axis this controller has.
std::string getTypeString() const
For test/branching, prefer using getType(). Get the type of the controller. Can be anything....
AnnVect3 trackedLinearSpeed
Linear velocity vector.
static bool isInRange(float v)
Return true if the value v is acceptable.
float getValue() const
Analog value between -1 and 1. Some trigger inputs are from 0 to 1 only.
bool hasBeenPressed(uint8_t buttonIndex)
virtual AnnHandControllerGestureHash gestcurrentGesturesHash()
Get the hash of the current gesture.
AnnQuaternion getWorldOrientation() const
Get orientation in world space.
virtual std::string getCurrentGesture()
Get the name of the current gesture. Please prefer use the hashed version.
AnnVect3 getLinearSpeed() const
Get tracked linear speed.
A 3D Vector.
Definition: AnnVect3.hpp:16
void setTrackedAngularSpeed(AnnVect3 v)
Set the angular velocity of the hand.
Namespace containing the totality of Annwvyn components.
Definition: AnnGetter.cpp:8
std::vector< AnnHandControllerAxis > axes
Axes vector.
AnnOgreVRRendererPtr AnnGetVRRenderer()
Get the VR renderer.
Definition: AnnGetter.cpp:20
bool tracked
tracked boolean, true if controller has been updated by the engine
void _attachModelItem(Ogre::Item *handModel)
Advanced method: Attach a 3D model to the hand. Previously attached model will be detached.
std::vector< uint8_t > & getReleasedButtonsVector()
Get a reference to the released event vector.
AnnGameObjectManagerPtr AnnGetGameObjectManager()
Get the game-object manager.
Definition: AnnGetter.cpp:22
static constexpr const char *const gestureNotAvailableString
Permanently set to "N/A".
void setTrackedLinearSpeed(AnnVect3 v)
Set the linear velocity of the hand.
AnnVect3 getAngularSpeed() const
Get tracked angular speed.
Represent the axis of an hand controller.
std::string name
Name of the axis.
AnnHandControllerAxis(const std::string &AxisName, float normalizedValue)
Create an AnnHandControllerAxis object.
HandControllerCapabilites_t capabilites
Capabilities of this controller.
Create a ostream to the Ogre logger.
AnnHandControllerTypeHash getType() const
Get the hash of the type of the controller.
Ogre::SceneManager * getSceneManager() const
Get ogre scene manager.
Definition: AnnEngine.cpp:331
AnnHandControllerSide
Identify the controller as "left hand", "right hand" or "invalid hand".
AnnStringUtilityPtr AnnGetStringUtility()
Get the string utility object.
Definition: AnnGetter.cpp:24
size_t AnnHandControllerGestureHash
Hash of a string identifier.
Ogre::SceneNode * node
Some Ogre Scene Nodes.
std::vector< byte > buttonsState
An array of the buttons states.
bool getButtonState(uint8_t buttonIndex)
void attachNode(Ogre::SceneNode *grabbedObject)
Attach the node as a child to the controller node.
Represent a Quaternion.
Open an output stream to the engine log.
Definition: AnnLogger.hpp:23
std::vector< byte > & getButtonStateVector()
Get a reference to the button state vector.
void setTrackedPosition(AnnVect3 position)
Set the position of the hand.
size_t AnnHandControllerID
ID of an hand controller is the index of an array. using size_t s.
size_t AnnHandControllerTypeHash
Hash of a string idientifier.
AnnHandControllerAxis invalidAxis
An invalid one to return a reference to if we can't return a valid axis.
AnnHandControllerAxis & getAxis(size_t index)
void setHandModel(const std::string &name)
Set model by name. IF model already attached, model will be detached, and the item will be destroyed.
std::vector< uint8_t > & getPressedButtonsVector()
Get a reference to the pressed event vector.
T size(T... args)
uint16_t HandControllerCapabilites_t
Fix the bitflag at 16bits wide.
AnnHandControllerTypeHash controllerTypeHash
Hash of the type, see controllerTypeString.
AnnVect3 trackedAngularSpeed
Angular velocity (Euler?) vector.
AnnHandController(const std::string &Type, Ogre::SceneNode *handNode, AnnHandControllerID controllerID, AnnHandControllerSide controllerSide)
Construct a Controller object.
void updateValue(float normalizedValue)
Change the value of the string.
size_t getNbButton() const
Return the number of buttons.
T c_str(T... args)
std::vector< uint8_t > releasedButtons
Ogre::Item * getHandModel() const
Return the current model :
std::string getName() const
Name of the axis.
std::vector< AnnHandControllerAxis > & getAxesVector()
Get a reference to the axes vector.
AnnVect3 getWorldPosition() const
Get position in world space.
Exception in hand controller "side" detection.
void detachModel()
Detach model without destroying it.
Ogre::Item * model
Currently attached entity.