forestBrushElement.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. docsURL;
  54. Parent::initPersistFields();
  55. addGroup( "ForestBrushElement" );
  56. addField( "forestItemData", TYPEID< ForestItemData >(), Offset( mData, ForestBrushElement ),
  57. "The type of ForestItem this element holds placement parameters for." );
  58. addField( "probability", TypeF32, Offset( mProbability, ForestBrushElement ),
  59. "The probability that this element will be created during an editor brush stroke "
  60. "is the sum of all element probabilities in the brush divided by the probability "
  61. "of this element." );
  62. addField( "rotationRange", TypeF32, Offset( mRotationRange, ForestBrushElement ),
  63. "The max rotation in degrees that items will be placed." );
  64. addField( "scaleMin", TypeF32, Offset( mScaleMin, ForestBrushElement ),
  65. "The minimum random size for each item." );
  66. addField( "scaleMax", TypeF32, Offset( mScaleMax, ForestBrushElement ),
  67. "The maximum random size of each item." );
  68. addField( "scaleExponent", TypeF32, Offset( mScaleExponent, ForestBrushElement ),
  69. "An exponent used to bias between the minimum and maximum random sizes." );
  70. addField( "sinkMin", TypeF32, Offset( mSinkMin, ForestBrushElement ),
  71. "Min variation in the sink radius." );
  72. addField( "sinkMax", TypeF32, Offset( mSinkMax, ForestBrushElement ),
  73. "Max variation in the sink radius." );
  74. addField( "sinkRadius", TypeF32, Offset( mSinkRadius, ForestBrushElement ),
  75. "This is the radius used to calculate how much to sink the trunk at "
  76. "its base and is used to sink the tree into the ground when its on a slope." );
  77. addField( "slopeMin", TypeF32, Offset( mSlopeMin, ForestBrushElement ),
  78. "The min surface slope in degrees this item will be placed on." );
  79. addField( "slopeMax", TypeF32, Offset( mSlopeMax, ForestBrushElement ),
  80. "The max surface slope in degrees this item will be placed on." );
  81. addField( "elevationMin", TypeF32, Offset( mElevationMin, ForestBrushElement ),
  82. "The min world space elevation this item will be placed." );
  83. addField( "elevationMax", TypeF32, Offset( mElevationMax, ForestBrushElement ),
  84. "The max world space elevation this item will be placed." );
  85. endGroup( "ForestBrushElement" );
  86. }
  87. //-------------------------------------------------------------------------
  88. // ForestBrushElementSet
  89. //-------------------------------------------------------------------------
  90. SimObjectPtr<SimGroup> ForestBrush::smGroup = NULL;
  91. IMPLEMENT_CONOBJECT( ForestBrush );
  92. ConsoleDocClass( ForestBrush,
  93. "@brief Container class for ForestBrushElements\n\n"
  94. "Editor use only.\n\n"
  95. "@internal"
  96. );
  97. ForestBrush::ForestBrush()
  98. {
  99. }
  100. bool ForestBrush::onAdd()
  101. {
  102. if ( !Parent::onAdd() )
  103. return false;
  104. getGroup()->addObject( this );
  105. return true;
  106. }
  107. void ForestBrush::addObject( SimObject *inObj )
  108. {
  109. ForestBrushElement *ele = dynamic_cast<ForestBrushElement*>( inObj );
  110. if ( !ele )
  111. return;
  112. //if ( containsItemData( ele->mData ) )
  113. // return;
  114. Parent::addObject( inObj );
  115. }
  116. SimGroup* ForestBrush::getGroup()
  117. {
  118. if ( !smGroup )
  119. {
  120. SimGroup *dummy;
  121. if ( Sim::findObject( "ForestBrushGroup", dummy ) )
  122. {
  123. smGroup = dummy;
  124. return smGroup;
  125. }
  126. smGroup = new SimGroup;
  127. smGroup->assignName( "ForestBrushGroup" );
  128. smGroup->registerObject();
  129. Sim::getRootGroup()->addObject( smGroup );
  130. }
  131. return smGroup;
  132. }
  133. bool ForestBrush::containsItemData( const ForestItemData *inData )
  134. {
  135. SimObjectList::iterator iter = mObjectList.begin();
  136. for ( ; iter != mObjectList.end(); iter++ )
  137. {
  138. ForestBrushElement *pElement = dynamic_cast<ForestBrushElement*>(*iter);
  139. if ( !pElement )
  140. continue;
  141. if ( pElement->mData == inData )
  142. return true;
  143. }
  144. return false;
  145. }
  146. DefineEngineMethod( ForestBrush, containsItemData, bool, ( const char * obj ), , "( ForestItemData obj )" )
  147. {
  148. ForestItemData *data = NULL;
  149. if ( !Sim::findObject( obj, data ) )
  150. {
  151. Con::warnf( "ForestBrush::containsItemData - invalid object passed" );
  152. return false;
  153. }
  154. return object->containsItemData( data );
  155. }
  156. //-------------------------------------------------------------------------
  157. // ForestBrushGroupSet
  158. //-------------------------------------------------------------------------
  159. IMPLEMENT_CONOBJECT(ForestBrushGroup);
  160. ConsoleDocClass(ForestBrushGroup,
  161. "@brief Container class for ForestBrushes\n\n"
  162. "Editor use only.\n\n"
  163. "@internal"
  164. );
  165. ForestBrushGroup::ForestBrushGroup()
  166. {
  167. }
  168. bool ForestBrushGroup::onAdd()
  169. {
  170. if (!Parent::onAdd())
  171. return false;
  172. SimSet* forestBrushSet;
  173. if (!Sim::findObject("ForestBrushSet", forestBrushSet))
  174. {
  175. Con::errorf("ForestBrushGroup::onAdd() - failed to find ForestBrushSet to add new ForestBrushGroup to!");
  176. }
  177. forestBrushSet->addObject(this);
  178. return true;
  179. }
  180. void ForestBrushGroup::addObject(SimObject* inObj)
  181. {
  182. ForestBrush* ele = dynamic_cast<ForestBrush*>(inObj);
  183. if (!ele)
  184. return;
  185. //if ( containsItemData( ele->mData ) )
  186. // return;
  187. Parent::addObject(inObj);
  188. }
  189. bool ForestBrushGroup::containsBrushData(const ForestBrush* inData)
  190. {
  191. SimObjectList::iterator iter = mObjectList.begin();
  192. for (; iter != mObjectList.end(); iter++)
  193. {
  194. ForestBrush* pElement = dynamic_cast<ForestBrush*>(*iter);
  195. if (!pElement)
  196. continue;
  197. if (pElement == inData)
  198. return true;
  199. }
  200. return false;
  201. }
  202. DefineEngineMethod(ForestBrushGroup, containsBrushData, bool, (const char* obj), , "( ForestBrush obj )")
  203. {
  204. ForestBrush* data = NULL;
  205. if (!Sim::findObject(obj, data))
  206. {
  207. Con::warnf("ForestBrush::containsBrushData - invalid object passed");
  208. return false;
  209. }
  210. return object->containsBrushData(data);
  211. }