forestBrushElement.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "forest/editor/forestBrushElement.h"
  24. #include "console/engineAPI.h"
  25. #include "forest/forestItem.h"
  26. //-------------------------------------------------------------------------
  27. // ForestBrushElement
  28. //-------------------------------------------------------------------------
  29. IMPLEMENT_CONOBJECT( ForestBrushElement );
  30. ConsoleDocClass( ForestBrushElement,
  31. "@brief Represents a type of ForestItem and parameters for how it is placed"
  32. " when painting with a ForestBrush that contains it.\n\n"
  33. "@ingroup Forest"
  34. );
  35. ForestBrushElement::ForestBrushElement()
  36. : mData( NULL ),
  37. mProbability( 1 ),
  38. mRotationRange( 360 ),
  39. mScaleMin( 1 ),
  40. mScaleMax( 1 ),
  41. mScaleExponent( 1 ),
  42. mSinkMin( 0.0f ),
  43. mSinkMax( 0.0f ),
  44. mSinkRadius( 1 ),
  45. mSlopeMin( 0.0f ),
  46. mSlopeMax( 90.0f ),
  47. mElevationMin( -10000.0f ),
  48. mElevationMax( 10000.0f )
  49. {
  50. }
  51. void ForestBrushElement::initPersistFields()
  52. {
  53. Parent::initPersistFields();
  54. addGroup( "ForestBrushElement" );
  55. addField( "forestItemData", TYPEID< ForestItemData >(), Offset( mData, ForestBrushElement ),
  56. "The type of ForestItem this element holds placement parameters for." );
  57. addField( "probability", TypeF32, Offset( mProbability, ForestBrushElement ),
  58. "The probability that this element will be created during an editor brush stroke "
  59. "is the sum of all element probabilities in the brush divided by the probability "
  60. "of this element." );
  61. addField( "rotationRange", TypeF32, Offset( mRotationRange, ForestBrushElement ),
  62. "The max rotation in degrees that items will be placed." );
  63. addField( "scaleMin", TypeF32, Offset( mScaleMin, ForestBrushElement ),
  64. "The minimum random size for each item." );
  65. addField( "scaleMax", TypeF32, Offset( mScaleMax, ForestBrushElement ),
  66. "The maximum random size of each item." );
  67. addField( "scaleExponent", TypeF32, Offset( mScaleExponent, ForestBrushElement ),
  68. "An exponent used to bias between the minimum and maximum random sizes." );
  69. addField( "sinkMin", TypeF32, Offset( mSinkMin, ForestBrushElement ),
  70. "Min variation in the sink radius." );
  71. addField( "sinkMax", TypeF32, Offset( mSinkMax, ForestBrushElement ),
  72. "Max variation in the sink radius." );
  73. addField( "sinkRadius", TypeF32, Offset( mSinkRadius, ForestBrushElement ),
  74. "This is the radius used to calculate how much to sink the trunk at "
  75. "its base and is used to sink the tree into the ground when its on a slope." );
  76. addField( "slopeMin", TypeF32, Offset( mSlopeMin, ForestBrushElement ),
  77. "The min surface slope in degrees this item will be placed on." );
  78. addField( "slopeMax", TypeF32, Offset( mSlopeMax, ForestBrushElement ),
  79. "The max surface slope in degrees this item will be placed on." );
  80. addField( "elevationMin", TypeF32, Offset( mElevationMin, ForestBrushElement ),
  81. "The min world space elevation this item will be placed." );
  82. addField( "elevationMax", TypeF32, Offset( mElevationMax, ForestBrushElement ),
  83. "The max world space elevation this item will be placed." );
  84. endGroup( "ForestBrushElement" );
  85. }
  86. //-------------------------------------------------------------------------
  87. // ForestBrushElementSet
  88. //-------------------------------------------------------------------------
  89. SimObjectPtr<SimGroup> ForestBrush::smGroup = NULL;
  90. IMPLEMENT_CONOBJECT( ForestBrush );
  91. ConsoleDocClass( ForestBrush,
  92. "@brief Container class for ForestBrushElements\n\n"
  93. "Editor use only.\n\n"
  94. "@internal"
  95. );
  96. ForestBrush::ForestBrush()
  97. {
  98. }
  99. bool ForestBrush::onAdd()
  100. {
  101. if ( !Parent::onAdd() )
  102. return false;
  103. getGroup()->addObject( this );
  104. return true;
  105. }
  106. void ForestBrush::addObject( SimObject *inObj )
  107. {
  108. ForestBrushElement *ele = dynamic_cast<ForestBrushElement*>( inObj );
  109. if ( !ele )
  110. return;
  111. //if ( containsItemData( ele->mData ) )
  112. // return;
  113. Parent::addObject( inObj );
  114. }
  115. SimGroup* ForestBrush::getGroup()
  116. {
  117. if ( !smGroup )
  118. {
  119. SimGroup *dummy;
  120. if ( Sim::findObject( "ForestBrushGroup", dummy ) )
  121. {
  122. smGroup = dummy;
  123. return smGroup;
  124. }
  125. smGroup = new SimGroup;
  126. smGroup->assignName( "ForestBrushGroup" );
  127. smGroup->registerObject();
  128. Sim::getRootGroup()->addObject( smGroup );
  129. }
  130. return smGroup;
  131. }
  132. bool ForestBrush::containsItemData( const ForestItemData *inData )
  133. {
  134. SimObjectList::iterator iter = mObjectList.begin();
  135. for ( ; iter != mObjectList.end(); iter++ )
  136. {
  137. ForestBrushElement *pElement = dynamic_cast<ForestBrushElement*>(*iter);
  138. if ( !pElement )
  139. continue;
  140. if ( pElement->mData == inData )
  141. return true;
  142. }
  143. return false;
  144. }
  145. DefineEngineMethod( ForestBrush, containsItemData, bool, ( const char * obj ), , "( ForestItemData obj )" )
  146. {
  147. ForestItemData *data = NULL;
  148. if ( !Sim::findObject( obj, data ) )
  149. {
  150. Con::warnf( "ForestBrush::containsItemData - invalid object passed" );
  151. return false;
  152. }
  153. return object->containsItemData( data );
  154. }
  155. //-------------------------------------------------------------------------
  156. // ForestBrushGroupSet
  157. //-------------------------------------------------------------------------
  158. IMPLEMENT_CONOBJECT(ForestBrushGroup);
  159. ConsoleDocClass(ForestBrushGroup,
  160. "@brief Container class for ForestBrushes\n\n"
  161. "Editor use only.\n\n"
  162. "@internal"
  163. );
  164. ForestBrushGroup::ForestBrushGroup()
  165. {
  166. }
  167. bool ForestBrushGroup::onAdd()
  168. {
  169. if (!Parent::onAdd())
  170. return false;
  171. SimSet* forestBrushSet;
  172. if (!Sim::findObject("ForestBrushSet", forestBrushSet))
  173. {
  174. Con::errorf("ForestBrushGroup::onAdd() - failed to find ForestBrushSet to add new ForestBrushGroup to!");
  175. }
  176. forestBrushSet->addObject(this);
  177. return true;
  178. }
  179. void ForestBrushGroup::addObject(SimObject* inObj)
  180. {
  181. ForestBrush* ele = dynamic_cast<ForestBrush*>(inObj);
  182. if (!ele)
  183. return;
  184. //if ( containsItemData( ele->mData ) )
  185. // return;
  186. Parent::addObject(inObj);
  187. }
  188. bool ForestBrushGroup::containsBrushData(const ForestBrush* inData)
  189. {
  190. SimObjectList::iterator iter = mObjectList.begin();
  191. for (; iter != mObjectList.end(); iter++)
  192. {
  193. ForestBrush* pElement = dynamic_cast<ForestBrush*>(*iter);
  194. if (!pElement)
  195. continue;
  196. if (pElement == inData)
  197. return true;
  198. }
  199. return false;
  200. }
  201. DefineEngineMethod(ForestBrushGroup, containsBrushData, bool, (const char* obj), , "( ForestBrush obj )")
  202. {
  203. ForestBrush* data = NULL;
  204. if (!Sim::findObject(obj, data))
  205. {
  206. Con::warnf("ForestBrush::containsBrushData - invalid object passed");
  207. return false;
  208. }
  209. return object->containsBrushData(data);
  210. }