UI: "No controller" & menu notifications

Resolves #629
Resolves #678
This commit is contained in:
Luke Street
2026-05-06 17:34:08 -06:00
parent 7f0955f022
commit 18eb0692f0
7 changed files with 200 additions and 12 deletions
+133 -6
View File
@@ -1,11 +1,13 @@
#include "overlay.hpp"
#include "aurora/lib/logging.hpp"
#include "magic_enum.hpp"
#include <algorithm>
#include "dusk/achievements.h"
#include "magic_enum.hpp"
#include "window.hpp"
#include <SDL3/SDL_gamepad.h>
#include <algorithm>
#include <dolphin/pad.h>
namespace dusk::ui {
namespace {
@@ -27,6 +29,8 @@ constexpr std::array<std::pair<const char*, const char*>, 3> kAutoSaveLayers{{
{"center", "res/org-icon-center.png"},
}};
constexpr auto kMenuNotificationDuration = std::chrono::milliseconds(2500);
Rml::Element* create_toast(Rml::Element* parent, const Toast& toast) {
if (toast.type == "autosave") {
auto* logo = append(parent, "logo");
@@ -75,6 +79,78 @@ Rml::Element* create_toast(Rml::Element* parent, const Toast& toast) {
return elem;
}
Rml::Element* create_controller_warning(Rml::Element* parent) {
auto* elem = append(parent, "toast");
elem->SetClass("controller-warning", true);
auto* heading = append(elem, "heading");
auto* title = append(heading, "span");
title->SetInnerRML("No controller assigned");
auto* icon = append(heading, "icon");
icon->SetClass("warning", true);
auto* message = append(elem, "message");
auto* content = append(message, "span");
content->SetInnerRML("Configure controller port 1 in Settings.");
return elem;
}
SDL_Gamepad* gamepad_for_port(u32 port) noexcept {
const s32 index = PADGetIndexForPort(port);
if (index < 0) {
return nullptr;
}
return PADGetSDLGamepadForIndex(static_cast<u32>(index));
}
Rml::String back_button_name() {
if (auto* gamepad = gamepad_for_port(PAD_CHAN0)) {
switch (SDL_GetGamepadType(gamepad)) {
case SDL_GAMEPAD_TYPE_PS3:
return "Select";
case SDL_GAMEPAD_TYPE_PS4:
return "Share";
case SDL_GAMEPAD_TYPE_PS5:
return "Create";
case SDL_GAMEPAD_TYPE_XBOX360:
return "Back";
case SDL_GAMEPAD_TYPE_XBOXONE:
return "View";
case SDL_GAMEPAD_TYPE_GAMECUBE:
return "R + Start";
default:
break;
}
}
return "Back";
}
Rml::Element* create_menu_notification(Rml::Element* parent) {
auto* elem = append(parent, "toast");
elem->SetClass("menu-notification", true);
auto* message = append(elem, "message");
auto* row = append(message, "row");
append(row, "span")->SetInnerRML("Press F1 or");
auto* icon = append(row, "icon");
icon->SetClass("controller", true);
append(row, "span")->SetInnerRML(escape(back_button_name()));
append(row, "span")->SetInnerRML("to open menu");
return elem;
}
void remove_element(Rml::Element*& elem) noexcept {
if (elem == nullptr) {
return;
}
if (auto* parent = elem->GetParentNode()) {
parent->RemoveChild(elem);
}
elem = nullptr;
}
} // namespace
Overlay::Overlay() : Document(kDocumentSource) {
@@ -86,6 +162,15 @@ Overlay::Overlay() : Document(kDocumentSource) {
{
mCurrentToast->SetPseudoClass("done", true);
}
} else if (mControllerWarning != nullptr &&
event.GetTargetElement() == mControllerWarning &&
!mControllerWarning->HasAttribute("open"))
{
mControllerWarning->SetPseudoClass("done", true);
} else if (mMenuNotification != nullptr && event.GetTargetElement() == mMenuNotification &&
!mMenuNotification->HasAttribute("open"))
{
mMenuNotification->SetPseudoClass("done", true);
}
});
}
@@ -98,6 +183,49 @@ void Overlay::show() {
void Overlay::update() {
Document::update();
if (mDocument == nullptr) {
return;
}
const bool showControllerWarning =
PADGetIndexForPort(PAD_CHAN0) < 0 && dynamic_cast<Window*>(top_document()) == nullptr;
if (showControllerWarning && mControllerWarning == nullptr) {
mControllerWarning = create_controller_warning(mDocument);
} else if (showControllerWarning && mControllerWarning != nullptr) {
mControllerWarning->SetAttribute("open", "");
mControllerWarning->SetPseudoClass("opened", true);
mControllerWarning->SetPseudoClass("done", false);
} else if (!showControllerWarning && mControllerWarning != nullptr) {
if (mControllerWarning->IsPseudoClassSet("done") ||
!mControllerWarning->IsPseudoClassSet("opened"))
{
remove_element(mControllerWarning);
} else {
mControllerWarning->RemoveAttribute("open");
}
}
if (mMenuNotification != nullptr) {
if (clock::now() >= mMenuNotificationStartTime + kMenuNotificationDuration) {
if (mMenuNotification->IsPseudoClassSet("done") ||
!mMenuNotification->IsPseudoClassSet("opened"))
{
remove_element(mMenuNotification);
} else {
mMenuNotification->RemoveAttribute("open");
}
} else {
mMenuNotification->SetAttribute("open", "");
mMenuNotification->SetPseudoClass("opened", true);
mMenuNotification->SetPseudoClass("done", false);
}
}
if (consume_menu_notification_request()) {
if (mMenuNotification == nullptr) {
mMenuNotification = create_menu_notification(mDocument);
}
mMenuNotificationStartTime = clock::now();
}
auto& toasts = get_toasts();
if (mCurrentToast == nullptr) {
@@ -123,8 +251,7 @@ void Overlay::update() {
// Fallback for large gaps in time where we never actually opened it
!mCurrentToast->IsPseudoClassSet("opened"))
{
mCurrentToast->GetParentNode()->RemoveChild(mCurrentToast);
mCurrentToast = nullptr;
remove_element(mCurrentToast);
toasts.pop_front();
} else {
mCurrentToast->RemoveAttribute("open");