xemu/ui/xui/misc.hh
Matt Borgerson 9c06980275 ui: Redesign user interface
Introduces a new user interface that looks much nicer, is easier to
navigate with controllers, provides more context to users, and is
scalable. Some additional features are included.

* Adds 'popup menu' with actions that can be used easily from controller
* Adds 'main menu', unifying other configuration dialogs
* Adds port-forwarding user interface
* Adds screenshot feature
* Adds volume control feature
* Adds gamepad auto-bind option
* Adds vsync configuration option
* Adds auto UI scaling
* Adds preferred window size selection
* Adds AV pack selection
* Exposes some existing config items in GUI
2022-05-07 16:09:34 -07:00

107 lines
3.4 KiB
C++

//
// xemu User Interface
//
// Copyright (C) 2020-2022 Matt Borgerson
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#pragma once
#include <string>
#include <memory>
#include <stdexcept>
#include <cstdio>
#include "common.hh"
#include "xemu-hud.h"
extern "C" {
#include <noc_file_dialog.h>
}
static inline
bool IsNavInputPressed(ImGuiNavInput i) {
ImGuiIO &io = ImGui::GetIO();
return io.NavInputs[i] > 0.0f && io.NavInputsDownDuration[i] == 0.0f;
}
static inline const char *PausedFileOpen(int flags, const char *filters,
const char *default_path,
const char *default_name)
{
bool is_running = runstate_is_running();
if (is_running) {
vm_stop(RUN_STATE_PAUSED);
}
const char *r = noc_file_dialog_open(flags, filters, default_path, default_name);
if (is_running) {
vm_start();
}
return r;
}
template<typename ... Args>
std::string string_format( const std::string& format, Args ... args )
{
int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
if( size_s <= 0 ){ throw std::runtime_error( "Error during formatting." ); }
auto size = static_cast<size_t>( size_s );
std::unique_ptr<char[]> buf( new char[ size ] );
std::snprintf( buf.get(), size, format.c_str(), args ... );
return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}
static inline bool IsShortcutKeyPressed(int scancode)
{
ImGuiIO& io = ImGui::GetIO();
const bool is_osx = io.ConfigMacOSXBehaviors;
const bool is_shortcut_key = (is_osx ? (io.KeySuper && !io.KeyCtrl) : (io.KeyCtrl && !io.KeySuper)) && !io.KeyAlt && !io.KeyShift; // OS X style: Shortcuts using Cmd/Super instead of Ctrl
return is_shortcut_key && ImGui::IsKeyPressed(scancode);
}
static inline float mix(float a, float b, float t)
{
return a*(1.0-t) + (b-a)*t;
}
static inline
int PushWindowTransparencySettings(bool transparent, float alpha_transparent = 0.4, float alpha_opaque = 1.0)
{
float alpha = transparent ? alpha_transparent : alpha_opaque;
ImVec4 c;
c = ImGui::GetStyle().Colors[transparent ? ImGuiCol_WindowBg : ImGuiCol_TitleBg];
c.w *= alpha;
ImGui::PushStyleColor(ImGuiCol_TitleBg, c);
c = ImGui::GetStyle().Colors[transparent ? ImGuiCol_WindowBg : ImGuiCol_TitleBgActive];
c.w *= alpha;
ImGui::PushStyleColor(ImGuiCol_TitleBgActive, c);
c = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
c.w *= alpha;
ImGui::PushStyleColor(ImGuiCol_WindowBg, c);
c = ImGui::GetStyle().Colors[ImGuiCol_Border];
c.w *= alpha;
ImGui::PushStyleColor(ImGuiCol_Border, c);
c = ImGui::GetStyle().Colors[ImGuiCol_FrameBg];
c.w *= alpha;
ImGui::PushStyleColor(ImGuiCol_FrameBg, c);
return 5;
}