Freeciv21
Develop your civilization from humble roots to a global empire
freeciv::gui_rect_iterator Class Reference

Iterates over all map tiles that intersect with a rectangle in GUI coordinates. More...

#include <view_map_geometry.h>

+ Collaboration diagram for freeciv::gui_rect_iterator:

Public Member Functions

 gui_rect_iterator (const struct tileset *t, const QRect &rect)
 Constructor. More...
 
bool has_corner () const
 Checks whether the current iteration point has a corner. More...
 
bool has_edge () const
 Checks whether the current iteration point has an edge. More...
 
bool has_tile () const
 Checks whether the current iteration point has a non-null tile. More...
 
const tile_cornercorner () const
 Retrieves the current corner. Only valid if has_corner is true. More...
 
const tile_edgeedge () const
 Retrieves the current edge. Only valid if has_edge is true. More...
 
const ::tiletile () const
 Retrieves the current tile. Only valid if has_tile is true. More...
 
int x () const
 Retrieves the x position of the current item. More...
 
int y () const
 Retrieves the y position of the current item. More...
 
bool next ()
 Iterates to the next item. More...
 

Private Attributes

bool m_isometric
 
int m_index
 
int m_count
 
int m_r1
 
int m_r2
 
int m_w
 
int m_h
 
int m_x0
 
int m_y0
 
int m_x1
 
int m_y1
 
int m_xi
 
int m_yi
 
tile_corner m_corner
 
tile_edge m_edge
 
::tilem_tile = nullptr
 
bool m_has_corner = false
 
bool m_has_edge = false
 
bool m_has_tile = false
 

Detailed Description

Iterates over all map tiles that intersect with a rectangle in GUI coordinates.

The order of iteration is guaranteed to satisfy the painter's algorithm. The iteration covers not only tiles but tile edges and corners.

Iteration is performed by repeatedly calling next and querying the current available items using has_corner, has_edge, or has_tile. If the item type matches your needs, you can retrieve the corner, edge, or tile using the corresponding functions.

Typical usage is as follows:

for (auto it = gui_rect_iterator(tileset, rect); it.next(); ) {
if (it.has_tile()) {
auto tile = it.tile();
// Do something
}
}
gui_rect_iterator(const struct tileset *t, const QRect &rect)
Constructor.
Definition: tile.h:42

One can also retrieve the canvas position of the current item using the x and y functions.

The order of iteration depends on the map topology. The basic idea is to operate on a rectangular grid, which works pretty nicely when the topology is neither isometric nor hexagonal:

| | |
t e t e t e t
| | |
e--c--e--c--e--c--e--
| | |
t e t e t e t
| | |
e--c--e--c--e--c--e--
| | |
t e t e t e t
| | |

In the diagram above, "t" stands for tile, "e" for edge, and "c" for corner. Iteration takes place from left to right and from top to bottom, i.e. in reading order.

Things get more complicated for isometric grids, as some points on the grid don't correspond to anything and need to be skipped. They are represented with dots in the diagram below:

t . c . t . c . t
/ \ / \
. e . e . e . e .
/ \ / \
c . t . c . t . c
\ / \ /
. e . e . e . e .
\ / \ /
t . c . t . c . t
/ \ / \
. e . e . e . e .
/ \ / \
c . t . c . t . c
\ / \ /
. e . e . e . e .
\ / \ /
t . c . t . c . t

For hexagonal grids, some points are corners and edges at the same time:

. --ce- . t . --ce- .
/ \ / \
e . e . e . e
/ \ / \
- . t . --ce- . t . -
\ / \ /
e . e . e . e
\ / \ /
. --ce- . t . --ce- .
/ \ / \
e . e . e . e
/ \ / \
- . t . --ce- . t . -
\ / \ /
e . e . e . e
\ / \ /
. --ce- . t . --ce- .
t . c . t . c . t
/ \ / \
. e . e . e . e .
/ \ / \
| | |
ce. t . ce. t . ce
| | |
\ / \ /
. e . e . e . e .
\ / \ /
| |
t . ce. t . ce. t
| |
/ \ / \
. e . e . e . e .
/ \ / \
| | |
ce. t . ce. t . ce
| | |
\ / \ /
. e . e . e . e .
\ / \ /
t . c . t . c . t

During iteration, it may happen that some tiles are null because they are not visible to the current player. In this case, has_tile will return false.

Definition at line 19 of file view_map_geometry.h.

Constructor & Destructor Documentation

◆ gui_rect_iterator()

freeciv::gui_rect_iterator::gui_rect_iterator ( const struct tileset t,
const QRect &  rect 
)

Constructor.

Definition at line 148 of file view_map_geometry.cpp.

Member Function Documentation

◆ corner()

const tile_corner& freeciv::gui_rect_iterator::corner ( ) const
inline

Retrieves the current corner. Only valid if has_corner is true.

Definition at line 33 of file view_map_geometry.h.

◆ edge()

const tile_edge& freeciv::gui_rect_iterator::edge ( ) const
inline

Retrieves the current edge. Only valid if has_edge is true.

Definition at line 36 of file view_map_geometry.h.

◆ has_corner()

bool freeciv::gui_rect_iterator::has_corner ( ) const
inline

Checks whether the current iteration point has a corner.

Definition at line 24 of file view_map_geometry.h.

◆ has_edge()

bool freeciv::gui_rect_iterator::has_edge ( ) const
inline

Checks whether the current iteration point has an edge.

Definition at line 27 of file view_map_geometry.h.

◆ has_tile()

bool freeciv::gui_rect_iterator::has_tile ( ) const
inline

Checks whether the current iteration point has a non-null tile.

Definition at line 30 of file view_map_geometry.h.

◆ next()

bool freeciv::gui_rect_iterator::next ( )

Iterates to the next item.

Returns false once the end of the iteration has been reached.

Definition at line 183 of file view_map_geometry.cpp.

Referenced by editor_end_selection_rectangle(), show_city_descriptions(), show_tile_labels(), and update_map_canvas().

◆ tile()

const ::tile* freeciv::gui_rect_iterator::tile ( ) const
inline

Retrieves the current tile. Only valid if has_tile is true.

Definition at line 39 of file view_map_geometry.h.

◆ x()

int freeciv::gui_rect_iterator::x ( ) const
inline

Retrieves the x position of the current item.

Definition at line 42 of file view_map_geometry.h.

◆ y()

int freeciv::gui_rect_iterator::y ( ) const
inline

Retrieves the y position of the current item.

Definition at line 45 of file view_map_geometry.h.

Member Data Documentation

◆ m_corner

tile_corner freeciv::gui_rect_iterator::m_corner
private

Definition at line 56 of file view_map_geometry.h.

Referenced by corner(), and next().

◆ m_count

int freeciv::gui_rect_iterator::m_count
private

Definition at line 51 of file view_map_geometry.h.

Referenced by gui_rect_iterator(), and next().

◆ m_edge

tile_edge freeciv::gui_rect_iterator::m_edge
private

Definition at line 57 of file view_map_geometry.h.

Referenced by edge(), and next().

◆ m_h

int freeciv::gui_rect_iterator::m_h
private

Definition at line 52 of file view_map_geometry.h.

Referenced by gui_rect_iterator(), and y().

◆ m_has_corner

bool freeciv::gui_rect_iterator::m_has_corner = false
private

Definition at line 59 of file view_map_geometry.h.

Referenced by has_corner(), and next().

◆ m_has_edge

bool freeciv::gui_rect_iterator::m_has_edge = false
private

Definition at line 59 of file view_map_geometry.h.

Referenced by has_edge(), and next().

◆ m_has_tile

bool freeciv::gui_rect_iterator::m_has_tile = false
private

Definition at line 59 of file view_map_geometry.h.

Referenced by has_tile(), and next().

◆ m_index

int freeciv::gui_rect_iterator::m_index
private

Definition at line 51 of file view_map_geometry.h.

Referenced by gui_rect_iterator(), and next().

◆ m_isometric

bool freeciv::gui_rect_iterator::m_isometric
private

Definition at line 50 of file view_map_geometry.h.

Referenced by gui_rect_iterator(), and next().

◆ m_r1

int freeciv::gui_rect_iterator::m_r1
private

Definition at line 52 of file view_map_geometry.h.

Referenced by gui_rect_iterator().

◆ m_r2

int freeciv::gui_rect_iterator::m_r2
private

Definition at line 52 of file view_map_geometry.h.

Referenced by gui_rect_iterator(), x(), and y().

◆ m_tile

::tile* freeciv::gui_rect_iterator::m_tile = nullptr
private

Definition at line 58 of file view_map_geometry.h.

Referenced by next(), and tile().

◆ m_w

int freeciv::gui_rect_iterator::m_w
private

Definition at line 52 of file view_map_geometry.h.

Referenced by gui_rect_iterator(), and x().

◆ m_x0

int freeciv::gui_rect_iterator::m_x0
private

Definition at line 53 of file view_map_geometry.h.

Referenced by gui_rect_iterator(), and next().

◆ m_x1

int freeciv::gui_rect_iterator::m_x1
private

Definition at line 53 of file view_map_geometry.h.

Referenced by gui_rect_iterator(), and next().

◆ m_xi

int freeciv::gui_rect_iterator::m_xi
private

Definition at line 54 of file view_map_geometry.h.

Referenced by next(), and x().

◆ m_y0

int freeciv::gui_rect_iterator::m_y0
private

Definition at line 53 of file view_map_geometry.h.

Referenced by gui_rect_iterator(), and next().

◆ m_y1

int freeciv::gui_rect_iterator::m_y1
private

Definition at line 53 of file view_map_geometry.h.

Referenced by gui_rect_iterator().

◆ m_yi

int freeciv::gui_rect_iterator::m_yi
private

Definition at line 54 of file view_map_geometry.h.

Referenced by next(), and y().