From e85506f71237172e6b981716b9034d3489e1f0d2 Mon Sep 17 00:00:00 2001 From: opencode Date: Wed, 2 Dec 2026 08:03:42 +0000 Subject: [PATCH] fix: absolute-cursor weapon selection, detonator art fix, feathered ring edges (issue #31) --- src/extras/weaponwheel.cpp | 205 ++++++++++++++++++++++++++++--------- 1 file changed, 158 insertions(+), 47 deletions(-) diff --git a/src/extras/weaponwheel.cpp b/src/extras/weaponwheel.cpp index d7afdc13..55c8b96e 100644 --- a/src/extras/weaponwheel.cpp +++ b/src/extras/weaponwheel.cpp @@ -93,13 +93,16 @@ enum #define WHEEL_ART_CENTRE_Y ( 30.67f / 1080.0f) #define WHEEL_STICK_DEADZONE 0.35f -#define WHEEL_MOUSE_DEADZONE 0.35f #define WHEEL_KEY_DEADZONE 0.50f -// Virtual-cursor gain per mouse pixel. Nudge between 0.004 and 0.006. -#define WHEEL_MOUSE_SENSITIVITY 0.005f -// First-order smoothing of the displayed cursor toward the accumulated target. -#define WHEEL_MOUSE_SMOOTHING 0.30f -#define WHEEL_CURSOR_CLAMP 1.0f +// Weapon wheel: absolute virtual cursor, 1:1 with the raw mouse delta. The +// engine disables the OS cursor during gameplay (GLFW_CURSOR_DISABLED), so there +// is no usable absolute window position; the cursor starts at screen centre and +// every mouse pixel moves it one screen pixel. The pointer must leave this +// radius (a few % of screen height) before it selects a segment. +#define WHEEL_CURSOR_DEADZONE 0.06f +#define WHEEL_CURSOR_ARM 0.012f // marker arm length, fraction of screen height +#define WHEEL_CURSOR_THICK 0.0018f // marker thickness, fraction of screen height +#define WHEEL_RING_FEATHER 0.0015f // alpha-feather band on the procedural ring edges #define WHEEL_ART_WW_PATH "weapon_wheel/ww%d.png" #define WHEEL_ART_ICON_PATH "weapon_wheel/icon%d.png" @@ -132,8 +135,7 @@ static int32 ms_nSegments; static int32 ms_nHovered; // -1 means "nothing pointed at" static int32 ms_nCurrentSegment; // segment of the currently equipped entry static bool ms_bSelectionMoved; // radio wheel: player actually cycled -static float ms_fCursorX, ms_fCursorY; // smoothed virtual cursor -static float ms_fMouseTargetX, ms_fMouseTargetY; // accumulated mouse target +static float ms_fCursorX, ms_fCursorY; // absolute virtual mouse cursor (screen px) static bool ms_bCameraWasDisabled; // bit was already set when a weapon wheel opened static bool ms_bOwnCameraBit; // this wheel set PLAYERCONTROL_CAMERA #ifdef IMPROVED_WEAPON_WHEEL_SLOWMO @@ -388,6 +390,22 @@ Wheel_SelectedWeapon(CPlayerPed *player, int32 segment) return Wheel_FirstWeaponInSegment(player, segment); } +// Segment 11 is the only one whose art frame (grenade) is not the only +// inventory type it can select: the detonator shares it. Using the grenade +// glyph and the full-white wwN wedge for a detonator would show a weapon the +// player does not have over a bright background, so those use the game's +// detonator sprite and the procedural gold sector instead. +static bool +Wheel_SegmentArtMatches(CPlayerPed *player, int32 segment) +{ + int32 weapon = Wheel_SelectedWeapon(player, segment); + if (weapon < 0) + return false; + if (segment == WEAPONTYPE_GRENADE) + return weapon == WEAPONTYPE_GRENADE; + return true; +} + static void Wheel_CycleSegment(CPlayerPed *player) { @@ -594,10 +612,10 @@ Wheel_Open(bool radio) ms_bOpen = true; ms_bRadio = radio; ms_bSelectionMoved = false; - ms_fCursorX = 0.0f; - ms_fCursorY = 0.0f; - ms_fMouseTargetX = 0.0f; - ms_fMouseTargetY = 0.0f; + // Absolute virtual cursor starts at the centre; the seeded segment stays + // selected until the pointer leaves the deadzone. + ms_fCursorX = SCREEN_WIDTH * 0.5f; + ms_fCursorY = SCREEN_HEIGHT * 0.5f; if (radio) { Wheel_BuildRadioList(); @@ -751,29 +769,34 @@ Wheel_UpdateInput(void) bestMag = mag; } - // Mouse: only deltas are available. Read the raw pad field directly: - // NewMouseControllerState.x/y is the un-scaled OS delta. The in-game mouse - // sensitivity (TheCamera::m_fMouseAccelHorzntl/Vertical) is applied later at - // the camera look sites, not here, so WHEEL_MOUSE_SENSITIVITY is the only - // gain in this path and the wheel feels the same at every sensitivity - // setting. Accumulate a virtual cursor target, then lerp the displayed - // cursor toward it so pointing is smooth rather than twitchy. - ms_fMouseTargetX += pad->NewMouseControllerState.x * WHEEL_MOUSE_SENSITIVITY; - ms_fMouseTargetY += pad->NewMouseControllerState.y * WHEEL_MOUSE_SENSITIVITY; - if (ms_fMouseTargetX < -WHEEL_CURSOR_CLAMP) ms_fMouseTargetX = -WHEEL_CURSOR_CLAMP; - if (ms_fMouseTargetX > WHEEL_CURSOR_CLAMP) ms_fMouseTargetX = WHEEL_CURSOR_CLAMP; - if (ms_fMouseTargetY < -WHEEL_CURSOR_CLAMP) ms_fMouseTargetY = -WHEEL_CURSOR_CLAMP; - if (ms_fMouseTargetY > WHEEL_CURSOR_CLAMP) ms_fMouseTargetY = WHEEL_CURSOR_CLAMP; + // Mouse: absolute virtual cursor, 1:1 with the raw pad delta. The engine + // keeps the OS cursor disabled during gameplay (GLFW_CURSOR_DISABLED), so + // there is no usable absolute window position: m_nMouseTempPosX/Y is only + // updated while the menu is active. Starting at screen centre and moving + // one screen pixel per mouse pixel gives precise, repeatable pointing with + // no gain, smoothing or dead-band lag. The selection deadzone is + // WHEEL_CURSOR_DEADZONE of screen height, measured from the centre. + // NewMouseControllerState.x/y is the raw OS delta, but UpdateMouse has + // already applied the player's look-inversion flags. A pointing UI must + // follow the physical mouse, so undo that inversion here (the ini on the + // test machine has InvertMouseVertically=1). + float mouseDX = pad->NewMouseControllerState.x; + float mouseDY = pad->NewMouseControllerState.y; + if (MousePointerStateHelper.bInvertHorizontally) + mouseDX = -mouseDX; + if (MousePointerStateHelper.bInvertVertically) + mouseDY = -mouseDY; - ms_fCursorX += (ms_fMouseTargetX - ms_fCursorX) * WHEEL_MOUSE_SMOOTHING; - ms_fCursorY += (ms_fMouseTargetY - ms_fCursorY) * WHEEL_MOUSE_SMOOTHING; + ms_fCursorX += mouseDX; + ms_fCursorY += mouseDY; + if (ms_fCursorX < 0.0f) ms_fCursorX = 0.0f; + if (ms_fCursorX > SCREEN_WIDTH) ms_fCursorX = SCREEN_WIDTH; + if (ms_fCursorY < 0.0f) ms_fCursorY = 0.0f; + if (ms_fCursorY > SCREEN_HEIGHT) ms_fCursorY = SCREEN_HEIGHT; - mag = Sqrt(ms_fCursorX * ms_fCursorX + ms_fCursorY * ms_fCursorY); - if (mag > bestMag && mag >= WHEEL_MOUSE_DEADZONE) { - bestX = ms_fCursorX; - bestY = ms_fCursorY; - bestMag = mag; - } + float cursorDX = ms_fCursorX - SCREEN_WIDTH * 0.5f; + float cursorDY = ms_fCursorY - SCREEN_HEIGHT * 0.5f; // screen y is down; helper flips it + float cursorDist = Sqrt(cursorDX * cursorDX + cursorDY * cursorDY); // Keyboard direction (arrows or WASD), normalised so it competes with the // analogue sources fairly. @@ -893,36 +916,60 @@ Wheel_DrawRingQuad(float a0, float a1, float r0, float r1, const CRGBA &color) color); } +static void +Wheel_DrawRingQuadFade(float a0, float a1, float rSolid, float rFade, const CRGBA &color) +{ + // Assumes the caller has set up 2D rendering state (GOURAUD, alpha blend, + // no texture). rSolid gets the sector colour, rFade is fully transparent. + float cx = SCREEN_WIDTH * 0.5f; + float cy = SCREEN_HEIGHT * 0.5f; + CRGBA clear(color.r, color.g, color.b, 0); + + float xA = cx + rSolid * Cos(a0), yA = cy - rSolid * Sin(a0); + float xB = cx + rFade * Cos(a0), yB = cy - rFade * Sin(a0); + float xC = cx + rFade * Cos(a1), yC = cy - rFade * Sin(a1); + float xD = cx + rSolid * Cos(a1), yD = cy - rSolid * Sin(a1); + + // SetVertices maps (x1,c0)->v3, (x2,c1)->v2, (x3,c2)->v0, (x4,c3)->v1. + CSprite2d::SetVertices(xD, yD, xC, yC, xA, yA, xB, yB, + color, clear, color, clear); + RwIm2DRenderPrimitive(rwPRIMTYPETRIFAN, CSprite2d::GetVertices(), 4); +} + static void Wheel_DrawRing(void) { - if (ms_nSegments <= 0) + if (ms_nSegments <= 0 || ms_nSegments > WHEEL_MAX_RADIO) return; float outer = SCREEN_HEIGHT * WHEEL_OUTER_RADIUS; float inner = SCREEN_HEIGHT * WHEEL_INNER_RADIUS; + float feather = SCREEN_HEIGHT * WHEEL_RING_FEATHER; float step = TWOPI / ms_nSegments; CPlayerPed *player = FindPlayerPed(); int32 highlight = ms_nHovered >= 0 ? ms_nHovered : ms_nCurrentSegment; + bool draw[WHEEL_MAX_RADIO]; + CRGBA color[WHEEL_MAX_RADIO]; for (int32 i = 0; i < ms_nSegments; i++) { bool available = true; if (!ms_bRadio) - available = player != nil && Wheel_FirstWeaponInSegment(player, i) >= 0; + available = player != nil && Wheel_SelectedWeapon(player, i) >= 0; // In art mode the selected sector is filled by the smooth wwN overlay. // Drawing the hard-edged procedural sector underneath it leaves a // jagged rim peeking past the texture's anti-aliased edge, so skip it. - if (!ms_bRadio && i == highlight && available && ms_apWwArt[i] != nil) + draw[i] = !(!ms_bRadio && i == highlight && available && ms_apWwArt[i] != nil + && Wheel_SegmentArtMatches(player, i)); + if (!draw[i]) continue; - CRGBA color; if (!available) - color = CRGBA(10, 10, 10, 70); + color[i] = CRGBA(10, 10, 10, 70); else if (i == highlight) - color = CRGBA(235, 200, 120, 225); + color[i] = CRGBA(235, 200, 120, 225); else - color = CRGBA(18, 18, 18, 150); + color[i] = CRGBA(18, 18, 18, 150); float center = HALFPI - i * step; float a0 = center + step * 0.5f; @@ -931,9 +978,38 @@ Wheel_DrawRing(void) for (int32 k = 0; k < WHEEL_ARC_STEPS; k++) { float aa = a0 + (a1 - a0) * (k / (float)WHEEL_ARC_STEPS); float ab = a0 + (a1 - a0) * ((k + 1) / (float)WHEEL_ARC_STEPS); - Wheel_DrawRingQuad(aa, ab, inner, outer, color); + Wheel_DrawRingQuad(aa, ab, inner + feather, outer - feather, color[i]); } } + + // Alpha-feather the inner/outer circumference instead of leaving the + // un-antialiased polygon edges: the rest of the 2D pass has no MSAA. + RwRenderStateSet(rwRENDERSTATETEXTURERASTER, nil); + RwRenderStateSet(rwRENDERSTATESHADEMODE, (void*)rwSHADEMODEGOURAUD); + RwRenderStateSet(rwRENDERSTATEZTESTENABLE, (void*)FALSE); + RwRenderStateSet(rwRENDERSTATEZWRITEENABLE, (void*)FALSE); + RwRenderStateSet(rwRENDERSTATEVERTEXALPHAENABLE, (void*)TRUE); + RwRenderStateSet(rwRENDERSTATESRCBLEND, (void*)rwBLENDSRCALPHA); + RwRenderStateSet(rwRENDERSTATEDESTBLEND, (void*)rwBLENDINVSRCALPHA); + + for (int32 i = 0; i < ms_nSegments; i++) { + if (!draw[i]) + continue; + + float center = HALFPI - i * step; + float a0 = center + step * 0.5f; + float a1 = center - step * 0.5f; + + for (int32 k = 0; k < WHEEL_ARC_STEPS; k++) { + float aa = a0 + (a1 - a0) * (k / (float)WHEEL_ARC_STEPS); + float ab = a0 + (a1 - a0) * ((k + 1) / (float)WHEEL_ARC_STEPS); + Wheel_DrawRingQuadFade(aa, ab, inner + feather, inner, color[i]); + Wheel_DrawRingQuadFade(aa, ab, outer - feather, outer, color[i]); + } + } + + RwRenderStateSet(rwRENDERSTATEZTESTENABLE, (void*)TRUE); + RwRenderStateSet(rwRENDERSTATEZWRITEENABLE, (void*)TRUE); } static void @@ -994,24 +1070,33 @@ Wheel_DrawWeaponIcons(void) float iconScale = selected ? 1.18f : 1.0f; uint8 alpha = available ? (selected ? 255 : 150) : 40; - // Prefer the art glyph; fall back to the game's HUD weapon sprite. - if (ms_apIconArt[i] != nil) { + // Prefer the art glyph; the detonator has no art frame (it shares the + // grenade segment), so it uses the game's detonator sprite. Fall back + // to the HUD sprite when the art file is missing. + bool useArt = ms_apIconArt[i] != nil && weapon != WEAPONTYPE_DETONATOR; + if (useArt) { Wheel_DrawArt(ms_apIconArt[i], ms_aIconOffX[i], ms_aIconOffY[i], ms_aIconW[i] * iconScale, ms_aIconH[i] * iconScale, CRGBA(255, 255, 255, alpha)); } else { + int32 sprite = i; + if (weapon == WEAPONTYPE_DETONATOR) + sprite = HUD_DETONATOR; + else if (weapon >= 0) + sprite = weapon; + float angle = HALFPI - i * step; float x = cx + radius * Cos(angle); float y = cy - radius * Sin(angle); float s = size * iconScale; - CHud::Sprites[i].Draw( + CHud::Sprites[sprite].Draw( CRect(x - s * 0.5f, y - s * 0.5f, x + s * 0.5f, y + s * 0.5f), CRGBA(255, 255, 255, alpha)); // In procedural mode the sprite alone is not always legible, so // label each segment. Art mode carries readable glyphs already. - Wheel_DrawSegmentLabel(i, ms_aWeaponShort[i], WHEEL_WEAPON_LABEL_R, - CRGBA(200, 200, 200, selected ? 255 : 170)); + Wheel_DrawSegmentLabel(i, weapon >= 0 ? ms_aWeaponLong[weapon] : ms_aWeaponShort[i], + WHEEL_WEAPON_LABEL_R, CRGBA(200, 200, 200, selected ? 255 : 170)); } } } @@ -1031,6 +1116,29 @@ Wheel_DrawRadio(void) } } +static void +Wheel_DrawMouseCursor(void) +{ + if (ms_bRadio) + return; + + float arm = SCREEN_HEIGHT * WHEEL_CURSOR_ARM; + float thick = Max(1.0f, SCREEN_HEIGHT * WHEEL_CURSOR_THICK); + CRGBA shadow(0, 0, 0, 200); + CRGBA white(255, 255, 255, 235); + + // Dark outline first, then the white cross, so it reads on the dimmed + // background and over the art. + CSprite2d::DrawRect(CRect(ms_fCursorX - arm - 1.0f, ms_fCursorY - thick - 1.0f, + ms_fCursorX + arm + 1.0f, ms_fCursorY + thick + 1.0f), shadow); + CSprite2d::DrawRect(CRect(ms_fCursorX - thick - 1.0f, ms_fCursorY - arm - 1.0f, + ms_fCursorX + thick + 1.0f, ms_fCursorY + arm + 1.0f), shadow); + CSprite2d::DrawRect(CRect(ms_fCursorX - arm, ms_fCursorY - thick, + ms_fCursorX + arm, ms_fCursorY + thick), white); + CSprite2d::DrawRect(CRect(ms_fCursorX - thick, ms_fCursorY - arm, + ms_fCursorX + thick, ms_fCursorY + arm), white); +} + void Wheel_Draw(void) { @@ -1063,7 +1171,8 @@ Wheel_Draw(void) // the wedge is full-white and would make the greyed-out entry look // selected/white. The dim procedural sector is enough feedback. bool highlightAvailable = highlight >= 0 && highlight < WHEEL_SEGMENTS - && player != nil && Wheel_FirstWeaponInSegment(player, highlight) >= 0; + && player != nil && Wheel_SelectedWeapon(player, highlight) >= 0 + && Wheel_SegmentArtMatches(player, highlight); if (highlightAvailable && ms_apWwArt[highlight] != nil) Wheel_DrawArt(ms_apWwArt[highlight], ms_aWwOffX[highlight], ms_aWwOffY[highlight], ms_aWwW[highlight], ms_aWwH[highlight], CRGBA(255, 255, 255, 255)); @@ -1108,6 +1217,8 @@ Wheel_Draw(void) Wheel_DrawCentredText(cx, cy - SCREEN_SCALE_Y(4.0f), text, 0.62f, 1.05f, CRGBA(200, 200, 200, 220)); } } + + Wheel_DrawMouseCursor(); } bool