VGUI ImageButton: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
m (clean up, added orphan, deadend tags)
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<pre>
{{Multiple issues|
{{Dead end|date=January 2024}}
{{Orphan|date=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 ==
 
<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 );
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];
char command[32]; // The command when it is clicked on
vgui::IImage *i_normalImage;
vgui::IImage *i_normalImage; // The image when the mouse isn't over it, and its not being clicked
vgui::IImage *i_mouseOverImage;
vgui::IImage *i_mouseOverImage; // The image that appears as when the mouse is hovering over it
vgui::IImage *i_mouseClickImage;
vgui::IImage *i_mouseClickImage; // The image that appears while the mouse is clicking
char m_normalImage[32];
char m_normalImage[32];
char m_mouseOverImage[32];
char m_mouseOverImage[32];
char m_mouseClickImage[32];
char m_mouseClickImage[32];
Panel* m_pParent;


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


virtual void SetImage( vgui::IImage *image )
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;
Q_strcpy( m_normalImage, normalImage );
i_normalImage = vgui::scheme()->GetImage( m_normalImage, false );


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


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


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


if ( ( code == MOUSE_LEFT ) && hasMouseClickImage )
void ImageButton::OnCursorExited()
SetNormalImage();
{
}
if ( hasMouseOverImage )
SetNormalImage();
}


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


void SetNormalImage( void )
if ( ( code == MOUSE_LEFT ) && hasMouseClickImage )
{
SetNormalImage();
SetImage(i_normalImage);
}
Repaint();
}


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


};
void ImageButton::SetNormalImage( void )
</pre>
{
SetImage(i_normalImage);
Repaint();
}


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


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.
void ImageButton::SetMouseClickImage( void )
{
SetImage(i_mouseClickImage);
Repaint();
}


This code written by brian4.
void ImageButton::SetImage( vgui::IImage *image )
{
BaseClass::SetImage( image );
}
</source>


TODO: Make this more wiki compatable as well as put part of it in a cpp file, the rest in a header file.
[[Category:Programming]]
[[Category:Custom VGUI Controls|I]]

Latest revision as of 10:18, 21 January 2024

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