SDL_gpu support for GUI1 buttons.
This commit is contained in:
parent
ac0bfc77d2
commit
5fd034d3ef
2 changed files with 208 additions and 11 deletions
|
@ -19,6 +19,7 @@
|
|||
#include "widgets/button.hpp"
|
||||
#include "game_config.hpp"
|
||||
#include "font.hpp"
|
||||
#include "text.hpp"
|
||||
#include "marked-up_text.hpp"
|
||||
#include "image.hpp"
|
||||
#include "log.hpp"
|
||||
|
@ -43,10 +44,18 @@ const int vertical_padding = font::SIZE_SMALL / 2;
|
|||
button::button(CVideo& video, const std::string& label, button::TYPE type,
|
||||
std::string button_image_name, SPACE_CONSUMPTION spacing,
|
||||
const bool auto_join, std::string overlay_image)
|
||||
: widget(video, auto_join), type_(type), label_(label),
|
||||
: widget(video, auto_join), type_(type),
|
||||
#ifdef SDL_GPU
|
||||
label_text_(label), label_image_(),
|
||||
image_(), pressedImage_(), activeImage_(), pressedActiveImage_(),
|
||||
disabledImage_(), pressedDisabledImage_(),
|
||||
overlayImage_(), overlayPressedImage_(), overlayActiveImage_(),
|
||||
#else
|
||||
label_(),
|
||||
image_(NULL), pressedImage_(NULL), activeImage_(NULL), pressedActiveImage_(NULL),
|
||||
disabledImage_(NULL), pressedDisabledImage_(NULL),
|
||||
overlayImage_(NULL), overlayPressedImage_(NULL), overlayActiveImage_(NULL),
|
||||
#endif
|
||||
state_(NORMAL), pressed_(false),
|
||||
spacing_(spacing), base_height_(0), base_width_(0),
|
||||
button_image_name_(), button_overlay_image_name_(overlay_image),
|
||||
|
@ -94,7 +103,86 @@ void button::load_images() {
|
|||
default:
|
||||
break;
|
||||
}
|
||||
#ifdef SDL_GPU
|
||||
sdl::timage button_image(image::get_texture(button_image_name_ + ".png" + button_image_path_suffix_));
|
||||
sdl::timage pressed_image(image::get_texture(button_image_name_ + "-pressed.png"+ button_image_path_suffix_));
|
||||
sdl::timage active_image(image::get_texture(button_image_name_ + "-active.png"+ button_image_path_suffix_));
|
||||
sdl::timage disabled_image;
|
||||
if (file_exists(game_config::path + "/images/" + button_image_name_ + "-disabled.png"+ button_image_path_suffix_))
|
||||
disabled_image = image::get_texture(button_image_name_ + "-disabled.png"+ button_image_path_suffix_);
|
||||
sdl::timage pressed_disabled_image, pressed_active_image, touched_image;
|
||||
|
||||
if (!button_overlay_image_name_.empty()) {
|
||||
overlayImage_ = image::get_texture(button_overlay_image_name_ + size_postfix + ".png"+ button_image_path_suffix_);
|
||||
overlayPressedImage_ = image::get_texture(button_overlay_image_name_ + size_postfix + "-pressed.png"+ button_image_path_suffix_);
|
||||
|
||||
if (file_exists(game_config::path + "/images/" + button_overlay_image_name_ + size_postfix + "-active.png"+ button_image_path_suffix_))
|
||||
overlayActiveImage_ = image::get_texture(button_overlay_image_name_ + size_postfix + "-active.png"+ button_image_path_suffix_);
|
||||
|
||||
if (file_exists(game_config::path + "/images/" + button_overlay_image_name_ + size_postfix + "-disabled.png"+ button_image_path_suffix_))
|
||||
overlayDisabledImage_ = image::get_texture(button_overlay_image_name_ + size_postfix + "-disabled.png"+ button_image_path_suffix_);
|
||||
if (overlayDisabledImage_.null())
|
||||
overlayDisabledImage_ = image::get_texture(button_overlay_image_name_ + size_postfix + ".png~GS()" + button_image_path_suffix_);
|
||||
|
||||
if (file_exists(game_config::path + "/images/" + button_overlay_image_name_ + size_postfix + "-disabled-pressed.png"+ button_image_path_suffix_))
|
||||
overlayPressedDisabledImage_ = image::get_texture(button_overlay_image_name_ + size_postfix + "-disabled-pressed.png"+ button_image_path_suffix_);
|
||||
if (overlayPressedDisabledImage_.null())
|
||||
overlayPressedDisabledImage_ = image::get_texture(button_overlay_image_name_ + size_postfix + "-pressed.png~GS()"+ button_image_path_suffix_);
|
||||
} else {
|
||||
overlayImage_ = sdl::timage();
|
||||
}
|
||||
|
||||
if (disabled_image.null()) {
|
||||
disabled_image = image::get_texture(button_image_name_ + ".png~GS()" + button_image_path_suffix_);
|
||||
}
|
||||
|
||||
if (pressed_image.null())
|
||||
pressed_image = button_image;
|
||||
|
||||
if (active_image.null())
|
||||
active_image = button_image;
|
||||
|
||||
if (type_ == TYPE_CHECK || type_ == TYPE_RADIO) {
|
||||
touched_image = image::get_texture(button_image_name_ + "-touched.png"+ button_image_path_suffix_);
|
||||
if (touched_image.null())
|
||||
touched_image = pressed_image;
|
||||
|
||||
pressed_active_image = (image::get_texture(button_image_name_ + "-active-pressed.png"+ button_image_path_suffix_));
|
||||
if (pressed_active_image.null())
|
||||
pressed_active_image = pressed_image;
|
||||
|
||||
if (file_exists(game_config::path + "/images/" + button_image_name_ + size_postfix + "-disabled-pressed.png"+ button_image_path_suffix_))
|
||||
pressed_disabled_image = image::get_texture(button_image_name_ + "-disabled-pressed.png"+ button_image_path_suffix_);
|
||||
if (pressed_disabled_image.null())
|
||||
pressed_disabled_image = image::get_texture(button_image_name_ + "-pressed.png~GS()"+ button_image_path_suffix_);
|
||||
}
|
||||
|
||||
if (button_image.null()) {
|
||||
ERR_DP << "error initializing button!" << std::endl;
|
||||
throw error();
|
||||
}
|
||||
|
||||
base_height_ = button_image.height();
|
||||
base_width_ = button_image.width();
|
||||
|
||||
if (type_ != TYPE_IMAGE) {
|
||||
set_label(label_text_);
|
||||
}
|
||||
|
||||
image_ = button_image;
|
||||
pressedImage_ = pressed_image;
|
||||
activeImage_ = active_image;
|
||||
disabledImage_ = disabled_image;
|
||||
if (type_ == TYPE_CHECK || type_ == TYPE_RADIO) {
|
||||
pressedDisabledImage_ = pressed_disabled_image;
|
||||
pressedActiveImage_ = pressed_active_image;
|
||||
touchedImage_ = touched_image;
|
||||
}
|
||||
|
||||
if (type_ == TYPE_IMAGE){
|
||||
calculate_size();
|
||||
}
|
||||
#else
|
||||
surface button_image(image::get_image(button_image_name_ + ".png" + button_image_path_suffix_));
|
||||
surface pressed_image(image::get_image(button_image_name_ + "-pressed.png"+ button_image_path_suffix_));
|
||||
surface active_image(image::get_image(button_image_name_ + "-active.png"+ button_image_path_suffix_));
|
||||
|
@ -180,6 +268,7 @@ void button::load_images() {
|
|||
if (type_ == TYPE_IMAGE){
|
||||
calculate_size();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
button::~button()
|
||||
|
@ -190,8 +279,13 @@ void button::calculate_size()
|
|||
{
|
||||
if (type_ == TYPE_IMAGE){
|
||||
SDL_Rect loc_image = location();
|
||||
#ifdef SDL_GPU
|
||||
loc_image.h = image_.height();
|
||||
loc_image.w = image_.width();
|
||||
#else
|
||||
loc_image.h = image_->h;
|
||||
loc_image.w = image_->w;
|
||||
#endif
|
||||
set_location(loc_image);
|
||||
return;
|
||||
}
|
||||
|
@ -204,19 +298,19 @@ void button::calculate_size()
|
|||
{
|
||||
int fs = font_size;
|
||||
int style = TTF_STYLE_NORMAL;
|
||||
std::string::const_iterator i_beg = label_.begin(), i_end = label_.end(),
|
||||
std::string::const_iterator i_beg = label_text_.begin(), i_end = label_text_.end(),
|
||||
i = font::parse_markup(i_beg, i_end, &fs, NULL, &style);
|
||||
if (i != i_end) {
|
||||
std::string tmp(i, i_end);
|
||||
label_.erase(i - i_beg, i_end - i_beg);
|
||||
label_ += font::make_text_ellipsis(tmp, fs, w, style);
|
||||
label_text_.erase(i - i_beg, i_end - i_beg);
|
||||
label_text_ += font::make_text_ellipsis(tmp, fs, w, style);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type_ != TYPE_IMAGE){
|
||||
textRect_ = font::draw_text(NULL, screen_area(), font_size,
|
||||
font::BUTTON_COLOR, label_, 0, 0);
|
||||
font::BUTTON_COLOR, label_text_, 0, 0);
|
||||
}
|
||||
|
||||
if (!change_size)
|
||||
|
@ -230,7 +324,7 @@ void button::calculate_size()
|
|||
set_width(std::max(textRect_.w+horizontal_padding,base_width_));
|
||||
}
|
||||
} else {
|
||||
if(label_.empty()) {
|
||||
if(label_text_.empty()) {
|
||||
set_width(base_width_);
|
||||
} else {
|
||||
set_width(checkbox_horizontal_padding + textRect_.w + base_width_);
|
||||
|
@ -287,6 +381,92 @@ void button::enable(bool new_val)
|
|||
|
||||
void button::draw_contents()
|
||||
{
|
||||
#ifdef SDL_GPU
|
||||
sdl::timage image = image_;
|
||||
const int image_w = image_.width();
|
||||
|
||||
int offset = 0;
|
||||
switch(state_) {
|
||||
case ACTIVE:
|
||||
image = activeImage_;
|
||||
break;
|
||||
case PRESSED:
|
||||
image = pressedImage_;
|
||||
if (type_ == TYPE_PRESS)
|
||||
offset = 1;
|
||||
break;
|
||||
case PRESSED_ACTIVE:
|
||||
image = pressedActiveImage_;
|
||||
break;
|
||||
case TOUCHED_NORMAL:
|
||||
case TOUCHED_PRESSED:
|
||||
image = touchedImage_;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_Rect const &loc = location();
|
||||
SDL_Rect clipArea = loc;
|
||||
const int texty = loc.y + loc.h / 2 - textRect_.h / 2 + offset;
|
||||
int textx;
|
||||
|
||||
if (type_ != TYPE_CHECK && type_ != TYPE_RADIO && type_ != TYPE_IMAGE)
|
||||
textx = loc.x + image.width() / 2 - textRect_.w / 2 + offset;
|
||||
else {
|
||||
clipArea.w += image_w + checkbox_horizontal_padding;
|
||||
textx = loc.x + image_w + checkbox_horizontal_padding / 2;
|
||||
}
|
||||
|
||||
SDL_Color button_color = font::BUTTON_COLOR;
|
||||
|
||||
if (!enabled()) {
|
||||
|
||||
if (state_ == PRESSED || state_ == PRESSED_ACTIVE)
|
||||
image = pressedDisabledImage_;
|
||||
else image = disabledImage_;
|
||||
|
||||
button_color = font::GRAY_COLOR;
|
||||
}
|
||||
|
||||
sdl::timage overlay;
|
||||
if (!overlayImage_.null()) {
|
||||
|
||||
overlay = enabled() ? overlayImage_ : overlayDisabledImage_;
|
||||
|
||||
if (!overlayPressedImage_.null()) {
|
||||
switch (state_) {
|
||||
case ACTIVE:
|
||||
if (!overlayActiveImage_.null())
|
||||
overlay = overlayActiveImage_;
|
||||
break;
|
||||
case PRESSED:
|
||||
case PRESSED_ACTIVE:
|
||||
case TOUCHED_NORMAL:
|
||||
case TOUCHED_PRESSED:
|
||||
overlay = enabled() ? overlayPressedImage_ : overlayPressedDisabledImage_;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
image.set_scale(float(loc.w) / image.width(), float(loc.h) / image.height());
|
||||
overlay.set_scale(float(loc.w) / overlay.width(), float(loc.h) / image.height());
|
||||
|
||||
video().draw_texture(image, loc.x, loc.y);
|
||||
video().draw_texture(overlay, loc.x, loc.y);
|
||||
if (type_ != TYPE_IMAGE){
|
||||
clipArea.x += offset;
|
||||
clipArea.y += offset;
|
||||
clipArea.w -= 2*offset;
|
||||
clipArea.h -= 2*offset;
|
||||
// label_image_.set_clip(clipArea);
|
||||
video().draw_texture(label_image_, textx, texty);
|
||||
}
|
||||
#else
|
||||
surface image = image_;
|
||||
const int image_w = image_->w;
|
||||
|
||||
|
@ -372,6 +552,7 @@ void button::draw_contents()
|
|||
}
|
||||
|
||||
update_rect(loc);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool button::hit(int x, int y) const
|
||||
|
@ -397,19 +578,23 @@ void button::set_overlay(const std::string& image_file)
|
|||
|
||||
void button::set_label(const std::string& val)
|
||||
{
|
||||
label_ = val;
|
||||
label_text_ = val;
|
||||
|
||||
//if we have a list of items, use the first one that isn't an image
|
||||
if (std::find(label_.begin(), label_.end(), COLUMN_SEPARATOR) != label_.end()) {
|
||||
const std::vector<std::string>& items = utils::split(label_, COLUMN_SEPARATOR);
|
||||
if (std::find(label_text_.begin(), label_text_.end(), COLUMN_SEPARATOR) != label_text_.end()) {
|
||||
const std::vector<std::string>& items = utils::split(label_text_, COLUMN_SEPARATOR);
|
||||
const std::vector<std::string>::const_iterator i = std::find_if(items.begin(),items.end(),not_image);
|
||||
if(i != items.end()) {
|
||||
label_ = *i;
|
||||
label_text_ = *i;
|
||||
}
|
||||
}
|
||||
|
||||
calculate_size();
|
||||
|
||||
font::ttext text;
|
||||
text.set_text(label_text_, false);
|
||||
label_image_ = text.render_as_texture();
|
||||
|
||||
set_dirty(true);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,10 @@
|
|||
|
||||
#include "../exceptions.hpp"
|
||||
|
||||
#ifdef SDL_GPU
|
||||
#include "sdl/image.hpp"
|
||||
#endif
|
||||
|
||||
namespace gui {
|
||||
|
||||
class button : public widget
|
||||
|
@ -69,11 +73,19 @@ private:
|
|||
|
||||
void calculate_size();
|
||||
|
||||
std::string label_;
|
||||
#ifdef SDL_GPU
|
||||
std::string label_text_;
|
||||
sdl::timage label_image_;
|
||||
sdl::timage image_, pressedImage_, activeImage_, pressedActiveImage_,
|
||||
touchedImage_, disabledImage_, pressedDisabledImage_,
|
||||
overlayImage_, overlayPressedImage_, overlayPressedDisabledImage_, overlayDisabledImage_,
|
||||
overlayActiveImage_;
|
||||
#else
|
||||
surface image_, pressedImage_, activeImage_, pressedActiveImage_,
|
||||
touchedImage_, disabledImage_, pressedDisabledImage_,
|
||||
overlayImage_, overlayPressedImage_, overlayPressedDisabledImage_, overlayDisabledImage_,
|
||||
overlayActiveImage_;
|
||||
#endif
|
||||
SDL_Rect textRect_;
|
||||
|
||||
enum STATE { UNINIT, NORMAL, ACTIVE, PRESSED, PRESSED_ACTIVE, TOUCHED_NORMAL, TOUCHED_PRESSED };
|
||||
|
|
Loading…
Add table
Reference in a new issue