terrainEditor.cpp 80 KB

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