Skip to content

Commit

Permalink
Merge pull request #595 from wesnoth/guifixes
Browse files Browse the repository at this point in the history
SDL2 related guifixes
  • Loading branch information
aginor committed Feb 28, 2016
2 parents efcfe3b + 9965f9e commit 504ed49
Show file tree
Hide file tree
Showing 36 changed files with 426 additions and 96 deletions.
7 changes: 6 additions & 1 deletion src/controller_base.hpp
Expand Up @@ -42,6 +42,7 @@
#include "hotkey/hotkey_command.hpp"
#include "joystick.hpp"
#include "key.hpp"
#include "video.hpp"
#include "quit_confirmation.hpp"

class CVideo;
Expand All @@ -54,7 +55,7 @@ namespace hotkey { class command_executor; }

namespace soundsource { class manager; }

class controller_base : public events::sdl_handler
class controller_base : public video2::draw_layering
{
public:
controller_base(const config& game_config, CVideo& video);
Expand Down Expand Up @@ -113,6 +114,10 @@ class controller_base : public events::sdl_handler
*/
void handle_event(const SDL_Event& event);

#if SDL_VERSION_ATLEAST(2, 0, 0)
void handle_window_event(const SDL_Event& ) {}
#endif

/**
* Process keydown (only when the general map display does not have focus).
*/
Expand Down
127 changes: 103 additions & 24 deletions src/display.cpp
Expand Up @@ -149,7 +149,8 @@ void display::remove_single_overlay(const map_location& loc, const std::string&



display::display(const display_context * dc, CVideo& video, boost::weak_ptr<wb::manager> wb, reports & reports_object, const config& theme_cfg, const config& level) :
display::display(const display_context * dc, CVideo& video, boost::weak_ptr<wb::manager> wb, reports & reports_object, const config& theme_cfg, const config& level, bool auto_join) :
video2::draw_layering(auto_join),
dc_(dc),
halo_man_(new halo::manager(*this)),
wb_(wb),
Expand Down Expand Up @@ -220,7 +221,8 @@ display::display(const display_context * dc, CVideo& video, boost::weak_ptr<wb::
draw_coordinates_(false),
draw_terrain_codes_(false),
arrows_map_(),
color_adjust_()
color_adjust_(),
dirty_()
#ifdef SDL_GPU
, update_panel_image_(true),
panel_image_()
Expand Down Expand Up @@ -254,6 +256,10 @@ display::display(const display_context * dc, CVideo& video, boost::weak_ptr<wb::

init_flags();

if(!menu_buttons_.empty() || !action_buttons_.empty() || !sliders_.empty() ) {
create_buttons();
}

#if defined(__GLIBC__) && !SDL_VERSION_ATLEAST(2,0,0)
// Runtime checks for bug #17573
// Get glibc runtime version information
Expand Down Expand Up @@ -867,6 +873,53 @@ gui::zoom_slider* display::find_slider(const std::string& id)
return NULL;
}

void display::layout_buttons()
{

DBG_DP << "positioning sliders...\n";
const std::vector<theme::slider>& sliders = theme_.sliders();
for(std::vector<theme::slider>::const_iterator i = sliders.begin(); i != sliders.end(); ++i) {
gui::zoom_slider* s = find_slider(i->get_id());
if (s) {
const SDL_Rect& loc = i->location(screen_area());
s->set_location(loc);
s->set_measurements(0,0);
s->set_volatile(
sdl::rects_overlap(s->location(),map_outside_area()));
}
}

DBG_DP << "positioning menu buttons...\n";
const std::vector<theme::menu>& buttons = theme_.menus();
for(std::vector<theme::menu>::const_iterator i = buttons.begin(); i != buttons.end(); ++i) {
gui::button* b = find_menu_button(i->get_id());
if(b) {
const SDL_Rect& loc = i->location(screen_area());
b->set_location(loc);
b->set_measurements(0,0);
b->set_label(i->title());
b->set_image(i->image());
b->set_volatile(
sdl::rects_overlap(b->location(),map_outside_area()));
}
}

DBG_DP << "positioning action buttons...\n";
const std::vector<theme::action>& actions = theme_.actions();
for(std::vector<theme::action>::const_iterator i = actions.begin(); i != actions.end(); ++i) {
gui::button* b = find_action_button(i->get_id());
if(b) {
const SDL_Rect& loc = i->location(screen_area());
b->set_location(loc);
b->set_measurements(0,0);
b->set_label(i->title());
b->set_image(i->image());
b->set_volatile(
sdl::rects_overlap(b->location(),map_outside_area()));
}
}
}

void display::create_buttons()
{
std::vector<gui::button> menu_work;
Expand All @@ -879,18 +932,13 @@ void display::create_buttons()
gui::zoom_slider s(screen_, i->image(), i->black_line());
DBG_DP << "drawing button " << i->get_id() << "\n";
s.set_id(i->get_id());
const SDL_Rect& loc = i->location(screen_area());
s.set_location(loc);
//TODO support for non zoom sliders
s.set_max(MaxZoom);
s.set_min(MinZoom);
s.set_value(zoom_);
if (!i->tooltip().empty()){
s.set_tooltip_string(i->tooltip());
}
if(sdl::rects_overlap(s.location(),map_outside_area())) {
s.set_volatile(true);
}

gui::zoom_slider* s_prev = find_slider(s.id());
if(s_prev) {
Expand All @@ -913,14 +961,9 @@ void display::create_buttons()
gui::button::DEFAULT_SPACE, true, i->overlay());
DBG_DP << "drawing button " << i->get_id() << "\n";
b.set_id(i->get_id());
const SDL_Rect& loc = i->location(screen_area());
b.set_location(loc.x,loc.y);
if (!i->tooltip().empty()){
b.set_tooltip_string(i->tooltip());
}
if(sdl::rects_overlap(b.location(),map_outside_area())) {
b.set_volatile(true);
}

gui::button* b_prev = find_menu_button(b.id());
if(b_prev) b.enable(b_prev->enabled());
Expand All @@ -935,28 +978,26 @@ void display::create_buttons()

DBG_DP << "drawing button " << i->get_id() << "\n";
b.set_id(i->get_id());
const SDL_Rect& loc = i->location(screen_area());
b.set_location(loc.x,loc.y);
if (!i->tooltip(0).empty()){
b.set_tooltip_string(i->tooltip(0));
}
if(sdl::rects_overlap(b.location(),map_outside_area())) {
b.set_volatile(true);
}

gui::button* b_prev = find_action_button(b.id());
if(b_prev) b.enable(b_prev->enabled());

action_work.push_back(b);
}



menu_buttons_.swap(menu_work);
action_buttons_.swap(action_work);
sliders_.swap(slider_work);

layout_buttons();
DBG_DP << "buttons created\n";
}

#ifdef SDL_GPU
void display::render_buttons()
{
BOOST_FOREACH(gui::button &btn, menu_buttons_) {
Expand All @@ -971,7 +1012,7 @@ void display::render_buttons()
sld.set_dirty(true);
}
}
#endif


gui::button::TYPE display::string_to_button_type(std::string type)
{
Expand Down Expand Up @@ -1584,8 +1625,7 @@ void display::draw_all_panels()
draw_label(video(),screen,*i);
}

//FIXME: does it really make sense to recreate buttons all the time?
create_buttons();
render_buttons();
#endif
}

Expand Down Expand Up @@ -2674,9 +2714,8 @@ void display::redraw_everything()

theme_.set_resolution(screen_area());

if(!menu_buttons_.empty() || !action_buttons_.empty() || !sliders_.empty() ) {
create_buttons();
}
layout_buttons();
render_buttons();

panelsDrawn_ = false;
if (!gui::in_dialog()) {
Expand Down Expand Up @@ -2709,11 +2748,28 @@ void display::clear_redraw_observers()
redraw_observers_.clear();
}

void display::draw() {
draw(true, false);
}

void display::draw(bool update) {
draw(update, false);
}


void display::draw(bool update,bool force) {
// log_scope("display::draw");

if (screen_.update_locked()) {
return;
}

if (dirty_) {
dirty_ = false;
redraw_everything();
return;
}

set_scontext_unsynced leave_synced_context;

draw_init();
Expand Down Expand Up @@ -3758,5 +3814,28 @@ void display::process_reachmap_changes()
reach_map_changed_ = false;
}

#if SDL_VERSION_ATLEAST(2, 0, 0)
void display::handle_window_event(const SDL_Event& event) {
if (event.type == SDL_WINDOWEVENT) {
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
case SDL_WINDOWEVENT_RESTORED:
case SDL_WINDOWEVENT_EXPOSED:
dirty_ = true;

break;
}
}


}
#endif

void display::handle_event(const SDL_Event& event) {
if (event.type == DRAW_ALL_EVENT) {
draw();
}
}

display *display::singleton_ = NULL;

31 changes: 19 additions & 12 deletions src/display.hpp
Expand Up @@ -74,12 +74,12 @@ namespace wb {

class gamemap;

class display : public filter_context
class display : public filter_context, public video2::draw_layering
{
public:
display(const display_context * dc, CVideo& video, boost::weak_ptr<wb::manager> wb,
reports & reports_object,
const config& theme_cfg, const config& level);
const config& theme_cfg, const config& level, bool auto_join=true);
virtual ~display();
/// Returns the display object if a display object exists. Otherwise it returns NULL.
/// the display object represents the game gui which handles themewml and drawing the map.
Expand Down Expand Up @@ -156,13 +156,6 @@ class display : public filter_context
/** remove_single_overlay will remove a single overlay from a tile */
void remove_single_overlay(const map_location& loc, const std::string& toDelete);








/**
* Updates internals that cache map size. This should be called when the map
* size has changed.
Expand Down Expand Up @@ -406,9 +399,11 @@ class display : public filter_context

gui::button::TYPE string_to_button_type(std::string type);
void create_buttons();
#ifdef SDL_GPU

void layout_buttons();

void render_buttons();
#endif

void invalidate_theme() { panelsDrawn_ = false; }

void refresh_report(std::string const &report_name, const config * new_cfg=NULL);
Expand Down Expand Up @@ -607,7 +602,11 @@ class display : public filter_context
* Not virtual, since it gathers common actions. Calls various protected
* virtuals (further below) to allow specialized behavior in derived classes.
*/
void draw(bool update=true, bool force=false);
virtual void draw();

void draw(bool update);

void draw(bool update, bool force);

map_labels& labels();
const map_labels& labels() const;
Expand Down Expand Up @@ -636,6 +635,12 @@ class display : public filter_context
bool is_blindfolded() const;

void write(config& cfg) const;

virtual void handle_event(const SDL_Event& );
#if SDL_VERSION_ATLEAST(2, 0, 0)
virtual void handle_window_event(const SDL_Event& event);
#endif

private:
void read(const config& cfg);

Expand Down Expand Up @@ -1156,6 +1161,8 @@ class display : public filter_context

tod_color color_adjust_;

bool dirty_;

#ifdef SDL_GPU
bool update_panel_image_;
sdl::timage panel_image_;
Expand Down
1 change: 1 addition & 0 deletions src/editor/controller/editor_controller.cpp
Expand Up @@ -90,6 +90,7 @@ editor_controller::editor_controller(const config &game_config, CVideo& video)
cursor::set(cursor::NORMAL);
image::set_color_adjustment(preferences::editor::tod_r(), preferences::editor::tod_g(), preferences::editor::tod_b());

gui().create_buttons();
gui().redraw_everything();
events::raise_draw_event();
}
Expand Down
1 change: 1 addition & 0 deletions src/editor/map/context_manager.cpp
Expand Up @@ -174,6 +174,7 @@ void context_manager::refresh_all()
{
gui_.rebuild_all();
get_map_context().set_needs_terrain_rebuild(false);
gui_.create_buttons();
gui_.invalidate_all();
gui_.draw(false);
get_map_context().clear_changed_locations();
Expand Down
4 changes: 3 additions & 1 deletion src/editor/palette/editor_palettes.cpp
Expand Up @@ -248,7 +248,9 @@ void editor_palette<Item>::adjust_size(const SDL_Rect& target)
static_cast<unsigned> (space_for_items / item_space_) *
item_width_;
nitems_ = std::min<int>(items_fitting, nmax_items_);
buttons_.resize(nitems_, gui::tristate_button(gui_.video(), this));
if (buttons_.size() != nitems_) {
buttons_.resize(nitems_, gui::tristate_button(gui_.video(), this));
}
set_location(target);
set_dirty(true);
gui_.video().clear_help_string(help_handle_);
Expand Down

0 comments on commit 504ed49

Please sign in to comment.