|
| 1 | +// |
| 2 | +// TransitionApp.cpp |
| 3 | +// neoForm |
| 4 | +// |
| 5 | +// Created by Charles Reischer 12/14/24. |
| 6 | +//{ |
| 7 | + |
| 8 | +#include "TransitionApp.hpp" |
| 9 | +#include "ofGraphics.h" |
| 10 | +#include <sstream> |
| 11 | + |
| 12 | +std::string TransitionApp::getName() { |
| 13 | + if (startApp == nullptr || endApp == nullptr) return "Interpolate"; |
| 14 | + std::stringstream name = std::stringstream("Interpolate ("); |
| 15 | + name << startApp->getName() << " -> " << endApp->getName() << ")"; |
| 16 | + return name.str(); |
| 17 | +} |
| 18 | + |
| 19 | +void TransitionApp::startTransition(Application* start, Application* end, float duration, Application** pointerToActiveApplication, bool* pointerToBlock) { |
| 20 | + startApp = start; |
| 21 | + endApp = end; |
| 22 | + transitionDuration = duration; |
| 23 | + transitionProgress = 0.0f; |
| 24 | + appManagerPointer = pointerToActiveApplication; |
| 25 | + appManagerBlock = pointerToBlock; |
| 26 | +} |
| 27 | + |
| 28 | +void TransitionApp::update(float dt) { |
| 29 | + if (startApp == nullptr || endApp == nullptr || appManagerPointer == nullptr) { |
| 30 | + std::cout << "breaking (nullptr)" << std::endl; |
| 31 | + return; |
| 32 | + } |
| 33 | + transitionProgress += dt; |
| 34 | + |
| 35 | + if (transitionProgress >= transitionDuration) { |
| 36 | + // manually sets the appManager's active app |
| 37 | + *appManagerPointer = endApp; |
| 38 | + *appManagerBlock = false; |
| 39 | + endApp->update(dt); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + startApp->update(dt); |
| 44 | + endApp->update(dt); |
| 45 | + |
| 46 | + // blend heights from startApp and endApp |
| 47 | + startApp->getHeightsForShapeDisplay(heightsForShapeDisplay); |
| 48 | + ofPixels endHeights; |
| 49 | + endApp->getHeightsForShapeDisplay(endHeights); |
| 50 | + |
| 51 | + for (int i = 0; i < heightsForShapeDisplay.size(); i++) { |
| 52 | + float l = transitionProgress/transitionDuration; |
| 53 | + heightsForShapeDisplay[i] = (1.0f - l) * heightsForShapeDisplay[i] + l * endHeights[i]; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +void fade(int x, int y, int width, int height, double alpha) { |
| 58 | + ofSetColor(0, 0, 0, alpha); |
| 59 | + ofDrawRectangle(x, y, width, height); |
| 60 | + ofSetColor(255); |
| 61 | +} |
| 62 | + |
| 63 | +void TransitionApp::drawGraphicsForShapeDisplay(int x, int y, int width, int height) { |
| 64 | + if (startApp == nullptr || endApp == nullptr) return; |
| 65 | + if (transitionProgress <= transitionDuration/2.0f) { |
| 66 | + startApp->drawGraphicsForShapeDisplay(x, y, width, height); |
| 67 | + fade(x, y, width, height, 255.0f * 2.0f * transitionProgress / transitionDuration); |
| 68 | + } else { |
| 69 | + endApp->drawGraphicsForShapeDisplay(x, y, width, height); |
| 70 | + fade(x, y, width , height, 255.0 * 2.0f * (transitionDuration - transitionProgress) / transitionDuration); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void TransitionApp::drawGraphicsForPublicDisplay(int x, int y, int width, int height) { |
| 75 | + if (startApp == nullptr || endApp == nullptr) return; |
| 76 | + if (transitionProgress <= transitionDuration/2.0f) { |
| 77 | + startApp->drawGraphicsForPublicDisplay(x, y, width, height); |
| 78 | + fade(x, y, width, height, 255.0f * 2.0f * transitionProgress / transitionDuration); |
| 79 | + } else { |
| 80 | + endApp->drawGraphicsForPublicDisplay(x, y, width, height); |
| 81 | + fade(x, y, width , height, 255.0 * 2.0f * (transitionDuration - transitionProgress) / transitionDuration); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void TransitionApp::drawGraphicsForProjector(int x, int y, int width, int height) { |
| 86 | + if (startApp == nullptr || endApp == nullptr) return; |
| 87 | + if (transitionProgress <= transitionDuration/2.0f) { |
| 88 | + startApp->drawGraphicsForProjector(x, y, width, height); |
| 89 | + fade(x, y, width, height, 255.0f * 2.0f * transitionProgress / transitionDuration); |
| 90 | + } else { |
| 91 | + endApp->drawGraphicsForProjector(x, y, width, height); |
| 92 | + fade(x, y, width , height, 255.0 * 2.0f * (transitionDuration - transitionProgress) / transitionDuration); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void TransitionApp::keyPressed(int key) { |
| 97 | + if (startApp == nullptr || endApp == nullptr) return; |
| 98 | + startApp->keyPressed(key); |
| 99 | + endApp->keyPressed(key); |
| 100 | +} |
0 commit comments