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
5 #include "CGUIImage.h"
\r
6 #ifdef _IRR_COMPILE_WITH_GUI_
\r
8 #include "IGUISkin.h"
\r
9 #include "IGUIEnvironment.h"
\r
10 #include "IVideoDriver.h"
\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
24 setDebugName("CGUIImage");
\r
30 CGUIImage::~CGUIImage()
\r
38 void CGUIImage::setImage(video::ITexture* image)
\r
40 if (image == Texture)
\r
52 //! Gets the image texture
\r
53 video::ITexture* CGUIImage::getImage() const
\r
58 //! sets the color of the image
\r
59 void CGUIImage::setColor(video::SColor color)
\r
64 //! Gets the color of the image
\r
65 video::SColor CGUIImage::getColor() const
\r
70 //! draws the element and its children
\r
71 void CGUIImage::draw()
\r
76 IGUISkin* skin = Environment->getSkin();
\r
77 video::IVideoDriver* driver = Environment->getVideoDriver();
\r
81 core::rect<s32> sourceRect(SourceRect);
\r
82 if (sourceRect.getWidth() == 0 || sourceRect.getHeight() == 0)
\r
84 sourceRect = core::rect<s32>(core::dimension2di(Texture->getOriginalSize()));
\r
89 const video::SColor Colors[] = {Color,Color,Color,Color};
\r
91 core::rect<s32> clippingRect(AbsoluteClippingRect);
\r
92 checkBounds(clippingRect);
\r
94 driver->draw2DImage(Texture, AbsoluteRect, sourceRect,
\r
95 &clippingRect, Colors, UseAlphaChannel);
\r
99 core::rect<s32> clippingRect(AbsoluteRect.UpperLeftCorner, sourceRect.getSize());
\r
100 checkBounds(clippingRect);
\r
101 clippingRect.clipAgainst(AbsoluteClippingRect);
\r
103 driver->draw2DImage(Texture, AbsoluteRect.UpperLeftCorner, sourceRect,
\r
104 &clippingRect, Color, UseAlphaChannel);
\r
107 else if ( DrawBackground )
\r
109 core::rect<s32> clippingRect(AbsoluteClippingRect);
\r
110 checkBounds(clippingRect);
\r
112 skin->draw2DRectangle(this, skin->getColor(EGDC_3D_DARK_SHADOW), AbsoluteRect, &clippingRect);
\r
115 IGUIElement::draw();
\r
119 //! sets if the image should use its alpha channel to draw itself
\r
120 void CGUIImage::setUseAlphaChannel(bool use)
\r
122 UseAlphaChannel = use;
\r
126 //! sets if the image should use its alpha channel to draw itself
\r
127 void CGUIImage::setScaleImage(bool scale)
\r
129 ScaleImage = scale;
\r
133 //! Returns true if the image is scaled to fit, false if not
\r
134 bool CGUIImage::isImageScaled() const
\r
139 //! Returns true if the image is using the alpha channel, false if not
\r
140 bool CGUIImage::isAlphaChannelUsed() const
\r
142 return UseAlphaChannel;
\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
148 SourceRect = sourceRect;
\r
151 //! Returns the customized source rectangle of the image to be used.
\r
152 core::rect<s32> CGUIImage::getSourceRect() const
\r
157 //! Restrict target drawing-area.
\r
158 void CGUIImage::setDrawBounds(const core::rect<f32>& drawBoundUVs)
\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
171 //! Get target drawing-area restrictions.
\r
172 core::rect<f32> CGUIImage::getDrawBounds() const
\r
178 } // end namespace gui
\r
179 } // end namespace irr
\r
182 #endif // _IRR_COMPILE_WITH_GUI_
\r