Skip to content

Commit

Permalink
GUI2: simplified some in-rect checks using sdl::point_in_rect
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Nov 11, 2017
1 parent ec388eb commit 68ed124
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/gui/widgets/horizontal_scrollbar.cpp
Expand Up @@ -72,11 +72,11 @@ unsigned horizontal_scrollbar::offset_after() const

bool horizontal_scrollbar::on_positioner(const point& coordinate) const
{
SDL_Rect positioner_rect =
sdl::create_rect(get_positioner_offset(), 0, get_positioner_length(), get_height());

// Note we assume the positioner is over the entire height of the widget.
return coordinate.x >= static_cast<int>(get_positioner_offset())
&& coordinate.x < static_cast<int>(get_positioner_offset()
+ get_positioner_length())
&& coordinate.y > 0 && coordinate.y < static_cast<int>(get_height());
return sdl::point_in_rect(coordinate, positioner_rect);
}

int horizontal_scrollbar::on_bar(const point& coordinate) const
Expand Down
7 changes: 2 additions & 5 deletions src/gui/widgets/pane.cpp
Expand Up @@ -24,6 +24,7 @@
#include "gui/core/event/message.hpp"
#include "gettext.hpp"
#include "lexical_cast.hpp"
#include "sdl/rect.hpp"

#include "utils/functional.hpp"

Expand Down Expand Up @@ -73,11 +74,7 @@ struct pane_implementation
* If the adjusted coordinate is in the item's grid let the grid
* resolve the coordinate.
*/
const SDL_Rect rect = item.item_grid->get_rectangle();
if(coordinate.x >= rect.x && coordinate.y >= rect.y
&& coordinate.x < rect.x + rect.w
&& coordinate.y < rect.y + rect.h) {

if(sdl::point_in_rect(coordinate, item.item_grid->get_rectangle())) {
return item.item_grid->find_at(coordinate, must_be_active);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/scrollbar.hpp
Expand Up @@ -16,7 +16,7 @@

#include "gui/core/notifier.hpp"
#include "gui/widgets/styled_widget.hpp"

#include "sdl/rect.hpp"
#include "utils/functional.hpp"

namespace gui2
Expand Down
14 changes: 7 additions & 7 deletions src/gui/widgets/slider.cpp
Expand Up @@ -22,6 +22,7 @@
#include "gui/core/register_widget.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "sdl/rect.hpp"
#include "sound.hpp"
#include "utils/math.hpp"
#include "gettext.hpp"
Expand Down Expand Up @@ -132,12 +133,11 @@ unsigned slider::offset_after() const

bool slider::on_positioner(const point& coordinate) const
{
SDL_Rect positioner_rect =
sdl::create_rect(get_positioner_offset(), 0, get_positioner_length(), get_height());

// Note we assume the positioner is over the entire height of the widget.
return
coordinate.x >= static_cast<int>(get_positioner_offset()) &&
coordinate.x < static_cast<int>(get_positioner_offset() + get_positioner_length()) &&
coordinate.y > 0 &&
coordinate.y < static_cast<int>(get_height());
return sdl::point_in_rect(coordinate, positioner_rect);
}

int slider::on_bar(const point& coordinate) const
Expand Down Expand Up @@ -265,14 +265,14 @@ void slider::set_step_size(int step_size)
{
const int old_min_value = get_minimum_value();
const int old_max_value = get_maximum_value();

const int range_diff = get_item_count() - 1;
const int old_value = get_value();

step_size_ = gcd(range_diff, step_size);
slider_set_item_last(range_diff / step_size_);
set_value(old_value);

assert(old_min_value == get_minimum_value());
assert(old_max_value == get_maximum_value());
}
Expand Down
10 changes: 5 additions & 5 deletions src/gui/widgets/vertical_scrollbar.cpp
Expand Up @@ -63,11 +63,11 @@ unsigned vertical_scrollbar::offset_after() const

bool vertical_scrollbar::on_positioner(const point& coordinate) const
{
// Note we assume the positioner is over the entire width of the widget.
return coordinate.y >= static_cast<int>(get_positioner_offset())
&& coordinate.y < static_cast<int>(get_positioner_offset()
+ get_positioner_length())
&& coordinate.x > 0 && coordinate.x < static_cast<int>(get_width());
SDL_Rect positioner_rect =
sdl::create_rect(0, get_positioner_offset(), get_width(), get_positioner_length());

// Note we assume the positioner is over the entire height of the widget.
return sdl::point_in_rect(coordinate, positioner_rect);
}

int vertical_scrollbar::on_bar(const point& coordinate) const
Expand Down
4 changes: 1 addition & 3 deletions src/gui/widgets/widget.cpp
Expand Up @@ -625,9 +625,7 @@ bool widget::is_at(const point& coordinate, const bool must_be_active) const
return false;
}

return coordinate.x >= x_ && coordinate.x < (x_ + static_cast<int>(width_))
&& coordinate.y >= y_
&& coordinate.y < (y_ + static_cast<int>(height_));
return sdl::point_in_rect(coordinate, get_rectangle());
}

} // namespace gui2

0 comments on commit 68ed124

Please sign in to comment.