Hyprland/src/managers/AnimationManager.cpp

161 lines
6.6 KiB
C++
Raw Normal View History

2022-03-23 22:01:59 +01:00
#include "AnimationManager.hpp"
#include "../Compositor.hpp"
void CAnimationManager::tick() {
bool animationsDisabled = false;
if (!g_pConfigManager->getInt("animations:enabled"))
animationsDisabled = true;
2022-03-31 17:50:00 +02:00
const bool WINDOWSENABLED = g_pConfigManager->getInt("animations:windows") && !animationsDisabled;
const bool BORDERSENABLED = g_pConfigManager->getInt("animations:borders") && !animationsDisabled;
2022-04-05 19:28:10 +02:00
const bool FADEENABLED = g_pConfigManager->getInt("animations:fadein") && !animationsDisabled;
2022-03-23 22:01:59 +01:00
const float ANIMSPEED = g_pConfigManager->getFloat("animations:speed");
2022-03-31 17:53:28 +02:00
// Process speeds
const float WINDOWSPEED = g_pConfigManager->getFloat("animations:windows_speed") == 0 ? ANIMSPEED : g_pConfigManager->getFloat("animations:windows_speed");
const float BORDERSPEED = g_pConfigManager->getFloat("animations:borders_speed") == 0 ? ANIMSPEED : g_pConfigManager->getFloat("animations:borders_speed");
2022-04-05 19:28:10 +02:00
const float FADESPEED = g_pConfigManager->getFloat("animations:fadein_speed") == 0 ? ANIMSPEED : g_pConfigManager->getFloat("animations:fadein_speed");
2022-03-31 17:53:28 +02:00
2022-03-31 17:50:00 +02:00
const auto BORDERACTIVECOL = CColor(g_pConfigManager->getInt("general:col.active_border"));
const auto BORDERINACTIVECOL = CColor(g_pConfigManager->getInt("general:col.inactive_border"));
2022-04-14 16:43:29 +02:00
const auto BORDERSIZE = g_pConfigManager->getInt("general:border_size");
2022-03-23 22:01:59 +01:00
for (auto& w : g_pCompositor->m_lWindows) {
2022-03-31 17:50:00 +02:00
2022-04-14 16:43:29 +02:00
// get the box before transforms, for damage tracking later
wlr_box WLRBOXPREV = { w.m_vRealPosition.x - BORDERSIZE - 1, w.m_vRealPosition.y - BORDERSIZE - 1, w.m_vRealSize.x + 2 * BORDERSIZE + 2, w.m_vRealSize.y + 2 * BORDERSIZE + 2};
bool needsDamage = false;
2022-04-05 19:28:10 +02:00
// process fadeinout
if (FADEENABLED) {
const auto GOALALPHA = w.m_bIsMapped ? 255.f : 0.f;
w.m_bFadingOut = false;
if (!deltazero(w.m_fAlpha, GOALALPHA)) {
if (deltaSmallToFlip(w.m_fAlpha, GOALALPHA)) {
w.m_fAlpha = GOALALPHA;
} else {
if (w.m_fAlpha > GOALALPHA)
w.m_bFadingOut = true;
w.m_fAlpha = parabolic(w.m_fAlpha, GOALALPHA, FADESPEED);
}
2022-04-14 16:43:29 +02:00
needsDamage = true;
2022-04-05 19:28:10 +02:00
}
} else {
2022-04-14 16:43:29 +02:00
const auto GOALALPHA = w.m_bIsMapped ? 255.f : 0.f;
if (!deltazero(GOALALPHA, w.m_fAlpha)) {
if (w.m_bIsMapped)
w.m_fAlpha = 255.f;
else {
w.m_fAlpha = 0.f;
w.m_bFadingOut = false;
}
needsDamage = true;
2022-04-05 19:28:10 +02:00
}
}
2022-04-05 20:53:16 +02:00
// process fadein/out for unmapped windows, but nothing else.
// we can't use windowValidMapped because we want to animate hidden windows too.
2022-04-14 16:43:29 +02:00
if (!g_pCompositor->windowExists(&w) || !w.m_bIsMapped || !g_pXWaylandManager->getWindowSurface(&w)){
if (needsDamage) {
g_pHyprRenderer->damageWindow(&w); // only window, it didnt move cuz its unmappy
}
2022-04-05 20:53:16 +02:00
continue;
2022-04-14 16:43:29 +02:00
}
2022-04-05 20:53:16 +02:00
2022-03-31 17:50:00 +02:00
// process the borders
const auto RENDERHINTS = g_pLayoutManager->getCurrentLayout()->requestRenderHints(&w);
const auto& COLOR = RENDERHINTS.isBorderColor ? RENDERHINTS.borderColor : g_pCompositor->isWindowActive(&w) ? BORDERACTIVECOL : BORDERINACTIVECOL;
2022-03-31 17:50:00 +02:00
if (BORDERSENABLED) {
if (!deltazero(COLOR, w.m_cRealBorderColor)) {
if (deltaSmallToFlip(COLOR, w.m_cRealBorderColor)) {
w.m_cRealBorderColor = COLOR;
} else {
2022-03-31 17:53:28 +02:00
w.m_cRealBorderColor = parabolic(BORDERSPEED, w.m_cRealBorderColor, COLOR);
2022-03-31 17:50:00 +02:00
}
2022-04-14 16:43:29 +02:00
needsDamage = true;
2022-03-31 17:50:00 +02:00
}
} else {
2022-04-14 16:43:29 +02:00
if (!deltazero(w.m_cRealBorderColor, COLOR))
needsDamage = true;
2022-03-31 17:50:00 +02:00
w.m_cRealBorderColor = COLOR;
2022-03-23 22:01:59 +01:00
}
// process the window
if (WINDOWSENABLED) {
2022-03-31 17:50:00 +02:00
if (!deltazero(w.m_vRealPosition, w.m_vEffectivePosition) || !deltazero(w.m_vRealSize, w.m_vEffectiveSize)) {
if (deltaSmallToFlip(w.m_vRealPosition, w.m_vEffectivePosition) && deltaSmallToFlip(w.m_vRealSize, w.m_vEffectiveSize)) {
w.m_vRealPosition = w.m_vEffectivePosition;
w.m_vRealSize = w.m_vEffectiveSize;
g_pXWaylandManager->setWindowSize(&w, w.m_vRealSize);
} else {
// if it is to be animated, animate it.
2022-03-31 17:53:28 +02:00
w.m_vRealPosition = Vector2D(parabolic(w.m_vRealPosition.x, w.m_vEffectivePosition.x, WINDOWSPEED), parabolic(w.m_vRealPosition.y, w.m_vEffectivePosition.y, WINDOWSPEED));
w.m_vRealSize = Vector2D(parabolic(w.m_vRealSize.x, w.m_vEffectiveSize.x, WINDOWSPEED), parabolic(w.m_vRealSize.y, w.m_vEffectiveSize.y, WINDOWSPEED));
2022-03-31 17:50:00 +02:00
}
2022-04-14 16:43:29 +02:00
needsDamage = true;
2022-03-23 22:01:59 +01:00
}
2022-03-31 17:50:00 +02:00
} else {
2022-04-14 16:43:29 +02:00
if (!deltazero(w.m_vRealPosition, w.m_vEffectivePosition) || !deltazero(w.m_vRealSize, w.m_vEffectiveSize))
needsDamage = true;
2022-03-31 17:50:00 +02:00
w.m_vRealPosition = w.m_vEffectivePosition;
w.m_vRealSize = w.m_vEffectiveSize;
2022-03-23 22:01:59 +01:00
}
2022-04-14 16:43:29 +02:00
if (needsDamage) {
g_pHyprRenderer->damageBox(&WLRBOXPREV);
g_pHyprRenderer->damageWindow(&w);
}
2022-03-23 22:01:59 +01:00
}
}
bool CAnimationManager::deltaSmallToFlip(const Vector2D& a, const Vector2D& b) {
return std::abs(a.x - b.x) < 0.5f && std::abs(a.y - b.y) < 0.5f;
}
2022-03-31 17:50:00 +02:00
bool CAnimationManager::deltaSmallToFlip(const CColor& a, const CColor& b) {
return std::abs(a.r - b.r) < 0.5f && std::abs(a.g - b.g) < 0.5f && std::abs(a.b - b.b) < 0.5f && std::abs(a.a - b.a) < 0.5f;
}
2022-04-14 16:43:29 +02:00
bool CAnimationManager::deltaSmallToFlip(const float& a, const float& b) {
return std::abs(a - b) < 0.5f;
}
2022-03-23 22:01:59 +01:00
bool CAnimationManager::deltazero(const Vector2D& a, const Vector2D& b) {
return a.x == b.x && a.y == b.y;
}
2022-04-14 16:43:29 +02:00
bool CAnimationManager::deltazero(const float& a, const float& b) {
return a == b;
}
2022-03-31 17:50:00 +02:00
bool CAnimationManager::deltazero(const CColor& a, const CColor& b) {
return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
}
double CAnimationManager::parabolic(const double from, const double to, const double incline) {
2022-03-23 22:01:59 +01:00
return from + ((to - from) / incline);
2022-03-31 17:50:00 +02:00
}
CColor CAnimationManager::parabolic(const double incline, const CColor& from, const CColor& to) {
CColor newColor;
newColor.r = parabolic(from.r, to.r, incline);
newColor.g = parabolic(from.g, to.g, incline);
newColor.b = parabolic(from.b, to.b, incline);
newColor.a = parabolic(from.a, to.a, incline);
return newColor;
2022-03-23 22:01:59 +01:00
}