#include "CGUIMeshViewer.h"
#ifdef _IRR_COMPILE_WITH_GUI_
#include "IGUIEnvironment.h"
#include "IVideoDriver.h"
#include "IAnimatedMesh.h"
#include "IMesh.h"
#include "os.h"
#include "IGUISkin.h"
namespace irr
{
namespace gui
{
CGUIMeshViewer::CGUIMeshViewer(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
: IGUIMeshViewer(environment, parent, id, rectangle), Mesh(0)
{
#ifdef _DEBUG
setDebugName("CGUIMeshViewer");
#endif
}
CGUIMeshViewer::~CGUIMeshViewer()
{
if (Mesh)
Mesh->drop();
}
void CGUIMeshViewer::setMesh(scene::IAnimatedMesh* mesh)
{
if (mesh)
mesh->grab();
if (Mesh)
Mesh->drop();
Mesh = mesh;
}
scene::IAnimatedMesh* CGUIMeshViewer::getMesh() const
{
return Mesh;
}
void CGUIMeshViewer::setMaterial(const video::SMaterial& material)
{
Material = material;
}
const video::SMaterial& CGUIMeshViewer::getMaterial() const
{
return Material;
}
bool CGUIMeshViewer::OnEvent(const SEvent& event)
{
return IGUIElement::OnEvent(event);
}
void CGUIMeshViewer::draw()
{
if (!IsVisible)
return;
IGUISkin* skin = Environment->getSkin();
video::IVideoDriver* driver = Environment->getVideoDriver();
core::rect<s32> viewPort = AbsoluteRect;
viewPort.LowerRightCorner.X -= 1;
viewPort.LowerRightCorner.Y -= 1;
viewPort.UpperLeftCorner.X += 1;
viewPort.UpperLeftCorner.Y += 1;
viewPort.clipAgainst(AbsoluteClippingRect);
core::rect<s32> frameRect(AbsoluteRect);
frameRect.LowerRightCorner.Y = frameRect.UpperLeftCorner.Y + 1;
skin->draw2DRectangle(this, skin->getColor(EGDC_3D_SHADOW), frameRect, &AbsoluteClippingRect);
frameRect.LowerRightCorner.Y = AbsoluteRect.LowerRightCorner.Y;
frameRect.LowerRightCorner.X = frameRect.UpperLeftCorner.X + 1;
skin->draw2DRectangle(this, skin->getColor(EGDC_3D_SHADOW), frameRect, &AbsoluteClippingRect);
frameRect = AbsoluteRect;
frameRect.UpperLeftCorner.X = frameRect.LowerRightCorner.X - 1;
skin->draw2DRectangle(this, skin->getColor(EGDC_3D_HIGH_LIGHT), frameRect, &AbsoluteClippingRect);
frameRect = AbsoluteRect;
frameRect.UpperLeftCorner.Y = AbsoluteRect.LowerRightCorner.Y - 1;
frameRect.LowerRightCorner.Y = AbsoluteRect.LowerRightCorner.Y;
skin->draw2DRectangle(this, skin->getColor(EGDC_3D_HIGH_LIGHT), frameRect, &AbsoluteClippingRect);
if (Mesh)
{
core::rect<s32> oldViewPort = driver->getViewPort();
driver->setViewPort(viewPort);
core::matrix4 mat;
mat.makeIdentity();
mat.setTranslation(core::vector3df(0,0,0));
driver->setTransform(video::ETS_WORLD, mat);
driver->setMaterial(Material);
u32 frame = 0;
if(Mesh->getFrameCount())
frame = (os::Timer::getTime()/20)%Mesh->getFrameCount();
const scene::IMesh* const m = Mesh->getMesh(frame);
for (u32 i=0; i<m->getMeshBufferCount(); ++i)
{
scene::IMeshBuffer* mb = m->getMeshBuffer(i);
driver->drawVertexPrimitiveList(mb->getVertices(),
mb->getVertexCount(), mb->getIndices(),
mb->getIndexCount()/ 3, mb->getVertexType(),
scene::EPT_TRIANGLES, mb->getIndexType());
}
driver->setViewPort(oldViewPort);
}
IGUIElement::draw();
}
}
}
#endif