[nostalgia/core/studio] Fix issue with pixel idx lookup in draw command
This commit is contained in:
parent
60e259ca34
commit
99cc853b94
@ -87,7 +87,8 @@ void TileSheetEditorImGui::drawTileSheet(const geo::Vec2 &fbSize) noexcept {
|
||||
if (wheelh != 0) {
|
||||
m_tileSheetEditor.scrollH(fbSize, wheelh);
|
||||
}
|
||||
if (io.MouseDown[0]) {
|
||||
if (io.MouseDown[0] && m_prevMouseDownPos != mousePos) {
|
||||
m_prevMouseDownPos = mousePos;
|
||||
auto clickPos = mousePos;
|
||||
const auto &winPos = ImGui::GetWindowPos();
|
||||
clickPos.x -= winPos.x + 10;
|
||||
@ -96,6 +97,7 @@ void TileSheetEditorImGui::drawTileSheet(const geo::Vec2 &fbSize) noexcept {
|
||||
}
|
||||
}
|
||||
if (io.MouseReleased[0]) {
|
||||
m_prevMouseDownPos = {-1, -1};
|
||||
m_tileSheetEditor.releaseMouseButton();
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ class TileSheetEditorImGui: public studio::Editor {
|
||||
glutils::FrameBuffer m_framebuffer;
|
||||
TileSheetEditor m_tileSheetEditor;
|
||||
float m_palViewWidth = 200;
|
||||
geo::Vec2 m_prevMouseDownPos;
|
||||
|
||||
public:
|
||||
TileSheetEditorImGui(Context *ctx, const ox::String &path);
|
||||
|
@ -17,6 +17,25 @@
|
||||
|
||||
namespace nostalgia::core {
|
||||
|
||||
enum class TileSheetTool: int {
|
||||
Select,
|
||||
Draw,
|
||||
Fill,
|
||||
};
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr auto toString(TileSheetTool t) noexcept {
|
||||
switch (t) {
|
||||
case TileSheetTool::Select:
|
||||
return "Select";
|
||||
case TileSheetTool::Draw:
|
||||
return "Draw";
|
||||
case TileSheetTool::Fill:
|
||||
return "Fill";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
class TileSheetEditor {
|
||||
|
||||
private:
|
||||
|
@ -23,32 +23,6 @@ enum class CommandId {
|
||||
ClipboardPaste = 5,
|
||||
};
|
||||
|
||||
enum class TileSheetTool: int {
|
||||
Select,
|
||||
Draw,
|
||||
Fill,
|
||||
};
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr auto toString(TileSheetTool t) noexcept {
|
||||
switch (t) {
|
||||
case TileSheetTool::Select:
|
||||
return "Select";
|
||||
case TileSheetTool::Draw:
|
||||
return "Draw";
|
||||
case TileSheetTool::Fill:
|
||||
return "Fill";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
struct PixelChunk {
|
||||
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.studio.PixelChunk";
|
||||
static constexpr auto TypeVersion = 1;
|
||||
geo::Point pt;
|
||||
int size = 0;
|
||||
};
|
||||
|
||||
struct DrawCommand: public studio::UndoCommand {
|
||||
private:
|
||||
struct Change {
|
||||
@ -62,18 +36,18 @@ struct DrawCommand: public studio::UndoCommand {
|
||||
public:
|
||||
constexpr DrawCommand(NostalgiaGraphic *img, std::size_t idx, int palIdx) noexcept {
|
||||
m_img = img;
|
||||
m_changes.emplace_back(idx, m_img->pixels[idx]);
|
||||
m_changes.emplace_back(idx, m_img->getPixel(idx));
|
||||
m_palIdx = palIdx;
|
||||
}
|
||||
|
||||
constexpr bool append(std::size_t idx) noexcept {
|
||||
if (m_changes.back().value.idx != idx) {
|
||||
if (m_changes.back().value.idx != idx && m_img->getPixel(idx) != m_palIdx) {
|
||||
// duplicate entries are bad
|
||||
auto existing = std::find_if(m_changes.cbegin(), m_changes.cend(), [idx](const auto &c) {
|
||||
return c.idx == idx;
|
||||
});
|
||||
if (existing == m_changes.cend()) {
|
||||
m_changes.emplace_back(idx, m_img->pixels[idx]);
|
||||
m_changes.emplace_back(idx, m_img->getPixel(idx));
|
||||
m_img->setPixel(idx, m_palIdx);
|
||||
return true;
|
||||
}
|
||||
@ -82,24 +56,19 @@ struct DrawCommand: public studio::UndoCommand {
|
||||
}
|
||||
|
||||
void redo() noexcept override {
|
||||
for (auto c : m_changes) {
|
||||
for (const auto &c : m_changes) {
|
||||
m_img->setPixel(c.idx, m_palIdx);
|
||||
}
|
||||
}
|
||||
|
||||
void undo() noexcept override {
|
||||
for (auto c : m_changes) {
|
||||
for (const auto &c : m_changes) {
|
||||
m_img->setPixel(c.idx, c.oldPalIdx);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
oxModelBegin(PixelChunk)
|
||||
oxModelField(pt)
|
||||
oxModelField(size)
|
||||
oxModelEnd()
|
||||
|
||||
struct TileSheetClipboard {
|
||||
static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.studio.TileSheetClipboard";
|
||||
static constexpr auto TypeVersion = 1;
|
||||
|
Loading…
Reference in New Issue
Block a user