Skip to content

Commit

Permalink
Merge pull request hrydgard#442 from Xele02/master
Browse files Browse the repository at this point in the history
Add fade in and out in dialogs
  • Loading branch information
hrydgard committed Jan 18, 2013
2 parents adc8e80 + 1f4f5ea commit faf4b76
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 69 deletions.
45 changes: 43 additions & 2 deletions Core/Dialog/PSPDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "../Util/PPGeDraw.h"
#include "PSPDialog.h"

#define FADE_TIME 0.5

PSPDialog::PSPDialog() : status(SCE_UTILITY_STATUS_SHUTDOWN)
, lastButtons(0)
, buttons(0)
Expand All @@ -41,7 +43,7 @@ PSPDialog::DialogStatus PSPDialog::GetStatus()
void PSPDialog::StartDraw()
{
PPGeBegin();
PPGeDraw4Patch(I_BUTTON, 0, 0, 480, 272, 0xcFFFFFFF);
PPGeDraw4Patch(I_BUTTON, 0, 0, 480, 272, CalcFadedColor(0xcFFFFFFF));
}
void PSPDialog::EndDraw()
{
Expand All @@ -50,7 +52,7 @@ void PSPDialog::EndDraw()

void PSPDialog::DisplayMessage(std::string text)
{
PPGeDrawText(text.c_str(), 40, 30, PPGE_ALIGN_LEFT, 0.5f, 0xFFFFFFFF);
PPGeDrawText(text.c_str(), 40, 30, PPGE_ALIGN_LEFT, 0.5f, CalcFadedColor(0xFFFFFFFF));
}

int PSPDialog::Shutdown()
Expand All @@ -59,6 +61,44 @@ int PSPDialog::Shutdown()
return 0;
}

void PSPDialog::StartFade(bool fadeIn_)
{
isFading = true;
fadeTimer = 0;
fadeIn = fadeIn_;
}

void PSPDialog::UpdateFade()
{
if(isFading)
{
fadeTimer += 1.0f/30; // Probably need a more real value of delta time
if(fadeTimer < FADE_TIME)
{
if(fadeIn)
fadeValue = fadeTimer / FADE_TIME * 255;
else
fadeValue = 255 - fadeTimer / FADE_TIME * 255;
}
else
{
fadeValue = (fadeIn?255:0);
isFading = false;
if(!fadeIn)
{
status = SCE_UTILITY_STATUS_FINISHED;
}
}
}
}

u32 PSPDialog::CalcFadedColor(u32 inColor)
{
u32 alpha = inColor >> 24;
alpha = alpha * fadeValue / 255;
return (inColor & 0x00FFFFFF) | (alpha << 24);
}

int PSPDialog::Update()
{
return 0;
Expand All @@ -74,6 +114,7 @@ void PSPDialog::DoState(PointerWrap &p)

bool PSPDialog::IsButtonPressed(int checkButton)
{
if(isFading) return false;
return (!(lastButtons & checkButton)) && (buttons & checkButton);
}

Expand Down
10 changes: 10 additions & 0 deletions Core/Dialog/PSPDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,18 @@ class PSPDialog
protected:
bool IsButtonPressed(int checkButton);
void DisplayMessage(std::string text);

void StartFade(bool fadeIn_);
void UpdateFade();
u32 CalcFadedColor(u32 inColor);

DialogStatus status;

unsigned int lastButtons;
unsigned int buttons;

float fadeTimer;
bool isFading;
bool fadeIn;
u32 fadeValue;
};
30 changes: 12 additions & 18 deletions Core/Dialog/PSPMsgDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,21 @@ int PSPMsgDialog::Init(unsigned int paramAddr)
status = SCE_UTILITY_STATUS_INITIALIZE;

lastButtons = __CtrlPeekButtons();
StartFade(true);
return 0;
}

void PSPMsgDialog::DisplayBack()
{
PPGeDrawImage(cancelButtonImg, 290, 220, 20, 20, 0, 0xFFFFFFFF);
PPGeDrawText("Back", 320, 220, PPGE_ALIGN_LEFT, 0.5f, 0xFFFFFFFF);
PPGeDrawImage(cancelButtonImg, 290, 220, 20, 20, 0, CalcFadedColor(0xFFFFFFFF));
PPGeDrawText("Back", 320, 220, PPGE_ALIGN_LEFT, 0.5f, CalcFadedColor(0xFFFFFFFF));
}

void PSPMsgDialog::DisplayYesNo()
{

PPGeDrawText("Yes", 200, 150, PPGE_ALIGN_LEFT, 0.5f, (yesnoChoice == 1?0xFF0000FF:0xFFFFFFFF));
PPGeDrawText("No", 320, 150, PPGE_ALIGN_LEFT, 0.5f, (yesnoChoice == 0?0xFF0000FF:0xFFFFFFFF));
PPGeDrawText("Yes", 200, 150, PPGE_ALIGN_LEFT, 0.5f, CalcFadedColor(yesnoChoice == 1?0xFF0000FF:0xFFFFFFFF));
PPGeDrawText("No", 320, 150, PPGE_ALIGN_LEFT, 0.5f, CalcFadedColor(yesnoChoice == 0?0xFF0000FF:0xFFFFFFFF));

if (IsButtonPressed(CTRL_LEFT) && yesnoChoice == 0)
{
Expand All @@ -148,22 +149,17 @@ void PSPMsgDialog::DisplayYesNo()

void PSPMsgDialog::DisplayOk()
{
PPGeDrawText("OK", 250, 150, PPGE_ALIGN_LEFT, 0.5f, 0xFF0000FF);
PPGeDrawText("OK", 250, 150, PPGE_ALIGN_LEFT, 0.5f, CalcFadedColor(0xFF0000FF));
}

void PSPMsgDialog::DisplayEnter()
{
PPGeDrawImage(okButtonImg, 200, 220, 20, 20, 0, 0xFFFFFFFF);
PPGeDrawText("Enter", 230, 220, PPGE_ALIGN_LEFT, 0.5f, 0xFFFFFFFF);
PPGeDrawImage(okButtonImg, 200, 220, 20, 20, 0, CalcFadedColor(0xFFFFFFFF));
PPGeDrawText("Enter", 230, 220, PPGE_ALIGN_LEFT, 0.5f, CalcFadedColor(0xFFFFFFFF));
}

int PSPMsgDialog::Update()
{
switch (status) {
case SCE_UTILITY_STATUS_FINISHED:
status = SCE_UTILITY_STATUS_SHUTDOWN;
break;
}

if (status != SCE_UTILITY_STATUS_RUNNING)
{
Expand All @@ -176,6 +172,8 @@ int PSPMsgDialog::Update()
}
else
{
UpdateFade();

buttons = __CtrlPeekButtons();

okButtonImg = I_CIRCLE;
Expand Down Expand Up @@ -205,30 +203,26 @@ int PSPMsgDialog::Update()
if(flag & DS_CANCELBUTTON)
DisplayBack();


// TODO : Dialogs should take control over input and not send them to the game while displaying
if (IsButtonPressed(cancelButtonFlag) && (flag & DS_CANCELBUTTON))
{
status = SCE_UTILITY_STATUS_FINISHED;
if(messageDialog.common.size == SCE_UTILITY_MSGDIALOG_SIZE_V3 ||
((messageDialog.common.size == SCE_UTILITY_MSGDIALOG_SIZE_V2) && (flag & DS_YESNO)))
messageDialog.buttonPressed = 3;
else
messageDialog.buttonPressed = 0;
StartFade(false);
}
else if(IsButtonPressed(okButtonFlag) && (flag & DS_VALIDBUTTON))
{
status = SCE_UTILITY_STATUS_FINISHED;
if(yesnoChoice == 0)
{
status = SCE_UTILITY_STATUS_FINISHED;
messageDialog.buttonPressed = 2;
}
else
{
status = SCE_UTILITY_STATUS_FINISHED;
messageDialog.buttonPressed = 1;
}
StartFade(false);
}


Expand Down
32 changes: 17 additions & 15 deletions Core/Dialog/PSPOskDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ int PSPOskDialog::Init(u32 oskPtr)
// Eat any keys pressed before the dialog inited.
__CtrlReadLatch();

StartFade(true);
return 0;
}

Expand All @@ -109,16 +110,16 @@ void PSPOskDialog::RenderKeyboard()
const float keyboardLeftSide = (480.0f - (23.0f * KEYSPERROW)) / 2.0f;
float previewLeftSide = (480.0f - (15.0f * limit)) / 2.0f;

PPGeDrawText(oskDesc.c_str(), 480/2, 20, PPGE_ALIGN_CENTER, 0.5f, 0xFFFFFFFF);
PPGeDrawText(oskDesc.c_str(), 480/2, 20, PPGE_ALIGN_CENTER, 0.5f, CalcFadedColor(0xFFFFFFFF));
for (int i = 0; i < limit; ++i)
{
u32 color = 0xFFFFFFFF;
u32 color = CalcFadedColor(0xFFFFFFFF);
if (i < (int) inputChars.size())
temp[0] = inputChars[i];
else if (i == inputChars.size())
{
temp[0] = oskKeys[selectedRow][selectedExtra];
color = 0xFF3060FF;
color = CalcFadedColor(0xFF3060FF);
}
else
temp[0] = '_';
Expand All @@ -129,15 +130,15 @@ void PSPOskDialog::RenderKeyboard()
{
for (int col = 0; col < KEYSPERROW; ++col)
{
u32 color = 0xFFFFFFFF;
u32 color = CalcFadedColor(0xFFFFFFFF);
if (selectedRow == row && col == selectedExtra)
color = 0xFF7f7f7f;
color = CalcFadedColor(0xFF7f7f7f);

temp[0] = oskKeys[row][col];
PPGeDrawText(temp, keyboardLeftSide + (25.0f * col), 70.0f + (25.0f * row), 0, 0.6f, color);

if (selectedRow == row && col == selectedExtra)
PPGeDrawText("_", keyboardLeftSide + (25.0f * col), 70.0f + (25.0f * row), 0, 0.6f, 0xFFFFFFFF);
PPGeDrawText("_", keyboardLeftSide + (25.0f * col), 70.0f + (25.0f * row), 0, 0.6f, CalcFadedColor(0xFFFFFFFF));
}
}

Expand All @@ -160,17 +161,19 @@ int PSPOskDialog::Update()
}
else if (status == SCE_UTILITY_STATUS_RUNNING)
{
UpdateFade();

StartDraw();
RenderKeyboard();
PPGeDrawImage(I_CROSS, 100, 220, 20, 20, 0, 0xFFFFFFFF);
PPGeDrawText("Select", 130, 220, PPGE_ALIGN_LEFT, 0.5f, 0xFFFFFFFF);
PPGeDrawImage(I_CROSS, 100, 220, 20, 20, 0, CalcFadedColor(0xFFFFFFFF));
PPGeDrawText("Select", 130, 220, PPGE_ALIGN_LEFT, 0.5f, CalcFadedColor(0xFFFFFFFF));

PPGeDrawImage(I_CIRCLE, 200, 220, 20, 20, 0, 0xFFFFFFFF);
PPGeDrawText("Delete", 230, 220, PPGE_ALIGN_LEFT, 0.5f, 0xFFFFFFFF);
PPGeDrawImage(I_CIRCLE, 200, 220, 20, 20, 0, CalcFadedColor(0xFFFFFFFF));
PPGeDrawText("Delete", 230, 220, PPGE_ALIGN_LEFT, 0.5f, CalcFadedColor(0xFFFFFFFF));

PPGeDrawImage(I_BUTTON, 290, 220, 50, 20, 0, 0xFFFFFFFF);
PPGeDrawText("Start", 305, 220, PPGE_ALIGN_LEFT, 0.5f, 0xFFFFFFFF);
PPGeDrawText("Finish", 350, 220, PPGE_ALIGN_LEFT, 0.5f, 0xFFFFFFFF);
PPGeDrawImage(I_BUTTON, 290, 220, 50, 20, 0, CalcFadedColor(0xFFFFFFFF));
PPGeDrawText("Start", 305, 220, PPGE_ALIGN_LEFT, 0.5f, CalcFadedColor(0xFFFFFFFF));
PPGeDrawText("Finish", 350, 220, PPGE_ALIGN_LEFT, 0.5f, CalcFadedColor(0xFFFFFFFF));

if (IsButtonPressed(CTRL_UP))
{
Expand All @@ -195,7 +198,6 @@ int PSPOskDialog::Update()

selectedChar = (selectedChar + NUMBEROFVALIDCHARS) % NUMBEROFVALIDCHARS;

// TODO : Dialogs should take control over input and not send them to the game while displaying
if (IsButtonPressed(CTRL_CROSS))
{
if ((int) inputChars.size() < limit)
Expand All @@ -208,7 +210,7 @@ int PSPOskDialog::Update()
}
else if (IsButtonPressed(CTRL_START))
{
status = SCE_UTILITY_STATUS_FINISHED;
StartFade(false);
}
EndDraw();
}
Expand Down
Loading

0 comments on commit faf4b76

Please sign in to comment.