diff --git a/src/extras/weaponwheel.cpp b/src/extras/weaponwheel.cpp new file mode 100644 index 00000000..008430d1 --- /dev/null +++ b/src/extras/weaponwheel.cpp @@ -0,0 +1,908 @@ +/* + * GTA V-style weapon and radio wheels (issue #31). + * + * This file is custom UI, not reversed code: it only exists when + * IMPROVED_WEAPON_WHEEL is defined (src/core/config.h) and it lives in the + * sanctioned src/extras/ layer. Guard-off builds do not compile it. + * + * Interaction (both wheels share one state machine and one radial primitive): + * weapon wheel keyboard+mouse: hold Tab, point with the mouse (or WASD/arrows) + * gamepad: hold R2, point with the right stick + * radio wheel keyboard+mouse: hold R while in a radio-equipped vehicle + * gamepad: hold L2 while in a radio-equipped vehicle + * Releasing applies the highlighted entry. Mouse wheel or D-pad left/right + * cycles the entry inside one segment (only the grenade/detonator segment has + * more than one entry). + * + * While a wheel is open PLAYERCONTROL_CAMERA is set so the mouse/right stick + * does not rotate the camera or fire; time is NOT slowed by default (see + * IMPROVED_WEAPON_WHEEL_SLOWMO). + * + * Optional art: `weapon_wheel/wwN.png` and `weapon_wheel/iconN.png` next to the + * executable. They are third-party assets and are never shipped with the + * repository. Anything missing or unreadable falls back to the procedural + * wheel, so the feature needs no data files at all. + */ + +#include "common.h" + +#ifdef IMPROVED_WEAPON_WHEEL + +#include "weaponwheel.h" + +#include "Camera.h" +#include "DMAudio.h" +#include "Font.h" +#include "Frontend.h" +#include "Game.h" +#include "Hud.h" +#include "Lists.h" +#include "MusicManager.h" +#include "Pad.h" +#include "PlayerInfo.h" +#include "PlayerPed.h" +#include "Replay.h" +#include "Sprite2d.h" +#include "Text.h" +#include "Timer.h" +#include "Vehicle.h" +#include "audio_enums.h" +#include "crossplatform.h" +#include "rtpng.h" + +extern wchar WideErrorString[]; + +enum +{ + WHEEL_SEGMENTS = 12, // one segment per eWeaponType 0..11 (detonator shares 11) + WHEEL_ARC_STEPS = 4, // quads per segment, only for the procedural ring + WHEEL_MAX_RADIO = 12 +}; + +// Radii as a fraction of screen height so the wheel stays circular and matches +// the 1080p art (wedge outer ~305px, inner ~169px, icons ~246px from centre). +#define WHEEL_OUTER_RADIUS 0.285f +#define WHEEL_INNER_RADIUS 0.155f +#define WHEEL_ICON_RADIUS 0.228f +#define WHEEL_ICON_SIZE 0.100f +#define WHEEL_RADIO_TEXT_R 0.220f +#define WHEEL_WEAPON_LABEL_R 0.335f + +#define WHEEL_STICK_DEADZONE 0.35f +#define WHEEL_MOUSE_DEADZONE 0.35f +#define WHEEL_KEY_DEADZONE 0.50f +#define WHEEL_MOUSE_SENS 0.015f +#define WHEEL_CURSOR_CLAMP 1.0f + +#define WHEEL_ART_WW_PATH "weapon_wheel/ww%d.png" +#define WHEEL_ART_ICON_PATH "weapon_wheel/icon%d.png" + +static const char *ms_aWeaponShort[WHEEL_SEGMENTS] = { + "FIST", "BAT", "PISTOL", "UZI", "SHOTGUN", "AK47", + "M16", "SNIPER", "ROCKET", "FLAME", "MOLOTOV", "GRENADE" +}; + +static const char *ms_aWeaponLong[] = { + "UNARMED", "BASEBALL BAT", "COLT .45", "UZI", "SHOTGUN", "AK47", + "M16", "SNIPER RIFLE", "ROCKET LAUNCHER", "FLAMETHROWER", "MOLOTOV", + "GRENADE", "DETONATOR" +}; + +// Radio stations that fit on the wheel, in segment order. USERTRACK and +// RADIO_OFF are appended while opening, depending on availability. +static const uint8 ms_aRadioBase[] = { + HEAD_RADIO, DOUBLE_CLEF, JAH_RADIO, RISE_FM, LIPS_106, + GAME_FM, MSX_FM, FLASHBACK, CHATTERBOX +}; +static const char *ms_aRadioShort[] = { + "HEAD", "DOUBLE CLEF", "K-JAH", "RISE", "LIPS", "GAME FM", + "MSX", "FLASHBACK", "CHATTERBOX", "MP3", "RADIO OFF" +}; + +static bool ms_bOpen; +static bool ms_bRadio; +static int32 ms_nSegments; +static int32 ms_nHovered; // -1 means "nothing pointed at" +static int32 ms_nCurrentSegment; // segment of the currently equipped entry +static float ms_fCursorX, ms_fCursorY; +static bool ms_bCameraWasDisabled; +#ifdef IMPROVED_WEAPON_WHEEL_SLOWMO +static float ms_fSavedTimeScale; +#endif + +// Remember the last weapon chosen per segment (GTA V remembers per category). +static int8 ms_aLastWeapon[WHEEL_SEGMENTS]; + +// Radio wheel contents, rebuilt on open. +static uint8 ms_aRadioStation[WHEEL_MAX_RADIO]; +static const char *ms_aRadioLabel[WHEEL_MAX_RADIO]; +static int32 ms_nRadioCount; + +// Optional art, loaded lazily and never freed (process lifetime, like other +// static UI state; avoids static-destructor ordering issues with RenderWare). +static bool ms_bArtTried; +static CSprite2d *ms_apWwArt[WHEEL_SEGMENTS]; +static float ms_aWwOffX[WHEEL_SEGMENTS], ms_aWwOffY[WHEEL_SEGMENTS]; +static float ms_aWwW[WHEEL_SEGMENTS], ms_aWwH[WHEEL_SEGMENTS]; +static CSprite2d *ms_apIconArt[WHEEL_SEGMENTS]; +static float ms_aIconOffX[WHEEL_SEGMENTS], ms_aIconOffY[WHEEL_SEGMENTS]; +static float ms_aIconW[WHEEL_SEGMENTS], ms_aIconH[WHEEL_SEGMENTS]; + +// --------------------------------------------------------------------------- +// Art loading +// --------------------------------------------------------------------------- + +// Read a PNG through the tree's case-resolving PNG path (RtPNGImageRead -> +// casepath on POSIX), crop it to the non-transparent bounding box so we don't +// keep twelve 1920x1080 textures in memory, and remember where the crop sat +// relative to the art's centre in units of the art height. +static CSprite2d * +Wheel_LoadArt(const char *path, float *pOffX, float *pOffY, float *pW, float *pH) +{ + // RtPNGImageRead asserts on a missing file in debug builds, so probe first. + // This is also the case-insensitive probe on POSIX (fcaseopen -> casepath). + FILE *file = fcaseopen(path, "rb"); + if (file == nil) + return nil; + uint8 magic[8]; + size_t got = fread(magic, 1, sizeof(magic), file); + fclose(file); + if (got != sizeof(magic) + || magic[0] != 0x89 || magic[1] != 'P' || magic[2] != 'N' || magic[3] != 'G') + return nil; + + RwImage *img = RtPNGImageRead(path); + if (img == nil) + return nil; + + int32 iw = RwImageGetWidth(img); + int32 ih = RwImageGetHeight(img); + uint8 *pixels = RwImageGetPixels(img); + int32 stride = RwImageGetStride(img); + + // The wheel art is RGBA32; anything else is rejected and falls back. + if (pixels == nil || iw <= 0 || ih <= 0 || RwImageGetDepth(img) != 32 + || stride < iw * 4) { + RwImageDestroy(img); + return nil; + } + + int32 minX = iw, minY = ih, maxX = -1, maxY = -1; + for (int32 y = 0; y < ih; y++) { + uint8 *row = pixels + y * stride; + for (int32 x = 0; x < iw; x++) { + if (row[x * 4 + 3] > 0) { + if (x < minX) minX = x; + if (x > maxX) maxX = x; + if (y < minY) minY = y; + if (y > maxY) maxY = y; + } + } + } + + if (maxX < minX || maxY < minY) { + RwImageDestroy(img); + return nil; + } + + int32 cw = maxX - minX + 1; + int32 ch = maxY - minY + 1; + + RwImage *crop = RwImageCreate(cw, ch, 32); + if (crop == nil) { + RwImageDestroy(img); + return nil; + } + RwImageAllocatePixels(crop); + + uint8 *dst = RwImageGetPixels(crop); + int32 dstStride = RwImageGetStride(crop); + if (dst == nil) { + RwImageDestroy(crop); + RwImageDestroy(img); + return nil; + } + for (int32 y = 0; y < ch; y++) + memcpy(dst + y * dstStride, pixels + (minY + y) * stride + minX * 4, cw * 4); + + // Normalised placement, relative to the art frame's centre. + *pOffX = ((minX + cw * 0.5f) - iw * 0.5f) / (float)ih; + *pOffY = ((minY + ch * 0.5f) - ih * 0.5f) / (float)ih; + *pW = cw / (float)ih; + *pH = ch / (float)ih; + + RwInt32 w, h, d, flags; + RwImageFindRasterFormat(crop, rwRASTERTYPETEXTURE, &w, &h, &d, &flags); + RwRaster *raster = RwRasterCreate(w, h, d, flags); + if (raster != nil) + RwRasterSetFromImage(raster, crop); + + RwImageDestroy(crop); + RwImageDestroy(img); + + if (raster == nil) + return nil; + + RwTexture *texture = RwTextureCreate(raster); + if (texture == nil) { + RwRasterDestroy(raster); + return nil; + } + RwTextureSetFilterMode(texture, rwFILTERLINEAR); + + CSprite2d *sprite = new CSprite2d; + sprite->m_pTexture = texture; + return sprite; +} + +static void +Wheel_LoadArtOnce(void) +{ + if (ms_bArtTried) + return; + ms_bArtTried = true; + + char path[64]; + for (int32 i = 0; i < WHEEL_SEGMENTS; i++) { + sprintf(path, WHEEL_ART_WW_PATH, i); + ms_apWwArt[i] = Wheel_LoadArt(path, &ms_aWwOffX[i], &ms_aWwOffY[i], &ms_aWwW[i], &ms_aWwH[i]); + + sprintf(path, WHEEL_ART_ICON_PATH, i); + ms_apIconArt[i] = Wheel_LoadArt(path, &ms_aIconOffX[i], &ms_aIconOffY[i], &ms_aIconW[i], &ms_aIconH[i]); + } +} + +// The art was authored for 1920x1080, so positions scale with screen height and +// are centred on the screen; the wheel therefore stays circular at any aspect. +static void +Wheel_DrawArt(CSprite2d *sprite, float offX, float offY, float w, float h, const CRGBA &color) +{ + if (sprite == nil) + return; + + float scale = SCREEN_HEIGHT; + float x = SCREEN_WIDTH * 0.5f + offX * scale; + float y = SCREEN_HEIGHT * 0.5f + offY * scale; + float dw = w * scale; + float dh = h * scale; + + sprite->Draw(CRect(x - dw * 0.5f, y - dh * 0.5f, x + dw * 0.5f, y + dh * 0.5f), color); +} + +// --------------------------------------------------------------------------- +// Weapon helpers +// --------------------------------------------------------------------------- + +static int32 +Wheel_SegmentForWeapon(int32 weapon) +{ + // The detonator has no art frame and shares the grenade segment. + if (weapon == WEAPONTYPE_DETONATOR) + return WEAPONTYPE_GRENADE; + return weapon; +} + +static bool +Wheel_WeaponUsable(CPlayerPed *player, int32 weapon) +{ + if (player == nil || !player->HasWeapon(weapon)) + return false; + if (weapon == WEAPONTYPE_UNARMED || weapon == WEAPONTYPE_BASEBALLBAT || weapon == WEAPONTYPE_DETONATOR) + return true; + return player->GetWeapon(weapon).m_nAmmoTotal > 0; +} + +// First usable weapon in a segment, or -1. Only the grenade segment can hold +// more than one inventory type. +static int32 +Wheel_FirstWeaponInSegment(CPlayerPed *player, int32 segment) +{ + if (segment < 0 || segment >= WHEEL_SEGMENTS) + return -1; + + if (segment == WEAPONTYPE_GRENADE) { + if (Wheel_WeaponUsable(player, WEAPONTYPE_GRENADE)) + return WEAPONTYPE_GRENADE; + if (Wheel_WeaponUsable(player, WEAPONTYPE_DETONATOR)) + return WEAPONTYPE_DETONATOR; + return -1; + } + + if (segment < WEAPONTYPE_LAST_WEAPONTYPE && Wheel_WeaponUsable(player, segment)) + return segment; + return -1; +} + +static int32 +Wheel_SelectedWeapon(CPlayerPed *player, int32 segment) +{ + int32 last = ms_aLastWeapon[segment]; + if (last >= 0 && Wheel_SegmentForWeapon(last) == segment && Wheel_WeaponUsable(player, last)) + return last; + return Wheel_FirstWeaponInSegment(player, segment); +} + +static void +Wheel_CycleSegment(CPlayerPed *player) +{ + if (ms_nHovered != WEAPONTYPE_GRENADE) + return; + + int32 grenade = Wheel_WeaponUsable(player, WEAPONTYPE_GRENADE) ? WEAPONTYPE_GRENADE : -1; + int32 detonator = Wheel_WeaponUsable(player, WEAPONTYPE_DETONATOR) ? WEAPONTYPE_DETONATOR : -1; + if (grenade < 0 || detonator < 0) + return; // nothing to cycle between + + ms_aLastWeapon[ms_nHovered] = (int8)(ms_aLastWeapon[ms_nHovered] == detonator ? grenade : detonator); +} + +static void +Wheel_ApplyWeapon(void) +{ + CPlayerPed *player = FindPlayerPed(); + if (player == nil || ms_nHovered < 0) + return; + + int32 weapon = Wheel_SelectedWeapon(player, ms_nHovered); + if (weapon < 0) + return; + + ms_aLastWeapon[ms_nHovered] = (int8)weapon; + player->m_nSelectedWepSlot = (int8)weapon; + + // Same rule as the reversed ProcessWeaponSwitch: don't yank the weapon out + // mid-attack/aim; the regular switch logic applies it when the state allows. + if (player->m_nPedState != PED_ATTACK && player->m_nPedState != PED_AIM_GUN && player->m_nPedState != PED_FIGHT) + player->MakeChangesForNewWeapon((int8)weapon); +} + +// --------------------------------------------------------------------------- +// Radio helpers +// --------------------------------------------------------------------------- + +static wchar * +Wheel_RadioFullName(uint8 station) +{ + wchar *s = nil; + + switch (station) { + case HEAD_RADIO: s = TheText.Get("FEA_FM0"); break; + case DOUBLE_CLEF: s = TheText.Get("FEA_FM1"); break; + case JAH_RADIO: s = TheText.Get("FEA_FM2"); break; + case RISE_FM: s = TheText.Get("FEA_FM3"); break; + case LIPS_106: s = TheText.Get("FEA_FM4"); break; + case GAME_FM: s = TheText.Get("FEA_FM5"); break; + case MSX_FM: s = TheText.Get("FEA_FM6"); break; + case FLASHBACK: s = TheText.Get("FEA_FM7"); break; + case CHATTERBOX: s = TheText.Get("FEA_FM8"); break; + case USERTRACK: s = TheText.Get("FEA_FM9"); break; + case RADIO_OFF: s = TheText.Get("FEA_FMN"); break; + default: return nil; + } + + if (s == nil || s == WideErrorString) + return nil; + return s; +} + +static void +Wheel_BuildRadioList(void) +{ + ms_nRadioCount = 0; + + for (int32 i = 0; i < (int32)ARRAY_SIZE(ms_aRadioBase) && ms_nRadioCount < WHEEL_MAX_RADIO; i++) { + ms_aRadioStation[ms_nRadioCount] = ms_aRadioBase[i]; + ms_aRadioLabel[ms_nRadioCount] = ms_aRadioShort[i]; + ms_nRadioCount++; + } + + if (DMAudio.IsMP3RadioChannelAvailable() && ms_nRadioCount < WHEEL_MAX_RADIO) { + ms_aRadioStation[ms_nRadioCount] = USERTRACK; + ms_aRadioLabel[ms_nRadioCount] = ms_aRadioShort[9]; + ms_nRadioCount++; + } + + if (ms_nRadioCount < WHEEL_MAX_RADIO) { + ms_aRadioStation[ms_nRadioCount] = RADIO_OFF; + ms_aRadioLabel[ms_nRadioCount] = ms_aRadioShort[10]; + ms_nRadioCount++; + } +} + +static int32 +Wheel_RadioSegmentForStation(uint8 station) +{ + for (int32 i = 0; i < ms_nRadioCount; i++) + if (ms_aRadioStation[i] == station) + return i; + return -1; +} + +static void +Wheel_ApplyRadio(void) +{ + if (ms_nHovered < 0 || ms_nHovered >= ms_nRadioCount) + return; + + MusicManager.SetRadioInCar(ms_aRadioStation[ms_nHovered]); + DMAudio.PlayFrontEndSound(SOUND_FRONTEND_RADIO_CHANGE, 100); +} + +// --------------------------------------------------------------------------- +// Open / close +// --------------------------------------------------------------------------- + +static bool +Wheel_PlayerInRadioVehicle(void) +{ + CVehicle *veh = FindPlayerVehicle(); + if (FindPlayerPed() == nil || veh == nil) + return false; + // Police vehicles have a fixed police radio, not a tunable station. + if (MusicManager.UsesPoliceRadio(veh)) + return false; + return MusicManager.PlayerInCar(); +} + +static bool +Wheel_AllowedToOpen(void) +{ + CPlayerPed *player = FindPlayerPed(); + + if (player == nil || player->DyingOrDead() || player->EnteringCar()) + return false; + if (FrontEndMenuManager.m_bMenuActive || CTimer::GetIsPaused()) + return false; + if (CReplay::IsPlayingBack() || TheCamera.m_WideScreenOn || CGame::playingIntro) + return false; + return true; +} + +static bool +Wheel_WeaponTriggerHeld(void) +{ + CPad *pad = CPad::GetPad(0); + + if (FindPlayerVehicle() != nil) + return false; + return !!pad->GetTab() || pad->NewState.RightShoulder2 != 0; +} + +static bool +Wheel_RadioTriggerHeld(void) +{ + CPad *pad = CPad::GetPad(0); + +#ifdef IMPROVED_RADIO_WHEEL + if (!Wheel_PlayerInRadioVehicle()) + return false; + return pad->NewState.LeftShoulder2 != 0 || !!pad->GetChar('R'); +#else + return false; +#endif +} + +static void +Wheel_ClearCameraDisable(void) +{ + if (!ms_bCameraWasDisabled) + CPad::GetPad(0)->SetEnablePlayerControls(PLAYERCONTROL_CAMERA); +} + +static void +Wheel_Open(bool radio) +{ + CPlayerPed *player = FindPlayerPed(); + + ms_bOpen = true; + ms_bRadio = radio; + ms_nHovered = -1; + ms_fCursorX = 0.0f; + ms_fCursorY = 0.0f; + + if (radio) { + Wheel_BuildRadioList(); + ms_nSegments = ms_nRadioCount; + ms_nCurrentSegment = Wheel_RadioSegmentForStation(MusicManager.GetRadioInCar()); + } else { + Wheel_LoadArtOnce(); + ms_nSegments = WHEEL_SEGMENTS; + ms_nCurrentSegment = player != nil ? Wheel_SegmentForWeapon(player->m_currentWeapon) : -1; + } + + CPad *pad = CPad::GetPad(0); + ms_bCameraWasDisabled = (pad->DisablePlayerControls & PLAYERCONTROL_CAMERA) != 0; + pad->SetDisablePlayerControls(PLAYERCONTROL_CAMERA); + +#ifdef IMPROVED_WEAPON_WHEEL_SLOWMO + ms_fSavedTimeScale = CTimer::GetTimeScale(); + CTimer::SetTimeScale(0.35f); +#endif +} + +static void +Wheel_Close(bool apply) +{ + if (apply) { + if (ms_bRadio) + Wheel_ApplyRadio(); + else + Wheel_ApplyWeapon(); + } + + Wheel_ClearCameraDisable(); + +#ifdef IMPROVED_WEAPON_WHEEL_SLOWMO + CTimer::SetTimeScale(ms_fSavedTimeScale); +#endif + + ms_bOpen = false; + ms_bRadio = false; + ms_nHovered = -1; +} + +// --------------------------------------------------------------------------- +// Input +// --------------------------------------------------------------------------- + +static int32 +Wheel_SegmentFromVector(float x, float y) +{ + if (ms_nSegments <= 0) + return -1; + + // Screen y points down; maths angles point up, so flip y. + float angle = Atan2(-y, x); + float step = TWOPI / ms_nSegments; + float rel = HALFPI - angle; + + while (rel < 0.0f) + rel += TWOPI; + while (rel >= TWOPI) + rel -= TWOPI; + + int32 segment = (int32)(rel / step + 0.5f); + if (segment >= ms_nSegments) + segment -= ms_nSegments; + return segment; +} + +static bool +Wheel_KeyDirection(float *x, float *y) +{ + CPad *pad = CPad::GetPad(0); + bool left = pad->GetLeft() || !!pad->GetChar('A'); + bool right = pad->GetRight() || !!pad->GetChar('D'); + bool up = pad->GetUp() || !!pad->GetChar('W'); + bool down = pad->GetDown() || !!pad->GetChar('S'); + + *x = (right ? 1.0f : 0.0f) - (left ? 1.0f : 0.0f); + *y = (down ? 1.0f : 0.0f) - (up ? 1.0f : 0.0f); + return *x != 0.0f || *y != 0.0f; +} + +static void +Wheel_UpdateInput(void) +{ + CPad *pad = CPad::GetPad(0); + float bestX = 0.0f, bestY = 0.0f, bestMag = 0.0f; + + // Right stick. Both backends produce NewState stick axes in roughly + // -128..127 (see CapturePad/AffectFromXinput), negative Y is up. + float stickX = pad->NewState.RightStickX / 128.0f; + float stickY = pad->NewState.RightStickY / 128.0f; + float mag = Sqrt(stickX * stickX + stickY * stickY); + if (mag > bestMag && mag >= WHEEL_STICK_DEADZONE) { + bestX = stickX; + bestY = stickY; + bestMag = mag; + } + + // Mouse: only deltas are available, so integrate them into a virtual + // direction while the wheel is held. + ms_fCursorX += pad->GetMouseX() * WHEEL_MOUSE_SENS; + ms_fCursorY += pad->GetMouseY() * WHEEL_MOUSE_SENS; + if (ms_fCursorX < -WHEEL_CURSOR_CLAMP) ms_fCursorX = -WHEEL_CURSOR_CLAMP; + if (ms_fCursorX > WHEEL_CURSOR_CLAMP) ms_fCursorX = WHEEL_CURSOR_CLAMP; + if (ms_fCursorY < -WHEEL_CURSOR_CLAMP) ms_fCursorY = -WHEEL_CURSOR_CLAMP; + if (ms_fCursorY > WHEEL_CURSOR_CLAMP) ms_fCursorY = WHEEL_CURSOR_CLAMP; + + 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; + } + + // Keyboard direction (arrows or WASD), normalised so it competes with the + // analogue sources fairly. + float keyX, keyY; + if (Wheel_KeyDirection(&keyX, &keyY)) { + mag = Sqrt(keyX * keyX + keyY * keyY); + if (mag > 0.0f) { + keyX /= mag; + keyY /= mag; + } + if (mag >= WHEEL_KEY_DEADZONE || bestMag == 0.0f) { + bestX = keyX; + bestY = keyY; + bestMag = 1.0f; + } + } + + if (bestMag > 0.0f) + ms_nHovered = Wheel_SegmentFromVector(bestX, bestY); + + if (ms_nHovered >= 0) { + bool next = pad->GetMouseWheelDownJustDown() + || (pad->NewState.DPadRight && !pad->OldState.DPadRight); + bool prev = pad->GetMouseWheelUpJustDown() + || (pad->NewState.DPadLeft && !pad->OldState.DPadLeft); + + if (ms_bRadio) { + if (next && ms_nRadioCount > 0) + ms_nHovered = (ms_nHovered + 1) % ms_nRadioCount; + else if (prev && ms_nRadioCount > 0) + ms_nHovered = (ms_nHovered + ms_nRadioCount - 1) % ms_nRadioCount; + } else { + CPlayerPed *player = FindPlayerPed(); + if (player != nil && (next || prev)) + Wheel_CycleSegment(player); + } + } +} + +// --------------------------------------------------------------------------- +// Process / draw +// --------------------------------------------------------------------------- + +void +Wheel_Process(void) +{ + CPad *pad = CPad::GetPad(0); + + if (!ms_bOpen) { + if (!Wheel_AllowedToOpen()) + return; + + if (Wheel_WeaponTriggerHeld()) { + Wheel_Open(false); + } else if (Wheel_RadioTriggerHeld()) { + Wheel_Open(true); + } + return; + } + + // While open, keep the camera/fire controls suppressed even if another + // system cleared the bit in between frames. + if (!ms_bCameraWasDisabled) + pad->SetDisablePlayerControls(PLAYERCONTROL_CAMERA); + + if (!Wheel_AllowedToOpen()) { + Wheel_Close(false); + return; + } + + bool stillWanted = ms_bRadio ? Wheel_RadioTriggerHeld() : Wheel_WeaponTriggerHeld(); + if (!stillWanted) { + Wheel_Close(true); + return; + } + + Wheel_UpdateInput(); +} + +static void +Wheel_DrawRing(void) +{ + if (ms_nSegments <= 0) + return; + + float cx = SCREEN_WIDTH * 0.5f; + float cy = SCREEN_HEIGHT * 0.5f; + float outer = SCREEN_HEIGHT * WHEEL_OUTER_RADIUS; + float inner = SCREEN_HEIGHT * WHEEL_INNER_RADIUS; + float step = TWOPI / ms_nSegments; + CPlayerPed *player = FindPlayerPed(); + int32 highlight = ms_nHovered >= 0 ? ms_nHovered : ms_nCurrentSegment; + + for (int32 i = 0; i < ms_nSegments; i++) { + bool available = true; + if (!ms_bRadio) + available = player != nil && Wheel_FirstWeaponInSegment(player, i) >= 0; + + CRGBA color; + if (!available) + color = CRGBA(10, 10, 10, 80); + else if (i == highlight) + color = CRGBA(90, 90, 90, 200); + else + color = CRGBA(18, 18, 18, 150); + + 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); + + CSprite2d::Draw2DPolygon( + cx + inner * Cos(aa), cy - inner * Sin(aa), + cx + outer * Cos(aa), cy - outer * Sin(aa), + cx + outer * Cos(ab), cy - outer * Sin(ab), + cx + inner * Cos(ab), cy - inner * Sin(ab), + color); + } + } +} + +static void +Wheel_DrawCentredText(float x, float y, wchar *text, float scaleX, float scaleY, const CRGBA &color) +{ + CFont::SetBackgroundOff(); + CFont::SetJustifyOff(); + CFont::SetRightJustifyOff(); + CFont::SetCentreOn(); + CFont::SetCentreSize(SCREEN_WIDTH); + CFont::SetPropOn(); + CFont::SetFontStyle(FONT_HEADING); + CFont::SetScale(SCREEN_SCALE_X(scaleX), SCREEN_SCALE_Y(scaleY)); + CFont::SetColor(color); + CFont::PrintString(x, y, text); +} + +static void +Wheel_DrawSegmentLabel(int32 segment, const char *ascii, float radiusFactor, const CRGBA &color) +{ + if (ms_nSegments <= 0) + return; + + float cx = SCREEN_WIDTH * 0.5f; + float cy = SCREEN_HEIGHT * 0.5f; + float radius = SCREEN_HEIGHT * radiusFactor; + float angle = HALFPI - segment * (TWOPI / ms_nSegments); + + wchar text[32]; + AsciiToUnicode(ascii, text); + Wheel_DrawCentredText(cx + radius * Cos(angle), cy - radius * Sin(angle), + text, 0.30f, 0.50f, color); +} + +static void +Wheel_DrawWeaponIcons(void) +{ + if (ms_nSegments <= 0) + return; + + CPlayerPed *player = FindPlayerPed(); + float cx = SCREEN_WIDTH * 0.5f; + float cy = SCREEN_HEIGHT * 0.5f; + float radius = SCREEN_HEIGHT * WHEEL_ICON_RADIUS; + float size = SCREEN_HEIGHT * WHEEL_ICON_SIZE; + float step = TWOPI / ms_nSegments; + + for (int32 i = 0; i < ms_nSegments; i++) { + int32 weapon = player != nil ? Wheel_SelectedWeapon(player, i) : -1; + bool available = weapon >= 0; + uint8 alpha = available ? 255 : 60; + + // Prefer the art glyph; fall back to the game's HUD weapon sprite. + if (ms_apIconArt[i] != nil) { + Wheel_DrawArt(ms_apIconArt[i], ms_aIconOffX[i], ms_aIconOffY[i], + ms_aIconW[i], ms_aIconH[i], CRGBA(255, 255, 255, alpha)); + } else { + float angle = HALFPI - i * step; + float x = cx + radius * Cos(angle); + float y = cy - radius * Sin(angle); + CHud::Sprites[i].Draw( + CRect(x - size * 0.5f, y - size * 0.5f, x + size * 0.5f, y + size * 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, alpha)); + } + } +} + +static void +Wheel_DrawRadio(void) +{ + if (ms_nSegments <= 0) + return; + + for (int32 i = 0; i < ms_nRadioCount; i++) { + CRGBA color = i == ms_nHovered ? CRGBA(255, 240, 180, 255) : CRGBA(210, 210, 210, 220); + Wheel_DrawSegmentLabel(i, ms_aRadioLabel[i], WHEEL_RADIO_TEXT_R, color); + } +} + +void +Wheel_Draw(void) +{ + if (!ms_bOpen) + return; + + float cx = SCREEN_WIDTH * 0.5f; + float cy = SCREEN_HEIGHT * 0.5f; + CPlayerPed *player = FindPlayerPed(); + int32 highlight = ms_nHovered >= 0 ? ms_nHovered : ms_nCurrentSegment; + + // The HUD's text is batched into CFont's sprite banks and is normally + // flushed after CHud::Draw. Flush it now so the dim overlay really sits on + // top of the HUD instead of under the queued text. + CFont::DrawFonts(); + + // Dim the game/HUD underneath, then the shared ring. + CSprite2d::DrawRect(CRect(0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT), CRGBA(0, 0, 0, 140)); + Wheel_DrawRing(); + + // Draw2DPolygon restores depth states to ON; the 2D pass wants them off. + RwRenderStateSet(rwRENDERSTATEZTESTENABLE, (void*)FALSE); + RwRenderStateSet(rwRENDERSTATEZWRITEENABLE, (void*)FALSE); + + if (ms_bRadio) { + Wheel_DrawRadio(); + } else { + // Art highlight for the pointed-at (or currently equipped) segment. + if (highlight >= 0 && highlight < WHEEL_SEGMENTS && 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)); + Wheel_DrawWeaponIcons(); + } + + // Centre readout. + wchar text[64]; + + if (ms_bRadio) { + int32 shown = ms_nHovered >= 0 ? ms_nHovered : ms_nCurrentSegment; + if (shown >= 0 && shown < ms_nRadioCount) { + wchar *full = Wheel_RadioFullName(ms_aRadioStation[shown]); + if (full != nil) + Wheel_DrawCentredText(cx, cy - SCREEN_SCALE_Y(4.0f), full, 0.62f, 1.05f, CRGBA(255, 240, 180, 255)); + else { + AsciiToUnicode(ms_aRadioLabel[shown], text); + Wheel_DrawCentredText(cx, cy - SCREEN_SCALE_Y(4.0f), text, 0.62f, 1.05f, CRGBA(255, 240, 180, 255)); + } + } else { + AsciiToUnicode("RADIO", text); + Wheel_DrawCentredText(cx, cy - SCREEN_SCALE_Y(4.0f), text, 0.62f, 1.05f, CRGBA(200, 200, 200, 220)); + } + } else { + int32 shown = ms_nHovered >= 0 ? ms_nHovered : ms_nCurrentSegment; + int32 weapon = -1; + if (shown >= 0 && player != nil) + weapon = Wheel_SelectedWeapon(player, shown); + + if (weapon >= 0) { + AsciiToUnicode(ms_aWeaponLong[weapon], text); + Wheel_DrawCentredText(cx, cy - SCREEN_SCALE_Y(4.0f), text, 0.62f, 1.05f, CRGBA(255, 240, 180, 255)); + + if (weapon != WEAPONTYPE_UNARMED && weapon != WEAPONTYPE_BASEBALLBAT) { + char number[16]; + wchar ammo[64]; + sprintf(number, "%d", player->GetWeapon(weapon).m_nAmmoTotal); + AsciiToUnicode(number, ammo); + Wheel_DrawCentredText(cx, cy + SCREEN_SCALE_Y(14.0f), ammo, 0.42f, 0.70f, CRGBA(210, 210, 210, 230)); + } + } else { + AsciiToUnicode(ms_nHovered >= 0 ? "EMPTY" : "WEAPONS", text); + Wheel_DrawCentredText(cx, cy - SCREEN_SCALE_Y(4.0f), text, 0.62f, 1.05f, CRGBA(200, 200, 200, 220)); + } + } +} + +bool +Wheel_IsWeaponWheelOpen(void) +{ + return ms_bOpen && !ms_bRadio; +} + +bool +Wheel_IsRadioWheelOpen(void) +{ + return ms_bOpen && ms_bRadio; +} + +#endif // IMPROVED_WEAPON_WHEEL diff --git a/src/extras/weaponwheel.h b/src/extras/weaponwheel.h new file mode 100644 index 00000000..1a4e886b --- /dev/null +++ b/src/extras/weaponwheel.h @@ -0,0 +1,19 @@ +#pragma once + +#ifdef IMPROVED_WEAPON_WHEEL + +// GTA V-style weapon and radio wheels (issue #31). +// Custom UI: this is not reversed code and only exists when IMPROVED_WEAPON_WHEEL +// is defined in src/core/config.h. +// +// The wheel is processed from CHud::Draw so it never runs outside the render/HUD +// path. Optional art (weapon_wheel/*.png next to the executable) is loaded at +// runtime and every piece falls back to the procedural wheel if it is absent. + +void Wheel_Process(void); +void Wheel_Draw(void); + +bool Wheel_IsWeaponWheelOpen(void); +bool Wheel_IsRadioWheelOpen(void); + +#endif // IMPROVED_WEAPON_WHEEL