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
|
#include "texturedbox.h"
#include <cassert>
namespace GUI
{
TexturedBox::TexturedBox(ImageCache& image_cache, const std::string& filename,
std::size_t x0, std::size_t y0,
std::size_t dx1, std::size_t dx2, std::size_t dx3,
std::size_t dy1, std::size_t dy2, std::size_t dy3)
: seg_a(image_cache, filename, x0 , y0 , dx1, dy1)
, seg_b(image_cache, filename, x0 + dx1 , y0 , dx2, dy1)
, seg_c(image_cache, filename, x0 + dx1 + dx2, y0 , dx3, dy1)
, seg_d(image_cache, filename, x0 , y0 + dy1 , dx1, dy2)
, seg_e(image_cache, filename, x0 + dx1 , y0 + dy1 , dx2, dy2)
, seg_f(image_cache, filename, x0 + dx1 + dx2, y0 + dy1 , dx3, dy2)
, seg_g(image_cache, filename, x0 , y0 + dy1 + dy2, dx1, dy3)
, seg_h(image_cache, filename, x0 + dx1 , y0 + dy1 + dy2, dx2, dy3)
, seg_i(image_cache, filename, x0 + dx1 + dx2, y0 + dy1 + dy2, dx3, dy3)
, dx1(dx1)
, dx2(dx2)
, dx3(dx3)
, dy1(dy1)
, dy2(dy2)
, dy3(dy3)
, _width(dx1 + dx2 + dx3)
, _height(dy1 + dy2 + dy3)
{
}
std::size_t TexturedBox::width() const
{
return _width;
}
std::size_t TexturedBox::height() const
{
return _height;
}
void TexturedBox::setSize(std::size_t width, std::size_t height)
{
_width = width;
_height = height;
}
const Colour& TexturedBox::getPixel(std::size_t x, std::size_t y) const
{
assert(x < _width);
assert(y < _height);
if(y < dy1)
{
if(x < dx1)
{
return seg_a.getPixel(x, y);
}
else if(x < (_width - dx3))
{
float scale = (float)(x - dx1) / (float)(_width - dx1 - dx3);
assert(seg_b.width() == dx2);
return seg_b.getPixel(scale * dx2, y);
}
else
{
return seg_c.getPixel(x - (_width - dx3), y);
}
}
else if(y < (_height - dy3))
{
if(x < dx1)
{
float scale = (float)(y - dy1) / (float)(_height - dy1 - dy3);
return seg_d.getPixel(x, scale * dy2);
}
else if(x < (_width - dx3))
{
float scale_x = (float)(x - dx1) / (float)(_width - dx1 - dx3);
float scale_y = (float)(y - dy1) / (float)(_height - dy1 - dy3);
return seg_e.getPixel(scale_x * dx2, scale_y * dy2);
}
else
{
float scale = (float)(y - dy1) / (float)(_height - dy1 - dy3);
return seg_f.getPixel(x - (_width - dx3), scale * dy2);
}
}
else
{
if(x < dx1)
{
return seg_g.getPixel(x, y - (_height - dy3));
}
else if(x < (_width - dx3))
{
float scale = (float)(x - dx1) / (float)(_width - dx1 - dx3);
return seg_h.getPixel(scale * dx2, y - (_height - dy3));
}
else
{
return seg_i.getPixel(x - (_width - dx3), y - (_height - dy3));
}
}
return outOfRange;
}
}
|