Cleaned up the SimpleMenu drawing routines. I was seeing intermittently weird edge lines on the spades of the menu system, as well as gaps. The code was doing a lot of hacky +/- of arbitrary pixel values for the positioning, so I fixed it up to do everything relative to the widths of the pole & the actual spade graphic's size. Also cleaned up the pngs themselves, as they had no need for a transparent pixel rim.

Also moved some static value definitions used in the drawing code from the class header to a private namespace in the cpp file, as they don't need to be seen by any clients of the class.
This commit is contained in:
wrenczes@gmail.com
2010-10-17 00:20:25 +00:00
parent 7e780e18c1
commit a7ffe4990b
4 changed files with 33 additions and 32 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 354 B

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 354 B

After

Width:  |  Height:  |  Size: 2.9 KiB

-7
View File
@@ -10,13 +10,6 @@
#include "hge/hgeparticle.h" #include "hge/hgeparticle.h"
class SimpleMenu:public JGuiController{ class SimpleMenu:public JGuiController{
private:
// See SimpleMenu.cpp for values
static const unsigned SIDE_SIZE;
static const unsigned VMARGIN;
static const unsigned HMARGIN;
static const signed LINE_HEIGHT;
private: private:
int mHeight, mWidth, mX, mY; int mHeight, mWidth, mX, mY;
int fontId; int fontId;
+33 -25
View File
@@ -5,10 +5,13 @@
#include "../include/GameApp.h" #include "../include/GameApp.h"
#include "../include/Translate.h" #include "../include/Translate.h"
const unsigned SimpleMenu::SIDE_SIZE = 7; namespace
const unsigned SimpleMenu::VMARGIN = 16; {
const unsigned SimpleMenu::HMARGIN = 30; const unsigned int kPoleWidth = 7;
const signed SimpleMenu::LINE_HEIGHT = 20; const unsigned int kVerticalMargin = 16;
const unsigned int kHorizontalMargin = 30;
const signed int kLineHeight = 20;
}
JQuad* SimpleMenu::spadeR = NULL; JQuad* SimpleMenu::spadeR = NULL;
JQuad* SimpleMenu::spadeL = NULL; JQuad* SimpleMenu::spadeL = NULL;
@@ -29,7 +32,7 @@ PIXEL_TYPE SimpleMenu::jewelGraphics[9] = {0x3FFFFFFF,0x63645AEA,0x610D0D98,
SimpleMenu::SimpleMenu(int id, JGuiListener* listener, int fontId, int x, int y, const char * _title, int _maxItems): JGuiController(id, listener), fontId(fontId){ SimpleMenu::SimpleMenu(int id, JGuiListener* listener, int fontId, int x, int y, const char * _title, int _maxItems): JGuiController(id, listener), fontId(fontId){
autoTranslate = true; autoTranslate = true;
mHeight = 2 * VMARGIN; mHeight = 2 * kVerticalMargin;
mWidth = 0; mWidth = 0;
mX = x; mX = x;
mY = y; mY = y;
@@ -40,7 +43,7 @@ SimpleMenu::SimpleMenu(int id, JGuiListener* listener, int fontId, int x, int y,
timeOpen = 0; timeOpen = 0;
closed = false; closed = false;
++refCount; ++refCount;
selectionTargetY = selectionY = y + VMARGIN; selectionTargetY = selectionY = y + kVerticalMargin;
JRenderer* renderer = JRenderer::GetInstance(); JRenderer* renderer = JRenderer::GetInstance();
@@ -48,8 +51,8 @@ SimpleMenu::SimpleMenu(int id, JGuiListener* listener, int fontId, int x, int y,
if (!spadeRTex) spadeRTex = resources.RetrieveTexture("spade_ur.png", RETRIEVE_MANAGE); if (!spadeRTex) spadeRTex = resources.RetrieveTexture("spade_ur.png", RETRIEVE_MANAGE);
if (!jewelTex) jewelTex= renderer->CreateTexture(5, 5, TEX_TYPE_USE_VRAM); if (!jewelTex) jewelTex= renderer->CreateTexture(5, 5, TEX_TYPE_USE_VRAM);
if (!sideTex) sideTex = resources.RetrieveTexture("menuside.png", RETRIEVE_MANAGE); if (!sideTex) sideTex = resources.RetrieveTexture("menuside.png", RETRIEVE_MANAGE);
if (NULL == spadeL) spadeL = resources.RetrieveQuad("spade_ul.png", 2, 1, 16, 13, "spade_ul", RETRIEVE_MANAGE); if (NULL == spadeL) spadeL = resources.RetrieveQuad("spade_ul.png", 0, 0, 11, 11, "spade_ul", RETRIEVE_MANAGE);
if (NULL == spadeR) spadeR = resources.RetrieveQuad("spade_ur.png", 2, 1, 16, 13, "spade_ur", RETRIEVE_MANAGE); if (NULL == spadeR) spadeR = resources.RetrieveQuad("spade_ur.png", 0, 0, 11, 11, "spade_ur", RETRIEVE_MANAGE);
if (NULL == jewel) jewel = NEW JQuad(jewelTex, 1, 1, 3, 3); if (NULL == jewel) jewel = NEW JQuad(jewelTex, 1, 1, 3, 3);
if (NULL == side) side = resources.RetrieveQuad("menuside.png", 1, 1, 1, 7,"menuside", RETRIEVE_MANAGE); if (NULL == side) side = resources.RetrieveQuad("menuside.png", 1, 1, 1, 7,"menuside", RETRIEVE_MANAGE);
@@ -59,27 +62,31 @@ SimpleMenu::SimpleMenu(int id, JGuiListener* listener, int fontId, int x, int y,
stars->FireAt(mX, mY); stars->FireAt(mX, mY);
} }
void SimpleMenu::drawHorzPole(int x, int y, int width) { void SimpleMenu::drawHorzPole(int x, int y, int width)
{
JRenderer* renderer = JRenderer::GetInstance(); JRenderer* renderer = JRenderer::GetInstance();
renderer->RenderQuad(side, x + 5 , y - SIDE_SIZE / 2, 0, width - 10); static int offset = (spadeR->mWidth - kPoleWidth) / 2;
renderer->RenderQuad(side, x, y, 0, width);
spadeR->SetHFlip(true); spadeR->SetHFlip(true);
spadeL->SetHFlip(false); spadeL->SetHFlip(false);
renderer->RenderQuad(spadeR, x - 9, y - 6); renderer->RenderQuad(spadeR, x - offset, y - offset);
renderer->RenderQuad(spadeL, x + width - 5, y - 6); renderer->RenderQuad(spadeL, x - offset + width, y - offset);
renderer->RenderQuad(jewel, x, y - 1); renderer->RenderQuad(jewel, x, y - 1);
renderer->RenderQuad(jewel, x + width - 1, y - 1); renderer->RenderQuad(jewel, x + width - 1, y - 1);
} }
void SimpleMenu::drawVertPole(int x, int y, int height) { void SimpleMenu::drawVertPole(int x, int y, int height)
{
JRenderer* renderer = JRenderer::GetInstance(); JRenderer* renderer = JRenderer::GetInstance();
renderer->RenderQuad(side, x - SIDE_SIZE / 2, y + height - 5, -M_PI/2, height - 10); static int offset = (spadeR->mHeight - kPoleWidth) / 2;
renderer->RenderQuad(side, x + kPoleWidth, y, M_PI/2, height);
spadeR->SetHFlip(false); spadeR->SetHFlip(false);
spadeL->SetHFlip(true); spadeL->SetHFlip(true);
renderer->RenderQuad(spadeR, x - 6, y + 7, -M_PI/2); renderer->RenderQuad(spadeR, x + kPoleWidth + offset, y - offset, M_PI/2);
renderer->RenderQuad(spadeL, x - 6, y + height + 11, -M_PI/2); renderer->RenderQuad(spadeL, x + kPoleWidth + offset, y - offset + height, M_PI/2);
renderer->RenderQuad(jewel, x - 1, y - 1); renderer->RenderQuad(jewel, x - 1, y - 1);
renderer->RenderQuad(jewel, x - 1, y + height - 1); renderer->RenderQuad(jewel, x - 1, y + height - 1);
@@ -89,15 +96,15 @@ void SimpleMenu::Render() {
WFont * titleFont = resources.GetWFont("smallface"); WFont * titleFont = resources.GetWFont("smallface");
WFont * mFont = resources.GetWFont(fontId); WFont * mFont = resources.GetWFont(fontId);
if (0 == mWidth) { if (0 == mWidth) {
float sY = mY + VMARGIN; float sY = mY + kVerticalMargin;
for (int i = startId; i < startId + mCount; ++i) { for (int i = startId; i < startId + mCount; ++i) {
int width = (static_cast<SimpleMenuItem*>(mObjects[i]))->GetWidth(); int width = (static_cast<SimpleMenuItem*>(mObjects[i]))->GetWidth();
if (mWidth < width) mWidth = width; if (mWidth < width) mWidth = width;
} }
if ((!title.empty()) && (mWidth < titleFont->GetStringWidth(title.c_str()))) mWidth = titleFont->GetStringWidth(title.c_str()); if ((!title.empty()) && (mWidth < titleFont->GetStringWidth(title.c_str()))) mWidth = titleFont->GetStringWidth(title.c_str());
mWidth += 2*HMARGIN; mWidth += 2*kHorizontalMargin;
for (int i = startId; i < startId + mCount; ++i) { for (int i = startId; i < startId + mCount; ++i) {
float y = mY + VMARGIN + i * LINE_HEIGHT; float y = mY + kVerticalMargin + i * kLineHeight;
SimpleMenuItem * smi = static_cast<SimpleMenuItem*>(mObjects[i]); SimpleMenuItem * smi = static_cast<SimpleMenuItem*>(mObjects[i]);
smi->Relocate(mX + mWidth / 2, y); smi->Relocate(mX + mWidth / 2, y);
if (smi->hasFocus()) sY = y; if (smi->hasFocus()) sY = y;
@@ -114,9 +121,11 @@ void SimpleMenu::Render() {
renderer->FillRect(mX, mY, mWidth, height, ARGB(180,0,0,0)); renderer->FillRect(mX, mY, mWidth, height, ARGB(180,0,0,0));
renderer->SetTexBlend(BLEND_SRC_ALPHA, BLEND_ONE_MINUS_SRC_ALPHA);
drawVertPole(mX, mY - 16, height + 32); drawVertPole(mX, mY - 16, height + 32);
drawVertPole(mX + mWidth, mY - 16, height + 32); drawVertPole(mX + mWidth, mY - 16, height + 32);
drawHorzPole(mX - 16, mY, mWidth + 32); drawHorzPole(mX - 16, mY, mWidth + 32);
drawHorzPole(mX - 25, mY + height, mWidth + 50);
renderer->SetTexBlend(BLEND_SRC_ALPHA, BLEND_ONE); renderer->SetTexBlend(BLEND_SRC_ALPHA, BLEND_ONE);
stars->Render(); stars->Render();
@@ -127,16 +136,15 @@ void SimpleMenu::Render() {
titleFont->DrawString(title.c_str(), mX+mWidth/2, mY - 3, JGETEXT_CENTER); titleFont->DrawString(title.c_str(), mX+mWidth/2, mY - 3, JGETEXT_CENTER);
for (int i = startId; i < startId + maxItems ; i++){ for (int i = startId; i < startId + maxItems ; i++){
if (i > mCount-1) break; if (i > mCount-1) break;
if ((static_cast<SimpleMenuItem*>(mObjects[i]))->mY - LINE_HEIGHT * startId < mY + height - LINE_HEIGHT + 7) { if ((static_cast<SimpleMenuItem*>(mObjects[i]))->mY - kLineHeight * startId < mY + height - kLineHeight + 7) {
if (static_cast<SimpleMenuItem*>(mObjects[i])->hasFocus()){ if (static_cast<SimpleMenuItem*>(mObjects[i])->hasFocus()){
resources.GetWFont(Constants::MAIN_FONT)->DrawString(static_cast<SimpleMenuItem*>(mObjects[i])->desc.c_str(),mX+mWidth+10,mY+15); resources.GetWFont(Constants::MAIN_FONT)->DrawString(static_cast<SimpleMenuItem*>(mObjects[i])->desc.c_str(),mX+mWidth+10,mY+15);
mFont->SetColor(ARGB(255,255,255,0)); mFont->SetColor(ARGB(255,255,255,0));
} else } else
mFont->SetColor(ARGB(150,255,255,255)); mFont->SetColor(ARGB(150,255,255,255));
(static_cast<SimpleMenuItem*>(mObjects[i]))->RenderWithOffset(-LINE_HEIGHT*startId); (static_cast<SimpleMenuItem*>(mObjects[i]))->RenderWithOffset(-kLineHeight*startId);
} }
} }
drawHorzPole(mX - 25, mY + height, mWidth + 50);
} }
void SimpleMenu::Update(float dt){ void SimpleMenu::Update(float dt){
@@ -148,7 +156,7 @@ void SimpleMenu::Update(float dt){
stars->Update(dt); stars->Update(dt);
selectionT += 3*dt; selectionT += 3*dt;
selectionY += (selectionTargetY - selectionY) * 8 * dt; selectionY += (selectionTargetY - selectionY) * 8 * dt;
stars->MoveTo(mX + HMARGIN + ((mWidth-2*HMARGIN)*(1+cos(selectionT))/2), selectionY + 5 * cos(selectionT*2.35) + LINE_HEIGHT / 2 - LINE_HEIGHT * startId); stars->MoveTo(mX + kHorizontalMargin + ((mWidth-2*kHorizontalMargin)*(1+cos(selectionT))/2), selectionY + 5 * cos(selectionT*2.35) + kLineHeight / 2 - kLineHeight * startId);
if (timeOpen < 0) { if (timeOpen < 0) {
timeOpen += dt * 10; timeOpen += dt * 10;
if (timeOpen >= 0) { timeOpen = 0; closed = true; stars->FireAt(mX, mY); } if (timeOpen >= 0) { timeOpen = 0; closed = true; stars->FireAt(mX, mY); }
@@ -159,10 +167,10 @@ void SimpleMenu::Update(float dt){
} }
void SimpleMenu::Add(int id, const char * text,string desc, bool forceFocus){ void SimpleMenu::Add(int id, const char * text,string desc, bool forceFocus){
SimpleMenuItem * smi = NEW SimpleMenuItem(this, id, fontId, text, 0, mY + VMARGIN + mCount*LINE_HEIGHT, (mCount == 0), autoTranslate); SimpleMenuItem * smi = NEW SimpleMenuItem(this, id, fontId, text, 0, mY + kVerticalMargin + mCount*kLineHeight, (mCount == 0), autoTranslate);
smi->desc = desc; smi->desc = desc;
JGuiController::Add(smi); JGuiController::Add(smi);
if (mCount <= maxItems) mHeight += LINE_HEIGHT; if (mCount <= maxItems) mHeight += kLineHeight;
if (forceFocus){ if (forceFocus){
mObjects[mCurr]->Leaving(JGE_BTN_DOWN); mObjects[mCurr]->Leaving(JGE_BTN_DOWN);
mCurr = mCount-1; mCurr = mCount-1;