aboutsummaryrefslogtreecommitdiff
path: root/game/addons/zylann.hterrain/native/src/int_range_2d.h
blob: 8072bead8c2cb014b67a06d7ca0adb93d094e5c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef INT_RANGE_2D_H
#define INT_RANGE_2D_H

#include "math_funcs.h"
#include "vector2i.h"
#include <core/Rect2.hpp>

struct IntRange2D {
    int min_x;
    int min_y;
    int max_x;
    int max_y;

    static inline IntRange2D from_min_max(godot::Vector2 min_pos, godot::Vector2 max_pos) {
        return IntRange2D(godot::Rect2(min_pos, max_pos));
    }

    static inline IntRange2D from_pos_size(godot::Vector2 min_pos, godot::Vector2 size) {
        return IntRange2D(godot::Rect2(min_pos, size));
    }

    IntRange2D(godot::Rect2 rect) {
        min_x = static_cast<int>(rect.position.x);
        min_y = static_cast<int>(rect.position.y);
        max_x = static_cast<int>(rect.position.x + rect.size.x);
        max_y = static_cast<int>(rect.position.y + rect.size.y);
    }

    inline bool is_inside(Vector2i size) const {
        return min_x >= size.x &&
               min_y >= size.y &&
               max_x <= size.x &&
               max_y <= size.y;
    }

    inline void clip(Vector2i size) {
        min_x = Math::clamp(min_x, 0, size.x);
        min_y = Math::clamp(min_y, 0, size.y);
        max_x = Math::clamp(max_x, 0, size.x);
        max_y = Math::clamp(max_y, 0, size.y);
    }

    inline void pad(int p) {
        min_x -= p;
        min_y -= p;
        max_x += p;
        max_y += p;
    }

    inline int get_width() const {
        return max_x - min_x;
    }

    inline int get_height() const {
        return max_y - min_y;
    }
};

#endif // INT_RANGE_2D_H