Merge branch 'main' of https://github.com/TwilitRealm/dusk into instant-text

This commit is contained in:
gymnast86
2026-04-17 01:30:19 -07:00
28 changed files with 829 additions and 178 deletions
+151 -21
View File
@@ -3,23 +3,24 @@
#include <numeric>
#include <string_view>
#include <chrono>
#include <thread>
#include "fmt/format.h"
#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui.h"
#include <imgui_internal.h>
#include "fmt/format.h"
#include "ImGuiConsole.hpp"
#include "JSystem/JUtility/JUTGamePad.h"
#include "SDL3/SDL_events.h"
#include "SDL3/SDL_mouse.h"
#include "m_Do/m_Do_controller_pad.h"
#include "m_Do/m_Do_main.h"
#include "aurora/lib/window.hpp"
#include "dusk/audio/DuskAudioSystem.h"
#include "dusk/config.hpp"
#include "dusk/dusk.h"
#include "dusk/main.h"
#include "dusk/settings.h"
#include "dusk/audio/DuskAudioSystem.h"
#include "dusk/dusk.h"
#include "m_Do/m_Do_controller_pad.h"
#include "m_Do/m_Do_main.h"
#include "tracy/Tracy.hpp"
#if _WIN32
@@ -30,6 +31,30 @@
using namespace std::string_literals;
using namespace std::string_view_literals;
namespace {
ImVec2 TouchEventToScreenPos(const SDL_TouchFingerEvent& touch) {
const AuroraWindowSize size = aurora::window::get_window_size();
return ImVec2{
touch.x * static_cast<float>(size.width),
touch.y * static_cast<float>(size.height),
};
}
ImGuiWindow* FindDragScrollWindow(ImGuiWindow* window) {
while (window != nullptr) {
const bool canScrollX = window->ScrollMax.x > 0.0f;
const bool canScrollY = window->ScrollMax.y > 0.0f;
const bool canScrollWithMouse = (window->Flags & (ImGuiWindowFlags_NoScrollWithMouse |
ImGuiWindowFlags_NoMouseInputs)) == 0;
if ((canScrollX || canScrollY) && canScrollWithMouse) {
return window;
}
window = window->ParentWindow;
}
return nullptr;
}
} // namespace
namespace dusk {
float ImGuiScale() { return 1.0f; }
@@ -208,6 +233,51 @@ namespace dusk {
ImGuiConsole::ImGuiConsole() {}
void ImGuiConsole::HandleSDLEvent(const SDL_Event& event) {
if (!IsGameLaunched) {
return;
}
switch (event.type) {
case SDL_EVENT_FINGER_DOWN:
if (!m_touchTapActive) {
m_touchTapActive = true;
m_touchTapMoved = false;
m_touchTapFingerId = event.tfinger.fingerID;
m_touchTapStartPos = TouchEventToScreenPos(event.tfinger);
}
break;
case SDL_EVENT_FINGER_MOTION:
if (m_touchTapActive && m_touchTapFingerId == event.tfinger.fingerID) {
const auto currentPos = TouchEventToScreenPos(event.tfinger);
const auto delta = currentPos - m_touchTapStartPos;
if (ImLengthSqr(delta) > 144.0f) {
m_touchTapMoved = true;
}
}
break;
case SDL_EVENT_FINGER_UP:
if (m_touchTapActive && m_touchTapFingerId == event.tfinger.fingerID) {
const bool shouldToggle =
!m_touchTapMoved && (m_isHidden || !ImGui::GetIO().WantCaptureMouse);
m_touchTapActive = false;
m_touchTapMoved = false;
if (shouldToggle) {
m_isHidden = !m_isHidden;
getSettings().backend.duskMenuOpen.setValue(!m_isHidden);
Save();
}
}
break;
case SDL_EVENT_FINGER_CANCELED:
m_touchTapActive = false;
m_touchTapMoved = false;
break;
default:
break;
}
}
void ImGuiConsole::UpdateSettings() {
getTransientSettings().skipFrameRateLimit = getSettings().game.enableTurboKeybind && ImGui::IsKeyDown(ImGuiKey_Tab);
@@ -237,26 +307,31 @@ namespace dusk {
if (!dusk::IsGameLaunched) {
m_preLaunchWindow.draw();
}
m_isHidden = getSettings().backend.duskMenuOpen;
CheckMenuViewToggle(ImGuiKey_F1, m_isHidden);
m_isHidden = !getSettings().backend.duskMenuOpen;
bool showMenu = !dusk::IsGameLaunched || !CheckMenuViewToggle(ImGuiKey_F1, m_isHidden);
if (dusk::IsGameLaunched) {
if (getSettings().backend.duskMenuOpen != m_isHidden) {
m_isHidden = !getSettings().backend.duskMenuOpen;
getSettings().backend.duskMenuOpen.setValue(m_isHidden);
config::Save();
const bool menuOpen = !m_isHidden;
if (getSettings().backend.duskMenuOpen != menuOpen) {
getSettings().backend.duskMenuOpen.setValue(menuOpen);
Save();
}
}
if ((!dusk::IsGameLaunched || getSettings().backend.duskMenuOpen) && ImGui::BeginMainMenuBar()) {
if (showMenu && ImGui::BeginMainMenuBar()) {
m_menuGame.draw();
m_menuEnhancements.draw();
m_menuTools.draw();
ImGui::SetCursorPosX(ImGui::GetWindowWidth() - 100.0f * ImGuiScale());
ImGuiIO& io = ImGui::GetIO();
ImGuiStringViewText(fmt::format(FMT_STRING("FPS: {:.2f}\n"), io.Framerate));
const auto fpsLabel =
fmt::format(FMT_STRING("FPS: {:.2f}\n"), ImGui::GetIO().Framerate);
const auto fpsSize =
ImGui::CalcTextSize(fpsLabel.data(), fpsLabel.data() + fpsLabel.size());
ImGui::SetCursorPosX(
ImMax(ImGui::GetCursorPosX(), ImGui::GetWindowWidth() -
ImGui::GetStyle().DisplaySafeAreaPadding.x -
fpsSize.x - ImGui::GetStyle().ItemSpacing.x));
ImGuiStringViewText(fpsLabel);
ImGui::EndMainMenuBar();
}
@@ -267,10 +342,15 @@ namespace dusk {
}
if (dusk::IsGameLaunched && !m_isLaunchInitialized) {
m_toasts.emplace_back("Press F1 to toggle menu"s, 2.5f);
m_toasts.emplace_back(ImGui::GetIO().MouseSource == ImGuiMouseSource_TouchScreen ?
"Tap to toggle menu"s :
"Press F1 to toggle menu"s,
2.5f);
m_isLaunchInitialized = true;
}
UpdateDragScroll();
m_menuGame.windowControllerConfig();
m_menuGame.windowInputViewer();
if (dusk::IsGameLaunched) {
@@ -289,8 +369,7 @@ namespace dusk {
DuskDebugPad(); // temporary, remove later
// Only show cursor when menu or any windows are open
if ((!dusk::IsGameLaunched || getSettings().backend.duskMenuOpen) || ImGui::GetIO().MetricsRenderWindows > 0)
{
if (showMenu || ImGui::GetIO().MetricsRenderWindows > 0) {
ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NoMouseCursorChange;
// Imgui will re-show cursor.
} else {
@@ -306,6 +385,57 @@ namespace dusk {
ShowPipelineProgress();
}
void ImGuiConsole::UpdateDragScroll() {
ImGuiContext& g = *ImGui::GetCurrentContext();
ImGuiIO& io = ImGui::GetIO();
if (io.MouseSource != ImGuiMouseSource_TouchScreen) {
m_dragScrollWindow = nullptr;
return;
}
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
m_dragScrollWindow = nullptr;
return;
}
if (io.WantTextInput || (g.ActiveId != 0 && g.InputTextState.ID == g.ActiveId)) {
m_dragScrollWindow = nullptr;
return;
}
if (!ImGui::IsMouseDragging(ImGuiMouseButton_Left, io.MouseDragThreshold)) {
return;
}
if (m_dragScrollWindow == nullptr) {
ImGuiWindow* hoveredWindow = nullptr;
ImGuiWindow* hoveredWindowUnderMovingWindow = nullptr;
ImGui::FindHoveredWindowEx(io.MousePos, false, &hoveredWindow,
&hoveredWindowUnderMovingWindow);
m_dragScrollWindow = FindDragScrollWindow(hoveredWindow);
m_dragScrollLastMousePos = io.MousePos;
}
if (m_dragScrollWindow == nullptr) {
return;
}
const auto mouseDelta = io.MousePos - m_dragScrollLastMousePos;
m_dragScrollLastMousePos = io.MousePos;
if (mouseDelta.x != 0.0f && m_dragScrollWindow->ScrollMax.x > 0.0f) {
ImGui::SetScrollX(m_dragScrollWindow,
ImClamp(m_dragScrollWindow->Scroll.x - mouseDelta.x, 0.0f,
m_dragScrollWindow->ScrollMax.x));
}
if (mouseDelta.y != 0.0f && m_dragScrollWindow->ScrollMax.y > 0.0f) {
ImGui::SetScrollY(m_dragScrollWindow,
ImClamp(m_dragScrollWindow->Scroll.y - mouseDelta.y, 0.0f,
m_dragScrollWindow->ScrollMax.y));
}
}
bool ImGuiConsole::CheckMenuViewToggle(ImGuiKey key, bool& active) {
if (ImGui::IsKeyPressed(key)) {
active = !active;
+14 -1
View File
@@ -1,11 +1,13 @@
#ifndef DUSK_IMGUI_HPP
#define DUSK_IMGUI_HPP
#include <aurora/aurora.h>
#include <deque>
#include <string>
#include <string_view>
#include <aurora/aurora.h>
#include <SDL3/SDL_touch.h>
#include "ImGuiFirstRunPreset.hpp"
#include "ImGuiMenuEnhancements.hpp"
#include "ImGuiMenuGame.hpp"
@@ -13,10 +15,14 @@
#include "ImGuiPreLaunchWindow.hpp"
#include "imgui.h"
union SDL_Event;
struct ImGuiWindow;
namespace dusk {
class ImGuiConsole {
public:
ImGuiConsole();
void HandleSDLEvent(const SDL_Event& event);
void UpdateSettings();
void PreDraw();
void PostDraw();
@@ -34,6 +40,12 @@ private:
bool m_isHidden = true;
bool m_isLaunchInitialized = false;
bool m_touchTapActive = false;
bool m_touchTapMoved = false;
SDL_FingerID m_touchTapFingerId = 0;
ImVec2 m_touchTapStartPos = {};
ImGuiWindow* m_dragScrollWindow = nullptr;
ImVec2 m_dragScrollLastMousePos = {};
std::deque<Toast> m_toasts;
ImGuiFirstRunPreset m_firstRunPreset;
@@ -46,6 +58,7 @@ private:
void ShowToasts();
void ShowPipelineProgress();
void UpdateDragScroll();
};
extern ImGuiConsole g_imguiConsole;
+18
View File
@@ -3,12 +3,14 @@
#include <SDL3/SDL_filesystem.h>
#include <SDL3/SDL_pixels.h>
#include <SDL3/SDL_surface.h>
#include <SDL3/SDL_video.h>
#include <aurora/imgui.h>
#include <cmath>
#include <cstring>
#include <fmt/format.h>
#include <string>
#include "aurora/lib/window.hpp"
#include "dusk/logging.h"
#ifdef IMGUI_ENABLE_FREETYPE
@@ -37,6 +39,20 @@ ImTextureID AddTexture(const char* assetName) {
}
return aurora_imgui_add_texture(image.width, image.height, image.data.get());
}
ImVec2 GetDisplaySafeAreaPadding() {
SDL_Window* window = aurora::window::get_sdl_window();
if (window == nullptr) {
return ImVec2(0.0f, 0.0f);
}
SDL_Rect safeRect{};
if (!SDL_GetWindowSafeArea(window, &safeRect)) {
return ImVec2(0.0f, 0.0f);
}
return ImVec2(static_cast<float>(safeRect.x), static_cast<float>(safeRect.y));
}
} // namespace
ImFont* ImGuiEngine::fontNormal;
@@ -75,6 +91,7 @@ void ImGuiEngine_Initialize(float scale) {
ImGuiIO& io = ImGui::GetIO();
io.Fonts->Clear();
io.FontGlobalScale = scale > 0.0f ? 1.0f / scale : 1.0f;
io.ConfigWindowsMoveFromTitleBarOnly = IsMobile;
ImGuiEngine::fontNormal =
CreateFont(std::floor(18.f * scale), GetAssetPath("Inter-Regular.ttf"), "Inter Regular");
@@ -104,6 +121,7 @@ void ImGuiEngine_Initialize(float scale) {
style.PopupRounding = 7.0;
style.TabBorderSize = 1.f;
style.TabRounding = 3.f;
style.DisplaySafeAreaPadding = GetDisplaySafeAreaPadding();
auto* colors = style.Colors;
colors[ImGuiCol_Text] = ImVec4(0.95f, 0.96f, 0.98f, 1.00f);
+6
View File
@@ -25,4 +25,10 @@ struct Image {
uint32_t height;
};
Image GetImage(const std::string& path);
#if (defined(__APPLE__) && TARGET_OS_IOS && !TARGET_OS_MACCATALYST) || defined(__ANDROID__)
inline constexpr bool IsMobile = true;
#else
inline constexpr bool IsMobile = false;
#endif
} // namespace dusk
+12 -9
View File
@@ -1,6 +1,7 @@
#include "fmt/format.h"
#include "imgui.h"
#include "ImGuiEngine.hpp"
#include "ImGuiConsole.hpp"
#include "ImGuiMenuGame.hpp"
#include "ImGuiConfig.hpp"
@@ -32,15 +33,17 @@ namespace dusk {
void ImGuiMenuGame::draw() {
if (ImGui::BeginMenu("Game")) {
if (ImGui::BeginMenu("Graphics")) {
if (ImGui::MenuItem("Toggle Fullscreen", hotkeys::TOGGLE_FULLSCREEN)) {
ToggleFullscreen();
}
if (!IsMobile) {
if (ImGui::MenuItem("Toggle Fullscreen", hotkeys::TOGGLE_FULLSCREEN)) {
ToggleFullscreen();
}
if (ImGui::MenuItem("Default Window Size")) {
getSettings().video.enableFullscreen.setValue(false);
VISetWindowFullscreen(false);
VISetWindowSize(FB_WIDTH * 2, FB_HEIGHT * 2);
VICenterWindow();
if (ImGui::MenuItem("Default Window Size")) {
getSettings().video.enableFullscreen.setValue(false);
VISetWindowFullscreen(false);
VISetWindowSize(FB_WIDTH * 2, FB_HEIGHT * 2);
VICenterWindow();
}
}
bool vsync = getSettings().video.enableVsync;
@@ -147,7 +150,7 @@ namespace dusk {
JUTGamePad::C3ButtonReset::sResetSwitchPushing = true;
}
if (ImGui::MenuItem("Exit")) {
if (!IsMobile && ImGui::MenuItem("Exit")) {
dusk::IsRunning = false;
}
+21 -29
View File
@@ -4,13 +4,13 @@
#include "ImGuiEngine.hpp"
#include "ImGuiPreLaunchWindow.hpp"
#include "../file_select.hpp"
#include "../iso_validate.hpp"
#include "ImGuiConsole.hpp"
#include "dusk/main.h"
#include "dusk/settings.h"
#include "../iso_validate.hpp"
#include <SDL3/SDL_dialog.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_filesystem.h>
#include "aurora/lib/internal.hpp"
@@ -46,30 +46,22 @@ static std::string ShowIsoInvalidError(const iso::ValidationError code) {
}
}
void fileDialogCallback(void* userdata, const char* const* filelist, [[maybe_unused]] int filter) {
void fileDialogCallback(void* userdata, const char* path, const char* error) {
auto* self = static_cast<ImGuiPreLaunchWindow*>(userdata);
self->m_errorString.clear();
if (filelist != nullptr) {
if (filelist[0] == nullptr) {
// Cancelled
self->m_selectedIsoPath.clear();
} else {
const auto path = filelist[0];
const auto ret = iso::validate(path);
if (ret != iso::ValidationError::Success) {
self->m_selectedIsoPath.clear();
self->m_errorString = std::move(ShowIsoInvalidError(ret));
return;
}
self->m_selectedIsoPath = path;
getSettings().backend.isoPath.setValue(path);
config::Save();
}
} else {
// Error occurred
if (error != nullptr) {
self->m_selectedIsoPath.clear();
self->m_errorString = fmt::format("File dialog error: {}", SDL_GetError());
self->m_errorString = fmt::format("File dialog error: {}", error);
return;
}
if (path == nullptr) {
self->m_selectedIsoPath.clear();
return;
}
self->m_selectedIsoPath = path;
getSettings().backend.isoPath.setValue(self->m_selectedIsoPath);
config::Save();
}
ImGuiPreLaunchWindow::ImGuiPreLaunchWindow() = default;
@@ -144,9 +136,9 @@ void ImGuiPreLaunchWindow::drawMainMenu() {
}
if (ImGuiButtonCenter("Select disc image...")) {
SDL_ShowOpenFileDialog(&fileDialogCallback, this, aurora::window::get_sdl_window(),
skGameDiscFileFilters.data(), int(skGameDiscFileFilters.size()),
nullptr, false);
ShowFileSelect(&fileDialogCallback, this, aurora::window::get_sdl_window(),
skGameDiscFileFilters.data(), int(skGameDiscFileFilters.size()), nullptr,
false);
}
} else {
if (ImGuiButtonCenter("Start game")) {
@@ -187,9 +179,9 @@ void ImGuiPreLaunchWindow::drawOptions() {
ImGui::InputText("Game ISO Path", &m_selectedIsoPath, ImGuiInputTextFlags_ReadOnly);
ImGui::SameLine();
if (ImGui::Button("Set")) {
SDL_ShowOpenFileDialog(&fileDialogCallback, this, aurora::window::get_sdl_window(),
skGameDiscFileFilters.data(), int(skGameDiscFileFilters.size()),
nullptr, false);
ShowFileSelect(&fileDialogCallback, this, aurora::window::get_sdl_window(),
skGameDiscFileFilters.data(), int(skGameDiscFileFilters.size()), nullptr,
false);
}
AuroraBackend configuredBackend = BACKEND_AUTO;