terrainEditor.cpp 80 KB

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