infinipaint/src/TimePoint.hpp

66 lines
2.4 KiB
C++
Raw Normal View History

/*
* InfiniPaint
* Copyright (C) 2025-2026 Yousef Khadadeh
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2025-07-06 20:43:53 +03:00
#pragma once
#include <chrono>
#include <algorithm>
#include <cmath>
#include <Helpers/BezierEasing.hpp>
#include <include/core/SkColor.h>
#include "SharedTypes.hpp"
template <typename T> T lerp_time(T t, T tEnd, T tStart = 0) {
return std::clamp<T>((t - tStart) / (tEnd - tStart), 0, 1);
}
WorldVec lerp_world_vec_double(const WorldVec& v1, const WorldVec& v2, double t);
template<typename T, typename TimeType> std::enable_if_t<!std::is_arithmetic_v<T>, T> lerp_vec(const T& v1, const T& v2, TimeType t) {
T item;
for(unsigned i = 0; i < v1.size(); i++)
item[i] = std::lerp(v1[i], v2[i], t);
return item;
}
template<typename T, typename TimeType> std::enable_if_t<std::is_arithmetic_v<T>, T> lerp_vec(T v1, T v2, TimeType t) {
return std::lerp(v1, v2, t);
}
template<typename TimeType> SkColor4f lerp_vec(const SkColor4f& t1, const SkColor4f& t2, TimeType t) {
return {std::lerp(t1[0], t2[0], t),
std::lerp(t1[1], t2[1], t),
std::lerp(t1[2], t2[2], t),
std::lerp(t1[3], t2[3], t)};
}
2026-04-09 10:20:40 +03:00
bool smooth_two_way_animation_time_check_for_change(float& timeToUpdate, float deltaTime, bool isWay1, float animDuration);
float smooth_two_way_animation_time_get_lerp(float& timeToUpdate, float deltaTime, bool isWay1, float animDuration);
void smooth_two_way_animation_time(float& timeToUpdate, float deltaTime, bool isWay1, float animDuration);
2025-07-06 20:43:53 +03:00
class TimePoint {
public:
TimePoint();
void update_time_point();
void update_time_since();
float get_time_since() const;
operator float() const;
private:
std::chrono::time_point<std::chrono::steady_clock> tp;
float timeSince;
};