123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include "tile_selection.hpp"
- #include <ClanLib/Display/display.h>
- #include <iostream>
- #include "math.hpp"
- #include "tileset.hpp"
- class TileSelectionImpl
- {
- public:
- TilemapLayer tilemap;
- CL_Point start_pos;
- CL_Rect selection;
- bool active;
- };
- TileSelection::TileSelection()
- : impl(new TileSelectionImpl())
- {
- impl->active = false;
- }
- TileSelection::~TileSelection()
- {
- }
- void
- TileSelection::start(TilemapLayer tilemap_, const CL_Point& pos)
- {
- impl->tilemap = tilemap_;
- impl->active = true;
- impl->start_pos = pos;
- update(impl->start_pos);
- }
- void
- TileSelection::update(const CL_Point& pos)
- {
- impl->selection = CL_Rect(std::min(impl->start_pos.x, pos.x),
- std::min(impl->start_pos.y, pos.y),
- std::max(impl->start_pos.x, pos.x) + 1,
- std::max(impl->start_pos.y, pos.y) + 1);
- }
- bool
- TileSelection::is_active()
- {
- return impl->active;
- }
- void
- TileSelection::clear()
- {
- impl->selection = CL_Rect();
- impl->active = false;
- }
- void
- TileSelection::draw(const CL_Color& color)
- {
- int tile_size = impl->tilemap.get_tileset().get_tile_size();
- CL_Display::fill_rect(CL_Rect(impl->selection.left * tile_size,
- impl->selection.top * tile_size,
- impl->selection.right * tile_size,
- impl->selection.bottom * tile_size),
- color);
- }
- TileBrush
- TileSelection::get_brush(const Field<int>& field) const
- {
- CL_Rect sel = impl->selection;
- sel.normalize();
- if (sel.left > field.get_width() - 1
- || sel.top > field.get_height() - 1
- || sel.right <= 0
- || sel.bottom <= 0)
- {
- std::cout << "Error: Invalid selection" << std::endl;
- TileBrush brush(1, 1);
- brush.at(0, 0) = 0;
- brush.set_opaque();
- return brush;
- }
- else
- {
-
- sel.left = Math::max(0, sel.left);
- sel.top = Math::max(0, sel.top);
- sel.right = Math::min(sel.right, field.get_width());
- sel.bottom = Math::min(sel.bottom, field.get_height());
- TileBrush brush(sel.get_width(),
- sel.get_height());
- for(int y = sel.top; y < sel.bottom; ++y)
- for(int x = sel.left; x < sel.right; ++x)
- {
- brush.at(x - sel.left,
- y - sel.top) = field.at(x, y);
- }
- return brush;
- }
- }
- CL_Rect
- TileSelection::get_rect() const
- {
- CL_Rect sel = impl->selection;
- sel.normalize();
- return sel;
- }
|