terrainEditor.cpp 80 KB

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