From 5943ae34275090e255db11328973d74c700cc0ec Mon Sep 17 00:00:00 2001 From: DawnFire42 Date: Wed, 4 Mar 2026 14:31:02 -0500 Subject: [PATCH] refactor(z-values): removed redundant inline with contexpr, Updated doc, magic numbers removed from TableZone. --- cockatrice/src/game/z_value_layer_manager.h | 6 ++-- cockatrice/src/game/z_values.h | 36 +++++++++++++++++---- cockatrice/src/game/zones/table_zone.cpp | 5 +-- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/cockatrice/src/game/z_value_layer_manager.h b/cockatrice/src/game/z_value_layer_manager.h index 5bf316dfd..303edb8a5 100644 --- a/cockatrice/src/game/z_value_layer_manager.h +++ b/cockatrice/src/game/z_value_layer_manager.h @@ -102,7 +102,7 @@ constexpr qreal OVERLAY_BASE = 2000000000.0; * @param zValue The Z-value to validate * @return true if the Z-value is valid for a card */ -[[nodiscard]] inline constexpr bool isValidCardZValue(qreal zValue) +[[nodiscard]] constexpr bool isValidCardZValue(qreal zValue) { return zValue >= 1.0 && zValue <= CARD_Z_VALUE_MAX; } @@ -115,7 +115,7 @@ constexpr qreal OVERLAY_BASE = 2000000000.0; * @param zValue The Z-value to validate * @return true if the Z-value is valid for an overlay element */ -[[nodiscard]] inline constexpr bool isOverlayZValue(qreal zValue) +[[nodiscard]] constexpr bool isOverlayZValue(qreal zValue) { return zValue >= OVERLAY_BASE; } @@ -126,7 +126,7 @@ constexpr qreal OVERLAY_BASE = 2000000000.0; * @param offset Offset from OVERLAY_BASE (0-7 for current elements) * @return The absolute Z-value for the overlay element */ -[[nodiscard]] inline constexpr qreal overlayZValue(qreal offset) +[[nodiscard]] constexpr qreal overlayZValue(qreal offset) { return OVERLAY_BASE + offset; } diff --git a/cockatrice/src/game/z_values.h b/cockatrice/src/game/z_values.h index 0dd901189..2c7fe9066 100644 --- a/cockatrice/src/game/z_values.h +++ b/cockatrice/src/game/z_values.h @@ -39,17 +39,41 @@ constexpr qreal DRAG_ITEM = ZValueLayerManager::overlayZValue(5.0); constexpr qreal DRAG_ITEM_CHILD = ZValueLayerManager::overlayZValue(6.0); constexpr qreal TOP_UI = ZValueLayerManager::overlayZValue(7.0); -/// Compute Z-value for child drag items based on hotspot position. -/// When dragging multiple cards together, each child card needs a unique Z-value -/// to prevent Z-fighting (flickering/flashing). The Z-values are derived from -/// their position when grabbed to conserve original stacking. The formula encodes -/// 2D coordinates into a single value where X has higher weight, ensuring -/// deterministic visual stacking. +/** + * @brief Compute Z-value for child drag items based on hotspot position. + * + * When dragging multiple cards together, each child card needs a unique Z-value + * to prevent Z-fighting (flickering/flashing). The Z-values are derived from + * their position when grabbed to conserve original stacking. The formula encodes + * 2D coordinates into a single value where X has higher weight, ensuring + * deterministic visual stacking. + * + * @param hotSpotX The X coordinate of the grab position + * @param hotSpotY The Y coordinate of the grab position + * @return Unique Z-value for the child drag item + */ [[nodiscard]] constexpr qreal childDragZValue(qreal hotSpotX, qreal hotSpotY) { return DRAG_ITEM_CHILD + hotSpotX * 1000000 + hotSpotY * 1000 + 1000; } +/** + * @brief Compute Z-value for cards on the table zone based on position. + * + * Cards lower on the table (higher Y) render above cards higher up, + * and cards to the right (higher X) render above cards to the left. + * This creates natural visual stacking for overlapping cards. + * + * @param x The X coordinate of the card position + * @param y The Y coordinate of the card position + * @return Z-value for the card's table position + */ +[[nodiscard]] constexpr qreal tableCardZValue(qreal x, qreal y) +{ + constexpr qreal CARD_HEIGHT_FOR_Z = 102.0; + return (y + CARD_HEIGHT_FOR_Z) * 100000.0 + (x + 1) * 100.0; +} + // Card layering (general architecture, not command-zone specific) constexpr qreal CARD_BASE = 1.0; constexpr qreal CARD_MAX = ZValueLayerManager::CARD_Z_VALUE_MAX; diff --git a/cockatrice/src/game/zones/table_zone.cpp b/cockatrice/src/game/zones/table_zone.cpp index c76514350..0cfb4d2de 100644 --- a/cockatrice/src/game/zones/table_zone.cpp +++ b/cockatrice/src/game/zones/table_zone.cpp @@ -7,6 +7,7 @@ #include "../board/card_item.h" #include "../player/player.h" #include "../player/player_actions.h" +#include "../z_values.h" #include "logic/table_zone_logic.h" #include @@ -167,7 +168,7 @@ void TableZone::reorganizeCards() actualY += 15; getLogic()->getCards()[i]->setPos(actualX, actualY); - getLogic()->getCards()[i]->setRealZValue((actualY + CARD_HEIGHT) * 100000 + (actualX + 1) * 100); + getLogic()->getCards()[i]->setRealZValue(ZValues::tableCardZValue(actualX, actualY)); QListIterator attachedCardIterator(getLogic()->getCards()[i]->getAttachedCards()); int j = 0; @@ -177,7 +178,7 @@ void TableZone::reorganizeCards() qreal childX = actualX - j * STACKED_CARD_OFFSET_X; qreal childY = y + 5; attachedCard->setPos(childX, childY); - attachedCard->setRealZValue((childY + CARD_HEIGHT) * 100000 + (childX + 1) * 100); + attachedCard->setRealZValue(ZValues::tableCardZValue(childX, childY)); } }