From dbdf6ed05479c061922b8ec4d9d39ba99c5b1b35 Mon Sep 17 00:00:00 2001 From: Tomas Jakobsson Date: Mon, 6 Nov 2017 11:46:15 +0100 Subject: [PATCH] Add math/Misc.h --- es-core/src/GuiComponent.h | 3 +- es-core/src/math/Misc.h | 61 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 es-core/src/math/Misc.h diff --git a/es-core/src/GuiComponent.h b/es-core/src/GuiComponent.h index ef1352d10..e53f63a15 100644 --- a/es-core/src/GuiComponent.h +++ b/es-core/src/GuiComponent.h @@ -2,6 +2,7 @@ #ifndef ES_CORE_GUI_COMPONENT_H #define ES_CORE_GUI_COMPONENT_H +#include "math/Misc.h" #include "math/Transform4x4f.h" #include "HelpPrompt.h" #include "HelpStyle.h" @@ -62,7 +63,7 @@ public: float getRotation() const; void setRotation(float rotation); - inline void setRotationDegrees(float rotation) { setRotation((float) (rotation * M_PI / 180)); } + inline void setRotationDegrees(float rotation) { setRotation((float)ES_DEG_TO_RAD(rotation)); } float getScale() const; void setScale(float scale); diff --git a/es-core/src/math/Misc.h b/es-core/src/math/Misc.h new file mode 100644 index 000000000..7796eddf1 --- /dev/null +++ b/es-core/src/math/Misc.h @@ -0,0 +1,61 @@ +#pragma once +#ifndef ES_CORE_MATH_MISC_H +#define ES_CORE_MATH_MISC_H + +#include + +#define ES_PI (3.1415926535897932384626433832795028841971693993751058209749445923) +#define ES_RAD_TO_DEG(x) ((x) * (180.0 / ES_PI)) +#define ES_DEG_TO_RAD(x) ((x) * (ES_PI / 180.0)) + +namespace Math +{ + inline float scroll_bounce(const float delayTime, const float scrollTime, const float currentTime, const int scrollLength) + { + if(currentTime < delayTime) + { + // wait + return 0; + } + else if(currentTime < (delayTime + scrollTime)) + { + // lerp from 0 to scrollLength + const float fraction = (currentTime - delayTime) / scrollTime; + return (float)(((1.0 - cos(ES_PI * fraction)) * 0.5) * scrollLength); + } + else if(currentTime < (delayTime + scrollTime + delayTime)) + { + // wait some more + return scrollLength; + } + else if(currentTime < (delayTime + scrollTime + delayTime + scrollTime)) + { + // lerp back from scrollLength to 0 + const float fraction = 1.0 - (currentTime - delayTime - scrollTime - delayTime) / scrollTime; + return (float)(((1.0 - cos(ES_PI * fraction)) * 0.5) * scrollLength); + } + + // and back to waiting + return 0; + } + + inline float scroll_loop(const float delayTime, const float scrollTime, const float currentTime, const int scrollLength) + { + if(currentTime < delayTime) + { + // wait + return 0; + } + else if(currentTime < (delayTime + scrollTime)) + { + // lerp from 0 to scrollLength + const float fraction = (currentTime - delayTime) / scrollTime; + return fraction * scrollLength; + } + + // and back to waiting + return 0; + } +} + +#endif // ES_CORE_MATH_MISC_H