|
| 1 | +/* |
| 2 | +* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 3 | +* |
| 4 | +* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | +* copy of this software and associated documentation files (the "Software"), |
| 6 | +* to deal in the Software without restriction, including without limitation |
| 7 | +* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | +* and/or sell copies of the Software, and to permit persons to whom the |
| 9 | +* Software is furnished to do so, subject to the following conditions: |
| 10 | +* |
| 11 | +* The above copyright notice and this permission notice shall be included in |
| 12 | +* all copies or substantial portions of the Software. |
| 13 | +* |
| 14 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | +* DEALINGS IN THE SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +#pragma once |
| 24 | + |
| 25 | +#include "../rtx_graph_component_macros.h" |
| 26 | +#include "../../imgui/dxvk_imgui.h" |
| 27 | +#include "../../../util/util_keybind.h" |
| 28 | +#include "../../../util/config/config.h" |
| 29 | + |
| 30 | +namespace dxvk { |
| 31 | +namespace components { |
| 32 | + |
| 33 | +#define LIST_INPUTS(X) \ |
| 34 | + X(RtComponentPropertyType::String, std::string("A"), keyString, "Key String", "The key combination string (e.g., 'A', 'CTRL,A', 'SHIFT,SPACE'). Supports key names and combinations like RTX options.") |
| 35 | + |
| 36 | +#define LIST_STATES(X) \ |
| 37 | + X(RtComponentPropertyType::Bool, false, wasPressedLastFrame, "", "Internal state to track if the key was pressed in the previous frame.") |
| 38 | + |
| 39 | +#define LIST_OUTPUTS(X) \ |
| 40 | + X(RtComponentPropertyType::Bool, false, isPressed, "Is Pressed", "True if the key combination is currently being pressed.") \ |
| 41 | + X(RtComponentPropertyType::Bool, false, wasJustPressed, "Was Just Pressed", "True if the key combination was just pressed this frame.") \ |
| 42 | + X(RtComponentPropertyType::Bool, false, wasClicked, "Was Clicked", "True for one frame after the key combination is released (press then release cycle).") |
| 43 | + |
| 44 | +REMIX_COMPONENT( \ |
| 45 | + /* the Component name */ KeyboardInput, \ |
| 46 | + /* the UI name */ "Keyboard Input", \ |
| 47 | + /* the UI categories */ "Sense", \ |
| 48 | + /* the doc string */ "Checks the state of a keyboard key or key combination using the same format as RTX options.", \ |
| 49 | + /* the version number */ 1, \ |
| 50 | + LIST_INPUTS, LIST_STATES, LIST_OUTPUTS); |
| 51 | + |
| 52 | +#undef LIST_INPUTS |
| 53 | +#undef LIST_STATES |
| 54 | +#undef LIST_OUTPUTS |
| 55 | + |
| 56 | +void KeyboardInput::updateRange(const Rc<DxvkContext>& context, const size_t start, const size_t end) { |
| 57 | + for (size_t i = start; i < end; i++) { |
| 58 | + // Parse the key string into VirtualKeys |
| 59 | + VirtualKeys virtualKeys; |
| 60 | + const bool parseSuccess = Config::parseOptionValue(m_keyString[i], virtualKeys); |
| 61 | + |
| 62 | + if (parseSuccess && !virtualKeys.empty()) { |
| 63 | + // Check if the key combination is currently pressed (continuous press) |
| 64 | + const bool currentlyPressed = ImGUI::checkHotkeyState(virtualKeys, true); |
| 65 | + |
| 66 | + // Check if the key combination was just pressed this frame (single press) |
| 67 | + const bool justPressed = ImGUI::checkHotkeyState(virtualKeys, false); |
| 68 | + |
| 69 | + // Check if the key was clicked (pressed last frame, released this frame) |
| 70 | + const bool wasClicked = m_wasPressedLastFrame[i] && !currentlyPressed; |
| 71 | + |
| 72 | + // Update outputs |
| 73 | + m_isPressed[i] = currentlyPressed; |
| 74 | + m_wasJustPressed[i] = justPressed; |
| 75 | + m_wasClicked[i] = wasClicked; |
| 76 | + |
| 77 | + // Update state for next frame |
| 78 | + m_wasPressedLastFrame[i] = currentlyPressed; |
| 79 | + } else { |
| 80 | + ONCE(Logger::err(str::format("Failed to parse key string: '", m_keyString[i], "'"))); |
| 81 | + // Invalid key string - default to false |
| 82 | + m_isPressed[i] = false; |
| 83 | + m_wasJustPressed[i] = false; |
| 84 | + m_wasClicked[i] = false; |
| 85 | + m_wasPressedLastFrame[i] = false; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace components |
| 91 | +} // namespace dxvk |
0 commit comments