yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
dimension_service.cc
Go to the documentation of this file.
2
3#include <algorithm>
4
7
8namespace yaze {
9namespace zelda3 {
10
11namespace {
12
14 const absl::StatusOr<GeometryBounds>& geometry_result) {
15 return geometry_result.ok() && geometry_result->width_tiles > 0 &&
16 geometry_result->height_tiles > 0;
17}
18
19} // namespace
20
22 static DimensionService instance;
23 return instance;
24}
25
27 const RoomObject& obj) const {
28 // Try ObjectGeometry first (exact buffer-replay).
29 auto geo_result = ObjectGeometry::Get().MeasureByObjectId(obj);
30 if (HasMeasuredGeometry(geo_result)) {
31 const auto& bounds = *geo_result;
32 return DimensionResult{
33 .offset_x_tiles = bounds.min_x_tiles,
34 .offset_y_tiles = bounds.min_y_tiles,
35 .width_tiles = std::max(1, bounds.width_tiles),
36 .height_tiles = std::max(1, bounds.height_tiles),
37 };
38 }
39
40 // Fall back to ObjectDimensionTable.
41 auto& dim_table = ObjectDimensionTable::Get();
42 if (dim_table.IsLoaded()) {
43 auto sel = dim_table.GetSelectionBounds(obj.id_, obj.size_);
44 return DimensionResult{
45 .offset_x_tiles = sel.offset_x,
46 .offset_y_tiles = sel.offset_y,
47 .width_tiles = std::max(1, sel.width),
48 .height_tiles = std::max(1, sel.height),
49 };
50 }
51
52 // Last resort: match legacy `ObjectDrawer` default branch (separate X/Y
53 // nibbles in `size_`, each 0..15 meaning span in tiles = nibble + 1).
54 int size_h = obj.size_ & 0x0F;
55 int size_v = (obj.size_ >> 4) & 0x0F;
56 return DimensionResult{
57 .offset_x_tiles = 0,
58 .offset_y_tiles = 0,
59 .width_tiles = std::max(1, size_h + 1),
60 .height_tiles = std::max(1, size_v + 1),
61 };
62}
63
65 const RoomObject& obj) const {
66 auto result = GetDimensions(obj);
67 return {result.width_pixels(), result.height_pixels()};
68}
69
70std::tuple<int, int, int, int> DimensionService::GetHitTestBounds(
71 const RoomObject& obj) const {
72 auto geo_result = ObjectGeometry::Get().MeasureByObjectId(obj);
73 if (HasMeasuredGeometry(geo_result)) {
74 const auto selection = geo_result->GetSelectionBounds();
75 return {obj.x_ + selection.x_tiles, obj.y_ + selection.y_tiles,
76 selection.width_tiles, selection.height_tiles};
77 }
78
79 auto result = GetDimensions(obj);
80 return {obj.x_ + result.offset_x_tiles, obj.y_ + result.offset_y_tiles,
81 result.width_tiles, result.height_tiles};
82}
83
84std::tuple<int, int, int, int> DimensionService::GetSelectionBoundsPixels(
85 const RoomObject& obj) const {
86 auto geo_result = ObjectGeometry::Get().MeasureByObjectId(obj);
87 if (HasMeasuredGeometry(geo_result)) {
88 const auto selection = geo_result->GetSelectionBounds();
89 int x_px = (obj.x_ + selection.x_tiles) * 8;
90 int y_px = (obj.y_ + selection.y_tiles) * 8;
91 return {x_px, y_px, selection.width_pixels(), selection.height_pixels()};
92 }
93
94 auto result = GetDimensions(obj);
95 int x_px = (obj.x_ + result.offset_x_tiles) * 8;
96 int y_px = (obj.y_ + result.offset_y_tiles) * 8;
97 return {x_px, y_px, result.width_pixels(), result.height_pixels()};
98}
99
100} // namespace zelda3
101} // namespace yaze
Unified dimension lookup for dungeon room objects.
static DimensionService & Get()
std::tuple< int, int, int, int > GetSelectionBoundsPixels(const RoomObject &obj) const
DimensionResult GetDimensions(const RoomObject &obj) const
std::tuple< int, int, int, int > GetHitTestBounds(const RoomObject &obj) const
std::pair< int, int > GetPixelDimensions(const RoomObject &obj) const
static ObjectDimensionTable & Get()
absl::StatusOr< GeometryBounds > MeasureByObjectId(const RoomObject &object) const
static ObjectGeometry & Get()
bool HasMeasuredGeometry(const absl::StatusOr< GeometryBounds > &geometry_result)