terrainEditor.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gui/worldEditor/terrainEditor.h"
  24. #include "core/frameAllocator.h"
  25. #include "core/strings/stringUnit.h"
  26. #include "console/consoleTypes.h"
  27. #include "console/simEvents.h"
  28. #include "sim/netConnection.h"
  29. #include "math/mathUtils.h"
  30. #include "gfx/primBuilder.h"
  31. #include "gfx/gfxDrawUtil.h"
  32. #include "gui/core/guiCanvas.h"
  33. #include "gui/worldEditor/terrainActions.h"
  34. #include "terrain/terrMaterial.h"
  35. IMPLEMENT_CONOBJECT(TerrainEditor);
  36. ConsoleDocClass( TerrainEditor,
  37. "@brief The base Terrain Editor tool\n\n"
  38. "Editor use only.\n\n"
  39. "@internal"
  40. );
  41. Selection::Selection() :
  42. Vector<GridInfo>(__FILE__, __LINE__),
  43. mName(0),
  44. mUndoFlags(0),
  45. mHashListSize(1024)
  46. {
  47. VECTOR_SET_ASSOCIATION(mHashLists);
  48. // clear the hash list
  49. mHashLists.setSize(mHashListSize);
  50. reset();
  51. }
  52. Selection::~Selection()
  53. {
  54. }
  55. void Selection::reset()
  56. {
  57. PROFILE_SCOPE( TerrainEditor_Selection_Reset );
  58. for(U32 i = 0; i < mHashListSize; i++)
  59. mHashLists[i] = -1;
  60. clear();
  61. }
  62. bool Selection::validate()
  63. {
  64. PROFILE_SCOPE( TerrainEditor_Selection_Validate );
  65. // scan all the hashes and verify that the heads they point to point back to them
  66. U32 hashesProcessed = 0;
  67. for(U32 i = 0; i < mHashLists.size(); i++)
  68. {
  69. U32 entry = mHashLists[i];
  70. if(entry == -1)
  71. continue;
  72. GridInfo info = (*this)[entry];
  73. U32 hashIndex = getHashIndex(info.mGridPoint.gridPos);
  74. if( entry != mHashLists[hashIndex] )
  75. {
  76. AssertFatal(false, "Selection hash lists corrupted");
  77. return false;
  78. }
  79. hashesProcessed++;
  80. }
  81. // scan all the entries and verify that anything w/ a prev == -1 is correctly in the hash table
  82. U32 headsProcessed = 0;
  83. for(U32 i = 0; i < size(); i++)
  84. {
  85. GridInfo info = (*this)[i];
  86. if(info.mPrev != -1)
  87. continue;
  88. U32 hashIndex = getHashIndex(info.mGridPoint.gridPos);
  89. if(mHashLists[hashIndex] != i)
  90. {
  91. AssertFatal(false, "Selection list heads corrupted");
  92. return false;
  93. }
  94. headsProcessed++;
  95. }
  96. AssertFatal(headsProcessed == hashesProcessed, "Selection's number of hashes and number of list heads differ.");
  97. return true;
  98. }
  99. U32 Selection::getHashIndex(const Point2I & pos)
  100. {
  101. PROFILE_SCOPE( TerrainEditor_Selection_GetHashIndex );
  102. Point2F pnt = Point2F((F32)pos.x, (F32)pos.y) + Point2F(1.3f,3.5f);
  103. return( (U32)(mFloor(mHashLists.size() * mFmod(pnt.len() * 0.618f, 1.0f))) );
  104. }
  105. S32 Selection::lookup(const Point2I & pos)
  106. {
  107. PROFILE_SCOPE( TerrainEditor_Selection_Lookup );
  108. U32 index = getHashIndex(pos);
  109. S32 entry = mHashLists[index];
  110. while(entry != -1)
  111. {
  112. if((*this)[entry].mGridPoint.gridPos == pos)
  113. return(entry);
  114. entry = (*this)[entry].mNext;
  115. }
  116. return(-1);
  117. }
  118. void Selection::insert(GridInfo info)
  119. {
  120. PROFILE_SCOPE( TerrainEditor_Selection_Insert );
  121. //validate();
  122. // get the index into the hash table
  123. U32 index = getHashIndex(info.mGridPoint.gridPos);
  124. // if there is an existing linked list, make it our next
  125. info.mNext = mHashLists[index];
  126. info.mPrev = -1;
  127. // if there is an existing linked list, make us it's prev
  128. U32 indexOfNewEntry = size();
  129. if(info.mNext != -1)
  130. (*this)[info.mNext].mPrev = indexOfNewEntry;
  131. // the hash table holds the heads of the linked lists. make us the head of this list.
  132. mHashLists[index] = indexOfNewEntry;
  133. // copy us into the vector
  134. push_back(info);
  135. //validate();
  136. }
  137. bool Selection::remove(const GridInfo &info)
  138. {
  139. PROFILE_SCOPE( TerrainEditor_Selection_Remove );
  140. if(size() < 1)
  141. return false;
  142. //AssertFatal( validate(), "Selection hashLists corrupted before Selection.remove()");
  143. U32 hashIndex = getHashIndex(info.mGridPoint.gridPos);
  144. S32 listHead = mHashLists[hashIndex];
  145. //AssertFatal(listHead < size(), "A Selection's hash table is corrupt.");
  146. if(listHead == -1)
  147. return(false);
  148. const S32 victimEntry = lookup(info.mGridPoint.gridPos);
  149. if( victimEntry == -1 )
  150. return(false);
  151. const GridInfo victim = (*this)[victimEntry];
  152. const S32 vicPrev = victim.mPrev;
  153. const S32 vicNext = victim.mNext;
  154. // remove us from the linked list, if there is one.
  155. if(vicPrev != -1)
  156. (*this)[vicPrev].mNext = vicNext;
  157. if(vicNext != -1)
  158. (*this)[vicNext].mPrev = vicPrev;
  159. // if we were the head of the list, make our next the new head in the hash table.
  160. if(vicPrev == -1)
  161. mHashLists[hashIndex] = vicNext;
  162. // if we're not the last element in the vector, copy the last element to our position.
  163. if(victimEntry != size() - 1)
  164. {
  165. // copy last into victim, and re-cache next & prev
  166. const GridInfo lastEntry = last();
  167. const S32 lastPrev = lastEntry.mPrev;
  168. const S32 lastNext = lastEntry.mNext;
  169. (*this)[victimEntry] = lastEntry;
  170. // update the new element's next and prev, to reestablish it in it's linked list.
  171. if(lastPrev != -1)
  172. (*this)[lastPrev].mNext = victimEntry;
  173. if(lastNext != -1)
  174. (*this)[lastNext].mPrev = victimEntry;
  175. // if it was the head of it's list, update the hash table with its new position.
  176. if(lastPrev == -1)
  177. {
  178. const U32 lastHash = getHashIndex(lastEntry.mGridPoint.gridPos);
  179. AssertFatal(mHashLists[lastHash] == size() - 1, "Selection hashLists corrupted during Selection.remove() (oldmsg)");
  180. mHashLists[lastHash] = victimEntry;
  181. }
  182. }
  183. // decrement the vector, we're done here
  184. pop_back();
  185. //AssertFatal( validate(), "Selection hashLists corrupted after Selection.remove()");
  186. return true;
  187. }
  188. bool Selection::add(const GridInfo &info)
  189. {
  190. PROFILE_SCOPE( TerrainEditor_Selection_Add );
  191. S32 index = lookup(info.mGridPoint.gridPos);
  192. if(index != -1)
  193. return(false);
  194. insert(info);
  195. return(true);
  196. }
  197. bool Selection::getInfo(Point2I pos, GridInfo & info)
  198. {
  199. PROFILE_SCOPE( TerrainEditor_Selection_GetInfo );
  200. S32 index = lookup(pos);
  201. if(index == -1)
  202. return(false);
  203. info = (*this)[index];
  204. return(true);
  205. }
  206. bool Selection::setInfo(GridInfo & info)
  207. {
  208. PROFILE_SCOPE( TerrainEditor_Selection_SetInfo );
  209. S32 index = lookup(info.mGridPoint.gridPos);
  210. if(index == -1)
  211. return(false);
  212. S32 next = (*this)[index].mNext;
  213. S32 prev = (*this)[index].mPrev;
  214. (*this)[index] = info;
  215. (*this)[index].mNext = next;
  216. (*this)[index].mPrev = prev;
  217. return(true);
  218. }
  219. F32 Selection::getAvgHeight()
  220. {
  221. PROFILE_SCOPE( TerrainEditor_Selection_GetAvgHeight );
  222. if(!size())
  223. return(0);
  224. F32 avg = 0.f;
  225. for(U32 i = 0; i < size(); i++)
  226. avg += (*this)[i].mHeight;
  227. return(avg / size());
  228. }
  229. F32 Selection::getMinHeight()
  230. {
  231. PROFILE_SCOPE( TerrainEditor_Selection_GetMinHeight );
  232. if(!size())
  233. return(0);
  234. F32 minHeight = (*this)[0].mHeight;
  235. for(U32 i = 1; i < size(); i++)
  236. minHeight = getMin(minHeight, (*this)[i].mHeight);
  237. return minHeight;
  238. }
  239. F32 Selection::getMaxHeight()
  240. {
  241. PROFILE_SCOPE( TerrainEditor_Selection_GetMaxHeight );
  242. if(!size())
  243. return(0);
  244. F32 maxHeight = (*this)[0].mHeight;
  245. for(U32 i = 1; i < size(); i++)
  246. maxHeight = getMax(maxHeight, (*this)[i].mHeight);
  247. return maxHeight;
  248. }
  249. Brush::Brush(TerrainEditor * editor) :
  250. mTerrainEditor(editor)
  251. {
  252. mSize = mTerrainEditor->getBrushSize();
  253. }
  254. const Point2I & Brush::getPosition()
  255. {
  256. return(mGridPoint.gridPos);
  257. }
  258. const GridPoint & Brush::getGridPoint()
  259. {
  260. return mGridPoint;
  261. }
  262. void Brush::setPosition(const Point3F & pos)
  263. {
  264. PROFILE_SCOPE( TerrainEditor_Brush_SetPosition_Point3F );
  265. mTerrainEditor->worldToGrid(pos, mGridPoint);
  266. update();
  267. }
  268. void Brush::setPosition(const Point2I & pos)
  269. {
  270. PROFILE_SCOPE( TerrainEditor_Brush_SetPosition_Point2I );
  271. mGridPoint.gridPos = pos;
  272. update();
  273. }
  274. void Brush::update()
  275. {
  276. PROFILE_SCOPE( TerrainEditor_Brush_update );
  277. if ( mGridPoint.terrainBlock )
  278. rebuild();
  279. }
  280. void Brush::render()
  281. {
  282. PROFILE_SCOPE( TerrainEditor_Brush_Render );
  283. // Render the brush's outline via the derived brush class.
  284. _renderOutline();
  285. // Render the brush's interior grid points.
  286. const U32 pointCount = mSize.x * mSize.y;
  287. if ( pointCount == 0 )
  288. return;
  289. if ( mRenderList.empty() || empty() )
  290. return;
  291. Vector<GFXVertexPCT> pointList;
  292. pointList.reserve( pointCount );
  293. for(S32 x = 0; x < mSize.x; x++)
  294. {
  295. for(S32 y = 0; y < mSize.y; y++)
  296. {
  297. S32 id = mRenderList[x*mSize.x+y];
  298. if ( id == -1 )
  299. continue;
  300. const GridInfo &gInfo = (*this)[ id ];
  301. Point3F pos;
  302. mTerrainEditor->gridToWorld( gInfo.mGridPoint.gridPos, pos, gInfo.mGridPoint.terrainBlock );
  303. if ( !mTerrainEditor->project( pos, &pos ) )
  304. continue;
  305. pointList.increment();
  306. GFXVertexPCT &pointInfo = pointList.last();
  307. pointInfo.point = pos;
  308. pointInfo.color.set( 255, 0, 255, gInfo.mWeight * 255 );
  309. pointInfo.texCoord.set( 1.0f, 0.0f );
  310. }
  311. }
  312. mTerrainEditor->renderPoints( pointList );
  313. }
  314. void BoxBrush::rebuild()
  315. {
  316. PROFILE_SCOPE( TerrainEditor_BoxBrush_Rebuild );
  317. reset();
  318. const F32 squareSize = mGridPoint.terrainBlock->getSquareSize();
  319. mRenderList.setSize(mSize.x*mSize.y);
  320. Point3F center( F32(mSize.x - 1) / 2.0f * squareSize, F32(mSize.y - 1) / 2.0f * squareSize, 0.0f );
  321. Filter filter;
  322. filter.set(1, &mTerrainEditor->mSoftSelectFilter);
  323. const Point3F mousePos = mTerrainEditor->getMousePos();
  324. F32 xFactorScale = center.x / ( center.x + 0.5f );
  325. F32 yFactorScale = center.y / ( center.y + 0.5f );
  326. const F32 softness = mTerrainEditor->getBrushSoftness();
  327. const F32 pressure = mTerrainEditor->getBrushPressure();
  328. Point3F posw( 0,0,0 );
  329. Point2I posg( 0,0 );
  330. Vector<GridInfo> infos;
  331. for ( S32 x = 0; x < mSize.x; x++ )
  332. {
  333. for(S32 y = 0; y < mSize.y; y++)
  334. {
  335. F32 xFactor = 0.0f;
  336. if ( center.x > 0 )
  337. xFactor = mAbs( center.x - x ) / center.x * xFactorScale;
  338. F32 yFactor = 0.0f;
  339. if ( center.y > 0 )
  340. yFactor = mAbs( center.y - y ) / center.y * yFactorScale;
  341. S32 &rl = mRenderList[x*mSize.x+y];
  342. posw.x = mousePos.x + (F32)x * squareSize - center.x;
  343. posw.y = mousePos.y + (F32)y * squareSize - center.y;
  344. // round to grid coords
  345. GridPoint gridPoint = mGridPoint;
  346. mTerrainEditor->worldToGrid( posw, gridPoint );
  347. // Check that the grid point is valid within the terrain. This assumes
  348. // that there is no wrap around past the edge of the terrain.
  349. if(!mTerrainEditor->isPointInTerrain(gridPoint))
  350. {
  351. rl = -1;
  352. continue;
  353. }
  354. infos.clear();
  355. mTerrainEditor->getGridInfos( gridPoint, infos );
  356. for (U32 z = 0; z < infos.size(); z++)
  357. {
  358. infos[z].mWeight = pressure *
  359. mLerp( infos[z].mWeight, filter.getValue(xFactor > yFactor ? xFactor : yFactor), softness );
  360. push_back(infos[z]);
  361. }
  362. rl = size()-1;
  363. }
  364. }
  365. }
  366. void BoxBrush::_renderOutline()
  367. {
  368. F32 squareSize = mGridPoint.terrainBlock->getSquareSize();
  369. RayInfo ri;
  370. Point3F start( 0, 0, 5000.0f );
  371. Point3F end( 0, 0, -5000.0f );
  372. bool hit;
  373. Vector<Point3F> pointList;
  374. pointList.reserve( 64 );
  375. const ColorI col( 255, 0, 255, 255 );
  376. const Point3F &mousePos = mTerrainEditor->getMousePos();
  377. static const Point2F offsetArray [5] =
  378. {
  379. Point2F( -1, -1 ),
  380. Point2F( 1, -1 ),
  381. Point2F( 1, 1 ),
  382. Point2F( -1, 1 ),
  383. Point2F( -1, -1 ) // repeat of offset[0]
  384. };
  385. // 64 total steps, 4 sides to the box, 16 steps per side.
  386. // 64 / 4 = 16
  387. const U32 steps = 16;
  388. for ( S32 i = 0; i < 4; i++ )
  389. {
  390. const Point2F &offset = offsetArray[i];
  391. const Point2F &next = offsetArray[i+1];
  392. for ( S32 j = 0; j < steps; j++ )
  393. {
  394. F32 frac = (F32)j / ( (F32)steps - 1.0f );
  395. Point2F tmp;
  396. tmp.interpolate( offset, next, frac );
  397. start.x = end.x = mousePos.x + tmp.x * squareSize * 0.5f * (F32)mSize.x;
  398. start.y = end.y = mousePos.y + tmp.y * squareSize * 0.5f * (F32)mSize.y;
  399. hit = gServerContainer.castRay( start, end, TerrainObjectType, &ri );
  400. if ( hit )
  401. pointList.push_back( ri.point );
  402. }
  403. }
  404. mTerrainEditor->drawLineList( pointList, col, 1.0f );
  405. }
  406. void EllipseBrush::rebuild()
  407. {
  408. PROFILE_SCOPE( TerrainEditor_EllipseBrush_Rebuild );
  409. reset();
  410. const F32 squareSize = mGridPoint.terrainBlock->getSquareSize();
  411. mRenderList.setSize(mSize.x*mSize.y);
  412. Point3F center( F32(mSize.x - 1) / 2.0f * squareSize, F32(mSize.y - 1) / 2.0f * squareSize, 0.0f );
  413. Filter filter;
  414. filter.set(1, &mTerrainEditor->mSoftSelectFilter);
  415. const Point3F mousePos = mTerrainEditor->getMousePos();
  416. // a point is in a circle if:
  417. // x^2 + y^2 <= r^2
  418. // a point is in an ellipse if:
  419. // (ax)^2 + (by)^2 <= 1
  420. // where a = 1/halfEllipseWidth and b = 1/halfEllipseHeight
  421. // for a soft-selected ellipse,
  422. // the factor is simply the filtered: ((ax)^2 + (by)^2)
  423. F32 a = 1.0f / (F32(mSize.x) * squareSize * 0.5f);
  424. F32 b = 1.0f / (F32(mSize.y) * squareSize * 0.5f);
  425. const F32 softness = mTerrainEditor->getBrushSoftness();
  426. const F32 pressure = mTerrainEditor->getBrushPressure();
  427. Point3F posw( 0,0,0 );
  428. Point2I posg( 0,0 );
  429. Vector<GridInfo> infos;
  430. for ( S32 x = 0; x < mSize.x; x++ )
  431. {
  432. for ( S32 y = 0; y < mSize.y; y++ )
  433. {
  434. F32 xp = center.x - x * squareSize;
  435. F32 yp = center.y - y * squareSize;
  436. F32 factor = (a * a * xp * xp) + (b * b * yp * yp);
  437. if ( factor > 1 )
  438. {
  439. mRenderList[x*mSize.x+y] = -1;
  440. continue;
  441. }
  442. S32 &rl = mRenderList[x*mSize.x+y];
  443. posw.x = mousePos.x + (F32)x * squareSize - center.x;
  444. posw.y = mousePos.y + (F32)y * squareSize - center.y;
  445. // round to grid coords
  446. GridPoint gridPoint = mGridPoint;
  447. mTerrainEditor->worldToGrid( posw, gridPoint );
  448. // Check that the grid point is valid within the terrain. This assumes
  449. // that there is no wrap around past the edge of the terrain.
  450. if ( !mTerrainEditor->isPointInTerrain( gridPoint ) )
  451. {
  452. rl = -1;
  453. continue;
  454. }
  455. infos.clear();
  456. mTerrainEditor->getGridInfos( gridPoint, infos );
  457. for ( U32 z = 0; z < infos.size(); z++ )
  458. {
  459. infos[z].mWeight = pressure * mLerp( infos[z].mWeight, filter.getValue( factor ), softness );
  460. push_back(infos[z]);
  461. }
  462. rl = size()-1;
  463. }
  464. }
  465. }
  466. void EllipseBrush::_renderOutline()
  467. {
  468. F32 squareSize = mGridPoint.terrainBlock->getSquareSize();
  469. RayInfo ri;
  470. Point3F start( 0, 0, 5000.0f );
  471. Point3F end( 0, 0, -5000.0f );
  472. bool hit;
  473. Vector<Point3F> pointList;
  474. ColorI col( 255, 0, 255, 255 );
  475. const U32 steps = 64;
  476. const Point3F &mousePos = mTerrainEditor->getMousePos();
  477. for ( S32 i = 0; i < steps; i++ )
  478. {
  479. F32 radians = (F32)i / (F32)(steps-1) * M_2PI_F;
  480. VectorF vec(0,1,0);
  481. MathUtils::vectorRotateZAxis( vec, radians );
  482. start.x = end.x = mousePos.x + vec.x * squareSize * (F32)mSize.x * 0.5f;
  483. start.y = end.y = mousePos.y + vec.y * squareSize * (F32)mSize.y * 0.5f;
  484. hit = gServerContainer.castRay( start, end, TerrainObjectType, &ri );
  485. if ( hit )
  486. pointList.push_back( ri.point );
  487. }
  488. mTerrainEditor->drawLineList( pointList, col, 1.0f );
  489. }
  490. SelectionBrush::SelectionBrush(TerrainEditor * editor) :
  491. Brush(editor)
  492. {
  493. //... grab the current selection
  494. }
  495. void SelectionBrush::rebuild()
  496. {
  497. reset();
  498. //... move the selection
  499. }
  500. void SelectionBrush::render(Vector<GFXVertexPC> & vertexBuffer, S32 & verts, S32 & elems, S32 & prims, const ColorF & inColorFull, const ColorF & inColorNone, const ColorF & outColorFull, const ColorF & outColorNone) const
  501. {
  502. //... render the selection
  503. }
  504. TerrainEditor::TerrainEditor() :
  505. mActiveTerrain(0),
  506. mMousePos(0,0,0),
  507. mMouseBrush(0),
  508. mInAction(false),
  509. mUndoSel(0),
  510. mGridUpdateMin( S32_MAX, S32_MAX ),
  511. mGridUpdateMax( 0, 0 ),
  512. mMaxBrushSize(256,256),
  513. mNeedsGridUpdate( false ),
  514. mNeedsMaterialUpdate( false ),
  515. mMouseDown( false )
  516. {
  517. VECTOR_SET_ASSOCIATION(mActions);
  518. //
  519. resetCurrentSel();
  520. //
  521. mBrushPressure = 1.0f;
  522. mBrushSize.set(1,1);
  523. mBrushSoftness = 1.0f;
  524. mBrushChanged = true;
  525. mMouseBrush = new BoxBrush(this);
  526. mMouseDownSeq = 0;
  527. mIsDirty = false;
  528. mIsMissionDirty = false;
  529. mPaintIndex = -1;
  530. // add in all the actions here..
  531. mActions.push_back(new SelectAction(this));
  532. mActions.push_back(new DeselectAction(this));
  533. mActions.push_back(new ClearAction(this));
  534. mActions.push_back(new SoftSelectAction(this));
  535. mActions.push_back(new OutlineSelectAction(this));
  536. mActions.push_back(new PaintMaterialAction(this));
  537. mActions.push_back(new ClearMaterialsAction(this));
  538. mActions.push_back(new RaiseHeightAction(this));
  539. mActions.push_back(new LowerHeightAction(this));
  540. mActions.push_back(new SetHeightAction(this));
  541. mActions.push_back(new SetEmptyAction(this));
  542. mActions.push_back(new ClearEmptyAction(this));
  543. mActions.push_back(new ScaleHeightAction(this));
  544. mActions.push_back(new BrushAdjustHeightAction(this));
  545. mActions.push_back(new AdjustHeightAction(this));
  546. mActions.push_back(new FlattenHeightAction(this));
  547. mActions.push_back(new SmoothHeightAction(this));
  548. mActions.push_back(new SmoothSlopeAction(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. }
  1772. void TerrainEditor::mirrorTerrain(S32 mirrorIndex)
  1773. {
  1774. if(!checkTerrainBlock(this, "mirrorTerrain"))
  1775. return;
  1776. // TODO!
  1777. /*
  1778. TerrainBlock * terrain = mActiveTerrain;
  1779. setDirty();
  1780. //
  1781. enum {
  1782. top = BIT(0),
  1783. bottom = BIT(1),
  1784. left = BIT(2),
  1785. right = BIT(3)
  1786. };
  1787. U32 sides[8] =
  1788. {
  1789. bottom,
  1790. bottom | left,
  1791. left,
  1792. left | top,
  1793. top,
  1794. top | right,
  1795. right,
  1796. bottom | right
  1797. };
  1798. U32 n = TerrainBlock::BlockSize;
  1799. U32 side = sides[mirrorIndex % 8];
  1800. bool diag = mirrorIndex & 0x01;
  1801. Point2I src((side & right) ? (n - 1) : 0, (side & bottom) ? (n - 1) : 0);
  1802. Point2I dest((side & left) ? (n - 1) : 0, (side & top) ? (n - 1) : 0);
  1803. Point2I origSrc(src);
  1804. Point2I origDest(dest);
  1805. // determine the run length
  1806. U32 minStride = ((side & top) || (side & bottom)) ? n : n / 2;
  1807. U32 majStride = ((side & left) || (side & right)) ? n : n / 2;
  1808. Point2I srcStep((side & right) ? -1 : 1, (side & bottom) ? -1 : 1);
  1809. Point2I destStep((side & left) ? -1 : 1, (side & top) ? -1 : 1);
  1810. //
  1811. U16 * heights = terrain->getHeightAddress(0,0);
  1812. U8 * baseMaterials = terrain->getBaseMaterialAddress(0,0);
  1813. TerrainBlock::Material * materials = terrain->getMaterial(0,0);
  1814. // create an undo selection
  1815. Selection * undo = new Selection;
  1816. // walk through all the positions
  1817. for(U32 i = 0; i < majStride; i++)
  1818. {
  1819. for(U32 j = 0; j < minStride; j++)
  1820. {
  1821. // skip the same position
  1822. if(src != dest)
  1823. {
  1824. U32 si = src.x + (src.y << TerrainBlock::BlockShift);
  1825. U32 di = dest.x + (dest.y << TerrainBlock::BlockShift);
  1826. // add to undo selection
  1827. GridInfo info;
  1828. getGridInfo(dest, info, terrain);
  1829. undo->add(info);
  1830. //... copy info... (height, basematerial, material)
  1831. heights[di] = heights[si];
  1832. baseMaterials[di] = baseMaterials[si];
  1833. materials[di] = materials[si];
  1834. }
  1835. // get to the new position
  1836. src.x += srcStep.x;
  1837. diag ? (dest.y += destStep.y) : (dest.x += destStep.x);
  1838. }
  1839. // get the next position for a run
  1840. src.y += srcStep.y;
  1841. diag ? (dest.x += destStep.x) : (dest.y += destStep.y);
  1842. // reset the minor run
  1843. src.x = origSrc.x;
  1844. diag ? (dest.y = origDest.y) : (dest.x = origDest.x);
  1845. // shorten the run length for diag runs
  1846. if(diag)
  1847. minStride--;
  1848. }
  1849. // rebuild stuff..
  1850. terrain->buildGridMap();
  1851. terrain->rebuildEmptyFlags();
  1852. terrain->packEmptySquares();
  1853. // add undo selection
  1854. submitUndo( undo );
  1855. */
  1856. }
  1857. bool TerrainEditor::isPointInTerrain( const GridPoint & gPoint)
  1858. {
  1859. PROFILE_SCOPE( TerrainEditor_IsPointInTerrain );
  1860. Point2I cPos;
  1861. gridToCenter( gPoint.gridPos, cPos );
  1862. const TerrainFile *file = gPoint.terrainBlock->getFile();
  1863. return file->isPointInTerrain( cPos.x, cPos.y );
  1864. }
  1865. void TerrainEditor::reorderMaterial( S32 index, S32 orderPos )
  1866. {
  1867. TerrainBlock *terr = getClientTerrain();
  1868. Vector<U8> layerMap = terr->getLayerMap();
  1869. Vector<TerrainMaterial*> materials = terr->getMaterials();
  1870. TerrainMaterial *pMat = materials[index];
  1871. submitMaterialUndo( String::ToString( "Reordered %s Material", terr->getMaterialName(index) ) );
  1872. materials.erase( index );
  1873. materials.insert( orderPos, pMat );
  1874. Vector<U8>::iterator itr = layerMap.begin();
  1875. for ( ; itr != layerMap.end(); itr++ )
  1876. {
  1877. // Was previous material, set to new index.
  1878. if ( *itr == index )
  1879. *itr = orderPos;
  1880. else
  1881. {
  1882. // We removed a Material prior to this one, bump it down.
  1883. if ( *itr > index )
  1884. (*itr)--;
  1885. // We added a Material prior to this one, bump it up.
  1886. if ( *itr >= orderPos )
  1887. (*itr)++;
  1888. }
  1889. }
  1890. terr->setMaterials( materials );
  1891. terr->setLayerMap( layerMap );
  1892. // We didn't really just "undo" but it happens to do everything we
  1893. // need to update the materials and gui.
  1894. onMaterialUndo( terr );
  1895. }
  1896. //------------------------------------------------------------------------------
  1897. ConsoleMethod( TerrainEditor, attachTerrain, void, 2, 3, "(TerrainBlock terrain)")
  1898. {
  1899. SimSet * missionGroup = dynamic_cast<SimSet*>(Sim::findObject("MissionGroup"));
  1900. if (!missionGroup)
  1901. {
  1902. Con::errorf(ConsoleLogEntry::Script, "TerrainEditor::attach: no mission group found");
  1903. return;
  1904. }
  1905. VectorPtr<TerrainBlock*> terrains;
  1906. // attach to first found terrainBlock
  1907. if (argc == 2)
  1908. {
  1909. for(SimSetIterator itr(missionGroup); *itr; ++itr)
  1910. {
  1911. TerrainBlock* terrBlock = dynamic_cast<TerrainBlock*>(*itr);
  1912. if (terrBlock)
  1913. terrains.push_back(terrBlock);
  1914. }
  1915. //if (terrains.size() == 0)
  1916. // Con::errorf(ConsoleLogEntry::Script, "TerrainEditor::attach: no TerrainBlock objects found!");
  1917. }
  1918. else // attach to named object
  1919. {
  1920. TerrainBlock* terrBlock = dynamic_cast<TerrainBlock*>(Sim::findObject(argv[2]));
  1921. if (terrBlock)
  1922. terrains.push_back(terrBlock);
  1923. if(terrains.size() == 0)
  1924. Con::errorf(ConsoleLogEntry::Script, "TerrainEditor::attach: failed to attach to object '%s'", argv[2]);
  1925. }
  1926. if (terrains.size() > 0)
  1927. {
  1928. for (U32 i = 0; i < terrains.size(); i++)
  1929. {
  1930. if (!terrains[i]->isServerObject())
  1931. {
  1932. terrains[i] = NULL;
  1933. Con::errorf(ConsoleLogEntry::Script, "TerrainEditor::attach: cannot attach to client TerrainBlock");
  1934. }
  1935. }
  1936. }
  1937. for (U32 i = 0; i < terrains.size(); i++)
  1938. {
  1939. if (terrains[i])
  1940. object->attachTerrain(terrains[i]);
  1941. }
  1942. }
  1943. ConsoleMethod( TerrainEditor, getTerrainBlockCount, S32, 2, 2, "()")
  1944. {
  1945. return object->getTerrainBlockCount();
  1946. }
  1947. ConsoleMethod( TerrainEditor, getTerrainBlock, S32, 3, 3, "(S32 index)")
  1948. {
  1949. TerrainBlock* tb = object->getTerrainBlock(dAtoi(argv[2]));
  1950. if(!tb)
  1951. return 0;
  1952. else
  1953. return tb->getId();
  1954. }
  1955. ConsoleMethod(TerrainEditor, getTerrainBlocksMaterialList, const char *, 2, 2, "() gets the list of current terrain materials for all terrain blocks.")
  1956. {
  1957. Vector<StringTableEntry> list;
  1958. object->getTerrainBlocksMaterialList(list);
  1959. if(list.size() == 0)
  1960. return "";
  1961. // Calculate the size of the return buffer
  1962. S32 size = 0;
  1963. for(U32 i = 0; i < list.size(); ++i)
  1964. {
  1965. size += dStrlen(list[i]);
  1966. ++size;
  1967. }
  1968. ++size;
  1969. // Copy the material names
  1970. char *ret = Con::getReturnBuffer(size);
  1971. ret[0] = 0;
  1972. for(U32 i = 0; i < list.size(); ++i)
  1973. {
  1974. dStrcat( ret, list[i] );
  1975. dStrcat( ret, "\n" );
  1976. }
  1977. return ret;
  1978. }
  1979. ConsoleMethod( TerrainEditor, setBrushType, void, 3, 3, "(string type)"
  1980. "One of box, ellipse, selection.")
  1981. {
  1982. object->setBrushType(argv[2]);
  1983. }
  1984. ConsoleMethod( TerrainEditor, getBrushType, const char*, 2, 2, "()")
  1985. {
  1986. return object->getBrushType();
  1987. }
  1988. ConsoleMethod( TerrainEditor, setBrushSize, void, 3, 4, "(int w [, int h])")
  1989. {
  1990. S32 w = dAtoi(argv[2]);
  1991. S32 h = argc > 3 ? dAtoi(argv[3]) : w;
  1992. object->setBrushSize( w, h );
  1993. }
  1994. ConsoleMethod( TerrainEditor, getBrushSize, const char*, 2, 2, "()")
  1995. {
  1996. Point2I size = object->getBrushSize();
  1997. char * ret = Con::getReturnBuffer(32);
  1998. dSprintf(ret, 32, "%d %d", size.x, size.y);
  1999. return ret;
  2000. }
  2001. ConsoleMethod( TerrainEditor, setBrushPressure, void, 3, 3, "(float pressure)")
  2002. {
  2003. object->setBrushPressure( dAtof( argv[2] ) );
  2004. }
  2005. ConsoleMethod( TerrainEditor, getBrushPressure, F32, 2, 2, "()")
  2006. {
  2007. return object->getBrushPressure();
  2008. }
  2009. ConsoleMethod( TerrainEditor, setBrushSoftness, void, 3, 3, "(float softness)")
  2010. {
  2011. object->setBrushSoftness( dAtof( argv[2] ) );
  2012. }
  2013. ConsoleMethod( TerrainEditor, getBrushSoftness, F32, 2, 2, "()")
  2014. {
  2015. return object->getBrushSoftness();
  2016. }
  2017. ConsoleMethod( TerrainEditor, getBrushPos, const char*, 2, 2, "Returns a Point2I.")
  2018. {
  2019. return object->getBrushPos();
  2020. }
  2021. ConsoleMethod( TerrainEditor, setBrushPos, void, 3, 4, "(int x, int y)")
  2022. {
  2023. //
  2024. Point2I pos;
  2025. if(argc == 3)
  2026. dSscanf(argv[2], "%d %d", &pos.x, &pos.y);
  2027. else
  2028. {
  2029. pos.x = dAtoi(argv[2]);
  2030. pos.y = dAtoi(argv[3]);
  2031. }
  2032. object->setBrushPos(pos);
  2033. }
  2034. ConsoleMethod( TerrainEditor, setAction, void, 3, 3, "(string action_name)")
  2035. {
  2036. object->setAction(argv[2]);
  2037. }
  2038. ConsoleMethod( TerrainEditor, getActionName, const char*, 3, 3, "(int num)")
  2039. {
  2040. return (object->getActionName(dAtoi(argv[2])));
  2041. }
  2042. ConsoleMethod( TerrainEditor, getNumActions, S32, 2, 2, "")
  2043. {
  2044. return(object->getNumActions());
  2045. }
  2046. ConsoleMethod( TerrainEditor, getCurrentAction, const char*, 2, 2, "")
  2047. {
  2048. return object->getCurrentAction();
  2049. }
  2050. ConsoleMethod( TerrainEditor, resetSelWeights, void, 3, 3, "(bool clear)")
  2051. {
  2052. object->resetSelWeights(dAtob(argv[2]));
  2053. }
  2054. ConsoleMethod( TerrainEditor, clearSelection, void, 2, 2, "")
  2055. {
  2056. object->clearSelection();
  2057. }
  2058. ConsoleMethod( TerrainEditor, processAction, void, 2, 3, "(string action=NULL)")
  2059. {
  2060. if(argc == 3)
  2061. object->processAction(argv[2]);
  2062. else object->processAction("");
  2063. }
  2064. ConsoleMethod( TerrainEditor, getActiveTerrain, S32, 2, 2, "")
  2065. {
  2066. S32 ret = 0;
  2067. TerrainBlock* terrain = object->getActiveTerrain();
  2068. if (terrain)
  2069. ret = terrain->getId();
  2070. return ret;
  2071. }
  2072. ConsoleMethod( TerrainEditor, getNumTextures, S32, 2, 2, "")
  2073. {
  2074. return object->getNumTextures();
  2075. }
  2076. ConsoleMethod( TerrainEditor, markEmptySquares, void, 2, 2, "")
  2077. {
  2078. object->markEmptySquares();
  2079. }
  2080. ConsoleMethod( TerrainEditor, mirrorTerrain, void, 3, 3, "")
  2081. {
  2082. object->mirrorTerrain(dAtoi(argv[2]));
  2083. }
  2084. ConsoleMethod(TerrainEditor, setTerraformOverlay, void, 3, 3, "(bool overlayEnable) - sets the terraformer current heightmap to draw as an overlay over the current terrain.")
  2085. {
  2086. // XA: This one needs to be implemented :)
  2087. }
  2088. ConsoleMethod(TerrainEditor, updateMaterial, bool, 4, 4,
  2089. "( int index, string matName )\n"
  2090. "Changes the material name at the index." )
  2091. {
  2092. TerrainBlock *terr = object->getClientTerrain();
  2093. if ( !terr )
  2094. return false;
  2095. U32 index = dAtoi( argv[2] );
  2096. if ( index >= terr->getMaterialCount() )
  2097. return false;
  2098. terr->updateMaterial( index, argv[3] );
  2099. object->setDirty();
  2100. return true;
  2101. }
  2102. ConsoleMethod(TerrainEditor, addMaterial, S32, 3, 3,
  2103. "( string matName )\n"
  2104. "Adds a new material." )
  2105. {
  2106. TerrainBlock *terr = object->getClientTerrain();
  2107. if ( !terr )
  2108. return false;
  2109. terr->addMaterial( argv[2] );
  2110. object->setDirty();
  2111. return true;
  2112. }
  2113. ConsoleMethod( TerrainEditor, removeMaterial, void, 3, 3, "( int index ) - Remove the material at the given index." )
  2114. {
  2115. TerrainBlock *terr = object->getClientTerrain();
  2116. if ( !terr )
  2117. return;
  2118. S32 index = dAtoi( argv[ 2 ] );
  2119. if ( index < 0 || index >= terr->getMaterialCount() )
  2120. {
  2121. Con::errorf( "TerrainEditor::removeMaterial - index out of range!" );
  2122. return;
  2123. }
  2124. if ( terr->getMaterialCount() == 1 )
  2125. {
  2126. Con::errorf( "TerrainEditor::removeMaterial - cannot remove material, there is only one!" );
  2127. return;
  2128. }
  2129. const char *matName = terr->getMaterialName( index );
  2130. object->submitMaterialUndo( String::ToString( "Remove TerrainMaterial %s", matName ) );
  2131. terr->removeMaterial( index );
  2132. object->setDirty();
  2133. object->scheduleMaterialUpdate();
  2134. object->setGridUpdateMinMax();
  2135. }
  2136. ConsoleMethod(TerrainEditor, getMaterialCount, S32, 2, 2,
  2137. "Returns the current material count." )
  2138. {
  2139. TerrainBlock *terr = object->getClientTerrain();
  2140. if ( terr )
  2141. return terr->getMaterialCount();
  2142. return 0;
  2143. }
  2144. ConsoleMethod(TerrainEditor, getMaterials, const char *, 2, 2, "() gets the list of current terrain materials.")
  2145. {
  2146. TerrainBlock *terr = object->getClientTerrain();
  2147. if ( !terr )
  2148. return "";
  2149. char *ret = Con::getReturnBuffer(4096);
  2150. ret[0] = 0;
  2151. for(U32 i = 0; i < terr->getMaterialCount(); i++)
  2152. {
  2153. dStrcat( ret, terr->getMaterialName(i) );
  2154. dStrcat( ret, "\n" );
  2155. }
  2156. return ret;
  2157. }
  2158. ConsoleMethod( TerrainEditor, getMaterialName, const char*, 3, 3, "( int index ) - Returns the name of the material at the given index." )
  2159. {
  2160. TerrainBlock *terr = object->getClientTerrain();
  2161. if ( !terr )
  2162. return "";
  2163. S32 index = dAtoi( argv[ 2 ] );
  2164. if( index < 0 || index >= terr->getMaterialCount() )
  2165. {
  2166. Con::errorf( "TerrainEditor::getMaterialName - index out of range!" );
  2167. return "";
  2168. }
  2169. const char* name = terr->getMaterialName( index );
  2170. return Con::getReturnBuffer( name );
  2171. }
  2172. ConsoleMethod( TerrainEditor, getMaterialIndex, S32, 3, 3, "( string name ) - Returns the index of the material with the given name or -1." )
  2173. {
  2174. TerrainBlock *terr = object->getClientTerrain();
  2175. if ( !terr )
  2176. return -1;
  2177. const char* name = argv[ 2 ];
  2178. const U32 count = terr->getMaterialCount();
  2179. for( U32 i = 0; i < count; ++ i )
  2180. if( dStricmp( name, terr->getMaterialName( i ) ) == 0 )
  2181. return i;
  2182. return -1;
  2183. }
  2184. ConsoleMethod( TerrainEditor, reorderMaterial, void, 4, 4, "( int index, int order ) "
  2185. "- Reorder material at the given index to the new position, changing the order in which it is rendered / blended." )
  2186. {
  2187. object->reorderMaterial( dAtoi( argv[2] ), dAtoi( argv[3] ) );
  2188. }
  2189. ConsoleMethod(TerrainEditor, getTerrainUnderWorldPoint, S32, 3, 5, "(x/y/z) Gets the terrain block that is located under the given world point.\n"
  2190. "@param x/y/z The world coordinates (floating point values) you wish to query at. "
  2191. "These can be formatted as either a string (\"x y z\") or separately as (x, y, z)\n"
  2192. "@return Returns the ID of the requested terrain block (0 if not found).\n\n")
  2193. {
  2194. TerrainEditor *tEditor = (TerrainEditor *) object;
  2195. if(tEditor == NULL)
  2196. return 0;
  2197. Point3F pos;
  2198. if(argc == 3)
  2199. dSscanf(argv[2], "%f %f %f", &pos.x, &pos.y, &pos.z);
  2200. else if(argc == 5)
  2201. {
  2202. pos.x = dAtof(argv[2]);
  2203. pos.y = dAtof(argv[3]);
  2204. pos.z = dAtof(argv[4]);
  2205. }
  2206. else
  2207. {
  2208. Con::errorf("TerrainEditor.getTerrainUnderWorldPoint(): Invalid argument count! Valid arguments are either \"x y z\" or x,y,z\n");
  2209. return 0;
  2210. }
  2211. TerrainBlock* terrain = tEditor->getTerrainUnderWorldPoint(pos);
  2212. if(terrain != NULL)
  2213. {
  2214. return terrain->getId();
  2215. }
  2216. return 0;
  2217. }
  2218. //------------------------------------------------------------------------------
  2219. void TerrainEditor::initPersistFields()
  2220. {
  2221. addGroup("Misc");
  2222. addField("isDirty", TypeBool, Offset(mIsDirty, TerrainEditor));
  2223. addField("isMissionDirty", TypeBool, Offset(mIsMissionDirty, TerrainEditor));
  2224. addField("renderBorder", TypeBool, Offset(mRenderBorder, TerrainEditor)); ///< Not currently used
  2225. addField("borderHeight", TypeF32, Offset(mBorderHeight, TerrainEditor)); ///< Not currently used
  2226. addField("borderFillColor", TypeColorI, Offset(mBorderFillColor, TerrainEditor)); ///< Not currently used
  2227. addField("borderFrameColor", TypeColorI, Offset(mBorderFrameColor, TerrainEditor)); ///< Not currently used
  2228. addField("borderLineMode", TypeBool, Offset(mBorderLineMode, TerrainEditor)); ///< Not currently used
  2229. addField("selectionHidden", TypeBool, Offset(mSelectionHidden, TerrainEditor));
  2230. addField("renderVertexSelection", TypeBool, Offset(mRenderVertexSelection, TerrainEditor)); ///< Not currently used
  2231. addField("renderSolidBrush", TypeBool, Offset(mRenderSolidBrush, TerrainEditor));
  2232. addField("processUsesBrush", TypeBool, Offset(mProcessUsesBrush, TerrainEditor));
  2233. addField("maxBrushSize", TypePoint2I, Offset(mMaxBrushSize, TerrainEditor));
  2234. // action values...
  2235. addField("adjustHeightVal", TypeF32, Offset(mAdjustHeightVal, TerrainEditor)); ///< RaiseHeightAction and LowerHeightAction
  2236. addField("setHeightVal", TypeF32, Offset(mSetHeightVal, TerrainEditor)); ///< SetHeightAction
  2237. addField("scaleVal", TypeF32, Offset(mScaleVal, TerrainEditor)); ///< ScaleHeightAction
  2238. addField("smoothFactor", TypeF32, Offset(mSmoothFactor, TerrainEditor)); ///< SmoothHeightAction
  2239. addField("noiseFactor", TypeF32, Offset(mNoiseFactor, TerrainEditor)); ///< PaintNoiseAction
  2240. addField("materialGroup", TypeS32, Offset(mMaterialGroup, TerrainEditor)); ///< Not currently used
  2241. addField("softSelectRadius", TypeF32, Offset(mSoftSelectRadius, TerrainEditor)); ///< SoftSelectAction
  2242. addField("softSelectFilter", TypeString, Offset(mSoftSelectFilter, TerrainEditor)); ///< SoftSelectAction brush filtering
  2243. addField("softSelectDefaultFilter", TypeString, Offset(mSoftSelectDefaultFilter, TerrainEditor)); ///< SoftSelectAction brush filtering
  2244. addField("adjustHeightMouseScale", TypeF32, Offset(mAdjustHeightMouseScale, TerrainEditor)); ///< Not currently used
  2245. addField("paintIndex", TypeS32, Offset(mPaintIndex, TerrainEditor)); ///< PaintMaterialAction
  2246. endGroup("Misc");
  2247. Parent::initPersistFields();
  2248. }
  2249. ConsoleMethod( TerrainEditor, getSlopeLimitMinAngle, F32, 2, 2, 0)
  2250. {
  2251. return object->mSlopeMinAngle;
  2252. }
  2253. ConsoleMethod( TerrainEditor, setSlopeLimitMinAngle, F32, 3, 3, 0)
  2254. {
  2255. F32 angle = dAtof( argv[2] );
  2256. if ( angle < 0.0f )
  2257. angle = 0.0f;
  2258. if ( angle > object->mSlopeMaxAngle )
  2259. angle = object->mSlopeMaxAngle;
  2260. object->mSlopeMinAngle = angle;
  2261. return angle;
  2262. }
  2263. ConsoleMethod( TerrainEditor, getSlopeLimitMaxAngle, F32, 2, 2, 0)
  2264. {
  2265. return object->mSlopeMaxAngle;
  2266. }
  2267. ConsoleMethod( TerrainEditor, setSlopeLimitMaxAngle, F32, 3, 3, 0)
  2268. {
  2269. F32 angle = dAtof( argv[2] );
  2270. if ( angle > 90.0f )
  2271. angle = 90.0f;
  2272. if ( angle < object->mSlopeMinAngle )
  2273. angle = object->mSlopeMinAngle;
  2274. object->mSlopeMaxAngle = angle;
  2275. return angle;
  2276. }
  2277. //------------------------------------------------------------------------------
  2278. void TerrainEditor::autoMaterialLayer( F32 mMinHeight, F32 mMaxHeight, F32 mMinSlope, F32 mMaxSlope, F32 mCoverage )
  2279. {
  2280. if (!mActiveTerrain)
  2281. return;
  2282. S32 mat = getPaintMaterialIndex();
  2283. if (mat == -1)
  2284. return;
  2285. mUndoSel = new Selection;
  2286. U32 terrBlocks = mActiveTerrain->getBlockSize();
  2287. for (U32 y = 0; y < terrBlocks; y++)
  2288. {
  2289. for (U32 x = 0; x < terrBlocks; x++)
  2290. {
  2291. // get info
  2292. GridPoint gp;
  2293. gp.terrainBlock = mActiveTerrain;
  2294. gp.gridPos.set(x, y);
  2295. GridInfo gi;
  2296. getGridInfo(gp, gi);
  2297. if (gi.mMaterial == mat)
  2298. continue;
  2299. if (mRandI(0, 100) > mCoverage)
  2300. continue;
  2301. Point3F wp;
  2302. gridToWorld(gp, wp);
  2303. if (!(wp.z >= mMinHeight && wp.z <= mMaxHeight))
  2304. continue;
  2305. // transform wp to object space
  2306. Point3F op;
  2307. mActiveTerrain->getWorldTransform().mulP(wp, &op);
  2308. Point3F norm;
  2309. mActiveTerrain->getNormal(Point2F(op.x, op.y), &norm, true);
  2310. if (mMinSlope > 0)
  2311. if (norm.z > mSin(mDegToRad(90.0f - mMinSlope)))
  2312. continue;
  2313. if (mMaxSlope < 90)
  2314. if (norm.z < mSin(mDegToRad(90.0f - mMaxSlope)))
  2315. continue;
  2316. gi.mMaterialChanged = true;
  2317. mUndoSel->add(gi);
  2318. gi.mMaterial = mat;
  2319. setGridInfo(gi);
  2320. }
  2321. }
  2322. if(mUndoSel->size())
  2323. submitUndo( mUndoSel );
  2324. else
  2325. delete mUndoSel;
  2326. mUndoSel = 0;
  2327. scheduleMaterialUpdate();
  2328. }
  2329. ConsoleMethod( TerrainEditor, autoMaterialLayer, void, 7, 7, "(float minHeight, float maxHeight, float minSlope, float maxSlope, float coverage)")
  2330. {
  2331. object->autoMaterialLayer( dAtof(argv[2]), dAtof(argv[3]), dAtof(argv[4]), dAtof(argv[5]), dAtof(argv[6]));
  2332. }