gfx_widgets: move libretro message widget into its own file

This commit is contained in:
natinusala 2020-03-17 17:16:21 +01:00
parent 3c4a959b83
commit 997136c029
6 changed files with 151 additions and 88 deletions

View File

@ -843,7 +843,8 @@ ifeq ($(HAVE_GFX_WIDGETS), 1)
OBJ += gfx/gfx_widgets.o \
gfx/widgets/gfx_widget_screenshot.o \
gfx/widgets/gfx_widget_volume.o \
gfx/widgets/gfx_widget_generic_message.o
gfx/widgets/gfx_widget_generic_message.o \
gfx/widgets/gfx_widget_libretro_message.o
endif
ifeq ($(HAVE_OVERLAY), 1)

View File

@ -286,16 +286,9 @@ static unsigned ai_service_overlay_height = 0;
static uintptr_t ai_service_overlay_texture = 0;
#endif
/* Libretro message */
static gfx_timer_t libretro_message_timer;
static char libretro_message[512] = {'\0'};
/* Metrics */
#define BASE_FONT_SIZE 32.0f
static float libretro_message_alpha = 0.0f;
static float last_scale_factor = 0.0f;
static float msg_queue_text_scale_factor = 0.0f;
static float widget_font_size = 0.0f;
@ -324,8 +317,6 @@ unsigned gfx_widgets_get_glyph_width(void)
return glyph_width;
}
static unsigned libretro_message_width = 0;
static unsigned msg_queue_height;
static unsigned msg_queue_icon_size_x;
static unsigned msg_queue_icon_size_y;
@ -372,7 +363,8 @@ unsigned gfx_widgets_get_last_video_height(void)
const static gfx_widget_t* const widgets[] = {
&gfx_widget_screenshot,
&gfx_widget_volume,
&gfx_widget_generic_message
&gfx_widget_generic_message,
&gfx_widget_libretro_message
};
static const size_t widgets_len = sizeof(widgets) / sizeof(widgets[0]);
@ -1586,27 +1578,6 @@ void gfx_widgets_frame(void *data)
}
#endif
/* Libretro message */
if (libretro_message_alpha > 0.0f)
{
unsigned text_color = COLOR_TEXT_ALPHA(0xffffffff, (unsigned)(libretro_message_alpha*255.0f));
gfx_display_set_alpha(gfx_widgets_backdrop_orig, libretro_message_alpha);
gfx_display_draw_quad(userdata,
video_width, video_height,
0, video_height - generic_message_height,
libretro_message_width, generic_message_height,
video_width, video_height,
gfx_widgets_backdrop_orig);
gfx_display_draw_text(font_regular, libretro_message,
simple_widget_padding,
video_height - generic_message_height/2 + widget_font_size/4,
video_width, video_height,
text_color, TEXT_ALIGN_LEFT,
1, false, 0, false);
}
#ifdef HAVE_CHEEVOS
/* Achievement notification */
if (cheevo_popup_queue_read_index >= 0 && cheevo_popup_queue[cheevo_popup_queue_read_index].title)
@ -2164,7 +2135,6 @@ static void gfx_widgets_achievement_next(void* userdata)
static void gfx_widgets_free(void)
{
size_t i;
gfx_animation_ctx_tag libretro_tag;
widgets_inited = false;
widgets_active = false;
@ -2231,16 +2201,6 @@ static void gfx_widgets_free(void)
video_coord_array_free(&font_raster_bold.carr);
font_driver_bind_block(NULL, NULL);
/* Reset state of all other widgets */
/* Libretro message */
libretro_tag = (uintptr_t) &libretro_message_timer;
libretro_message_alpha = 0.0f;
gfx_timer_kill(&libretro_message_timer);
gfx_animation_kill_by_tag(&libretro_tag);
/* AI Service overlay */
/* ... */
}
bool gfx_widgets_set_fps_text(const char *new_fps_text)
@ -2545,46 +2505,3 @@ void gfx_widgets_push_achievement(const char *title, const char *badge)
SLOCK_UNLOCK(cheevo_popup_queue_lock);
}
#endif
static void gfx_widgets_libretro_message_fadeout(void *userdata)
{
gfx_animation_ctx_entry_t entry;
gfx_animation_ctx_tag tag = (uintptr_t) &libretro_message_timer;
/* Start fade out animation */
entry.cb = NULL;
entry.duration = MSG_QUEUE_ANIMATION_DURATION;
entry.easing_enum = EASING_OUT_QUAD;
entry.subject = &libretro_message_alpha;
entry.tag = tag;
entry.target_value = 0.0f;
entry.userdata = NULL;
gfx_animation_push(&entry);
}
void gfx_widgets_set_libretro_message(const char *msg, unsigned duration)
{
gfx_timer_ctx_entry_t timer;
gfx_animation_ctx_tag tag = (uintptr_t) &libretro_message_timer;
if (!widgets_active)
return;
strlcpy(libretro_message, msg, sizeof(libretro_message));
libretro_message_alpha = DEFAULT_BACKDROP;
/* Kill and restart the timer / animation */
gfx_timer_kill(&libretro_message_timer);
gfx_animation_kill_by_tag(&tag);
timer.cb = gfx_widgets_libretro_message_fadeout;
timer.duration = duration;
timer.userdata = NULL;
gfx_timer_start(&libretro_message_timer, &timer);
/* Compute text width */
libretro_message_width = font_driver_get_message_width(font_regular, msg, (unsigned)strlen(msg), 1) + simple_widget_padding * 2;
}

View File

@ -122,6 +122,7 @@ typedef struct gfx_widget gfx_widget_t;
extern const gfx_widget_t gfx_widget_screenshot;
extern const gfx_widget_t gfx_widget_volume;
extern const gfx_widget_t gfx_widget_generic_message;
extern const gfx_widget_t gfx_widget_libretro_message;
bool gfx_widgets_active(void);
void gfx_widgets_set_persistence(bool persist);
@ -174,7 +175,7 @@ void gfx_widgets_push_achievement(const char *title, const char *badge);
void gfx_widget_set_message(char *message);
/* Warning: not thread safe! */
void gfx_widgets_set_libretro_message(const char *message, unsigned duration);
void gfx_widget_set_libretro_message(const char *message, unsigned duration);
/* All the functions below should be called in
* the video driver - once they are all added, set

View File

@ -0,0 +1,143 @@
/* 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/>.
*/
#include "../gfx_widgets.h"
#include "../gfx_animation.h"
#include "../gfx_display.h"
#include "../../retroarch.h"
struct gfx_widget_libretro_message_state
{
gfx_timer_t timer;
char message[512];
unsigned width;
float alpha;
};
typedef struct gfx_widget_libretro_message_state gfx_widget_libretro_message_state_t;
static gfx_widget_libretro_message_state_t p_w_libretro_message_st = {
0.0f,
{'\0'},
0,
0.0f
};
static gfx_widget_libretro_message_state_t* gfx_widget_libretro_message_get_state(void)
{
return &p_w_libretro_message_st;
}
static void gfx_widget_libretro_message_fadeout(void *userdata)
{
gfx_widget_libretro_message_state_t* state = gfx_widget_libretro_message_get_state();
gfx_animation_ctx_entry_t entry;
gfx_animation_ctx_tag tag = (uintptr_t) &state->timer;
/* Start fade out animation */
entry.cb = NULL;
entry.duration = MSG_QUEUE_ANIMATION_DURATION;
entry.easing_enum = EASING_OUT_QUAD;
entry.subject = &state->alpha;
entry.tag = tag;
entry.target_value = 0.0f;
entry.userdata = NULL;
gfx_animation_push(&entry);
}
void gfx_widget_set_libretro_message(const char *msg, unsigned duration)
{
gfx_widget_libretro_message_state_t* state = gfx_widget_libretro_message_get_state();
gfx_timer_ctx_entry_t timer;
gfx_animation_ctx_tag tag = (uintptr_t) &state->timer;
if (!gfx_widgets_active())
return;
strlcpy(state->message, msg, sizeof(state->message));
state->alpha = DEFAULT_BACKDROP;
/* Kill and restart the timer / animation */
gfx_timer_kill(&state->timer);
gfx_animation_kill_by_tag(&tag);
timer.cb = gfx_widget_libretro_message_fadeout;
timer.duration = duration;
timer.userdata = NULL;
gfx_timer_start(&state->timer, &timer);
/* Compute text width */
state->width = font_driver_get_message_width(gfx_widgets_get_font_regular(), msg, (unsigned)strlen(msg), 1) + gfx_widgets_get_padding() * 2;
}
static void gfx_widget_libretro_message_frame(void *data)
{
gfx_widget_libretro_message_state_t* state = gfx_widget_libretro_message_get_state();
if (state->alpha > 0.0f)
{
video_frame_info_t* video_info = (video_frame_info_t*)data;
void* userdata = video_info->userdata;
unsigned video_width = video_info->width;
unsigned video_height = video_info->height;
unsigned height = gfx_widgets_get_generic_message_height();
float* backdrop_orign = gfx_widgets_get_backdrop_orig();
unsigned text_color = COLOR_TEXT_ALPHA(0xffffffff, (unsigned)(state->alpha*255.0f));
gfx_display_set_alpha(backdrop_orign, state->alpha);
gfx_display_draw_quad(userdata,
video_width, video_height,
0, video_height - height,
state->width, height,
video_width, video_height,
backdrop_orign);
gfx_display_draw_text(gfx_widgets_get_font_regular(), state->message,
gfx_widgets_get_padding(),
video_height - height/2 + gfx_widgets_get_font_size()/4,
video_width, video_height,
text_color, TEXT_ALIGN_LEFT,
1, false, 0, false);
}
}
static void gfx_widget_libretro_message_free(void)
{
gfx_widget_libretro_message_state_t* state = gfx_widget_libretro_message_get_state();
gfx_animation_ctx_tag tag = (uintptr_t) &state->timer;
state->alpha = 0.0f;
gfx_timer_kill(&state->timer);
gfx_animation_kill_by_tag(&tag);
}
const gfx_widget_t gfx_widget_libretro_message = {
NULL, /* init */
gfx_widget_libretro_message_free,
NULL, /* context_reset*/
NULL, /* context_destroy */
NULL, /* layout */
NULL, /* iterate */
gfx_widget_libretro_message_frame
};

View File

@ -1264,6 +1264,7 @@ MENU
#include "../gfx/widgets/gfx_widget_screenshot.c"
#include "../gfx/widgets/gfx_widget_volume.c"
#include "../gfx/widgets/gfx_widget_generic_message.c"
#include "../gfx/widgets/gfx_widget_libretro_message.c"
#endif
#include "../input/input_osk.c"

View File

@ -11203,7 +11203,7 @@ static bool rarch_environment_cb(unsigned cmd, void *data)
RARCH_LOG("[Environ]: SET_MESSAGE: %s\n", msg->msg);
#if defined(HAVE_GFX_WIDGETS)
if (gfx_widgets_active())
gfx_widgets_set_libretro_message(msg->msg,
gfx_widget_set_libretro_message(msg->msg,
roundf((float)msg->frames / 60.0f * 1000.0f));
else
#endif