terrainEditor.cpp 80 KB

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