diff --git a/src/contextmenu.cpp b/src/contextmenu.cpp index ca35859..52735f0 100644 --- a/src/contextmenu.cpp +++ b/src/contextmenu.cpp @@ -22,7 +22,6 @@ struct ContextMenu::MenuOption { ContextMenu::ContextMenu(GMenu2X &gmenu2x, Menu &menu) : gmenu2x(gmenu2x) , menu(menu) - , fadeAlpha(0) , selected(0) { Translator &tr = gmenu2x.tr; @@ -95,24 +94,20 @@ ContextMenu::ContextMenu(GMenu2X &gmenu2x, Menu &menu) // Init background fade animation. tickStart = SDL_GetTicks(); - startAnimating(); + fadeAlpha = 0; } -void ContextMenu::runAnimations() +bool ContextMenu::runAnimations() { if (fadeAlpha < 200) { const long tickNow = SDL_GetTicks(); fadeAlpha = intTransition(0, 200, tickStart, 500, tickNow); - if (fadeAlpha == 200) { - stopAnimating(); - } } + return fadeAlpha < 200; } void ContextMenu::paint(Surface &s) { - runAnimations(); - Font *font = gmenu2x.font; // Darken background. diff --git a/src/contextmenu.h b/src/contextmenu.h index bb2b2be..7b6a78d 100644 --- a/src/contextmenu.h +++ b/src/contextmenu.h @@ -21,6 +21,7 @@ public: ContextMenu(GMenu2X &gmenu2x, Menu &menu); // Layer implementation: + virtual bool runAnimations(); virtual void paint(Surface &s); virtual bool handleButtonPress(InputManager::Button button); virtual bool handleTouchscreen(Touchscreen &ts); @@ -28,8 +29,6 @@ public: private: struct MenuOption; - void runAnimations(); - GMenu2X &gmenu2x; Menu &menu; std::vector> options; diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 765fa14..a0b1406 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -591,24 +591,21 @@ void GMenu2X::main() { bool quit = false; while (!quit) { - // Check whether any layers are animating and remove dismissed layers - // from the stack. - bool animating = false; + // Remove dismissed layers from the stack. for (auto it = layers.begin(); it != layers.end(); ) { - auto layer = *it; - switch (layer->getStatus()) { - case Layer::Status::DISMISSED: - it = layers.erase(it); - break; - case Layer::Status::ANIMATING: - animating = true; - // fall through - case Layer::Status::PASSIVE: - ++it; - break; + if ((*it)->getStatus() == Layer::Status::DISMISSED) { + it = layers.erase(it); + } else { + ++it; } } + // Run animations. + bool animating = false; + for (auto layer : layers) { + animating |= layer->runAnimations(); + } + // Paint layers. for (auto layer : layers) { layer->paint(*s); diff --git a/src/layer.h b/src/layer.h index fb38160..baf8d9a 100644 --- a/src/layer.h +++ b/src/layer.h @@ -16,10 +16,16 @@ class Touchscreen; */ class Layer { public: - enum class Status { PASSIVE, ANIMATING, DISMISSED }; + enum class Status { DISMISSED, NORMAL }; 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. */ @@ -50,22 +56,8 @@ protected: 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: - Status status = Status::PASSIVE; + Status status = Status::NORMAL; }; #endif // LAYER_H diff --git a/src/menu.cpp b/src/menu.cpp index 8413eee..4ed63e5 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -171,18 +171,14 @@ void Menu::calcSectionRange(int &leftSection, int &rightSection) { rightSection - numSections + 1); } -void Menu::runAnimations() { +bool Menu::runAnimations() { if (sectionAnimation.isRunning()) { sectionAnimation.step(); - if (!sectionAnimation.isRunning()) { - stopAnimating(); - } } + return sectionAnimation.isRunning(); } void Menu::paint(Surface &s) { - runAnimations(); - const uint width = s.width(), height = s.height(); Font &font = *gmenu2x->font; SurfaceCollection &sc = gmenu2x->sc; @@ -371,13 +367,11 @@ vector *Menu::sectionLinks(int i) { void Menu::decSectionIndex() { sectionAnimation.adjust(-1 << 16); - startAnimating(); setSectionIndex(iSection - 1); } void Menu::incSectionIndex() { sectionAnimation.adjust(1 << 16); - startAnimating(); setSectionIndex(iSection + 1); } diff --git a/src/menu.h b/src/menu.h index 32f82dc..c4e5c09 100644 --- a/src/menu.h +++ b/src/menu.h @@ -65,8 +65,6 @@ private: Animation sectionAnimation; - void runAnimations(); - /** * Determine which section headers are visible. * The output values are relative to the middle section at 0. @@ -127,6 +125,7 @@ public: void skinUpdated(); // Layer implementation: + virtual bool runAnimations(); virtual void paint(Surface &s); virtual bool handleButtonPress(InputManager::Button button); virtual bool handleTouchscreen(Touchscreen &ts);