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