made DeckMenuItem a subclass of SimpleMenuItem

moved code to check on click position into base class from DeckMenuItem
TODO: apply same logic for standard menus
This commit is contained in:
techdragon.nguyen@gmail.com
2012-01-08 14:55:22 +00:00
parent 5ff2ac3123
commit 1cef5d690b
4 changed files with 49 additions and 41 deletions

View File

@@ -5,24 +5,18 @@
#include <JLBFont.h>
#include <JGui.h>
#include "DeckMenu.h"
#include "SimpleMenuItem.h"
using std::string;
class DeckMenuItem: public JGuiObject
class DeckMenuItem: public SimpleMenuItem
{
private:
bool mHasFocus;
bool mScrollEnabled;
bool mDisplayInitialized;
bool mIsValidSelection;
DeckMenu* parent;
int fontId;
string mText;
float mTitleResetWidth;
static float mYOffset;
void checkUserClick();
public:
string imageFilename;
@@ -30,11 +24,9 @@ public:
float mScrollerOffset;
DeckMetaData *meta;
float mX;
float mY;
void Relocate(float x, float y);
float GetWidth();
string GetText()
{
return mText;

View File

@@ -14,16 +14,25 @@ using std::string;
class SimpleMenuItem: public JGuiObject
{
private:
bool mHasFocus;
SimpleMenu* parent;
int fontId;
string mText;
float mScale;
float mTargetScale;
protected:
int fontId;
string mText;
bool mHasFocus;
static float mYOffset;
float mXOffset;
bool mIsValidSelection;
void checkUserClick();
public:
string desc;
SimpleMenuItem(int id);
SimpleMenuItem(SimpleMenu* _parent, int id, int fontId, string text, float x, float y, bool hasFocus = false, bool autoTranslate = false);
float mX;

View File

@@ -1,4 +1,4 @@
#include "PrecompiledHeader.h"
#include "PrecompiledHeader.h"
#include "DeckMenuItem.h"
#include "Translate.h"
@@ -11,19 +11,22 @@
const int kHorizontalScrollSpeed = 30; // higher numbers mean faster scrolling
float DeckMenuItem::mYOffset = 0;
DeckMenuItem::DeckMenuItem(DeckMenu* _parent, int id, int fontId, string text, float x, float y, bool hasFocus, bool autoTranslate, DeckMetaData *deckMetaData)
: JGuiObject(id), parent(_parent), fontId(fontId), mX(x), mY(y)
DeckMenuItem::DeckMenuItem(DeckMenu* _parent, int id, int iFontId, string text, float x, float y, bool hasFocus, bool autoTranslate, DeckMetaData *deckMetaData): SimpleMenuItem(id)
{
mEngine = JGE::GetInstance();
parent = _parent;
fontId = iFontId;
mY = y;
mX = x;
WFont * mFont = WResourceManager::Instance()->GetWFont(fontId);
meta = deckMetaData;
mText = trim(text);
mIsValidSelection = false;
SimpleMenuItem::mIsValidSelection = false;
if (autoTranslate)
mText = _(mText);
mXOffset = kItemXOffset;
mHasFocus = hasFocus;
float newImageWidth = 0.0f;
@@ -89,7 +92,7 @@ void DeckMenuItem::Update(float dt)
void DeckMenuItem::RenderWithOffset(float yOffset)
{
mYOffset = yOffset;
SimpleMenuItem::mYOffset = yOffset;
WFont * mFont = WResourceManager::Instance()->GetWFont(fontId);
@@ -120,26 +123,10 @@ void DeckMenuItem::Render()
RenderWithOffset(0);
}
void DeckMenuItem::checkUserClick()
{
#ifdef IOS
int x1 = -1, y1 = -1;
if (mEngine->GetLeftClickCoordinates(x1, y1))
{
mIsValidSelection = false;
int x2 = kItemXOffset, y2 = static_cast<int>(mY + mYOffset);
if ( (x1 >= x2) && (x1 <= (x2 + ITEM_PX_WIDTH)) && (y1 >= y2) && (y1 < (y2 + kItemYHeight)))
mIsValidSelection = true;
}
#else
mIsValidSelection = true;
#endif
}
void DeckMenuItem::Entering()
{
checkUserClick();
SimpleMenuItem::checkUserClick();
mHasFocus = true;
parent->mSelectionTargetY = mY;
}
@@ -147,14 +134,14 @@ void DeckMenuItem::Entering()
bool DeckMenuItem::Leaving(JButton key)
{
// check to see if the user clicked on the object, if so return true.
checkUserClick();
SimpleMenuItem::checkUserClick();
mHasFocus = false;
return true;
}
bool DeckMenuItem::ButtonPressed()
{
return mIsValidSelection;
return SimpleMenuItem::mIsValidSelection;
}
void DeckMenuItem::Relocate(float x, float y)

View File

@@ -6,6 +6,11 @@
float SimpleMenuItem::mYOffset = 0;
SimpleMenuItem::SimpleMenuItem(int id): JGuiObject(id)
{
}
SimpleMenuItem::SimpleMenuItem(SimpleMenu* _parent, int id, int fontId, string text, float x, float y, bool hasFocus, bool autoTranslate) :
JGuiObject(id), parent(_parent), fontId(fontId), mX(x), mY(y)
{
@@ -17,7 +22,8 @@ SimpleMenuItem::SimpleMenuItem(SimpleMenu* _parent, int id, int fontId, string t
mScale = 1.0f;
mTargetScale = 1.0f;
mXOffset = mX;
if (hasFocus) Entering();
}
@@ -47,6 +53,20 @@ void SimpleMenuItem::Update(float dt)
}
}
void SimpleMenuItem::checkUserClick()
{
int x1 = -1, y1 = -1;
if (mEngine->GetLeftClickCoordinates(x1, y1))
{
mIsValidSelection = false;
int x2 = mXOffset, y2 = static_cast<int>(mY + mYOffset);
if ( (x1 >= x2) && (x1 <= (x2 + 200)) && (y1 >= y2) && (y1 < (y2 + 30)))
mIsValidSelection = true;
}
else
mIsValidSelection = true;
}
void SimpleMenuItem::Entering()
{
mHasFocus = true;