notesObject.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include "T3D/notesObject.h"
  2. #include "math/mathIO.h"
  3. #include "scene/sceneRenderState.h"
  4. #include "core/stream/bitStream.h"
  5. #include "materials/sceneData.h"
  6. #include "gfx/gfxDebugEvent.h"
  7. #include "gfx/gfxTransformSaver.h"
  8. #include "renderInstance/renderPassManager.h"
  9. #include "math\mathUtils.h"
  10. #include "gfx/gfxDrawUtil.h"
  11. IMPLEMENT_CO_NETOBJECT_V1(NotesObject);
  12. extern bool gEditingMission;
  13. //-----------------------------------------------------------------------------
  14. // Object setup and teardown
  15. //-----------------------------------------------------------------------------
  16. NotesObject::NotesObject()
  17. {
  18. // Flag this object so that it will always
  19. // be sent across the network to clients
  20. mNetFlags.set(Ghostable | ScopeAlways);
  21. // Set it as a "static" object
  22. mTypeMask |= StaticObjectType | StaticShapeObjectType;
  23. mShowArrow = true;
  24. mArrowLength = 5;
  25. mArrowRadius = 0.25;
  26. }
  27. NotesObject::~NotesObject()
  28. {
  29. }
  30. //-----------------------------------------------------------------------------
  31. // Object Editing
  32. //-----------------------------------------------------------------------------
  33. void NotesObject::initPersistFields()
  34. {
  35. Parent::initPersistFields();
  36. addField("Note", TypeCommand, Offset(mNote, NotesObject), "");
  37. addField("showArrow", TypeBool, Offset(mShowArrow, NotesObject), "");
  38. addField("arrowColor", TypeColorF, Offset(mArrowColor, NotesObject), "");
  39. }
  40. void NotesObject::inspectPostApply()
  41. {
  42. // Apply any transformations set in the editor
  43. Parent::inspectPostApply();
  44. if (isServerObject())
  45. {
  46. setMaskBits(-1);
  47. }
  48. }
  49. bool NotesObject::onAdd()
  50. {
  51. if (!Parent::onAdd())
  52. return false;
  53. // Set up a 1x1x1 bounding box
  54. mObjBox.set(Point3F(-0.5f, -0.5f, -0.5f),
  55. Point3F(0.5f, 0.5f, 0.5f));
  56. resetWorldBox();
  57. // Add this object to the scene
  58. addToScene();
  59. return true;
  60. }
  61. void NotesObject::onRemove()
  62. {
  63. // Remove this object from the scene
  64. removeFromScene();
  65. Parent::onRemove();
  66. }
  67. void NotesObject::setTransform(const MatrixF& mat)
  68. {
  69. // Let SceneObject handle all of the matrix manipulation
  70. Parent::setTransform(mat);
  71. // Dirty our network mask so that the new transform gets
  72. // transmitted to the client object
  73. setMaskBits(TransformMask);
  74. }
  75. U32 NotesObject::packUpdate(NetConnection* conn, U32 mask, BitStream* stream)
  76. {
  77. // Allow the Parent to get a crack at writing its info
  78. U32 retMask = Parent::packUpdate(conn, mask, stream);
  79. // Write our transform information
  80. //if (stream->writeFlag(mask & TransformMask))
  81. {
  82. mathWrite(*stream, getTransform());
  83. mathWrite(*stream, getScale());
  84. stream->write(mShowArrow);
  85. stream->write(mArrowColor);
  86. }
  87. return retMask;
  88. }
  89. void NotesObject::unpackUpdate(NetConnection* conn, BitStream* stream)
  90. {
  91. // Let the Parent read any info it sent
  92. Parent::unpackUpdate(conn, stream);
  93. //if (stream->readFlag()) // TransformMask
  94. {
  95. mathRead(*stream, &mObjToWorld);
  96. mathRead(*stream, &mObjScale);
  97. setTransform(mObjToWorld);
  98. stream->read(&mShowArrow);
  99. stream->read(&mArrowColor);
  100. }
  101. }
  102. void NotesObject::prepRenderImage(SceneRenderState* state)
  103. {
  104. if (!gEditingMission)
  105. return;
  106. ObjectRenderInst* ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  107. ri->renderDelegate.bind(this, &NotesObject::_render);
  108. ri->type = RenderPassManager::RIT_Editor;
  109. state->getRenderPass()->addInst(ri);
  110. }
  111. void NotesObject::_render(ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat)
  112. {
  113. if (showArrow())
  114. {
  115. GFXStateBlockDesc desc;
  116. desc.setBlend(true);
  117. desc.setZReadWrite(true, false);
  118. VectorF forwardVec = getTransform().getForwardVector();
  119. forwardVec.normalize();
  120. Point3F startPos = forwardVec * -(mArrowLength * 0.5) + getPosition();
  121. Point3F endPos = forwardVec * (mArrowLength * 0.5) + getPosition();
  122. ColorI arrowColor = mArrowColor.toColorI();
  123. arrowColor.alpha = 128;
  124. GFX->getDrawUtil()->drawArrow(desc, startPos, endPos, arrowColor, mArrowRadius);
  125. }
  126. }