VGUI ImageButton

From Valve Developer Community
(Redirected from Vgui ImageButton)
Jump to navigation Jump to search
Wikipedia - Letter.png
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
Dead End - Icon.png
This article has no Wikipedia icon links to other VDC articles. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

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 );

	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 );
}