vdr-plugin-skindesigner/coreengine/animation.c

680 lines
18 KiB
C
Raw Normal View History

2016-07-22 15:21:09 +02:00
#include "../config.h"
2016-02-06 09:55:15 +01:00
#include "animation.h"
#include <math.h>
/******************************************************************
2016-07-22 15:21:09 +02:00
* cDetacher
2016-02-06 09:55:15 +01:00
******************************************************************/
2016-07-22 15:21:09 +02:00
cDetacher::cDetacher(cDetachable *detachable, bool wait, bool animation) : cThread("detacher thread") {
2016-02-06 09:55:15 +01:00
this->detachable = detachable;
waitOnWakeup = wait;
keepSleeping = false;
2016-02-06 09:55:15 +01:00
doAnimation = animation;
}
2016-07-22 15:21:09 +02:00
cDetacher::~cDetacher(void) {
2016-02-06 09:55:15 +01:00
sleepWait.Signal();
Cancel(2);
}
2016-07-22 15:21:09 +02:00
void cDetacher::WakeUp(void) {
2016-02-06 09:55:15 +01:00
sleepWait.Signal();
}
2016-07-22 15:21:09 +02:00
void cDetacher::ResetSleep(void) {
keepSleeping = true;
sleepWait.Signal();
}
2016-07-22 15:21:09 +02:00
void cDetacher::Stop(bool deletePixmaps) {
2016-02-06 09:55:15 +01:00
sleepWait.Signal();
Cancel(2);
}
2016-07-22 15:21:09 +02:00
void cDetacher::Action(void) {
if (!detachable) {
return;
}
if (waitOnWakeup) {
Wait();
int delay = 50 + detachable->Delay();
Sleep(delay);
} else {
int delay = detachable->Delay();
if (delay > 0)
Sleep(delay);
}
detachable->ParseDetached();
detachable->RenderDetached();
if (!doAnimation)
detachable->Flush();
if (!Running()) return;
if (doAnimation) {
detachable->StartAnimation();
2016-02-06 09:55:15 +01:00
}
}
2016-07-22 15:21:09 +02:00
void cDetacher::Sleep(int duration) {
2016-02-06 09:55:15 +01:00
if (duration <= 0)
return;
do {
keepSleeping = false;
sleepWait.Wait(duration);
} while (keepSleeping);
2016-02-06 09:55:15 +01:00
}
2016-07-22 15:21:09 +02:00
void cDetacher::Wait(void) {
2016-02-06 09:55:15 +01:00
//wait has to be waked up from outside
sleepWait.Wait(0);
}
2016-07-22 15:21:09 +02:00
/******************************************************************
* cAnimaton
******************************************************************/
cAnimation::cAnimation(void) {
started = 0;
finished = false;
persistent = false;
frametime = 1000 / config.FPS;
}
cAnimation::~cAnimation(void) {
}
/******************************************************************
* cScroller
******************************************************************/
cScroller::cScroller(cScrollable *scrollable) {
this->scrollable = scrollable;
paused = true;
pauseTime = 0;
scrollingStarted = false;
secondDelay = false;
delScrollPix = true;
Init();
}
cScroller::~cScroller(void) {
}
void cScroller::Init(void) {
delay = scrollable->ScrollDelay();
orientation = scrollable->ScrollOrientation();
2016-02-06 09:55:15 +01:00
if (orientation == eOrientation::horizontal) {
2016-07-22 15:21:09 +02:00
scrollLength = scrollable->ScrollWidth();
2016-02-06 09:55:15 +01:00
} else if (orientation == eOrientation::vertical) {
2016-07-22 15:21:09 +02:00
scrollLength = scrollable->ScrollHeight();
2016-02-06 09:55:15 +01:00
}
eScrollMode mode = scrollable->ScrollMode();
2016-07-22 15:21:09 +02:00
carriageReturn = (mode == eScrollMode::carriagereturn) ? true : false;
drawPortX = 0.0f;
drawPortY = 0.0f;
2016-02-06 09:55:15 +01:00
eScrollSpeed speed = scrollable->ScrollSpeed();
if (speed == eScrollSpeed::slow)
2016-07-22 15:21:09 +02:00
scrollDelta = 0.5f;
2016-02-06 09:55:15 +01:00
else if (speed == eScrollSpeed::fast)
2016-07-22 15:21:09 +02:00
scrollDelta = 2.0f;
else
scrollDelta = 1.0f;
}
2016-02-06 09:55:15 +01:00
2016-07-22 15:21:09 +02:00
void cScroller::Reactivate(void) {}
void cScroller::SetInitial(void) {
scrollable->SetScrollingStarted();
}
2016-02-06 09:55:15 +01:00
2016-07-22 15:21:09 +02:00
bool cScroller::Pause(void) {
if (!paused)
return false;
if ((pauseTime + frametime) > delay) {
paused = false;
pauseTime = 0;
return false;
}
pauseTime += frametime;
return true;
}
2016-02-06 09:55:15 +01:00
2016-07-22 15:21:09 +02:00
bool cScroller::Overflow(void) {
if (orientation == eOrientation::horizontal) {
if (!carriageReturn && (drawPortX >= 1)) {
drawPortX = 0;
2016-02-06 09:55:15 +01:00
scrollDelta *= -1;
2016-07-22 15:21:09 +02:00
paused = true;
return true;
}
if (carriageReturn && (drawPortX >= 0) && secondDelay) {
cPoint drawPortPoint(drawPortX,0);
scrollable->SetDrawPort(drawPortPoint);
drawPortX = -1;
paused = true;
secondDelay = false;
return true;
}
2016-07-23 17:42:10 +02:00
if (abs((int)drawPortX) < scrollLength)
2016-07-22 15:21:09 +02:00
return false;
if (carriageReturn) {
drawPortX = 0;
secondDelay = true;
} else {
scrollDelta *= -1;
drawPortX -= scrollDelta;
}
} else if (orientation == eOrientation::vertical) {
if ((drawPortY >= 0) && secondDelay) {
cPoint drawPortPoint(0, drawPortY);
scrollable->SetDrawPort(drawPortPoint);
drawPortY = -1;
paused = true;
secondDelay = false;
return true;
2016-02-06 09:55:15 +01:00
}
2016-07-23 17:42:10 +02:00
if (abs((int)drawPortY) < scrollLength)
2016-07-22 15:21:09 +02:00
return false;
secondDelay = true;
drawPortY = 0;
}
paused = true;
return true;
}
2016-02-06 09:55:15 +01:00
2016-07-22 15:21:09 +02:00
void cScroller::SetFinished(void) {
finished = true;
if (delScrollPix) {
scrollable->StopScrolling();
2016-02-06 09:55:15 +01:00
}
}
2016-07-22 15:21:09 +02:00
bool cScroller::Tick(void) {
if (finished) {
return false;
2016-02-06 09:55:15 +01:00
}
2016-07-22 15:21:09 +02:00
if (Pause())
return true;
if (!scrollingStarted) {
scrollable->StartScrolling();
scrollingStarted = true;
2016-02-06 09:55:15 +01:00
}
2016-07-22 15:21:09 +02:00
if (Overflow())
return true;
cPoint drawPortPoint(0,0);
if (orientation == eOrientation::horizontal) {
drawPortX -= scrollDelta;
drawPortPoint.SetX(drawPortX);
} else if (orientation == eOrientation::vertical) {
drawPortY -= scrollDelta;
drawPortPoint.SetY(drawPortY);
}
scrollable->SetDrawPort(drawPortPoint);
return true;
};
/******************************************************************
* cFader
******************************************************************/
cFader::cFader(cFadable *fadable) {
this->fadable = fadable;
fadein = true;
fadetime = fadable->FadeTime();
step = 100.0f / ((double)fadetime / (double)frametime);
transparency = 100;
hideWhenFinished = false;
}
cFader::~cFader(void) {
2016-02-06 09:55:15 +01:00
}
2016-07-22 15:21:09 +02:00
void cFader::Reactivate(void) {
started = 0;
finished = false;
fadein = false;
}
void cFader::SetInitial(void) {
fadable->SetTransparency(transparency);
}
void cFader::SetFadeOut(void) {
fadein = false;
transparency = 0;
}
void cFader::SetFinished(void) {
finished = true;
if (hideWhenFinished)
fadable->SetTransparency(100);
}
bool cFader::Tick(void) {
if (finished) {
if (fadein)
fadable->SetTransparency(0);
else
fadable->SetTransparency(100);
return false;
}
if (!started) {
started = cTimeMs::Now();
}
if ((int)(cTimeMs::Now() - started) > fadetime) {
if (fadein)
fadable->SetTransparency(0);
else
fadable->SetTransparency(100);
finished = true;
return false;
}
fadable->SetTransparency(transparency);
if (fadein) {
transparency -= step;
2016-02-06 09:55:15 +01:00
} else {
2016-07-22 15:21:09 +02:00
transparency += step;
2016-02-06 09:55:15 +01:00
}
2016-07-22 15:21:09 +02:00
return true;
};
/******************************************************************
* cShifter
******************************************************************/
cShifter::cShifter(cShiftable *shiftable) {
this->shiftable = shiftable;
step = 0;
shiftin = true;
shifttime = 0;
x = 0.0f;
y = 0.0f;
2016-07-30 17:31:34 +02:00
stepXLinear = 0.0f;
stepYLinear = 0.0f;
2016-07-22 15:21:09 +02:00
stepsFast = 0;
2016-07-30 17:31:34 +02:00
stepXFast = 0.0f;
stepXSlow = 0.0f;
stepYFast = 0.0f;
stepYSlow = 0.0f;
2016-07-22 15:21:09 +02:00
Init();
}
cShifter::~cShifter(void) {
}
void cShifter::Init(void) {
shifttime = shiftable->ShiftTime();
mode = (eShiftMode)shiftable->ShiftMode();
shiftable->ShiftPositions(&start, &end);
int steps = (double)shifttime / (double)frametime;
if (steps <= 0) steps = 1;
2016-07-22 15:21:09 +02:00
float percentFast = 33.3f;
float distanceFast = 85.0f;
stepsFast = (float)steps * percentFast / 100.0f;
if (start.X() == end.X()) {
stepYLinear = (float)(end.Y() - start.Y()) / (float)steps;
stepYFast = (float)(end.Y() - start.Y()) * distanceFast / 100.0f / (float)stepsFast;
stepYSlow = (float)(end.Y() - start.Y()) * (100.0f - distanceFast) / 100.0f / (float)(steps-stepsFast);
} else if (start.Y() == end.Y()) {
2016-07-30 17:31:34 +02:00
stepXLinear = (float)(end.X() - start.X()) / (float)steps;
2016-07-22 15:21:09 +02:00
stepXFast = (float)(end.X() - start.X()) * distanceFast / 100.0f / (float)stepsFast;
stepXSlow = (float)(end.X() - start.X()) * (100.0f - distanceFast) / 100.0f / (float)(steps-stepsFast);
} else {
2016-07-30 17:31:34 +02:00
stepXLinear = (float)(end.X() - start.X()) / (float)steps;
2016-07-22 15:21:09 +02:00
stepXFast = (float)(end.X() - start.X()) * distanceFast / 100.0f / (float)stepsFast;
stepXSlow = (float)(end.X() - start.X()) * (100.0f - distanceFast) / 100.0f / (float)(steps-stepsFast);
2016-07-30 17:31:34 +02:00
stepYLinear = (float)(end.Y() - start.Y()) / (float)steps;
2016-07-22 15:21:09 +02:00
stepYFast = (float)(end.Y() - start.Y()) * distanceFast / 100.0f / (float)stepsFast;
stepYSlow = (float)(end.Y() - start.Y()) * (100.0f - distanceFast) / 100.0f / (float)(steps-stepsFast);
2016-02-06 09:55:15 +01:00
}
if (shiftin) {
x = start.X();
y = start.Y();
} else {
x = end.X();
y = end.Y();
}
2016-07-22 15:21:09 +02:00
}
void cShifter::Reactivate(void) {
started = 0;
finished = false;
shiftin = false;
step = 0;
2016-07-31 07:19:05 +02:00
Init();
2016-07-22 15:21:09 +02:00
}
void cShifter::SetInitial(void) {
cPoint pos(x, y);
shiftable->SetPosition(pos, end);
}
void cShifter::NextPosition(void) {
if (mode == eShiftMode::linear) {
if (shiftin) {
x += stepXLinear;
y += stepYLinear;
} else {
x -= stepXLinear;
y -= stepYLinear;
2016-02-06 09:55:15 +01:00
}
2016-07-22 15:21:09 +02:00
} else if (mode == eShiftMode::slowedDown) {
if (shiftin) {
if (step <= stepsFast) {
x += stepXFast;
y += stepYFast;
} else {
x += stepXSlow;
y += stepYSlow;
}
2016-02-06 09:55:15 +01:00
} else {
2016-07-22 15:21:09 +02:00
if (step <= stepsFast) {
x -= stepXFast;
y -= stepYFast;
} else {
x -= stepXSlow;
y -= stepYSlow;
}
2016-02-06 09:55:15 +01:00
}
}
}
2016-07-22 15:21:09 +02:00
bool cShifter::Tick(void) {
if (finished)
return false;
if (!started) {
started = cTimeMs::Now();
}
if ((int)(cTimeMs::Now() - started) > shifttime) {
if (shiftin)
shiftable->SetPosition(end, end);
else
shiftable->SetPosition(start, end);
finished = true;
return false;
}
cPoint pos(x, y);
shiftable->SetPosition(pos, end);
step++;
NextPosition();
return true;
};
2016-02-06 09:55:15 +01:00
2016-07-22 15:21:09 +02:00
/******************************************************************
* cListShifter
******************************************************************/
cListShifter::cListShifter(cListShiftable *shiftable) {
this->shiftable = shiftable;
shifttime = shiftable->ListShiftTime();
distance = shiftable->ShiftDistance();
orientation = shiftable->ShiftOrientation();
2016-02-06 09:55:15 +01:00
int steps = (double)shifttime / (double)frametime;
if (steps <= 0) steps = 1;
2016-07-22 15:21:09 +02:00
step = distance / steps;
shiftin = true;
fromtop = true;
}
cListShifter::~cListShifter(void) {
}
void cListShifter::Reactivate(void) {}
void cListShifter::SetInitial(void) {
if (shiftin) {
if (orientation == eOrientation::horizontal) {
if (fromtop) {
pos.SetX(-1 * distance);
pos.SetY(0);
} else {
pos.SetX(distance);
pos.SetY(0);
}
} else {
if (fromtop) {
pos.SetX(0);
pos.SetY(-1 * distance);
} else {
pos.SetX(0);
pos.SetY(distance);
}
}
}
shiftable->SetIndicatorPosition(pos);
}
void cListShifter::NextPosition(void) {
int x = pos.X();
int y = pos.Y();
if (orientation == eOrientation::horizontal) {
if (fromtop) {
pos.SetX(x+step);
} else {
pos.SetX(x-step);
}
2016-02-06 09:55:15 +01:00
} else {
2016-07-22 15:21:09 +02:00
if (fromtop) {
pos.SetY(y+step);
} else {
pos.SetY(y-step);
}
2016-02-06 09:55:15 +01:00
}
2016-07-22 15:21:09 +02:00
}
2016-02-06 09:55:15 +01:00
2016-07-22 15:21:09 +02:00
void cListShifter::EndPosition(void) {
if (shiftin) {
pos.SetX(0);
pos.SetY(0);
} else {
if (orientation == eOrientation::horizontal) {
pos.SetX(distance);
} else {
pos.SetY(distance);
}
}
shiftable->SetIndicatorPosition(pos);
}
2016-02-06 09:55:15 +01:00
2016-07-22 15:21:09 +02:00
bool cListShifter::Tick(void) {
if (finished) {
EndPosition();
return false;
}
if (!started) {
started = cTimeMs::Now();
}
if ((int)(cTimeMs::Now() - started) > shifttime) {
EndPosition();
finished = true;
return false;
2016-02-06 09:55:15 +01:00
}
2016-07-22 15:21:09 +02:00
shiftable->SetIndicatorPosition(pos);
NextPosition();
return true;
};
/******************************************************************
* cBlinker
******************************************************************/
cBlinker::cBlinker(cBlinkable *blinkable, int blinkFunc) {
this->blinkable = blinkable;
this->blinkFunc = blinkFunc;
freq = blinkable->BlinkFreq(blinkFunc);
blinkOn = false;
paused = true;
pauseTime = 0;
}
cBlinker::~cBlinker(void) {
}
void cBlinker::Reactivate(void) {}
void cBlinker::SetInitial(void) {}
bool cBlinker::Pause(void) {
if (!paused)
return false;
if ((pauseTime + frametime) > freq) {
paused = false;
pauseTime = 0;
return false;
}
pauseTime += frametime;
return true;
}
bool cBlinker::Tick(void) {
if (finished)
return false;
if (Pause())
return true;
blinkable->DoBlink(blinkFunc, blinkOn);
blinkOn = !blinkOn;
paused = true;
pauseTime = 0;
return true;
};
/******************************************************************
* cAnimator
******************************************************************/
cAnimator::cAnimator(cSdOsd *osd) : cThread("animator thread") {
this->osd = osd;
timeneeded = 0;
timeslice = 1000 / config.FPS;
}
cAnimator::~cAnimator(void) {
Stop();
}
void cAnimator::Sleep(uint64_t start) {
timeneeded = cTimeMs::Now() - start;
int sleepTime = (timeslice - timeneeded) > 0 ? timeslice - timeneeded : 0;
if (sleepTime)
sleepWait.Wait(sleepTime);
}
void cAnimator::DoTick(bool &animActive) {
animLock.Lock();
for (cAnimation *animation = animations.First(); animation; animation = animations.Next(animation)) {
if (Running()) {
bool currentAnimActive = animation->Tick();
animActive = animActive || currentAnimActive;
2016-02-06 09:55:15 +01:00
}
2016-07-22 15:21:09 +02:00
}
animLock.Unlock();
}
/*****************************************************************************************
* Cleanup Anims
* removes finished anims
* remembers persistent anims
*****************************************************************************************/
void cAnimator::CleanupAnims(void) {
bool found;
animLock.Lock();
do {
found = false;
for (cAnimation *animation = animations.First(); animation; animation = animations.Next(animation)) {
if (!animation->Finished())
continue;
if (animation->Persistent()) {
animations.Del(animation, false);
animationsPersistent.Add(animation);
} else {
animations.Del(animation);
2016-02-06 09:55:15 +01:00
}
2016-07-22 15:21:09 +02:00
found = true;
2016-02-06 09:55:15 +01:00
break;
}
2016-07-22 15:21:09 +02:00
} while (found);
animLock.Unlock();
}
/*****************************************************************************************
* Main Loop
*****************************************************************************************/
void cAnimator::Action(void) {
while(Running()) {
bool animActive = false;
uint64_t start = cTimeMs::Now();
DoTick(animActive); if (!Running()) break;
osd->Flush(); if (!Running()) break;
CleanupAnims(); if (!Running()) break;
if (!animActive) {
pauseWait.Wait();
2016-02-06 09:55:15 +01:00
} else {
2016-07-22 15:21:09 +02:00
Sleep(start);
2016-02-06 09:55:15 +01:00
}
}
2016-07-22 15:21:09 +02:00
}
/*****************************************************************************************
* Add Animation
* if startAnim is set to true, main loop gets waked up
*****************************************************************************************/
void cAnimator::AddAnimation(cAnimation *animation, bool startAnim) {
animation->SetInitial();
animLock.Lock();
animations.Ins(animation);
animLock.Unlock();
if (startAnim)
pauseWait.Signal();
}
/*****************************************************************************************
* Remove Animation
* animation will be set to finished and removed later by Cleanup()
*****************************************************************************************/
void cAnimator::RemoveAnimation(cAnimation *remove) {
animLock.Lock();
for (cAnimation *animation = animations.First(); animation; animation = animations.Next(animation)) {
if (animation == remove) {
animation->SetFinished();
break;
}
}
2016-07-22 15:21:09 +02:00
animLock.Unlock();
}
/*****************************************************************************************
* Finish Main Loop
*****************************************************************************************/
void cAnimator::Stop(void) {
if (!Running())
return;
Cancel(-1);
pauseWait.Signal();
sleepWait.Signal();
Cancel(2);
}
/*****************************************************************************************
* shift or fade out persistent animations
*****************************************************************************************/
void cAnimator::Finish(void) {
bool animActive = true;
bool reactivate = true;
while(animActive) {
animActive = false;
uint64_t start = cTimeMs::Now();
animLock.Lock();
for (cAnimation *animation = animationsPersistent.First(); animation; animation = animationsPersistent.Next(animation)) {
if (reactivate)
animation->Reactivate();
bool currentAnimActive = animation->Tick();
animActive = animActive || currentAnimActive;
}
animLock.Unlock();
reactivate = false;
if (!animActive)
break;
2019-10-12 15:56:07 +02:00
osd->Flush();
2016-07-22 15:21:09 +02:00
Sleep(start);
2016-02-06 09:55:15 +01:00
}
}