/src/webots/nodes/WbSlot.cpp

https://github.com/cyberbotics/webots · C++ · 218 lines · 161 code · 40 blank · 17 comment · 28 complexity · cbc1517e34fa420e82aaa961de99f3bc MD5 · raw file

  1. // Copyright 1996-2020 Cyberbotics Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "WbSlot.hpp"
  15. #include "WbBoundingSphere.hpp"
  16. #include "WbNodeUtilities.hpp"
  17. #include "WbSolid.hpp"
  18. #include "WbSolidReference.hpp"
  19. void WbSlot::init() {
  20. // user fields
  21. mEndPoint = findSFNode("endPoint");
  22. mSlotType = findSFString("type");
  23. }
  24. WbSlot::WbSlot(WbTokenizer *tokenizer) : WbBaseNode("Slot", tokenizer) {
  25. init();
  26. }
  27. WbSlot::WbSlot(const WbSlot &other) : WbBaseNode(other) {
  28. init();
  29. }
  30. WbSlot::WbSlot(const WbNode &other) : WbBaseNode(other) {
  31. init();
  32. }
  33. WbSlot::~WbSlot() {
  34. }
  35. void WbSlot::validateProtoNode() {
  36. WbSlot *slot = slotEndPoint();
  37. if (slot) {
  38. slot->validateProtoNode();
  39. return;
  40. }
  41. WbSolid *solid = solidEndPoint();
  42. if (solid)
  43. solid->validateProtoNode();
  44. }
  45. void WbSlot::preFinalize() {
  46. WbBaseNode::preFinalize();
  47. connect(mEndPoint, &WbSFString::changed, this, &WbSlot::endPointChanged);
  48. WbGroup *pg = dynamic_cast<WbGroup *>(parentNode());
  49. if (pg) // parent is a group
  50. connect(this, &WbSlot::endPointInserted, pg, &WbGroup::insertChildFromSlot);
  51. WbSlot *ps = dynamic_cast<WbSlot *>(parentNode());
  52. if (ps) // parent is another slot
  53. connect(this, &WbSlot::endPointInserted, ps, &WbSlot::endPointInserted);
  54. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  55. if (e && !e->isPreFinalizedCalled())
  56. e->preFinalize();
  57. }
  58. void WbSlot::postFinalize() {
  59. WbBaseNode::postFinalize();
  60. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  61. if (e && !e->isPostFinalizedCalled())
  62. e->postFinalize();
  63. connect(mSlotType, &WbSFString::changed, this, &WbSlot::updateType);
  64. }
  65. void WbSlot::updateType() {
  66. QString connectedType;
  67. const WbSlot *const parentSlot = dynamic_cast<WbSlot *>(parentNode());
  68. const WbSlot *const childSlot = slotEndPoint();
  69. if (parentSlot)
  70. connectedType = parentSlot->slotType();
  71. else if (childSlot)
  72. connectedType = childSlot->slotType();
  73. else
  74. return;
  75. QString errorMessage;
  76. if (!WbNodeUtilities::isSlotTypeMatch(slotType(), connectedType, errorMessage))
  77. parsingWarn(tr("Invalid 'type' changed to '%1': %2").arg(slotType()).arg(errorMessage));
  78. }
  79. WbSolid *WbSlot::solidEndPoint() const {
  80. return dynamic_cast<WbSolid *>(mEndPoint->value());
  81. }
  82. WbSolidReference *WbSlot::solidReferenceEndPoint() const {
  83. return dynamic_cast<WbSolidReference *>(mEndPoint->value());
  84. }
  85. WbSlot *WbSlot::slotEndPoint() const {
  86. return dynamic_cast<WbSlot *>(mEndPoint->value());
  87. }
  88. WbGroup *WbSlot::groupEndPoint() const {
  89. return dynamic_cast<WbGroup *>(mEndPoint->value());
  90. }
  91. void WbSlot::setEndPoint(WbNode *node) {
  92. WbBaseNode *const e = static_cast<WbBaseNode *>(node);
  93. mEndPoint->removeValue();
  94. mEndPoint->setValue(e);
  95. endPointChanged();
  96. }
  97. void WbSlot::createOdeObjects() {
  98. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  99. if (e)
  100. e->createOdeObjects();
  101. }
  102. void WbSlot::createWrenObjects() {
  103. WbBaseNode::createWrenObjects();
  104. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  105. if (e)
  106. e->createWrenObjects();
  107. }
  108. void WbSlot::propagateSelection(bool selected) {
  109. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  110. if (e)
  111. e->propagateSelection(selected);
  112. }
  113. void WbSlot::setMatrixNeedUpdate() {
  114. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  115. if (e)
  116. e->setMatrixNeedUpdate();
  117. }
  118. void WbSlot::setScaleNeedUpdate() {
  119. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  120. if (e)
  121. e->setScaleNeedUpdate();
  122. }
  123. void WbSlot::updateCollisionMaterial(bool triggerChange, bool onSelection) {
  124. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  125. if (e)
  126. e->updateCollisionMaterial(triggerChange, onSelection);
  127. }
  128. void WbSlot::setSleepMaterial() {
  129. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  130. if (e)
  131. e->setSleepMaterial();
  132. }
  133. WbBoundingSphere *WbSlot::boundingSphere() const {
  134. WbBaseNode *const baseNode = static_cast<WbBaseNode *>(mEndPoint->value());
  135. if (baseNode)
  136. return baseNode->boundingSphere();
  137. return NULL;
  138. }
  139. void WbSlot::endPointChanged() {
  140. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  141. if (e) {
  142. e->setParentNode(this);
  143. emit endPointInserted(e);
  144. }
  145. }
  146. void WbSlot::reset() {
  147. WbBaseNode::reset();
  148. WbNode *const e = mEndPoint->value();
  149. if (e)
  150. e->reset();
  151. }
  152. void WbSlot::save() {
  153. WbBaseNode::save();
  154. WbNode *const e = mEndPoint->value();
  155. if (e)
  156. e->save();
  157. }
  158. //////////////////////////////////////////////////////////////
  159. // WREN related methods for resizable WbGeometry children //
  160. //////////////////////////////////////////////////////////////
  161. void WbSlot::attachResizeManipulator() {
  162. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  163. if (e)
  164. e->attachResizeManipulator();
  165. }
  166. void WbSlot::detachResizeManipulator() const {
  167. WbBaseNode *const e = static_cast<WbBaseNode *>(mEndPoint->value());
  168. if (e)
  169. e->detachResizeManipulator();
  170. }
  171. void WbSlot::write(WbVrmlWriter &writer) const {
  172. if (writer.isWebots())
  173. WbBaseNode::write(writer);
  174. else {
  175. if (hasEndpoint())
  176. mEndPoint->value()->write(writer);
  177. }
  178. }