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
12 static DimensionService instance;
13 return instance;
14}
15
17 const RoomObject& obj) const {
18 // Try ObjectGeometry first (exact buffer-replay).
19 auto geo_result = ObjectGeometry::Get().MeasureByObjectId(obj);
20 if (geo_result.ok()) {
21 const auto& bounds = *geo_result;
22 return DimensionResult{
23 .offset_x_tiles = bounds.min_x_tiles,
24 .offset_y_tiles = bounds.min_y_tiles,
25 .width_tiles = std::max(1, bounds.width_tiles),
26 .height_tiles = std::max(1, bounds.height_tiles),
27 };
28 }
29
30 // Fall back to ObjectDimensionTable.
31 auto& dim_table = ObjectDimensionTable::Get();
32 if (dim_table.IsLoaded()) {
33 auto sel = dim_table.GetSelectionBounds(obj.id_, obj.size_);
34 return DimensionResult{
35 .offset_x_tiles = sel.offset_x,
36 .offset_y_tiles = sel.offset_y,
37 .width_tiles = std::max(1, sel.width),
38 .height_tiles = std::max(1, sel.height),
39 };
40 }
41
42 // Last resort: match legacy `ObjectDrawer` default branch (separate X/Y
43 // nibbles in `size_`, each 0..15 meaning span in tiles = nibble + 1).
44 int size_h = obj.size_ & 0x0F;
45 int size_v = (obj.size_ >> 4) & 0x0F;
46 return DimensionResult{
47 .offset_x_tiles = 0,
48 .offset_y_tiles = 0,
49 .width_tiles = std::max(1, size_h + 1),
50 .height_tiles = std::max(1, size_v + 1),
51 };
52}
53
55 const RoomObject& obj) const {
56 auto result = GetDimensions(obj);
57 return {result.width_pixels(), result.height_pixels()};
58}
59
60std::tuple<int, int, int, int> DimensionService::GetHitTestBounds(
61 const RoomObject& obj) const {
62 auto geo_result = ObjectGeometry::Get().MeasureByObjectId(obj);
63 if (geo_result.ok()) {
64 const auto selection = geo_result->GetSelectionBounds();
65 return {obj.x_ + selection.x_tiles, obj.y_ + selection.y_tiles,
66 selection.width_tiles, selection.height_tiles};
67 }
68
69 auto result = GetDimensions(obj);
70 return {obj.x_ + result.offset_x_tiles, obj.y_ + result.offset_y_tiles,
71 result.width_tiles, result.height_tiles};
72}
73
74std::tuple<int, int, int, int> DimensionService::GetSelectionBoundsPixels(
75 const RoomObject& obj) const {
76 auto geo_result = ObjectGeometry::Get().MeasureByObjectId(obj);
77 if (geo_result.ok()) {
78 const auto selection = geo_result->GetSelectionBounds();
79 int x_px = (obj.x_ + selection.x_tiles) * 8;
80 int y_px = (obj.y_ + selection.y_tiles) * 8;
81 return {x_px, y_px, selection.width_pixels(), selection.height_pixels()};
82 }
83
84 auto result = GetDimensions(obj);
85 int x_px = (obj.x_ + result.offset_x_tiles) * 8;
86 int y_px = (obj.y_ + result.offset_y_tiles) * 8;
87 return {x_px, y_px, result.width_pixels(), result.height_pixels()};
88}
89
90} // namespace zelda3
91} // 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()