terrainEditor.cpp 79 KB

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