yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
scratch_color_id_command.cc
Go to the documentation of this file.
2
3#include <map>
4#include <set>
5#include <utility>
6
7#include "util/macro.h"
8
9namespace yaze::editor {
10namespace {
11
12template <typename Library>
13auto* FindAsset(Library& library, uint64_t id) {
14 decltype(&library.assets.front()) found = nullptr;
15 for (auto& asset : library.assets) {
16 if (asset.id == id) {
17 found = &asset;
18 break;
19 }
20 }
21 return found;
22}
23
24} // namespace
25
26std::vector<ScratchColorIdTarget> WholeScratchAssetSelection(
27 uint64_t asset_id) {
28 std::vector<ScratchColorIdTarget> targets;
29 for (uint16_t tile = 0; tile < 64; ++tile) {
30 targets.push_back({asset_id, tile});
31 }
32 return targets;
33}
34
35absl::StatusOr<ScratchColorIdPlan> PlanScratchColorIdPermutation(
36 const GraphicsScratchLibrary& library,
37 const std::vector<ScratchColorIdTarget>& targets,
38 const ColorIdPermutation& permutation) {
40 std::map<uint64_t, uint32_t> ranges;
41 std::vector<ColorIdBacking> backings;
42 std::vector<ColorIdTarget> edit_targets;
43 for (const auto& target : targets) {
44 auto [range, inserted] = ranges.emplace(
45 target.asset_id, static_cast<uint32_t>(plan.asset_ids.size()));
46 if (inserted) {
47 const auto* asset = FindAsset(library, target.asset_id);
48 if (!asset) {
49 return absl::NotFoundError("Selected scratch asset is unavailable");
50 }
51 RETURN_IF_ERROR(asset->Validate());
52 plan.asset_ids.push_back(asset->id);
53 backings.push_back({range->second, asset->bpp, asset->pixels});
54 }
55 edit_targets.push_back({range->second, target.tile_index, target.pixel_mask,
56 target.hflip, target.vflip});
57 }
59 PlanColorIdEdit(backings, edit_targets, permutation));
60 return plan;
61}
62
64 const ScratchColorIdPlan& plan) {
65 std::vector<std::pair<GraphicsScratchAsset*, std::vector<uint8_t>>> next;
66 std::set<uint32_t> seen;
67 for (const auto& edit : plan.edit.ranges) {
68 if (edit.range >= plan.asset_ids.size() ||
69 !seen.insert(edit.range).second) {
70 return absl::InvalidArgumentError("Invalid scratch color-ID range");
71 }
72 auto* asset = FindAsset(library, plan.asset_ids[edit.range]);
73 if (!asset) {
74 return absl::FailedPreconditionError(
75 "Scratch asset was removed after planning");
76 }
77 if (asset->bpp != plan.edit.permutation.bpp) {
78 return absl::FailedPreconditionError(
79 "Scratch asset depth changed after planning");
80 }
81 auto pixels = asset->pixels;
82 RETURN_IF_ERROR(ApplyColorIdRangeEdit(edit, asset->bpp, true, pixels));
83 next.emplace_back(asset, std::move(pixels));
84 }
85 for (auto& [asset, pixels] : next) {
86 asset->pixels = std::move(pixels);
87 }
88 return absl::OkStatus();
89}
90
91} // namespace yaze::editor
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition macro.h:62
Editors are the view controllers for the application.
std::vector< ScratchColorIdTarget > WholeScratchAssetSelection(uint64_t asset_id)
absl::StatusOr< ScratchColorIdPlan > PlanScratchColorIdPermutation(const GraphicsScratchLibrary &library, const std::vector< ScratchColorIdTarget > &targets, const ColorIdPermutation &permutation)
absl::StatusOr< ColorIdEditPlan > PlanColorIdEdit(const std::vector< ColorIdBacking > &backings, const std::vector< ColorIdTarget > &targets, const ColorIdPermutation &permutation)
absl::Status ApplyScratchColorIdPlan(GraphicsScratchLibrary &library, const ScratchColorIdPlan &plan)
absl::Status ApplyColorIdRangeEdit(const ColorIdRangeEdit &edit, int bpp, bool forward, std::vector< uint8_t > &pixels)
#define RETURN_IF_ERROR(expr)
Definition snes.cc:22
std::vector< ColorIdRangeEdit > ranges