Hi everyone,
I've been working with tweening for text lately. I thought I'd share the code I've written; hopefully it will prove useful for others:
The header file is TextTween.h :
/*enum of Tween Methods*/
enum class TweenMethod{LINEAR,SMOOTH1,SMOOTH2,EASE_IN1,EASE_IN2,EASE_OUT1,EASE_OUT2,BOUNCE,OVERSHOOT,NONE};//for tweens
/*Text Tween Properties struct*/
struct TextTweenProperties {
float durationData;
unsigned alphaBegin;
unsigned alphaEnd;
TweenMethod alphaMethod;
float angleBegin;
float angleEnd;
TweenMethod angleMethod;
unsigned redBegin;
unsigned redEnd;
TweenMethod redMethod;
unsigned greenBegin;
unsigned greenEnd;
TweenMethod greenMethod;
unsigned blueBegin;
unsigned blueEnd;
TweenMethod blueMethod;
float fontSizeBegin;
float fontSizeEnd;
TweenMethod fontSizeMethod;
float characterSpacingBegin;
float characterSpacingEnd;
TweenMethod characterSpacingMethod;
float lineSpacingBegin;
float lineSpacingEnd;
TweenMethod lineSpacingMethod;
float xBegin;
float xEnd;
TweenMethod xMethod;
float yBegin;
float yEnd;
TweenMethod yMethod;
TextTweenProperties():alphaMethod(TweenMethod::NONE),angleMethod(TweenMethod::NONE), redMethod(TweenMethod::NONE),greenMethod(TweenMethod::NONE),blueMethod(TweenMethod::NONE),fontSizeMethod(TweenMethod::NONE),characterSpacingMethod(TweenMethod::NONE),lineSpacingMethod(TweenMethod::NONE),xMethod(TweenMethod::NONE),yMethod(TweenMethod::NONE){}
};
/*TextTweenClass*/
class TextTweenClass {
private:
unsigned textTweenId;
TextTweenProperties tweenPropertiesData;
public:
TextTweenClass(float duration=1);//apparently you NEED a default constructor to allow use in maps
~TextTweenClass();
TextTweenClass(const TextTweenClass& t);//copy constructor
TextTweenClass& operator=(const TextTweenClass& t);//copy assignment
unsigned getId() const {return textTweenId;}
TextTweenClass& setAlpha(unsigned begin,unsigned end,TweenMethod m=TweenMethod::LINEAR);
TextTweenClass& setAngle(float begin,float end,TweenMethod m=TweenMethod::LINEAR);
TextTweenClass& setRed(unsigned begin,unsigned end,TweenMethod m=TweenMethod::LINEAR);
TextTweenClass& setGreen(unsigned begin,unsigned end,TweenMethod m=TweenMethod::LINEAR);
TextTweenClass& setBlue(unsigned begin,unsigned end,TweenMethod m=TweenMethod::LINEAR);
TextTweenClass& setFontSize(float begin,float end,TweenMethod m=TweenMethod::LINEAR);
TextTweenClass& setCharacterSpacing(float begin,float end,TweenMethod m=TweenMethod::LINEAR);
TextTweenClass& setLineSpacing(float begin,float end, TweenMethod m=TweenMethod::LINEAR);
TextTweenClass& setX(float begin,float end,TweenMethod m=TweenMethod::LINEAR);
TextTweenClass& setY(float begin,float end,TweenMethod m=TweenMethod::LINEAR);
TextTweenClass& play(unsigned textId,float delay);
bool isPlaying(unsigned textId);
TextTweenClass& pause(unsigned textId);
TextTweenClass& resume(unsigned textId);
TextTweenClass& stop(unsigned textId);
};
And the implementation file is TextTween.cpp :
#include "TextTween.h"
#include "agk.h"
/*TextTweenClass*/
TextTweenClass::TextTweenClass(float duration){
//you can put some check here to make sure duration is positive
tweenPropertiesData.durationData=duration;
textTweenId=agk::CreateTweenText(duration);
}
TextTweenClass::~TextTweenClass(){
agk::DeleteTween(getId());
}
TextTweenClass::TextTweenClass(const TextTweenClass& t):
textTweenId(agk::CreateTweenText(t.tweenPropertiesData.durationData)),
tweenPropertiesData(t.tweenPropertiesData)
{
if (tweenPropertiesData.alphaMethod!=TweenMethod::NONE){
setAlpha(tweenPropertiesData.alphaBegin,tweenPropertiesData.alphaEnd,tweenPropertiesData.alphaMethod);
}
if (tweenPropertiesData.angleMethod!=TweenMethod::NONE) {
setAngle(tweenPropertiesData.angleBegin,tweenPropertiesData.angleEnd,tweenPropertiesData.angleMethod);
}
if (tweenPropertiesData.redMethod!=TweenMethod::NONE){
setRed(tweenPropertiesData.redBegin,tweenPropertiesData.redEnd,tweenPropertiesData.redMethod);
}
if (tweenPropertiesData.greenMethod!=TweenMethod::NONE){
setGreen(tweenPropertiesData.greenBegin,tweenPropertiesData.greenEnd,tweenPropertiesData.greenMethod);
}
if (tweenPropertiesData.blueMethod!=TweenMethod::NONE){
setBlue(tweenPropertiesData.blueBegin,tweenPropertiesData.blueEnd,tweenPropertiesData.blueMethod);
}
if (tweenPropertiesData.fontSizeMethod!=TweenMethod::NONE) {
setFontSize(tweenPropertiesData.fontSizeBegin, tweenPropertiesData.fontSizeEnd,tweenPropertiesData.fontSizeMethod);
}
if (tweenPropertiesData.characterSpacingMethod!=TweenMethod::NONE) {
setCharacterSpacing(tweenPropertiesData.characterSpacingBegin, tweenPropertiesData.characterSpacingEnd,tweenPropertiesData.characterSpacingMethod);
}
if (tweenPropertiesData.lineSpacingMethod!=TweenMethod::NONE) {
setLineSpacing(tweenPropertiesData.lineSpacingBegin, tweenPropertiesData.lineSpacingEnd,tweenPropertiesData.lineSpacingMethod);
}
if (tweenPropertiesData.xMethod!=TweenMethod::NONE) {
setX(tweenPropertiesData.xBegin, tweenPropertiesData.xEnd,tweenPropertiesData.xMethod);
}
if (tweenPropertiesData.yMethod!=TweenMethod::NONE) {
setY(tweenPropertiesData.yBegin, tweenPropertiesData.yEnd,tweenPropertiesData.yMethod);
}
}
TextTweenClass& TextTweenClass::operator=(const TextTweenClass& t){
if (agk::GetTweenTextExists(getId())) agk::DeleteTween(getId());
textTweenId=agk::CreateTweenText(t.tweenPropertiesData.durationData);
tweenPropertiesData=t.tweenPropertiesData;
if (tweenPropertiesData.alphaMethod!=TweenMethod::NONE){
setAlpha(tweenPropertiesData.alphaBegin,tweenPropertiesData.alphaEnd,tweenPropertiesData.alphaMethod);
}
if (tweenPropertiesData.angleMethod!=TweenMethod::NONE) {
setAngle(tweenPropertiesData.angleBegin,tweenPropertiesData.angleEnd,tweenPropertiesData.angleMethod);
}
if (tweenPropertiesData.redMethod!=TweenMethod::NONE){
setRed(tweenPropertiesData.redBegin,tweenPropertiesData.redEnd,tweenPropertiesData.redMethod);
}
if (tweenPropertiesData.greenMethod!=TweenMethod::NONE){
setGreen(tweenPropertiesData.greenBegin,tweenPropertiesData.greenEnd,tweenPropertiesData.greenMethod);
}
if (tweenPropertiesData.blueMethod!=TweenMethod::NONE){
setBlue(tweenPropertiesData.blueBegin,tweenPropertiesData.blueEnd,tweenPropertiesData.blueMethod);
}
if (tweenPropertiesData.fontSizeMethod!=TweenMethod::NONE) {
setFontSize(tweenPropertiesData.fontSizeBegin, tweenPropertiesData.fontSizeEnd,tweenPropertiesData.fontSizeMethod);
}
if (tweenPropertiesData.characterSpacingMethod!=TweenMethod::NONE) {
setCharacterSpacing(tweenPropertiesData.characterSpacingBegin, tweenPropertiesData.characterSpacingEnd,tweenPropertiesData.characterSpacingMethod);
}
if (tweenPropertiesData.lineSpacingMethod!=TweenMethod::NONE) {
setLineSpacing(tweenPropertiesData.lineSpacingBegin, tweenPropertiesData.lineSpacingEnd,tweenPropertiesData.lineSpacingMethod);
}
if (tweenPropertiesData.xMethod!=TweenMethod::NONE) {
setX(tweenPropertiesData.xBegin, tweenPropertiesData.xEnd,tweenPropertiesData.xMethod);
}
if (tweenPropertiesData.yMethod!=TweenMethod::NONE) {
setY(tweenPropertiesData.yBegin, tweenPropertiesData.yEnd,tweenPropertiesData.yMethod);
}
return *this;
}
TextTweenClass& TextTweenClass::setAlpha(unsigned begin,unsigned end,TweenMethod m){
// you can put some code here to check that begin and end are non-negative integers less than 256
tweenPropertiesData.alphaBegin=begin;
tweenPropertiesData.alphaEnd=end;
tweenPropertiesData.alphaMethod=m;
agk::SetTweenTextAlpha(getId(), begin, end, static_cast<int>(m));
return *this;
}
TextTweenClass& TextTweenClass::setAngle(float begin,float end,TweenMethod m){
tweenPropertiesData.angleBegin=begin;
tweenPropertiesData.angleEnd=end;
tweenPropertiesData.angleMethod=m;
agk::SetTweenTextAngle(getId(), begin, end, static_cast<int>(m));
return *this;
}
TextTweenClass& TextTweenClass::setRed(unsigned begin,unsigned end,TweenMethod m){
// you can put some code here to check that begin and end are non-negative integers less than 256
tweenPropertiesData.redBegin=begin;
tweenPropertiesData.redEnd=end;
tweenPropertiesData.redMethod=m;
agk::SetTweenTextRed(getId(), begin, end, static_cast<int>(m));
return *this;
}
TextTweenClass& TextTweenClass::setGreen(unsigned begin,unsigned end,TweenMethod m){
// you can put some code here to check that begin and end are non-negative integers less than 256
tweenPropertiesData.greenBegin=begin;
tweenPropertiesData.greenEnd=end;
tweenPropertiesData.greenMethod=m;
agk::SetTweenTextGreen(getId(), begin, end, static_cast<int>(m));
return *this;
}
TextTweenClass& TextTweenClass::setBlue(unsigned begin,unsigned end,TweenMethod m){
// you can put some code here to check that begin and end are non-negative integers less than 256
tweenPropertiesData.blueBegin=begin;
tweenPropertiesData.blueEnd=end;
tweenPropertiesData.blueMethod=m;
agk::SetTweenTextBlue(getId(), begin, end, static_cast<int>(m));
return *this;
}
TextTweenClass& TextTweenClass::setFontSize(float begin,float end,TweenMethod m){
// you can put some code here to check that begin and end are non-negative integers less than 256
tweenPropertiesData.fontSizeBegin=begin;
tweenPropertiesData.fontSizeEnd=end;
tweenPropertiesData.fontSizeMethod=m;
agk::SetTweenTextSize(getId(), begin, end, static_cast<int>(m));
return *this;
}
TextTweenClass& TextTweenClass::setCharacterSpacing(float begin,float end,TweenMethod m){
tweenPropertiesData.characterSpacingBegin=begin;
tweenPropertiesData.characterSpacingEnd=end;
tweenPropertiesData.characterSpacingMethod=m;
agk::SetTweenTextSpacing(getId(), begin, end, static_cast<int>(m));
return *this;
}
TextTweenClass& TextTweenClass::setLineSpacing(float begin,float end, TweenMethod m){
// "tweening command for line space is bugged; already reported to AGK"
tweenPropertiesData.lineSpacingBegin=begin;
tweenPropertiesData.lineSpacingEnd=end;
tweenPropertiesData.lineSpacingMethod=m;
agk::SetTweenTextLineSpacing(getId(), begin, end, static_cast<int>(m));
return *this;
}
TextTweenClass& TextTweenClass::setX(float begin,float end,TweenMethod m){
tweenPropertiesData.xBegin=begin;
tweenPropertiesData.xEnd=end;
tweenPropertiesData.xMethod=m;
agk::SetTweenTextX(getId(), begin, end, static_cast<int>(m));
return *this;
}
TextTweenClass& TextTweenClass::setY(float begin,float end,TweenMethod m){
tweenPropertiesData.yBegin=begin;
tweenPropertiesData.yEnd=end;
tweenPropertiesData.yMethod=m;
agk::SetTweenTextY(getId(), begin, end, static_cast<int>(m));
return *this;
}
TextTweenClass& TextTweenClass::play(unsigned textId,float delay){
agk::PlayTweenText(getId(), textId, delay);
return *this;
}
bool TextTweenClass::isPlaying(unsigned textId){
return agk::GetTweenTextPlaying(getId(), textId);
}
TextTweenClass& TextTweenClass::pause(unsigned textId){
agk::PauseTweenText(getId(), textId);
return *this;
}
TextTweenClass& TextTweenClass::resume(unsigned textId){
agk::ResumeTweenText(getId(), textId);
return *this;
}
TextTweenClass& TextTweenClass::stop(unsigned textId){
agk::StopTweenText(getId(), textId);
return *this;
}
AppGameKit 2018.10.10
iMac Book Pro, MacOS 10.14, Xcode 10.1;
iPhone 6, iOS 12; iPad (3rd gen), iOS 9.35; iPad Pro 12'9 (2nd gen), iOS 12.
Dell Precision T7400, Windows 7 Professional 64bit, Visual Studio Community 2017;