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)
|
||||
: 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.
|
||||
|
@ -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<std::shared_ptr<MenuOption>> options;
|
||||
|
@ -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);
|
||||
|
24
src/layer.h
24
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
|
||||
|
10
src/menu.cpp
10
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<Link*> *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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user