terrainEditor.cpp 79 KB

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