yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
tile8_source_interaction.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_OVERWORLD_TILE8_SOURCE_INTERACTION_H
2#define YAZE_APP_EDITOR_OVERWORLD_TILE8_SOURCE_INTERACTION_H
3
4#include <algorithm>
5
6namespace yaze {
7namespace editor {
8
9inline bool ComputeTile8UsageHighlight(bool source_hovered,
10 bool right_mouse_down) {
11 return source_hovered && right_mouse_down;
12}
13
14inline int ComputeTile8IndexFromCanvasMouse(float mouse_x, float mouse_y,
15 int source_bitmap_width_px,
16 int max_tile_count,
17 float display_scale) {
18 if (source_bitmap_width_px <= 0 || max_tile_count <= 0 ||
19 display_scale <= 0) {
20 return -1;
21 }
22 if (mouse_x < 0.0f || mouse_y < 0.0f) {
23 return -1;
24 }
25
26 const int tile_x = static_cast<int>(mouse_x / (8.0f * display_scale));
27 const int tile_y = static_cast<int>(mouse_y / (8.0f * display_scale));
28 if (tile_x < 0 || tile_y < 0) {
29 return -1;
30 }
31
32 const int tiles_per_row = source_bitmap_width_px / 8;
33 if (tiles_per_row <= 0) {
34 return -1;
35 }
36
37 const int tile8_id = tile_x + (tile_y * tiles_per_row);
38 if (tile8_id < 0 || tile8_id >= max_tile_count) {
39 return -1;
40 }
41 return tile8_id;
42}
43
44inline float ComputeTile8SourceDisplayScale(float available_width_px,
45 int source_bitmap_width_px,
46 float min_scale = 1.0f,
47 float max_scale = 4.0f) {
48 if (source_bitmap_width_px <= 0 || available_width_px <= 0.0f ||
49 min_scale <= 0.0f || max_scale < min_scale) {
50 return min_scale;
51 }
52
53 // Leave room for child-window padding and the vertical scrollbar. The Tile8
54 // sheet should normally fit horizontally; height is handled by scrolling.
55 constexpr float kHorizontalChromeAllowance = 24.0f;
56 const float usable_width =
57 std::max(1.0f, available_width_px - kHorizontalChromeAllowance);
58 const float fit_scale =
59 usable_width / static_cast<float>(source_bitmap_width_px);
60 if (fit_scale < min_scale) {
61 // Fit wins over nominal readability. The Tile8 source is still vertically
62 // scrollable, but it should not force a horizontal clip/scroll at default
63 // or narrow dock widths.
64 constexpr float kAbsoluteMinimumReadableScale = 0.35f;
65 return std::clamp(fit_scale, kAbsoluteMinimumReadableScale, max_scale);
66 }
67 return std::clamp(fit_scale, min_scale, max_scale);
68}
69
70inline float ComputeTile8SourcePanelHeight(float available_height_px,
71 int source_bitmap_height_px,
72 float display_scale,
73 float preferred_height_px = 0.0f,
74 float min_height_px = 220.0f,
75 float max_height_px = 520.0f) {
76 if (min_height_px <= 0.0f) {
77 min_height_px = 1.0f;
78 }
79 max_height_px = std::max(min_height_px, max_height_px);
80
81 constexpr float kVerticalChromeAllowance = 18.0f;
82 const float sheet_height =
83 source_bitmap_height_px > 0 && display_scale > 0.0f
84 ? (static_cast<float>(source_bitmap_height_px) * display_scale) +
85 kVerticalChromeAllowance
86 : max_height_px;
87
88 float target_height =
89 preferred_height_px > 0.0f ? preferred_height_px : sheet_height;
90 target_height = std::clamp(target_height, min_height_px, max_height_px);
91
92 if (available_height_px <= 0.0f) {
93 return target_height;
94 }
95
96 const float clamped_max_height =
97 std::min(max_height_px, std::max(1.0f, available_height_px));
98 const float clamped_min_height = std::min(min_height_px, clamped_max_height);
99 return std::clamp(target_height, clamped_min_height, clamped_max_height);
100}
101
102} // namespace editor
103} // namespace yaze
104
105#endif // YAZE_APP_EDITOR_OVERWORLD_TILE8_SOURCE_INTERACTION_H
float ComputeTile8SourcePanelHeight(float available_height_px, int source_bitmap_height_px, float display_scale, float preferred_height_px=0.0f, float min_height_px=220.0f, float max_height_px=520.0f)
bool ComputeTile8UsageHighlight(bool source_hovered, bool right_mouse_down)
int ComputeTile8IndexFromCanvasMouse(float mouse_x, float mouse_y, int source_bitmap_width_px, int max_tile_count, float display_scale)
float ComputeTile8SourceDisplayScale(float available_width_px, int source_bitmap_width_px, float min_scale=1.0f, float max_scale=4.0f)