Editor namespace housekeeping, Canvas expansion

This commit is contained in:
Justin Scofield
2022-07-19 20:21:32 -04:00
parent 86150f82bd
commit 7a795fd75d
7 changed files with 44 additions and 230 deletions

View File

@@ -58,11 +58,10 @@ void Editor::UpdateScreen() {
DrawYazeMenu();
TAB_BAR("##TabBar")
DrawProjectEditor();
DrawOverworldEditor();
DrawDungeonEditor();
DrawGraphicsEditor();
DrawSpriteEditor();
DrawScreenEditor();
END_TAB_BAR()
ImGui::End();
@@ -197,154 +196,12 @@ void Editor::DrawHelpMenu() const {
if (ImGui::MenuItem("About")) {
// insert the about window here
}
ImGui::Text("Title: %s", rom_.getTitle());
ImGui::Text("ROM Size: %ld", rom_.getSize());
ImGui::EndMenu();
}
}
void Editor::DrawGraphicsSheet(int offset) {
SDL_Surface *surface =
SDL_CreateRGBSurfaceWithFormat(0, 128, 32, 8, SDL_PIXELFORMAT_INDEX8);
std::cout << "Drawing surface" << std::endl;
uchar *sheet_buffer = nullptr;
for (int i = 0; i < 8; i++) {
std::cout << "Red value: " << current_palette_[i].x << std::endl;
std::cout << "Green value: " << current_palette_[i].y << std::endl;
std::cout << "Blue value: " << current_palette_[i].z << std::endl;
surface->format->palette->colors[i].r = current_palette_[i].x * 255;
surface->format->palette->colors[i].g = current_palette_[i].y * 255;
surface->format->palette->colors[i].b = current_palette_[i].z * 255;
}
unsigned int snes_addr = 0;
unsigned int pc_addr = 0;
snes_addr = (unsigned int)((((rom_.data()[0x4F80 + offset]) << 16) |
((rom_.data()[0x505F + offset]) << 8) |
((rom_.data()[0x513E + offset]))));
pc_addr = core::SnesToPc(snes_addr);
std::cout << "Decompressing..." << std::endl;
auto decomp = rom_.Decompress(pc_addr);
std::cout << "Converting to 8bpp sheet..." << std::endl;
sheet_buffer = rom_.SNES3bppTo8bppSheet(decomp);
std::cout << "Assigning pixel data..." << std::endl;
surface->pixels = sheet_buffer;
std::cout << "Creating texture from surface..." << std::endl;
SDL_Texture *sheet_texture = nullptr;
sheet_texture = SDL_CreateTextureFromSurface(sdl_renderer_.get(), surface);
image_cache_[offset] = sheet_texture;
if (sheet_texture == nullptr) {
std::cout << "Error: " << SDL_GetError() << std::endl;
}
}
void Editor::DrawProjectEditor() {
if (ImGui::BeginTabItem("Project")) {
if (ImGui::BeginTable(
"##projectTable", 2,
ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable)) {
ImGui::TableSetupColumn("##inputs", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableSetupColumn("##outputs");
ImGui::TableNextColumn();
ImGui::Text("Title: %s", rom_.getTitle());
ImGui::Text("ROM Size: %ld", rom_.getSize());
ImGui::Separator();
// ----------------------------------------------------------------------
static bool loaded_image = false;
static int tilesheet_offset = 0;
ImGui::Text("Palette:");
for (int i = 0; i < 8; i++) {
std::string id = "##PaletteColor" + std::to_string(i);
ImGui::SameLine();
ImGui::ColorEdit4(id.c_str(), &current_palette_[i].x,
ImGuiColorEditFlags_NoInputs |
ImGuiColorEditFlags_DisplayRGB |
ImGuiColorEditFlags_DisplayHex);
}
ImGui::SetNextItemWidth(100.f);
ImGui::InputInt("Tilesheet Offset", &tilesheet_offset);
BASIC_BUTTON("Retrieve gfx") {
if (rom_.isLoaded()) {
DrawGraphicsSheet(tilesheet_offset);
loaded_image = true;
}
}
// ----------------------------------------------------------------------
ImGui::TableNextColumn();
static ImVector<ImVec2> points;
static ImVec2 scrolling(0.0f, 0.0f);
static bool opt_enable_context_menu = true;
ImVec2 canvas_p0 = ImGui::GetCursorScreenPos();
auto canvas_sz = ImVec2(
512 + 1, ImGui::GetContentRegionAvail().y + (tilesheet_offset * 256));
auto canvas_p1 =
ImVec2(canvas_p0.x + canvas_sz.x, canvas_p0.y + canvas_sz.y);
// Draw border and background color
const ImGuiIO &io = ImGui::GetIO();
ImDrawList *draw_list = ImGui::GetWindowDrawList();
draw_list->AddRectFilled(canvas_p0, canvas_p1, IM_COL32(32, 32, 32, 255));
draw_list->AddRect(canvas_p0, canvas_p1, IM_COL32(255, 255, 255, 255));
// This will catch our interactions
ImGui::InvisibleButton(
"canvas", canvas_sz,
ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight);
const bool is_active = ImGui::IsItemActive(); // Held
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);
// Pan (we use a zero mouse threshold when there's no context menu)
const float mouse_threshold_for_pan =
opt_enable_context_menu ? -1.0f : 0.0f;
if (is_active && ImGui::IsMouseDragging(ImGuiMouseButton_Right,
mouse_threshold_for_pan)) {
scrolling.x += io.MouseDelta.x;
scrolling.y += io.MouseDelta.y;
}
// Context menu (under default mouse threshold)
ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right);
if (opt_enable_context_menu && drag_delta.x == 0.0f &&
drag_delta.y == 0.0f)
ImGui::OpenPopupOnItemClick("context",
ImGuiPopupFlags_MouseButtonRight);
if (ImGui::BeginPopup("context")) {
ImGui::MenuItem("Placeholder");
ImGui::EndPopup();
}
// Draw grid around the canvas
draw_list->PushClipRect(canvas_p0, canvas_p1, true);
// Draw the tilesheets loaded from the ROM
if (loaded_image) {
for (const auto &[key, value] : image_cache_) {
int offset = 128 * (key + 1);
int top_left_y = canvas_p0.y + 2;
if (key >= 1) {
top_left_y = canvas_p0.y + 128 * key;
}
draw_list->AddImage((void *)value,
ImVec2(canvas_p0.x + 2, top_left_y),
ImVec2(canvas_p0.x + 512, canvas_p0.y + offset));
}
}
draw_list->PopClipRect();
ImGui::EndTable();
}
ImGui::EndTabItem();
}
}
void Editor::DrawOverworldEditor() {
TAB_ITEM("Overworld")
overworld_editor_.Update();
@@ -363,11 +220,6 @@ void Editor::DrawScreenEditor() {
END_TAB_ITEM()
}
void Editor::DrawGraphicsEditor() {
TAB_ITEM("Graphics")
END_TAB_ITEM()
}
void Editor::DrawSpriteEditor() {
TAB_ITEM("Sprites")
END_TAB_ITEM()

View File

@@ -37,13 +37,9 @@ class Editor {
void DrawViewMenu();
void DrawHelpMenu() const;
void DrawGraphicsSheet(int offset = 0);
void DrawProjectEditor();
void DrawOverworldEditor();
void DrawDungeonEditor();
void DrawScreenEditor();
void DrawGraphicsEditor();
void DrawSpriteEditor();
bool is_loaded_ = true;

View File

@@ -282,65 +282,26 @@ void OverworldEditor::DrawTile16Selector() const {
draw_list->PopClipRect();
}
void OverworldEditor::DrawTile8Selector() const {
static ImVec2 scrolling(0.0f, 0.0f);
ImVec2 canvas_p0 = ImGui::GetCursorScreenPos();
auto canvas_sz = ImVec2(256 + 1, kNumSheetsToLoad * 64 + 1);
auto canvas_p1 = ImVec2(canvas_p0.x + canvas_sz.x, canvas_p0.y + canvas_sz.y);
// Draw border and background color
const ImGuiIO &io = ImGui::GetIO();
ImDrawList *draw_list = ImGui::GetWindowDrawList();
draw_list->AddRectFilled(canvas_p0, canvas_p1, IM_COL32(32, 32, 32, 255));
draw_list->AddRect(canvas_p0, canvas_p1, IM_COL32(255, 255, 255, 255));
// This will catch our interactions
ImGui::InvisibleButton(
"Tile8SelectorCanvas", canvas_sz,
ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight);
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);
// Context menu (under default mouse threshold)
ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right);
if (drag_delta.x == 0.0f && drag_delta.y == 0.0f)
ImGui::OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight);
if (ImGui::BeginPopup("context")) {
ImGui::EndPopup();
}
void OverworldEditor::DrawTile8Selector() {
graphics_bin_canvas_.DrawBackground(
ImVec2(256 + 1, kNumSheetsToLoad * 64 + 1));
graphics_bin_canvas_.UpdateContext();
graphics_bin_canvas_.DrawGrid(16.0f);
if (all_gfx_loaded_) {
for (const auto &[key, value] : graphics_bin_) {
int offset = 64 * (key + 1);
int top_left_y = canvas_p0.y + 2;
int top_left_y = graphics_bin_canvas_.GetZeroPoint().y + 2;
if (key >= 1) {
top_left_y = canvas_p0.y + 64 * key;
top_left_y = graphics_bin_canvas_.GetZeroPoint().y + 64 * key;
}
draw_list->AddImage((void *)value.GetTexture(),
ImVec2(canvas_p0.x + 2, top_left_y),
ImVec2(canvas_p0.x + 256, canvas_p0.y + offset));
graphics_bin_canvas_.GetDrawList()->AddImage(
(void *)value.GetTexture(),
ImVec2(graphics_bin_canvas_.GetZeroPoint().x + 2, top_left_y),
ImVec2(graphics_bin_canvas_.GetZeroPoint().x + 256,
graphics_bin_canvas_.GetZeroPoint().y + offset));
}
}
// Draw grid + all lines in the canvas
draw_list->PushClipRect(canvas_p0, canvas_p1, true);
if (opt_enable_grid) {
const float GRID_STEP = 16.0f;
for (float x = fmodf(scrolling.x, GRID_STEP); x < canvas_sz.x;
x += GRID_STEP)
draw_list->AddLine(ImVec2(canvas_p0.x + x, canvas_p0.y),
ImVec2(canvas_p0.x + x, canvas_p1.y),
IM_COL32(200, 200, 200, 40));
for (float y = fmodf(scrolling.y, GRID_STEP); y < canvas_sz.y;
y += GRID_STEP)
draw_list->AddLine(ImVec2(canvas_p0.x, canvas_p0.y + y),
ImVec2(canvas_p1.x, canvas_p0.y + y),
IM_COL32(200, 200, 200, 40));
}
draw_list->PopClipRect();
graphics_bin_canvas_.DrawOverlay();
}
void OverworldEditor::DrawPseudoVRAM() {
@@ -354,9 +315,12 @@ void OverworldEditor::DrawPseudoVRAM() {
pseudo_vram_canvas_.DrawBackground();
pseudo_vram_canvas_.UpdateContext();
pseudo_vram_canvas_.DrawGrid();
// draw_list->AddImage((void *)rom_.GetVRAM().GetTileset(0).GetTexture(),
// ImVec2(canvas_p0.x + 2, canvas_p0.y + 2),
// ImVec2(canvas_p0.x + 256, canvas_p0.y + 64));
pseudo_vram_canvas_.GetDrawList()->AddImage(
(void *)rom_.GetVRAM().GetTileset(0).GetTexture(),
ImVec2(pseudo_vram_canvas_.GetZeroPoint().x + 2,
pseudo_vram_canvas_.GetZeroPoint().y + 2),
ImVec2(pseudo_vram_canvas_.GetZeroPoint().x + 256,
pseudo_vram_canvas_.GetZeroPoint().y + 64));
pseudo_vram_canvas_.DrawOverlay();
}

View File

@@ -35,7 +35,7 @@ class OverworldEditor {
void DrawOverworldCanvas();
void DrawTileSelector();
void DrawTile16Selector() const;
void DrawTile8Selector() const;
void DrawTile8Selector();
void DrawPseudoVRAM();
void LoadBlockset();
@@ -68,6 +68,7 @@ class OverworldEditor {
gui::Canvas overworld_map_canvas_;
gui::Canvas pseudo_vram_canvas_;
gui::Canvas graphics_bin_canvas_;
ImVec4 current_palette_[8];

View File

@@ -77,9 +77,8 @@ void ROM::LoadAllGraphicsData() {
data = Decompress(gfx_addr, core::UncompressedSheetSize);
}
gfx::Bitmap tilesheet_bmp(
core::kTilesheetWidth, core::kTilesheetHeight,
core::kTilesheetDepth, SNES3bppTo8bppSheet(data));
gfx::Bitmap tilesheet_bmp(core::kTilesheetWidth, core::kTilesheetHeight,
core::kTilesheetDepth, SNES3bppTo8bppSheet(data));
tilesheet_bmp.CreateTexture(sdl_renderer_);
graphics_bin_[i] = tilesheet_bmp;
@@ -157,10 +156,6 @@ uchar *ROM::Decompress(int pos, int size, bool reversed) {
// Reversed byte order for overworld maps
if (reversed) {
auto addr = (current_rom_[pos + 2]) | ((current_rom_[pos + 1]) << 8);
if (addr > buffer_pos) {
std::cout << "size error" << std::endl;
}
if (buffer_pos + length >= size) {
size *= 2;
buffer = new uchar[size];

View File

@@ -91,9 +91,10 @@ void Canvas::Update() {
draw_list->PopClipRect();
}
void Canvas::DrawBackground() {
void Canvas::DrawBackground(ImVec2 canvas_size) {
canvas_p0_ = ImGui::GetCursorScreenPos();
if (!custom_canvas_size_) canvas_sz_ = ImGui::GetContentRegionAvail();
if (canvas_size.x != 0) canvas_sz_ = canvas_size;
canvas_p1_ = ImVec2(canvas_p0_.x + canvas_sz_.x, canvas_p0_.y + canvas_sz_.y);
@@ -150,18 +151,17 @@ void Canvas::UpdateContext() {
}
}
void Canvas::DrawGrid() {
void Canvas::DrawGrid(float grid_step) {
// Draw grid + all lines in the canvas
draw_list_->PushClipRect(canvas_p0_, canvas_p1_, true);
if (enable_grid_) {
const float GRID_STEP = 64.0f;
for (float x = fmodf(scrolling_.x, GRID_STEP); x < canvas_sz_.x;
x += GRID_STEP)
for (float x = fmodf(scrolling_.x, grid_step); x < canvas_sz_.x;
x += grid_step)
draw_list_->AddLine(ImVec2(canvas_p0_.x + x, canvas_p0_.y),
ImVec2(canvas_p0_.x + x, canvas_p1_.y),
IM_COL32(200, 200, 200, 40));
for (float y = fmodf(scrolling_.y, GRID_STEP); y < canvas_sz_.y;
y += GRID_STEP)
for (float y = fmodf(scrolling_.y, grid_step); y < canvas_sz_.y;
y += grid_step)
draw_list_->AddLine(ImVec2(canvas_p0_.x, canvas_p0_.y + y),
ImVec2(canvas_p1_.x, canvas_p0_.y + y),
IM_COL32(200, 200, 200, 40));

View File

@@ -4,11 +4,11 @@
#include <imgui/imgui.h>
#include <cmath>
#include <memory>
#include <string>
namespace yaze {
namespace gui {
class Canvas {
public:
Canvas() = default;
@@ -17,11 +17,16 @@ class Canvas {
void Update();
void DrawBackground();
void DrawBackground(ImVec2 canvas_size = ImVec2(0, 0));
void UpdateContext();
void DrawGrid();
void DrawGrid(float grid_step = 64.0f);
void DrawOverlay(); // last
void SetCanvasSize(ImVec2 canvas_size) {
canvas_sz_ = canvas_size;
custom_canvas_size_ = true;
}
auto GetDrawList() const { return draw_list_; }
auto GetZeroPoint() const { return canvas_p0_; }
private:
@@ -30,15 +35,16 @@ class Canvas {
bool dragging_select_ = false;
bool custom_canvas_size_ = false;
std::string title_;
ImDrawList* draw_list_;
ImVector<ImVec2> points_;
ImVec2 scrolling_;
ImVec2 canvas_sz_;
ImVec2 canvas_p0_;
ImVec2 canvas_p1_;
std::string title_;
};
} // namespace gui
} // namespace yaze