mirror of
https://github.com/DragonMinded/jubeatmenu.git
synced 2024-11-27 16:00:51 +01:00
Clean up a few things.
Allow multiple inputs to be queued for scrolling. Fix freezing animations if clock goes backwards. Refactor display Tick() to be less messy.
This commit is contained in:
parent
935aba2000
commit
c6fa4d43b1
@ -18,7 +18,7 @@ Animation::~Animation()
|
||||
// Empty
|
||||
}
|
||||
|
||||
void Animation::Animate(int animationOffset, int animationDistance, double pixelsPerSecond)
|
||||
void Animation::Animate(int animationOffset, int animationDistance, double pixelsPerSecond, bool allowDeceleration)
|
||||
{
|
||||
if (isAnimating) { return; }
|
||||
|
||||
@ -26,8 +26,9 @@ void Animation::Animate(int animationOffset, int animationDistance, double pixel
|
||||
location = 0.0;
|
||||
offset = animationOffset;
|
||||
speed = pixelsPerSecond / (1000.0);
|
||||
originalSpeed = speed;
|
||||
speedChangeLocation = (double)abs(animationDistance) * SPEED_REDUCE_LOCATION;
|
||||
speedChanges = 0;
|
||||
speedChanges = allowDeceleration ? 0 : MAX_SPEED_CHANGES;
|
||||
isAnimating = true;
|
||||
lastMilliseconds = CurrentMilliseconds();
|
||||
}
|
||||
@ -37,6 +38,14 @@ bool Animation::IsAnimating()
|
||||
return isAnimating;
|
||||
}
|
||||
|
||||
void Animation::CancelDeceleration()
|
||||
{
|
||||
if (!isAnimating) { return; }
|
||||
|
||||
speed = originalSpeed;
|
||||
speedChanges = MAX_SPEED_CHANGES;
|
||||
}
|
||||
|
||||
int Animation::Position()
|
||||
{
|
||||
int position = (unsigned int)location;
|
||||
@ -54,8 +63,15 @@ LONG Animation::CurrentMilliseconds()
|
||||
void Animation::Tick()
|
||||
{
|
||||
LONG currentMilliseconds = CurrentMilliseconds();
|
||||
if (currentMilliseconds - lastMilliseconds > 0)
|
||||
if (currentMilliseconds - lastMilliseconds != 0)
|
||||
{
|
||||
if (currentMilliseconds - lastMilliseconds < 0)
|
||||
{
|
||||
// We went backwards?
|
||||
lastMilliseconds = currentMilliseconds;
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate how much we should have moved
|
||||
location += speed * (double)(currentMilliseconds - lastMilliseconds);
|
||||
if (location >= abs(distance))
|
||||
|
@ -10,7 +10,8 @@ public:
|
||||
Animation();
|
||||
~Animation();
|
||||
|
||||
void Animate(int animationOffset, int animationDistance, double pixelsPerSecond);
|
||||
void Animate(int animationOffset, int animationDistance, double pixelsPerSecond, bool allowDeceleration);
|
||||
void CancelDeceleration();
|
||||
bool IsAnimating();
|
||||
void Tick();
|
||||
int Position();
|
||||
@ -24,5 +25,6 @@ private:
|
||||
int speedChanges;
|
||||
int offset;
|
||||
double speed;
|
||||
double originalSpeed;
|
||||
LONG lastMilliseconds;
|
||||
};
|
||||
|
@ -368,6 +368,8 @@ Display::Display(HINSTANCE hInstance, IO *ioInst, Menu *mInst)
|
||||
selected = 0;
|
||||
newPage = -1;
|
||||
lastLocation = 0;
|
||||
leftPresses = 0;
|
||||
rightPresses = 0;
|
||||
|
||||
// Register the callback
|
||||
WNDCLASS wc = { };
|
||||
@ -405,37 +407,74 @@ Display::~Display()
|
||||
delete globalAnimation;
|
||||
}
|
||||
|
||||
void Display::InvalidateOnUpdates()
|
||||
{
|
||||
unsigned int buttonsHeld = io->ButtonsHeld();
|
||||
bool update = false;
|
||||
|
||||
if (globalButtonsHeld != buttonsHeld)
|
||||
{
|
||||
globalButtonsHeld = buttonsHeld;
|
||||
update = true;
|
||||
}
|
||||
if (globalSelected != selected)
|
||||
{
|
||||
globalSelected = selected;
|
||||
update = true;
|
||||
}
|
||||
if (lastLocation != globalAnimation->Position())
|
||||
{
|
||||
lastLocation = globalAnimation->Position();
|
||||
update = true;
|
||||
}
|
||||
if (globalPage != page)
|
||||
{
|
||||
globalPage = page;
|
||||
update = true;
|
||||
}
|
||||
if (globalSeconds != menu->SecondsLeft())
|
||||
{
|
||||
globalSeconds = menu->SecondsLeft();
|
||||
update = true;
|
||||
}
|
||||
|
||||
if (update)
|
||||
{
|
||||
InvalidateRect(hwnd, NULL, FALSE);
|
||||
UpdateWindow(hwnd);
|
||||
}
|
||||
}
|
||||
|
||||
void Display::Tick(void)
|
||||
{
|
||||
/* Don't handle input while animating */
|
||||
unsigned int buttonsHeld = io->ButtonsHeld();
|
||||
/* Make sure animations happen */
|
||||
globalAnimation->Tick();
|
||||
|
||||
/* Make sure we respect multiple inputs while animating */
|
||||
unsigned int max_pages = ((menu->NumberOfEntries() - GAMES_PER_PAGE) + 2) / 3;
|
||||
if (io->ButtonPressed(BUTTON_13))
|
||||
{
|
||||
leftPresses ++;
|
||||
if (page < (max_pages - 1))
|
||||
{
|
||||
/* We're not at the last page, so quick-scroll the current animation */
|
||||
globalAnimation->CancelDeceleration();
|
||||
}
|
||||
}
|
||||
if (io->ButtonPressed(BUTTON_14))
|
||||
{
|
||||
rightPresses ++;
|
||||
if (page > 0)
|
||||
{
|
||||
/* We're not at the last page, so quick-scroll the current animation */
|
||||
globalAnimation->CancelDeceleration();
|
||||
}
|
||||
}
|
||||
|
||||
/* Don't handle game select input while animating */
|
||||
if (globalAnimation->IsAnimating())
|
||||
{
|
||||
bool update = false;
|
||||
|
||||
if (globalButtonsHeld != buttonsHeld)
|
||||
{
|
||||
globalButtonsHeld = buttonsHeld;
|
||||
update = true;
|
||||
}
|
||||
if (lastLocation != globalAnimation->Position())
|
||||
{
|
||||
lastLocation = globalAnimation->Position();
|
||||
update = true;
|
||||
}
|
||||
if (globalSeconds != menu->SecondsLeft())
|
||||
{
|
||||
globalSeconds = menu->SecondsLeft();
|
||||
update = true;
|
||||
}
|
||||
|
||||
if (update)
|
||||
{
|
||||
InvalidateRect(hwnd, NULL, FALSE);
|
||||
UpdateWindow(hwnd);
|
||||
}
|
||||
|
||||
InvalidateOnUpdates();
|
||||
MessageHandler();
|
||||
return;
|
||||
}
|
||||
@ -444,12 +483,9 @@ void Display::Tick(void)
|
||||
if (newPage >= 0)
|
||||
{
|
||||
page = newPage;
|
||||
globalPage = page;
|
||||
newPage = -1;
|
||||
globalButtonsHeld = buttonsHeld;
|
||||
InvalidateRect(hwnd, NULL, FALSE);
|
||||
UpdateWindow(hwnd);
|
||||
|
||||
InvalidateOnUpdates();
|
||||
MessageHandler();
|
||||
return;
|
||||
}
|
||||
@ -460,25 +496,46 @@ void Display::Tick(void)
|
||||
/* Figure out actions */
|
||||
if (menu->NumberOfEntries() > GAMES_PER_PAGE)
|
||||
{
|
||||
unsigned int max_pages = ((menu->NumberOfEntries() - GAMES_PER_PAGE) + 2) / 3;
|
||||
|
||||
/* Activate page left/right buttons */
|
||||
if (io->ButtonPressed(BUTTON_13))
|
||||
if (leftPresses > 0)
|
||||
{
|
||||
/* Scroll left */
|
||||
if (page < (max_pages - 1))
|
||||
{
|
||||
/* We have more pages we can animate scrolling to */
|
||||
leftPresses --;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Next page is the end, we should decelerate */
|
||||
leftPresses = 0;
|
||||
}
|
||||
|
||||
if (page < max_pages)
|
||||
{
|
||||
globalAnimation->Animate(0, -BUTTON_HORIZONTAL_STRIDE, ANIMATION_SPEED);
|
||||
globalAnimation->Animate(0, -BUTTON_HORIZONTAL_STRIDE, ANIMATION_SPEED, leftPresses == 0);
|
||||
lastLocation = globalAnimation->Position();
|
||||
newPage = page + 1;
|
||||
moved = true;
|
||||
}
|
||||
} else if (io->ButtonPressed(BUTTON_14))
|
||||
}
|
||||
else if (rightPresses > 0)
|
||||
{
|
||||
/* Scroll right */
|
||||
if (page > 1)
|
||||
{
|
||||
/* We have more pages we can animate scrolling to */
|
||||
rightPresses --;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Next page is the end, we should decelerate */
|
||||
rightPresses = 0;
|
||||
}
|
||||
|
||||
if (page > 0)
|
||||
{
|
||||
globalAnimation->Animate(-BUTTON_HORIZONTAL_STRIDE, BUTTON_HORIZONTAL_STRIDE, ANIMATION_SPEED);
|
||||
globalAnimation->Animate(-BUTTON_HORIZONTAL_STRIDE, BUTTON_HORIZONTAL_STRIDE, ANIMATION_SPEED, rightPresses == 0);
|
||||
lastLocation = globalAnimation->Position();
|
||||
page--;
|
||||
newPage = page;
|
||||
@ -503,34 +560,7 @@ void Display::Tick(void)
|
||||
|
||||
/* Update the screen to show any button presses or selection
|
||||
changes. */
|
||||
bool update = false;
|
||||
if (globalButtonsHeld != buttonsHeld)
|
||||
{
|
||||
globalButtonsHeld = buttonsHeld;
|
||||
update = true;
|
||||
}
|
||||
if (globalSelected != selected)
|
||||
{
|
||||
globalSelected = selected;
|
||||
update = true;
|
||||
}
|
||||
if (globalPage != page)
|
||||
{
|
||||
globalPage = page;
|
||||
update = true;
|
||||
}
|
||||
if (globalSeconds != menu->SecondsLeft())
|
||||
{
|
||||
globalSeconds = menu->SecondsLeft();
|
||||
update = true;
|
||||
}
|
||||
|
||||
if (update)
|
||||
{
|
||||
InvalidateRect(hwnd, NULL, FALSE);
|
||||
UpdateWindow(hwnd);
|
||||
}
|
||||
|
||||
InvalidateOnUpdates();
|
||||
MessageHandler();
|
||||
}
|
||||
|
||||
|
@ -47,6 +47,8 @@ public:
|
||||
unsigned int GetSelectedItem();
|
||||
|
||||
private:
|
||||
void InvalidateOnUpdates();
|
||||
|
||||
HINSTANCE inst;
|
||||
HWND hwnd;
|
||||
|
||||
@ -57,4 +59,6 @@ private:
|
||||
unsigned int page;
|
||||
unsigned int selected;
|
||||
int lastLocation;
|
||||
unsigned int leftPresses;
|
||||
unsigned int rightPresses;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user