PDA

View Full Version : RoundedRectRenderComponent



BigBMan
10-05-2011, 07:01 AM
I'm looking to create a rounded rect component for dialogs/menus, etc. The code below is my first successful attempt.

Not too well versed in OpenGL Ortho rendering, so this may be suboptimal. For the corners, I'm rendering four "frames" of a single image (608). For the center (potentially sub-optimal part), I'm filling in the space by rendering a horizontal and vertical rect. I'm thinking ideally, I could blit the transparency of the corner images over a single rect, but not sure if that's the right approach.

Anyways, future improvements include images for sides/borders, testing out scaling, growing/shrinking, further uses for menus/widgets...

Thoughts?



#include "PlatformPrecomp.h"

#include "RoundedRectRenderComponent.h"
#include "util/GLESUtils.h"
#include "BaseApp.h"
#include "Entity/EntityUtils.h"

RoundedRectRenderComponent::RoundedRectRenderCompo nent()
{
m_pSurf = NULL;
SetName("RoundedRectRender");
}

RoundedRectRenderComponent::~RoundedRectRenderComp onent()
{
}

void RoundedRectRenderComponent::OnAdd(Entity *pEnt)
{
EntityComponent::OnAdd(pEnt);

//shared with the rest of the entity
m_pPos2d = &GetParent()->GetVar("pos2d")->GetVector2();
m_pSize2d = &GetParent()->GetVar("size2d")->GetVector2();
m_pScale2d = &GetParent()->GetShared()->GetVarWithDefault("scale2d", Variant(1.0f, 1.0f))->GetVector2();
m_pRotation = &GetParent()->GetVar("rotation")->GetFloat(); //in degrees

m_pAlpha = &GetParent()->GetShared()->GetVarWithDefault("alpha", Variant(1.0f))->GetFloat();
m_pColor = &GetParent()->GetShared()->GetVarWithDefault("color", Variant(MAKE_RGBA(255,0,0,255)))->GetUINT32();
m_pColorMod = &GetParent()->GetShared()->GetVarWithDefault("colorMod", Variant(MAKE_RGBA(255,255,255,255)))->GetUINT32();

m_pFileName = &GetVar("fileName")->GetString(); //local to us

GetParent()->GetFunction("OnUpdate")->sig_function.connect(1, boost::bind(&RoundedRectRenderComponent::OnUpdate, this, _1));
GetParent()->GetFunction("OnRender")->sig_function.connect(1, boost::bind(&RoundedRectRenderComponent::OnRender, this, _1));

//if "fileName" is set, we'll know about it here
GetVar("fileName")->GetSigOnChanged()->connect(boost::bind(&RoundedRectRenderComponent::OnFileNameChanged, this, _1));
GetVar("fileName")->Set("interface/scroll_bar_caps.rttex"); //default
}


void RoundedRectRenderComponent::OnFileNameChanged(Vari ant *pDataObject)
{
SAFE_DELETE(m_pSurf);

m_pSurf = GetResourceManager()->GetSurfaceAnim(pDataObject->GetString());
if (m_pSurf)
{
m_pSurf->SetupAnim(2,2);
} else
{
LogMsg("RoundedRectRenderComponent: Can't load scroll bar pieces");
}
}

void RoundedRectRenderComponent::OnRemove()
{
EntityComponent::OnRemove();
}

void RoundedRectRenderComponent::OnUpdate(VariantList *pVList)
{
}

void RoundedRectRenderComponent::OnRender(VariantList *pVList)
{
if (!m_pSurf) return; //can't do anything without the graphics loaded

if (*m_pAlpha > 0.01)
{
CL_Vec2f vFinalPos = pVList->m_variant[0].GetVector2()+*m_pPos2d;
uint32 color = ColorCombine(*m_pColor, *m_pColorMod, *m_pAlpha);

if (GET_ALPHA(color) == 0) return;

CL_Vec2f vRotationPt = vFinalPos;

g_globalBatcher.Flush();

if (*m_pRotation != 0)
{
SetupOrtho();
PushRotationMatrix(*m_pRotation, vRotationPt);
vFinalPos -= vRotationPt;
}

CL_Rectf r = CL_Rectf(vFinalPos.x, vFinalPos.y, vFinalPos.x+ m_pSize2d->x, vFinalPos.y+m_pSize2d->y);

//
// Rect
//
//DrawFilledRect(r, color);

//
// Blit corners
//
// top-left
m_pSurf->BlitAnim(r.left, r.top, 0, 0, color);

// top-right
m_pSurf->BlitAnim(r.right - m_pSurf->GetFrameWidth(), r.top, 1, 0, color);

// Draw horizontal rect
DrawFilledRect(CL_Rectf(r.left, r.top + m_pSurf->GetFrameHeight(),
r.right, r.bottom - m_pSurf->GetFrameHeight()),
color);

// Draw vertical rect
DrawFilledRect(CL_Rectf(r.left + m_pSurf->GetFrameWidth(), r.top,
r.right - m_pSurf->GetFrameWidth(), r.bottom),
color);

// bottom-left
m_pSurf->BlitAnim(r.left, r.bottom - m_pSurf->GetFrameHeight(), 0, 1, color);

// bottom-right
m_pSurf->BlitAnim(r.right - m_pSurf->GetFrameWidth(), r.bottom - m_pSurf->GetFrameHeight(), 1, 1, color);

if (*m_pRotation != 0)
{
PopRotationMatrix();
}
}
}

Seth
10-07-2011, 05:27 AM
Nice. I'm sure speed is fine. :ninja: I've love a full "dynamic skinned bmp gui" system, one of those things I always want but never put in the time to write. :sweatdrop: