Skip to content

Commit

Permalink
Merge pull request #570 from aginor/resizehandling
Browse files Browse the repository at this point in the history
Improve window event handling under SDL2
  • Loading branch information
aginor committed Dec 31, 2015
2 parents b4a34b2 + 2f65f0a commit 20c93ce
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 6 deletions.
14 changes: 14 additions & 0 deletions src/construct_dialog.cpp
Expand Up @@ -318,6 +318,8 @@ int dialog::show()
const dialog_manager manager;
const resize_lock prevent_resizing;

get_frame().join();

//draw
draw_frame();
update_widget_positions();
Expand Down Expand Up @@ -397,18 +399,21 @@ void dialog::update_widget_positions()
if(!preview_panes_.empty()) {
for(pp_iterator i = preview_panes_.begin(); i != preview_panes_.end(); ++i) {
preview_pane *pane = *i;
pane->leave();
pane->join();
pane->set_location(dim_.panes.find(pane)->second);
}
}
if(text_widget_) {
text_widget_->leave();
text_widget_->join();
text_widget_->set_location(dim_.textbox);
if(text_widget_->get_label()) {
text_widget_->get_label()->set_location(dim_.label_x, dim_.label_y);
}
}
if(get_menu().height() > 0) {
menu_->leave();
menu_->join();
menu_->set_numeric_keypress_selection(text_widget_ == NULL);
menu_->set_width( dim_.menu_width );
Expand All @@ -419,6 +424,7 @@ void dialog::update_widget_positions()
menu_->set_location( dim_.menu_x, dim_.menu_y );
}
if(image_) {
image_->leave();
image_->join();
image_->set_location(dim_.image_x, dim_.image_y);
if(image_->caption()) {
Expand All @@ -428,24 +434,29 @@ void dialog::update_widget_positions()
button_iterator b;
for(b = top_buttons_.begin(); b != top_buttons_.end(); ++b) {
dialog_button *btn = *b;
btn->leave();
btn->join();
std::pair<int,int> coords = dim_.buttons.find(btn)->second;
btn->set_location(coords.first, coords.second);
}
for(b = extra_buttons_.begin(); b != extra_buttons_.end(); ++b) {
dialog_button *btn = *b;
btn->leave();
btn->join();
std::pair<int,int> coords = dim_.buttons.find(btn)->second;
btn->set_location(coords.first, coords.second);
}
for(b = standard_buttons_.begin(); b != standard_buttons_.end(); ++b) {
dialog_button *btn = *b;
btn->leave();
btn->join();
}
if(help_button_) {
help_button_->leave();
help_button_->join();
}
message_->set_location(dim_.message);
message_->leave();
message_->join();
}

Expand Down Expand Up @@ -799,6 +810,9 @@ int dialog::process(dialog_process_info &info)
}
}

draw_frame();
//draw_contents();

events::raise_process_event();
events::raise_draw_event();

Expand Down
7 changes: 4 additions & 3 deletions src/display.cpp
Expand Up @@ -45,6 +45,7 @@
#include "unit_animation_component.hpp"
#include "unit_drawer.hpp"
#include "whiteboard/manager.hpp"
#include "show_dialog.hpp"

#include "SDL_image.h"

Expand Down Expand Up @@ -1654,7 +1655,6 @@ static void draw_background(surface screen, const SDL_Rect& area, const std::str
}
#endif

//TODO: convert this to use sdl::ttexture
void display::draw_text_in_hex(const map_location& loc,
const tdrawing_layer layer, const std::string& text,
size_t font_size, SDL_Color color, double x_in_hex, double y_in_hex)
Expand Down Expand Up @@ -2692,8 +2692,9 @@ void display::redraw_everything()
}

panelsDrawn_ = false;

labels().recalculate_labels();
if (!gui::in_dialog()) {
labels().recalculate_labels();
}

redraw_background_ = true;

Expand Down
66 changes: 66 additions & 0 deletions src/events.cpp
Expand Up @@ -33,6 +33,7 @@
#include <deque>
#include <utility>
#include <vector>
#include <algorithm>

#define ERR_GEN LOG_STREAM(err, lg::general)

Expand Down Expand Up @@ -276,6 +277,37 @@ bool has_focus(const sdl_handler* hand, const SDL_Event* event)
return false;
}

#if SDL_VERSION_ATLEAST(2,0,0)

const Uint32 resize_timeout = 100;
SDL_Event last_resize_event;
bool last_resize_event_used = true;

bool resize_comparer(SDL_Event a, SDL_Event b) {
return a.type == SDL_WINDOWEVENT && a.type == b.type &&
a.window.event == SDL_WINDOWEVENT_RESIZED &&
a.window.event == b.window.event;
}

bool remove_on_resize(const SDL_Event &a) {
if (a.type == DRAW_EVENT) {
return true;
}
if (a.type == SHOW_HELPTIP_EVENT) {
return true;
}
if ((a.type == SDL_WINDOWEVENT) &&
(a.window.event == SDL_WINDOWEVENT_RESIZED ||
a.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
a.window.event == SDL_WINDOWEVENT_EXPOSED)) {
return true;
}

return false;
}

#endif

void pump()
{
SDL_PumpEvents();
Expand Down Expand Up @@ -311,6 +343,7 @@ void pump()
}
events.push_back(temp_event);
}

std::vector<SDL_Event>::iterator ev_it = events.begin();
for(int i=1; i < begin_ignoring; ++i){
if(is_input(*ev_it)) {
Expand All @@ -320,7 +353,32 @@ void pump()
++ev_it;
}
}

std::vector<SDL_Event>::iterator ev_end = events.end();
#if SDL_VERSION_ATLEAST(2, 0, 0)
bool resize_found = false;
for(ev_it = events.begin(); ev_it != ev_end; ++ev_it){
SDL_Event &event = *ev_it;
if (event.type == SDL_WINDOWEVENT &&
event.window.event == SDL_WINDOWEVENT_RESIZED) {
resize_found = true;
last_resize_event = event;
last_resize_event_used = false;

}
}
// remove all inputs, draw events and only keep the last of the resize events
// This will turn horrible after ~38 days when the Uint32 wraps.
if (resize_found || SDL_GetTicks() <= last_resize_event.window.timestamp + resize_timeout) {
events.erase(std::remove_if(events.begin(), events.end(), remove_on_resize), events.end());
} else if(SDL_GetTicks() > last_resize_event.window.timestamp + resize_timeout && !last_resize_event_used) {
events.insert(events.begin(), last_resize_event);
last_resize_event_used = true;
}

ev_end = events.end();
#endif

for(ev_it = events.begin(); ev_it != ev_end; ++ev_it){
SDL_Event &event = *ev_it;
switch(event.type) {
Expand All @@ -340,12 +398,20 @@ void pump()

case SDL_WINDOWEVENT_EXPOSED:
update_whole_screen();
if (display::get_singleton())
display::get_singleton()->redraw_everything();
break;

case SDL_WINDOWEVENT_RESIZED: {
info.resize_dimensions.first = event.window.data1;
info.resize_dimensions.second = event.window.data2;
break;
case SDL_WINDOWEVENT_MOVED:
case SDL_WINDOWEVENT_CLOSE:
break;
default:
if (display::get_singleton())
display::get_singleton()->redraw_everything();
}
}
break;
Expand Down
1 change: 1 addition & 0 deletions src/generators/default_map_generator.cpp
Expand Up @@ -287,6 +287,7 @@ void default_map_generator::user_config(display& disp)
width_slider.set_min(min_width+(players_slider.value()-2)*extra_size_per_player);
height_slider.set_min(min_width+(players_slider.value()-2)*extra_size_per_player);

f.draw();
events::raise_process_event();
events::raise_draw_event();

Expand Down
5 changes: 5 additions & 0 deletions src/help/help.cpp
Expand Up @@ -117,6 +117,7 @@ help_manager::~help_manager()
void show_help(display &disp, const std::string& show_topic, int xloc, int yloc)
{
show_help(disp, toplevel, show_topic, xloc, yloc);
disp.redraw_everything();
}

/**
Expand All @@ -128,6 +129,7 @@ void show_unit_help(display &disp, const std::string& show_topic, bool has_varia
{
show_help(disp, toplevel,
hidden_symbol(hidden) + (has_variations ? ".." : "") + unit_prefix + show_topic, xloc, yloc);
disp.redraw_everything();
}

/**
Expand All @@ -138,6 +140,7 @@ void show_unit_help(display &disp, const std::string& show_topic, bool has_varia
void show_terrain_help(display &disp, const std::string& show_topic, bool hidden, int xloc, int yloc)
{
show_help(disp, toplevel, hidden_symbol(hidden) + terrain_prefix + show_topic, xloc, yloc);
disp.redraw_everything();
}


Expand All @@ -148,6 +151,7 @@ void show_terrain_help(display &disp, const std::string& show_topic, bool hidden
void show_variation_help(display &disp, const std::string& unit, const std::string &variation, bool hidden, int xloc, int yloc)
{
show_help(disp, toplevel, hidden_symbol(hidden) + variation_prefix + unit + "_" + variation, xloc, yloc);
disp.redraw_everything();
}

/**
Expand Down Expand Up @@ -222,6 +226,7 @@ void show_help(display &disp, const section &toplevel_sec,
for (;;) {
events::pump();
events::raise_process_event();
f.draw();
events::raise_draw_event();
if (key[SDLK_ESCAPE]) {
// Escape quits from the dialog.
Expand Down
22 changes: 21 additions & 1 deletion src/show_dialog.cpp
Expand Up @@ -111,7 +111,8 @@ dialog_frame::dialog_frame(CVideo &video, const std::string& title,
top_right_(image::get_image("dialogs/" + dialog_style_.panel + "-border-topright.png")),
bot_right_(image::get_image("dialogs/" + dialog_style_.panel + "-border-botright.png")),
bg_(image::get_image("dialogs/" + dialog_style_.panel + "-background.png")),
have_border_(top_ != NULL && bot_ != NULL && left_ != NULL && right_ != NULL)
have_border_(top_ != NULL && bot_ != NULL && left_ != NULL && right_ != NULL),
dirty_(true)
#endif
{
}
Expand Down Expand Up @@ -144,6 +145,20 @@ int dialog_frame::top_padding() const {
return padding;
}

void dialog_frame::set_dirty(bool dirty) {
dirty_ = dirty;
}

void dialog_frame::handle_event(const SDL_Event& event) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
if (event.type == SDL_WINDOWEVENT) {
dirty_ = true;
}
#else
UNUSED(event);
#endif
}

int dialog_frame::bottom_padding() const {
int padding = 0;
if(buttons_ != NULL) {
Expand Down Expand Up @@ -376,6 +391,9 @@ SDL_Rect dialog_frame::draw_title(CVideo* video)

void dialog_frame::draw()
{
if (!dirty_)
return;

//draw background
draw_background();

Expand All @@ -402,6 +420,8 @@ void dialog_frame::draw()
if(help_button_ != NULL) {
help_button_->set_location(dim_.interior.x+ButtonHPadding, buttons_area.y);
}

dirty_ = false;
}

} //end namespace gui
Expand Down
7 changes: 6 additions & 1 deletion src/show_dialog.hpp
Expand Up @@ -52,7 +52,7 @@ struct dialog_manager : private cursor::setter, private font::floating_label_con
bool reset_to;
};

class dialog_frame {
class dialog_frame :public events::sdl_handler {
public:
struct dimension_measurements {
dimension_measurements();
Expand Down Expand Up @@ -95,6 +95,10 @@ class dialog_frame {
//also called by layout with null param
SDL_Rect draw_title(CVideo *video);

void set_dirty(bool dirty = true);

virtual void handle_event(const SDL_Event& event);

private:
void clear_background();

Expand All @@ -112,6 +116,7 @@ class dialog_frame {
surface top_, bot_, left_, right_, top_left_, bot_left_, top_right_, bot_right_, bg_;
#endif
bool have_border_;
bool dirty_;
};

//frame_measurements draw_dialog_frame(int x, int y, int w, int h, CVideo &video, const std::string* dialog_style=NULL, surface_restorer* restorer=NULL);
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/button.cpp
Expand Up @@ -718,6 +718,8 @@ void button::mouse_up(SDL_MouseButtonEvent const &event)

void button::handle_event(const SDL_Event& event)
{
gui::widget::handle_event(event);

if (hidden() || !enabled())
return;

Expand Down
2 changes: 2 additions & 0 deletions src/widgets/scrollarea.cpp
Expand Up @@ -150,6 +150,8 @@ unsigned scrollarea::scrollbar_width() const

void scrollarea::handle_event(const SDL_Event& event)
{
gui::widget::handle_event(event);

if (mouse_locked() || hidden())
return;

Expand Down
2 changes: 2 additions & 0 deletions src/widgets/scrollbar.cpp
Expand Up @@ -321,6 +321,8 @@ void scrollbar::draw_contents()

void scrollbar::handle_event(const SDL_Event& event)
{
gui::widget::handle_event(event);

if (mouse_locked() || hidden())
return;

Expand Down
2 changes: 2 additions & 0 deletions src/widgets/slider.cpp
Expand Up @@ -314,6 +314,8 @@ bool slider::requires_event_focus(const SDL_Event* event) const

void slider::handle_event(const SDL_Event& event)
{
gui::widget::handle_event(event);

if (!enabled() || hidden())
return;

Expand Down
1 change: 1 addition & 0 deletions src/widgets/textbox.cpp
Expand Up @@ -447,6 +447,7 @@ bool textbox::requires_event_focus(const SDL_Event* event) const

void textbox::handle_event(const SDL_Event& event)
{
gui::widget::handle_event(event);
handle_event(event, false);
}

Expand Down

0 comments on commit 20c93ce

Please sign in to comment.