feat(editor): tilepicker respecta l'amplada del tileset si hi cap
This commit is contained in:
1 parent
27cf2f032d
commit
bcbf990a5f
1 file changed
+19
-11
@@ -22,10 +22,11 @@
|
||||
namespace {
|
||||
|
||||
// Geometria del TilePicker en espai canvas. `cols × rows_visible` tiles
|
||||
// encapsulats dins d'un rectangle amb `PICKER_MARGIN` per vora. Com que
|
||||
// el tileset real (p.ex. 32×16 tiles de 16 px = 512×256) no cabria a
|
||||
// 1× dins del canvas 336×240, linealitzem els índexs i els repartim
|
||||
// en una graella que SÍ cap, amb scroll vertical per veure la resta.
|
||||
// encapsulats dins d'un rectangle amb `PICKER_MARGIN` per vora. Si el
|
||||
// tileset cap en amplada (TCOLS·tile_size ≤ INNER_W), en respectem la
|
||||
// graella natural — els tiles surten ordenats igual que al GIF, només
|
||||
// amb scroll vertical si cal. Si no, linealitzem els índexs en una
|
||||
// graella més ampla que sí cap, amb scroll vertical per veure la resta.
|
||||
struct PickerLayout {
|
||||
int x{0};
|
||||
int y{0};
|
||||
@@ -45,18 +46,25 @@ namespace {
|
||||
const int INNER_W = GameCanvas::WIDTH - (2 * M);
|
||||
const int INNER_H = GameCanvas::HEIGHT - (2 * M);
|
||||
if (lay.tile_size <= 0) { return lay; }
|
||||
lay.cols = INNER_W / lay.tile_size;
|
||||
const int MAX_COLS = INNER_W / lay.tile_size;
|
||||
lay.rows_visible = INNER_H / lay.tile_size;
|
||||
lay.x = M;
|
||||
lay.y = M;
|
||||
lay.w = lay.cols * lay.tile_size;
|
||||
lay.h = lay.rows_visible * lay.tile_size;
|
||||
int tcols = 0;
|
||||
const auto TILESET = Resource::Cache::get().getSurface(tm.getTilesetPath());
|
||||
if (TILESET != nullptr) {
|
||||
const int TCOLS = TILESET->getWidth() / lay.tile_size;
|
||||
tcols = TILESET->getWidth() / lay.tile_size;
|
||||
const int TROWS = TILESET->getHeight() / lay.tile_size;
|
||||
lay.total_tiles = TCOLS * TROWS;
|
||||
lay.total_tiles = tcols * TROWS;
|
||||
}
|
||||
// Com que els índexs del tileset són row-major (idx = row·TCOLS + col),
|
||||
// fer servir cols = TCOLS reprodueix exactament la disposició del GIF.
|
||||
const bool FITS_W = (tcols > 0 && tcols <= MAX_COLS);
|
||||
lay.cols = FITS_W ? tcols : MAX_COLS;
|
||||
lay.w = lay.cols * lay.tile_size;
|
||||
lay.h = lay.rows_visible * lay.tile_size;
|
||||
// Centrat horitzontalment quan la graella natural és més estreta;
|
||||
// verticalment mantenim el marge superior i deixem scroll si cal.
|
||||
lay.x = M + ((INNER_W - lay.w) / 2);
|
||||
lay.y = M;
|
||||
const int TOTAL_ROWS = (lay.cols > 0) ? ((lay.total_tiles + lay.cols - 1) / lay.cols) : 0;
|
||||
lay.max_scroll = std::max(0, TOTAL_ROWS - lay.rows_visible);
|
||||
return lay;
|
||||
|
||||
Reference in new issue
Block a user