Hyprland/src/managers/AnimationManager.cpp

396 lines
17 KiB
C++
Raw Normal View History

2022-03-23 22:01:59 +01:00
#include "AnimationManager.hpp"
#include "../Compositor.hpp"
2022-04-23 21:47:16 +02:00
CAnimationManager::CAnimationManager() {
2022-04-23 21:54:03 +02:00
std::vector<Vector2D> points = {Vector2D(0, 0.75f), Vector2D(0.15f, 1.f)};
2022-04-23 21:47:16 +02:00
m_mBezierCurves["default"].setup(&points);
}
void CAnimationManager::removeAllBeziers() {
m_mBezierCurves.clear();
// add the default one
2022-04-23 21:54:03 +02:00
std::vector<Vector2D> points = {Vector2D(0, 0.75f), Vector2D(0.15f, 1.f)};
2022-04-23 21:47:16 +02:00
m_mBezierCurves["default"].setup(&points);
}
void CAnimationManager::addBezierWithName(std::string name, const Vector2D& p1, const Vector2D& p2) {
std::vector points = {p1, p2};
m_mBezierCurves[name].setup(&points);
}
2022-03-23 22:01:59 +01:00
void CAnimationManager::tick() {
bool animationsDisabled = false;
static auto *const PANIMENABLED = &g_pConfigManager->getConfigValuePtr("animations:enabled")->intValue;
if (!*PANIMENABLED)
2022-03-23 22:01:59 +01:00
animationsDisabled = true;
static auto *const PBORDERSIZE = &g_pConfigManager->getConfigValuePtr("general:border_size")->intValue;
static auto *const PSHADOWSENABLED = &g_pConfigManager->getConfigValuePtr("decoration:drop_shadow")->intValue;
2022-07-28 13:28:43 +02:00
const auto DEFAULTBEZIER = m_mBezierCurves.find("default");
2022-04-05 20:53:16 +02:00
2022-04-23 21:47:16 +02:00
for (auto& av : m_lAnimatedVariables) {
// first of all, check if we need to update it at all
if (!av->isBeingAnimated())
continue;
if (av->m_eDamagePolicy == AVARDAMAGE_SHADOW && !*PSHADOWSENABLED) {
av->warp();
continue;
}
2022-04-23 14:16:02 +02:00
// get speed
2022-07-28 13:28:43 +02:00
const auto SPEED = av->m_pConfig->pValues->internalSpeed;
2022-03-31 17:50:00 +02:00
2022-06-30 12:54:58 +02:00
// get the spent % (0 - 1)
const auto DURATIONPASSED = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - av->animationBegin).count();
const float SPENT = std::clamp((DURATIONPASSED / 100.f) / SPEED, 0.f, 1.f);
2022-04-23 14:16:02 +02:00
// window stuff
const auto PWINDOW = (CWindow*)av->m_pWindow;
2022-05-12 11:27:31 +02:00
const auto PWORKSPACE = (CWorkspace*)av->m_pWorkspace;
const auto PLAYER = (SLayerSurface*)av->m_pLayer;
2022-07-27 12:32:00 +02:00
CMonitor* PMONITOR = nullptr;
2022-05-12 11:27:31 +02:00
wlr_box WLRBOXPREV = {0,0,0,0};
if (PWINDOW) {
2022-06-26 22:15:06 +02:00
WLRBOXPREV = PWINDOW->getFullWindowBoundingBox();
PMONITOR = g_pCompositor->getMonitorFromID(PWINDOW->m_iMonitorID);
2022-05-12 11:27:31 +02:00
} else if (PWORKSPACE) {
PMONITOR = g_pCompositor->getMonitorFromID(PWORKSPACE->m_iMonitorID);
2022-05-12 11:27:31 +02:00
WLRBOXPREV = {(int)PMONITOR->vecPosition.x, (int)PMONITOR->vecPosition.y, (int)PMONITOR->vecSize.x, (int)PMONITOR->vecSize.y};
} else if (PLAYER) {
WLRBOXPREV = PLAYER->geometry;
PMONITOR = g_pCompositor->getMonitorFromVector(Vector2D(PLAYER->geometry.x, PLAYER->geometry.y) + Vector2D(PLAYER->geometry.width, PLAYER->geometry.height) / 2.f);
2022-05-12 11:27:31 +02:00
}
2022-04-23 14:16:02 +02:00
2022-04-23 21:47:16 +02:00
// beziers are with a switch unforto
2022-04-23 14:16:02 +02:00
// TODO: maybe do something cleaner
2022-04-23 21:47:16 +02:00
2022-04-23 14:16:02 +02:00
switch (av->m_eVarType) {
case AVARTYPE_FLOAT: {
2022-06-30 12:54:58 +02:00
// for disabled anims just warp
2022-07-28 13:28:43 +02:00
if (av->m_pConfig->pValues->internalEnabled == 0 || animationsDisabled) {
2022-06-30 12:54:58 +02:00
av->warp();
break;
2022-03-31 17:50:00 +02:00
}
2022-06-30 12:54:58 +02:00
if (SPENT >= 1.f) {
av->warp();
break;
}
2022-06-30 12:54:58 +02:00
const auto DELTA = av->m_fGoal - av->m_fBegun;
2022-07-28 13:28:43 +02:00
const auto BEZIER = m_mBezierCurves.find(av->m_pConfig->pValues->internalBezier);
2022-06-30 12:54:58 +02:00
if (BEZIER != m_mBezierCurves.end())
av->m_fValue = av->m_fBegun + BEZIER->second.getYForPoint(SPENT) * DELTA;
else
av->m_fValue = av->m_fBegun + DEFAULTBEZIER->second.getYForPoint(SPENT) * DELTA;
2022-04-23 14:16:02 +02:00
break;
2022-03-31 17:50:00 +02:00
}
2022-04-23 14:16:02 +02:00
case AVARTYPE_VECTOR: {
2022-06-30 12:54:58 +02:00
// for disabled anims just warp
2022-07-28 13:28:43 +02:00
if (av->m_pConfig->pValues->internalEnabled == 0 || animationsDisabled) {
2022-06-30 12:54:58 +02:00
av->warp();
break;
2022-03-31 17:50:00 +02:00
}
2022-06-30 12:54:58 +02:00
if (SPENT >= 1.f) {
av->warp();
break;
}
2022-06-30 12:54:58 +02:00
const auto DELTA = av->m_vGoal - av->m_vBegun;
2022-07-28 13:28:43 +02:00
const auto BEZIER = m_mBezierCurves.find(av->m_pConfig->pValues->internalBezier);
2022-06-30 12:54:58 +02:00
if (BEZIER != m_mBezierCurves.end())
av->m_vValue = av->m_vBegun + DELTA * BEZIER->second.getYForPoint(SPENT);
else
av->m_vValue = av->m_vBegun + DELTA * DEFAULTBEZIER->second.getYForPoint(SPENT);
2022-04-23 14:16:02 +02:00
break;
}
case AVARTYPE_COLOR: {
2022-06-30 12:54:58 +02:00
// for disabled anims just warp
2022-07-28 13:28:43 +02:00
if (av->m_pConfig->pValues->internalEnabled == 0 || animationsDisabled) {
2022-06-30 12:54:58 +02:00
av->warp();
break;
2022-04-23 14:16:02 +02:00
}
2022-06-30 12:54:58 +02:00
if (SPENT >= 1.f) {
av->warp();
break;
}
2022-06-30 12:54:58 +02:00
const auto DELTA = av->m_cGoal - av->m_cBegun;
2022-07-28 13:28:43 +02:00
const auto BEZIER = m_mBezierCurves.find(av->m_pConfig->pValues->internalBezier);
2022-06-30 12:54:58 +02:00
if (BEZIER != m_mBezierCurves.end())
av->m_cValue = av->m_cBegun + DELTA * BEZIER->second.getYForPoint(SPENT);
else
av->m_cValue = av->m_cBegun + DELTA * DEFAULTBEZIER->second.getYForPoint(SPENT);
2022-04-23 14:16:02 +02:00
break;
2022-03-23 22:01:59 +01:00
}
2022-04-23 14:36:03 +02:00
default: {
;
}
2022-03-23 22:01:59 +01:00
}
2022-04-14 16:43:29 +02:00
// damage the window with the damage policy
switch (av->m_eDamagePolicy) {
case AVARDAMAGE_ENTIRE: {
g_pHyprRenderer->damageBox(&WLRBOXPREV);
2022-05-12 11:27:31 +02:00
if (PWINDOW) {
2022-06-27 00:25:37 +02:00
PWINDOW->updateWindowDecos();
2022-08-05 19:23:53 +02:00
g_pHyprRenderer->damageWindow(PWINDOW);
2022-06-27 00:25:37 +02:00
} else if (PWORKSPACE) {
2022-06-30 15:44:26 +02:00
for (auto& w : g_pCompositor->m_vWindows) {
if (!w->m_bIsMapped || w->m_bHidden)
2022-06-27 00:25:37 +02:00
continue;
2022-06-30 15:44:26 +02:00
if (w->m_iWorkspaceID != PWORKSPACE->m_iID)
2022-06-27 00:25:37 +02:00
continue;
2022-06-30 15:44:26 +02:00
w->updateWindowDecos();
}
} else if (PLAYER) {
if (PLAYER->layer == ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND || PLAYER->layer == ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM)
g_pHyprOpenGL->markBlurDirtyForMonitor(PMONITOR);
}
break;
}
case AVARDAMAGE_BORDER: {
2022-05-12 11:27:31 +02:00
RASSERT(PWINDOW, "Tried to AVARDAMAGE_BORDER a non-window AVAR!");
// damage only the border.
static auto *const PROUNDING = &g_pConfigManager->getConfigValuePtr("decoration:rounding")->intValue;
const auto ROUNDINGSIZE = *PROUNDING + 1;
const auto BORDERSIZE = *PBORDERSIZE;
// damage for old box
2022-05-18 12:27:43 +02:00
g_pHyprRenderer->damageBox(WLRBOXPREV.x - BORDERSIZE, WLRBOXPREV.y - BORDERSIZE, WLRBOXPREV.width + 2 * BORDERSIZE, BORDERSIZE + ROUNDINGSIZE); // top
g_pHyprRenderer->damageBox(WLRBOXPREV.x - BORDERSIZE, WLRBOXPREV.y - BORDERSIZE, BORDERSIZE + ROUNDINGSIZE, WLRBOXPREV.height + 2 * BORDERSIZE); // left
g_pHyprRenderer->damageBox(WLRBOXPREV.x + WLRBOXPREV.width - ROUNDINGSIZE, WLRBOXPREV.y - BORDERSIZE, BORDERSIZE + ROUNDINGSIZE, WLRBOXPREV.height + 2 * BORDERSIZE); // right
g_pHyprRenderer->damageBox(WLRBOXPREV.x, WLRBOXPREV.y + WLRBOXPREV.height - ROUNDINGSIZE, WLRBOXPREV.width + 2 * BORDERSIZE, BORDERSIZE + ROUNDINGSIZE); // bottom
// damage for new box
const wlr_box WLRBOXNEW = {PWINDOW->m_vRealPosition.vec().x, PWINDOW->m_vRealPosition.vec().y, PWINDOW->m_vRealSize.vec().x, PWINDOW->m_vRealSize.vec().y};
2022-05-18 12:27:43 +02:00
g_pHyprRenderer->damageBox(WLRBOXNEW.x - BORDERSIZE, WLRBOXNEW.y - BORDERSIZE, WLRBOXNEW.width + 2 * BORDERSIZE, BORDERSIZE + ROUNDINGSIZE); // top
g_pHyprRenderer->damageBox(WLRBOXNEW.x - BORDERSIZE, WLRBOXNEW.y - BORDERSIZE, BORDERSIZE + ROUNDINGSIZE, WLRBOXNEW.height + 2 * BORDERSIZE); // left
g_pHyprRenderer->damageBox(WLRBOXNEW.x + WLRBOXNEW.width - ROUNDINGSIZE, WLRBOXNEW.y - BORDERSIZE, BORDERSIZE + ROUNDINGSIZE, WLRBOXNEW.height + 2 * BORDERSIZE); // right
g_pHyprRenderer->damageBox(WLRBOXNEW.x, WLRBOXNEW.y + WLRBOXNEW.height - ROUNDINGSIZE, WLRBOXNEW.width + 2 * BORDERSIZE, BORDERSIZE + ROUNDINGSIZE); // bottom
break;
} case AVARDAMAGE_SHADOW: {
RASSERT(PWINDOW, "Tried to AVARDAMAGE_SHADOW a non-window AVAR!");
static auto* const PSHADOWIGNOREWINDOW = &g_pConfigManager->getConfigValuePtr("decoration:shadow_ignore_window")->intValue;
const auto PDECO = PWINDOW->getDecorationByType(DECORATION_SHADOW);
if (PDECO) {
const auto EXTENTS = PDECO->getWindowDecorationExtents();
2022-08-05 19:23:53 +02:00
wlr_box dmg = {PWINDOW->m_vRealPosition.vec().x - EXTENTS.topLeft.x, PWINDOW->m_vRealPosition.vec().y - EXTENTS.topLeft.y,
PWINDOW->m_vRealSize.vec().x + EXTENTS.topLeft.x + EXTENTS.bottomRight.x, PWINDOW->m_vRealSize.vec().y + EXTENTS.topLeft.y + EXTENTS.bottomRight.y};
if (!*PSHADOWIGNOREWINDOW) {
// easy, damage the entire box
g_pHyprRenderer->damageBox(&dmg);
} else {
pixman_region32_t rg;
pixman_region32_init_rect(&rg, dmg.x, dmg.y, dmg.width, dmg.height);
pixman_region32_t wb;
pixman_region32_init_rect(&wb, PWINDOW->m_vRealPosition.vec().x, PWINDOW->m_vRealPosition.vec().y, PWINDOW->m_vRealSize.vec().x, PWINDOW->m_vRealSize.vec().y);
pixman_region32_subtract(&rg, &rg, &wb);
g_pHyprRenderer->damageRegion(&rg);
pixman_region32_fini(&rg);
pixman_region32_fini(&wb);
}
}
break;
}
default: {
Debug::log(ERR, "av has damage policy INVALID???");
break;
}
}
2022-04-27 16:34:28 +02:00
2022-05-05 15:16:10 +02:00
// set size and pos if valid, but only if damage policy entire (dont if border for example)
2022-07-08 11:24:07 +02:00
if (g_pCompositor->windowValidMapped(PWINDOW) && av->m_eDamagePolicy == AVARDAMAGE_ENTIRE && PWINDOW->m_iX11Type != 2)
2022-04-27 16:34:28 +02:00
g_pXWaylandManager->setWindowSize(PWINDOW, PWINDOW->m_vRealSize.goalv());
// manually schedule a frame
2022-07-13 18:18:23 +02:00
g_pCompositor->scheduleFrameForMonitor(PMONITOR);
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;
2022-05-14 16:43:30 +02:00
}
2022-05-15 14:18:31 +02:00
//
// Anims
//
//
2022-05-14 16:43:30 +02:00
2022-08-06 22:11:08 +02:00
void CAnimationManager::animationPopin(CWindow* pWindow, bool close, float minPerc) {
2022-05-15 14:18:31 +02:00
const auto GOALPOS = pWindow->m_vRealPosition.goalv();
const auto GOALSIZE = pWindow->m_vRealSize.goalv();
2022-05-14 16:43:30 +02:00
if (!close) {
2022-08-06 22:11:08 +02:00
pWindow->m_vRealSize.setValue((GOALSIZE * minPerc).clamp({5, 5}, {GOALSIZE.x, GOALSIZE.y}));
pWindow->m_vRealPosition.setValue(GOALPOS + GOALSIZE / 2.f - pWindow->m_vRealSize.m_vValue / 2.f);
} else {
2022-08-06 22:11:08 +02:00
pWindow->m_vRealSize = (GOALSIZE * minPerc).clamp({5, 5}, {GOALSIZE.x, GOALSIZE.y});
pWindow->m_vRealPosition = GOALPOS + GOALSIZE / 2.f - pWindow->m_vRealSize.m_vGoal / 2.f;
}
2022-05-15 14:18:31 +02:00
}
void CAnimationManager::animationSlide(CWindow* pWindow, std::string force, bool close) {
2022-05-15 14:18:31 +02:00
pWindow->m_vRealSize.warp(); // size we preserve in slide
2022-05-14 16:43:30 +02:00
const auto GOALPOS = pWindow->m_vRealPosition.goalv();
const auto GOALSIZE = pWindow->m_vRealSize.goalv();
2022-05-15 14:18:31 +02:00
const auto PMONITOR = g_pCompositor->getMonitorFromID(pWindow->m_iMonitorID);
Vector2D posOffset;
2022-05-15 14:18:31 +02:00
if (force != "") {
if (force == "bottom") posOffset = Vector2D(GOALPOS.x, PMONITOR->vecPosition.y + PMONITOR->vecSize.y);
else if (force == "left") posOffset = GOALPOS - Vector2D(GOALSIZE.x, 0);
else if (force == "right") posOffset = GOALPOS + Vector2D(GOALSIZE.x, 0);
else posOffset = Vector2D(GOALPOS.x, PMONITOR->vecPosition.y - GOALSIZE.y);
if (!close)
pWindow->m_vRealPosition.setValue(posOffset);
else
pWindow->m_vRealPosition = posOffset;
2022-05-15 14:18:31 +02:00
return;
}
const auto MIDPOINT = GOALPOS + GOALSIZE / 2.f;
// check sides it touches
const bool DISPLAYLEFT = STICKS(pWindow->m_vPosition.x, PMONITOR->vecPosition.x + PMONITOR->vecReservedTopLeft.x);
const bool DISPLAYRIGHT = STICKS(pWindow->m_vPosition.x + pWindow->m_vSize.x, PMONITOR->vecPosition.x + PMONITOR->vecSize.x - PMONITOR->vecReservedBottomRight.x);
const bool DISPLAYTOP = STICKS(pWindow->m_vPosition.y, PMONITOR->vecPosition.y + PMONITOR->vecReservedTopLeft.y);
const bool DISPLAYBOTTOM = STICKS(pWindow->m_vPosition.y + pWindow->m_vSize.y, PMONITOR->vecPosition.y + PMONITOR->vecSize.y - PMONITOR->vecReservedBottomRight.y);
2022-05-14 16:43:30 +02:00
2022-05-15 14:18:31 +02:00
if (DISPLAYBOTTOM && DISPLAYTOP) {
if (DISPLAYLEFT && DISPLAYRIGHT) {
posOffset = GOALPOS + Vector2D(0, GOALSIZE.y);
2022-05-15 14:18:31 +02:00
} else if (DISPLAYLEFT) {
posOffset = GOALPOS - Vector2D(GOALSIZE.x, 0);
2022-05-15 14:18:31 +02:00
} else {
posOffset = GOALPOS + Vector2D(GOALSIZE.x, 0);
2022-05-15 14:18:31 +02:00
}
} else if (DISPLAYTOP) {
posOffset = GOALPOS - Vector2D(0, GOALSIZE.y);
2022-05-15 14:18:31 +02:00
} else if (DISPLAYBOTTOM) {
posOffset = GOALPOS + Vector2D(0, GOALSIZE.y);
2022-05-15 14:18:31 +02:00
} else {
if (MIDPOINT.y > PMONITOR->vecPosition.y + PMONITOR->vecSize.y / 2.f)
posOffset = Vector2D(GOALPOS.x, PMONITOR->vecPosition.y + PMONITOR->vecSize.y);
2022-05-15 14:18:31 +02:00
else
posOffset = Vector2D(GOALPOS.x, PMONITOR->vecPosition.y - GOALSIZE.y);
2022-05-15 14:18:31 +02:00
}
if (!close)
pWindow->m_vRealPosition.setValue(posOffset);
else
pWindow->m_vRealPosition = posOffset;
2022-05-15 14:18:31 +02:00
}
void CAnimationManager::onWindowPostCreateClose(CWindow* pWindow, bool close) {
2022-07-28 13:28:43 +02:00
if (!close) {
pWindow->m_vRealPosition.m_pConfig = g_pConfigManager->getAnimationPropertyConfig("windowsIn");
pWindow->m_vRealSize.m_pConfig = g_pConfigManager->getAnimationPropertyConfig("windowsIn");
pWindow->m_fAlpha.m_pConfig = g_pConfigManager->getAnimationPropertyConfig("fadeIn");
} else {
pWindow->m_vRealPosition.m_pConfig = g_pConfigManager->getAnimationPropertyConfig("windowsOut");
pWindow->m_vRealSize.m_pConfig = g_pConfigManager->getAnimationPropertyConfig("windowsOut");
pWindow->m_fAlpha.m_pConfig = g_pConfigManager->getAnimationPropertyConfig("fadeOut");
}
auto ANIMSTYLE = pWindow->m_vRealPosition.m_pConfig->pValues->internalStyle;
2022-05-15 14:18:31 +02:00
transform(ANIMSTYLE.begin(), ANIMSTYLE.end(), ANIMSTYLE.begin(), ::tolower);
2022-05-14 16:43:30 +02:00
2022-05-15 14:18:31 +02:00
// if the window is not being animated, that means the layout set a fixed size for it, don't animate.
if (!pWindow->m_vRealPosition.isBeingAnimated() && !pWindow->m_vRealSize.isBeingAnimated())
return;
2022-05-14 16:43:30 +02:00
2022-05-15 14:18:31 +02:00
if (pWindow->m_sAdditionalConfigData.animationStyle != "") {
// the window has config'd special anim
if (pWindow->m_sAdditionalConfigData.animationStyle.find("slide") == 0) {
if (pWindow->m_sAdditionalConfigData.animationStyle.contains(' ')) {
2022-05-15 14:18:31 +02:00
// has a direction
animationSlide(pWindow, pWindow->m_sAdditionalConfigData.animationStyle.substr(pWindow->m_sAdditionalConfigData.animationStyle.find(' ') + 1), close);
2022-05-14 16:43:30 +02:00
} else {
animationSlide(pWindow, "", close);
2022-05-14 16:43:30 +02:00
}
} else {
2022-05-15 14:18:31 +02:00
// anim popin, fallback
2022-08-06 22:11:08 +02:00
float minPerc = 0.f;
if (pWindow->m_sAdditionalConfigData.animationStyle.find("%") != 0) {
try {
auto percstr = pWindow->m_sAdditionalConfigData.animationStyle.substr(pWindow->m_sAdditionalConfigData.animationStyle.find_last_of(' '));
minPerc = std::stoi(percstr.substr(0, percstr.length() - 1));
} catch (std::exception& e) {
; // oops
}
}
animationPopin(pWindow, close, minPerc / 100.f);
2022-05-14 16:43:30 +02:00
}
} else {
2022-05-15 14:18:31 +02:00
if (ANIMSTYLE == "slide") {
animationSlide(pWindow, "", close);
2022-05-15 14:18:31 +02:00
} else {
// anim popin, fallback
2022-08-06 22:11:08 +02:00
float minPerc = 0.f;
if (ANIMSTYLE.find("%") != 0) {
try {
auto percstr = ANIMSTYLE.substr(ANIMSTYLE.find_last_of(' '));
minPerc = std::stoi(percstr.substr(0, percstr.length() - 1));
} catch (std::exception& e) {
; // oops
}
}
animationPopin(pWindow, close, minPerc / 100.f);
2022-05-15 14:18:31 +02:00
}
2022-05-14 16:43:30 +02:00
}
2022-03-23 22:01:59 +01:00
}