1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 module desgui.core.image;
26 
27 import desgui.core.widget;
28 import desil;
29 
30 class DiImageException : DiException
31 {
32     @safe pure nothrow this( string msg, string file=__FILE__, size_t line=__LINE__ )
33     { super( msg, file, line ); }
34 }
35 
36 class DiImage : DiWidget
37 {
38 protected:
39     DiDrawRect plane;
40     AspectRatio asprat;
41     imsize_t tc;
42     LineAlign px = LineAlign.CENTER, py = LineAlign.CENTER;
43 public:
44 
45     enum AspectRatio { IGNORE, FIT, EXPAND }
46     enum LineAlign { START, END, CENTER }
47 
48     this( DiWidget par, in irect r )
49     {
50         super( par );
51 
52         plane = ctx.draw.rect();
53         plane.useTexture = plane.UseTexture.FULL;
54 
55         reshape.connect( (r) 
56         {
57             ivec2 t_size, t_pos;
58 
59             float imratio = tc.h / cast(float)(tc.w);
60             float wiratio = rect.h / cast(float)(rect.w);
61 
62             final switch( asprat )
63             {
64                 case AspectRatio.IGNORE:
65                     t_size = rect.size;
66                     break;
67                 case AspectRatio.FIT:
68                     if( wiratio > imratio )
69                         t_size = ivec2( rect.w, rect.w * imratio );
70                     else
71                         t_size = ivec2( rect.h / imratio, rect.h );
72                     break;
73                 case AspectRatio.EXPAND:
74                     if( wiratio < imratio )
75                         t_size = ivec2( rect.w, rect.w * imratio );
76                     else
77                         t_size = ivec2( rect.h / imratio, rect.h );
78                     break;
79             }
80 
81             final switch( px )
82             {
83                 case LineAlign.START: t_pos.x = 0; break;
84                 case LineAlign.CENTER: t_pos.x = ( rect.w - t_size.x ) / 2; break;
85                 case LineAlign.END: t_pos.x = rect.w - t_size.x; break;
86             }
87 
88             final switch( py )
89             {
90                 case LineAlign.START: t_pos.y = 0; break;
91                 case LineAlign.CENTER: t_pos.y = ( rect.h - t_size.y ) / 2; break;
92                 case LineAlign.END: t_pos.y = rect.h - t_size.y; break;
93             }
94 
95             plane.reshape( irect( t_pos, t_size ) );
96         });
97 
98         draw.connect({ plane.draw(); });
99 
100         reshape( r );
101     }
102 
103     this( DiWidget par, in irect r, in Image im )
104     {
105         this( par, r );
106         image( im );
107     }
108 
109     this( DiWidget par, in irect r, in ImageReadAccess ira )
110     {
111         this( par, r );
112         image( ira );
113     }
114 
115     void image( in Image im ) 
116     {
117         plane.image( im ); 
118         tc = im.size;
119         reshape( rect );
120     }
121 
122     void image( in ImageReadAccess ira ) 
123     {
124         plane.image( ira ); 
125         tc = ira.size;
126         reshape( rect );
127     }
128 
129     @property
130     {
131         AspectRatio aspectRatio() const { return asprat; }
132         void aspectRatio( AspectRatio val ){ asprat = val; reshape(rect); }
133 
134         LineAlign alignX() const { return px; }
135         LineAlign alignY() const { return py; }
136         void alignX( LineAlign val ){ px = val; reshape( rect ); }
137         void alignY( LineAlign val ){ py = val; reshape( rect ); }
138     }
139 }