How to Make Custom Textured Buttons Using HUD C++?

Dear Mothership and Community,

My C++ HUD Tutorial is Done!

This is a fully functional plug and
play HUD class of more than 800 lines
of C++ code for you to use as a
template for your UE4 projects, enjoy!

My solution does not use a button class, I use a USTRUCT() instead, because this is more streamlined given the way I setup the rest of my HUD code, and because the amount of data I store in a button is tiny.

If you choose to implement a button class to make the drawing of the title and the background of the button more connected to the actual button boundaries feel free to post your modified version :slight_smile:

video

===================================

===================================

The main menu background actually glows through constant Hue shifts, it is a material with an entertaining node setup :slight_smile:

tooltips

In particular notice how the tool tips appear by the cursor and move with the cursor as cursor moves around within the boundaries of the button :slight_smile:


#Pic

#800+ Lines of C++ Code For You

#Description

In this tutorial I am giving you a fully functional 800+ lines of sample HUD class that features

-multiple convenience functions for

=== Drawing ===

  • drawing textures
  • drawing materials
  • drawing lines
  • drawing rectangles
  • drawing text

The functions use the new UE4 method of using CanvasItems.

=== Centering ==

  • how to draw stuff centered in the screen (used by confirm dialog)

=== Rest of Game Code Integration ===
I have code in the tutorial to show you how to get a reference to your custom PC or Character class from within the HUD.

From your PC reference you can get access to your custom Game Mode and Game State classes (using the Cast<> code I show in tutorial PostInit)

=== Player Keyboard,Mouse,Controller Input ===

  • How to capture player key presses for ANY key,mouse,controller that you want from within the HUD
    in this tutorial code,

ESC: clears the screen lock so camera will move with cursor.

F: Toggle screen lock so only the cursor moves as player moves the mouse.

H: Toggles hiding the HUD

LMB: Clicks on buttons

Y: Confirm yes if confirm dialog open

N: Cancel if confirm dialog is open

=== Text Size ==
How to measure text, accounting for scaling and font size, to size backgrounds,buttons,tooltips appropriately

=== Buttons ===

  • a button system and corresponding USTRUCT

=== Cursor ===

  • how to draw cursor that player can move in-game

  • how to change cursor when hovering over button

=== Tooltips ===

  • button tooltips

== Drawing Images/Textures/Materials ===

  • drawing custom button and menu backgrounds that come from textures/materials

  • how to stop camera from moving as player moves mouse cursor in-game

Code Sample, all 800+ lines at link above

//===============
// Cursor In Buttons
//===============
int32 AJoyHUD::CheckCursorInButton(const TArray& ButtonArray)
{
	for(int32 b = 0; b < ButtonArray.Num(); b++)
	{
		CurCheckButton = &ButtonArray[b];
			
		//check cursor in bounds
		if (CurCheckButton->minX <= MouseLocation.X && MouseLocation.X <= CurCheckButton->maxX &&
			CurCheckButton->minY <= MouseLocation.Y && MouseLocation.Y <= CurCheckButton->maxY )
		{
			
			//Active Button Type
			ActiveButton_Type = CurCheckButton->type; 
			
			//Tool Tip
			ActiveButton_Tip = CurCheckButton->toolTip; 
			
			//Change Cursor
			CursorHoveringInButton = true;
		
			//Mouse Clicked?
			if (ThePC->WasInputKeyJustPressed(EKeys::LeftMouseButton))
			{
				return ActiveButton_Type;
				//~~~~~~~~~~~~~~
				//no need to check rest of buttons
			}
		}
	}
	
	//No Click Occurred This Tick
	return -1;	
}

//Check Confirm
void AJoyHUD::CheckCursorInButtonsConfirm()
{
	//Check Confirm Buttons
	ClickedButtonType = CheckCursorInButton(ButtonsConfirm); //fills global ActiveButton_Type
	
	if(ClickedButtonType == BUTTONTYPE_CONFIRM_YES )
	{
		ThePC->ConsoleCommand("Exit");
		return;
	}
	if(ClickedButtonType == BUTTONTYPE_CONFIRM_NO)
	{
		ConfirmDialogOpen = false;
		ButtonsConfirm.Empty(); //Buttons not needed anymore
		return;
	}
}

//Check Buttons
void AJoyHUD::CheckCursorInButtonsMain()
{
	//Check Confirm Buttons
	ClickedButtonType = CheckCursorInButton(ButtonsMain);

	if(ClickedButtonType == BUTTONTYPE_MAIN_RESTART )
	{
		ThePC->ConsoleCommand("RestartLevel");
		return;
	}
	if(ClickedButtonType == BUTTONTYPE_MAIN_EXIT)
	{
		ConfirmDialogOpen = true;
		return;
	}
}
void AJoyHUD::DrawHUD_CheckCursorInButtons()
{
	if(ConfirmDialogOpen)
	{
		CheckCursorInButtonsConfirm();
		
		//Take Focus Away From All Other buttons
		return; 
		//~~~~
	}
	
	//Main
	CheckCursorInButtonsMain();
}