mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-11-29 09:30:39 +02:00
Query animation status instead of storing it
This makes it a lot easier to support more than one possible animation in the same layer.
This commit is contained in:
parent
f820bf8d6e
commit
90afa096e7
@ -22,7 +22,6 @@ struct ContextMenu::MenuOption {
|
|||||||
ContextMenu::ContextMenu(GMenu2X &gmenu2x, Menu &menu)
|
ContextMenu::ContextMenu(GMenu2X &gmenu2x, Menu &menu)
|
||||||
: gmenu2x(gmenu2x)
|
: gmenu2x(gmenu2x)
|
||||||
, menu(menu)
|
, menu(menu)
|
||||||
, fadeAlpha(0)
|
|
||||||
, selected(0)
|
, selected(0)
|
||||||
{
|
{
|
||||||
Translator &tr = gmenu2x.tr;
|
Translator &tr = gmenu2x.tr;
|
||||||
@ -95,24 +94,20 @@ ContextMenu::ContextMenu(GMenu2X &gmenu2x, Menu &menu)
|
|||||||
|
|
||||||
// Init background fade animation.
|
// Init background fade animation.
|
||||||
tickStart = SDL_GetTicks();
|
tickStart = SDL_GetTicks();
|
||||||
startAnimating();
|
fadeAlpha = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContextMenu::runAnimations()
|
bool ContextMenu::runAnimations()
|
||||||
{
|
{
|
||||||
if (fadeAlpha < 200) {
|
if (fadeAlpha < 200) {
|
||||||
const long tickNow = SDL_GetTicks();
|
const long tickNow = SDL_GetTicks();
|
||||||
fadeAlpha = intTransition(0, 200, tickStart, 500, tickNow);
|
fadeAlpha = intTransition(0, 200, tickStart, 500, tickNow);
|
||||||
if (fadeAlpha == 200) {
|
|
||||||
stopAnimating();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return fadeAlpha < 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContextMenu::paint(Surface &s)
|
void ContextMenu::paint(Surface &s)
|
||||||
{
|
{
|
||||||
runAnimations();
|
|
||||||
|
|
||||||
Font *font = gmenu2x.font;
|
Font *font = gmenu2x.font;
|
||||||
|
|
||||||
// Darken background.
|
// Darken background.
|
||||||
|
@ -21,6 +21,7 @@ public:
|
|||||||
ContextMenu(GMenu2X &gmenu2x, Menu &menu);
|
ContextMenu(GMenu2X &gmenu2x, Menu &menu);
|
||||||
|
|
||||||
// Layer implementation:
|
// Layer implementation:
|
||||||
|
virtual bool runAnimations();
|
||||||
virtual void paint(Surface &s);
|
virtual void paint(Surface &s);
|
||||||
virtual bool handleButtonPress(InputManager::Button button);
|
virtual bool handleButtonPress(InputManager::Button button);
|
||||||
virtual bool handleTouchscreen(Touchscreen &ts);
|
virtual bool handleTouchscreen(Touchscreen &ts);
|
||||||
@ -28,8 +29,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
struct MenuOption;
|
struct MenuOption;
|
||||||
|
|
||||||
void runAnimations();
|
|
||||||
|
|
||||||
GMenu2X &gmenu2x;
|
GMenu2X &gmenu2x;
|
||||||
Menu &menu;
|
Menu &menu;
|
||||||
std::vector<std::shared_ptr<MenuOption>> options;
|
std::vector<std::shared_ptr<MenuOption>> options;
|
||||||
|
@ -591,24 +591,21 @@ void GMenu2X::main() {
|
|||||||
|
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
while (!quit) {
|
while (!quit) {
|
||||||
// Check whether any layers are animating and remove dismissed layers
|
// Remove dismissed layers from the stack.
|
||||||
// from the stack.
|
|
||||||
bool animating = false;
|
|
||||||
for (auto it = layers.begin(); it != layers.end(); ) {
|
for (auto it = layers.begin(); it != layers.end(); ) {
|
||||||
auto layer = *it;
|
if ((*it)->getStatus() == Layer::Status::DISMISSED) {
|
||||||
switch (layer->getStatus()) {
|
it = layers.erase(it);
|
||||||
case Layer::Status::DISMISSED:
|
} else {
|
||||||
it = layers.erase(it);
|
++it;
|
||||||
break;
|
|
||||||
case Layer::Status::ANIMATING:
|
|
||||||
animating = true;
|
|
||||||
// fall through
|
|
||||||
case Layer::Status::PASSIVE:
|
|
||||||
++it;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run animations.
|
||||||
|
bool animating = false;
|
||||||
|
for (auto layer : layers) {
|
||||||
|
animating |= layer->runAnimations();
|
||||||
|
}
|
||||||
|
|
||||||
// Paint layers.
|
// Paint layers.
|
||||||
for (auto layer : layers) {
|
for (auto layer : layers) {
|
||||||
layer->paint(*s);
|
layer->paint(*s);
|
||||||
|
24
src/layer.h
24
src/layer.h
@ -16,10 +16,16 @@ class Touchscreen;
|
|||||||
*/
|
*/
|
||||||
class Layer {
|
class Layer {
|
||||||
public:
|
public:
|
||||||
enum class Status { PASSIVE, ANIMATING, DISMISSED };
|
enum class Status { DISMISSED, NORMAL };
|
||||||
|
|
||||||
virtual ~Layer() {}
|
virtual ~Layer() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform one frame worth of animation.
|
||||||
|
* Returns true iff there are any animations in progress.
|
||||||
|
*/
|
||||||
|
virtual bool runAnimations() { return false; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Paints this layer on the given surface.
|
* Paints this layer on the given surface.
|
||||||
*/
|
*/
|
||||||
@ -50,22 +56,8 @@ protected:
|
|||||||
status = Status::DISMISSED;
|
status = Status::DISMISSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Request that this layer be repainted every frame.
|
|
||||||
*/
|
|
||||||
void startAnimating() {
|
|
||||||
if (status == Status::PASSIVE) status = Status::ANIMATING;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request that this layer be repainted only after an event.
|
|
||||||
*/
|
|
||||||
void stopAnimating() {
|
|
||||||
if (status == Status::ANIMATING) status = Status::PASSIVE;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Status status = Status::PASSIVE;
|
Status status = Status::NORMAL;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LAYER_H
|
#endif // LAYER_H
|
||||||
|
10
src/menu.cpp
10
src/menu.cpp
@ -171,18 +171,14 @@ void Menu::calcSectionRange(int &leftSection, int &rightSection) {
|
|||||||
rightSection - numSections + 1);
|
rightSection - numSections + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::runAnimations() {
|
bool Menu::runAnimations() {
|
||||||
if (sectionAnimation.isRunning()) {
|
if (sectionAnimation.isRunning()) {
|
||||||
sectionAnimation.step();
|
sectionAnimation.step();
|
||||||
if (!sectionAnimation.isRunning()) {
|
|
||||||
stopAnimating();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return sectionAnimation.isRunning();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::paint(Surface &s) {
|
void Menu::paint(Surface &s) {
|
||||||
runAnimations();
|
|
||||||
|
|
||||||
const uint width = s.width(), height = s.height();
|
const uint width = s.width(), height = s.height();
|
||||||
Font &font = *gmenu2x->font;
|
Font &font = *gmenu2x->font;
|
||||||
SurfaceCollection &sc = gmenu2x->sc;
|
SurfaceCollection &sc = gmenu2x->sc;
|
||||||
@ -371,13 +367,11 @@ vector<Link*> *Menu::sectionLinks(int i) {
|
|||||||
|
|
||||||
void Menu::decSectionIndex() {
|
void Menu::decSectionIndex() {
|
||||||
sectionAnimation.adjust(-1 << 16);
|
sectionAnimation.adjust(-1 << 16);
|
||||||
startAnimating();
|
|
||||||
setSectionIndex(iSection - 1);
|
setSectionIndex(iSection - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::incSectionIndex() {
|
void Menu::incSectionIndex() {
|
||||||
sectionAnimation.adjust(1 << 16);
|
sectionAnimation.adjust(1 << 16);
|
||||||
startAnimating();
|
|
||||||
setSectionIndex(iSection + 1);
|
setSectionIndex(iSection + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,8 +65,6 @@ private:
|
|||||||
|
|
||||||
Animation sectionAnimation;
|
Animation sectionAnimation;
|
||||||
|
|
||||||
void runAnimations();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine which section headers are visible.
|
* Determine which section headers are visible.
|
||||||
* The output values are relative to the middle section at 0.
|
* The output values are relative to the middle section at 0.
|
||||||
@ -127,6 +125,7 @@ public:
|
|||||||
void skinUpdated();
|
void skinUpdated();
|
||||||
|
|
||||||
// Layer implementation:
|
// Layer implementation:
|
||||||
|
virtual bool runAnimations();
|
||||||
virtual void paint(Surface &s);
|
virtual void paint(Surface &s);
|
||||||
virtual bool handleButtonPress(InputManager::Button button);
|
virtual bool handleButtonPress(InputManager::Button button);
|
||||||
virtual bool handleTouchscreen(Touchscreen &ts);
|
virtual bool handleTouchscreen(Touchscreen &ts);
|
||||||
|
Loading…
Reference in New Issue
Block a user