GraphicsEditor updates, move pipelines to gui

This commit is contained in:
scawful
2023-11-26 20:09:25 -05:00
parent e529e6ca34
commit f22b066dba
20 changed files with 271 additions and 203 deletions

View File

@@ -10,7 +10,6 @@
namespace yaze {
namespace app {
namespace gui {
constexpr uint32_t kRectangleColor = IM_COL32(32, 32, 32, 255);
@@ -18,6 +17,25 @@ constexpr uint32_t kRectangleBorder = IM_COL32(255, 255, 255, 255);
constexpr ImGuiButtonFlags kMouseFlags =
ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight;
void Canvas::Update(const gfx::Bitmap &bitmap, ImVec2 bg_size, int tile_size,
float scale, float grid_size) {
DrawBackground(bg_size);
DrawContextMenu();
DrawTileSelector(tile_size);
DrawBitmap(bitmap, 0, 0, scale);
DrawGrid(grid_size);
DrawOverlay();
}
void Canvas::UpdateEvent(const std::function<void()> &event, ImVec2 bg_size,
int tile_size, float grid_size) {
DrawBackground(bg_size);
DrawContextMenu();
event();
DrawGrid(grid_size);
DrawOverlay();
}
void Canvas::DrawBackground(ImVec2 canvas_size) {
canvas_p0_ = ImGui::GetCursorScreenPos();
if (!custom_canvas_size_) canvas_sz_ = ImGui::GetContentRegionAvail();
@@ -63,6 +81,7 @@ void Canvas::DrawContextMenu() {
bool Canvas::DrawTilePainter(const Bitmap &bitmap, int size, float scale) {
const ImGuiIO &io = ImGui::GetIO();
const bool is_hovered = ImGui::IsItemHovered();
is_hovered_ = is_hovered;
// Lock scrolled origin
const ImVec2 origin(canvas_p0_.x + scrolling_.x, canvas_p0_.y + scrolling_.y);
const ImVec2 mouse_pos(io.MousePos.x - origin.x, io.MousePos.y - origin.y);
@@ -88,10 +107,6 @@ bool Canvas::DrawTilePainter(const Bitmap &bitmap, int size, float scale) {
ImVec2(origin.x + painter_pos.x, origin.y + painter_pos.y),
ImVec2(origin.x + painter_pos.x + bitmap.width() * scale,
origin.y + painter_pos.y + bitmap.height() * scale));
// ImVec2(
// canvas_p0_.x + x_offset + scrolling_.x + (bitmap.width() * scale),
// canvas_p0_.y + y_offset + scrolling_.y + (bitmap.height() * scale)));
}
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
@@ -169,9 +184,6 @@ void Canvas::RenderUpdatedBitmap(const ImVec2 &click_position,
destination.WriteToPixel(pixel_index, tile_data[y * tile_size + x]);
}
}
// Render the updated bitmap to the canvas
// rom()->RenderBitmap(&current_bitmap);
}
void Canvas::DrawBitmap(const Bitmap &bitmap, int border_offset, bool ready) {

View File

@@ -22,6 +22,12 @@ class Canvas {
explicit Canvas(ImVec2 canvas_size)
: custom_canvas_size_(true), canvas_sz_(canvas_size) {}
void Update(const gfx::Bitmap& bitmap, ImVec2 bg_size, int tile_size,
float scale = 1.0f, float grid_size = 64.0f);
void UpdateEvent(const std::function<void()>& event, ImVec2 bg_size,
int tile_size, float grid_size = 64.0f);
// Background for the Canvas represents region without any content drawn to
// it, but can be controlled by the user.
void DrawBackground(ImVec2 canvas_size = ImVec2(0, 0));
@@ -42,8 +48,8 @@ class Canvas {
void 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);
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);
@@ -70,6 +76,7 @@ class Canvas {
canvas_sz_ = canvas_size;
custom_canvas_size_ = true;
}
auto IsMouseHovering() const { return is_hovered_; }
private:
bool enable_grid_ = true;
@@ -85,11 +92,6 @@ class Canvas {
ImVec2 canvas_p1_;
ImVec2 mouse_pos_in_canvas_;
ImVec2 drawn_tile_pos_;
std::vector<app::gfx::Bitmap> changed_tiles_;
app::gfx::Bitmap current_tile_;
std::string title_;
};
} // namespace gui

194
src/app/gui/pipeline.cc Normal file
View File

@@ -0,0 +1,194 @@
#include "pipeline.h"
#include <ImGuiFileDialog/ImGuiFileDialog.h>
#include <imgui/imgui.h>
#include <imgui/misc/cpp/imgui_stdlib.h>
#include <imgui_memory_editor.h>
#include <functional>
#include <optional>
#include "absl/strings/string_view.h"
#include "app/core/common.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/snes_palette.h"
#include "app/gui/canvas.h"
#include "app/gui/color.h"
#include "app/gui/input.h"
#include "app/rom.h"
namespace yaze {
namespace app {
namespace gui {
void SelectablePalettePipeline(uint64_t& palette_id, bool& refresh_graphics,
gfx::SNESPalette& palette) {
if (ImGuiID child_id = ImGui::GetID((void*)(intptr_t)100);
ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true,
ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
ImGui::BeginGroup(); // Lock X position
ImGui::Text("Palette");
for (int n = 0; n < palette.size(); n++) {
// static gfx::SNESColor transparent_color;
// if ((n % 8) == 0) {
// gui::SNESColorButton("##transparent", transparent_color, 0,
// ImVec2(20, 20));
// ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
// }
ImGui::PushID(n);
if ((n % 7) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
// Check if the current row is selected
bool is_selected = (palette_id == n / 7);
// Add outline rectangle to the selected row
if (is_selected) {
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(1.0f, 1.0f, 0.0f, 1.0f));
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 2.0f);
}
if (gui::SNESColorButton("##palette", palette[n],
ImGuiColorEditFlags_NoAlpha |
ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip,
ImVec2(20, 20))) {
palette_id = n / 7;
refresh_graphics = true;
}
if (is_selected) {
ImGui::PopStyleColor();
ImGui::PopStyleVar();
}
ImGui::PopID();
}
ImGui::EndGroup();
}
ImGui::EndChild();
}
void GraphicsBinCanvasPipeline(int width, int height, int tile_size,
int num_sheets_to_load, int canvas_id,
bool is_loaded, gfx::BitmapTable& graphics_bin) {
gui::Canvas canvas;
if (ImGuiID child_id = ImGui::GetID((void*)(intptr_t)canvas_id);
ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true,
ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
canvas.DrawBackground(ImVec2(width + 1, num_sheets_to_load * height + 1));
canvas.DrawContextMenu();
if (is_loaded) {
for (const auto& [key, value] : graphics_bin) {
int offset = height * (key + 1);
int top_left_y = canvas.GetZeroPoint().y + 2;
if (key >= 1) {
top_left_y = canvas.GetZeroPoint().y + height * key;
}
canvas.GetDrawList()->AddImage(
(void*)value.texture(),
ImVec2(canvas.GetZeroPoint().x + 2, top_left_y),
ImVec2(canvas.GetZeroPoint().x + 0x100,
canvas.GetZeroPoint().y + offset));
}
}
canvas.DrawTileSelector(tile_size);
canvas.DrawGrid(tile_size);
canvas.DrawOverlay();
}
ImGui::EndChild();
}
void GraphicsManagerCanvasPipeline(int width, int height, int tile_size,
int num_sheets, int canvas_id,
bool is_loaded,
const gfx::BitmapManager& graphics_manager) {
gui::Canvas canvas;
if (ImGuiID child_id = ImGui::GetID((void*)(intptr_t)canvas_id);
ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true,
ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
canvas.DrawBackground(ImVec2(width + 1, num_sheets * height + 1));
canvas.DrawContextMenu();
if (is_loaded) {
for (const auto& [key, value] : graphics_manager) {
int offset = height * (key + 1);
int top_left_y = canvas.GetZeroPoint().y + 2;
if (key >= 1) {
top_left_y = canvas.GetZeroPoint().y + height * key;
}
canvas.GetDrawList()->AddImage(
(void*)value->texture(),
ImVec2(canvas.GetZeroPoint().x + 2, top_left_y),
ImVec2(canvas.GetZeroPoint().x + 0x100,
canvas.GetZeroPoint().y + offset));
}
}
canvas.DrawTileSelector(tile_size);
canvas.DrawGrid(tile_size);
canvas.DrawOverlay();
}
ImGui::EndChild();
}
void ButtonPipe(absl::string_view button_text, std::function<void()> callback) {
if (ImGui::Button(button_text.data())) {
callback();
}
}
void BitmapCanvasPipeline(gui::Canvas& canvas, const gfx::Bitmap& bitmap,
int width, int height, int tile_size, bool is_loaded,
bool scrollbar, int canvas_id) {
auto draw_canvas = [](gui::Canvas& canvas, const gfx::Bitmap& bitmap,
int width, int height, int tile_size, bool is_loaded) {
canvas.DrawBackground(ImVec2(width + 1, height + 1));
canvas.DrawContextMenu();
canvas.DrawBitmap(bitmap, 2, is_loaded);
canvas.DrawTileSelector(tile_size);
canvas.DrawGrid(tile_size);
canvas.DrawOverlay();
};
if (scrollbar) {
if (ImGuiID child_id = ImGui::GetID((void*)(intptr_t)canvas_id);
ImGui::BeginChild(child_id, ImGui::GetContentRegionAvail(), true,
ImGuiWindowFlags_AlwaysVerticalScrollbar)) {
draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
}
ImGui::EndChild();
} else {
draw_canvas(canvas, bitmap, width, height, tile_size, is_loaded);
}
}
void BuildAndRenderBitmapPipeline(int width, int height, int depth, Bytes data,
ROM& z3_rom, gfx::Bitmap& bitmap,
gfx::SNESPalette& palette) {
bitmap.Create(width, height, depth, data);
bitmap.ApplyPalette(palette);
z3_rom.RenderBitmap(&bitmap);
}
void FileDialogPipeline(absl::string_view display_key,
absl::string_view file_extensions,
std::optional<absl::string_view> button_text,
std::function<void()> callback) {
if (button_text.has_value() && ImGui::Button(button_text->data())) {
ImGuiFileDialog::Instance()->OpenDialog(display_key.data(), "Choose File",
file_extensions.data(), ".");
}
if (ImGuiFileDialog::Instance()->Display(
display_key.data(), ImGuiWindowFlags_NoCollapse, ImVec2(600, 400))) {
if (ImGuiFileDialog::Instance()->IsOk()) {
callback();
}
ImGuiFileDialog::Instance()->Close();
}
}
} // namespace gui
} // namespace app
} // namespace yaze