1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-07-04 20:52:56 +03:00

Allow layers to play animations

This commit is contained in:
Maarten ter Huurne 2013-08-11 00:05:18 +02:00
parent 71f4391cda
commit f414ce4685
4 changed files with 60 additions and 20 deletions

View File

@ -596,6 +596,24 @@ void GMenu2X::main() {
bool quit = false; bool quit = false;
while (!quit) { while (!quit) {
// Check whether any layers are animating and remove dismissed layers
// from the stack.
bool animating = false;
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;
}
}
// Paint layers. // Paint layers.
for (auto layer : layers) { for (auto layer : layers) {
layer->paint(*s); layer->paint(*s);
@ -613,20 +631,19 @@ void GMenu2X::main() {
} }
// Handle other input events. // Handle other input events.
InputManager::Button button = input.waitForPressedButton(); InputManager::ButtonEvent event;
for (auto it = layers.rbegin(); it != layers.rend(); ++it) { bool gotEvent;
if ((*it)->handleButtonPress(button)) { const bool wait = !animating;
break; do {
} do {
} gotEvent = input.getEvent(&event, wait);
} while (gotEvent && event.state != InputManager::PRESSED);
// Remove dismissed layers from the stack. } while (wait && !gotEvent);
for (auto it = layers.begin(); it != layers.end(); ) { if (gotEvent) {
auto layer = *it; for (auto it = layers.rbegin(); it != layers.rend(); ++it) {
if (layer->wasDismissed()) { if ((*it)->handleButtonPress(event.button)) {
it = layers.erase(it); break;
} else { }
++it;
} }
} }
} }

View File

@ -32,14 +32,14 @@ void HelpPopup::paint(Surface &s) {
bool HelpPopup::handleButtonPress(InputManager::Button button) { bool HelpPopup::handleButtonPress(InputManager::Button button) {
if (button == InputManager::CANCEL) { if (button == InputManager::CANCEL) {
dismissed = true; dismiss();
} }
return true; return true;
} }
bool HelpPopup::handleTouchscreen(Touchscreen &ts) { bool HelpPopup::handleTouchscreen(Touchscreen &ts) {
if (ts.pressed()) { if (ts.pressed()) {
dismissed = true; dismiss();
ts.setHandled(); ts.setHandled();
} }
return true; return true;

View File

@ -58,6 +58,7 @@ public:
bool waitForEvent(ButtonEvent *event); bool waitForEvent(ButtonEvent *event);
Button waitForPressedButton(); Button waitForPressedButton();
bool pollEvent(ButtonEvent *event); bool pollEvent(ButtonEvent *event);
bool getEvent(ButtonEvent *bevent, bool wait);
private: private:
enum ButtonSource { UNMAPPED, KEYBOARD, JOYSTICK }; enum ButtonSource { UNMAPPED, KEYBOARD, JOYSTICK };
@ -69,7 +70,6 @@ private:
Menu *menu; Menu *menu;
void readConfFile(const std::string &conffile); void readConfFile(const std::string &conffile);
bool getEvent(ButtonEvent *bevent, bool wait);
ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE]; ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE];
#ifndef SDL_JOYSTICK_DISABLED #ifndef SDL_JOYSTICK_DISABLED

View File

@ -16,6 +16,8 @@ class Touchscreen;
*/ */
class Layer { class Layer {
public: public:
enum class Status { PASSIVE, ANIMATING, DISMISSED };
virtual ~Layer() {} virtual ~Layer() {}
/** /**
@ -36,13 +38,34 @@ public:
*/ */
virtual bool handleTouchscreen(Touchscreen &ts) = 0; virtual bool handleTouchscreen(Touchscreen &ts) = 0;
bool wasDismissed() { return dismissed; } Status getStatus() { return status; }
protected: protected:
/** /**
* Set this to true to request the layer to be removed from the stack. * Request the Layer to be removed from the stack.
* There could be a few more calls to the Layer before it is actually
* removed, so be prepared to handle those.
*/ */
bool dismissed = false; void dismiss() {
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;
}; };
#endif // LAYER_H #endif // LAYER_H