terrainEditor.cpp 79 KB

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