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
7 #include "IGUISkin.h"
\r
8 #include "IGUIEnvironment.h"
\r
9 #include "IVideoDriver.h"
\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
23 setDebugName("CGUIImage");
\r
29 CGUIImage::~CGUIImage()
\r
37 void CGUIImage::setImage(video::ITexture* image)
\r
39 if (image == Texture)
\r
51 //! Gets the image texture
\r
52 video::ITexture* CGUIImage::getImage() const
\r
57 //! sets the color of the image
\r
58 void CGUIImage::setColor(video::SColor color)
\r
63 //! Gets the color of the image
\r
64 video::SColor CGUIImage::getColor() const
\r
69 //! draws the element and its children
\r
70 void CGUIImage::draw()
\r
75 IGUISkin* skin = Environment->getSkin();
\r
76 video::IVideoDriver* driver = Environment->getVideoDriver();
\r
80 core::rect<s32> sourceRect(SourceRect);
\r
81 if (sourceRect.getWidth() == 0 || sourceRect.getHeight() == 0)
\r
83 sourceRect = core::rect<s32>(core::dimension2di(Texture->getOriginalSize()));
\r
88 const video::SColor Colors[] = {Color,Color,Color,Color};
\r
90 core::rect<s32> clippingRect(AbsoluteClippingRect);
\r
91 checkBounds(clippingRect);
\r
93 driver->draw2DImage(Texture, AbsoluteRect, sourceRect,
\r
94 &clippingRect, Colors, UseAlphaChannel);
\r
98 core::rect<s32> clippingRect(AbsoluteRect.UpperLeftCorner, sourceRect.getSize());
\r
99 checkBounds(clippingRect);
\r
100 clippingRect.clipAgainst(AbsoluteClippingRect);
\r
102 driver->draw2DImage(Texture, AbsoluteRect.UpperLeftCorner, sourceRect,
\r
103 &clippingRect, Color, UseAlphaChannel);
\r
106 else if ( DrawBackground )
\r
108 core::rect<s32> clippingRect(AbsoluteClippingRect);
\r
109 checkBounds(clippingRect);
\r
111 skin->draw2DRectangle(this, skin->getColor(EGDC_3D_DARK_SHADOW), AbsoluteRect, &clippingRect);
\r
114 IGUIElement::draw();
\r
118 //! sets if the image should use its alpha channel to draw itself
\r
119 void CGUIImage::setUseAlphaChannel(bool use)
\r
121 UseAlphaChannel = use;
\r
125 //! sets if the image should use its alpha channel to draw itself
\r
126 void CGUIImage::setScaleImage(bool scale)
\r
128 ScaleImage = scale;
\r
132 //! Returns true if the image is scaled to fit, false if not
\r
133 bool CGUIImage::isImageScaled() const
\r
138 //! Returns true if the image is using the alpha channel, false if not
\r
139 bool CGUIImage::isAlphaChannelUsed() const
\r
141 return UseAlphaChannel;
\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
147 SourceRect = sourceRect;
\r
150 //! Returns the customized source rectangle of the image to be used.
\r
151 core::rect<s32> CGUIImage::getSourceRect() const
\r
156 //! Restrict target drawing-area.
\r
157 void CGUIImage::setDrawBounds(const core::rect<f32>& drawBoundUVs)
\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
170 //! Get target drawing-area restrictions.
\r
171 core::rect<f32> CGUIImage::getDrawBounds() const
\r
177 } // end namespace gui
\r
178 } // end namespace irr
\r