GUI2: convert internal color handling to use SDL_Color

This commit is contained in:
Charles Dang 2016-11-23 19:39:12 +11:00
parent c6fbe9a94c
commit 3203a8de10
7 changed files with 59 additions and 45 deletions

View file

@ -82,13 +82,14 @@ namespace
/***** ***** ***** ***** ***** DRAWING PRIMITIVES ***** ***** ***** ***** *****/
static void set_renderer_color(SDL_Renderer* renderer, Uint32 color)
static void set_renderer_color(SDL_Renderer* renderer, SDL_Color color)
{
SDL_SetRenderDrawColor(renderer,
(color & 0xFF000000) >> 24,
(color & 0x00FF0000) >> 16,
(color & 0x0000FF00) >> 8,
(color & 0x000000FF));
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
}
static bool color_empty(const SDL_Color& color)
{
return color.r == 0 && color.g == 0 && color.b == 0 && color.a == 0;
}
/**
@ -109,7 +110,7 @@ static void set_renderer_color(SDL_Renderer* renderer, Uint32 color)
*/
static void draw_line(surface& canvas,
SDL_Renderer* renderer,
Uint32 color,
SDL_Color color,
unsigned x1,
unsigned y1,
const unsigned x2,
@ -151,7 +152,7 @@ static void draw_line(surface& canvas,
*/
static void draw_circle(surface& canvas,
SDL_Renderer* renderer,
Uint32 color,
SDL_Color color,
const int x_center,
const int y_center,
const int radius)
@ -228,7 +229,7 @@ private:
alpha_; /**< Alpha value override computed as a formula. */
/** The color of the line. */
Uint32 color_;
SDL_Color color_;
/**
* The thickness of the line.
@ -585,9 +586,9 @@ void line_shape::draw(surface& canvas,
const unsigned alpha = alpha_(variables);
// Override alpha from color with formula.
const Uint32 final_color = alpha_.has_formula()
? (color_ & 0xFFFFFF00) + (alpha & 0xFF)
: color_;
if(alpha_.has_formula()) {
color_.a = alpha;
}
DBG_GUI_D << "Line: draw from " << x1 << ',' << y1 << " to " << x2 << ','
<< y2 << " canvas size " << canvas->w << ',' << canvas->h
@ -604,7 +605,7 @@ void line_shape::draw(surface& canvas,
// lock the surface
surface_lock locker(canvas);
draw_line(canvas, renderer, final_color, x1, y1, x2, y2);
draw_line(canvas, renderer, color_, x1, y1, x2, y2);
}
/***** ***** ***** ***** ***** Rectangle ***** ***** ***** ***** *****/
@ -645,14 +646,14 @@ private:
*
* If the color is fully transparent the border isn't drawn.
*/
Uint32 border_color_;
SDL_Color border_color_;
/**
* The border color of the rectangle.
*
* If the color is fully transparent the rectangle won't be filled.
*/
Uint32 fill_color_;
SDL_Color fill_color_;
};
/*WIKI
@ -697,7 +698,7 @@ rectangle_shape::rectangle_shape(const config& cfg)
, border_color_(decode_color(cfg["border_color"]))
, fill_color_(decode_color(cfg["fill_color"]))
{
if(border_color_ == 0) {
if(color_empty(border_color_)) {
border_thickness_ = 0;
}
@ -747,7 +748,7 @@ void rectangle_shape::draw(surface& canvas,
}
// Fill the background, if applicable
if(fill_color_ && w && h) {
if(!color_empty(fill_color_) && w && h) {
set_renderer_color(renderer, fill_color_);
SDL_Rect area {
@ -787,7 +788,7 @@ private:
radius_; /**< The radius of the circle. */
/** The color of the circle. */
Uint32 color_;
SDL_Color color_;
};
/*WIKI
@ -1210,7 +1211,7 @@ private:
typed_formula<PangoAlignment> text_alignment_;
/** The color of the text. */
Uint32 color_;
SDL_Color color_;
/** The text to draw. */
typed_formula<t_string> text_;

View file

@ -22,6 +22,7 @@
class config;
class CVideo;
struct SDL_Color;
namespace gui2
{
@ -66,7 +67,7 @@ public:
std::string linked_group;
int debug_border_mode;
unsigned debug_border_color;
SDL_Color debug_border_color;
};
typedef std::shared_ptr<builder_widget> builder_widget_ptr;

View file

@ -24,6 +24,8 @@
#include "formula/string_utils.hpp"
#include "tstring.hpp"
#include <SDL.h>
namespace gui2
{
@ -72,23 +74,20 @@ font::pango_text::FONT_STYLE decode_font_style(const std::string& style)
return font_style_map[style];
}
uint32_t decode_color(const std::string& color)
SDL_Color decode_color(const std::string& color)
{
std::vector<std::string> fields = utils::split(color);
// make sure we have four fields
while(fields.size() < 4)
// Make sure we have four fields
while(fields.size() < 4) {
fields.push_back("0");
uint32_t result = 0;
for(int i = 0; i < 4; ++i) {
// shift the previous value before adding, since it's a nop on the
// first run there's no need for an if.
result = result << 8;
result |= lexical_cast_default<int>(fields[i]);
}
return result;
return {
static_cast<Uint8>(std::stoul(fields[0])),
static_cast<Uint8>(std::stoul(fields[1])),
static_cast<Uint8>(std::stoul(fields[2])),
static_cast<Uint8>(std::stoul(fields[3]))};
}
PangoAlignment decode_text_alignment(const std::string& alignment)

View file

@ -21,9 +21,9 @@
#include <pango/pango-layout.h>
#include <cstdint>
#include <string>
struct SDL_Color;
struct SDL_Rect;
class surface;
class t_string;
@ -65,7 +65,7 @@ SDL_Rect create_rect(const point& origin, const point& size);
*
* @returns The color.
*/
uint32_t decode_color(const std::string& color);
SDL_Color decode_color(const std::string& color);
/**
* Converts a text alignment string to a text alignment.

View file

@ -684,7 +684,7 @@ void builder_styled_widget::init_control(styled_widget* control) const
control->set_use_tooltip_on_label_overflow(use_tooltip_on_label_overflow);
control->set_use_markup(use_markup);
control->set_debug_border_mode(debug_border_mode);
control->set_debug_border_colour(debug_border_color);
control->set_debug_border_color(debug_border_color);
}
widget* builder_styled_widget::build(const replacements_map& /*replacements*/) const

View file

@ -42,7 +42,7 @@ widget::widget()
, redraw_action_(redraw_action::full)
, clipping_rectangle_()
, debug_border_mode_(0)
, debug_border_colour_(0)
, debug_border_color_({0,0,0,0})
{
DBG_GUI_LF << "widget create: " << static_cast<void*>(this) << "\n";
}
@ -64,7 +64,7 @@ widget::widget(const builder_widget& builder)
, redraw_action_(redraw_action::full)
, clipping_rectangle_()
, debug_border_mode_(builder.debug_border_mode)
, debug_border_colour_(builder.debug_border_color)
, debug_border_color_(builder.debug_border_color)
{
DBG_GUI_LF << "widget create: " << static_cast<void*>(this) << "\n";
}
@ -480,9 +480,9 @@ void widget::set_debug_border_mode(const unsigned debug_border_mode)
debug_border_mode_ = debug_border_mode;
}
void widget::set_debug_border_colour(const unsigned debug_border_colour)
void widget::set_debug_border_color(const SDL_Color debug_border_colour)
{
debug_border_colour_ = debug_border_colour;
debug_border_color_ = debug_border_colour;
}
void widget::draw_debug_border(surface& frame_buffer)
@ -490,17 +490,23 @@ void widget::draw_debug_border(surface& frame_buffer)
SDL_Rect r = redraw_action_ == redraw_action::partly ? clipping_rectangle_
: get_rectangle();
// TODO: maybe should make these functions take an SDL_Color
Uint32 c = SDL_MapRGBA(frame_buffer->format,
debug_border_color_.r,
debug_border_color_.g,
debug_border_color_.b,
debug_border_color_.a);
switch(debug_border_mode_) {
case 0:
/* DO NOTHING */
break;
case 1:
sdl::draw_rectangle(
r.x, r.y, r.w, r.h, debug_border_colour_, frame_buffer);
sdl::draw_rectangle(r.x, r.y, r.w, r.h, c, frame_buffer);
break;
case 2:
sdl::fill_rect(frame_buffer, &r, debug_border_colour_);
sdl::fill_rect(frame_buffer, &r, c);
break;
default:
@ -515,6 +521,13 @@ widget::draw_debug_border(surface& frame_buffer, int x_offset, int y_offset)
? calculate_clipping_rectangle(x_offset, y_offset)
: calculate_blitting_rectangle(x_offset, y_offset);
// TODO: maybe should make these functions take an SDL_Color
Uint32 c = SDL_MapRGBA(frame_buffer->format,
debug_border_color_.r,
debug_border_color_.g,
debug_border_color_.b,
debug_border_color_.a);
switch(debug_border_mode_) {
case 0:
/* DO NOTHING */
@ -522,11 +535,11 @@ widget::draw_debug_border(surface& frame_buffer, int x_offset, int y_offset)
case 1:
sdl::draw_rectangle(
r.x, r.y, r.w, r.h, debug_border_colour_, frame_buffer);
r.x, r.y, r.w, r.h, c, frame_buffer);
break;
case 2:
sdl::fill_rect(frame_buffer, &r, debug_border_colour_);
sdl::fill_rect(frame_buffer, &r, c);
break;
default:

View file

@ -666,7 +666,7 @@ public:
void set_debug_border_mode(const unsigned debug_border_mode);
void set_debug_border_colour(const unsigned debug_border_colour);
void set_debug_border_color(const SDL_Color debug_border_colour);
/*** *** *** *** *** *** *** *** Members. *** *** *** *** *** *** *** ***/
@ -705,7 +705,7 @@ private:
unsigned debug_border_mode_;
/** The colour for the debug border. */
unsigned debug_border_colour_;
SDL_Color debug_border_color_;
void draw_debug_border(surface& frame_buffer);
void draw_debug_border(surface& frame_buffer, int x_offset, int y_offset);