yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
dungeon_canvas_transform.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_DUNGEON_DUNGEON_CANVAS_TRANSFORM_H_
2#define YAZE_APP_EDITOR_DUNGEON_DUNGEON_CANVAS_TRANSFORM_H_
3
4#include <cmath>
5#include <utility>
6
7#include "imgui/imgui.h"
8
9namespace yaze::editor {
10
11// Pure coordinate transform shared by dungeon interaction and overlay paths.
12// Room pixels are always unscaled and relative to the 512x512 room origin.
14 public:
15 DungeonCanvasTransform(ImVec2 canvas_origin, ImVec2 scrolling, float scale)
16 : canvas_origin_(canvas_origin),
17 scrolling_(scrolling),
18 scale_(std::isfinite(scale) && scale > 0.0f ? scale : 1.0f) {}
19
20 [[nodiscard]] ImVec2 room_origin_screen() const {
21 return ImVec2(canvas_origin_.x + scrolling_.x,
23 }
24
25 [[nodiscard]] float scale() const { return scale_; }
26
27 [[nodiscard]] ImVec2 ScreenToRoomPixels(ImVec2 screen) const {
28 const ImVec2 origin = room_origin_screen();
29 return ImVec2((screen.x - origin.x) / scale_,
30 (screen.y - origin.y) / scale_);
31 }
32
33 [[nodiscard]] std::pair<int, int> ScreenToRoomPixelCoordinates(
34 ImVec2 screen) const {
35 const ImVec2 room = ScreenToRoomPixels(screen);
36 return {static_cast<int>(std::floor(room.x)),
37 static_cast<int>(std::floor(room.y))};
38 }
39
40 [[nodiscard]] std::pair<int, int> ScreenToRoomTiles(ImVec2 screen,
41 int tile_size) const {
42 const ImVec2 room = ScreenToRoomPixels(screen);
43 return {static_cast<int>(std::floor(room.x / tile_size)),
44 static_cast<int>(std::floor(room.y / tile_size))};
45 }
46
47 [[nodiscard]] ImVec2 RoomPixelsToScreen(ImVec2 room) const {
48 const ImVec2 origin = room_origin_screen();
49 return ImVec2(origin.x + room.x * scale_, origin.y + room.y * scale_);
50 }
51
52 [[nodiscard]] ImVec2 RoomSizeToScreen(ImVec2 room_size) const {
53 return ImVec2(room_size.x * scale_, room_size.y * scale_);
54 }
55
56 private:
58 ImVec2 scrolling_;
59 float scale_;
60};
61
62} // namespace yaze::editor
63
64#endif // YAZE_APP_EDITOR_DUNGEON_DUNGEON_CANVAS_TRANSFORM_H_
std::pair< int, int > ScreenToRoomPixelCoordinates(ImVec2 screen) const
ImVec2 ScreenToRoomPixels(ImVec2 screen) const
DungeonCanvasTransform(ImVec2 canvas_origin, ImVec2 scrolling, float scale)
ImVec2 RoomSizeToScreen(ImVec2 room_size) const
std::pair< int, int > ScreenToRoomTiles(ImVec2 screen, int tile_size) const
Editors are the view controllers for the application.