Tile16 Editor updates (big commit)

This commit is contained in:
scawful
2024-01-23 22:37:23 -05:00
parent da06f46596
commit c3e616e60f
10 changed files with 213 additions and 137 deletions

View File

@@ -287,7 +287,7 @@ void Canvas::DrawTileOnBitmap(int tile_size, gfx::Bitmap &bitmap,
}
}
void Canvas::DrawTileSelector(int size) {
bool Canvas::DrawTileSelector(int size) {
const ImGuiIO &io = ImGui::GetIO();
const bool is_hovered = ImGui::IsItemHovered();
const ImVec2 origin(canvas_p0_.x + scrolling_.x, canvas_p0_.y + scrolling_.y);
@@ -303,50 +303,30 @@ void Canvas::DrawTileSelector(int size) {
points_.push_back(painter_pos);
points_.push_back(ImVec2(painter_pos.x + size, painter_pos.y + size));
mouse_pos_in_canvas_ = painter_pos;
}
if (is_hovered && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
return true;
}
return false;
}
void Canvas::HandleTileEdits(Canvas &blockset_canvas,
bool Canvas::HandleTileEdits(Canvas &blockset_canvas,
std::vector<gfx::Bitmap> &source_blockset,
gfx::Bitmap &destination, int &current_tile,
float scale, int tile_painter_size,
int tiles_per_row) {
int &current_tile, float scale,
int tile_painter_size, int tiles_per_row) {
if (!blockset_canvas.points().empty()) {
uint16_t x = blockset_canvas.points().front().x / 32;
uint16_t y = blockset_canvas.points().front().y / 32;
current_tile = x + (y * tiles_per_row);
if (DrawTilePainter(source_blockset[current_tile], tile_painter_size,
scale)) {
RenderUpdatedBitmap(drawn_tile_position(),
source_blockset[current_tile].mutable_data(),
destination);
}
}
}
void Canvas::RenderUpdatedBitmap(const ImVec2 &click_position,
const Bytes &tile_data,
gfx::Bitmap &destination) {
// Calculate the tile position relative to the current active map
constexpr int tile_size = 16; // Tile size is 16x16 pixels
// Calculate the tile index for x and y based on the click_position
int tile_index_x = (static_cast<int>(click_position.x) % 512) / tile_size;
int tile_index_y = (static_cast<int>(click_position.y) % 512) / tile_size;
// Calculate the pixel start position based on tile index and tile size
ImVec2 start_position;
start_position.x = tile_index_x * tile_size;
start_position.y = tile_index_y * tile_size;
// Update the bitmap's pixel data based on the start_position and tile_data
for (int y = 0; y < tile_size; ++y) {
for (int x = 0; x < tile_size; ++x) {
int pixel_index =
(start_position.y + y) * destination.width() + (start_position.x + x);
destination.WriteToPixel(pixel_index, tile_data[y * tile_size + x]);
return true;
}
}
return false;
}
void Canvas::DrawBitmap(const Bitmap &bitmap, int border_offset, bool ready) {

View File

@@ -75,16 +75,12 @@ class Canvas {
// 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);
bool DrawTileSelector(int size);
void HandleTileEdits(Canvas& blockset_canvas,
bool HandleTileEdits(Canvas& blockset_canvas,
std::vector<gfx::Bitmap>& source_blockset,
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);
int& current_tile, float scale = 1.0f,
int tile_painter_size = 16, int tiles_per_row = 8);
// Draws the contents of the Bitmap image to the Canvas
void DrawBitmap(const Bitmap& bitmap, int border_offset = 0,
@@ -113,6 +109,7 @@ class Canvas {
auto points() const { return points_; }
auto mutable_points() { return &points_; }
auto push_back(ImVec2 pos) { points_.push_back(pos); }
auto draw_list() const { return draw_list_; }
auto zero_point() const { return canvas_p0_; }
auto scrolling() const { return scrolling_; }
@@ -121,6 +118,9 @@ class Canvas {
void set_global_scale(float scale) { global_scale_ = scale; }
auto global_scale() const { return global_scale_; }
auto custom_labels_enabled() const { return enable_custom_labels_; }
auto custom_step() const { return custom_step_; }
auto width() const { return canvas_sz_.x; }
auto height() const { return canvas_sz_.y; }
auto labels(int i) {
if (i >= labels_.size()) {
@@ -135,6 +135,18 @@ class Canvas {
return &labels_[i];
}
int GetTileIdFromMousePos() {
int x = mouse_pos_in_canvas_.x;
int y = mouse_pos_in_canvas_.y;
int num_columns = width() / custom_step_;
int num_rows = height() / custom_step_;
int tile_id = (x / custom_step_) + (y / custom_step_) * num_columns;
if (tile_id >= num_columns * num_rows) {
tile_id = -1; // Invalid tile ID
}
return tile_id;
}
auto set_current_labels(int i) { current_labels_ = i; }
auto set_highlight_tile_id(int i) { highlight_tile_id = i; }

View File

@@ -16,9 +16,7 @@ namespace yaze {
namespace app {
namespace gui {
class DynamicLayout {
};
class DynamicLayout {};
TextEditor::LanguageDefinition GetAssemblyLanguageDef();
@@ -29,7 +27,7 @@ class BitmapViewer {
public:
BitmapViewer() : current_bitmap_index_(0) {}
void Display(const std::vector<gfx::Bitmap>& bitmaps) {
void Display(const std::vector<gfx::Bitmap>& bitmaps, float scale = 1.0f) {
if (bitmaps.empty()) {
ImGui::Text("No bitmaps available.");
return;
@@ -57,8 +55,9 @@ class BitmapViewer {
// Assuming Bitmap has a function to get its texture ID, and width and
// height.
ImTextureID tex_id = current_bitmap.texture();
ImVec2 size(current_bitmap.width(), current_bitmap.height());
ImGui::Image(tex_id, size);
ImVec2 size(current_bitmap.width() * scale,
current_bitmap.height() * scale);
// ImGui::Image(tex_id, size);
// Scroll if the image is larger than the display area.
if (ImGui::BeginChild("BitmapScrollArea", ImVec2(0, 0), false,