- unitat mouse per amagar el punter
- overlay captura el esc i confirma la eixida (falla en game)
This commit is contained in:
26
source/core/input/mouse.cpp
Normal file
26
source/core/input/mouse.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "core/input/mouse.hpp"
|
||||
|
||||
namespace Mouse {
|
||||
|
||||
static constexpr Uint32 HIDE_DELAY = 3000; // Temps en ms per a amagar el cursor
|
||||
static Uint32 last_move_time = 0;
|
||||
static bool cursor_visible = true;
|
||||
|
||||
void handleEvent(const SDL_Event& event) {
|
||||
if (event.type == SDL_EVENT_MOUSE_MOTION) {
|
||||
last_move_time = SDL_GetTicks();
|
||||
if (!cursor_visible) {
|
||||
SDL_ShowCursor();
|
||||
cursor_visible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateCursorVisibility() {
|
||||
if (cursor_visible && (SDL_GetTicks() - last_move_time > HIDE_DELAY)) {
|
||||
SDL_HideCursor();
|
||||
cursor_visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Mouse
|
||||
11
source/core/input/mouse.hpp
Normal file
11
source/core/input/mouse.hpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
namespace Mouse {
|
||||
// Gestiona el moviment del ratolí (mostra el cursor si es mou)
|
||||
void handleEvent(const SDL_Event& event);
|
||||
|
||||
// Amaga el cursor si no s'ha mogut en un temps
|
||||
void updateCursorVisibility();
|
||||
} // namespace Mouse
|
||||
@@ -3,7 +3,9 @@
|
||||
#include <string>
|
||||
|
||||
#include "core/input/global_inputs.hpp"
|
||||
#include "core/input/mouse.hpp"
|
||||
#include "core/jail/jgame.hpp"
|
||||
#include "core/rendering/overlay.hpp"
|
||||
#include "game/options.hpp"
|
||||
|
||||
const bool* keystates; // = SDL_GetKeyboardState( NULL );
|
||||
@@ -11,6 +13,7 @@ SDL_Event event;
|
||||
Uint8 cheat[5];
|
||||
bool key_pressed = false;
|
||||
int waitTime = 0;
|
||||
static bool esc_blocked_ = false; // Bloqueja ESC per polling quan l'overlay l'ha consumit
|
||||
|
||||
// Comprova si un scancode pertany a les tecles reservades per a la GUI
|
||||
static bool isGuiKey(SDL_Scancode sc) {
|
||||
@@ -47,20 +50,40 @@ void JI_Update() {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_EVENT_QUIT) JG_QuitSignal();
|
||||
if (event.type == SDL_EVENT_KEY_UP) {
|
||||
// Si és una tecla GUI, no la passem al joc legacy
|
||||
if (!isGuiKey(event.key.scancode)) {
|
||||
// ESC interceptat per l'overlay (doble pulsació per eixir)
|
||||
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
|
||||
if (Overlay::handleEscape()) {
|
||||
// Consumit: primera pulsació, bloqueja ESC per polling
|
||||
esc_blocked_ = true;
|
||||
} else {
|
||||
// Segona pulsació: desbloqueja i passa al joc
|
||||
esc_blocked_ = false;
|
||||
key_pressed = true;
|
||||
JG_QuitSignal();
|
||||
}
|
||||
} else if (!isGuiKey(event.key.scancode)) {
|
||||
// Tecles normals del joc
|
||||
key_pressed = true;
|
||||
JI_moveCheats(event.key.scancode);
|
||||
}
|
||||
}
|
||||
Mouse::handleEvent(event);
|
||||
}
|
||||
|
||||
// GlobalInputs processa les tecles GUI per polling
|
||||
// Desbloqueja ESC quan la tecla ja no està polsada i l'overlay ha fet timeout
|
||||
if (esc_blocked_ && !keystates[SDL_SCANCODE_ESCAPE]) {
|
||||
esc_blocked_ = false;
|
||||
}
|
||||
|
||||
Mouse::updateCursorVisibility();
|
||||
GlobalInputs::handle();
|
||||
}
|
||||
|
||||
bool JI_KeyPressed(int key) {
|
||||
return waitTime > 0 ? false : (keystates[key] != 0);
|
||||
if (waitTime > 0) return false;
|
||||
// Si ESC està bloquejat per l'overlay, no la passem al joc
|
||||
if (key == SDL_SCANCODE_ESCAPE && esc_blocked_) return false;
|
||||
return keystates[key] != 0;
|
||||
}
|
||||
|
||||
bool JI_CheatActivated(const char* cheat_code) {
|
||||
|
||||
@@ -50,6 +50,9 @@ namespace Overlay {
|
||||
// --- Render info ---
|
||||
static std::string render_info_text_;
|
||||
|
||||
// --- Doble ESC per a eixir ---
|
||||
static bool esc_waiting_ = false;
|
||||
|
||||
void init() {
|
||||
font_ = std::make_unique<Text>("fonts/8bithud.fnt", "fonts/8bithud.gif");
|
||||
last_ticks_ = SDL_GetTicks();
|
||||
@@ -141,6 +144,11 @@ namespace Overlay {
|
||||
notifications_.erase(
|
||||
std::remove_if(notifications_.begin(), notifications_.end(), [](const Notification& n) { return n.status == Status::FINISHED; }),
|
||||
notifications_.end());
|
||||
|
||||
// Si la notificació d'ESC ha desaparegut, reseteja l'estat
|
||||
if (esc_waiting_ && notifications_.empty()) {
|
||||
esc_waiting_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
void showNotification(const char* text, float duration_seconds) {
|
||||
@@ -175,4 +183,16 @@ namespace Overlay {
|
||||
render_info_text_ = text;
|
||||
}
|
||||
|
||||
auto handleEscape() -> bool {
|
||||
if (!esc_waiting_) {
|
||||
// Primera pulsació: mostra avís i consumeix
|
||||
esc_waiting_ = true;
|
||||
showNotification("TORNA A PULSAR ESC PER EIXIR", 2.0F);
|
||||
return true; // Consumit
|
||||
}
|
||||
// Segona pulsació: deixa passar
|
||||
esc_waiting_ = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace Overlay
|
||||
|
||||
@@ -15,4 +15,8 @@ namespace Overlay {
|
||||
// Activa/desactiva la info de renderitzat (FPS, driver, shader, preset)
|
||||
void toggleRenderInfo();
|
||||
void setRenderInfoText(const char* text);
|
||||
|
||||
// Gestió d'eixida amb doble ESC
|
||||
// Retorna true si l'ESC ha sigut consumit (no s'ha de passar al joc)
|
||||
auto handleEscape() -> bool;
|
||||
} // namespace Overlay
|
||||
|
||||
Reference in New Issue
Block a user