overworld editor tile selector/painter routines

This commit is contained in:
scawful
2022-12-31 13:53:07 -06:00
parent 95d69bcb92
commit a8729add01
6 changed files with 173 additions and 90 deletions

View File

@@ -63,6 +63,37 @@ void Canvas::DrawContextMenu() {
}
}
void Canvas::DrawTilePainter(const Bitmap& bitmap, int size) {
const ImGuiIO &io = ImGui::GetIO();
const bool is_hovered = ImGui::IsItemHovered(); // Hovered
const ImVec2 origin(canvas_p0_.x + scrolling_.x,
canvas_p0_.y + scrolling_.y); // Lock scrolled origin
const ImVec2 mouse_pos_in_canvas(io.MousePos.x - origin.x,
io.MousePos.y - origin.y);
if (is_hovered) {
if (!points_.empty()) {
points_.clear();
}
ImVec2 draw_tile_outline_pos;
draw_tile_outline_pos.x =
std::floor((double)mouse_pos_in_canvas.x / size) * size;
draw_tile_outline_pos.y =
std::floor((double)mouse_pos_in_canvas.y / size) * size;
points_.push_back(draw_tile_outline_pos);
points_.push_back(
ImVec2(draw_tile_outline_pos.x + size, draw_tile_outline_pos.y + size));
draw_list_->AddImage((void *)bitmap.GetTexture(),
ImVec2(draw_tile_outline_pos.x, draw_tile_outline_pos.y),
ImVec2(draw_tile_outline_pos.x + (bitmap.GetWidth() * 2),
draw_tile_outline_pos.y + (bitmap.GetHeight() * 2)));
} else {
points_.clear();
}
}
void Canvas::DrawTileSelector(int size) {
const ImGuiIO &io = ImGui::GetIO();
const bool is_hovered = ImGui::IsItemHovered(); // Hovered
@@ -70,7 +101,7 @@ void Canvas::DrawTileSelector(int size) {
canvas_p0_.y + scrolling_.y); // Lock scrolled origin
const ImVec2 mouse_pos_in_canvas(io.MousePos.x - origin.x,
io.MousePos.y - origin.y);
// Add first and second point
if (is_hovered && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
if (!points_.empty()) {
points_.clear();

View File

@@ -22,6 +22,7 @@ class Canvas {
void DrawBackground(ImVec2 canvas_size = ImVec2(0, 0));
void DrawContextMenu();
void DrawTilePainter(const Bitmap& bitmap, int size);
void DrawTileSelector(int size);
void DrawBitmap(const Bitmap& bitmap, int border_offset = 0,
bool ready = true);
@@ -55,6 +56,7 @@ class Canvas {
ImVec2 mouse_pos_in_canvas_;
std::vector<app::gfx::Bitmap> changed_tiles_;
app::gfx::Bitmap current_tile_;
std::string title_;
};