@@ -14,42 +14,83 @@ namespace cycfi { namespace elements
14
14
// image implementation
15
15
// //////////////////////////////////////////////////////////////////////////
16
16
image::image (fs::path const & path, float scale)
17
- : _pixmap (std::make_shared<artist::image>(path))
17
+ : _image (std::make_shared<artist::image>(path))
18
18
, _scale(scale)
19
19
{
20
- if (!_pixmap ->impl ())
20
+ if (!_image ->impl ())
21
21
throw std::runtime_error{" Error: Invalid image." };
22
22
}
23
23
24
- image::image (image_ptr pixmap_ , float scale)
25
- : _pixmap(pixmap_ )
24
+ image::image (image_ptr image_ , float scale)
25
+ : _image(image_ )
26
26
, _scale(scale)
27
27
{
28
- if (!_pixmap ->impl ())
28
+ if (!_image ->impl ())
29
29
throw std::runtime_error{" Error: Invalid image." };
30
30
}
31
31
32
+ image::image (fs::path const & path, fit_enum)
33
+ : image{path, -1 }
34
+ {
35
+ }
36
+
37
+ image::image (image_ptr image_, fit_enum)
38
+ : image{image_, -1 }
39
+ {
40
+ }
41
+
32
42
point image::size () const
33
43
{
34
- auto s = _pixmap->size ();
35
- return {s.x * _scale, s.y * _scale};
44
+ auto s = _image->size ();
45
+ if (_scale > 0 )
46
+ return {s.x * _scale, s.y * _scale};
47
+ else // fit
48
+ return {-1 , -1 }; // We do not know the actual size
36
49
}
37
50
38
51
rect image::source_rect (context const & ctx) const
39
52
{
40
- return {0 , 0 , ctx.bounds .width () / _scale, ctx.bounds .height () / _scale};
53
+ if (_scale > 0 )
54
+ {
55
+ return {0 , 0 , ctx.bounds .width () / _scale, ctx.bounds .height () / _scale};
56
+ }
57
+ else // fit
58
+ {
59
+ auto s = _image->size ();
60
+ return {0 , 0 , s.x , s.y };
61
+ }
41
62
}
42
63
43
64
view_limits image::limits (basic_context const & /* ctx */ ) const
44
65
{
45
- auto size_ = size ();
46
- return {{size_.x , size_.y }, {size_.x , size_.y }};
66
+ if (_scale > 0 )
67
+ {
68
+ auto size_ = size ();
69
+ return {{size_.x , size_.y }, {size_.x , size_.y }};
70
+ }
71
+ else // fit
72
+ {
73
+ return full_limits;
74
+ }
47
75
}
48
76
49
77
void image::draw (context const & ctx)
50
78
{
51
79
auto src = source_rect (ctx);
52
- ctx.canvas .draw (pixmap (), src, ctx.bounds );
80
+ if (_scale > 0 )
81
+ {
82
+ ctx.canvas .draw (get_image (), src, ctx.bounds );
83
+ }
84
+ else
85
+ {
86
+ float aspect_ratio = src.width () / src.height ();
87
+ auto dest = ctx.bounds ;
88
+ if (auto h = dest.width () / aspect_ratio; h <= ctx.bounds .height ())
89
+ dest.height (dest.width () / aspect_ratio);
90
+ else
91
+ dest.width (dest.height () * aspect_ratio);
92
+ ctx.canvas .draw (get_image (), src, center (dest, ctx.bounds ));
93
+ }
53
94
}
54
95
55
96
basic_sprite::basic_sprite (fs::path const & path, float height, float scale)
@@ -60,13 +101,13 @@ namespace cycfi { namespace elements
60
101
61
102
view_limits basic_sprite::limits (basic_context const & /* ctx */ ) const
62
103
{
63
- auto width = pixmap ().size ().x ;
104
+ auto width = get_image ().size ().x ;
64
105
return {{width * scale (), _height}, {width * scale (), _height}};
65
106
}
66
107
67
108
std::size_t basic_sprite::num_frames () const
68
109
{
69
- return (pixmap ().size ().y * scale ()) / _height;
110
+ return (get_image ().size ().y * scale ()) / _height;
70
111
}
71
112
72
113
void basic_sprite::index (std::size_t index_)
@@ -77,13 +118,13 @@ namespace cycfi { namespace elements
77
118
78
119
point basic_sprite::size () const
79
120
{
80
- return {pixmap ().size ().x * scale (), _height};
121
+ return {get_image ().size ().x * scale (), _height};
81
122
}
82
123
83
124
rect basic_sprite::source_rect (context const & /* ctx */ ) const
84
125
{
85
126
auto sc = scale ();
86
- auto width = pixmap ().size ().x ;
127
+ auto width = get_image ().size ().x ;
87
128
return rect{0 , (_height/sc) * _index, width, (_height/sc) * (_index + 1 )};
88
129
}
89
130
}}
0 commit comments