terrainEditor.cpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949
  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 "gui/worldEditor/terrainEditor.h"
  24. #include "core/frameAllocator.h"
  25. #include "core/strings/stringUnit.h"
  26. #include "console/consoleTypes.h"
  27. #include "console/simEvents.h"
  28. #include "console/engineAPI.h"
  29. #include "sim/netConnection.h"
  30. #include "math/mathUtils.h"
  31. #include "gfx/primBuilder.h"
  32. #include "gfx/gfxDrawUtil.h"
  33. #include "gui/core/guiCanvas.h"
  34. #include "gui/worldEditor/terrainActions.h"
  35. #include "terrain/terrMaterial.h"
  36. #include "T3D/Scene.h"
  37. IMPLEMENT_CONOBJECT(TerrainEditor);
  38. ConsoleDocClass( TerrainEditor,
  39. "@brief The base Terrain Editor tool\n\n"
  40. "Editor use only.\n\n"
  41. "@internal"
  42. );
  43. Selection::Selection() :
  44. Vector<GridInfo>(__FILE__, __LINE__),
  45. mName(0),
  46. mUndoFlags(0),
  47. mHashListSize(1024)
  48. {
  49. VECTOR_SET_ASSOCIATION(mHashLists);
  50. // clear the hash list
  51. mHashLists.setSize(mHashListSize);
  52. reset();
  53. }
  54. Selection::~Selection()
  55. {
  56. }
  57. void Selection::reset()
  58. {
  59. PROFILE_SCOPE( TerrainEditor_Selection_Reset );
  60. for(U32 i = 0; i < mHashListSize; i++)
  61. mHashLists[i] = -1;
  62. clear();
  63. }
  64. bool Selection::validate()
  65. {
  66. PROFILE_SCOPE( TerrainEditor_Selection_Validate );
  67. // scan all the hashes and verify that the heads they point to point back to them
  68. U32 hashesProcessed = 0;
  69. for(U32 i = 0; i < mHashLists.size(); i++)
  70. {
  71. U32 entry = mHashLists[i];
  72. if(entry == -1)
  73. continue;
  74. GridInfo info = (*this)[entry];
  75. U32 hashIndex = getHashIndex(info.mGridPoint.gridPos);
  76. if( entry != mHashLists[hashIndex] )
  77. {
  78. AssertFatal(false, "Selection hash lists corrupted");
  79. return false;
  80. }
  81. hashesProcessed++;
  82. }
  83. // scan all the entries and verify that anything w/ a prev == -1 is correctly in the hash table
  84. U32 headsProcessed = 0;
  85. for(U32 i = 0; i < size(); i++)
  86. {
  87. GridInfo info = (*this)[i];
  88. if(info.mPrev != -1)
  89. continue;
  90. U32 hashIndex = getHashIndex(info.mGridPoint.gridPos);
  91. if(mHashLists[hashIndex] != i)
  92. {
  93. AssertFatal(false, "Selection list heads corrupted");
  94. return false;
  95. }
  96. headsProcessed++;
  97. }
  98. AssertFatal(headsProcessed == hashesProcessed, "Selection's number of hashes and number of list heads differ.");
  99. return true;
  100. }
  101. U32 Selection::getHashIndex(const Point2I & pos)
  102. {
  103. PROFILE_SCOPE( TerrainEditor_Selection_GetHashIndex );
  104. Point2F pnt = Point2F((F32)pos.x, (F32)pos.y) + Point2F(1.3f,3.5f);
  105. return( (U32)(mFloor(mHashLists.size() * mFmod(pnt.len() * 0.618f, 1.0f))) );
  106. }
  107. S32 Selection::lookup(const Point2I & pos)
  108. {
  109. PROFILE_SCOPE( TerrainEditor_Selection_Lookup );
  110. U32 index = getHashIndex(pos);
  111. S32 entry = mHashLists[index];
  112. while(entry != -1)
  113. {
  114. if((*this)[entry].mGridPoint.gridPos == pos)
  115. return(entry);
  116. entry = (*this)[entry].mNext;
  117. }
  118. return(-1);
  119. }
  120. void Selection::insert(GridInfo info)
  121. {
  122. PROFILE_SCOPE( TerrainEditor_Selection_Insert );
  123. //validate();
  124. // get the index into the hash table
  125. U32 index = getHashIndex(info.mGridPoint.gridPos);
  126. // if there is an existing linked list, make it our next
  127. info.mNext = mHashLists[index];
  128. info.mPrev = -1;
  129. // if there is an existing linked list, make us it's prev
  130. U32 indexOfNewEntry = size();
  131. if(info.mNext != -1)
  132. (*this)[info.mNext].mPrev = indexOfNewEntry;
  133. // the hash table holds the heads of the linked lists. make us the head of this list.
  134. mHashLists[index] = indexOfNewEntry;
  135. // copy us into the vector
  136. push_back(info);
  137. //validate();
  138. }
  139. bool Selection::remove(const GridInfo &info)
  140. {
  141. PROFILE_SCOPE( TerrainEditor_Selection_Remove );
  142. if(size() < 1)
  143. return false;
  144. //AssertFatal( validate(), "Selection hashLists corrupted before Selection.remove()");
  145. U32 hashIndex = getHashIndex(info.mGridPoint.gridPos);
  146. S32 listHead = mHashLists[hashIndex];
  147. //AssertFatal(listHead < size(), "A Selection's hash table is corrupt.");
  148. if(listHead == -1)
  149. return(false);
  150. const S32 victimEntry = lookup(info.mGridPoint.gridPos);
  151. if( victimEntry == -1 )
  152. return(false);
  153. const GridInfo victim = (*this)[victimEntry];
  154. const S32 vicPrev = victim.mPrev;
  155. const S32 vicNext = victim.mNext;
  156. // remove us from the linked list, if there is one.
  157. if(vicPrev != -1)
  158. (*this)[vicPrev].mNext = vicNext;
  159. if(vicNext != -1)
  160. (*this)[vicNext].mPrev = vicPrev;
  161. // if we were the head of the list, make our next the new head in the hash table.
  162. if(vicPrev == -1)
  163. mHashLists[hashIndex] = vicNext;
  164. // if we're not the last element in the vector, copy the last element to our position.
  165. if(victimEntry != size() - 1)
  166. {
  167. // copy last into victim, and re-cache next & prev
  168. const GridInfo lastEntry = last();
  169. const S32 lastPrev = lastEntry.mPrev;
  170. const S32 lastNext = lastEntry.mNext;
  171. (*this)[victimEntry] = lastEntry;
  172. // update the new element's next and prev, to reestablish it in it's linked list.
  173. if(lastPrev != -1)
  174. (*this)[lastPrev].mNext = victimEntry;
  175. if(lastNext != -1)
  176. (*this)[lastNext].mPrev = victimEntry;
  177. // if it was the head of it's list, update the hash table with its new position.
  178. if(lastPrev == -1)
  179. {
  180. const U32 lastHash = getHashIndex(lastEntry.mGridPoint.gridPos);
  181. AssertFatal(mHashLists[lastHash] == size() - 1, "Selection hashLists corrupted during Selection.remove() (oldmsg)");
  182. mHashLists[lastHash] = victimEntry;
  183. }
  184. }
  185. // decrement the vector, we're done here
  186. pop_back();
  187. //AssertFatal( validate(), "Selection hashLists corrupted after Selection.remove()");
  188. return true;
  189. }
  190. bool Selection::add(const GridInfo &info)
  191. {
  192. PROFILE_SCOPE( TerrainEditor_Selection_Add );
  193. S32 index = lookup(info.mGridPoint.gridPos);
  194. if(index != -1)
  195. return(false);
  196. insert(info);
  197. return(true);
  198. }
  199. bool Selection::getInfo(Point2I pos, GridInfo & info)
  200. {
  201. PROFILE_SCOPE( TerrainEditor_Selection_GetInfo );
  202. S32 index = lookup(pos);
  203. if(index == -1)
  204. return(false);
  205. info = (*this)[index];
  206. return(true);
  207. }
  208. bool Selection::setInfo(GridInfo & info)
  209. {
  210. PROFILE_SCOPE( TerrainEditor_Selection_SetInfo );
  211. S32 index = lookup(info.mGridPoint.gridPos);
  212. if(index == -1)
  213. return(false);
  214. S32 next = (*this)[index].mNext;
  215. S32 prev = (*this)[index].mPrev;
  216. (*this)[index] = info;
  217. (*this)[index].mNext = next;
  218. (*this)[index].mPrev = prev;
  219. return(true);
  220. }
  221. F32 Selection::getAvgHeight()
  222. {
  223. PROFILE_SCOPE( TerrainEditor_Selection_GetAvgHeight );
  224. if(!size())
  225. return(0);
  226. F32 avg = 0.f;
  227. for(U32 i = 0; i < size(); i++)
  228. avg += (*this)[i].mHeight;
  229. return(avg / size());
  230. }
  231. F32 Selection::getMinHeight()
  232. {
  233. PROFILE_SCOPE( TerrainEditor_Selection_GetMinHeight );
  234. if(!size())
  235. return(0);
  236. F32 minHeight = (*this)[0].mHeight;
  237. for(U32 i = 1; i < size(); i++)
  238. minHeight = getMin(minHeight, (*this)[i].mHeight);
  239. return minHeight;
  240. }
  241. F32 Selection::getMaxHeight()
  242. {
  243. PROFILE_SCOPE( TerrainEditor_Selection_GetMaxHeight );
  244. if(!size())
  245. return(0);
  246. F32 maxHeight = (*this)[0].mHeight;
  247. for(U32 i = 1; i < size(); i++)
  248. maxHeight = getMax(maxHeight, (*this)[i].mHeight);
  249. return maxHeight;
  250. }
  251. Brush::Brush(TerrainEditor * editor) :
  252. mTerrainEditor(editor)
  253. {
  254. mSize = mTerrainEditor->getBrushSize();
  255. }
  256. const Point2I & Brush::getPosition()
  257. {
  258. return(mGridPoint.gridPos);
  259. }
  260. const GridPoint & Brush::getGridPoint()
  261. {
  262. return mGridPoint;
  263. }
  264. void Brush::setPosition(const Point3F & pos)
  265. {
  266. PROFILE_SCOPE( TerrainEditor_Brush_SetPosition_Point3F );
  267. mTerrainEditor->worldToGrid(pos, mGridPoint);
  268. update();
  269. }
  270. void Brush::setPosition(const Point2I & pos)
  271. {
  272. PROFILE_SCOPE( TerrainEditor_Brush_SetPosition_Point2I );
  273. mGridPoint.gridPos = pos;
  274. update();
  275. }
  276. void Brush::update()
  277. {
  278. PROFILE_SCOPE( TerrainEditor_Brush_update );
  279. if ( mGridPoint.terrainBlock )
  280. rebuild();
  281. }
  282. void Brush::render()
  283. {
  284. PROFILE_SCOPE( TerrainEditor_Brush_Render );
  285. // Render the brush's outline via the derived brush class.
  286. _renderOutline();
  287. // Render the brush's interior grid points.
  288. const U32 pointCount = mSize.x * mSize.y;
  289. if ( pointCount == 0 )
  290. return;
  291. if ( mRenderList.empty() || empty() )
  292. return;
  293. Vector<GFXVertexPCT> pointList;
  294. pointList.reserve( pointCount );
  295. for(S32 x = 0; x < mSize.x; x++)
  296. {
  297. for(S32 y = 0; y < mSize.y; y++)
  298. {
  299. S32 id = mRenderList[x*mSize.x+y];
  300. if ( id == -1 )
  301. continue;
  302. const GridInfo &gInfo = (*this)[ id ];
  303. Point3F pos;
  304. mTerrainEditor->gridToWorld( gInfo.mGridPoint.gridPos, pos, gInfo.mGridPoint.terrainBlock );
  305. if ( !mTerrainEditor->project( pos, &pos ) )
  306. continue;
  307. pointList.increment();
  308. GFXVertexPCT &pointInfo = pointList.last();
  309. pointInfo.point = pos;
  310. pointInfo.color.set( 255, 0, 255, gInfo.mWeight * 255 );
  311. pointInfo.texCoord.set( 1.0f, 0.0f );
  312. }
  313. }
  314. mTerrainEditor->renderPoints( pointList );
  315. }
  316. void BoxBrush::rebuild()
  317. {
  318. PROFILE_SCOPE( TerrainEditor_BoxBrush_Rebuild );
  319. reset();
  320. const F32 squareSize = mGridPoint.terrainBlock->getSquareSize();
  321. mRenderList.setSize(mSize.x*mSize.y);
  322. Point3F center( F32(mSize.x - 1) / 2.0f * squareSize, F32(mSize.y - 1) / 2.0f * squareSize, 0.0f );
  323. Filter filter;
  324. filter.set(1, &mTerrainEditor->mSoftSelectFilter);
  325. const Point3F mousePos = mTerrainEditor->getMousePos();
  326. F32 xFactorScale = center.x / ( center.x + 0.5f );
  327. F32 yFactorScale = center.y / ( center.y + 0.5f );
  328. const F32 softness = mTerrainEditor->getBrushSoftness();
  329. const F32 pressure = mTerrainEditor->getBrushPressure();
  330. Point3F posw( 0,0,0 );
  331. Point2I posg( 0,0 );
  332. Vector<GridInfo> infos;
  333. for ( S32 x = 0; x < mSize.x; x++ )
  334. {
  335. for(S32 y = 0; y < mSize.y; y++)
  336. {
  337. F32 xFactor = 0.0f;
  338. if ( center.x > 0 )
  339. xFactor = mAbs( center.x - x ) / center.x * xFactorScale;
  340. F32 yFactor = 0.0f;
  341. if ( center.y > 0 )
  342. yFactor = mAbs( center.y - y ) / center.y * yFactorScale;
  343. S32 &rl = mRenderList[x*mSize.x+y];
  344. posw.x = mousePos.x + (F32)x * squareSize - center.x;
  345. posw.y = mousePos.y + (F32)y * squareSize - center.y;
  346. // round to grid coords
  347. GridPoint gridPoint = mGridPoint;
  348. mTerrainEditor->worldToGrid( posw, gridPoint );
  349. // Check that the grid point is valid within the terrain. This assumes
  350. // that there is no wrap around past the edge of the terrain.
  351. if(!mTerrainEditor->isPointInTerrain(gridPoint))
  352. {
  353. rl = -1;
  354. continue;
  355. }
  356. infos.clear();
  357. mTerrainEditor->getGridInfos( gridPoint, infos );
  358. for (U32 z = 0; z < infos.size(); z++)
  359. {
  360. infos[z].mWeight = pressure *
  361. mLerp( infos[z].mWeight, filter.getValue(xFactor > yFactor ? xFactor : yFactor), softness );
  362. push_back(infos[z]);
  363. }
  364. rl = size()-1;
  365. }
  366. }
  367. }
  368. void BoxBrush::_renderOutline()
  369. {
  370. F32 squareSize = mGridPoint.terrainBlock->getSquareSize();
  371. RayInfo ri;
  372. Point3F start( 0, 0, 5000.0f );
  373. Point3F end( 0, 0, -5000.0f );
  374. bool hit;
  375. Vector<Point3F> pointList;
  376. pointList.reserve( 64 );
  377. const ColorI col( 255, 0, 255, 255 );
  378. const Point3F &mousePos = mTerrainEditor->getMousePos();
  379. static const Point2F offsetArray [5] =
  380. {
  381. Point2F( -1, -1 ),
  382. Point2F( 1, -1 ),
  383. Point2F( 1, 1 ),
  384. Point2F( -1, 1 ),
  385. Point2F( -1, -1 ) // repeat of offset[0]
  386. };
  387. // 64 total steps, 4 sides to the box, 16 steps per side.
  388. // 64 / 4 = 16
  389. const U32 steps = 16;
  390. for ( S32 i = 0; i < 4; i++ )
  391. {
  392. const Point2F &offset = offsetArray[i];
  393. const Point2F &next = offsetArray[i+1];
  394. for ( S32 j = 0; j < steps; j++ )
  395. {
  396. F32 frac = (F32)j / ( (F32)steps - 1.0f );
  397. Point2F tmp;
  398. tmp.interpolate( offset, next, frac );
  399. start.x = end.x = mousePos.x + tmp.x * squareSize * 0.5f * (F32)mSize.x;
  400. start.y = end.y = mousePos.y + tmp.y * squareSize * 0.5f * (F32)mSize.y;
  401. hit = gServerContainer.castRay( start, end, TerrainObjectType, &ri );
  402. if ( hit )
  403. pointList.push_back( ri.point );
  404. }
  405. }
  406. mTerrainEditor->drawLineList( pointList, col, 1.0f );
  407. }
  408. void EllipseBrush::rebuild()
  409. {
  410. PROFILE_SCOPE( TerrainEditor_EllipseBrush_Rebuild );
  411. reset();
  412. const F32 squareSize = mGridPoint.terrainBlock->getSquareSize();
  413. mRenderList.setSize(mSize.x*mSize.y);
  414. Point3F center( F32(mSize.x - 1) / 2.0f * squareSize, F32(mSize.y - 1) / 2.0f * squareSize, 0.0f );
  415. Filter filter;
  416. filter.set(1, &mTerrainEditor->mSoftSelectFilter);
  417. const Point3F mousePos = mTerrainEditor->getMousePos();
  418. // a point is in a circle if:
  419. // x^2 + y^2 <= r^2
  420. // a point is in an ellipse if:
  421. // (ax)^2 + (by)^2 <= 1
  422. // where a = 1/halfEllipseWidth and b = 1/halfEllipseHeight
  423. // for a soft-selected ellipse,
  424. // the factor is simply the filtered: ((ax)^2 + (by)^2)
  425. F32 a = 1.0f / (F32(mSize.x) * squareSize * 0.5f);
  426. F32 b = 1.0f / (F32(mSize.y) * squareSize * 0.5f);
  427. const F32 softness = mTerrainEditor->getBrushSoftness();
  428. const F32 pressure = mTerrainEditor->getBrushPressure();
  429. Point3F posw( 0,0,0 );
  430. Point2I posg( 0,0 );
  431. Vector<GridInfo> infos;
  432. for ( S32 x = 0; x < mSize.x; x++ )
  433. {
  434. for ( S32 y = 0; y < mSize.y; y++ )
  435. {
  436. F32 xp = center.x - x * squareSize;
  437. F32 yp = center.y - y * squareSize;
  438. F32 factor = (a * a * xp * xp) + (b * b * yp * yp);
  439. if ( factor > 1 )
  440. {
  441. mRenderList[x*mSize.x+y] = -1;
  442. continue;
  443. }
  444. S32 &rl = mRenderList[x*mSize.x+y];
  445. posw.x = mousePos.x + (F32)x * squareSize - center.x;
  446. posw.y = mousePos.y + (F32)y * squareSize - center.y;
  447. // round to grid coords
  448. GridPoint gridPoint = mGridPoint;
  449. mTerrainEditor->worldToGrid( posw, gridPoint );
  450. // Check that the grid point is valid within the terrain. This assumes
  451. // that there is no wrap around past the edge of the terrain.
  452. if ( !mTerrainEditor->isPointInTerrain( gridPoint ) )
  453. {
  454. rl = -1;
  455. continue;
  456. }
  457. infos.clear();
  458. mTerrainEditor->getGridInfos( gridPoint, infos );
  459. for ( U32 z = 0; z < infos.size(); z++ )
  460. {
  461. infos[z].mWeight = pressure * mLerp( infos[z].mWeight, filter.getValue( factor ), softness );
  462. push_back(infos[z]);
  463. }
  464. rl = size()-1;
  465. }
  466. }
  467. }
  468. void EllipseBrush::_renderOutline()
  469. {
  470. F32 squareSize = mGridPoint.terrainBlock->getSquareSize();
  471. RayInfo ri;
  472. Point3F start( 0, 0, 5000.0f );
  473. Point3F end( 0, 0, -5000.0f );
  474. bool hit;
  475. Vector<Point3F> pointList;
  476. ColorI col( 255, 0, 255, 255 );
  477. const U32 steps = 64;
  478. const Point3F &mousePos = mTerrainEditor->getMousePos();
  479. for ( S32 i = 0; i < steps; i++ )
  480. {
  481. F32 radians = (F32)i / (F32)(steps-1) * M_2PI_F;
  482. VectorF vec(0,1,0);
  483. MathUtils::vectorRotateZAxis( vec, radians );
  484. start.x = end.x = mousePos.x + vec.x * squareSize * (F32)mSize.x * 0.5f;
  485. start.y = end.y = mousePos.y + vec.y * squareSize * (F32)mSize.y * 0.5f;
  486. hit = gServerContainer.castRay( start, end, TerrainObjectType, &ri );
  487. if ( hit )
  488. pointList.push_back( ri.point );
  489. }
  490. mTerrainEditor->drawLineList( pointList, col, 1.0f );
  491. }
  492. SelectionBrush::SelectionBrush(TerrainEditor * editor) :
  493. Brush(editor)
  494. {
  495. //... grab the current selection
  496. }
  497. void SelectionBrush::rebuild()
  498. {
  499. reset();
  500. //... move the selection
  501. }
  502. void SelectionBrush::render(Vector<GFXVertexPCT> & vertexBuffer, S32 & verts, S32 & elems, S32 & prims, const LinearColorF & inColorFull, const LinearColorF & inColorNone, const LinearColorF & outColorFull, const LinearColorF & outColorNone) const
  503. {
  504. //... render the selection
  505. }
  506. TerrainEditor::TerrainEditor() :
  507. mActiveTerrain(0),
  508. mMousePos(0,0,0),
  509. mMouseBrush(0),
  510. mInAction(false),
  511. mGridUpdateMin( S32_MAX, S32_MAX ),
  512. mUndoSel(0),
  513. mGridUpdateMax( 0, 0 ),
  514. mNeedsGridUpdate( false ),
  515. mMaxBrushSize(256,256),
  516. mNeedsMaterialUpdate( false ),
  517. mMouseDown( false )
  518. {
  519. VECTOR_SET_ASSOCIATION(mActions);
  520. //
  521. resetCurrentSel();
  522. //
  523. mBrushPressure = 1.0f;
  524. mBrushSize.set(1,1);
  525. mBrushSoftness = 1.0f;
  526. mBrushChanged = true;
  527. mMouseBrush = new BoxBrush(this);
  528. mMouseDownSeq = 0;
  529. mIsDirty = false;
  530. mIsMissionDirty = false;
  531. mPaintIndex = -1;
  532. // add in all the actions here..
  533. mActions.push_back(new SelectAction(this));
  534. mActions.push_back(new DeselectAction(this));
  535. mActions.push_back(new ClearAction(this));
  536. mActions.push_back(new SoftSelectAction(this));
  537. mActions.push_back(new OutlineSelectAction(this));
  538. mActions.push_back(new PaintMaterialAction(this));
  539. mActions.push_back(new ClearMaterialsAction(this));
  540. mActions.push_back(new RaiseHeightAction(this));
  541. mActions.push_back(new LowerHeightAction(this));
  542. mActions.push_back(new SetHeightAction(this));
  543. mActions.push_back(new SetEmptyAction(this));
  544. mActions.push_back(new ClearEmptyAction(this));
  545. mActions.push_back(new ScaleHeightAction(this));
  546. mActions.push_back(new BrushAdjustHeightAction(this));
  547. mActions.push_back(new AdjustHeightAction(this));
  548. mActions.push_back(new FlattenHeightAction(this));
  549. mActions.push_back(new SmoothHeightAction(this));
  550. mActions.push_back(new SmoothSlopeAction(this));
  551. mActions.push_back(new PaintNoiseAction(this));
  552. //mActions.push_back(new ThermalErosionAction(this));
  553. // set the default action
  554. mCurrentAction = mActions[0];
  555. mRenderBrush = mCurrentAction->useMouseBrush();
  556. // persist data defaults
  557. mRenderBorder = true;
  558. mBorderHeight = 10;
  559. mBorderFillColor.set(0,255,0,20);
  560. mBorderFrameColor.set(0,255,0,128);
  561. mBorderLineMode = false;
  562. mSelectionHidden = false;
  563. mRenderVertexSelection = false;
  564. mRenderSolidBrush = false;
  565. mProcessUsesBrush = false;
  566. //
  567. mAdjustHeightVal = 10;
  568. mSetHeightVal = 100;
  569. mScaleVal = 1;
  570. mSmoothFactor = 0.1f;
  571. mNoiseFactor = 1.0f;
  572. mMaterialGroup = 0;
  573. mSoftSelectRadius = 50.f;
  574. mAdjustHeightMouseScale = 0.1f;
  575. mSoftSelectDefaultFilter = StringTable->insert("1.000000 0.833333 0.666667 0.500000 0.333333 0.166667 0.000000");
  576. mSoftSelectFilter = mSoftSelectDefaultFilter;
  577. mSlopeMinAngle = 0.0f;
  578. mSlopeMaxAngle = 90.0f;
  579. }
  580. TerrainEditor::~TerrainEditor()
  581. {
  582. // mouse
  583. delete mMouseBrush;
  584. // terrain actions
  585. U32 i;
  586. for(i = 0; i < mActions.size(); i++)
  587. delete mActions[i];
  588. // undo stuff
  589. delete mUndoSel;
  590. }
  591. TerrainAction * TerrainEditor::lookupAction(const char * name)
  592. {
  593. for(U32 i = 0; i < mActions.size(); i++)
  594. if(!dStricmp(mActions[i]->getName(), name))
  595. return(mActions[i]);
  596. return(0);
  597. }
  598. bool TerrainEditor::onAdd()
  599. {
  600. if ( !Parent::onAdd() )
  601. return false;
  602. GFXStateBlockDesc desc;
  603. desc.setZReadWrite( false );
  604. desc.zWriteEnable = false;
  605. desc.setCullMode( GFXCullNone );
  606. desc.setBlend( true, GFXBlendSrcAlpha, GFXBlendDestAlpha );
  607. mStateBlock = GFX->createStateBlock( desc );
  608. return true;
  609. }
  610. bool TerrainEditor::onWake()
  611. {
  612. if ( !Parent::onWake() )
  613. return false;
  614. // Push our default cursor on here once.
  615. GuiCanvas *root = getRoot();
  616. if ( root )
  617. {
  618. S32 currCursor = PlatformCursorController::curArrow;
  619. PlatformWindow *window = root->getPlatformWindow();
  620. PlatformCursorController *controller = window->getCursorController();
  621. controller->pushCursor( currCursor );
  622. }
  623. return true;
  624. }
  625. void TerrainEditor::onSleep()
  626. {
  627. // Pop our default cursor off.
  628. GuiCanvas *root = getRoot();
  629. if ( root )
  630. {
  631. PlatformWindow *window = root->getPlatformWindow();
  632. PlatformCursorController *controller = window->getCursorController();
  633. controller->popCursor();
  634. }
  635. Parent::onSleep();
  636. }
  637. void TerrainEditor::get3DCursor( GuiCursor *&cursor,
  638. bool &visible,
  639. const Gui3DMouseEvent &event_ )
  640. {
  641. cursor = NULL;
  642. visible = false;
  643. GuiCanvas *root = getRoot();
  644. if ( !root )
  645. return;
  646. S32 currCursor = PlatformCursorController::curArrow;
  647. if ( root->mCursorChanged == currCursor )
  648. return;
  649. PlatformWindow *window = root->getPlatformWindow();
  650. PlatformCursorController *controller = window->getCursorController();
  651. // We've already changed the cursor,
  652. // so set it back before we change it again.
  653. if( root->mCursorChanged != -1)
  654. controller->popCursor();
  655. // Now change the cursor shape
  656. controller->pushCursor(currCursor);
  657. root->mCursorChanged = currCursor;
  658. }
  659. void TerrainEditor::onDeleteNotify(SimObject * object)
  660. {
  661. Parent::onDeleteNotify(object);
  662. if (dynamic_cast<TerrainBlock*>(object) == mActiveTerrain)
  663. mActiveTerrain = NULL;
  664. }
  665. TerrainBlock* TerrainEditor::getClientTerrain( TerrainBlock *serverTerrain ) const
  666. {
  667. if ( !serverTerrain )
  668. serverTerrain = mActiveTerrain;
  669. return serverTerrain ? dynamic_cast<TerrainBlock*>( serverTerrain->getClientObject() ) : NULL;
  670. }
  671. bool TerrainEditor::isMainTile(const GridPoint & gPoint) const
  672. {
  673. const S32 blockSize = (S32)gPoint.terrainBlock->getBlockSize();
  674. Point2I testPos = gPoint.gridPos;
  675. if (!String::compare(getCurrentAction(),"paintMaterial"))
  676. {
  677. if (testPos.x == blockSize)
  678. testPos.x--;
  679. if (testPos.y == blockSize)
  680. testPos.y--;
  681. }
  682. return (testPos.x >= 0 && testPos.x < blockSize && testPos.y >= 0 && testPos.y < blockSize);
  683. }
  684. TerrainBlock* TerrainEditor::getTerrainUnderWorldPoint(const Point3F & wPos)
  685. {
  686. PROFILE_SCOPE( TerrainEditor_GetTerrainUnderWorldPoint );
  687. // Cast a ray straight down from the world position and see which
  688. // Terrain is the closest to our starting point
  689. Point3F startPnt = wPos;
  690. Point3F endPnt = wPos + Point3F(0.0f, 0.0f, -1000.0f);
  691. S32 blockIndex = -1;
  692. F32 nearT = 1.0f;
  693. for (U32 i = 0; i < mTerrainBlocks.size(); i++)
  694. {
  695. Point3F tStartPnt, tEndPnt;
  696. mTerrainBlocks[i]->getWorldTransform().mulP(startPnt, &tStartPnt);
  697. mTerrainBlocks[i]->getWorldTransform().mulP(endPnt, &tEndPnt);
  698. RayInfo ri;
  699. if (mTerrainBlocks[i]->castRayI(tStartPnt, tEndPnt, &ri, true))
  700. {
  701. if (ri.t < nearT)
  702. {
  703. blockIndex = i;
  704. nearT = ri.t;
  705. }
  706. }
  707. }
  708. if (blockIndex > -1)
  709. return mTerrainBlocks[blockIndex];
  710. return NULL;
  711. }
  712. bool TerrainEditor::gridToWorld(const GridPoint & gPoint, Point3F & wPos)
  713. {
  714. PROFILE_SCOPE( TerrainEditor_GridToWorld );
  715. const MatrixF & mat = gPoint.terrainBlock->getTransform();
  716. Point3F origin;
  717. mat.getColumn(3, &origin);
  718. wPos.x = gPoint.gridPos.x * gPoint.terrainBlock->getSquareSize() + origin.x;
  719. wPos.y = gPoint.gridPos.y * gPoint.terrainBlock->getSquareSize() + origin.y;
  720. wPos.z = getGridHeight(gPoint) + origin.z;
  721. return isMainTile(gPoint);
  722. }
  723. bool TerrainEditor::gridToWorld(const Point2I & gPos, Point3F & wPos, TerrainBlock* terrain)
  724. {
  725. GridPoint gridPoint;
  726. gridPoint.gridPos = gPos;
  727. gridPoint.terrainBlock = terrain;
  728. return gridToWorld(gridPoint, wPos);
  729. }
  730. bool TerrainEditor::worldToGrid(const Point3F & wPos, GridPoint & gPoint)
  731. {
  732. PROFILE_SCOPE( TerrainEditor_WorldToGrid );
  733. // If the grid point TerrainBlock is NULL then find the closest Terrain underneath that
  734. // point - pad a little upward in case our incoming point already lies exactly on the terrain
  735. if (!gPoint.terrainBlock)
  736. gPoint.terrainBlock = getTerrainUnderWorldPoint(wPos + Point3F(0.0f, 0.0f, 0.05f));
  737. if (gPoint.terrainBlock == NULL)
  738. return false;
  739. gPoint.gridPos = gPoint.terrainBlock->getGridPos(wPos);
  740. return isMainTile(gPoint);
  741. }
  742. bool TerrainEditor::worldToGrid(const Point3F & wPos, Point2I & gPos, TerrainBlock* terrain)
  743. {
  744. GridPoint gridPoint;
  745. gridPoint.terrainBlock = terrain;
  746. bool ret = worldToGrid(wPos, gridPoint);
  747. gPos = gridPoint.gridPos;
  748. return ret;
  749. }
  750. bool TerrainEditor::gridToCenter(const Point2I & gPos, Point2I & cPos) const
  751. {
  752. // TODO: What is this for... megaterrain or tiled terrains?
  753. cPos.x = gPos.x; // & TerrainBlock::BlockMask;
  754. cPos.y = gPos.y;// & TerrainBlock::BlockMask;
  755. //if (gPos.x == TerrainBlock::BlockSize)
  756. // cPos.x = gPos.x;
  757. //if (gPos.y == TerrainBlock::BlockSize)
  758. // cPos.y = gPos.y;
  759. //return isMainTile(gPos);
  760. return true;
  761. }
  762. //------------------------------------------------------------------------------
  763. //bool TerrainEditor::getGridInfo(const Point3F & wPos, GridInfo & info)
  764. //{
  765. // Point2I gPos;
  766. // worldToGrid(wPos, gPos);
  767. // return getGridInfo(gPos, info);
  768. //}
  769. bool TerrainEditor::getGridInfo(const GridPoint & gPoint, GridInfo & info)
  770. {
  771. //
  772. info.mGridPoint = gPoint;
  773. info.mMaterial = getGridMaterial(gPoint);
  774. info.mHeight = getGridHeight(gPoint);
  775. info.mWeight = 1.f;
  776. info.mPrimarySelect = true;
  777. info.mMaterialChanged = false;
  778. Point2I cPos;
  779. gridToCenter(gPoint.gridPos, cPos);
  780. return isMainTile(gPoint);
  781. }
  782. bool TerrainEditor::getGridInfo(const Point2I & gPos, GridInfo & info, TerrainBlock* terrain)
  783. {
  784. GridPoint gridPoint;
  785. gridPoint.gridPos = gPos;
  786. gridPoint.terrainBlock = terrain;
  787. return getGridInfo(gridPoint, info);
  788. }
  789. void TerrainEditor::getGridInfos(const GridPoint & gPoint, Vector<GridInfo>& infos)
  790. {
  791. PROFILE_SCOPE( TerrainEditor_GetGridInfos );
  792. // First we test against the brush terrain so that we can
  793. // favor it (this should be the same as the active terrain)
  794. bool foundBrush = false;
  795. GridInfo baseInfo;
  796. if (getGridInfo(gPoint, baseInfo))
  797. {
  798. infos.push_back(baseInfo);
  799. foundBrush = true;
  800. }
  801. // We are going to need the world position to test against
  802. Point3F wPos;
  803. gridToWorld(gPoint, wPos);
  804. // Now loop through our terrain blocks and decide which ones hit the point
  805. // If we already found a hit against our brush terrain we only add points
  806. // that are relatively close to the found point
  807. for (U32 i = 0; i < mTerrainBlocks.size(); i++)
  808. {
  809. // Skip if we've already found the point on the brush terrain
  810. if (foundBrush && mTerrainBlocks[i] == baseInfo.mGridPoint.terrainBlock)
  811. continue;
  812. // Get our grid position
  813. Point2I gPos;
  814. worldToGrid(wPos, gPos, mTerrainBlocks[i]);
  815. GridInfo info;
  816. if (getGridInfo(gPos, info, mTerrainBlocks[i]))
  817. {
  818. // Skip adding this if we already found a GridInfo from the brush terrain
  819. // and the resultant world point isn't equivalent
  820. if (foundBrush)
  821. {
  822. // Convert back to world (since the height can be different)
  823. // Possibly use getHeight() here?
  824. Point3F testWorldPt;
  825. gridToWorld(gPos, testWorldPt, mTerrainBlocks[i]);
  826. if (mFabs( wPos.z - testWorldPt.z ) > 4.0f )
  827. continue;
  828. }
  829. infos.push_back(info);
  830. }
  831. }
  832. }
  833. void TerrainEditor::setGridInfo(const GridInfo & info, bool checkActive)
  834. {
  835. PROFILE_SCOPE( TerrainEditor_SetGridInfo );
  836. setGridHeight(info.mGridPoint, info.mHeight);
  837. setGridMaterial(info.mGridPoint, info.mMaterial);
  838. }
  839. F32 TerrainEditor::getGridHeight(const GridPoint & gPoint)
  840. {
  841. PROFILE_SCOPE( TerrainEditor_GetGridHeight );
  842. Point2I cPos;
  843. gridToCenter( gPoint.gridPos, cPos );
  844. const TerrainFile *file = gPoint.terrainBlock->getFile();
  845. return fixedToFloat( file->getHeight( cPos.x, cPos.y ) );
  846. }
  847. void TerrainEditor::gridUpdateComplete( bool materialChanged )
  848. {
  849. PROFILE_SCOPE( TerrainEditor_GridUpdateComplete );
  850. // TODO: This updates all terrains and not just the ones
  851. // that were changed. We should keep track of the mGridUpdate
  852. // in world space and transform it into terrain space.
  853. if(mGridUpdateMin.x <= mGridUpdateMax.x)
  854. {
  855. for (U32 i = 0; i < mTerrainBlocks.size(); i++)
  856. {
  857. TerrainBlock *clientTerrain = getClientTerrain( mTerrainBlocks[i] );
  858. if ( materialChanged )
  859. clientTerrain->updateGridMaterials(mGridUpdateMin, mGridUpdateMax);
  860. mTerrainBlocks[i]->updateGrid(mGridUpdateMin, mGridUpdateMax);
  861. clientTerrain->updateGrid(mGridUpdateMin, mGridUpdateMax);
  862. }
  863. }
  864. mGridUpdateMin.set( S32_MAX, S32_MAX );
  865. mGridUpdateMax.set( 0, 0 );
  866. mNeedsGridUpdate = false;
  867. }
  868. void TerrainEditor::materialUpdateComplete()
  869. {
  870. PROFILE_SCOPE( TerrainEditor_MaterialUpdateComplete );
  871. if(mActiveTerrain && (mGridUpdateMin.x <= mGridUpdateMax.x))
  872. {
  873. TerrainBlock * clientTerrain = getClientTerrain(mActiveTerrain);
  874. clientTerrain->updateGridMaterials(mGridUpdateMin, mGridUpdateMax);
  875. }
  876. mGridUpdateMin.set( S32_MAX, S32_MAX );
  877. mGridUpdateMax.set( 0, 0 );
  878. mNeedsMaterialUpdate = false;
  879. }
  880. void TerrainEditor::setGridHeight(const GridPoint & gPoint, const F32 height)
  881. {
  882. PROFILE_SCOPE( TerrainEditor_SetGridHeight );
  883. Point2I cPos;
  884. gridToCenter(gPoint.gridPos, cPos);
  885. mGridUpdateMin.setMin( cPos );
  886. mGridUpdateMax.setMax( cPos );
  887. gPoint.terrainBlock->setHeight(cPos, height);
  888. }
  889. U8 TerrainEditor::getGridMaterial( const GridPoint &gPoint ) const
  890. {
  891. PROFILE_SCOPE( TerrainEditor_GetGridMaterial );
  892. Point2I cPos;
  893. gridToCenter( gPoint.gridPos, cPos );
  894. const TerrainFile *file = gPoint.terrainBlock->getFile();
  895. return file->getLayerIndex( cPos.x, cPos.y );
  896. }
  897. void TerrainEditor::setGridMaterial( const GridPoint &gPoint, U8 index )
  898. {
  899. PROFILE_SCOPE( TerrainEditor_SetGridMaterial );
  900. Point2I cPos;
  901. gridToCenter( gPoint.gridPos, cPos );
  902. TerrainFile *file = gPoint.terrainBlock->getFile();
  903. // If we changed the empty state then we need
  904. // to do a grid update as well.
  905. U8 currIndex = file->getLayerIndex( cPos.x, cPos.y );
  906. if ( ( currIndex == (U8)-1 && index != (U8)-1 ) ||
  907. ( currIndex != (U8)-1 && index == (U8)-1 ) )
  908. {
  909. mGridUpdateMin.setMin( cPos );
  910. mGridUpdateMax.setMax( cPos );
  911. mNeedsGridUpdate = true;
  912. }
  913. file->setLayerIndex( cPos.x, cPos.y, index );
  914. }
  915. //------------------------------------------------------------------------------
  916. TerrainBlock* TerrainEditor::collide(const Gui3DMouseEvent & evt, Point3F & pos)
  917. {
  918. PROFILE_SCOPE( TerrainEditor_Collide );
  919. if (mTerrainBlocks.size() == 0)
  920. return NULL;
  921. if ( mMouseDown && !String::compare(getCurrentAction(),"paintMaterial") )
  922. {
  923. if ( !mActiveTerrain )
  924. return NULL;
  925. Point3F tpos, tvec;
  926. tpos = evt.pos;
  927. tvec = evt.vec;
  928. mMousePlane.intersect( evt.pos, evt.vec, &pos );
  929. return mActiveTerrain;
  930. }
  931. const U32 mask = TerrainObjectType;
  932. Point3F start( evt.pos );
  933. Point3F end( evt.pos + ( evt.vec * 10000.0f ) );
  934. RayInfo rinfo;
  935. bool hit = gServerContainer.castRay( start, end, mask, &rinfo );
  936. if ( !hit )
  937. return NULL;
  938. pos = rinfo.point;
  939. return (TerrainBlock*)(rinfo.object);
  940. //
  941. //// call the terrain block's ray collision routine directly
  942. //Point3F startPnt = event.pos;
  943. //Point3F endPnt = event.pos + event.vec * 1000.0f;
  944. //S32 blockIndex = -1;
  945. //F32 nearT = 1.0f;
  946. //for (U32 i = 0; i < mTerrainBlocks.size(); i++)
  947. //{
  948. // Point3F tStartPnt, tEndPnt;
  949. // mTerrainBlocks[i]->getWorldTransform().mulP(startPnt, &tStartPnt);
  950. // mTerrainBlocks[i]->getWorldTransform().mulP(endPnt, &tEndPnt);
  951. // RayInfo ri;
  952. // if (mTerrainBlocks[i]->castRayI(tStartPnt, tEndPnt, &ri, true))
  953. // {
  954. // if (ri.t < nearT)
  955. // {
  956. // blockIndex = i;
  957. // nearT = ri.t;
  958. // }
  959. // }
  960. //}
  961. //if (blockIndex > -1)
  962. //{
  963. // pos.interpolate(startPnt, endPnt, nearT);
  964. // return mTerrainBlocks[blockIndex];
  965. //}
  966. //return NULL;
  967. }
  968. //------------------------------------------------------------------------------
  969. void TerrainEditor::updateGuiInfo()
  970. {
  971. PROFILE_SCOPE( TerrainEditor_UpdateGuiInfo );
  972. char buf[128];
  973. // mouse num grids
  974. // mouse avg height
  975. // selection num grids
  976. // selection avg height
  977. dSprintf(buf, sizeof(buf), "%d %g %g %g %d %g",
  978. mMouseBrush->size(), mMouseBrush->getMinHeight(),
  979. mMouseBrush->getAvgHeight(), mMouseBrush->getMaxHeight(),
  980. mDefaultSel.size(), mDefaultSel.getAvgHeight());
  981. Con::executef(this, "onGuiUpdate", buf);
  982. // If the brush setup has changed send out
  983. // a notification of that!
  984. if ( mBrushChanged && isMethod( "onBrushChanged" ) )
  985. {
  986. mBrushChanged = false;
  987. Con::executef( this, "onBrushChanged" );
  988. }
  989. }
  990. //------------------------------------------------------------------------------
  991. void TerrainEditor::onPreRender()
  992. {
  993. PROFILE_SCOPE( TerrainEditor_OnPreRender );
  994. if ( mNeedsGridUpdate )
  995. gridUpdateComplete( mNeedsMaterialUpdate );
  996. else if ( mNeedsMaterialUpdate )
  997. materialUpdateComplete();
  998. Parent::onPreRender();
  999. }
  1000. void TerrainEditor::renderScene(const RectI &)
  1001. {
  1002. PROFILE_SCOPE( TerrainEditor_RenderScene );
  1003. if(mTerrainBlocks.size() == 0)
  1004. return;
  1005. if(!mSelectionHidden)
  1006. renderSelection(mDefaultSel, LinearColorF::RED, LinearColorF::GREEN, LinearColorF::BLUE, LinearColorF::BLUE, true, false);
  1007. if(mRenderBrush && mMouseBrush->size())
  1008. renderBrush(*mMouseBrush, LinearColorF::GREEN, LinearColorF::RED, LinearColorF::BLUE, LinearColorF::BLUE, false, true);
  1009. if(mRenderBorder)
  1010. renderBorder();
  1011. }
  1012. //------------------------------------------------------------------------------
  1013. void TerrainEditor::renderGui( Point2I offset, const RectI &updateRect )
  1014. {
  1015. PROFILE_SCOPE( TerrainEditor_RenderGui );
  1016. if ( !mActiveTerrain )
  1017. return;
  1018. // Just in case...
  1019. if ( mMouseBrush->getGridPoint().terrainBlock != mActiveTerrain )
  1020. mMouseBrush->setTerrain( mActiveTerrain );
  1021. mMouseBrush->render();
  1022. }
  1023. void TerrainEditor::renderPoints( const Vector<GFXVertexPCT> &pointList )
  1024. {
  1025. PROFILE_SCOPE( TerrainEditor_RenderPoints );
  1026. const U32 pointCount = pointList.size();
  1027. const U32 vertCount = pointCount * 6;
  1028. GFXStateBlockDesc desc;
  1029. desc.setBlend( true );
  1030. desc.setZReadWrite( false, false );
  1031. GFX->setupGenericShaders();
  1032. GFX->setStateBlockByDesc( desc );
  1033. U32 vertsLeft = vertCount;
  1034. U32 offset = 0;
  1035. while ( vertsLeft > 0 )
  1036. {
  1037. U32 vertsThisDrawCall = getMin( (U32)vertsLeft, (U32)GFX_MAX_DYNAMIC_VERTS );
  1038. vertsLeft -= vertsThisDrawCall;
  1039. GFXVertexBufferHandle<GFXVertexPCT> vbuff( GFX, vertsThisDrawCall, GFXBufferTypeVolatile );
  1040. GFXVertexPCT *vert = vbuff.lock();
  1041. const U32 loops = vertsThisDrawCall / 6;
  1042. for ( S32 i = 0; i < loops; i++ )
  1043. {
  1044. const GFXVertexPCT &pointInfo = pointList[i + offset];
  1045. vert[0].color = vert[1].color = vert[2].color = vert[3].color = vert[4].color = vert[5].color = pointInfo.color;
  1046. const F32 halfSize = pointInfo.texCoord.x * 0.5f;
  1047. const Point3F &pos = pointInfo.point;
  1048. Point3F p0( pos.x - halfSize, pos.y - halfSize, 0.0f );
  1049. Point3F p1( pos.x + halfSize, pos.y - halfSize, 0.0f );
  1050. Point3F p2( pos.x + halfSize, pos.y + halfSize, 0.0f );
  1051. Point3F p3( pos.x - halfSize, pos.y + halfSize, 0.0f );
  1052. vert[0].point = p0;
  1053. vert[1].point = p1;
  1054. vert[2].point = p2;
  1055. vert[3].point = p0;
  1056. vert[4].point = p2;
  1057. vert[5].point = p3;
  1058. vert += 6;
  1059. }
  1060. vbuff.unlock();
  1061. GFX->setVertexBuffer( vbuff );
  1062. GFX->drawPrimitive( GFXTriangleList, 0, vertsThisDrawCall / 3 );
  1063. offset += loops;
  1064. }
  1065. }
  1066. //------------------------------------------------------------------------------
  1067. void TerrainEditor::renderSelection( const Selection & sel, const LinearColorF & inColorFull, const LinearColorF & inColorNone, const LinearColorF & outColorFull, const LinearColorF & outColorNone, bool renderFill, bool renderFrame )
  1068. {
  1069. PROFILE_SCOPE( TerrainEditor_RenderSelection );
  1070. // Draw nothing if nothing selected.
  1071. if(sel.size() == 0)
  1072. return;
  1073. Vector<GFXVertexPCT> vertexBuffer;
  1074. LinearColorF color;
  1075. ColorI iColor;
  1076. vertexBuffer.setSize(sel.size() * 5);
  1077. F32 squareSize = ( mActiveTerrain ) ? mActiveTerrain->getSquareSize() : 1;
  1078. // 'RenderVertexSelection' looks really bad so just always use the good one.
  1079. if( false && mRenderVertexSelection)
  1080. {
  1081. for(U32 i = 0; i < sel.size(); i++)
  1082. {
  1083. Point3F wPos;
  1084. bool center = gridToWorld(sel[i].mGridPoint, wPos);
  1085. F32 weight = sel[i].mWeight;
  1086. if(center)
  1087. {
  1088. if ( weight < 0.f || weight > 1.f )
  1089. color = inColorFull;
  1090. else
  1091. color.interpolate( inColorNone, inColorFull, weight );
  1092. }
  1093. else
  1094. {
  1095. if ( weight < 0.f || weight > 1.f)
  1096. color = outColorFull;
  1097. else
  1098. color.interpolate( outColorFull, outColorNone, weight );
  1099. }
  1100. //
  1101. iColor = color.toColorI();
  1102. GFXVertexPCT *verts = &(vertexBuffer[i * 5]);
  1103. verts[0].point = wPos + Point3F(-squareSize, squareSize, 0);
  1104. verts[0].color = iColor;
  1105. verts[1].point = wPos + Point3F( squareSize, squareSize, 0);
  1106. verts[1].color = iColor;
  1107. verts[2].point = wPos + Point3F( -squareSize, -squareSize, 0);
  1108. verts[2].color = iColor;
  1109. verts[3].point = wPos + Point3F( squareSize, -squareSize, 0);
  1110. verts[3].color = iColor;
  1111. verts[4].point = verts[0].point;
  1112. verts[4].color = iColor;
  1113. }
  1114. }
  1115. else
  1116. {
  1117. // walk the points in the selection
  1118. for(U32 i = 0; i < sel.size(); i++)
  1119. {
  1120. GridPoint selectedGridPoint = sel[i].mGridPoint;
  1121. Point2I gPos = selectedGridPoint.gridPos;
  1122. GFXVertexPCT *verts = &(vertexBuffer[i * 5]);
  1123. bool center = gridToWorld(selectedGridPoint, verts[0].point);
  1124. gridToWorld(Point2I(gPos.x + 1, gPos.y), verts[1].point, selectedGridPoint.terrainBlock);
  1125. gridToWorld(Point2I(gPos.x + 1, gPos.y + 1), verts[2].point, selectedGridPoint.terrainBlock);
  1126. gridToWorld(Point2I(gPos.x, gPos.y + 1), verts[3].point, selectedGridPoint.terrainBlock);
  1127. verts[4].point = verts[0].point;
  1128. F32 weight = sel[i].mWeight;
  1129. if( !mRenderSolidBrush )
  1130. {
  1131. if ( center )
  1132. {
  1133. if ( weight < 0.f || weight > 1.f )
  1134. color = inColorFull;
  1135. else
  1136. color.interpolate(inColorNone, inColorFull, weight );
  1137. }
  1138. else
  1139. {
  1140. if( weight < 0.f || weight > 1.f )
  1141. color = outColorFull;
  1142. else
  1143. color.interpolate(outColorFull, outColorNone, weight );
  1144. }
  1145. iColor = color.toColorI();
  1146. }
  1147. else
  1148. {
  1149. if ( center )
  1150. {
  1151. iColor = LinearColorF(inColorNone).toColorI();
  1152. }
  1153. else
  1154. {
  1155. iColor = LinearColorF(outColorFull).toColorI();
  1156. }
  1157. }
  1158. verts[0].color = iColor;
  1159. verts[1].color = iColor;
  1160. verts[2].color = iColor;
  1161. verts[3].color = iColor;
  1162. verts[4].color = iColor;
  1163. }
  1164. }
  1165. // Render this bad boy, by stuffing everything into a volatile buffer
  1166. // and rendering...
  1167. GFXVertexBufferHandle<GFXVertexPCT> selectionVB(GFX, vertexBuffer.size(), GFXBufferTypeStatic);
  1168. selectionVB.lock(0, vertexBuffer.size());
  1169. // Copy stuff
  1170. dMemcpy((void*)&selectionVB[0], (void*)&vertexBuffer[0], sizeof(GFXVertexPCT) * vertexBuffer.size());
  1171. selectionVB.unlock();
  1172. GFX->setupGenericShaders();
  1173. GFX->setStateBlock( mStateBlock );
  1174. GFX->setVertexBuffer(selectionVB);
  1175. if(renderFill)
  1176. for(U32 i=0; i < sel.size(); i++)
  1177. GFX->drawPrimitive( GFXTriangleStrip, i*5, 4);
  1178. if(renderFrame)
  1179. for(U32 i=0; i < sel.size(); i++)
  1180. GFX->drawPrimitive( GFXLineStrip , i*5, 4);
  1181. }
  1182. void TerrainEditor::renderBrush( const Brush & brush, const LinearColorF & inColorFull, const LinearColorF & inColorNone, const LinearColorF & outColorFull, const LinearColorF & outColorNone, bool renderFill, bool renderFrame )
  1183. {
  1184. }
  1185. void TerrainEditor::renderBorder()
  1186. {
  1187. // TODO: Disabled rendering the terrain borders... it was
  1188. // very annoying getting a fullscreen green tint on things.
  1189. //
  1190. // We should consider killing this all together or coming
  1191. // up with a new technique.
  1192. /*
  1193. Point2I pos(0,0);
  1194. Point2I dir[4] = {
  1195. Point2I(1,0),
  1196. Point2I(0,1),
  1197. Point2I(-1,0),
  1198. Point2I(0,-1)
  1199. };
  1200. GFX->setStateBlock( mStateBlock );
  1201. //
  1202. if(mBorderLineMode)
  1203. {
  1204. PrimBuild::color(mBorderFrameColor);
  1205. PrimBuild::begin( GFXLineStrip, TerrainBlock::BlockSize * 4 + 1 );
  1206. for(U32 i = 0; i < 4; i++)
  1207. {
  1208. for(U32 j = 0; j < TerrainBlock::BlockSize; j++)
  1209. {
  1210. Point3F wPos;
  1211. gridToWorld(pos, wPos, mActiveTerrain);
  1212. PrimBuild::vertex3fv( wPos );
  1213. pos += dir[i];
  1214. }
  1215. }
  1216. Point3F wPos;
  1217. gridToWorld(Point2I(0,0), wPos, mActiveTerrain);
  1218. PrimBuild::vertex3fv( wPos );
  1219. PrimBuild::end();
  1220. }
  1221. else
  1222. {
  1223. GridSquare * gs = mActiveTerrain->findSquare(TerrainBlock::BlockShift, Point2I(0,0));
  1224. F32 height = F32(gs->maxHeight) * 0.03125f + mBorderHeight;
  1225. const MatrixF & mat = mActiveTerrain->getTransform();
  1226. Point3F pos;
  1227. mat.getColumn(3, &pos);
  1228. Point2F min(pos.x, pos.y);
  1229. Point2F max(pos.x + TerrainBlock::BlockSize * mActiveTerrain->getSquareSize(),
  1230. pos.y + TerrainBlock::BlockSize * mActiveTerrain->getSquareSize());
  1231. ColorI & a = mBorderFillColor;
  1232. ColorI & b = mBorderFrameColor;
  1233. for(U32 i = 0; i < 2; i++)
  1234. {
  1235. //
  1236. if(i){ PrimBuild::color(a); PrimBuild::begin( GFXTriangleFan, 4 ); } else { PrimBuild::color(b); PrimBuild::begin( GFXLineStrip, 5 ); }
  1237. PrimBuild::vertex3f(min.x, min.y, 0);
  1238. PrimBuild::vertex3f(max.x, min.y, 0);
  1239. PrimBuild::vertex3f(max.x, min.y, height);
  1240. PrimBuild::vertex3f(min.x, min.y, height);
  1241. if(!i) PrimBuild::vertex3f( min.x, min.y, 0.f );
  1242. PrimBuild::end();
  1243. //
  1244. if(i){ PrimBuild::color(a); PrimBuild::begin( GFXTriangleFan, 4 ); } else { PrimBuild::color(b); PrimBuild::begin( GFXLineStrip, 5 ); }
  1245. PrimBuild::vertex3f(min.x, max.y, 0);
  1246. PrimBuild::vertex3f(max.x, max.y, 0);
  1247. PrimBuild::vertex3f(max.x, max.y, height);
  1248. PrimBuild::vertex3f(min.x, max.y, height);
  1249. if(!i) PrimBuild::vertex3f( min.x, min.y, 0.f );
  1250. PrimBuild::end();
  1251. //
  1252. if(i){ PrimBuild::color(a); PrimBuild::begin( GFXTriangleFan, 4 ); } else { PrimBuild::color(b); PrimBuild::begin( GFXLineStrip, 5 ); }
  1253. PrimBuild::vertex3f(min.x, min.y, 0);
  1254. PrimBuild::vertex3f(min.x, max.y, 0);
  1255. PrimBuild::vertex3f(min.x, max.y, height);
  1256. PrimBuild::vertex3f(min.x, min.y, height);
  1257. if(!i) PrimBuild::vertex3f( min.x, min.y, 0.f );
  1258. PrimBuild::end();
  1259. //
  1260. if(i){ PrimBuild::color(a); PrimBuild::begin( GFXTriangleFan, 4 ); } else { PrimBuild::color(b); PrimBuild::begin( GFXLineStrip, 5 ); }
  1261. PrimBuild::vertex3f(max.x, min.y, 0);
  1262. PrimBuild::vertex3f(max.x, max.y, 0);
  1263. PrimBuild::vertex3f(max.x, max.y, height);
  1264. PrimBuild::vertex3f(max.x, min.y, height);
  1265. if(!i) PrimBuild::vertex3f( min.x, min.y, 0.f );
  1266. PrimBuild::end();
  1267. }
  1268. }
  1269. */
  1270. }
  1271. void TerrainEditor::submitUndo( Selection *sel )
  1272. {
  1273. // Grab the mission editor undo manager.
  1274. UndoManager *undoMan = NULL;
  1275. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  1276. {
  1277. Con::errorf( "TerrainEditor::submitUndo() - EUndoManager not found!" );
  1278. return;
  1279. }
  1280. // Create and submit the action.
  1281. TerrainEditorUndoAction *action = new TerrainEditorUndoAction( "Terrain Editor Action" );
  1282. action->mSel = sel;
  1283. action->mTerrainEditor = this;
  1284. undoMan->addAction( action );
  1285. // Mark the editor as dirty!
  1286. setDirty();
  1287. }
  1288. void TerrainEditor::TerrainEditorUndoAction::undo()
  1289. {
  1290. // NOTE: This function also handles TerrainEditorUndoAction::redo().
  1291. bool materialChanged = false;
  1292. for (U32 i = 0; i < mSel->size(); i++)
  1293. {
  1294. // Grab the current grid info for this point.
  1295. GridInfo info;
  1296. mTerrainEditor->getGridInfo( (*mSel)[i].mGridPoint, info );
  1297. info.mMaterialChanged = (*mSel)[i].mMaterialChanged;
  1298. materialChanged |= info.mMaterialChanged;
  1299. // Restore the previous grid info.
  1300. mTerrainEditor->setGridInfo( (*mSel)[i] );
  1301. // Save the old grid info so we can
  1302. // restore it later.
  1303. (*mSel)[i] = info;
  1304. }
  1305. // Mark the editor as dirty!
  1306. mTerrainEditor->setDirty();
  1307. mTerrainEditor->gridUpdateComplete( materialChanged );
  1308. mTerrainEditor->mMouseBrush->update();
  1309. }
  1310. void TerrainEditor::submitMaterialUndo( String actionName )
  1311. {
  1312. // Grab the mission editor undo manager.
  1313. UndoManager *undoMan = NULL;
  1314. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  1315. {
  1316. Con::errorf( "TerrainEditor::submitMaterialUndo() - EUndoManager not found!" );
  1317. return;
  1318. }
  1319. TerrainBlock *terr = getClientTerrain();
  1320. // Create and submit the action.
  1321. TerrainMaterialUndoAction *action = new TerrainMaterialUndoAction( actionName );
  1322. action->mTerrain = terr;
  1323. action->mMaterials = terr->getMaterials();
  1324. action->mLayerMap = terr->getLayerMap();
  1325. action->mEditor = this;
  1326. undoMan->addAction( action );
  1327. // Mark the editor as dirty!
  1328. setDirty();
  1329. }
  1330. void TerrainEditor::onMaterialUndo( TerrainBlock *terr )
  1331. {
  1332. setDirty();
  1333. scheduleMaterialUpdate();
  1334. setGridUpdateMinMax();
  1335. terr->mDetailsDirty = true;
  1336. terr->mLayerTexDirty = true;
  1337. Con::executef( this, "onMaterialUndo" );
  1338. }
  1339. void TerrainEditor::TerrainMaterialUndoAction::undo()
  1340. {
  1341. Vector<TerrainMaterial*> tempMaterials = mTerrain->getMaterials();
  1342. Vector<U8> tempLayers = mTerrain->getLayerMap();
  1343. mTerrain->setMaterials(mMaterials);
  1344. mTerrain->setLayerMap(mLayerMap);
  1345. mMaterials = tempMaterials;
  1346. mLayerMap = tempLayers;
  1347. mEditor->onMaterialUndo( mTerrain );
  1348. }
  1349. void TerrainEditor::TerrainMaterialUndoAction::redo()
  1350. {
  1351. undo();
  1352. }
  1353. class TerrainProcessActionEvent : public SimEvent
  1354. {
  1355. U32 mSequence;
  1356. public:
  1357. TerrainProcessActionEvent(U32 seq)
  1358. {
  1359. mSequence = seq;
  1360. }
  1361. void process(SimObject *object)
  1362. {
  1363. ((TerrainEditor *) object)->processActionTick(mSequence);
  1364. }
  1365. };
  1366. void TerrainEditor::processActionTick(U32 sequence)
  1367. {
  1368. if(mMouseDownSeq == sequence)
  1369. {
  1370. Sim::postEvent(this, new TerrainProcessActionEvent(mMouseDownSeq), Sim::getCurrentTime() + 30);
  1371. mCurrentAction->process(mMouseBrush, mLastEvent, false, TerrainAction::Update);
  1372. }
  1373. }
  1374. bool TerrainEditor::onInputEvent(const InputEventInfo & event)
  1375. {
  1376. /*
  1377. if ( mRightMousePassThru &&
  1378. event.deviceType == KeyboardDeviceType &&
  1379. event.objType == SI_KEY &&
  1380. event.objInst == KEY_TAB &&
  1381. event.action == SI_MAKE )
  1382. {
  1383. if ( isMethod( "onToggleToolWindows" ) )
  1384. Con::executef( this, "onToggleToolWindows" );
  1385. }
  1386. */
  1387. return Parent::onInputEvent( event );
  1388. }
  1389. void TerrainEditor::on3DMouseDown(const Gui3DMouseEvent & event)
  1390. {
  1391. getRoot()->showCursor( false );
  1392. if(mTerrainBlocks.size() == 0)
  1393. return;
  1394. if (!String::compare(getCurrentAction(),"paintMaterial"))
  1395. {
  1396. Point3F pos;
  1397. TerrainBlock* hitTerrain = collide(event, pos);
  1398. if(!hitTerrain)
  1399. return;
  1400. // Set the active terrain
  1401. bool changed = mActiveTerrain != hitTerrain;
  1402. mActiveTerrain = hitTerrain;
  1403. if (changed)
  1404. {
  1405. Con::executef(this, "onActiveTerrainChange", Con::getIntArg(hitTerrain->getId()));
  1406. mMouseBrush->setTerrain(mActiveTerrain);
  1407. //if(mRenderBrush)
  1408. //mCursorVisible = false;
  1409. mMousePos = pos;
  1410. mMouseBrush->setPosition(mMousePos);
  1411. return;
  1412. }
  1413. }
  1414. else if ((event.modifier & SI_ALT) && !String::compare(getCurrentAction(),"setHeight"))
  1415. {
  1416. // Set value to terrain height at mouse position
  1417. GridInfo info;
  1418. getGridInfo(mMouseBrush->getGridPoint(), info);
  1419. mSetHeightVal = info.mHeight;
  1420. mBrushChanged = true;
  1421. return;
  1422. }
  1423. mMousePlane.set( mMousePos, Point3F(0,0,1) );
  1424. mMouseDown = true;
  1425. mSelectionLocked = false;
  1426. mouseLock();
  1427. mMouseDownSeq++;
  1428. mUndoSel = new Selection;
  1429. mCurrentAction->process(mMouseBrush, event, true, TerrainAction::Begin);
  1430. // process on ticks - every 30th of a second.
  1431. Sim::postEvent(this, new TerrainProcessActionEvent(mMouseDownSeq), Sim::getCurrentTime() + 30);
  1432. }
  1433. void TerrainEditor::on3DMouseMove(const Gui3DMouseEvent & event)
  1434. {
  1435. PROFILE_SCOPE( TerrainEditor_On3DMouseMove );
  1436. if(mTerrainBlocks.size() == 0)
  1437. return;
  1438. Point3F pos;
  1439. TerrainBlock* hitTerrain = collide(event, pos);
  1440. if(!hitTerrain)
  1441. {
  1442. mMouseBrush->reset();
  1443. }
  1444. else
  1445. {
  1446. // We do not change the active terrain as the mouse moves when
  1447. // in painting mode. This is because it causes the material
  1448. // window to change as you cursor over to it.
  1449. if ( String::compare(getCurrentAction(),"paintMaterial") != 0 )
  1450. {
  1451. // Set the active terrain
  1452. bool changed = mActiveTerrain != hitTerrain;
  1453. mActiveTerrain = hitTerrain;
  1454. if (changed)
  1455. Con::executef(this, "onActiveTerrainChange", Con::getIntArg(hitTerrain->getId()));
  1456. }
  1457. mMousePos = pos;
  1458. mMouseBrush->setTerrain(mActiveTerrain);
  1459. mMouseBrush->setPosition(mMousePos);
  1460. }
  1461. }
  1462. void TerrainEditor::on3DMouseDragged(const Gui3DMouseEvent & event)
  1463. {
  1464. PROFILE_SCOPE( TerrainEditor_On3DMouseDragged );
  1465. if ( mTerrainBlocks.empty() )
  1466. return;
  1467. if ( !isMouseLocked() )
  1468. return;
  1469. Point3F pos;
  1470. if ( !mSelectionLocked )
  1471. {
  1472. if ( !collide( event, pos) )
  1473. mMouseBrush->reset();
  1474. }
  1475. // check if the mouse has actually moved in grid space
  1476. bool selChanged = false;
  1477. if ( !mSelectionLocked )
  1478. {
  1479. Point2I gMouse;
  1480. Point2I gLastMouse;
  1481. worldToGrid( pos, gMouse );
  1482. worldToGrid( mMousePos, gLastMouse );
  1483. mMousePos = pos;
  1484. mMouseBrush->setPosition( mMousePos );
  1485. selChanged = gMouse != gLastMouse;
  1486. }
  1487. if ( mMouseDown )
  1488. return;
  1489. mCurrentAction->process( mMouseBrush, event, true, TerrainAction::Update );
  1490. }
  1491. void TerrainEditor::on3DMouseUp(const Gui3DMouseEvent & event)
  1492. {
  1493. mMouseDown = false;
  1494. getRoot()->showCursor( true );
  1495. if ( mTerrainBlocks.size() == 0 )
  1496. return;
  1497. if ( isMouseLocked() )
  1498. {
  1499. mouseUnlock();
  1500. mMouseDownSeq++;
  1501. mCurrentAction->process( mMouseBrush, event, false, TerrainAction::End );
  1502. if ( mUndoSel->size() )
  1503. submitUndo( mUndoSel );
  1504. else
  1505. delete mUndoSel;
  1506. mUndoSel = 0;
  1507. mInAction = false;
  1508. }
  1509. }
  1510. bool TerrainEditor::onMouseWheelDown( const GuiEvent & event )
  1511. {
  1512. if ( event.modifier & SI_PRIMARY_CTRL && event.modifier & SI_SHIFT )
  1513. {
  1514. setBrushPressure( mBrushPressure - 0.1f );
  1515. return true;
  1516. }
  1517. else if ( event.modifier & SI_SHIFT )
  1518. {
  1519. setBrushSoftness( mBrushSoftness + 0.05f );
  1520. return true;
  1521. }
  1522. else if ( event.modifier & SI_PRIMARY_CTRL )
  1523. {
  1524. Point2I newBrush = getBrushSize() - Point2I(1,1);
  1525. setBrushSize( newBrush.x, newBrush.y );
  1526. return true;
  1527. }
  1528. return Parent::onMouseWheelDown( event );
  1529. }
  1530. bool TerrainEditor::onMouseWheelUp( const GuiEvent & event )
  1531. {
  1532. if ( event.modifier & SI_PRIMARY_CTRL && event.modifier & SI_SHIFT )
  1533. {
  1534. setBrushPressure( mBrushPressure + 0.1f );
  1535. return true;
  1536. }
  1537. else if ( event.modifier & SI_SHIFT )
  1538. {
  1539. setBrushSoftness( mBrushSoftness - 0.05f );
  1540. return true;
  1541. }
  1542. else if( event.modifier & SI_PRIMARY_CTRL )
  1543. {
  1544. Point2I newBrush = getBrushSize() + Point2I(1,1);
  1545. setBrushSize( newBrush.x, newBrush.y );
  1546. return true;
  1547. }
  1548. return Parent::onMouseWheelUp( event );
  1549. }
  1550. //------------------------------------------------------------------------------
  1551. // any console function which depends on a terrainBlock attached to the editor
  1552. // should call this
  1553. bool checkTerrainBlock(TerrainEditor * object, const char * funcName)
  1554. {
  1555. if(!object->terrainBlockValid())
  1556. {
  1557. Con::errorf(ConsoleLogEntry::Script, "TerrainEditor::%s: not attached to a terrain block!", funcName);
  1558. return(false);
  1559. }
  1560. return(true);
  1561. }
  1562. void TerrainEditor::attachTerrain(TerrainBlock *terrBlock)
  1563. {
  1564. mActiveTerrain = terrBlock;
  1565. mTerrainBlocks.push_back_unique(terrBlock);
  1566. }
  1567. void TerrainEditor::detachTerrain(TerrainBlock *terrBlock)
  1568. {
  1569. if (mActiveTerrain == terrBlock)
  1570. mActiveTerrain = NULL; //do we want to set this to an existing terrain?
  1571. if (mMouseBrush->getGridPoint().terrainBlock == terrBlock)
  1572. mMouseBrush->setTerrain(NULL);
  1573. // reset the brush as its gridinfos may still have references to the old terrain
  1574. mMouseBrush->reset();
  1575. mTerrainBlocks.remove(terrBlock);
  1576. }
  1577. TerrainBlock* TerrainEditor::getTerrainBlock(S32 index)
  1578. {
  1579. if(index < 0 || index >= mTerrainBlocks.size())
  1580. return NULL;
  1581. return mTerrainBlocks[index];
  1582. }
  1583. void TerrainEditor::getTerrainBlocksMaterialList(Vector<StringTableEntry>& list)
  1584. {
  1585. for(S32 i=0; i<mTerrainBlocks.size(); ++i)
  1586. {
  1587. TerrainBlock* tb = mTerrainBlocks[i];
  1588. if(!tb)
  1589. continue;
  1590. for(S32 m=0; m<tb->getMaterialCount(); ++m)
  1591. {
  1592. TerrainMaterial* mat = tb->getMaterial(m);
  1593. if (mat)
  1594. list.push_back_unique(mat->getInternalName());
  1595. }
  1596. }
  1597. }
  1598. void TerrainEditor::setBrushType( const char *type )
  1599. {
  1600. if ( mMouseBrush && String::compare( mMouseBrush->getType(), type ) == 0 )
  1601. return;
  1602. if(!dStricmp(type, "box"))
  1603. {
  1604. delete mMouseBrush;
  1605. mMouseBrush = new BoxBrush(this);
  1606. mBrushChanged = true;
  1607. }
  1608. else if(!dStricmp(type, "ellipse"))
  1609. {
  1610. delete mMouseBrush;
  1611. mMouseBrush = new EllipseBrush(this);
  1612. mBrushChanged = true;
  1613. }
  1614. else if(!dStricmp(type, "selection"))
  1615. {
  1616. delete mMouseBrush;
  1617. mMouseBrush = new SelectionBrush(this);
  1618. mBrushChanged = true;
  1619. }
  1620. else {}
  1621. }
  1622. const char* TerrainEditor::getBrushType() const
  1623. {
  1624. if ( mMouseBrush )
  1625. return mMouseBrush->getType();
  1626. return "";
  1627. }
  1628. void TerrainEditor::setBrushSize( S32 w, S32 h )
  1629. {
  1630. w = mClamp( w, 1, mMaxBrushSize.x );
  1631. h = mClamp( h, 1, mMaxBrushSize.y );
  1632. if ( w == mBrushSize.x && h == mBrushSize.y )
  1633. return;
  1634. mBrushSize.set( w, h );
  1635. mBrushChanged = true;
  1636. if ( mMouseBrush )
  1637. {
  1638. mMouseBrush->setSize( mBrushSize );
  1639. if ( mMouseBrush->getGridPoint().terrainBlock )
  1640. mMouseBrush->rebuild();
  1641. }
  1642. }
  1643. void TerrainEditor::setBrushPressure( F32 pressure )
  1644. {
  1645. pressure = mClampF( pressure, 0.01f, 1.0f );
  1646. if ( mBrushPressure == pressure )
  1647. return;
  1648. mBrushPressure = pressure;
  1649. mBrushChanged = true;
  1650. if ( mMouseBrush && mMouseBrush->getGridPoint().terrainBlock )
  1651. mMouseBrush->rebuild();
  1652. }
  1653. void TerrainEditor::setBrushSoftness( F32 softness )
  1654. {
  1655. softness = mClampF( softness, 0.01f, 1.0f );
  1656. if ( mBrushSoftness == softness )
  1657. return;
  1658. mBrushSoftness = softness;
  1659. mBrushChanged = true;
  1660. if ( mMouseBrush && mMouseBrush->getGridPoint().terrainBlock )
  1661. mMouseBrush->rebuild();
  1662. }
  1663. const char* TerrainEditor::getBrushPos()
  1664. {
  1665. AssertFatal(mMouseBrush!=NULL, "TerrainEditor::getBrushPos: no mouse brush!");
  1666. Point2I pos = mMouseBrush->getPosition();
  1667. static const U32 bufSize = 32;
  1668. char * ret = Con::getReturnBuffer(bufSize);
  1669. dSprintf(ret, bufSize, "%d %d", pos.x, pos.y);
  1670. return(ret);
  1671. }
  1672. void TerrainEditor::setBrushPos(Point2I pos)
  1673. {
  1674. AssertFatal(mMouseBrush!=NULL, "TerrainEditor::setBrushPos: no mouse brush!");
  1675. mMouseBrush->setPosition(pos);
  1676. }
  1677. void TerrainEditor::setAction(const char* action)
  1678. {
  1679. for(U32 i = 0; i < mActions.size(); i++)
  1680. {
  1681. if(!dStricmp(mActions[i]->getName(), action))
  1682. {
  1683. mCurrentAction = mActions[i];
  1684. //
  1685. mRenderBrush = mCurrentAction->useMouseBrush();
  1686. return;
  1687. }
  1688. }
  1689. }
  1690. const char* TerrainEditor::getActionName(U32 index)
  1691. {
  1692. if(index >= mActions.size())
  1693. return("");
  1694. return(mActions[index]->getName());
  1695. }
  1696. const char* TerrainEditor::getCurrentAction() const
  1697. {
  1698. return(mCurrentAction->getName());
  1699. }
  1700. S32 TerrainEditor::getNumActions()
  1701. {
  1702. return(mActions.size());
  1703. }
  1704. void TerrainEditor::resetSelWeights(bool clear)
  1705. {
  1706. //
  1707. if(!clear)
  1708. {
  1709. for(U32 i = 0; i < mDefaultSel.size(); i++)
  1710. {
  1711. mDefaultSel[i].mPrimarySelect = false;
  1712. mDefaultSel[i].mWeight = 1.f;
  1713. }
  1714. return;
  1715. }
  1716. Selection sel;
  1717. U32 i;
  1718. for(i = 0; i < mDefaultSel.size(); i++)
  1719. {
  1720. if(mDefaultSel[i].mPrimarySelect)
  1721. {
  1722. mDefaultSel[i].mWeight = 1.f;
  1723. sel.add(mDefaultSel[i]);
  1724. }
  1725. }
  1726. mDefaultSel.reset();
  1727. for(i = 0; i < sel.size(); i++)
  1728. mDefaultSel.add(sel[i]);
  1729. }
  1730. void TerrainEditor::clearSelection()
  1731. {
  1732. mDefaultSel.reset();
  1733. }
  1734. void TerrainEditor::processAction(const char* sAction)
  1735. {
  1736. if(!checkTerrainBlock(this, "processAction"))
  1737. return;
  1738. TerrainAction * action = mCurrentAction;
  1739. if (String::compare(sAction, "") != 0)
  1740. {
  1741. action = lookupAction(sAction);
  1742. if(!action)
  1743. {
  1744. Con::errorf(ConsoleLogEntry::General, "TerrainEditor::cProcessAction: invalid action name '%s'.", sAction);
  1745. return;
  1746. }
  1747. }
  1748. if(!getCurrentSel()->size() && !mProcessUsesBrush)
  1749. return;
  1750. mUndoSel = new Selection;
  1751. Gui3DMouseEvent event;
  1752. if(mProcessUsesBrush)
  1753. action->process(mMouseBrush, event, true, TerrainAction::Process);
  1754. else
  1755. action->process(getCurrentSel(), event, true, TerrainAction::Process);
  1756. // check if should delete the undo
  1757. if(mUndoSel->size())
  1758. submitUndo( mUndoSel );
  1759. else
  1760. delete mUndoSel;
  1761. mUndoSel = 0;
  1762. }
  1763. S32 TerrainEditor::getNumTextures()
  1764. {
  1765. if(!checkTerrainBlock(this, "getNumTextures"))
  1766. return(0);
  1767. // walk all the possible material lists and count them..
  1768. U32 count = 0;
  1769. for (U32 t = 0; t < mTerrainBlocks.size(); t++)
  1770. count += mTerrainBlocks[t]->getMaterialCount();
  1771. return count;
  1772. }
  1773. void TerrainEditor::markEmptySquares()
  1774. {
  1775. if(!checkTerrainBlock(this, "markEmptySquares"))
  1776. return;
  1777. }
  1778. void TerrainEditor::mirrorTerrain(S32 mirrorIndex)
  1779. {
  1780. if(!checkTerrainBlock(this, "mirrorTerrain"))
  1781. return;
  1782. // TODO!
  1783. /*
  1784. TerrainBlock * terrain = mActiveTerrain;
  1785. setDirty();
  1786. //
  1787. enum {
  1788. top = BIT(0),
  1789. bottom = BIT(1),
  1790. left = BIT(2),
  1791. right = BIT(3)
  1792. };
  1793. U32 sides[8] =
  1794. {
  1795. bottom,
  1796. bottom | left,
  1797. left,
  1798. left | top,
  1799. top,
  1800. top | right,
  1801. right,
  1802. bottom | right
  1803. };
  1804. U32 n = TerrainBlock::BlockSize;
  1805. U32 side = sides[mirrorIndex % 8];
  1806. bool diag = mirrorIndex & 0x01;
  1807. Point2I src((side & right) ? (n - 1) : 0, (side & bottom) ? (n - 1) : 0);
  1808. Point2I dest((side & left) ? (n - 1) : 0, (side & top) ? (n - 1) : 0);
  1809. Point2I origSrc(src);
  1810. Point2I origDest(dest);
  1811. // determine the run length
  1812. U32 minStride = ((side & top) || (side & bottom)) ? n : n / 2;
  1813. U32 majStride = ((side & left) || (side & right)) ? n : n / 2;
  1814. Point2I srcStep((side & right) ? -1 : 1, (side & bottom) ? -1 : 1);
  1815. Point2I destStep((side & left) ? -1 : 1, (side & top) ? -1 : 1);
  1816. //
  1817. U16 * heights = terrain->getHeightAddress(0,0);
  1818. U8 * baseMaterials = terrain->getBaseMaterialAddress(0,0);
  1819. TerrainBlock::Material * materials = terrain->getMaterial(0,0);
  1820. // create an undo selection
  1821. Selection * undo = new Selection;
  1822. // walk through all the positions
  1823. for(U32 i = 0; i < majStride; i++)
  1824. {
  1825. for(U32 j = 0; j < minStride; j++)
  1826. {
  1827. // skip the same position
  1828. if(src != dest)
  1829. {
  1830. U32 si = src.x + (src.y << TerrainBlock::BlockShift);
  1831. U32 di = dest.x + (dest.y << TerrainBlock::BlockShift);
  1832. // add to undo selection
  1833. GridInfo info;
  1834. getGridInfo(dest, info, terrain);
  1835. undo->add(info);
  1836. //... copy info... (height, basematerial, material)
  1837. heights[di] = heights[si];
  1838. baseMaterials[di] = baseMaterials[si];
  1839. materials[di] = materials[si];
  1840. }
  1841. // get to the new position
  1842. src.x += srcStep.x;
  1843. diag ? (dest.y += destStep.y) : (dest.x += destStep.x);
  1844. }
  1845. // get the next position for a run
  1846. src.y += srcStep.y;
  1847. diag ? (dest.x += destStep.x) : (dest.y += destStep.y);
  1848. // reset the minor run
  1849. src.x = origSrc.x;
  1850. diag ? (dest.y = origDest.y) : (dest.x = origDest.x);
  1851. // shorten the run length for diag runs
  1852. if(diag)
  1853. minStride--;
  1854. }
  1855. // rebuild stuff..
  1856. terrain->buildGridMap();
  1857. terrain->rebuildEmptyFlags();
  1858. terrain->packEmptySquares();
  1859. // add undo selection
  1860. submitUndo( undo );
  1861. */
  1862. }
  1863. bool TerrainEditor::isPointInTerrain( const GridPoint & gPoint)
  1864. {
  1865. PROFILE_SCOPE( TerrainEditor_IsPointInTerrain );
  1866. Point2I cPos;
  1867. gridToCenter( gPoint.gridPos, cPos );
  1868. const TerrainFile *file = gPoint.terrainBlock->getFile();
  1869. return file->isPointInTerrain( cPos.x, cPos.y );
  1870. }
  1871. void TerrainEditor::reorderMaterial( S32 index, S32 orderPos )
  1872. {
  1873. TerrainBlock *terr = getClientTerrain();
  1874. Vector<U8> layerMap = terr->getLayerMap();
  1875. Vector<TerrainMaterial*> materials = terr->getMaterials();
  1876. TerrainMaterial *pMat = materials[index];
  1877. submitMaterialUndo( String::ToString( "Reordered %s Material", terr->getMaterialName(index) ) );
  1878. materials.erase( index );
  1879. materials.insert( orderPos, pMat );
  1880. Vector<U8>::iterator itr = layerMap.begin();
  1881. for ( ; itr != layerMap.end(); itr++ )
  1882. {
  1883. // Was previous material, set to new index.
  1884. if ( *itr == index )
  1885. *itr = orderPos;
  1886. else
  1887. {
  1888. // We removed a Material prior to this one, bump it down.
  1889. if ( *itr > index )
  1890. (*itr)--;
  1891. // We added a Material prior to this one, bump it up.
  1892. if ( *itr >= orderPos )
  1893. (*itr)++;
  1894. }
  1895. }
  1896. terr->setMaterials( materials );
  1897. terr->setLayerMap( layerMap );
  1898. // We didn't really just "undo" but it happens to do everything we
  1899. // need to update the materials and gui.
  1900. onMaterialUndo( terr );
  1901. }
  1902. //------------------------------------------------------------------------------
  1903. DefineEngineMethod( TerrainEditor, attachTerrain, void, (const char * terrain), (""), "(TerrainBlock terrain)")
  1904. {
  1905. Scene* scene = Scene::getRootScene();
  1906. if (!scene)
  1907. {
  1908. Con::errorf(ConsoleLogEntry::Script, "TerrainEditor::attach: no scene found");
  1909. return;
  1910. }
  1911. VectorPtr<TerrainBlock*> terrains;
  1912. // attach to first found terrainBlock
  1913. if (String::compare (terrain,"")==0)
  1914. {
  1915. for(SimSetIterator itr(scene); *itr; ++itr)
  1916. {
  1917. TerrainBlock* terrBlock = dynamic_cast<TerrainBlock*>(*itr);
  1918. if (terrBlock)
  1919. terrains.push_back(terrBlock);
  1920. }
  1921. //if (terrains.size() == 0)
  1922. // Con::errorf(ConsoleLogEntry::Script, "TerrainEditor::attach: no TerrainBlock objects found!");
  1923. }
  1924. else // attach to named object
  1925. {
  1926. TerrainBlock* terrBlock = dynamic_cast<TerrainBlock*>(Sim::findObject(terrain));
  1927. if (terrBlock)
  1928. terrains.push_back(terrBlock);
  1929. if(terrains.size() == 0)
  1930. Con::errorf(ConsoleLogEntry::Script, "TerrainEditor::attach: failed to attach to object '%s'", terrain);
  1931. }
  1932. if (terrains.size() > 0)
  1933. {
  1934. for (U32 i = 0; i < terrains.size(); i++)
  1935. {
  1936. if (!terrains[i]->isServerObject())
  1937. {
  1938. terrains[i] = NULL;
  1939. Con::errorf(ConsoleLogEntry::Script, "TerrainEditor::attach: cannot attach to client TerrainBlock");
  1940. }
  1941. }
  1942. }
  1943. for (U32 i = 0; i < terrains.size(); i++)
  1944. {
  1945. if (terrains[i])
  1946. object->attachTerrain(terrains[i]);
  1947. }
  1948. }
  1949. DefineEngineMethod( TerrainEditor, getTerrainBlockCount, S32, (), , "()")
  1950. {
  1951. return object->getTerrainBlockCount();
  1952. }
  1953. DefineEngineMethod( TerrainEditor, getTerrainBlock, S32, (S32 index), , "(S32 index)")
  1954. {
  1955. TerrainBlock* tb = object->getTerrainBlock(index);
  1956. if(!tb)
  1957. return 0;
  1958. else
  1959. return tb->getId();
  1960. }
  1961. DefineEngineMethod(TerrainEditor, getTerrainBlocksMaterialList, const char *, (), , "() gets the list of current terrain materials for all terrain blocks.")
  1962. {
  1963. Vector<StringTableEntry> list;
  1964. object->getTerrainBlocksMaterialList(list);
  1965. if(list.size() == 0)
  1966. return "";
  1967. // Calculate the size of the return buffer
  1968. S32 size = 0;
  1969. for(U32 i = 0; i < list.size(); ++i)
  1970. {
  1971. size += dStrlen(list[i]);
  1972. ++size;
  1973. }
  1974. ++size;
  1975. // Copy the material names
  1976. char *ret = Con::getReturnBuffer(size);
  1977. ret[0] = 0;
  1978. for(U32 i = 0; i < list.size(); ++i)
  1979. {
  1980. dStrcat( ret, list[i], size );
  1981. dStrcat( ret, "\n", size );
  1982. }
  1983. return ret;
  1984. }
  1985. DefineEngineMethod( TerrainEditor, setBrushType, void, (String type), , "(string type)"
  1986. "One of box, ellipse, selection.")
  1987. {
  1988. object->setBrushType(type);
  1989. }
  1990. DefineEngineMethod( TerrainEditor, getBrushType, const char*, (), , "()")
  1991. {
  1992. return object->getBrushType();
  1993. }
  1994. DefineEngineMethod( TerrainEditor, setBrushSize, void, ( S32 w, S32 h), (0), "(int w [, int h])")
  1995. {
  1996. object->setBrushSize( w, h==0?w:h );
  1997. }
  1998. DefineEngineMethod( TerrainEditor, getBrushSize, const char*, (), , "()")
  1999. {
  2000. Point2I size = object->getBrushSize();
  2001. static const U32 bufSize = 32;
  2002. char * ret = Con::getReturnBuffer(bufSize);
  2003. dSprintf(ret, bufSize, "%d %d", size.x, size.y);
  2004. return ret;
  2005. }
  2006. DefineEngineMethod( TerrainEditor, setBrushPressure, void, (F32 pressure), , "(float pressure)")
  2007. {
  2008. object->setBrushPressure( pressure );
  2009. }
  2010. DefineEngineMethod( TerrainEditor, getBrushPressure, F32, (), , "()")
  2011. {
  2012. return object->getBrushPressure();
  2013. }
  2014. DefineEngineMethod( TerrainEditor, setBrushSoftness, void, (F32 softness), , "(float softness)")
  2015. {
  2016. object->setBrushSoftness( softness );
  2017. }
  2018. DefineEngineMethod( TerrainEditor, getBrushSoftness, F32, (), , "()")
  2019. {
  2020. return object->getBrushSoftness();
  2021. }
  2022. DefineEngineMethod( TerrainEditor, getBrushPos, const char*, (), , "Returns a Point2I.")
  2023. {
  2024. return object->getBrushPos();
  2025. }
  2026. DefineEngineMethod( TerrainEditor, setBrushPos, void, (Point2I pos), , "Location")
  2027. {
  2028. object->setBrushPos(pos);
  2029. }
  2030. DefineEngineMethod( TerrainEditor, setAction, void, (const char * action_name), , "(string action_name)")
  2031. {
  2032. object->setAction(action_name);
  2033. }
  2034. DefineEngineMethod( TerrainEditor, getActionName, const char*, (U32 index), , "(int num)")
  2035. {
  2036. return (object->getActionName(index));
  2037. }
  2038. DefineEngineMethod( TerrainEditor, getNumActions, S32, (), , "")
  2039. {
  2040. return(object->getNumActions());
  2041. }
  2042. DefineEngineMethod( TerrainEditor, getCurrentAction, const char*, (), , "")
  2043. {
  2044. return object->getCurrentAction();
  2045. }
  2046. DefineEngineMethod( TerrainEditor, resetSelWeights, void, (bool clear), , "(bool clear)")
  2047. {
  2048. object->resetSelWeights(clear);
  2049. }
  2050. DefineEngineMethod( TerrainEditor, clearSelection, void, (), , "")
  2051. {
  2052. object->clearSelection();
  2053. }
  2054. DefineEngineMethod( TerrainEditor, processAction, void, (String action), (""), "(string action=NULL)")
  2055. {
  2056. object->processAction(action);
  2057. }
  2058. DefineEngineMethod( TerrainEditor, getActiveTerrain, S32, (), , "")
  2059. {
  2060. S32 ret = 0;
  2061. TerrainBlock* terrain = object->getActiveTerrain();
  2062. if (terrain)
  2063. ret = terrain->getId();
  2064. return ret;
  2065. }
  2066. DefineEngineMethod( TerrainEditor, getNumTextures, S32, (), , "")
  2067. {
  2068. return object->getNumTextures();
  2069. }
  2070. DefineEngineMethod( TerrainEditor, markEmptySquares, void, (), , "")
  2071. {
  2072. object->markEmptySquares();
  2073. }
  2074. DefineEngineMethod( TerrainEditor, mirrorTerrain, void, (S32 mirrorIndex), , "")
  2075. {
  2076. object->mirrorTerrain(mirrorIndex);
  2077. }
  2078. DefineEngineMethod(TerrainEditor, setTerraformOverlay, void, (bool overlayEnable), , "(bool overlayEnable) - sets the terraformer current heightmap to draw as an overlay over the current terrain.")
  2079. {
  2080. // XA: This one needs to be implemented :)
  2081. }
  2082. DefineEngineMethod(TerrainEditor, updateMaterial, bool, ( U32 index, String matName ), ,
  2083. "( int index, string matName )\n"
  2084. "Changes the material name at the index." )
  2085. {
  2086. TerrainBlock *terr = object->getClientTerrain();
  2087. if ( !terr )
  2088. return false;
  2089. if ( index >= terr->getMaterialCount() )
  2090. return false;
  2091. terr->updateMaterial( index, matName );
  2092. object->setDirty();
  2093. return true;
  2094. }
  2095. DefineEngineMethod(TerrainEditor, addMaterial, S32, ( String matName ), ,
  2096. "( string matName )\n"
  2097. "Adds a new material." )
  2098. {
  2099. TerrainBlock *terr = object->getClientTerrain();
  2100. if ( !terr )
  2101. return false;
  2102. terr->addMaterial( matName );
  2103. object->setDirty();
  2104. return true;
  2105. }
  2106. DefineEngineMethod( TerrainEditor, removeMaterial, void, ( S32 index ), , "( int index ) - Remove the material at the given index." )
  2107. {
  2108. TerrainBlock *terr = object->getClientTerrain();
  2109. if ( !terr )
  2110. return;
  2111. if ( index < 0 || index >= terr->getMaterialCount() )
  2112. {
  2113. Con::errorf( "TerrainEditor::removeMaterial - index out of range!" );
  2114. return;
  2115. }
  2116. if ( terr->getMaterialCount() == 1 )
  2117. {
  2118. Con::errorf( "TerrainEditor::removeMaterial - cannot remove material, there is only one!" );
  2119. return;
  2120. }
  2121. const char *matName = terr->getMaterialName( index );
  2122. object->submitMaterialUndo( String::ToString( "Remove TerrainMaterial %s", matName ) );
  2123. terr->removeMaterial( index );
  2124. object->setDirty();
  2125. object->scheduleMaterialUpdate();
  2126. object->setGridUpdateMinMax();
  2127. }
  2128. DefineEngineMethod(TerrainEditor, getMaterialCount, S32, (), ,
  2129. "Returns the current material count." )
  2130. {
  2131. TerrainBlock *terr = object->getClientTerrain();
  2132. if ( terr )
  2133. return terr->getMaterialCount();
  2134. return 0;
  2135. }
  2136. DefineEngineMethod(TerrainEditor, getMaterials, const char *, (), , "() gets the list of current terrain materials.")
  2137. {
  2138. TerrainBlock *terr = object->getClientTerrain();
  2139. if ( !terr )
  2140. return "";
  2141. char *ret = Con::getReturnBuffer(4096);
  2142. ret[0] = 0;
  2143. for(U32 i = 0; i < terr->getMaterialCount(); i++)
  2144. {
  2145. dStrcat( ret, terr->getMaterialName(i), 4096 );
  2146. dStrcat( ret, "\n", 4096 );
  2147. }
  2148. return ret;
  2149. }
  2150. DefineEngineMethod( TerrainEditor, getMaterialName, const char*, (S32 index), , "( int index ) - Returns the name of the material at the given index." )
  2151. {
  2152. TerrainBlock *terr = object->getClientTerrain();
  2153. if ( !terr )
  2154. return "";
  2155. if( index < 0 || index >= terr->getMaterialCount() )
  2156. {
  2157. Con::errorf( "TerrainEditor::getMaterialName - index out of range!" );
  2158. return "";
  2159. }
  2160. const char* name = terr->getMaterialName( index );
  2161. return Con::getReturnBuffer( name );
  2162. }
  2163. DefineEngineMethod( TerrainEditor, getMaterialIndex, S32, ( String name ), , "( string name ) - Returns the index of the material with the given name or -1." )
  2164. {
  2165. TerrainBlock *terr = object->getClientTerrain();
  2166. if ( !terr )
  2167. return -1;
  2168. const U32 count = terr->getMaterialCount();
  2169. for( U32 i = 0; i < count; ++ i )
  2170. if( dStricmp( name, terr->getMaterialName( i ) ) == 0 )
  2171. return i;
  2172. return -1;
  2173. }
  2174. DefineEngineMethod( TerrainEditor, reorderMaterial, void, ( S32 index, S32 orderPos ), , "( int index, int order ) "
  2175. "- Reorder material at the given index to the new position, changing the order in which it is rendered / blended." )
  2176. {
  2177. object->reorderMaterial( index, orderPos );
  2178. }
  2179. DefineEngineMethod(TerrainEditor, getTerrainUnderWorldPoint, S32, (const char * ptOrX, const char * Y, const char * Z), ("", "", ""),
  2180. "(x/y/z) Gets the terrain block that is located under the given world point.\n"
  2181. "@param x/y/z The world coordinates (floating point values) you wish to query at. "
  2182. "These can be formatted as either a string (\"x y z\") or separately as (x, y, z)\n"
  2183. "@return Returns the ID of the requested terrain block (0 if not found).\n\n")
  2184. {
  2185. TerrainEditor *tEditor = (TerrainEditor *) object;
  2186. if(tEditor == NULL)
  2187. return 0;
  2188. Point3F pos;
  2189. if(!String::isEmpty(ptOrX) && String::isEmpty(Y) && String::isEmpty(Z))
  2190. dSscanf(ptOrX, "%f %f %f", &pos.x, &pos.y, &pos.z);
  2191. else if(!String::isEmpty(ptOrX) && !String::isEmpty(Y) && !String::isEmpty(Z))
  2192. {
  2193. pos.x = dAtof(ptOrX);
  2194. pos.y = dAtof(Y);
  2195. pos.z = dAtof(Z);
  2196. }
  2197. else
  2198. {
  2199. Con::errorf("TerrainEditor.getTerrainUnderWorldPoint(): Invalid argument count! Valid arguments are either \"x y z\" or x,y,z\n");
  2200. return 0;
  2201. }
  2202. TerrainBlock* terrain = tEditor->getTerrainUnderWorldPoint(pos);
  2203. if(terrain != NULL)
  2204. {
  2205. return terrain->getId();
  2206. }
  2207. return 0;
  2208. }
  2209. //------------------------------------------------------------------------------
  2210. void TerrainEditor::initPersistFields()
  2211. {
  2212. addGroup("Misc");
  2213. addField("isDirty", TypeBool, Offset(mIsDirty, TerrainEditor));
  2214. addField("isMissionDirty", TypeBool, Offset(mIsMissionDirty, TerrainEditor));
  2215. addField("renderBorder", TypeBool, Offset(mRenderBorder, TerrainEditor)); ///< Not currently used
  2216. addField("borderHeight", TypeF32, Offset(mBorderHeight, TerrainEditor)); ///< Not currently used
  2217. addField("borderFillColor", TypeColorI, Offset(mBorderFillColor, TerrainEditor)); ///< Not currently used
  2218. addField("borderFrameColor", TypeColorI, Offset(mBorderFrameColor, TerrainEditor)); ///< Not currently used
  2219. addField("borderLineMode", TypeBool, Offset(mBorderLineMode, TerrainEditor)); ///< Not currently used
  2220. addField("selectionHidden", TypeBool, Offset(mSelectionHidden, TerrainEditor));
  2221. addField("renderVertexSelection", TypeBool, Offset(mRenderVertexSelection, TerrainEditor)); ///< Not currently used
  2222. addField("renderSolidBrush", TypeBool, Offset(mRenderSolidBrush, TerrainEditor));
  2223. addField("processUsesBrush", TypeBool, Offset(mProcessUsesBrush, TerrainEditor));
  2224. addField("maxBrushSize", TypePoint2I, Offset(mMaxBrushSize, TerrainEditor));
  2225. // action values...
  2226. addField("adjustHeightVal", TypeF32, Offset(mAdjustHeightVal, TerrainEditor)); ///< RaiseHeightAction and LowerHeightAction
  2227. addField("setHeightVal", TypeF32, Offset(mSetHeightVal, TerrainEditor)); ///< SetHeightAction
  2228. addField("scaleVal", TypeF32, Offset(mScaleVal, TerrainEditor)); ///< ScaleHeightAction
  2229. addField("smoothFactor", TypeF32, Offset(mSmoothFactor, TerrainEditor)); ///< SmoothHeightAction
  2230. addField("noiseFactor", TypeF32, Offset(mNoiseFactor, TerrainEditor)); ///< PaintNoiseAction
  2231. addField("materialGroup", TypeS32, Offset(mMaterialGroup, TerrainEditor)); ///< Not currently used
  2232. addField("softSelectRadius", TypeF32, Offset(mSoftSelectRadius, TerrainEditor)); ///< SoftSelectAction
  2233. addField("softSelectFilter", TypeString, Offset(mSoftSelectFilter, TerrainEditor)); ///< SoftSelectAction brush filtering
  2234. addField("softSelectDefaultFilter", TypeString, Offset(mSoftSelectDefaultFilter, TerrainEditor)); ///< SoftSelectAction brush filtering
  2235. addField("adjustHeightMouseScale", TypeF32, Offset(mAdjustHeightMouseScale, TerrainEditor)); ///< Not currently used
  2236. addField("paintIndex", TypeS32, Offset(mPaintIndex, TerrainEditor)); ///< PaintMaterialAction
  2237. endGroup("Misc");
  2238. Parent::initPersistFields();
  2239. }
  2240. DefineEngineMethod( TerrainEditor, getSlopeLimitMinAngle, F32, (), , "")
  2241. {
  2242. return object->mSlopeMinAngle;
  2243. }
  2244. DefineEngineMethod( TerrainEditor, setSlopeLimitMinAngle, F32, (F32 angle), , "")
  2245. {
  2246. if ( angle < 0.0f )
  2247. angle = 0.0f;
  2248. if ( angle > object->mSlopeMaxAngle )
  2249. angle = object->mSlopeMaxAngle;
  2250. object->mSlopeMinAngle = angle;
  2251. return angle;
  2252. }
  2253. DefineEngineMethod( TerrainEditor, getSlopeLimitMaxAngle, F32, (), , "")
  2254. {
  2255. return object->mSlopeMaxAngle;
  2256. }
  2257. DefineEngineMethod( TerrainEditor, setSlopeLimitMaxAngle, F32, (F32 angle), , "")
  2258. {
  2259. if ( angle > 90.0f )
  2260. angle = 90.0f;
  2261. if ( angle < object->mSlopeMinAngle )
  2262. angle = object->mSlopeMinAngle;
  2263. object->mSlopeMaxAngle = angle;
  2264. return angle;
  2265. }
  2266. //------------------------------------------------------------------------------
  2267. void TerrainEditor::autoMaterialLayer( F32 mMinHeight, F32 mMaxHeight, F32 mMinSlope, F32 mMaxSlope, F32 mCoverage )
  2268. {
  2269. #define AUTOPAINT_UNDO
  2270. if (!mActiveTerrain)
  2271. return;
  2272. S32 mat = getPaintMaterialIndex();
  2273. if (mat == -1)
  2274. return;
  2275. #ifndef AUTOPAINT_UNDO
  2276. mUndoSel = new Selection;
  2277. #endif
  2278. U32 terrBlocks = mActiveTerrain->getBlockSize();
  2279. for (U32 y = 0; y < terrBlocks; y++)
  2280. {
  2281. for (U32 x = 0; x < terrBlocks; x++)
  2282. {
  2283. // get info
  2284. GridPoint gp;
  2285. gp.terrainBlock = mActiveTerrain;
  2286. gp.gridPos.set(x, y);
  2287. GridInfo gi;
  2288. getGridInfo(gp, gi);
  2289. if (gi.mMaterial == mat)
  2290. continue;
  2291. if (mRandI(0, 100) > mCoverage)
  2292. continue;
  2293. Point3F wp;
  2294. gridToWorld(gp, wp);
  2295. if (!(wp.z >= mMinHeight && wp.z <= mMaxHeight))
  2296. continue;
  2297. // transform wp to object space
  2298. Point3F op;
  2299. mActiveTerrain->getWorldTransform().mulP(wp, &op);
  2300. Point3F norm;
  2301. mActiveTerrain->getNormal(Point2F(op.x, op.y), &norm, true);
  2302. if (mMinSlope > 0)
  2303. if (norm.z > mSin(mDegToRad(90.0f - mMinSlope)))
  2304. continue;
  2305. if (mMaxSlope < 90)
  2306. if (norm.z < mSin(mDegToRad(90.0f - mMaxSlope)))
  2307. continue;
  2308. gi.mMaterialChanged = true;
  2309. #ifndef AUTOPAINT_UNDO
  2310. mUndoSel->add(gi);
  2311. #endif
  2312. gi.mMaterial = mat;
  2313. setGridInfo(gi);
  2314. }
  2315. }
  2316. #ifndef AUTOPAINT_UNDO
  2317. if(mUndoSel->size())
  2318. submitUndo( mUndoSel );
  2319. else
  2320. delete mUndoSel;
  2321. mUndoSel = 0;
  2322. #endif
  2323. scheduleMaterialUpdate();
  2324. }
  2325. DefineEngineMethod( TerrainEditor, autoMaterialLayer, void, (F32 minHeight, F32 maxHeight, F32 minSlope, F32 maxSlope, F32 coverage),,
  2326. "Rule based terrain painting.\n"
  2327. "@param minHeight Minimum terrain height."
  2328. "@param maxHeight Maximum terrain height."
  2329. "@param minSlope Minimum terrain slope."
  2330. "@param maxSlope Maximum terrain slope."
  2331. "@param coverage Terrain coverage amount.")
  2332. {
  2333. object->autoMaterialLayer( minHeight,maxHeight, minSlope, maxSlope, coverage );
  2334. }