Add error handling to SnesPalette and SnesColor member fns

This commit is contained in:
scawful
2024-04-07 12:07:47 -04:00
parent f28e33b5d0
commit 03a38fc712
19 changed files with 188 additions and 117 deletions

View File

@@ -59,6 +59,8 @@ absl::Status DungeonEditor::Update() {
}
absl::Status DungeonEditor::Initialize() {
ASSIGN_OR_RETURN(auto dungeon_man_pal_group,
rom()->palette_group("dungeon_main"));
for (int i = 0; i < 0x100 + 40; i++) {
rooms_.emplace_back(zelda3::dungeon::Room(i));
rooms_[i].LoadHeader();
@@ -76,8 +78,7 @@ absl::Status DungeonEditor::Initialize() {
ASSIGN_OR_RETURN(auto palette_id,
rom()->ReadWord(0xDEC4B + dungeon_palette_ptr));
int p_id = palette_id / 180;
auto color = rom()->palette_group("dungeon_main")[p_id][3];
auto color = dungeon_man_pal_group[p_id][3];
room_palette_[rooms_[i].palette] = color.rgb();
}
@@ -85,8 +86,7 @@ absl::Status DungeonEditor::Initialize() {
LoadRoomEntrances();
// Load the palette group and palette for the dungeon
full_palette_ =
rom()->palette_group("dungeon_main")[current_palette_group_id_];
full_palette_ = dungeon_man_pal_group[current_palette_group_id_];
ASSIGN_OR_RETURN(current_palette_group_,
gfx::CreatePaletteGroupFromLargePalette(full_palette_));
@@ -105,10 +105,12 @@ absl::Status DungeonEditor::RefreshGraphics() {
current_palette_group_[current_palette_id_], 0);
rom()->UpdateBitmap(graphics_bin_[block].get(), true);
}
ASSIGN_OR_RETURN(auto sprites_aux1_pal_group,
rom()->palette_group("sprites_aux1"));
for (int i = 9; i < 16; i++) {
int block = rooms_[current_room_id_].blocks()[i];
graphics_bin_[block].get()->ApplyPaletteWithTransparent(
rom()->palette_group("sprites_aux1")[current_palette_id_], 0);
sprites_aux1_pal_group[current_palette_id_], 0);
rom()->UpdateBitmap(graphics_bin_[block].get(), true);
}
return absl::OkStatus();
@@ -156,13 +158,14 @@ void DungeonEditor::LoadDungeonRoomSize() {
}
}
void DungeonEditor::UpdateDungeonRoomView() {
absl::Status DungeonEditor::UpdateDungeonRoomView() {
DrawToolset();
if (palette_showing_) {
ImGui::Begin("Palette Editor", &palette_showing_, 0);
current_palette_ =
rom()->palette_group("dungeon_main")[current_palette_group_id_];
ASSIGN_OR_RETURN(auto dungeon_main_pal_group,
rom()->palette_group("dungeon_main"));
current_palette_ = dungeon_main_pal_group[current_palette_group_id_];
gui::SelectablePalettePipeline(current_palette_id_, refresh_graphics_,
current_palette_);
ImGui::End();

View File

@@ -51,7 +51,7 @@ class DungeonEditor : public Editor,
void LoadDungeonRoomSize();
void UpdateDungeonRoomView();
absl::Status UpdateDungeonRoomView();
void DrawToolset();
void DrawRoomSelector();

View File

@@ -321,8 +321,10 @@ absl::Status GraphicsEditor::UpdateGfxTabView() {
}
absl::Status GraphicsEditor::UpdatePaletteColumn() {
auto palette_group = rom()->palette_group(
kPaletteGroupAddressesKeys[edit_palette_group_name_index_]);
ASSIGN_OR_RETURN(
auto palette_group,
rom()->palette_group(
kPaletteGroupAddressesKeys[edit_palette_group_name_index_]));
auto palette = palette_group[edit_palette_index_];
@@ -711,7 +713,7 @@ absl::Status GraphicsEditor::DecompressImportData(int size) {
converted_sheet);
if (rom()->is_loaded()) {
auto palette_group = rom()->palette_group("ow_main");
ASSIGN_OR_RETURN(auto palette_group, rom()->palette_group("ow_main"));
z3_rom_palette_ = palette_group[current_palette_];
if (col_file_) {
bin_bitmap_.ApplyPalette(col_file_palette_);
@@ -743,8 +745,9 @@ absl::Status GraphicsEditor::DecompressSuperDonkey() {
col_file_palette_group_[current_palette_index_]);
} else {
// ROM palette
auto palette_group =
rom()->palette_group(kPaletteGroupAddressesKeys[current_palette_]);
ASSIGN_OR_RETURN(
auto palette_group,
rom()->palette_group(kPaletteGroupAddressesKeys[current_palette_]));
z3_rom_palette_ = palette_group[current_palette_index_];
graphics_bin_[i].ApplyPalette(z3_rom_palette_);
}
@@ -768,8 +771,9 @@ absl::Status GraphicsEditor::DecompressSuperDonkey() {
col_file_palette_group_[current_palette_index_]);
} else {
// ROM palette
auto palette_group =
rom()->palette_group(kPaletteGroupAddressesKeys[current_palette_]);
ASSIGN_OR_RETURN(
auto palette_group,
rom()->palette_group(kPaletteGroupAddressesKeys[current_palette_]));
z3_rom_palette_ = palette_group[current_palette_index_];
graphics_bin_[i].ApplyPalette(z3_rom_palette_);
}

View File

@@ -75,39 +75,42 @@ absl::Status PaletteEditor::Update() {
return absl::OkStatus();
}
void PaletteEditor::EditColorInPalette(gfx::SnesPalette& palette, int index) {
absl::Status PaletteEditor::EditColorInPalette(gfx::SnesPalette& palette,
int index) {
if (index >= palette.size()) {
// Handle error: the index is out of bounds
return;
return absl::InvalidArgumentError("Index out of bounds");
}
// Get the current color
auto currentColor = palette.GetColor(index).rgb();
ASSIGN_OR_RETURN(auto color, palette.GetColor(index));
auto currentColor = color.rgb();
if (ImGui::ColorPicker4("Color Picker", (float*)&palette[index])) {
// The color was modified, update it in the palette
palette(index, currentColor);
}
return absl::OkStatus();
}
void PaletteEditor::ResetColorToOriginal(
absl::Status PaletteEditor::ResetColorToOriginal(
gfx::SnesPalette& palette, int index,
const gfx::SnesPalette& originalPalette) {
if (index >= palette.size() || index >= originalPalette.size()) {
// Handle error: the index is out of bounds
return;
return absl::InvalidArgumentError("Index out of bounds");
}
auto originalColor = originalPalette.GetColor(index).rgb();
ASSIGN_OR_RETURN(auto color, originalPalette.GetColor(index));
auto originalColor = color.rgb();
palette(index, originalColor);
return absl::OkStatus();
}
absl::Status PaletteEditor::DrawPaletteGroup(int category) {
if (!rom()->is_loaded()) {
return absl::NotFoundError("ROM not open, no palettes to display");
}
const auto size =
rom()->palette_group(kPaletteGroupNames[category].data()).size();
ASSIGN_OR_RETURN(auto palette_group,
rom()->palette_group(kPaletteGroupNames[category].data()));
const auto size = palette_group.size();
auto palettes =
rom()->mutable_palette_group(kPaletteGroupNames[category].data());
static bool edit_color = false;
@@ -129,7 +132,7 @@ absl::Status PaletteEditor::DrawPaletteGroup(int category) {
// Small icon of the color in the palette
if (gui::SnesColorButton(popup_id, *palette->mutable_color(n),
palette_button_flags)) {
current_color_ = palette->GetColor(n);
ASSIGN_OR_RETURN(current_color_, palette->GetColor(n));
// EditColorInPalette(*palette, n);
}

View File

@@ -80,9 +80,9 @@ class PaletteEditor : public SharedROM {
absl::Status Update();
absl::Status DrawPaletteGroups();
void EditColorInPalette(gfx::SnesPalette& palette, int index);
void ResetColorToOriginal(gfx::SnesPalette& palette, int index,
const gfx::SnesPalette& originalPalette);
absl::Status EditColorInPalette(gfx::SnesPalette& palette, int index);
absl::Status ResetColorToOriginal(gfx::SnesPalette& palette, int index,
const gfx::SnesPalette& originalPalette);
void DisplayPalette(gfx::SnesPalette& palette, bool loaded);
void DrawPortablePalette(gfx::SnesPalette& palette);
absl::Status DrawPaletteGroup(int category);
@@ -90,11 +90,12 @@ class PaletteEditor : public SharedROM {
private:
absl::Status HandleColorPopup(gfx::SnesPalette& palette, int i, int j, int n);
void InitializeSavedPalette(const gfx::SnesPalette& palette) {
absl::Status InitializeSavedPalette(const gfx::SnesPalette& palette) {
for (int n = 0; n < palette.size(); n++) {
saved_palette_[n].x = palette.GetColor(n).rgb().x / 255;
saved_palette_[n].y = palette.GetColor(n).rgb().y / 255;
saved_palette_[n].z = palette.GetColor(n).rgb().z / 255;
ASSIGN_OR_RETURN(auto color, palette.GetColor(n));
saved_palette_[n].x = color.rgb().x / 255;
saved_palette_[n].y = color.rgb().y / 255;
saved_palette_[n].z = color.rgb().z / 255;
saved_palette_[n].w = 255; // Alpha
}
}

View File

@@ -107,8 +107,8 @@ absl::Status Tile16Editor::UpdateBlockset() {
if (notify_tile16.modified()) {
current_tile16_ = notify_tile16.get();
current_tile16_bmp_ = tile16_individual_[notify_tile16];
current_tile16_bmp_.ApplyPalette(
rom()->palette_group("ow_main")[current_palette_]);
ASSIGN_OR_RETURN(auto ow_main_pal_group, rom()->palette_group("ow_main"));
current_tile16_bmp_.ApplyPalette(ow_main_pal_group[current_palette_]);
rom()->RenderBitmap(&current_tile16_bmp_);
}
}
@@ -150,6 +150,8 @@ absl::Status Tile16Editor::DrawToCurrentTile16(ImVec2 click_position) {
}
absl::Status Tile16Editor::UpdateTile16Edit() {
ASSIGN_OR_RETURN(auto ow_main_pal_group, rom()->palette_group("ow_main"));
if (ImGui::BeginChild("Tile8 Selector",
ImVec2(ImGui::GetContentRegionAvail().x, 0x175),
true)) {
@@ -157,7 +159,7 @@ absl::Status Tile16Editor::UpdateTile16Edit() {
tile8_source_canvas_.DrawContextMenu();
if (tile8_source_canvas_.DrawTileSelector(32)) {
current_gfx_individual_[current_tile8_].ApplyPaletteWithTransparent(
rom()->palette_group("ow_main")[0], current_palette_);
ow_main_pal_group[0], current_palette_);
rom()->UpdateBitmap(&current_gfx_individual_[current_tile8_]);
}
tile8_source_canvas_.DrawBitmap(current_gfx_bmp_, 0, 0, 4.0f);
@@ -173,7 +175,7 @@ absl::Status Tile16Editor::UpdateTile16Edit() {
current_tile8_ = x + (y * 8);
current_gfx_individual_[current_tile8_].ApplyPaletteWithTransparent(
rom()->palette_group("ow_main")[0], current_palette_);
ow_main_pal_group[0], current_palette_);
rom()->UpdateBitmap(&current_gfx_individual_[current_tile8_]);
}
@@ -244,6 +246,8 @@ absl::Status Tile16Editor::InitBlockset(
}
absl::Status Tile16Editor::LoadTile8() {
ASSIGN_OR_RETURN(auto ow_main_pal_group, rom()->palette_group("ow_main"));
current_gfx_individual_.reserve(1024);
for (int index = 0; index < 1024; index++) {
@@ -278,7 +282,7 @@ absl::Status Tile16Editor::LoadTile8() {
current_gfx_individual_.emplace_back();
current_gfx_individual_[index].Create(0x08, 0x08, 0x08, tile_data);
current_gfx_individual_[index].ApplyPaletteWithTransparent(
rom()->palette_group("ow_main")[0], current_palette_);
ow_main_pal_group[0], current_palette_);
rom()->RenderBitmap(&current_gfx_individual_[index]);
}

View File

@@ -45,12 +45,13 @@ class Tile16Editor : public GfxContext, public SharedROM {
absl::Status LoadTile8();
auto set_tile16(int id) {
absl::Status set_tile16(int id) {
current_tile16_ = id;
current_tile16_bmp_ = tile16_individual_[id];
current_tile16_bmp_.ApplyPalette(
rom()->palette_group("ow_main")[current_palette_]);
ASSIGN_OR_RETURN(auto ow_main_pal_group, rom()->palette_group("ow_main"));
current_tile16_bmp_.ApplyPalette(ow_main_pal_group[current_palette_]);
rom()->RenderBitmap(&current_tile16_bmp_);
return absl::OkStatus();
}
private:
@@ -86,7 +87,8 @@ class Tile16Editor : public GfxContext, public SharedROM {
gfx::Bitmap tile16_blockset_bmp_;
// Canvas for editing the selected tile
gui::Canvas tile16_edit_canvas_{ImVec2(0x40, 0x40), gui::CanvasGridSize::k64x64};
gui::Canvas tile16_edit_canvas_{ImVec2(0x40, 0x40),
gui::CanvasGridSize::k64x64};
gfx::Bitmap current_tile16_bmp_;
gfx::Bitmap current_tile8_bmp_;

View File

@@ -344,21 +344,21 @@ void OverworldEditor::RefreshOverworldMap() {
}
// TODO: Palette throws out of bounds error unexpectedly.
void OverworldEditor::RefreshMapPalette() {
absl::Status OverworldEditor::RefreshMapPalette() {
if (overworld_.overworld_map(current_map_)->is_large_map()) {
// We need to update the map and its siblings if it's a large map
for (int i = 1; i < 4; i++) {
int sibling_index = overworld_.overworld_map(current_map_)->parent() + i;
if (i >= 2) sibling_index += 6;
overworld_.mutable_overworld_map(sibling_index)->LoadPalette();
maps_bmp_[sibling_index].ApplyPalette(
RETURN_IF_ERROR(maps_bmp_[sibling_index].ApplyPalette(
*overworld_.mutable_overworld_map(sibling_index)
->mutable_current_palette());
->mutable_current_palette()));
}
}
maps_bmp_[current_map_].ApplyPalette(
RETURN_IF_ERROR(maps_bmp_[current_map_].ApplyPalette(
*overworld_.mutable_overworld_map(current_map_)
->mutable_current_palette());
->mutable_current_palette()));
}
void OverworldEditor::RefreshMapProperties() {
@@ -415,7 +415,7 @@ void OverworldEditor::DrawOverworldMapSettings() {
->mutable_area_palette(),
kInputFieldSize)) {
RefreshMapProperties();
RefreshMapPalette();
status_ = RefreshMapPalette();
}
ImGui::EndGroup();

View File

@@ -103,7 +103,7 @@ class OverworldEditor : public Editor,
void RefreshChildMap(int i);
void RefreshOverworldMap();
void RefreshMapPalette();
absl::Status RefreshMapPalette();
void RefreshMapProperties();
void RefreshTile16Blockset();