GuiDTSSnapshotCtrl
by Thomas -elfprince13- Dickerson · 11/03/2009 (12:46 am) · 2 comments
I'd appreciate some sort of mention if you use this in your game, and that you provide feedback here if you make changes that you think would be helpful. Create the following two files (guiDTSSnapshotCtrl.h and guiDTSSnapshotCtrl.cc) and add them to your project. Script examples are at the bottom.
The only instability I've noticed is that sometimes when resizing the control by dragging the corners around in the GUI editor that it crashes. Resizing otherwise seems to be ok. Also, there appears to be strange visual distortion if the control is significantly wider than it is tall. Also, the transparency seems to be a little off, but that seems to be a side effect of using TSShapeInstance::snapshot(); I'm not sure of the cause of either of these, but if you happen to find them, I'd appreciate the heads up.
Script declaration:
use ShapeViewer.setSkin() and ShapeViewer.setShapeName() to change either the skin or the shapename. Setting the skin to an empty string will cause the shape to render using the textures it would have if it was created and then never had a skin set.
The only instability I've noticed is that sometimes when resizing the control by dragging the corners around in the GUI editor that it crashes. Resizing otherwise seems to be ok. Also, there appears to be strange visual distortion if the control is significantly wider than it is tall. Also, the transparency seems to be a little off, but that seems to be a side effect of using TSShapeInstance::snapshot(); I'm not sure of the cause of either of these, but if you happen to find them, I'd appreciate the heads up.
/*
* guiDTSSnapshotCtrl.h
* torque_xcode_2_2_UB
*
* Created by Thomas Dickerson on 10/31/09.
* Copyright 2009 Thomas Dickerson. All rights reserved.
*
*/
#ifndef _GUIDTSSNAPSHOTCTRL_H_
#define _GUIDTSSNAPSHOTCTRL_H_
#ifndef _GUICONTROL_H_
#include "gui/core/guiControl.h"
#endif
#ifndef _GTEXMANAGER_H_
#include "dgl/gTexManager.h"
#endif
/// Renders a bitmap.
class GuiDTSSnapshotCtrl : public GuiControl
{
private:
typedef GuiControl Parent;
protected:
static bool setShapeName( void *obj, const char *data );
static bool setSkin( void *obj, const char *data );
StringTableEntry mShapeName;
StringTableEntry mSkin;
TextureHandle mTextureHandle;
Point2I startPoint;
bool mWrap;
public:
//creation methods
DECLARE_CONOBJECT(GuiDTSSnapshotCtrl);
GuiDTSSnapshotCtrl();
static void initPersistFields();
//Parental methods
bool onWake();
void onSleep();
void inspectPostApply();
void setShapeName(const char *name,bool resize = false, bool updateShapeName = true);
void setSkin(const char *skin);
S32 getWidth() const { return(mTextureHandle.getWidth()); }
S32 getHeight() const { return(mTextureHandle.getHeight()); }
void onRender(Point2I offset, const RectI &updateRect);
void setValue(S32 x, S32 y);
};
#endif/*
* guiDTSSnapshotCtrl.cc
* torque_xcode_2_2_UB
*
* Created by Thomas Dickerson on 10/31/09.
* Copyright 2009 Thomas Dickerson. All rights reserved.
*
*/
#include "console/console.h"
#include "console/consoleTypes.h"
#include "dgl/dgl.h"
#include "ts/tsShape.h"
#include "ts/tsShapeInstance.h"
#include "gui/controls/guiDTSSnapshotCtrl.h"
IMPLEMENT_CONOBJECT(GuiDTSSnapshotCtrl);
GuiDTSSnapshotCtrl::GuiDTSSnapshotCtrl(void)
{
mShapeName = StringTable->insert("");
mSkin = StringTable->insert("");
startPoint.set(0, 0);
mWrap = false;
}
bool GuiDTSSnapshotCtrl::setShapeName( void *obj, const char *data )
{
static_cast<GuiDTSSnapshotCtrl *>( obj )->setShapeName( data );
return false;
}
bool GuiDTSSnapshotCtrl::setSkin( void *obj, const char *data )
{
static_cast<GuiDTSSnapshotCtrl *>( obj )->setSkin( data );
static_cast<GuiDTSSnapshotCtrl *>( obj )->setShapeName( NULL, false, false );
return false;
}
void GuiDTSSnapshotCtrl::initPersistFields()
{
Parent::initPersistFields();
addGroup("Misc");
addProtectedField( "shapeName", TypeFilename, Offset( mShapeName, GuiDTSSnapshotCtrl ), &setShapeName, &defaultProtectedGetFn, "" );
addProtectedField( "skin", TypeFilename, Offset( mSkin, GuiDTSSnapshotCtrl ), &setSkin, &defaultProtectedGetFn, "" );
addField("wrap", TypeBool, Offset(mWrap, GuiDTSSnapshotCtrl));
endGroup("Misc");
}
ConsoleMethod( GuiDTSSnapshotCtrl, setValue, void, 4, 4, "(int xAxis, int yAxis)"
"Set the offset of the bitmap.")
{
object->setValue(dAtoi(argv[2]), dAtoi(argv[3]));
}
ConsoleMethod( GuiDTSSnapshotCtrl, setShapeName, void, 3, 3, "(string filename)"
"Set the DTS file to be rendered in the control")
{
object->setShapeName(argv[2]);
}
ConsoleMethod( GuiDTSSnapshotCtrl, setSkin, void, 3, 3, "(string skin)"
"Set the skin that is being rendered with")
{
object->setSkin(argv[2]);
}
bool GuiDTSSnapshotCtrl::onWake()
{
if (! Parent::onWake())
return false;
setActive(true);
setShapeName(mShapeName);
return true;
}
void GuiDTSSnapshotCtrl::onSleep()
{
mTextureHandle = NULL;
Parent::onSleep();
}
//-------------------------------------
void GuiDTSSnapshotCtrl::inspectPostApply()
{
// if the extent is set to (0,0) in the gui editor and appy hit, this control will
// set it's extent to be exactly the size of the bitmap (if present)
Parent::inspectPostApply();
if (!mWrap && (mBounds.extent.x == 0) && (mBounds.extent.y == 0) && mTextureHandle)
{
TextureObject *texture = (TextureObject *) mTextureHandle;
mBounds.extent.x = texture->bitmapWidth;
mBounds.extent.y = texture->bitmapHeight;
}
}
void GuiDTSSnapshotCtrl::setSkin(const char *skin)
{
mSkin = StringTable->insert(skin);
setShapeName(NULL, false, false);
}
void GuiDTSSnapshotCtrl::setShapeName(const char *name, bool resize, bool updateShapeName)
{
if(updateShapeName) //haxy nonsense for setting the skin
mShapeName = StringTable->insert(name);
if (*mShapeName) {
//mTextureHandle = TextureHandle(mBitmapName, BitmapTexture, true);
/*
snapshotty goodness here
*/
Resource<TSShape> shape;
shape = ResourceManager->load(mShapeName);
if(shape.isNull()){
mTextureHandle = NULL;
setUpdate();
return;
}
shape->init();
shape->initMaterialList();
shape->preloadMaterialList();
TSShapeInstance *shapeinst = new TSShapeInstance(shape, true);
if(!shapeinst){
mTextureHandle = NULL;
setUpdate();
return;
}
if(*mSkin){
StringHandle sh(mSkin);
shapeinst->reSkin(sh);
}
MatrixF angMat;
angMat.mul(MatrixF(EulerF(0,0,0)),MatrixF(EulerF(2*M_PI_F/3.0,0,0)));
Point2I extents = getExtent();
GBitmap* snapshot = shapeinst->snapshot(extents.x, extents.y, false, angMat, true);
mTextureHandle = TextureHandle(NULL, snapshot, BitmapTexture);
delete shapeinst;
// Resize the control to fit the bitmap
if (resize) {
TextureObject* texture = (TextureObject *) mTextureHandle;
mBounds.extent.x = texture->bitmapWidth;
mBounds.extent.y = texture->bitmapHeight;
Point2I extent = getParent()->getExtent();
parentResized(extent,extent);
}
}
else
mTextureHandle = NULL;
setUpdate();
}
void GuiDTSSnapshotCtrl::onRender(Point2I offset, const RectI &updateRect)
{
if (mTextureHandle)
{
dglClearBitmapModulation();
if(mWrap)
{
TextureObject* texture = (TextureObject *) mTextureHandle;
RectI srcRegion;
RectI dstRegion;
float xdone = ((float)mBounds.extent.x/(float)texture->bitmapWidth)+1;
float ydone = ((float)mBounds.extent.y/(float)texture->bitmapHeight)+1;
int xshift = startPoint.x%texture->bitmapWidth;
int yshift = startPoint.y%texture->bitmapHeight;
for(int y = 0; y < ydone; ++y)
for(int x = 0; x < xdone; ++x)
{
srcRegion.set(0,0,texture->bitmapWidth,texture->bitmapHeight);
dstRegion.set( ((texture->bitmapWidth*x)+offset.x)-xshift,
((texture->bitmapHeight*y)+offset.y)-yshift,
texture->bitmapWidth,
texture->bitmapHeight);
dglDrawBitmapStretchSR(texture,dstRegion, srcRegion, false);
}
}
else
{
RectI rect(offset, mBounds.extent);
dglDrawBitmapStretch(mTextureHandle, rect);
}
}
if (mProfile->mBorder)
{
RectI rect(offset.x, offset.y, mBounds.extent.x, mBounds.extent.y);
dglDrawRect(rect, mProfile->mBorderColor);
}
renderChildControls(offset, updateRect);
}
void GuiDTSSnapshotCtrl::setValue(S32 x, S32 y)
{
if (mTextureHandle)
{
TextureObject* texture = (TextureObject *) mTextureHandle;
x+=texture->bitmapWidth/2;
y+=texture->bitmapHeight/2;
}
while (x < 0)
x += 256;
startPoint.x = x % 256;
while (y < 0)
y += 256;
startPoint.y = y % 256;
}Script declaration:
new GuiDTSSnapshotCtrl(ShapeViewer){
canSaveDynamicFields = "0";
Profile = "SnapshotProfile";
HorizSizing = "right";
VertSizing = "top";
position = "245 2";
Extent = "256 256";
MinExtent = "8 2";
canSave = "1";
Visible = "1";
hovertime = "1000";
shapeName = "";
skin = "";
wrap = "1";
};use ShapeViewer.setSkin() and ShapeViewer.setShapeName() to change either the skin or the shapename. Setting the skin to an empty string will cause the shape to render using the textures it would have if it was created and then never had a skin set.
About the author
C.S. PhD student at Brown University. Project lead for FreeBuild. Administrator, Cemetech tech community. Webmaster for the Village2Village Projects and the Vermont Sustainable Heating Initiative.
#2
i.e., you get a non-interactive picture of the model. Saves on rendering costs a bit since it renders the model once and saves to a texture.
As far as I know, this ought to work with any other filetype (like Collada .dae) that supports being loaded as a TSShape by the engine, but I haven't tested it.
09/23/2012 (2:08 pm)
This "Adds a GUI Control that allows you to preview skinned dts shapes as a static bmp. Useful for creating inventory guis."i.e., you get a non-interactive picture of the model. Saves on rendering costs a bit since it renders the model once and saves to a texture.
As far as I know, this ought to work with any other filetype (like Collada .dae) that supports being loaded as a TSShape by the engine, but I haven't tested it.

Ahsan Muzaheed
Default Studio Name
is it same thing like GuiObjectView,just having snap shot feature ?