terrainEditor.cpp 79 KB

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