Torque3D Documentation / _generateds / guiTerrPreviewCtrl.cpp

guiTerrPreviewCtrl.cpp

Engine/source/gui/worldEditor/guiTerrPreviewCtrl.cpp

More...

Public Functions

ConsoleDocClass(GuiTerrPreviewCtrl , "@brief Very old GUI used <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> terrain <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">preview\n\n</a>" "<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Deprecated\n\n</a>" "@internal" )
DefineEngineMethod(GuiTerrPreviewCtrl , getOrigin , Point2F , () , "Return <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/class/classpoint2f/">Point2F</a> containing the position of the origin." )
DefineEngineMethod(GuiTerrPreviewCtrl , getRoot , Point2F , () , "Return <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/class/classpoint2f/">Point2F</a> representing the position of the root." )
DefineEngineMethod(GuiTerrPreviewCtrl , getValue , const char * , () , "Returns <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> 4-tuple containing: root_x root_y origin_x origin_y" )
DefineEngineMethod(GuiTerrPreviewCtrl , reset , void , () , "Reset the view of the terrain." )
DefineEngineMethod(GuiTerrPreviewCtrl , setOrigin , void , (Point2F pos) , "(float x, float y)" "Set the origin of the view." )
DefineEngineMethod(GuiTerrPreviewCtrl , setRoot , void , () , "Add the origin <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the root and reset the origin." )
DefineEngineMethod(GuiTerrPreviewCtrl , setValue , void , (const char *tuple) , "Accepts <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> 4-tuple in the same form as getValue <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">returns.\n\n</a>" "@see GuiTerrPreviewCtrl::getValue()" )

Detailed Description

Public Functions

ConsoleDocClass(GuiTerrPreviewCtrl , "@brief Very old GUI used <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> terrain <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">preview\n\n</a>" "<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Deprecated\n\n</a>" "@internal" )

DefineEngineMethod(GuiTerrPreviewCtrl , getOrigin , Point2F , () , "Return <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/class/classpoint2f/">Point2F</a> containing the position of the origin." )

DefineEngineMethod(GuiTerrPreviewCtrl , getRoot , Point2F , () , "Return <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/class/classpoint2f/">Point2F</a> representing the position of the root." )

DefineEngineMethod(GuiTerrPreviewCtrl , getValue , const char * , () , "Returns <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> 4-tuple containing: root_x root_y origin_x origin_y" )

DefineEngineMethod(GuiTerrPreviewCtrl , reset , void , () , "Reset the view of the terrain." )

DefineEngineMethod(GuiTerrPreviewCtrl , setOrigin , void , (Point2F pos) , "(float x, float y)" "Set the origin of the view." )

DefineEngineMethod(GuiTerrPreviewCtrl , setRoot , void , () , "Add the origin <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the root and reset the origin." )

DefineEngineMethod(GuiTerrPreviewCtrl , setValue , void , (const char *tuple) , "Accepts <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> 4-tuple in the same form as getValue <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">returns.\n\n</a>" "@see GuiTerrPreviewCtrl::getValue()" )

IMPLEMENT_CONOBJECT(GuiTerrPreviewCtrl )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "console/console.h"
 25#include "console/engineAPI.h"
 26#include "console/consoleTypes.h"
 27#include "terrain/terrData.h"
 28#include "gui/worldEditor/guiTerrPreviewCtrl.h"
 29#include "gfx/primBuilder.h"
 30#include "T3D/gameFunctions.h"
 31
 32IMPLEMENT_CONOBJECT(GuiTerrPreviewCtrl);
 33
 34ConsoleDocClass( GuiTerrPreviewCtrl,
 35   "@brief Very old GUI used for terrain preview\n\n"
 36   "Deprecated\n\n"
 37   "@internal"
 38);
 39
 40GuiTerrPreviewCtrl::GuiTerrPreviewCtrl(void) : mTerrainSize(2048.0f), mTerrainEditor(NULL)
 41{
 42   mRoot.set( 0, 0 );
 43   mOrigin.set( 0, 0 );
 44   mWorldScreenCenter.set( mTerrainSize*0.5f, mTerrainSize*0.5f );
 45   mControlsStateBlock = NULL;
 46   mTerrainBitmapStateBlock = NULL;
 47}
 48
 49bool GuiTerrPreviewCtrl::onAdd()
 50{
 51   if(Parent::onAdd() == false)
 52   {
 53      return false;
 54   }
 55
 56   SimObject* inTerrEditor = Sim::findObject("ETerrainEditor");
 57   if(!inTerrEditor)
 58   {
 59      Con::errorf(ConsoleLogEntry::General, "TerrainEditor::onAdd: failed to load Terrain Editor");
 60      return false;
 61   }
 62
 63   mTerrainEditor = dynamic_cast<TerrainEditor*>(inTerrEditor);
 64
 65   GFXStateBlockDesc desc;
 66
 67   desc.setBlend(false, GFXBlendOne, GFXBlendZero);
 68
 69   desc.samplersDefined = true;
 70   desc.samplers[0].addressModeU = GFXAddressWrap;
 71   desc.samplers[0].addressModeV = GFXAddressWrap;
 72   desc.setCullMode(GFXCullNone);
 73   desc.setZReadWrite(false);
 74
 75   mTerrainBitmapStateBlock = GFX->createStateBlock(desc);
 76
 77
 78   mControlsStateBlock = GFX->createStateBlock(desc);
 79
 80   return true;
 81}
 82
 83void GuiTerrPreviewCtrl::initPersistFields()
 84{
 85   Parent::initPersistFields();
 86}
 87
 88
 89DefineEngineMethod( GuiTerrPreviewCtrl, reset, void, (), , "Reset the view of the terrain.")
 90{
 91   object->reset();
 92}
 93
 94DefineEngineMethod( GuiTerrPreviewCtrl, setRoot, void, (), , "Add the origin to the root and reset the origin.")
 95{
 96   object->setRoot();
 97}
 98
 99DefineEngineMethod( GuiTerrPreviewCtrl, getRoot, Point2F, (), , "Return a Point2F representing the position of the root.")
100{
101   return object->getRoot();
102
103}
104
105DefineEngineMethod( GuiTerrPreviewCtrl, setOrigin, void, (Point2F pos), , "(float x, float y)"
106              "Set the origin of the view.")
107{
108   object->setOrigin( pos );
109}
110
111DefineEngineMethod( GuiTerrPreviewCtrl, getOrigin, Point2F, (), , "Return a Point2F containing the position of the origin.")
112{
113   return object->getOrigin();
114
115}
116
117DefineEngineMethod( GuiTerrPreviewCtrl, getValue, const char*, (), , "Returns a 4-tuple containing: root_x root_y origin_x origin_y")
118{
119   Point2F r = object->getRoot();
120   Point2F o = object->getOrigin();
121
122   static char valuebuf[64];
123   dSprintf(valuebuf,sizeof(valuebuf),"%g %g %g %g", r.x, -r.y, o.x, -o.y);
124   return valuebuf;
125}
126
127DefineEngineMethod( GuiTerrPreviewCtrl, setValue, void, (const char * tuple), , "Accepts a 4-tuple in the same form as getValue returns.\n\n"
128              "@see GuiTerrPreviewCtrl::getValue()")
129{
130   Point2F r,o;
131   dSscanf(tuple, "%g %g %g %g", &r.x, &r.y, &o.x, &o.y);
132   r.y = -r.y;
133   o.y = -o.y;
134   object->reset();
135   object->setRoot(r);
136   object->setOrigin(o);
137}
138
139bool GuiTerrPreviewCtrl::onWake()
140{
141   if (! Parent::onWake())
142      return false;
143
144   return true;
145}
146
147void GuiTerrPreviewCtrl::onSleep()
148{
149   Parent::onSleep();
150}
151
152void GuiTerrPreviewCtrl::setBitmap(const GFXTexHandle &handle)
153{
154   mTextureHandle = handle;
155}
156
157
158void GuiTerrPreviewCtrl::reset()
159{
160   mRoot.set(0,0);
161   mOrigin.set(0,0);
162}
163
164void GuiTerrPreviewCtrl::setRoot()
165{
166   mRoot += mOrigin;
167   mOrigin.set(0,0);
168}
169
170void GuiTerrPreviewCtrl::setRoot(const Point2F &p)
171{
172   mRoot = p;
173}
174
175void GuiTerrPreviewCtrl::setOrigin(const Point2F &p)
176{
177   mOrigin = p;
178}
179
180
181Point2F& GuiTerrPreviewCtrl::wrap(const Point2F &p)
182{
183   static Point2F result;
184   result = p;
185
186   while (result.x < 0.0f)
187      result.x += mTerrainSize;
188   while (result.x > mTerrainSize)
189      result.x -= mTerrainSize;
190   while (result.y < 0.0f)
191      result.y += mTerrainSize;
192   while (result.y > mTerrainSize)
193      result.y -= mTerrainSize;
194
195   return result;
196}
197
198Point2F& GuiTerrPreviewCtrl::worldToTexture(const Point2F &p)
199{
200   static Point2F result;
201   result = wrap( p + mRoot ) / mTerrainSize;
202   return result;
203}
204
205
206Point2F& GuiTerrPreviewCtrl::worldToCtrl(const Point2F &p)
207{
208   static Point2F result;
209   result = wrap( p - mCamera - mWorldScreenCenter );
210   result *= getWidth() / mTerrainSize;
211   return result;
212}
213
214
215void GuiTerrPreviewCtrl::onPreRender()
216{
217   setUpdate();
218}
219
220void GuiTerrPreviewCtrl::onRender(Point2I offset, const RectI &updateRect)
221{
222   CameraQuery query;
223   GameProcessCameraQuery(&query);
224   Point3F cameraRot;
225   TerrainBlock *terrBlock = NULL;
226
227   MatrixF matrix = query.cameraMatrix;
228   matrix.getColumn(3,&cameraRot);           // get Camera translation
229   mCamera.set(cameraRot.x, -cameraRot.y);
230   matrix.getRow(1,&cameraRot);              // get camera rotation
231
232   if (mTerrainEditor != NULL)
233      terrBlock = mTerrainEditor->getActiveTerrain();
234
235   if (!terrBlock)
236      return;
237
238   for(U32 i = 0; i < GFX->getNumSamplers(); i++)
239      GFX->setTexture(i, NULL);
240   
241   GFX->setupGenericShaders(GFXDevice::GSModColorTexture);
242   
243   Point2F terrPos(terrBlock->getPosition().x, terrBlock->getPosition().y);
244
245   mTerrainSize = terrBlock->getWorldBlockSize();
246
247
248   //----------------------------------------- RENDER the Terrain Bitmap
249   if (mTextureHandle)
250   {
251
252      GFXTextureObject *texture = (GFXTextureObject*)mTextureHandle;
253      if (texture)
254      {
255         //GFX->setLightingEnable(false);
256         GFX->setStateBlock(mTerrainBitmapStateBlock);
257         GFX->setTexture(0, texture);
258
259         Point2F screenP1(offset.x - 0.5f, offset.y + 0.5f);
260         Point2F screenP2(offset.x + getWidth() - 0.5f, offset.y + getWidth() + 0.5f);
261         Point2F textureP1( worldToTexture( mCamera - terrPos ) - Point2F(0.5f, 0.5f));
262         Point2F textureP2(textureP1 + Point2F(1.0f, 1.0f));
263
264         // the texture if flipped horz to reflect how the terrain is really drawn
265         PrimBuild::color3f(1.0f, 1.0f, 1.0f);
266         PrimBuild::begin(GFXTriangleStrip, 4);
267         PrimBuild::texCoord2f(textureP1.x, textureP1.y);
268         PrimBuild::vertex2f(screenP1.x, screenP1.y);       // left top
269
270         PrimBuild::texCoord2f(textureP2.x, textureP1.y);
271         PrimBuild::vertex2f(screenP2.x, screenP1.y);       // right top
272
273         PrimBuild::texCoord2f(textureP1.x, textureP2.y);
274         PrimBuild::vertex2f(screenP1.x, screenP2.y);       // left bottom
275
276         PrimBuild::texCoord2f(textureP2.x, textureP2.y);
277         PrimBuild::vertex2f(screenP2.x, screenP2.y);       // right bottom
278         PrimBuild::end();
279      }
280   }
281   //Draw blank texture
282   else
283   {
284      RectI rect(offset.x, offset.y, getWidth(), getHeight());
285       GFX->getDrawUtil()->drawRect(rect, ColorI(0,0,0));
286   }
287
288   GFX->setStateBlock(mControlsStateBlock);
289
290   //----------------------------------------- RENDER the '+' at the center of the Block
291
292   PrimBuild::color4f(1.0f, 1.0f, 1.0f, 1.0f);
293   Point2F center( worldToCtrl(terrPos + Point2F(mTerrainSize * 0.5f, mTerrainSize * 0.5f)) );
294   S32 y;
295   for (y=-1; y<=1; y++)
296   {
297      F32 yoffset = offset.y + y*256.0f;
298      for (S32 x=-1; x<=1; x++)
299      {
300         F32 xoffset = offset.x + x*256.0f;
301         PrimBuild::begin(GFXLineList, 4);
302            PrimBuild::vertex2f(xoffset + center.x, yoffset + center.y-5);
303            PrimBuild::vertex2f(xoffset + center.x, yoffset + center.y+6);
304            PrimBuild::vertex2f(xoffset + center.x-5, yoffset + center.y);
305            PrimBuild::vertex2f(xoffset + center.x+6, yoffset + center.y);
306         PrimBuild::end();
307      }
308   }
309
310   //----------------------------------------- RENDER the Block Corners
311   Point2F cornerf( worldToCtrl(terrPos)  + Point2F(0.125f, 0.125f));
312   Point2I corner=<a href="/coding/class/classpoint2i/">Point2I</a>((S32)cornerf.x,(S32)cornerf.y);
313   for (y=-1; y<=1; y++)
314   {
315      S32 yoffset = offset.y + y*256;
316      for (S32 x=-1; x<=1; x++)
317      {
318         S32 xoffset = offset.x + x*256;
319         PrimBuild::begin(GFXLineStrip, 3);
320            PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.3f);
321            PrimBuild::vertex2i(xoffset + corner.x, yoffset + corner.y-128);
322            PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.7f);
323            PrimBuild::vertex2i(xoffset + corner.x, yoffset + corner.y);
324            PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.3f);
325            PrimBuild::vertex2i(xoffset + corner.x+128, yoffset + corner.y);
326         PrimBuild::end();
327         PrimBuild::begin(GFXLineStrip, 3);
328            PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.3f);
329            PrimBuild::vertex2i(xoffset + corner.x, yoffset + corner.y+128);
330            PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.7f);
331            PrimBuild::vertex2i(xoffset + corner.x, yoffset + corner.y);
332            PrimBuild::color4f(1.0f, 1.0f, 1.0f, 0.3f);
333            PrimBuild::vertex2i(xoffset + corner.x-128, yoffset + corner.y);
334         PrimBuild::end();
335      }
336   }
337
338  //----------------------------------------- RENDER the Viewcone
339   Point2F pointA(cameraRot.x * -40, cameraRot.y * -40);
340   Point2F pointB(-pointA.y, pointA.x);
341
342   F32 tann = mTan(0.5f);
343   Point2F point1( pointA + pointB * tann );
344   Point2F point2( pointA - pointB * tann );
345
346   center.set((F32)(offset.x + getWidth() / 2), (F32)(offset.y + getHeight() / 2 ));
347   PrimBuild::begin(GFXLineStrip, 3);
348      PrimBuild::color4f(1.0f, 0.0f, 0.0f, 0.7f);
349      PrimBuild::vertex2i((S32)(center.x + point1.x), (S32)(center.y + point1.y));
350      PrimBuild::color4f(1.0f, 0.0f, 0.0f, 1.0f);
351      PrimBuild::vertex2i((S32)center.x,(S32)center.y);
352      PrimBuild::color4f(1.0f, 0.0f, 0.0f, 0.7f);
353      PrimBuild::vertex2i((S32)(center.x + point2.x), (S32)(center.y + point2.y));
354   PrimBuild::end();
355
356
357   renderChildControls(offset, updateRect);
358}
359
360