Add DrawTileOnBitmap to Canvas

This commit is contained in:
scawful
2023-12-19 18:42:06 -05:00
parent 9aaa91727d
commit 96cd8f1b15
2 changed files with 33 additions and 2 deletions

View File

@@ -178,6 +178,10 @@ bool Canvas::DrawSolidTilePainter(const ImVec4 &color, int size) {
painter_pos.x = std::floor((double)mouse_pos.x / size) * size;
painter_pos.y = std::floor((double)mouse_pos.y / size) * size;
// Clamp the size to a grid
painter_pos.x = std::clamp(painter_pos.x, 0.0f, canvas_sz_.x);
painter_pos.y = std::clamp(painter_pos.y, 0.0f, canvas_sz_.y);
auto painter_pos_end = ImVec2(painter_pos.x + size, painter_pos.y + size);
points_.push_back(painter_pos);
points_.push_back(painter_pos_end);
@@ -200,6 +204,28 @@ bool Canvas::DrawSolidTilePainter(const ImVec4 &color, int size) {
return false;
}
void Canvas::DrawTileOnBitmap(const ImVec2 &position, int tile_size,
gfx::Bitmap &bitmap, uint16_t color) {
// Calculate the tile indices based on the click position
int tile_index_x = static_cast<int>(position.x) / tile_size;
int tile_index_y = static_cast<int>(position.y) / tile_size;
// Calculate the pixel start position based on tile index and tile size
ImVec2 start_position(tile_index_x * tile_size, tile_index_y * tile_size);
// Update the bitmap's pixel data based on the start_position and color
for (int y = 0; y < tile_size; ++y) {
for (int x = 0; x < tile_size; ++x) {
// Calculate the actual pixel index in the bitmap
int pixel_index =
(start_position.y + y) * bitmap.width() + (start_position.x + x);
// Write the color to the pixel
bitmap.WriteToPixel(pixel_index, color);
}
}
}
void Canvas::DrawTileSelector(int size) {
const ImGuiIO &io = ImGui::GetIO();
const bool is_hovered = ImGui::IsItemHovered();
@@ -230,7 +256,7 @@ void Canvas::HandleTileEdits(Canvas &blockset_canvas,
current_tile = x + (y * tiles_per_row);
if (DrawTilePainter(source_blockset[current_tile], tile_painter_size,
scale)) {
RenderUpdatedBitmap(GetCurrentDrawnTilePosition(),
RenderUpdatedBitmap(drawn_tile_position(),
source_blockset[current_tile].mutable_data(),
destination);
}

View File

@@ -47,6 +47,10 @@ class Canvas {
bool DrawTilePainter(const Bitmap& bitmap, int size, float scale = 1.0f);
bool DrawSolidTilePainter(const ImVec4& color, int size);
// Draws a tile on the canvas at the specified position
void DrawTileOnBitmap(const ImVec2& position, int tile_size,
gfx::Bitmap& bitmap, uint16_t color);
// Dictates which tile is currently selected based on what the user clicks
// in the canvas window. Represented and split apart into a grid of tiles.
void DrawTileSelector(int size);
@@ -56,6 +60,7 @@ class Canvas {
gfx::Bitmap& destination, int& current_tile,
float scale = 1.0f, int tile_painter_size = 16,
int tiles_per_row = 8);
void RenderUpdatedBitmap(const ImVec2& click_position, const Bytes& tile_data,
gfx::Bitmap& destination);
@@ -78,7 +83,7 @@ class Canvas {
auto GetDrawList() const { return draw_list_; }
auto GetZeroPoint() const { return canvas_p0_; }
auto Scrolling() const { return scrolling_; }
auto GetCurrentDrawnTilePosition() const { return drawn_tile_pos_; }
auto drawn_tile_position() const { return drawn_tile_pos_; }
auto GetCanvasSize() const { return canvas_sz_; }
void SetCanvasSize(ImVec2 canvas_size) {
canvas_sz_ = canvas_size;