1// Aseprite
2// Copyright (C) 2019-2020 Igara Studio S.A.
3//
4// This program is distributed under the terms of
5// the End-User License Agreement for Aseprite.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "app/ui/tileset_selector.h"
12
13#include "app/i18n/strings.h"
14#include "doc/sprite.h"
15#include "doc/tilesets.h"
16#include "fmt/format.h"
17#include "ui/listitem.h"
18#include "ui/window.h"
19
20namespace app {
21
22using namespace ui;
23
24TilesetSelector::TilesetSelector(const doc::Sprite* sprite,
25 const TilesetSelector::Info& info)
26{
27 initTheme();
28
29 name()->setText(info.name);
30 gridWidth()->setTextf("%d", info.grid.tileSize().w);
31 gridHeight()->setTextf("%d", info.grid.tileSize().h);
32 baseIndex()->setTextf("%d", info.baseIndex);
33
34 doc::tileset_index tsi = 0;
35 for (doc::Tileset* tileset : *sprite->tilesets()) {
36 auto item = new ListItem(
37 fmt::format("Tileset #{0} ({1}x{2}): \"{3}\"",
38 tsi,
39 tileset->grid().tileSize().w,
40 tileset->grid().tileSize().w,
41 tileset->name()));
42 tilesets()->addItem(item);
43
44 if (info.tsi == tsi)
45 tilesets()->setSelectedItem(item);
46
47 ++tsi;
48 }
49
50 if (info.enabled) {
51 tilesets()->Change.connect(
52 [this, sprite]() {
53 int index = tilesets()->getSelectedItemIndex();
54 bool editable = (index == 0);
55
56 if (index == 0) {
57 name()->setText("");
58 baseIndex()->setTextf("%d", 1);
59 }
60 else {
61 doc::Tileset* ts = sprite->tilesets()->get(index-1);
62 doc::Grid grid = ts->grid();
63 name()->setText(ts->name());
64 gridWidth()->setTextf("%d", grid.tileSize().w);
65 gridHeight()->setTextf("%d", grid.tileSize().h);
66 baseIndex()->setTextf("%d", ts->baseIndex());
67 }
68
69 name()->setEnabled(editable);
70 gridWidth()->setEnabled(editable);
71 gridHeight()->setEnabled(editable);
72 baseIndex()->setEnabled(editable);
73 });
74 }
75 else {
76 tilesets()->setEnabled(false);
77 gridWidth()->setEnabled(false);
78 gridHeight()->setEnabled(false);
79 }
80}
81
82TilesetSelector::Info TilesetSelector::getInfo()
83{
84 int itemIndex = tilesets()->getSelectedItemIndex();
85 Info info;
86 if (itemIndex == 0) {
87 gfx::Size sz(std::max(1, gridWidth()->textInt()),
88 std::max(1, gridHeight()->textInt()));
89
90 info.newTileset = true;
91 info.grid = doc::Grid::MakeRect(sz);
92 }
93 else {
94 info.newTileset = false;
95 info.tsi = itemIndex-1;
96 }
97 info.name = name()->text();
98 info.baseIndex = baseIndex()->textInt();
99 return info;
100}
101
102} // namespace app
103