Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A thin wrapper for SDL_Rect #6814

Merged
merged 7 commits into from
Jul 2, 2022
12 changes: 6 additions & 6 deletions src/controller_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void controller_base::long_touch_callback(int x, int y)

if(!yes_actually_dragging
&& (mouse_state & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0
&& sdl::point_in_rect(x_now, y_now, get_display().map_area()))
&& get_display().map_area().contains(x_now, y_now))
{
const theme::menu* const m = get_mouse_handler_base().gui().get_theme().context_menu();
if(m != nullptr) {
Expand Down Expand Up @@ -214,7 +214,7 @@ void controller_base::handle_event(const SDL_Event& event)
int y = static_cast<int>(reinterpret_cast<std::intptr_t>(event.user.data2));
if(event.user.code == static_cast<int>(SDL_TOUCH_MOUSEID)
// TODO: Move to right_click_show_menu?
&& sdl::point_in_rect(x, y, get_display().map_area())
&& get_display().map_area().contains(x, y)
// TODO: This chain repeats in several places, move to a method.
&& get_display().get_theme().context_menu() != nullptr) {
show_menu(get_display().get_theme().context_menu()->items(),
Expand Down Expand Up @@ -284,7 +284,7 @@ bool controller_base::handle_scroll(int mousex, int mousey, int mouse_flags)
: 0;

for(const theme::menu& m : get_display().get_theme().menus()) {
if(sdl::point_in_rect(mousex, mousey, m.get_location())) {
if(m.get_location().contains(mousex, mousey)) {
scroll_threshold = 0;
}
}
Expand Down Expand Up @@ -334,9 +334,9 @@ bool controller_base::handle_scroll(int mousex, int mousey, int mouse_flags)
const SDL_Point original_loc = mh_base.get_scroll_start();

if(mh_base.scroll_started()) {
const SDL_Rect& rect = get_display().map_outside_area();

if(sdl::point_in_rect(mousex, mousey, rect) && mh_base.scroll_started()) {
if(get_display().map_outside_area().contains(mousex, mousey)
&& mh_base.scroll_started())
{
// Scroll speed is proportional from the distance from the first
// middle click and scrolling speed preference.
const double speed = 0.01 * scroll_amount;
Expand Down
38 changes: 18 additions & 20 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,9 @@ bool display::is_blindfolded() const
return blindfold_ctr_ > 0;
}

const SDL_Rect& display::max_map_area() const
rect display::max_map_area() const
{
static SDL_Rect max_area{0, 0, 0, 0};
rect max_area{0, 0, 0, 0};

// hex_size() is always a multiple of 4
// and hex_width() a multiple of 3,
Expand All @@ -551,18 +551,16 @@ const SDL_Rect& display::max_map_area() const
return max_area;
}

const SDL_Rect& display::map_area() const
rect display::map_area() const
{
static SDL_Rect max_area;
max_area = max_map_area();
rect max_area = max_map_area();

// if it's for map_screenshot, maximize and don't recenter
if(map_screenshot_) {
return max_area;
}

static SDL_Rect res;
res = map_outside_area();
rect res = map_outside_area();

if(max_area.w < res.w) {
// map is smaller, center
Expand All @@ -589,13 +587,13 @@ bool display::outside_area(const SDL_Rect& area, const int x, const int y)
// This function uses the screen as reference
const map_location display::hex_clicked_on(int xclick, int yclick) const
{
const SDL_Rect& rect = map_area();
if(sdl::point_in_rect(xclick,yclick,rect) == false) {
rect r = map_area();
if(!r.contains(xclick, yclick)) {
return map_location();
}

xclick -= rect.x;
yclick -= rect.y;
xclick -= r.x;
yclick -= r.y;

return pixel_position_to_hex(xpos_ + xclick, ypos_ + yclick);
}
Expand Down Expand Up @@ -752,7 +750,7 @@ map_location display::minimap_location_on(int x, int y)
// TODO: don't return location for this,
// instead directly scroll to the clicked pixel position

if(!sdl::point_in_rect(x, y, minimap_area())) {
if(!minimap_area().contains(x, y)) {
return map_location();
}

Expand Down Expand Up @@ -1529,8 +1527,8 @@ void display::render_image(int x, int y, const display::drawing_layer drawing_la
}

// TODO: highdpi - are x,y correct here?
SDL_Rect dest = scaled_to_zoom({x, y, image_size.x, image_size.y});
if (!sdl::rects_overlap(dest, map_area())) {
rect dest = scaled_to_zoom({x, y, image_size.x, image_size.y});
if (!dest.overlaps(map_area())) {
return;
}

Expand Down Expand Up @@ -1935,10 +1933,10 @@ bool display::scroll(int xmove, int ymove, bool force)
//

if(!screen_.update_locked()) {
SDL_Rect dstrect = map_area();
rect dstrect = map_area();
dstrect.x += diff_x;
dstrect.y += diff_y;
dstrect = sdl::intersect_rects(dstrect, map_area());
dstrect.clip(map_area());

SDL_Rect srcrect = dstrect;
srcrect.x -= diff_x;
Expand Down Expand Up @@ -2497,7 +2495,7 @@ const map_labels& display::labels() const
return *map_labels_;
}

const SDL_Rect& display::get_clip_rect()
rect display::get_clip_rect() const
{
return map_area();
}
Expand All @@ -2511,8 +2509,8 @@ void display::draw_invalidated() {
int ypos = get_location_y(loc);

//const bool on_map = get_map().on_board(loc);
SDL_Rect hex_rect = sdl::create_rect(xpos, ypos, zoom_, zoom_);
if(!sdl::rects_overlap(hex_rect,clip_rect)) {
rect hex_rect(xpos, ypos, zoom_, zoom_);
if(!hex_rect.overlaps(clip_rect)) {
continue;
}
draw_hex(loc);
Expand Down Expand Up @@ -3002,7 +3000,7 @@ bool display::propagate_invalidation(const std::set<map_location>& locs)

bool display::invalidate_visible_locations_in_rect(const SDL_Rect& rect)
{
return invalidate_locations_in_rect(sdl::intersect_rects(map_area(), rect));
return invalidate_locations_in_rect(map_area().intersect(rect));
}

bool display::invalidate_locations_in_rect(const SDL_Rect& rect)
Expand Down
14 changes: 7 additions & 7 deletions src/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,30 @@ class display : public video2::draw_layering
* Between mapx and x is the sidebar region.
*/

const SDL_Rect& minimap_area() const
const rect& minimap_area() const
{ return theme_.mini_map_location(screen_.draw_area()); }
const SDL_Rect& palette_area() const
const rect& palette_area() const
{ return theme_.palette_location(screen_.draw_area()); }
const SDL_Rect& unit_image_area() const
const rect& unit_image_area() const
{ return theme_.unit_image_location(screen_.draw_area()); }

/**
* Returns the maximum area used for the map
* regardless to resolution and view size
*/
const SDL_Rect& max_map_area() const;
rect max_map_area() const;

/**
* Returns the area used for the map
*/
const SDL_Rect& map_area() const;
rect map_area() const;

/**
* Returns the available area for a map, this may differ
* from the above. This area will get the background area
* applied to it.
*/
const SDL_Rect& map_outside_area() const { return map_screenshot_ ?
rect map_outside_area() const { return map_screenshot_ ?
max_map_area() : theme_.main_map_location(screen_.draw_area()); }

/** Check if the bbox of the hex at x,y has pixels outside the area rectangle. */
Expand Down Expand Up @@ -666,7 +666,7 @@ class display : public video2::draw_layering
* Get the clipping rectangle for drawing.
* Virtual since the editor might use a slightly different approach.
*/
virtual const SDL_Rect& get_clip_rect();
virtual rect get_clip_rect() const;

/**
* Only called when there's actual redrawing to do. Loops through
Expand Down
7 changes: 3 additions & 4 deletions src/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,7 @@ draw::clip_setter draw::reduce_clip(const SDL_Rect& clip)
if (!draw::clip_enabled()) {
return draw::clip_setter(clip);
}
SDL_Rect c = draw::get_clip();
return draw::clip_setter(sdl::intersect_rects(clip, c));
return draw::clip_setter(draw::get_clip().intersect(clip));
}

void draw::force_clip(const SDL_Rect& clip)
Expand All @@ -474,7 +473,7 @@ void draw::force_clip(const SDL_Rect& clip)
SDL_RenderSetClipRect(renderer(), &clip);
}

SDL_Rect draw::get_clip()
rect draw::get_clip()
{
// TODO: highdpi - fix whatever reason there is for this guard (CI fail)
if (!renderer()) {
Expand All @@ -487,7 +486,7 @@ SDL_Rect draw::get_clip()
return CVideo::get_singleton().draw_area();
}

SDL_Rect clip;
::rect clip;
SDL_RenderGetClipRect(renderer(), &clip);
return clip;
}
Expand Down
5 changes: 3 additions & 2 deletions src/draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
* resolution when possible, without any extra handling required.
*/

#include <SDL2/SDL_rect.h>
#include "sdl/rect.hpp"

#include <vector>

struct color_t;
Expand Down Expand Up @@ -329,7 +330,7 @@ void force_clip(const SDL_Rect& clip);
*
* If clipping is disabled, this will return the full drawing area.
*/
SDL_Rect get_clip();
::rect get_clip();

/** Whether clipping is enabled. */
bool clip_enabled();
Expand Down
2 changes: 1 addition & 1 deletion src/editor/editor_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void editor_display::draw_hex(const map_location& loc)
}
}

const SDL_Rect& editor_display::get_clip_rect()
rect editor_display::get_clip_rect() const
{
return map_outside_area();
}
Expand Down
2 changes: 1 addition & 1 deletion src/editor/editor_display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class editor_display : public display
/** Inherited from display. */
virtual overlay_map& get_overlays() override;

const SDL_Rect& get_clip_rect() override;
rect get_clip_rect() const override;
void draw_sidebar() override;

std::set<map_location> brush_locations_;
Expand Down
2 changes: 1 addition & 1 deletion src/editor/palette/location_palette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class location_palette_item : public gui::widget
//TODO move to widget
bool hit(int x, int y) const
{
return sdl::point_in_rect(x, y, location());
return location().contains(x, y);
}

void mouse_up(const SDL_MouseButtonEvent& e)
Expand Down
2 changes: 1 addition & 1 deletion src/editor/palette/palette_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void palette_manager::handle_event(const SDL_Event& event) {

if (event.type == SDL_MOUSEMOTION) {
// If the mouse is inside the palette, give it focus.
if (sdl::point_in_rect(event.button.x, event.button.y, location())) {
if (location().contains(event.button.x, event.button.y)) {
if (!focus(&event)) set_focus(true);
}
// If the mouse is outside, remove focus.
Expand Down
2 changes: 1 addition & 1 deletion src/editor/palette/tristate_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void tristate_button::draw_contents()

//TODO move to widget
bool tristate_button::hit(int x, int y) const {
return sdl::point_in_rect(x, y, location());
return location().contains(x, y);
}

void tristate_button::mouse_motion(const SDL_MouseMotionEvent& event) {
Expand Down
12 changes: 4 additions & 8 deletions src/gui/core/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,22 +455,18 @@ void text_shape::draw(wfl::map_formula_callable& variables)
const int y = y_(local_variables);
const int w = w_(local_variables);
const int h = h_(local_variables);
SDL_Rect dst_rect{x, y, w, h};
rect dst_rect{x, y, w, h};

// Get the visible portion of text.
SDL_Rect visible = sdl::intersect_rects(draw::get_clip(), dst_rect);
rect visible = dst_rect.intersect(draw::get_clip());

// Get the source region of text for clipping.
SDL_Rect clip_in = visible;
rect clip_in = visible;
clip_in.x -= x;
clip_in.y -= y;

// Source region for high-dpi text needs to have pixel scale applied.
const int pixel_scale = CVideo::get_singleton().get_pixel_scale();
clip_in.x *= pixel_scale;
clip_in.y *= pixel_scale;
clip_in.w *= pixel_scale;
clip_in.h *= pixel_scale;
clip_in *= CVideo::get_singleton().get_pixel_scale();

// Render the currently visible portion of text
// TODO: highdpi - it would be better to render this all, but some things currently have far too much text. Namely the credits screen.
Expand Down
3 changes: 1 addition & 2 deletions src/gui/dialogs/drop_down_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ void drop_down_menu::mouse_up_callback(bool&, bool&, const point& coordinate)
list.select_row(sel, false);
}

SDL_Rect rect = get_window()->get_rectangle();
if(!sdl::point_in_rect(coordinate, rect)) {
if(!get_window()->get_rectangle().contains(coordinate)) {
set_retval(retval::CANCEL);
} else if(!keep_open_) {
set_retval(retval::OK);
Expand Down
5 changes: 3 additions & 2 deletions src/gui/dialogs/terrain_layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ void terrain_layers::pre_show(window& window)
// Cut and mask the image
// ~CROP and ~BLIT have limitations, we do some math to avoid them
// TODO: ^ eh? what limitations?
SDL_Rect r2 = sdl::intersect_rects(r, {0,0,img_size.x,img_size.y});
if(r2.w > 0 && r2.h > 0) {
rect r2{0, 0, img_size.x, img_size.y};
r2.clip(r);
if(!r2.empty()) {
image_steam
<< "~BLIT(" << name
<< "~CROP("
Expand Down
7 changes: 4 additions & 3 deletions src/gui/widgets/horizontal_scrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ 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());
rect positioner_rect(
get_positioner_offset(), 0, get_positioner_length(), get_height()
);

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

int horizontal_scrollbar::on_bar(const point& coordinate) const
Expand Down
12 changes: 6 additions & 6 deletions src/gui/widgets/listbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,14 @@ void listbox::list_item_clicked(widget& caller)
return;
}

const SDL_Rect& visible = content_visible_area();
SDL_Rect rect = generator_->item(selected_item).get_rectangle();
const rect& visible = content_visible_area();
rect r = generator_->item(selected_item).get_rectangle();

if(sdl::rects_overlap(visible, rect)) {
rect.x = visible.x;
rect.w = visible.w;
if(visible.overlaps(r)) {
r.x = visible.x;
r.w = visible.w;

show_content_rect(rect);
show_content_rect(r);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct pane_implementation
* If the adjusted coordinate is in the item's grid let the grid
* resolve the coordinate.
*/
if(sdl::point_in_rect(coordinate, item.item_grid->get_rectangle())) {
if(item.item_grid->get_rectangle().contains(coordinate)) {
return item.item_grid->find_at(coordinate, must_be_active);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/scrollbar_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ void scrollbar_container::set_visible_rectangle(const SDL_Rect& rectangle)
container_base::set_visible_rectangle(rectangle);

// Now get the visible part of the content.
content_visible_area_ = sdl::intersect_rects(rectangle, content_->get_rectangle());
content_visible_area_ = content_->get_rectangle().intersect(rectangle);

content_grid_->set_visible_rectangle(content_visible_area_);
}
Expand Down