here I would like to give you my old Statusbar and Imagebutton classes.
This is an extension for the irrlicht-gui.
This is easy to use, but it is not finale version.
It is more a simple test which is working.
First I will post the code here and then attach a zip file to this post with the source code.
IGUIStatusBar.h
Code: Select all
#pragma once
namespace irr
{
namespace gui
{
class IGUIStatusBar : public IGUIElement
{
public:
IGUIStatusBar(IGUIEnvironment* environment, IGUIElement* parent, s32 id, const core::recti& rectangle);
virtual ~IGUIStatusBar();
virtual void addChild(IGUIElement* child);
virtual void setText(u32 index, const wchar_t* text)=0;
virtual void OnUpdateKeys(const irr::SEvent::SKeyInput& keyboard)=0;
private:
virtual void setText(const wchar_t* text);
virtual const wchar_t* getText()const;
};
IGUIStatusBar* createStatusBar(const core::recti& rectangle, IGUIEnvironment* environment, IGUIElement* parent = 0, s32 id = -1, s32 index = 1 );
};
};
Code: Select all
#include "../IrrExtension.h"
namespace irr
{
namespace gui
{
IGUIStatusBar::IGUIStatusBar(IGUIEnvironment* environment, IGUIElement* parent, s32 id, const core::recti& rectangle):
IGUIElement(EGUIET_ELEMENT,environment,0,id,rectangle)
{
#ifdef _DEBUG
setDebugName("IGUIStatusBar");
#endif
// attach to parent
parent = (parent?parent:environment->getRootGUIElement());
// if we were given a parent to attach to
if (parent)
{
parent->addChild(this);
recalculateAbsolutePosition(true);
}
}
IGUIStatusBar::~IGUIStatusBar()
{
}
void IGUIStatusBar::addChild(IGUIElement* child)
{
if( child && child->getType() == EGUIET_STATIC_TEXT)
{
addChildToEnd(child);
child->updateAbsolutePosition();
}
}
void IGUIStatusBar::setText(const wchar_t* text)
{
// not implement
}
const wchar_t* IGUIStatusBar::getText()const
{
return 0;
}
};
};
Code: Select all
#pragma once
namespace irr
{
namespace gui
{
/* This class represent a simple status bar but at time it is only for textlables. */
class CGUIStatusBar : public IGUIStatusBar
{
private:
IGUIStaticText** m_pLblKeys;
IGUIElement* m_pHoveredElement;
IGUIFont* LastFont;
bool HasFocus;
s32 Numbers;
core::stringw m_pLastUserText;
bool Changes;
public:
CGUIStatusBar(IGUIEnvironment* environment, IGUIElement* parent, s32 id, const core::recti& rectangle, s32 numbers = 1);
~CGUIStatusBar();
void updateAbsolutePosition();
void setText(u32 index, const wchar_t* text);
const wchar_t* getText(u32 index)const;
void draw();
bool OnEvent(const SEvent& e);
void OnUpdateKeys(const irr::SEvent::SKeyInput& keyboard);
private:
void recalculateSize();
void recalculateKeyLabel(IGUIStaticText* element, s32 diff, bool rightside);
void updateVisualKeys();
};
};
};
Code: Select all
#include "../IrrExtension.h"
#if defined(WIN32) || defined (_WIN64)
#include <Windows.h>
#endif
namespace irr
{
namespace gui
{
IGUIStatusBar* createStatusBar(const core::recti& rectangle, IGUIEnvironment* environment, IGUIElement* parent, s32 id, s32 index )
{
if( !parent )
{
parent = environment->getRootGUIElement();
}
return new CGUIStatusBar(environment,parent,id,rectangle,index);
}
CGUIStatusBar::CGUIStatusBar(IGUIEnvironment* environment, IGUIElement* parent, s32 id, const core::recti& rectangle, s32 numbers):
IGUIStatusBar(environment,parent,id,rectangle),
LastFont(0),
HasFocus(false),
m_pHoveredElement(0),
Numbers(0),
m_pLastUserText(0)
{
#ifdef _DEBUG
setDebugName("CGUIStatusBar");
#endif
s32 index = 3;
if( numbers >= 0 )
{
index += numbers;
}
m_pLastUserText = irrT("");
m_pLblKeys = new IGUIStaticText*[index];
for( s32 x = 0; x < index;x++)
{
m_pLblKeys[x] = 0;
}
Numbers = index;
recalculateSize();
core::recti rect;
//rect.LowerRightCorner = rect.LowerRightCorner;
s32 width = (RelativeRect.getWidth()-(RelativeRect.getHeight()-2));
rect.UpperLeftCorner.X = (width-45);
rect.UpperLeftCorner.Y = 2;
rect.LowerRightCorner.X = width;
rect.LowerRightCorner.Y = (RelativeRect.getHeight()-2);
m_pLblKeys[2] = environment->addStaticText(irrT("SCRL"), rect, true, true, this);
m_pLblKeys[2]->setTextAlignment(irr::gui::EGUIA_CENTER,irr::gui::EGUIA_CENTER);
rect.LowerRightCorner.X = rect.UpperLeftCorner.X-1;
rect.UpperLeftCorner.X = ( rect.LowerRightCorner.X-40);
m_pLblKeys[1] = environment->addStaticText(irrT("NUM"), rect, true, true, this);
m_pLblKeys[1]->setTextAlignment(irr::gui::EGUIA_CENTER,irr::gui::EGUIA_CENTER);
rect.LowerRightCorner.X = rect.UpperLeftCorner.X-1;
rect.UpperLeftCorner.X = ( rect.LowerRightCorner.X-35);
m_pLblKeys[0] = environment->addStaticText(irrT("CAP"), rect, true, true, this);
m_pLblKeys[0]->setTextAlignment(irr::gui::EGUIA_CENTER,irr::gui::EGUIA_CENTER);
if( Numbers >= 4 )
{
if( Numbers == 4 )
{
rect.LowerRightCorner.X = rect.UpperLeftCorner.X-1;
rect.UpperLeftCorner.X = 2;
}
m_pLblKeys[3] = environment->addStaticText(irrT(""), rect, true, true, this);
}
// set bg color
IGUISkin* skin = Environment->getSkin();
video::SColor bg = skin->getColor(irr::gui::EGDC_TOOLTIP_BACKGROUND);
m_pLblKeys[0]->setBackgroundColor(bg);
m_pLblKeys[1]->setBackgroundColor(bg);
m_pLblKeys[2]->setBackgroundColor(bg);
updateVisualKeys();
}
CGUIStatusBar::~CGUIStatusBar()
{
}
void CGUIStatusBar::updateVisualKeys()
{
#if defined(_WIN32) || defined(_WIN64)
if( GetKeyState(VK_CAPITAL)!=0 )//&& !m_pLblKeys[0]->isDrawBackgroundEnabled())
{
m_pLblKeys[0]->setDrawBackground(true);
}
else //if( m_pLblKeys[0]->isDrawBackgroundEnabled() )
{
m_pLblKeys[0]->setDrawBackground(false);
}
if (GetKeyState(VK_NUMLOCK)!=0 )//&& !m_pLblKeys[1]->isDrawBackgroundEnabled())
{
m_pLblKeys[1]->setDrawBackground(true);
}
else// if( m_pLblKeys[1]->isDrawBackgroundEnabled() )
{
m_pLblKeys[1]->setDrawBackground(false);
}
//irr::KEY_CAPITAL
if(GetKeyState(VK_SCROLL)!=0 )//&& !m_pLblKeys[2]->isDrawBackgroundEnabled())
{
m_pLblKeys[2]->setDrawBackground(true);
}
else// if( m_pLblKeys[2]->isDrawBackgroundEnabled() )
{
m_pLblKeys[2]->setDrawBackground(false);
}
#endif
}
void CGUIStatusBar::setText(u32 index, const wchar_t* text)
{
s32 iIndex = (index+3);
if( iIndex < Numbers )
{
if( m_pLblKeys[iIndex] )
{
m_pLblKeys[iIndex]->setText(text);
if( iIndex == 3 )
{
m_pLastUserText = text;
}
}
}
}
const wchar_t* CGUIStatusBar::getText(u32 index)const
{
s32 iIndex = (index+3);
if( iIndex < Numbers )
{
if( m_pLblKeys[iIndex] )
{
m_pLblKeys[iIndex]->getText();
}
}
return 0;
}
void CGUIStatusBar::recalculateSize()
{
core::recti clientRect; // client rect of parent
if ( Parent && Parent->hasType(irr::gui::EGUIET_WINDOW) )
{
clientRect = static_cast<IGUIWindow*>(Parent)->getClientRect();
}
else if ( Parent )
{
clientRect = core::recti(0,0, Parent->getAbsolutePosition().getWidth(), Parent->getAbsolutePosition().getHeight());
}
else
{
clientRect = RelativeRect;
}
//
IGUISkin* skin = Environment->getSkin();
IGUIFont* font = skin->getFont(irr::gui::EGDF_MENU);
if (!font)
{
if (Parent && skin)
RelativeRect = core::recti(clientRect.UpperLeftCorner.X, clientRect.UpperLeftCorner.Y,
clientRect.LowerRightCorner.X, clientRect.UpperLeftCorner.Y+skin->getSize(irr::gui::EGDS_MENU_HEIGHT));
return;
}
core::recti rect;
rect.UpperLeftCorner.X = clientRect.UpperLeftCorner.X;
//rect.UpperLeftCorner.Y = clientRect.UpperLeftCorner.X;
rect.LowerRightCorner = clientRect.LowerRightCorner;
s32 height = font->getDimension(L"A").Height + 5;
rect.UpperLeftCorner.Y = (rect.LowerRightCorner.Y-height);
// parentAbsolute.getWidth() - LastParentRect.getWidth();
s32 diff = (Parent->getAbsolutePosition().getWidth()-LastParentRect.getWidth());//DesiredRect.LowerRightCorner.X;
setRelativePosition(rect);
//diff = ( irr::core::max_<s32>(diff,DesiredRect.LowerRightCorner.X)-irr::core::min_<s32>(diff,DesiredRect.LowerRightCorner.X));
for( s32 x = 0; x < Numbers; x++ )
{
if( m_pLblKeys[x] )
{
if( m_pLblKeys[x]->getOverrideFont() != font )
m_pLblKeys[x]->setOverrideFont(font);
recalculateKeyLabel(m_pLblKeys[x], diff, ((x>2)?true:false));
}
}
}
void CGUIStatusBar::recalculateKeyLabel(IGUIStaticText* element, s32 diff, bool rightside)
{
if( element )
{
core::recti rect = element->getRelativePosition();
core::recti rectClient = core::recti(rect.UpperLeftCorner.X+diff, rect.UpperLeftCorner.Y, rect.LowerRightCorner.X+diff, rect.LowerRightCorner.Y);
if( rightside )
{
rectClient = core::recti(rect.UpperLeftCorner.X, rect.UpperLeftCorner.Y, rect.LowerRightCorner.X+diff, rect.LowerRightCorner.Y);
}
element->setRelativePosition(rectClient);
}
}
void CGUIStatusBar::updateAbsolutePosition()
{
if (Parent)
{
core::recti clientRect; // client rect of parent
if ( Parent && Parent->hasType(irr::gui::EGUIET_WINDOW) )
{
clientRect = static_cast<IGUIWindow*>(Parent)->getClientRect();
}
else if ( Parent )
{
clientRect = core::recti(0,0, Parent->getAbsolutePosition().getWidth(), Parent->getAbsolutePosition().getHeight());
}
else
{
clientRect = RelativeRect;
}
IGUISkin* skin = Environment->getSkin();
IGUIFont* font = skin->getFont(irr::gui::EGDF_MENU);
if (!font)
{
if (Parent && skin)
RelativeRect = core::recti(clientRect.UpperLeftCorner.X, clientRect.UpperLeftCorner.Y,
clientRect.LowerRightCorner.X, clientRect.UpperLeftCorner.Y+skin->getSize(irr::gui::EGDS_MENU_HEIGHT));
return;
}
s32 height = font->getDimension(L"A").Height + 5;
//s32 diff = DesiredRect.LowerRightCorner.X;
s32 diff = (Parent->getAbsolutePosition().getWidth()-LastParentRect.getWidth());
DesiredRect.LowerRightCorner.X = Parent->getAbsolutePosition().getWidth();
DesiredRect.LowerRightCorner.Y = Parent->getAbsolutePosition().getHeight();
DesiredRect.UpperLeftCorner.Y = (DesiredRect.LowerRightCorner.Y-height);
//diff = ( irr::core::max_<s32>(diff,DesiredRect.LowerRightCorner.X)-irr::core::min_<s32>(diff,DesiredRect.LowerRightCorner.X));
for( s32 x = 0; x < Numbers; x++ )
{
if( m_pLblKeys[x] )
{
if( m_pLblKeys[x]->getOverrideFont() != font )
m_pLblKeys[x]->setOverrideFont(font);
recalculateKeyLabel(m_pLblKeys[x], diff, ((x>2)?true:false));
}
}
}
IGUIElement::updateAbsolutePosition();
}
void CGUIStatusBar::draw()
{
if( IsVisible )
{
// IGUISkin* skin = Environment->getSkin();
// IVideoDriver* driver = Environment->getVideoDriver();
//skin->draw2DRectangle(this, skin->getColor(irr::gui::EGDC_3D_DARK_SHADOW), AbsoluteRect, &AbsoluteClippingRect);
IGUISkin* skin = Environment->getSkin();
IGUIFont* font = skin->getFont(irr::gui::EGDF_MENU);
if (font != LastFont)
{
if (LastFont)
LastFont->drop();
LastFont = font;
if (LastFont)
LastFont->grab();
recalculateSize();
}
if( !HasFocus )
{
updateVisualKeys();
}
core::recti rect = AbsoluteRect;
// draw frame
skin->draw3DToolBar(this, rect, &AbsoluteClippingRect);
if( (Numbers > 3 ) && (m_pHoveredElement != Environment->getHovered()) )
{
IGUIElement* hovered = Environment->getHovered();
if( hovered && hovered != this && hovered->getParent() != this)
{
m_pHoveredElement = hovered;
}
else
{
m_pHoveredElement = 0;
}
Changes = true;
}
if( m_pHoveredElement && Changes)
{
if( (Numbers > 3 ) )
{
if( m_pHoveredElement->getToolTipText().size() > 0 && m_pHoveredElement != Environment->getRootGUIElement())
{
m_pLblKeys[3]->setText(m_pHoveredElement->getToolTipText().c_str());
}
else
{
m_pLblKeys[3]->setText(m_pLastUserText.c_str());
}
}
Changes = false;
}
else if( !m_pHoveredElement && Changes )
{
if( (Numbers > 3 ) )
{
m_pLblKeys[3]->setText(m_pLastUserText.c_str());
}
Changes = false;
}
// draw children
IGUIElement::draw();
}
}
void CGUIStatusBar::OnUpdateKeys(const irr::SEvent::SKeyInput& keyboard)
{
if( !HasFocus && keyboard.PressedDown)
{
switch(keyboard.Key)
{
case(irr::KEY_NUMLOCK):
case(irr::KEY_CAPITAL):
case(irr::KEY_SCROLL):
{
updateVisualKeys();
}
break;
default:
break;
}
}
}
bool CGUIStatusBar::OnEvent(const SEvent& e)
{
if( IsEnabled )
{
if( e.EventType == irr::EET_KEY_INPUT_EVENT && e.KeyInput.PressedDown)
{
switch(e.KeyInput.Key)
{
case(irr::KEY_NUMLOCK):
case(irr::KEY_CAPITAL):
case(irr::KEY_SCROLL):
{
updateVisualKeys();
}
break;
default:
break;
}
}
else if( e.EventType == irr::EET_GUI_EVENT )
{
if( e.GUIEvent.EventType == irr::gui::EGET_ELEMENT_FOCUSED && e.GUIEvent.Caller == this)
{
HasFocus = true;
}
else if( e.GUIEvent.EventType == irr::gui::EGET_ELEMENT_FOCUS_LOST && e.GUIEvent.Caller == this)
{
HasFocus = false;
}
}
//if( HasFocus )
//{
return IGUIElement::OnEvent(e);
//}
}
return false;
}
};
};
Code: Select all
#pragma once
namespace irr
{
namespace gui
{
class CGUIImageButton : public IGUIImage
{
video::ITexture* Texture;
video::SColor Color;
bool UseAlphaChannel;
bool ScaleImage;
bool MouseHover;
bool Focus;
bool Pressed;
public:
CGUIImageButton(IGUIEnvironment* environment, core::recti rectangle = core::recti(0,0,100,100), IGUIElement* parent = 0, s32 id = -1);
virtual ~CGUIImageButton();
//! Sets an image texture
virtual void setImage(video::ITexture* image);
//! Gets the image texture
virtual video::ITexture* getImage()const;
//! Sets the color of the image
virtual void setColor(video::SColor color);
//! Sets if the image should scale to fit the element
virtual void setScaleImage(bool scale);
//! Sets if the image should use its alpha channel to draw itself
virtual void setUseAlphaChannel(bool use);
//! Gets the color of the image
virtual video::SColor getColor()const;
//! Returns true if the image is scaled to fit, false if not
virtual bool isImageScaled()const;
//! Returns true if the image is using the alpha channel, false if not
virtual bool isAlphaChannelUsed()const;
//! draws the element and its children
virtual void draw();
//!
virtual bool OnEvent(const SEvent& e);
};
};
};
Code: Select all
#include "../IrrExtension.h"
namespace irr
{
namespace gui
{
CGUIImageButton::CGUIImageButton(IGUIEnvironment* environment, core::recti rectangle, IGUIElement* parent, s32 id):
IGUIImage(environment, parent, id, rectangle),
Texture(0),
Color(255,255,255,255),
UseAlphaChannel(true),
ScaleImage(false),
MouseHover(false),
Focus(false),
Pressed(false)
{
#ifdef _DEBUG
setDebugName("CGUIImageButton");
#endif
}
CGUIImageButton::~CGUIImageButton()
{
if (Texture)
Texture->drop();
}
//! sets an image
void CGUIImageButton::setImage(video::ITexture* image)
{
if (image == Texture)
return;
if (Texture)
Texture->drop();
Texture = image;
if (Texture)
Texture->grab();
}
//! Gets the image texture
video::ITexture* CGUIImageButton::getImage() const
{
return Texture;
}
//! sets the color of the image
void CGUIImageButton::setColor(video::SColor color)
{
Color = color;
}
//! Gets the color of the image
video::SColor CGUIImageButton::getColor() const
{
return Color;
}
//! draws the element and its children
void CGUIImageButton::draw()
{
if (!IsVisible)
return;
IGUISkin* skin = Environment->getSkin();
video::IVideoDriver* driver = Environment->getVideoDriver();
if (Texture)
{
if (ScaleImage)
{
const video::SColor Colors[] = {Color,Color,Color,Color};
driver->draw2DImage(Texture, AbsoluteRect,
core::recti(core::position2di(0,0), core::dimension2di(Texture->getOriginalSize())),
&AbsoluteClippingRect, Colors, UseAlphaChannel);
}
else
{
driver->draw2DImage(Texture, AbsoluteRect.UpperLeftCorner,
core::recti(core::position2di(0,0), core::dimension2di(Texture->getOriginalSize())),
&AbsoluteClippingRect, Color, UseAlphaChannel);
}
if( !Focus )
{
core::vector2di coords;// <<< If required: Coordinates from mouse cursor-position must be set for this block.
if( getAbsolutePosition().isPointInside(coords))
{
MouseHover = true;
}
else
{
MouseHover = false;
}
}
// hover
if( MouseHover )
{
video::SColor color = skin->getColor(irr::gui::EGDC_3D_HIGH_LIGHT);
color.setAlpha(55);
skin->draw2DRectangle(this, color, AbsoluteRect, &AbsoluteClippingRect);
driver->draw2DRectangleOutline(AbsoluteRect, skin->getColor(irr::gui::EGDC_3D_LIGHT));
}
else
{
driver->draw2DRectangleOutline(AbsoluteRect, skin->getColor(irr::gui::EGDC_3D_DARK_SHADOW));
}
}
else
{
skin->draw2DRectangle(this, skin->getColor(irr::gui::EGDC_3D_DARK_SHADOW), AbsoluteRect, &AbsoluteClippingRect);
driver->draw2DRectangleOutline(AbsoluteRect, skin->getColor(irr::gui::EGDC_TOOLTIP_BACKGROUND));
}
IGUIElement::draw();
}
//! sets if the image should use its alpha channel to draw itself
void CGUIImageButton::setUseAlphaChannel(bool use)
{
UseAlphaChannel = use;
}
//! sets if the image should use its alpha channel to draw itself
void CGUIImageButton::setScaleImage(bool scale)
{
ScaleImage = scale;
}
//! Returns true if the image is scaled to fit, false if not
bool CGUIImageButton::isImageScaled() const
{
return ScaleImage;
}
//! Returns true if the image is using the alpha channel, false if not
bool CGUIImageButton::isAlphaChannelUsed() const
{
return UseAlphaChannel;
}
bool CGUIImageButton::OnEvent(const SEvent& e)
{
bool bResult = false;
if( isEnabled() )
{
if( e.EventType == irr::EET_MOUSE_INPUT_EVENT )
{
if( e.MouseInput.Event == irr::EMIE_MOUSE_MOVED )
{
s32 x = e.MouseInput.X;
s32 y = e.MouseInput.Y;
if( getAbsolutePosition().isPointInside(core::vector2di(x,y)))
{
MouseHover = true;
}
else
{
MouseHover = false;
}
}
else if( Focus && e.MouseInput.Event == irr::EMIE_LMOUSE_PRESSED_DOWN )
{
if (Environment->hasFocus(this) && !AbsoluteClippingRect.isPointInside(core::position2di(e.MouseInput.X, e.MouseInput.Y)))
{
Environment->removeFocus(this);
return false;
}
Pressed = true;
Environment->setFocus(this);
return true;
}
else if( Focus && e.MouseInput.Event == irr::EMIE_LMOUSE_LEFT_UP )
{
bool wasPressed = Pressed;
if ( !AbsoluteClippingRect.isPointInside( core::position2di(e.MouseInput.X, e.MouseInput.Y ) ) )
{
Pressed = false;
return true;
}
Pressed = false;
if (Parent && wasPressed)
{
SEvent newEvent;
newEvent.EventType = irr::EET_GUI_EVENT;
newEvent.GUIEvent.Caller = this;
newEvent.GUIEvent.Element = 0;
newEvent.GUIEvent.EventType = irr::gui::EGET_BUTTON_CLICKED;
Parent->OnEvent(newEvent);
}
return true;
}
}
else if( e.EventType == irr::EET_GUI_EVENT )
{
if( e.GUIEvent.EventType == irr::gui::EGET_ELEMENT_FOCUSED && e.GUIEvent.Caller == this)
{
Focus = true;
//MouseHover = false;
}
else if( e.GUIEvent.EventType == irr::gui::EGET_ELEMENT_FOCUS_LOST && e.GUIEvent.Caller == this)
{
Focus = false;
//MouseHover = false;
}
}
}
return ((Focus)?IGUIElement::OnEvent(e):false);
}
};
};
You can find the File under the category "Irrlicht3D Projekte" in the table with the name "StatusBar&ImageButton (src)".
to the source code [extern homepage]
Have fun with it.