2020-03-11 10:44:45 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
|
|
|
* Copyright (C) 2014-2017 - Jean-André Santoni
|
|
|
|
* Copyright (C) 2015-2018 - Andre Leiradella
|
|
|
|
* Copyright (C) 2018-2020 - natinusala
|
|
|
|
*
|
|
|
|
* RetroArch 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 Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch 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 RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-09-09 03:47:32 +00:00
|
|
|
#include <retro_miscellaneous.h>
|
|
|
|
|
2020-03-11 10:44:45 +00:00
|
|
|
#include "../gfx_widgets.h"
|
|
|
|
#include "../gfx_animation.h"
|
|
|
|
#include "../gfx_display.h"
|
|
|
|
|
|
|
|
/* Constants */
|
|
|
|
#define VOLUME_DURATION 3000
|
|
|
|
|
|
|
|
enum gfx_widget_volume_icon
|
|
|
|
{
|
|
|
|
ICON_MED = 0,
|
|
|
|
ICON_MAX,
|
|
|
|
ICON_MIN,
|
|
|
|
ICON_MUTE,
|
|
|
|
|
2020-03-12 23:11:43 +00:00
|
|
|
ICON_LAST
|
2020-03-11 10:44:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char* const ICONS_NAMES[ICON_LAST] = {
|
|
|
|
"menu_volume_med.png",
|
|
|
|
"menu_volume_max.png",
|
|
|
|
"menu_volume_min.png",
|
|
|
|
"menu_volume_mute.png",
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Widget state */
|
|
|
|
struct gfx_widget_volume_state
|
|
|
|
{
|
2020-06-08 02:09:12 +00:00
|
|
|
uintptr_t tag;
|
2020-08-31 22:25:35 +00:00
|
|
|
uintptr_t textures[ICON_LAST];
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
unsigned widget_width;
|
|
|
|
unsigned widget_height;
|
|
|
|
|
|
|
|
float bar_background[16];
|
|
|
|
float bar_normal[16];
|
|
|
|
float bar_loud[16];
|
|
|
|
float bar_loudest[16];
|
2020-08-31 22:25:35 +00:00
|
|
|
float alpha;
|
|
|
|
float text_alpha;
|
|
|
|
float db;
|
|
|
|
float percent;
|
2022-07-06 12:41:15 +00:00
|
|
|
float timer; /* float alignment */
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-08-31 22:25:35 +00:00
|
|
|
bool mute;
|
2020-03-11 10:44:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct gfx_widget_volume_state gfx_widget_volume_state_t;
|
|
|
|
|
2020-03-12 16:00:13 +00:00
|
|
|
static gfx_widget_volume_state_t p_w_volume_st = {
|
|
|
|
(uintptr_t) &p_w_volume_st,
|
2020-08-31 22:25:35 +00:00
|
|
|
{0},
|
2020-03-11 10:44:45 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
COLOR_HEX_TO_FLOAT(0x1A1A1A, 1.0f),
|
|
|
|
COLOR_HEX_TO_FLOAT(0x198AC6, 1.0f),
|
|
|
|
COLOR_HEX_TO_FLOAT(0xF5DD19, 1.0f),
|
|
|
|
COLOR_HEX_TO_FLOAT(0xC23B22, 1.0f),
|
2020-08-31 22:25:35 +00:00
|
|
|
0.0f,
|
|
|
|
0.0f,
|
|
|
|
0.0f,
|
|
|
|
1.0f,
|
|
|
|
0.0f,
|
|
|
|
false
|
2020-03-11 10:44:45 +00:00
|
|
|
};
|
|
|
|
|
2020-06-08 00:31:36 +00:00
|
|
|
static void gfx_widget_volume_frame(void* data, void *user_data)
|
2020-03-11 10:44:45 +00:00
|
|
|
{
|
2020-08-31 22:25:35 +00:00
|
|
|
static float pure_white[16] = {
|
2020-08-31 21:46:24 +00:00
|
|
|
1.00, 1.00, 1.00, 1.00,
|
|
|
|
1.00, 1.00, 1.00, 1.00,
|
|
|
|
1.00, 1.00, 1.00, 1.00,
|
|
|
|
1.00, 1.00, 1.00, 1.00,
|
|
|
|
};
|
2020-10-02 20:23:51 +00:00
|
|
|
gfx_widget_volume_state_t *state = &p_w_volume_st;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
if (state->alpha > 0.0f)
|
|
|
|
{
|
2024-09-09 03:47:32 +00:00
|
|
|
char msg[NAME_MAX_LENGTH];
|
|
|
|
char percentage_msg[NAME_MAX_LENGTH];
|
2020-06-08 00:31:36 +00:00
|
|
|
video_frame_info_t *video_info = (video_frame_info_t*)data;
|
2020-09-30 17:18:40 +00:00
|
|
|
dispgfx_widget_t *p_dispwidget = (dispgfx_widget_t*)user_data;
|
|
|
|
gfx_widget_font_data_t *font_regular = &p_dispwidget->gfx_widget_fonts.regular;
|
2020-04-06 10:38:07 +00:00
|
|
|
|
2020-06-08 00:54:01 +00:00
|
|
|
void *userdata = video_info->userdata;
|
|
|
|
unsigned video_width = video_info->width;
|
|
|
|
unsigned video_height = video_info->height;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-10-01 08:50:45 +00:00
|
|
|
unsigned padding = p_dispwidget->simple_widget_padding;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-10-02 20:12:05 +00:00
|
|
|
float* backdrop_orig = p_dispwidget->backdrop_orig;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-06-08 00:54:01 +00:00
|
|
|
uintptr_t volume_icon = 0;
|
|
|
|
unsigned icon_size = state->textures[ICON_MED] ? state->widget_height : padding;
|
|
|
|
unsigned text_color = COLOR_TEXT_ALPHA(0xffffffff, (unsigned)(state->text_alpha*255.0f));
|
|
|
|
unsigned text_color_db = COLOR_TEXT_ALPHA(TEXT_COLOR_FAINT, (unsigned)(state->text_alpha*255.0f));
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-06-08 00:54:01 +00:00
|
|
|
unsigned bar_x = icon_size;
|
|
|
|
unsigned bar_height = font_regular->line_height / 2;
|
|
|
|
unsigned bar_width = state->widget_width - bar_x - padding;
|
|
|
|
unsigned bar_y = state->widget_height / 2 + bar_height;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-06-08 00:54:01 +00:00
|
|
|
float *bar_background = NULL;
|
|
|
|
float *bar_foreground = NULL;
|
|
|
|
float bar_percentage = 0.0f;
|
2021-04-05 13:13:28 +00:00
|
|
|
gfx_display_t *p_disp = (gfx_display_t*)video_info->disp_userdata;
|
2020-09-23 19:37:18 +00:00
|
|
|
gfx_display_ctx_driver_t *dispctx = p_disp->dispctx;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-04-06 10:38:07 +00:00
|
|
|
/* Note: Volume + percentage text has no component
|
|
|
|
* that extends below the baseline, so we shift
|
|
|
|
* the text down by the font descender to achieve
|
|
|
|
* better spacing */
|
2024-09-09 03:47:32 +00:00
|
|
|
unsigned volume_text_y = (bar_y / 2.0f)
|
|
|
|
+ font_regular->line_centre_offset
|
2020-06-08 00:54:01 +00:00
|
|
|
+ font_regular->line_descender;
|
2020-04-06 10:38:07 +00:00
|
|
|
|
2020-06-08 00:54:01 +00:00
|
|
|
msg[0] = '\0';
|
|
|
|
percentage_msg[0] = '\0';
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
if (state->mute)
|
2020-06-08 00:54:01 +00:00
|
|
|
volume_icon = state->textures[ICON_MUTE];
|
2020-03-11 10:44:45 +00:00
|
|
|
else if (state->percent <= 1.0f)
|
|
|
|
{
|
|
|
|
if (state->percent <= 0.5f)
|
2020-06-08 00:54:01 +00:00
|
|
|
volume_icon = state->textures[ICON_MIN];
|
2020-03-11 10:44:45 +00:00
|
|
|
else
|
2020-06-08 00:54:01 +00:00
|
|
|
volume_icon = state->textures[ICON_MED];
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-06-08 00:54:01 +00:00
|
|
|
bar_background = state->bar_background;
|
|
|
|
bar_foreground = state->bar_normal;
|
|
|
|
bar_percentage = state->percent;
|
2020-03-11 10:44:45 +00:00
|
|
|
}
|
|
|
|
else if (state->percent > 1.0f && state->percent <= 2.0f)
|
|
|
|
{
|
2020-06-08 00:54:01 +00:00
|
|
|
volume_icon = state->textures[ICON_MAX];
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-06-08 00:54:01 +00:00
|
|
|
bar_background = state->bar_normal;
|
|
|
|
bar_foreground = state->bar_loud;
|
|
|
|
bar_percentage = state->percent - 1.0f;
|
2020-03-11 10:44:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-08 00:54:01 +00:00
|
|
|
volume_icon = state->textures[ICON_MAX];
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-06-08 00:54:01 +00:00
|
|
|
bar_background = state->bar_loud;
|
|
|
|
bar_foreground = state->bar_loudest;
|
|
|
|
bar_percentage = state->percent - 2.0f;
|
2020-03-11 10:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bar_percentage > 1.0f)
|
2020-06-08 00:54:01 +00:00
|
|
|
bar_percentage = 1.0f;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
/* Backdrop */
|
|
|
|
gfx_display_set_alpha(backdrop_orig, state->alpha);
|
|
|
|
|
2021-03-25 19:12:34 +00:00
|
|
|
gfx_display_draw_quad(
|
|
|
|
p_disp,
|
|
|
|
userdata,
|
|
|
|
video_width,
|
|
|
|
video_height,
|
|
|
|
0, 0,
|
2023-01-21 16:12:58 +00:00
|
|
|
(state->mute)
|
|
|
|
? state->widget_height
|
|
|
|
: state->widget_width,
|
2021-03-25 19:12:34 +00:00
|
|
|
state->widget_height,
|
|
|
|
video_width,
|
|
|
|
video_height,
|
2021-08-22 15:57:21 +00:00
|
|
|
backdrop_orig,
|
|
|
|
NULL
|
2021-03-25 19:12:34 +00:00
|
|
|
);
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
/* Icon */
|
|
|
|
if (volume_icon)
|
|
|
|
{
|
|
|
|
gfx_display_set_alpha(pure_white, state->text_alpha);
|
|
|
|
|
2020-09-23 19:37:18 +00:00
|
|
|
if (dispctx && dispctx->blend_begin)
|
|
|
|
dispctx->blend_begin(userdata);
|
2020-03-11 10:44:45 +00:00
|
|
|
gfx_widgets_draw_icon(
|
|
|
|
userdata,
|
2020-10-02 09:36:11 +00:00
|
|
|
p_disp,
|
2020-03-11 10:44:45 +00:00
|
|
|
video_width,
|
|
|
|
video_height,
|
|
|
|
icon_size, icon_size,
|
|
|
|
volume_icon,
|
2022-07-06 09:40:59 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0.0f, /* rad */
|
|
|
|
1.0f, /* cos(rad) = cos(0) = 1.0f */
|
|
|
|
0.0f, /* sine(rad) = sine(0) = 0.0f */
|
|
|
|
pure_white
|
2020-03-11 10:44:45 +00:00
|
|
|
);
|
2020-09-23 19:37:18 +00:00
|
|
|
if (dispctx && dispctx->blend_end)
|
|
|
|
dispctx->blend_end(userdata);
|
2020-03-11 10:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state->mute)
|
|
|
|
{
|
|
|
|
if (!state->textures[ICON_MUTE])
|
|
|
|
{
|
|
|
|
const char *text = msg_hash_to_str(MSG_AUDIO_MUTED);
|
2020-04-06 10:38:07 +00:00
|
|
|
gfx_widgets_draw_text(font_regular,
|
|
|
|
text,
|
2020-06-08 00:54:01 +00:00
|
|
|
state->widget_width / 2,
|
2024-09-09 03:47:32 +00:00
|
|
|
state->widget_height / 2.0f
|
2020-06-08 00:54:01 +00:00
|
|
|
+ font_regular->line_centre_offset,
|
2020-04-06 10:38:07 +00:00
|
|
|
video_width, video_height,
|
|
|
|
text_color, TEXT_ALIGN_CENTER,
|
|
|
|
true);
|
2020-03-11 10:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Bar */
|
|
|
|
gfx_display_set_alpha(bar_background, state->text_alpha);
|
|
|
|
gfx_display_set_alpha(bar_foreground, state->text_alpha);
|
|
|
|
|
2021-03-25 19:12:34 +00:00
|
|
|
gfx_display_draw_quad(
|
|
|
|
p_disp,
|
|
|
|
userdata,
|
2020-03-11 10:44:45 +00:00
|
|
|
video_width,
|
|
|
|
video_height,
|
|
|
|
bar_x + bar_percentage * bar_width, bar_y,
|
|
|
|
bar_width - bar_percentage * bar_width, bar_height,
|
|
|
|
video_width, video_height,
|
2021-08-22 15:57:21 +00:00
|
|
|
bar_background,
|
|
|
|
NULL
|
2020-03-11 10:44:45 +00:00
|
|
|
);
|
|
|
|
|
2021-03-25 19:12:34 +00:00
|
|
|
gfx_display_draw_quad(
|
|
|
|
p_disp,
|
|
|
|
userdata,
|
2020-03-11 10:44:45 +00:00
|
|
|
video_width,
|
|
|
|
video_height,
|
|
|
|
bar_x, bar_y,
|
|
|
|
bar_percentage * bar_width, bar_height,
|
|
|
|
video_width, video_height,
|
2021-08-22 15:57:21 +00:00
|
|
|
bar_foreground,
|
|
|
|
NULL
|
2020-03-11 10:44:45 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/* Text */
|
|
|
|
snprintf(msg, sizeof(msg), (state->db >= 0 ? "+%.1f dB" : "%.1f dB"),
|
|
|
|
state->db);
|
|
|
|
|
|
|
|
snprintf(percentage_msg, sizeof(percentage_msg), "%d%%",
|
|
|
|
(int)(state->percent * 100.0f));
|
|
|
|
|
2020-04-06 10:38:07 +00:00
|
|
|
gfx_widgets_draw_text(font_regular,
|
|
|
|
msg,
|
|
|
|
state->widget_width - padding, volume_text_y,
|
|
|
|
video_width, video_height,
|
|
|
|
text_color_db,
|
|
|
|
TEXT_ALIGN_RIGHT,
|
|
|
|
false);
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-04-06 10:38:07 +00:00
|
|
|
gfx_widgets_draw_text(font_regular,
|
2020-03-11 10:44:45 +00:00
|
|
|
percentage_msg,
|
|
|
|
icon_size, volume_text_y,
|
|
|
|
video_width, video_height,
|
|
|
|
text_color,
|
|
|
|
TEXT_ALIGN_LEFT,
|
2020-04-06 10:38:07 +00:00
|
|
|
false);
|
2020-03-11 10:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_widget_volume_timer_end(void *userdata)
|
|
|
|
{
|
|
|
|
gfx_animation_ctx_entry_t entry;
|
2020-10-02 20:23:51 +00:00
|
|
|
gfx_widget_volume_state_t *state = &p_w_volume_st;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
entry.cb = NULL;
|
|
|
|
entry.duration = MSG_QUEUE_ANIMATION_DURATION;
|
|
|
|
entry.easing_enum = EASING_OUT_QUAD;
|
|
|
|
entry.subject = &state->alpha;
|
|
|
|
entry.tag = state->tag;
|
|
|
|
entry.target_value = 0.0f;
|
|
|
|
entry.userdata = NULL;
|
|
|
|
|
|
|
|
gfx_animation_push(&entry);
|
|
|
|
|
|
|
|
entry.subject = &state->text_alpha;
|
|
|
|
|
|
|
|
gfx_animation_push(&entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfx_widget_volume_update_and_show(float new_volume, bool mute)
|
|
|
|
{
|
|
|
|
gfx_timer_ctx_entry_t entry;
|
2020-10-02 20:23:51 +00:00
|
|
|
gfx_widget_volume_state_t *state = &p_w_volume_st;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
gfx_animation_kill_by_tag(&state->tag);
|
|
|
|
|
|
|
|
state->db = new_volume;
|
|
|
|
state->percent = pow(10, new_volume/20);
|
|
|
|
state->alpha = DEFAULT_BACKDROP;
|
|
|
|
state->text_alpha = 1.0f;
|
|
|
|
state->mute = mute;
|
|
|
|
|
|
|
|
entry.cb = gfx_widget_volume_timer_end;
|
|
|
|
entry.duration = VOLUME_DURATION;
|
|
|
|
entry.userdata = NULL;
|
|
|
|
|
2020-10-14 05:26:24 +00:00
|
|
|
gfx_animation_timer_start(&state->timer, &entry);
|
2020-03-11 10:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 00:31:36 +00:00
|
|
|
static void gfx_widget_volume_layout(
|
|
|
|
void *data,
|
|
|
|
bool is_threaded, const char *dir_assets, char *font_path)
|
2020-03-11 10:44:45 +00:00
|
|
|
{
|
2020-09-30 17:18:40 +00:00
|
|
|
dispgfx_widget_t *p_dispwidget = (dispgfx_widget_t*)data;
|
2020-10-02 20:23:51 +00:00
|
|
|
gfx_widget_volume_state_t *state = &p_w_volume_st;
|
2020-10-01 08:55:37 +00:00
|
|
|
unsigned last_video_width = p_dispwidget->last_video_width;
|
2020-09-30 17:18:40 +00:00
|
|
|
gfx_widget_font_data_t *font_regular = &p_dispwidget->gfx_widget_fonts.regular;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
2020-06-08 00:31:36 +00:00
|
|
|
state->widget_height = font_regular->line_height * 4;
|
|
|
|
state->widget_width = state->widget_height * 4;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
/* Volume widget cannot exceed screen width
|
|
|
|
* > If it does, scale it down */
|
|
|
|
if (state->widget_width > last_video_width)
|
|
|
|
{
|
|
|
|
state->widget_width = last_video_width;
|
|
|
|
state->widget_height = state->widget_width / 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_widget_volume_context_reset(bool is_threaded,
|
|
|
|
unsigned width, unsigned height, bool fullscreen,
|
|
|
|
const char *dir_assets, char *font_path,
|
|
|
|
char* menu_png_path,
|
|
|
|
char* widgets_png_path)
|
|
|
|
{
|
|
|
|
size_t i;
|
2020-10-02 20:23:51 +00:00
|
|
|
gfx_widget_volume_state_t *state = &p_w_volume_st;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ICON_LAST; i++)
|
|
|
|
gfx_display_reset_textures_list(ICONS_NAMES[i], menu_png_path, &state->textures[i], TEXTURE_FILTER_MIPMAP_LINEAR, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_widget_volume_context_destroy(void)
|
|
|
|
{
|
|
|
|
size_t i;
|
2020-10-02 20:23:51 +00:00
|
|
|
gfx_widget_volume_state_t *state = &p_w_volume_st;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ICON_LAST; i++)
|
|
|
|
video_driver_texture_unload(&state->textures[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gfx_widget_volume_free(void)
|
|
|
|
{
|
2020-10-02 20:23:51 +00:00
|
|
|
gfx_widget_volume_state_t *state = &p_w_volume_st;
|
2020-03-11 10:44:45 +00:00
|
|
|
|
|
|
|
/* Kill all running animations */
|
|
|
|
gfx_animation_kill_by_tag(&state->tag);
|
|
|
|
|
|
|
|
state->alpha = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gfx_widget_t gfx_widget_volume = {
|
|
|
|
NULL, /* init */
|
|
|
|
gfx_widget_volume_free,
|
|
|
|
gfx_widget_volume_context_reset,
|
|
|
|
gfx_widget_volume_context_destroy,
|
|
|
|
gfx_widget_volume_layout,
|
|
|
|
NULL, /* iterate */
|
|
|
|
gfx_widget_volume_frame
|
|
|
|
};
|