* Use readbutton in the JGuiController.
This commit is contained in:
jean.chalard
2008-11-27 14:17:15 +00:00
parent b9e2980952
commit 5fec3d4a3d
+216 -254
View File
@@ -1,254 +1,216 @@
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
// //
// JGE++ is a hardware accelerated 2D game SDK for PSP/Windows. // JGE++ is a hardware accelerated 2D game SDK for PSP/Windows.
// //
// Licensed under the BSD license, see LICENSE in JGE root for details. // Licensed under the BSD license, see LICENSE in JGE root for details.
// //
// Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com> // Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) <jhkhui@gmail.com>
// //
//------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------
#include "../include/JGE.h" #include "../include/JGE.h"
#include "../include/JGui.h" #include "../include/JGui.h"
JGE* JGuiObject::mEngine = NULL; JGE* JGuiObject::mEngine = NULL;
JGE* JGuiController::mEngine = NULL; JGE* JGuiController::mEngine = NULL;
JGuiObject::JGuiObject(int id): mId(id) JGuiObject::JGuiObject(int id): mId(id)
{ {
mEngine = JGE::GetInstance(); mEngine = JGE::GetInstance();
} }
JGuiObject::~JGuiObject() JGuiObject::~JGuiObject()
{ {
// JGERelease(); // JGERelease();
} }
bool JGuiObject::Leaving(u32 key __attribute__((unused))) bool JGuiObject::Leaving(u32 key __attribute__((unused)))
{ {
return true; return true;
} }
bool JGuiObject::ButtonPressed() bool JGuiObject::ButtonPressed()
{ {
return false; return false;
} }
void JGuiObject::Entering() void JGuiObject::Entering()
{ {
} }
int JGuiObject::GetId() int JGuiObject::GetId()
{ {
return mId; return mId;
} }
void JGuiObject::Update(float dt __attribute__((unused))) void JGuiObject::Update(float dt __attribute__((unused)))
{ {
} }
JGuiController::JGuiController(int id, JGuiListener* listener) : mId(id), mListener(listener) JGuiController::JGuiController(int id, JGuiListener* listener) : mId(id), mListener(listener)
{ {
mEngine = JGE::GetInstance(); mEngine = JGE::GetInstance();
mBg = NULL; mBg = NULL;
mShadingBg = NULL; mShadingBg = NULL;
mCount = 0; mCount = 0;
mCurr = 0; mCurr = 0;
mCursorX = SCREEN_WIDTH/2; mCursorX = SCREEN_WIDTH/2;
mCursorY = SCREEN_HEIGHT/2; mCursorY = SCREEN_HEIGHT/2;
mShowCursor = false; mShowCursor = false;
mActionButton = PSP_CTRL_CIRCLE; mActionButton = PSP_CTRL_CIRCLE;
mLastKey = 0;
mStyle = JGUI_STYLE_WRAPPING;
mStyle = JGUI_STYLE_WRAPPING;
mActive = true;
mActive = true; }
}
JGuiController::~JGuiController()
JGuiController::~JGuiController() {
{ for (int i=0;i<mCount;i++)
for (int i=0;i<mCount;i++) if (mObjects[i]!=NULL)
if (mObjects[i]!=NULL) delete mObjects[i];
delete mObjects[i];
// JGERelease();
// JGERelease(); }
}
void JGuiController::Render()
void JGuiController::Render() {
{ // if (mShadingBg != NULL)
// if (mShadingBg != NULL) // jge->Gfx_BlendRect(mShadingBg, mShadingColor);
// jge->Gfx_BlendRect(mShadingBg, mShadingColor);
// if (mBg != NULL)
// if (mBg != NULL) // jge->Gfx_DrawImage(mBg, mBgX, mBgY);
// jge->Gfx_DrawImage(mBg, mBgX, mBgY);
for (int i=0;i<mCount;i++)
for (int i=0;i<mCount;i++) if (mObjects[i]!=NULL)
if (mObjects[i]!=NULL) mObjects[i]->Render();
mObjects[i]->Render(); }
}
void JGuiController::Update(float dt)
{
bool JGuiController::KeyRepeated(u32 key, float dt) for (int i=0;i<mCount;i++)
{ if (mObjects[i]!=NULL)
mObjects[i]->Update(dt);
bool doKey = false;
if (mLastKey != key) u32 key = mEngine->ReadButton();
{ if (key == mActionButton)
mLastKey = key; {
doKey = true; if (mObjects[mCurr] != NULL && mObjects[mCurr]->ButtonPressed())
mKeyRepeatDelay = JGUI_INITIAL_DELAY; {
} if (mListener != NULL)
else {
{ mListener->ButtonPressed(mId, mObjects[mCurr]->GetId());
mKeyRepeatDelay -= dt; return;
if (mKeyRepeatDelay <= 0.0f) }
{ }
mKeyRepeatDelay = JGUI_REPEAT_DELAY; }
doKey = true; else if ((PSP_CTRL_LEFT == key) || (PSP_CTRL_UP == key)) // || mEngine->GetAnalogY() < 64 || mEngine->GetAnalogX() < 64)
} {
} int n = mCurr;
n--;
return doKey; if (n<0)
} {
if ((mStyle&JGUI_STYLE_WRAPPING))
n = mCount-1;
void JGuiController::Update(float dt) else
{ n = 0;
for (int i=0;i<mCount;i++) }
if (mObjects[i]!=NULL)
mObjects[i]->Update(dt); if (n != mCurr && mObjects[mCurr] != NULL && mObjects[mCurr]->Leaving(PSP_CTRL_UP))
{
if (mEngine->GetButtonClick(mActionButton)) mCurr = n;
{ mObjects[mCurr]->Entering();
if (mObjects[mCurr] != NULL && mObjects[mCurr]->ButtonPressed()) }
{ }
if (mListener != NULL) else if ((PSP_CTRL_RIGHT == key) || (PSP_CTRL_DOWN == key)) // || mEngine->GetAnalogY()>192 || mEngine->GetAnalogX()>192)
{ {
mListener->ButtonPressed(mId, mObjects[mCurr]->GetId()); int n = mCurr;
return; n++;
} if (n>mCount-1)
} {
} if ((mStyle&JGUI_STYLE_WRAPPING))
n = 0;
if (mEngine->GetButtonState(PSP_CTRL_LEFT) || mEngine->GetButtonState(PSP_CTRL_UP) || mEngine->GetAnalogY()<64 || mEngine->GetAnalogX()<64) else
{ n = mCount-1;
if (KeyRepeated(PSP_CTRL_UP, dt)) }
{
int n = mCurr; if (n != mCurr && mObjects[mCurr] != NULL && mObjects[mCurr]->Leaving(PSP_CTRL_DOWN))
n--; {
if (n<0) mCurr = n;
{ mObjects[mCurr]->Entering();
if ((mStyle&JGUI_STYLE_WRAPPING)) }
n = mCount-1; }
else }
n = 0;
}
void JGuiController::Add(JGuiObject* ctrl)
if (n != mCurr && mObjects[mCurr] != NULL && mObjects[mCurr]->Leaving(PSP_CTRL_UP)) {
{ if (mCount<MAX_GUIOBJECT)
mCurr = n; {
mObjects[mCurr]->Entering(); mObjects[mCount++] = ctrl;
} }
} }
}
else if (mEngine->GetButtonState(PSP_CTRL_RIGHT) || mEngine->GetButtonState(PSP_CTRL_DOWN) || mEngine->GetAnalogY()>192 || mEngine->GetAnalogX()>192)
{ void JGuiController::Remove(int id)
if (KeyRepeated(PSP_CTRL_DOWN, dt)) {
{ for (int i=0;i<mCount;i++)
int n = mCurr; {
n++; if (mObjects[i] != NULL && mObjects[i]->GetId()==id)
if (n>mCount-1) {
{ delete mObjects[i];
if ((mStyle&JGUI_STYLE_WRAPPING)) for (int j=i;j<mCount-1;j++)
n = 0; {
else mObjects[j] = mObjects[j+1];
n = mCount-1; }
} mObjects[mCount-1] = NULL;
mCount--;
if (n != mCurr && mObjects[mCurr] != NULL && mObjects[mCurr]->Leaving(PSP_CTRL_DOWN)) if (mCurr == mCount)
{ mCurr = 0;
mCurr = n; return;
mObjects[mCurr]->Entering(); }
} }
} }
}
else
mLastKey = 0; void JGuiController::Remove(JGuiObject* ctrl)
{
} for (int i=0;i<mCount;i++)
{
if (mObjects[i] != NULL && mObjects[i]==ctrl)
void JGuiController::Add(JGuiObject* ctrl) {
{ delete mObjects[i];
if (mCount<MAX_GUIOBJECT) for (int j=i;j<mCount-1;j++)
{ {
mObjects[mCount++] = ctrl; mObjects[j] = mObjects[j+1];
} }
} mObjects[mCount-1] = NULL;
mCount--;
if (mCurr == mCount)
void JGuiController::Remove(int id) mCurr = 0;
{ return;
for (int i=0;i<mCount;i++) }
{ }
if (mObjects[i] != NULL && mObjects[i]->GetId()==id) }
{
delete mObjects[i];
for (int j=i;j<mCount-1;j++) void JGuiController::SetActionButton(u32 button) { mActionButton = button; }
{ void JGuiController::SetStyle(int style) { mStyle = style; }
mObjects[j] = mObjects[j+1]; void JGuiController::SetCursor(JSprite* cursor) { mCursor = cursor; }
} bool JGuiController::IsActive() { return mActive; }
mObjects[mCount-1] = NULL; void JGuiController::SetActive(bool flag) { mActive = flag; }
mCount--;
if (mCurr == mCount)
mCurr = 0;
return;
}
}
}
void JGuiController::Remove(JGuiObject* ctrl)
{
for (int i=0;i<mCount;i++)
{
if (mObjects[i] != NULL && mObjects[i]==ctrl)
{
delete mObjects[i];
for (int j=i;j<mCount-1;j++)
{
mObjects[j] = mObjects[j+1];
}
mObjects[mCount-1] = NULL;
mCount--;
if (mCurr == mCount)
mCurr = 0;
return;
}
}
}
void JGuiController::SetActionButton(u32 button) { mActionButton = button; }
void JGuiController::SetStyle(int style) { mStyle = style; }
void JGuiController::SetCursor(JSprite* cursor) { mCursor = cursor; }
bool JGuiController::IsActive() { return mActive; }
void JGuiController::SetActive(bool flag) { mActive = flag; }