]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/CGUIImage.cpp
2d2a08cbf5c4eb1d6cf98d7ddae5c0c31da1a70f
[irrlicht.git] / source / Irrlicht / CGUIImage.cpp
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #include "CGUIImage.h"\r
6 #ifdef _IRR_COMPILE_WITH_GUI_\r
7 \r
8 #include "IGUISkin.h"\r
9 #include "IGUIEnvironment.h"\r
10 #include "IVideoDriver.h"\r
11 \r
12 namespace irr\r
13 {\r
14 namespace gui\r
15 {\r
16 \r
17 \r
18 //! constructor\r
19 CGUIImage::CGUIImage(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)\r
20 : IGUIImage(environment, parent, id, rectangle), Texture(0), Color(255,255,255,255),\r
21         UseAlphaChannel(false), ScaleImage(false), DrawBounds(0.f, 0.f, 1.f, 1.f), DrawBackground(true)\r
22 {\r
23         #ifdef _DEBUG\r
24         setDebugName("CGUIImage");\r
25         #endif\r
26 }\r
27 \r
28 \r
29 //! destructor\r
30 CGUIImage::~CGUIImage()\r
31 {\r
32         if (Texture)\r
33                 Texture->drop();\r
34 }\r
35 \r
36 \r
37 //! sets an image\r
38 void CGUIImage::setImage(video::ITexture* image)\r
39 {\r
40         if (image == Texture)\r
41                 return;\r
42 \r
43         if (Texture)\r
44                 Texture->drop();\r
45 \r
46         Texture = image;\r
47 \r
48         if (Texture)\r
49                 Texture->grab();\r
50 }\r
51 \r
52 //! Gets the image texture\r
53 video::ITexture* CGUIImage::getImage() const\r
54 {\r
55         return Texture;\r
56 }\r
57 \r
58 //! sets the color of the image\r
59 void CGUIImage::setColor(video::SColor color)\r
60 {\r
61         Color = color;\r
62 }\r
63 \r
64 //! Gets the color of the image\r
65 video::SColor CGUIImage::getColor() const\r
66 {\r
67         return Color;\r
68 }\r
69 \r
70 //! draws the element and its children\r
71 void CGUIImage::draw()\r
72 {\r
73         if (!IsVisible)\r
74                 return;\r
75 \r
76         IGUISkin* skin = Environment->getSkin();\r
77         video::IVideoDriver* driver = Environment->getVideoDriver();\r
78 \r
79         if (Texture)\r
80         {\r
81                 core::rect<s32> sourceRect(SourceRect);\r
82                 if (sourceRect.getWidth() == 0 || sourceRect.getHeight() == 0)\r
83                 {\r
84                         sourceRect = core::rect<s32>(core::dimension2di(Texture->getOriginalSize()));\r
85                 }\r
86 \r
87                 if (ScaleImage)\r
88                 {\r
89                         const video::SColor Colors[] = {Color,Color,Color,Color};\r
90 \r
91                         core::rect<s32> clippingRect(AbsoluteClippingRect);\r
92                         checkBounds(clippingRect);\r
93 \r
94                         driver->draw2DImage(Texture, AbsoluteRect, sourceRect,\r
95                                 &clippingRect, Colors, UseAlphaChannel);\r
96                 }\r
97                 else\r
98                 {\r
99                         core::rect<s32> clippingRect(AbsoluteRect.UpperLeftCorner, sourceRect.getSize());\r
100                         checkBounds(clippingRect);\r
101                         clippingRect.clipAgainst(AbsoluteClippingRect);\r
102 \r
103                         driver->draw2DImage(Texture, AbsoluteRect.UpperLeftCorner, sourceRect,\r
104                                 &clippingRect, Color, UseAlphaChannel);\r
105                 }\r
106         }\r
107         else if ( DrawBackground )\r
108         {\r
109                 core::rect<s32> clippingRect(AbsoluteClippingRect);\r
110                 checkBounds(clippingRect);\r
111 \r
112                 skin->draw2DRectangle(this, skin->getColor(EGDC_3D_DARK_SHADOW), AbsoluteRect, &clippingRect);\r
113         }\r
114 \r
115         IGUIElement::draw();\r
116 }\r
117 \r
118 \r
119 //! sets if the image should use its alpha channel to draw itself\r
120 void CGUIImage::setUseAlphaChannel(bool use)\r
121 {\r
122         UseAlphaChannel = use;\r
123 }\r
124 \r
125 \r
126 //! sets if the image should use its alpha channel to draw itself\r
127 void CGUIImage::setScaleImage(bool scale)\r
128 {\r
129         ScaleImage = scale;\r
130 }\r
131 \r
132 \r
133 //! Returns true if the image is scaled to fit, false if not\r
134 bool CGUIImage::isImageScaled() const\r
135 {\r
136         return ScaleImage;\r
137 }\r
138 \r
139 //! Returns true if the image is using the alpha channel, false if not\r
140 bool CGUIImage::isAlphaChannelUsed() const\r
141 {\r
142         return UseAlphaChannel;\r
143 }\r
144 \r
145 //! Sets the source rectangle of the image. By default the full image is used.\r
146 void CGUIImage::setSourceRect(const core::rect<s32>& sourceRect)\r
147 {\r
148         SourceRect = sourceRect;\r
149 }\r
150 \r
151 //! Returns the customized source rectangle of the image to be used.\r
152 core::rect<s32> CGUIImage::getSourceRect() const\r
153 {\r
154         return SourceRect;\r
155 }\r
156 \r
157 //! Restrict target drawing-area.\r
158 void CGUIImage::setDrawBounds(const core::rect<f32>& drawBoundUVs)\r
159 {\r
160         DrawBounds = drawBoundUVs;\r
161         DrawBounds.UpperLeftCorner.X = core::clamp(DrawBounds.UpperLeftCorner.X, 0.f, 1.f);\r
162         DrawBounds.UpperLeftCorner.Y = core::clamp(DrawBounds.UpperLeftCorner.Y, 0.f, 1.f);\r
163         DrawBounds.LowerRightCorner.X = core::clamp(DrawBounds.LowerRightCorner.X, 0.f, 1.f);\r
164         DrawBounds.LowerRightCorner.X = core::clamp(DrawBounds.LowerRightCorner.X, 0.f, 1.f);\r
165         if ( DrawBounds.UpperLeftCorner.X > DrawBounds.LowerRightCorner.X )\r
166                 DrawBounds.UpperLeftCorner.X = DrawBounds.LowerRightCorner.X;\r
167         if ( DrawBounds.UpperLeftCorner.Y > DrawBounds.LowerRightCorner.Y )\r
168                 DrawBounds.UpperLeftCorner.Y = DrawBounds.LowerRightCorner.Y;\r
169 }\r
170 \r
171 //! Get target drawing-area restrictions.\r
172 core::rect<f32> CGUIImage::getDrawBounds() const\r
173 {\r
174         return DrawBounds;\r
175 }\r
176 \r
177 \r
178 } // end namespace gui\r
179 } // end namespace irr\r
180 \r
181 \r
182 #endif // _IRR_COMPILE_WITH_GUI_\r
183 \r