File: tilereg.cc

package info (click to toggle)
crawl 2%3A0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,188 kB
  • sloc: cpp: 363,709; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (181 lines) | stat: -rw-r--r-- 2,754 bytes parent folder | download | duplicates (2)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
 * @file
 * @brief Region system implementations
**/

#include "AppHdr.h"

#ifdef USE_TILE_LOCAL

#include "tilereg.h"

#include "glwrapper.h"

Region::Region() :
    ox(0),
    oy(0),
    dx(0),
    dy(0),
    mx(0),
    my(0),
    wx(0),
    wy(0),
    sx(0),
    sy(0),
    ex(0),
    ey(0)
{
}

void Region::resize(int _mx, int _my)
{
    mx = _mx;
    my = _my;

    recalculate();
}

void Region::place(int _sx, int _sy, int margin)
{
    sx = _sx;
    sy = _sy;
    ox = margin;
    oy = margin;

    recalculate();
}

void Region::place(int _sx, int _sy, int _ex, int _ey, int margin)
{
    sx = _sx;
    sy = _sy;
    ex = _ex;
    ey = _ey;
    wx = ex - sx;
    wy = ey - sy;
    ox = margin;
    oy = margin;

    on_resize();
}

void Region::place(int _sx, int _sy)
{
    sx = _sx;
    sy = _sy;

    recalculate();
}

int Region::grid_width_to_pixels(int x) const
{
    return x * dx;
}

int Region::grid_height_to_pixels(int y) const
{
    return y * dy;
}


void Region::calculate_grid_size(int inner_x, int inner_y)
{
    mx = dx ? inner_x / dx : 0;
    my = dy ? inner_y / dy : 0;
}

void Region::resize_to_fit(int _wx, int _wy)
{
    if (_wx < 0 || _wy < 0)
    {
        mx = wx = my = wy = 0;
        ey = sy;
        ex = sy;
        return;
    }

    int inner_x = _wx - 2 * ox;
    int inner_y = _wy - 2 * oy;

    calculate_grid_size(inner_x, inner_y);

    recalculate();

    // update in case any of this changes in recalculate
    // XX this logic is kind of odd, but this seems to work
    calculate_grid_size(inner_x, inner_y);

    ex = sx + _wx;
    ey = sy + _wy;
}

void Region::recalculate()
{
    wx = ox * 2 + grid_width_to_pixels(mx);
    wy = oy * 2 + grid_height_to_pixels(my);
    ex = sx + wx;
    ey = sy + wy;

    on_resize();
}

Region::~Region()
{
}

bool Region::inside(int x, int y)
{
    return x >= sx && y >= sy && x <= ex && y <= ey;
}

bool Region::mouse_pos(int mouse_x, int mouse_y, int &cx, int &cy)
{
    int x = mouse_x - ox - sx;
    int y = mouse_y - oy - sy;

    bool valid = (x >= 0 && y >= 0);

    ASSERT(dx > 0);
    ASSERT(dy > 0);
    x /= dx;
    y /= dy;
    valid &= (x < mx && y < my);

    cx = x;
    cy = y;

    return valid;
}

void Region::set_transform(bool no_scaling)
{
    GLW_3VF trans(sx + ox, sy + oy, 0);
    GLW_3VF scale;

    if (no_scaling)
        scale = GLW_3VF(1, 1, 1);
    else
        scale = GLW_3VF(dx, dy, 1);

    glmanager->set_transform(trans, scale);
}

TileRegion::TileRegion(const TileRegionInit &init)
{
    ASSERT(init.im);
    ASSERT(init.tag_font);

    m_image = init.im;
    dx = init.tile_x;
    dy = init.tile_y;
    m_tag_font = init.tag_font;

    // To quiet Valgrind.
    m_dirty = true;
}

TileRegion::~TileRegion()
{
}

#endif