yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
overworld_context_target.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_OVERWORLD_CANVAS_OVERWORLD_CONTEXT_TARGET_H_
2#define YAZE_APP_EDITOR_OVERWORLD_CANVAS_OVERWORLD_CONTEXT_TARGET_H_
3
4#include <cmath>
5#include <optional>
6
7#include "imgui/imgui.h"
8#include "zelda3/common.h"
9
10namespace yaze::editor {
11
12// A value captured when the menu opens. Hover, selection, zoom, and game-state
13// changes while navigating the menu must not change an action's destination.
15 int map_id = -1;
16 int parent_map_id = -1;
17 int world = 0;
18 int game_state = 0;
20 int tile16_id = -1;
21
22 bool valid(int map_count = zelda3::kNumOverworldMaps) const {
23 if (world < 0 || world > 2 || game_state < 0 || game_state > 2 ||
24 !std::isfinite(world_position.x) || !std::isfinite(world_position.y) ||
25 world_position.x < 0 || world_position.x >= 4096 ||
26 world_position.y < 0 || world_position.y >= 4096) {
27 return false;
28 }
29 const int physical_map = world * 64 +
30 static_cast<int>(world_position.x) / 512 +
31 (static_cast<int>(world_position.y) / 512) * 8;
32 return map_id == physical_map && map_id < map_count &&
33 parent_map_id >= world * 64 && parent_map_id < (world + 1) * 64 &&
34 parent_map_id < map_count;
35 }
36};
37
38// Resolve geometry without depending on the previously hovered/selected map.
39// The caller adds the model's parent map and Tile16 ID to the returned value.
40inline std::optional<OverworldContextTarget> ResolveOverworldContextTarget(
41 int world, int game_state, ImVec2 screen_position, ImVec2 canvas_origin,
42 ImVec2 scrolling, float scale, int map_count = zelda3::kNumOverworldMaps) {
43 if (!std::isfinite(scale) || scale <= 0) {
44 return std::nullopt;
45 }
47 target.world = world;
48 target.game_state = game_state;
49 target.world_position =
50 ImVec2((screen_position.x - canvas_origin.x - scrolling.x) / scale,
51 (screen_position.y - canvas_origin.y - scrolling.y) / scale);
52 const auto pos = target.world_position;
53 if (!std::isfinite(pos.x) || !std::isfinite(pos.y) || pos.x < 0 ||
54 pos.x >= 4096 || pos.y < 0 || pos.y >= 4096 || world < 0 || world > 2) {
55 return std::nullopt;
56 }
57 target.map_id = world * 64 + static_cast<int>(pos.x) / 512 +
58 (static_cast<int>(pos.y) / 512) * 8;
59 target.parent_map_id = target.map_id;
60 return target.valid(map_count) ? std::make_optional(target) : std::nullopt;
61}
62
63} // namespace yaze::editor
64
65#endif // YAZE_APP_EDITOR_OVERWORLD_CANVAS_OVERWORLD_CONTEXT_TARGET_H_
Editors are the view controllers for the application.
std::optional< OverworldContextTarget > ResolveOverworldContextTarget(int world, int game_state, ImVec2 screen_position, ImVec2 canvas_origin, ImVec2 scrolling, float scale, int map_count=zelda3::kNumOverworldMaps)
constexpr int kNumOverworldMaps
Definition common.h:85
bool valid(int map_count=zelda3::kNumOverworldMaps) const