terrainEditor.cpp 78 KB

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