Skip to content

Commit

Permalink
obs-text: Add gradient feature
Browse files Browse the repository at this point in the history
Closes #636
  • Loading branch information
Michael Fabian Dirks authored and jp9000 committed Sep 29, 2016
1 parent 444f7e3 commit 4bafe66
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 4 additions & 0 deletions plugins/obs-text/data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Filter.TextFiles="Text Files"
Filter.AllFiles="All Files"
Color="Color"
Opacity="Opacity"
Gradient="Gradient"
Gradient.Color="Gradient Color"
Gradient.Opacity="Gradient Opacity"
Gradient.Direction="Gradient Direction"
BkColor="Background Color"
BkOpacity="Background Opacity"
Alignment="Alignment"
Expand Down
54 changes: 52 additions & 2 deletions plugins/obs-text/gdiplus/obs-text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ using namespace Gdiplus;
#define S_FILE "file"
#define S_TEXT "text"
#define S_COLOR "color"
#define S_GRADIENT "gradient"
#define S_GRADIENT_COLOR "gradient_color"
#define S_GRADIENT_DIR "gradient_dir"
#define S_GRADIENT_OPACITY "gradient_opacity"
#define S_ALIGN "align"
#define S_VALIGN "valign"
#define S_OPACITY "opacity"
Expand Down Expand Up @@ -73,6 +77,10 @@ using namespace Gdiplus;
#define T_FILE T_("TextFile")
#define T_TEXT T_("Text")
#define T_COLOR T_("Color")
#define T_GRADIENT T_("Gradient")
#define T_GRADIENT_COLOR T_("Gradient.Color")
#define T_GRADIENT_DIR T_("Gradient.Direction")
#define T_GRADIENT_OPACITY T_("Gradient.Opacity")
#define T_ALIGN T_("Alignment")
#define T_VALIGN T_("VerticalAlignment")
#define T_OPACITY T_("Opacity")
Expand Down Expand Up @@ -195,11 +203,15 @@ struct TextSource {
wstring face;
int face_size = 0;
uint32_t color = 0xFFFFFF;
uint32_t color2 = 0xFFFFFF;
float gradient_dir = 0;
uint32_t opacity = 100;
uint32_t opacity2 = 100;
uint32_t bk_color = 0;
uint32_t bk_opacity = 0;
Align align = Align::Left;
VAlign valign = VAlign::Top;
bool gradient = false;
bool bold = false;
bool italic = false;
bool underline = false;
Expand Down Expand Up @@ -515,7 +527,10 @@ void TextSource::RenderText()
bits.get());

Graphics graphics_bitmap(&bitmap);
SolidBrush brush(Color(get_alpha_val(opacity) | (color & 0xFFFFFF)));
LinearGradientBrush brush(RectF(0, 0, (float)size.cx, (float)size.cy),
Color(calc_color(color, opacity)),
Color(calc_color(color2, opacity2)),
gradient_dir, 1);
DWORD full_bk_color = bk_color & 0xFFFFFF;

if (!text.empty() || use_extents)
Expand Down Expand Up @@ -625,6 +640,10 @@ inline void TextSource::Update(obs_data_t *s)
const char *valign_str = obs_data_get_string(s, S_VALIGN);
uint32_t new_color = obs_data_get_uint32(s, S_COLOR);
uint32_t new_opacity = obs_data_get_uint32(s, S_OPACITY);
bool gradient = obs_data_get_bool(s, S_GRADIENT);
uint32_t new_color2 = obs_data_get_uint32(s, S_GRADIENT_COLOR);
uint32_t new_opacity2 = obs_data_get_uint32(s, S_GRADIENT_OPACITY);
float new_grad_dir = (float)obs_data_get_double(s, S_GRADIENT_DIR);
bool new_vertical = obs_data_get_bool(s, S_VERTICAL);
bool new_outline = obs_data_get_bool(s, S_OUTLINE);
uint32_t new_o_color = obs_data_get_uint32(s, S_OUTLINE_COLOR);
Expand Down Expand Up @@ -674,11 +693,15 @@ inline void TextSource::Update(obs_data_t *s)
/* ----------------------------- */

new_color = rgb_to_bgr(new_color);
new_color2 = rgb_to_bgr(new_color2);
new_o_color = rgb_to_bgr(new_o_color);
new_bk_color = rgb_to_bgr(new_bk_color);

color = new_color;
opacity = new_opacity;
color2 = new_color2;
opacity2 = new_opacity2;
gradient_dir = new_grad_dir;
vertical = new_vertical;

bk_color = new_bk_color;
Expand All @@ -688,6 +711,11 @@ inline void TextSource::Update(obs_data_t *s)
extents_cx = n_extents_cx;
extents_cy = n_extents_cy;

if (!gradient) {
color2 = color;
opacity2 = opacity;
}

read_from_file = new_use_file;

chatlog_mode = new_chat_mode;
Expand Down Expand Up @@ -807,6 +835,17 @@ static bool chatlog_mode_changed(obs_properties_t *props, obs_property_t *p,
return true;
}

static bool gradient_changed(obs_properties_t *props, obs_property_t *p,
obs_data_t *s)
{
bool gradient = obs_data_get_bool(s, S_GRADIENT);

set_vis(gradient, S_GRADIENT_COLOR, true);
set_vis(gradient, S_GRADIENT_OPACITY, true);
set_vis(gradient, S_GRADIENT_DIR, true);
return true;
}

static bool extents_modified(obs_properties_t *props, obs_property_t *p,
obs_data_t *s)
{
Expand Down Expand Up @@ -855,8 +894,16 @@ static obs_properties_t *get_properties(void *data)

obs_properties_add_bool(props, S_VERTICAL, T_VERTICAL);
obs_properties_add_color(props, S_COLOR, T_COLOR);

obs_properties_add_int_slider(props, S_OPACITY, T_OPACITY, 0, 100, 1);

p = obs_properties_add_bool(props, S_GRADIENT, T_GRADIENT);
obs_property_set_modified_callback(p, gradient_changed);

obs_properties_add_color(props, S_GRADIENT_COLOR, T_GRADIENT_COLOR);
obs_properties_add_int_slider(props, S_GRADIENT_OPACITY,
T_GRADIENT_OPACITY, 0, 100, 1);
obs_properties_add_float_slider(props, S_GRADIENT_DIR,
T_GRADIENT_DIR, 0, 360, 0.1);

obs_properties_add_color(props, S_BKCOLOR, T_BKCOLOR);
obs_properties_add_int_slider(props, S_BKOPACITY, T_BKOPACITY,
Expand Down Expand Up @@ -937,6 +984,9 @@ bool obs_module_load(void)
obs_data_set_default_string(settings, S_VALIGN, S_VALIGN_TOP);
obs_data_set_default_int(settings, S_COLOR, 0xFFFFFF);
obs_data_set_default_int(settings, S_OPACITY, 100);
obs_data_set_default_int(settings, S_GRADIENT_COLOR, 0xFFFFFF);
obs_data_set_default_int(settings, S_GRADIENT_OPACITY, 100);
obs_data_set_default_double(settings, S_GRADIENT_DIR, 90.0);
obs_data_set_default_int(settings, S_BKCOLOR, 0x000000);
obs_data_set_default_int(settings, S_BKOPACITY, 0);
obs_data_set_default_int(settings, S_OUTLINE_SIZE, 2);
Expand Down

0 comments on commit 4bafe66

Please sign in to comment.