VGUI ImageButton: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 1: Line 1:
This class allows you to create buttons that are images. Depending upon what you pass in the constructor, this 'imagebutton' will change its image if your mouse hovers over it, or you click on it, and then change back when your mouse moves away from it, or you finish clicking.
This class allows you to create buttons that are images. Depending upon what you pass in the constructor, this 'imagebutton' will change its image if your mouse hovers over it, or you click on it, and then change back when your mouse moves away from it, or you finish clicking.


<pre>
== vgui_imagebutton.h ==
 
<source lang=cpp>
#ifndef IMAGEBUTTON_H
#define IMAGEBUTTON_H
 
#ifdef _WIN32
#pragma once
#endif
 
#include <vgui_controls/ImagePanel.h>
 
namespace vgui
{
 
class ImageButton : public vgui::ImagePanel
class ImageButton : public vgui::ImagePanel
{
{
DECLARE_CLASS_SIMPLE( ImageButton, vgui::ImagePanel );
public:
ImageButton( Panel *parent, const char *panelName, const char *normalImage, const char *mouseOverImage = NULL, const char *mouseClickImage = NULL, const char *pCmd=NULL ); //: ImagePanel( parent, panelName );
virtual void OnCursorEntered(); // When the mouse hovers over this panel, change images
virtual void OnCursorExited(); // When the mouse leaves this panel, change back
virtual void OnMouseReleased( vgui::MouseCode code );
virtual void OnMousePressed( vgui::MouseCode code );
void SetNormalImage( void );
void SetMouseOverImage( void );
void SetMouseClickImage( void );
private:
private:
DECLARE_CLASS_SIMPLE( ImageButton, vgui::ImagePanel );
 
char command[32]; // The command when it is clicked on
char command[32]; // The command when it is clicked on
vgui::IImage *i_normalImage; // The image when the mouse isn't over it, and its not being clicked
vgui::IImage *i_normalImage; // The image when the mouse isn't over it, and its not being clicked
Line 13: Line 43:
char m_mouseOverImage[32];
char m_mouseOverImage[32];
char m_mouseClickImage[32];
char m_mouseClickImage[32];
Panel* m_pParent;


bool hasCommand; // If this is to act as a button
bool hasCommand; // If this is to act as a button
Line 18: Line 49:
bool hasMouseClickImage; // If this changes images when the mouse is clicking it
bool hasMouseClickImage; // If this changes images when the mouse is clicking it


virtual void SetImage( vgui::IImage *image ) //Private because this really shouldnt be changed
virtual void SetImage( vgui::IImage *image ); //Private because this really shouldnt be changed
{
};
BaseClass::SetImage( image );
 
}
} //namespace vgui
 
#endif //IMAGEBUTTON_H
</source>
 
== vgui_imagebutton.cpp ==


public:
<source lang=cpp>
ImageButton( Panel *parent, const char *panelName, const char *normalImage, const char *mouseOverImage = NULL, const char *mouseClickImage = NULL, const char *pCmd=NULL ) : vgui::ImagePanel( parent, panelName )
{
if ( pCmd != NULL )
{
Q_strcpy( command, pCmd );
hasCommand = true;
}
else
hasCommand = false;


Q_strcpy( m_normalImage, normalImage );
#include "cbase.h"
i_normalImage = vgui::scheme()->GetImage( m_normalImage, false );
#include "vgui_imagebutton.h"
#include "vgui/mousecode.h"


if ( mouseOverImage != NULL )
using namespace vgui;
{
Q_strcpy( m_mouseOverImage, mouseOverImage );
i_mouseOverImage = vgui::scheme()->GetImage( m_mouseOverImage, false );
hasMouseOverImage = true;
}
else
hasMouseOverImage = false;


if ( mouseClickImage != NULL )
ImageButton::ImageButton( Panel *parent, const char *panelName, const char *normalImage, const char *mouseOverImage, const char *mouseClickImage, const char *pCmd ) : ImagePanel( parent, panelName )  
{
{
Q_strcpy( m_mouseClickImage, mouseClickImage );
m_pParent = parent;
i_mouseClickImage = vgui::scheme()->GetImage( m_mouseClickImage, false );
SetParent(parent);
hasMouseClickImage = true;
}
else
hasMouseClickImage = false;


SetNormalImage();
if ( pCmd != NULL )
{
Q_strcpy( command, pCmd );
hasCommand = true;
}
}
else
hasCommand = false;


        // When the mouse hovers over this panel, change images
Q_strcpy( m_normalImage, normalImage );
virtual void OnCursorEntered()
i_normalImage = vgui::scheme()->GetImage( m_normalImage, false );
 
if ( mouseOverImage != NULL )
{
{
if ( hasMouseOverImage )
Q_strcpy( m_mouseOverImage, mouseOverImage );
SetMouseOverImage();
i_mouseOverImage = vgui::scheme()->GetImage( m_mouseOverImage, false );
hasMouseOverImage = true;
}
}
else
hasMouseOverImage = false;


        // When the mouse leaves this panel, change back
if ( mouseClickImage != NULL )
virtual void OnCursorExited()
{
{
if ( hasMouseOverImage )
Q_strcpy( m_mouseClickImage, mouseClickImage );
SetNormalImage();
i_mouseClickImage = vgui::scheme()->GetImage( m_mouseClickImage, false );
hasMouseClickImage = true;
}
}
else
hasMouseClickImage = false;
SetNormalImage();
}
void ImageButton::OnCursorEntered()
{
if ( hasMouseOverImage )
SetMouseOverImage();
}


virtual void OnMouseReleased( vgui::MouseCode code )
void ImageButton::OnCursorExited()
{
{
Panel *parentPanel = GetParent();
if ( hasMouseOverImage )
parentPanel->OnCommand( command );
SetNormalImage();
}


if ( ( code == MOUSE_LEFT ) && hasMouseClickImage )
void ImageButton::OnMouseReleased( vgui::MouseCode code )
SetNormalImage();
{
}
m_pParent->OnCommand( command );


virtual void OnMousePressed( vgui::MouseCode code )
if ( ( code == MOUSE_LEFT ) && hasMouseClickImage )
{
SetNormalImage();
if ( ( code == MOUSE_LEFT ) && hasMouseClickImage )
}
SetMouseClickImage();
}


void SetNormalImage( void )
void ImageButton::OnMousePressed( vgui::MouseCode code )
{
{
SetImage(i_normalImage);
if ( ( code == MOUSE_LEFT ) && hasMouseClickImage )
Repaint();
SetMouseClickImage();
}
}
 
void ImageButton::SetNormalImage( void )
{
SetImage(i_normalImage);
Repaint();
}


void SetMouseOverImage( void )
void ImageButton::SetMouseOverImage( void )
{
{
SetImage(i_mouseOverImage);
SetImage(i_mouseOverImage);
Repaint();
Repaint();
}
}


};
void ImageButton::SetMouseClickImage( void )
</pre>
{
SetImage(i_mouseClickImage);
Repaint();
}


void ImageButton::SetImage( vgui::IImage *image )
{
BaseClass::SetImage( image );
}
</source>


[[Category:Programming]][[Category:Custom VGUI Controls|I]]
[[Category:Programming]][[Category:Custom VGUI Controls|I]]

Revision as of 12:44, 10 June 2009

This class allows you to create buttons that are images. Depending upon what you pass in the constructor, this 'imagebutton' will change its image if your mouse hovers over it, or you click on it, and then change back when your mouse moves away from it, or you finish clicking.

vgui_imagebutton.h

#ifndef IMAGEBUTTON_H
#define IMAGEBUTTON_H

#ifdef _WIN32
#pragma once
#endif

#include <vgui_controls/ImagePanel.h>

namespace vgui
{

class ImageButton : public vgui::ImagePanel
{
	DECLARE_CLASS_SIMPLE( ImageButton, vgui::ImagePanel );

public:
	ImageButton( Panel *parent, const char *panelName, const char *normalImage, const char *mouseOverImage = NULL, const char *mouseClickImage = NULL, const char *pCmd=NULL ); //: ImagePanel( parent, panelName );

	virtual void OnCursorEntered(); // When the mouse hovers over this panel, change images
	virtual void OnCursorExited(); // When the mouse leaves this panel, change back

	virtual void OnMouseReleased( vgui::MouseCode code );

	 virtual void OnMousePressed( vgui::MouseCode code );

	void SetNormalImage( void );
	void SetMouseOverImage( void );
	void SetMouseClickImage( void );

private:

	char command[32]; // The command when it is clicked on
	vgui::IImage *i_normalImage; // The image when the mouse isn't over it, and its not being clicked
	vgui::IImage *i_mouseOverImage; // The image that appears as when the mouse is hovering over it
	vgui::IImage *i_mouseClickImage; // The image that appears while the mouse is clicking
	char m_normalImage[32];
	char m_mouseOverImage[32];
	char m_mouseClickImage[32];
	Panel* m_pParent;

	bool hasCommand; // If this is to act as a button
	bool hasMouseOverImage; // If this changes images when the mouse is hovering over it
	bool hasMouseClickImage; // If this changes images when the mouse is clicking it

	virtual void SetImage( vgui::IImage *image ); //Private because this really shouldnt be changed
};

} //namespace vgui

#endif //IMAGEBUTTON_H

vgui_imagebutton.cpp

#include "cbase.h"
#include "vgui_imagebutton.h"
#include "vgui/mousecode.h"

using namespace vgui;

ImageButton::ImageButton( Panel *parent, const char *panelName, const char *normalImage, const char *mouseOverImage, const char *mouseClickImage, const char *pCmd ) : ImagePanel( parent, panelName ) 
{
	m_pParent = parent;
	SetParent(parent);

	if ( pCmd != NULL )
	{
		Q_strcpy( command, pCmd );
		hasCommand = true;
	}
	else
		hasCommand = false;

	Q_strcpy( m_normalImage, normalImage );
	i_normalImage = vgui::scheme()->GetImage( m_normalImage, false );

	if ( mouseOverImage != NULL )
	{
		Q_strcpy( m_mouseOverImage, mouseOverImage );
		i_mouseOverImage = vgui::scheme()->GetImage( m_mouseOverImage, false );
		hasMouseOverImage = true;
	}
	else
		hasMouseOverImage = false;

	if ( mouseClickImage != NULL )
	{
		Q_strcpy( m_mouseClickImage, mouseClickImage );
		i_mouseClickImage = vgui::scheme()->GetImage( m_mouseClickImage, false );
		hasMouseClickImage = true;
	}
	else
		hasMouseClickImage = false;

	SetNormalImage();
}

void ImageButton::OnCursorEntered()
{
	if ( hasMouseOverImage )
		SetMouseOverImage();
}

void ImageButton::OnCursorExited()
{
	if ( hasMouseOverImage )
		SetNormalImage();
}

void ImageButton::OnMouseReleased( vgui::MouseCode code )
{
	m_pParent->OnCommand( command );

	if ( ( code == MOUSE_LEFT ) && hasMouseClickImage )
		SetNormalImage();
}

void ImageButton::OnMousePressed( vgui::MouseCode code )
{
	if ( ( code == MOUSE_LEFT ) && hasMouseClickImage )
		SetMouseClickImage();
}

void ImageButton::SetNormalImage( void )
{
	SetImage(i_normalImage);
	Repaint();
}

void ImageButton::SetMouseOverImage( void )
{
	SetImage(i_mouseOverImage);
	Repaint();
}

void ImageButton::SetMouseClickImage( void )
{
	SetImage(i_mouseClickImage);
	Repaint();
}

void ImageButton::SetImage( vgui::IImage *image )
{
	BaseClass::SetImage( image );
}