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