Replace MessageDialog with new Alert widget

This commit is contained in:
rock88 2020-06-16 20:28:39 +03:00
parent d24560b56d
commit 9ca9468664
9 changed files with 164 additions and 33 deletions

View file

@ -124,7 +124,8 @@ MOONLIGHT_LIBRETRO_CXX_SOURCES = \
Logger.cpp \
LogsWindow.cpp \
GamepadMapper.cpp \
InputSettingsWindow.cpp
InputSettingsWindow.cpp \
Alert.cpp
MOONLIGHT_COMMON_C_SOURCES = \
callbacks.c \

View file

@ -103,6 +103,7 @@
36DFE0CD2459FA3F00FC51CE /* nanogui_resources.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 36DFE0CB2459FA3F00FC51CE /* nanogui_resources.cpp */; };
36DFE0CE2459FAB100FC51CE /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 36DBDEA22450C2640057C8D3 /* OpenGL.framework */; };
36E63790247010C70032F5FB /* Data.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 36E6378E247010C70032F5FB /* Data.cpp */; };
36EB490F249927C60059EDB7 /* Alert.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 36EB490D249927C60059EDB7 /* Alert.cpp */; };
36F16475247473A300D70AD9 /* mbedtls_to_openssl_wrapper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 36F16474247473A300D70AD9 /* mbedtls_to_openssl_wrapper.cpp */; };
/* End PBXBuildFile section */
@ -324,6 +325,8 @@
36E6378E247010C70032F5FB /* Data.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Data.cpp; sourceTree = "<group>"; };
36E6378F247010C70032F5FB /* Data.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Data.hpp; sourceTree = "<group>"; };
36EB490A24954B5B0059EDB7 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
36EB490D249927C60059EDB7 /* Alert.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Alert.cpp; sourceTree = "<group>"; };
36EB490E249927C60059EDB7 /* Alert.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Alert.hpp; sourceTree = "<group>"; };
36F1646F2474736E00D70AD9 /* switch_wrapper.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = switch_wrapper.c; sourceTree = "<group>"; };
36F164712474736E00D70AD9 /* evp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = evp.h; sourceTree = "<group>"; };
36F164722474736E00D70AD9 /* rand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rand.h; sourceTree = "<group>"; };
@ -742,6 +745,8 @@
36DFDCF32459F7A200FC51CE /* Application.hpp */,
3652F081245C60D1001FABF3 /* LoadingOverlay.cpp */,
3652F082245C60D1001FABF3 /* LoadingOverlay.hpp */,
36EB490D249927C60059EDB7 /* Alert.cpp */,
36EB490E249927C60059EDB7 /* Alert.hpp */,
);
path = ui;
sourceTree = "<group>";
@ -932,6 +937,7 @@
3652EFE7245B3B00001FABF3 /* label.cpp in Sources */,
36BFCCF62479724A00245D40 /* GameStreamClient.cpp in Sources */,
3652EFDA245B3B00001FABF3 /* layout.cpp in Sources */,
36EB490F249927C60059EDB7 /* Alert.cpp in Sources */,
364A6A8F24786E2200460028 /* BoxArtManager.cpp in Sources */,
3652F080245C292B001FABF3 /* ByteBuffer.c in Sources */,
3652EFF3245B3B00001FABF3 /* slider.cpp in Sources */,

100
src/ui/Alert.cpp Normal file
View file

@ -0,0 +1,100 @@
#include "Alert.hpp"
#include "nanovg.h"
using namespace nanogui;
Alert::Alert(Widget* parent, const std::string &title, const std::string &message, bool add_ok_button): Widget(parent->screen()) {
set_fixed_size(parent->screen()->size());
m_container = add<Widget>();
m_container->set_layout(new BoxLayout(Orientation::Vertical, Alignment::Middle, 10, 20));
m_container->add<Label>(title)->set_font_size(40);
m_container->add<Label>(message)->set_font_size(32);
m_button_container = m_container->add<Widget>();
m_button_container->set_layout(new BoxLayout(Orientation::Vertical, Alignment::Middle, 0, 10));
screen()->perform_layout();
if (add_ok_button) {
add_button("OK");
}
}
void Alert::perform_layout(NVGcontext *ctx) {
set_fixed_size(screen()->size());
m_button_container->set_fixed_width(screen()->width() / 2);
m_container->set_fixed_width(screen()->width() / 2);
m_container->perform_layout(ctx);
for (auto child: m_button_container->children()) {
child->set_fixed_width(m_container->fixed_width() - 20);
}
m_button_container->perform_layout(ctx);
m_container->set_position(Vector2i(screen()->width() / 4, screen()->height() / 4));
Widget::perform_layout(ctx);
}
void Alert::draw(NVGcontext *ctx) {
nvgSave(ctx);
// Draw bg
nvgFillColor(ctx, Color(0, 0, 0, 100));
nvgBeginPath(ctx);
nvgRect(ctx, 0, 0, width(), height());
nvgFill(ctx);
// Draw container bg
nvgFillColor(ctx, Color(48, 48, 48, 255));
nvgBeginPath(ctx);
nvgRect(ctx, m_container->absolute_position().x(), m_container->absolute_position().y(), m_container->width(), m_container->height());
nvgFill(ctx);
// Draw header
nvgFillColor(ctx, Color(62, 78, 184, 255));
nvgBeginPath(ctx);
nvgRect(ctx, m_container->absolute_position().x(), m_container->absolute_position().y(), m_container->width(), 60);
nvgFill(ctx);
nvgRestore(ctx);
Widget::draw(ctx);
}
void Alert::add_button(const std::string &caption, const std::function<void()> &callback) {
auto button = m_button_container->add<Button>(caption);
button->set_fixed_width(m_container->fixed_width() - 20);
button->set_fixed_height(48);
button->set_callback([this, callback] {
dispose();
if (callback != nullptr) {
callback();
}
});
screen()->perform_layout();
}
bool Alert::mouse_enter_event(const nanogui::Vector2i &p, bool enter) {
return true;
}
bool Alert::mouse_button_event(const nanogui::Vector2i &p, int button, bool down, int modifiers) {
Widget::mouse_button_event(p, button, down, modifiers);
return true;
}
bool Alert::scroll_event(const nanogui::Vector2i &p, const nanogui::Vector2f &rel) {
return true;
}
void Alert::dispose() {
auto m_screen = screen();
m_screen->update_focus(nullptr);
m_screen->remove_child(this);
m_screen->perform_layout();
}

22
src/ui/Alert.hpp Normal file
View file

@ -0,0 +1,22 @@
#include <nanogui/nanogui.h>
#pragma once
class Alert: public nanogui::Widget {
public:
Alert(nanogui::Widget* parent, const std::string &title, const std::string &message, bool add_ok_button = true);
void perform_layout(NVGcontext *ctx) override;
void draw(NVGcontext *ctx) override;
void add_button(const std::string &caption, const std::function<void()> &callback = nullptr);
bool mouse_enter_event(const nanogui::Vector2i &p, bool enter) override;
bool mouse_button_event(const nanogui::Vector2i &p, int button, bool down, int modifiers) override;
bool scroll_event(const nanogui::Vector2i &p, const nanogui::Vector2f &rel) override;
void dispose();
private:
nanogui::Widget* m_container;
nanogui::Widget* m_button_container;
};

View file

@ -1,6 +1,7 @@
#include "AddHostWindow.hpp"
#include "GameStreamClient.hpp"
#include "LoadingOverlay.hpp"
#include "Alert.hpp"
using namespace nanogui;
@ -65,7 +66,7 @@ AddHostWindow::AddHostWindow(Widget *parent): ContentWindow(parent, "Add Host")
if (result.isSuccess()) {
this->pop();
} else {
screen()->add<MessageDialog>(MessageDialog::Type::Warning, "Error", result.error());
screen()->add<Alert>("Error", result.error());
}
});
}

View file

@ -3,6 +3,7 @@
#include "AppButton.hpp"
#include "StreamWindow.hpp"
#include "GamepadMapper.hpp"
#include "Alert.hpp"
using namespace nanogui;
@ -57,12 +58,12 @@ void AppListWindow::reload(std::function<void()> callback) {
callback();
}
} else {
screen()->add<MessageDialog>(MessageDialog::Type::Warning, "Error", result.error());
screen()->add<Alert>("Error", result.error());
}
});
} else {
loader->dispose();
screen()->add<MessageDialog>(MessageDialog::Type::Warning, "Error", result.error());
screen()->add<Alert>("Error", result.error());
}
});
}
@ -74,17 +75,16 @@ void AppListWindow::run_game(int app_id) {
GamepadMapper::mapper()->load_gamepad_map(app_id);
push<StreamWindow>(m_address, app_id);
} else {
auto dialog = screen()->add<MessageDialog>(MessageDialog::Type::Information, "Info", "Another game already running", "Resume", "Close", true);
dialog->set_callback([this, app_id, current_app_id](auto result) {
if (result == 0) {
GamepadMapper::mapper()->load_gamepad_map(current_app_id);
push<StreamWindow>(m_address, current_app_id);
} else {
close_game([this, app_id] {
GamepadMapper::mapper()->load_gamepad_map(app_id);
push<StreamWindow>(m_address, app_id);
});
}
auto alert = screen()->add<Alert>("Info", "Another game already running", false);
alert->add_button("Resume", [this, current_app_id] {
GamepadMapper::mapper()->load_gamepad_map(current_app_id);
push<StreamWindow>(m_address, current_app_id);
});
alert->add_button("Close", [this, app_id] {
close_game([this, app_id] {
GamepadMapper::mapper()->load_gamepad_map(app_id);
push<StreamWindow>(m_address, app_id);
});
});
}
}
@ -98,7 +98,7 @@ void AppListWindow::close_game(std::function<void()> callback) {
if (result.isSuccess()) {
reload(callback);
} else {
screen()->add<MessageDialog>(MessageDialog::Type::Warning, "Error", result.error());
screen()->add<Alert>("Error", result.error());
}
});
}

View file

@ -1,6 +1,7 @@
#include "ContentWindow.hpp"
#include "Application.hpp"
#include "LoadingOverlay.hpp"
#include "Alert.hpp"
#include "nanovg.h"
#include <nanogui/opengl.h>
#include <math.h>
@ -238,24 +239,24 @@ bool ContentWindow::gamepad_button_event(int jid, int button, int action) {
return false;
}
std::vector<MessageDialog *> messages;
std::vector<Alert *> alerts;
std::vector<LoadingOverlay *> loaders;
for (auto child: screen()->children()) {
if (auto message = dynamic_cast<MessageDialog *>(child)) {
messages.push_back(message);
if (auto alert = dynamic_cast<Alert *>(child)) {
alerts.push_back(alert);
}
if (auto loader = dynamic_cast<LoadingOverlay *>(child)) {
loaders.push_back(loader);
}
}
bool handle_button = messages.empty() && loaders.empty();
bool handle_button = alerts.empty() && loaders.empty();
if (button == NANOGUI_GAMEPAD_BUTTON_B) {
if (!messages.empty()) {
for (auto message: messages) {
message->dispose();
if (!alerts.empty()) {
for (auto alert: alerts) {
alert->dispose();
}
return false;
}
@ -266,8 +267,8 @@ bool ContentWindow::gamepad_button_event(int jid, int button, int action) {
pop();
return false;
} else if (!messages.empty()) {
find_new_selectable(selectables_child_recursive(messages.front()), jid, button, action);
} else if (!alerts.empty()) {
find_new_selectable(selectables_child_recursive(alerts.front()), jid, button, action);
}
if (!handle_button) {

View file

@ -8,6 +8,7 @@
#include "SettingsWindow.hpp"
#include "Settings.hpp"
#include "InputSettingsWindow.hpp"
#include "Alert.hpp"
#include "nanovg.h"
using namespace nanogui;
@ -55,17 +56,15 @@ void MainWindow::reload() {
if (result.isSuccess()) {
reload();
} else {
screen()->add<MessageDialog>(MessageDialog::Type::Warning, "Error", result.error());
screen()->add<Alert>("Error", result.error());
}
});
}
} else {
auto alert = screen()->add<MessageDialog>(MessageDialog::Type::Warning, "Error", "Innactive host...", "OK", "Delete", true);
alert->set_callback([this, button](int action) {
if (action) {
Settings::settings()->remove_host(button->address());
reload();
}
auto alert = screen()->add<Alert>("Error", "Innactive host...");
alert->add_button("Delete", [this, button] {
Settings::settings()->remove_host(button->address());
reload();
});
}
});

View file

@ -1,5 +1,6 @@
#include "StreamWindow.hpp"
#include "LoadingOverlay.hpp"
#include "Alert.hpp"
#include "InputController.hpp"
#include "FFmpegVideoDecoder.hpp"
#include "GLVideoRenderer.hpp"
@ -38,7 +39,7 @@ StreamWindow::StreamWindow(Widget *parent, const std::string &address, int app_i
if (result.isSuccess()) {
//
} else {
screen()->add<MessageDialog>(MessageDialog::Type::Warning, "Error", result.error());
screen()->add<Alert>("Error", result.error());
auto app = static_cast<Application *>(screen());
app->pop_window();