Button + Slider Class

Posts 115 of 22 · Page 1 of 2
Button + Slider Class
Just a few things I wrote up.

Sorry if they are a little messy.

Button class :

Code:
class cButton{
public:

	int width, height, x, y;
	bool isOn;
	char *buttonText;
	D3DCOLOR textCol, selTextCol, border, fill, selBor, selFill, overFill;
	CD3DFont *buttonFont;

	cButton();
	cButton(int inX, int inY, int w, int h, bool on, char* text, D3DCOLOR borCol, D3DCOLOR fillCol, D3DCOLOR selBorCol, D3DCOLOR selFillCol, D3DCOLOR overFillCol, D3DCOLOR textColor, D3DCOLOR selTextColor){

		x = inX; y = inY; width = w; height = h; isOn = on; buttonText = text; border = borCol; fill = fillCol;
		selFill = selFillCol; selBor = selBorCol; overFill = overFillCol; textCol = textColor; selTextCol = selTextColor;

	};
	void drawButton(LPDIRECT3DDEVICE9 pDevice){

		SIZE fSize;

		this->buttonFont->GetTextExtent(this->buttonText, &fSize);

		if(GetAsyncKeyState(VK_LBUTTON)&1){

			if(isMouseinRegion(this->x, this->y, this->x + this->width, this->y + this->height)){

				this->isOn = !this->isOn;

			}

		}

		if(!this->isOn){
		
			if(isMouseinRegion(this->x, this->y, this->x + this->width, this->y + this->height)){
			
				DrawBox(this->x, this->y, this->width, this->height, this->overFill, this->border, pDevice);

			} else {

				DrawBox(this->x, this->y, this->width, this->height, this->fill, this->border, pDevice);

			}
			this->buttonFont->DrawText((float)((this->width / 2) - (fSize.cx / 2)), (float)((this->height / 2) - (fSize.cy / 2)), this->textCol, this->buttonText);

		} else {

			if(isMouseinRegion(this->x, this->y, this->x + this->width, this->y + this->height)){
			
				DrawBox(this->x, this->y, this->width, this->height, this->overFill, this->selBor, pDevice);

			} else {

				DrawBox(this->x, this->y, this->width, this->height, this->selFill, this->selBor, pDevice);

			}
			this->buttonFont->DrawText((float)((this->width / 2) - (fSize.cx / 2)), (float)((this->height / 2) - (fSize.cy / 2)), this->selTextCol, this->buttonText);

		}
		delete &fSize;

	}
	void setFont(CD3DFont *butFont){

		this->buttonFont = butFont;

	}

};
Slider class :

Code:
class cSlider{
public:

	int x, y, maxVal, minVal, length, barHeight, barWidth, xOffset, barX;
	double startVal, curVal;
	char cMin[20], cMax[20];
	bool isSliding;
	D3DCOLOR barCol, backBorderCol, backCol;
	CD3DFont *sliderFont;

	cSlider();
	cSlider(int xValue, int yValue, int maxValue, int minValue, double startValue, int lengthValue, int barHeightValue, int barWidthValue, D3DCOLOR barColor, D3DCOLOR backBorderColor, D3DCOLOR backColor){

		x = xValue; y = yValue; maxVal = maxValue; minVal = minValue; barX = (startValue / maxValue) * lengthValue + xValue; startVal = startValue;
		length = lengthValue; barHeight = barHeightValue; barWidth = barWidthValue; barCol = barColor; isSliding = false;
		sprintf_s(cMin, 20, "%d", minVal);
		sprintf_s(cMax, 20, "%d", maxVal);

	};
	void drawSlider(LPDIRECT3DDEVICE9 pDevice){

		this->curVal = ((this->barX - this->x) / (double)this->length) * this->maxVal;

		if(GetAsyncKeyState(VK_LBUTTON) < 0){

			POINT cPos;
			GetCursorPos(&cPos);

			if(!this->isSliding){

				if(isMouseinRegion(this->barX, this->y, this->barX + this->barWidth, this->y + this->barHeight + 2)){

					this->xOffset = cPos.x - this->barX;
					this->isSliding = true;

				}

			} else {

				this->barX = cPos.x - this->xOffset;

				if(this->barX > this->x + this->length){
				
					this->barX = this->x + this->length;

				}
				if(this->barX < this->x){

					this->barX = this->x;

				}

			}

		} else {

			this->isSliding = false;

		}

		DrawBox(this->x - 2, this->y, this->length + this->barWidth + 4, this->barHeight + 4, this->backCol, this->backBorderCol, pDevice);
		FillRGB(this->barX, this->y + 2, this->barWidth, this->barHeight, this->barCol, pDevice);
		SIZE tSize;
		this->sliderFont->GetTextExtent(cMin, &tSize);
		this->sliderFont->DrawText((float)(this->x - (tSize.cx / 2)), (float)(this->y + this->barHeight + 5), D3DCOLOR_XRGB(255, 255, 255), cMin);
		this->sliderFont->GetTextExtent(cMax, &tSize);
		this->sliderFont->DrawText((float)(this->x + this->length + this->barWidth - (tSize.cx / 2)), (float)(this->y + this->barHeight + 5), D3DCOLOR_XRGB(255, 255, 255), cMax);
		delete &tSize;

	}
	void setFont(CD3DFont *slideFont){

		sliderFont = slideFont;

	}


};
====Button Class====
Constructor :
(x, y, width, height, starting value(on/off), button text, border color, box color, selected border color, selected box color, box color when mouse is hovering over, normal text color, selected text color)

- DrawButton -
This has one parameter which is the device it just draws the button.

- SetFont -
Do this after initializing your font. It is used to draw the text.( Note : This uses CD3DFont not ID3DXFont )

( Integers )
x, y - Coordinates of the button
height, width - Of the button

( Booleans )
isOn - Duh.

( Chars )
buttonText - Duh.

( Colors )
textCol, selTextCol - Off text / On text
border, fill - Off border / box color
selBor selFill - On border / box color
overFill - Color when the mouse is hovering over the button for the box

====Slider Class====
Constructor :
(x, y, max value, min value, starting value, length of slider when drawn, bar height, bar width, bar color, back border color, back color)

- DrawSlider -
This has one parameter which is the device it just draws the slider.

- SetFont -
Do this after initializing your font. It is used to draw the max/min.( Note : This uses CD3DFont not ID3DXFont )

( Integers )
x, y - The coordinates for the slider
maxVal, minVal - The minimum and maximum value for the slider (ex : 0 - 200)
length - Length of the slider when it is drawn
barHeight, barWidth - Height/width of the bar that you drag
xOffset, barX - The xOffset for dragging and the bar's x value

( Doubles )
startVal, curVal - The starting value and the current value for the slider

( Bools )
isSliding - Is the bar sliding ?

( Chars )
cMin, cMax - For drawing the max/min values below the bar

( Colors )
barCol - The bar's color
backBorderCol, backCol - Border + back color for the slider

Functions you will need :

Code:
bool isMouseinRegion(int x1, int y1, int x2, int y2)
{

	POINT cPos;
	GetCursorPos(&cPos);

	if(cPos.x > x1 && cPos.x < x2 && cPos.y > y1 && cPos.y < y2){

		return true;

	} else {

		return false;

	}

}
void FillRGB( int x, int y, int w, int h, D3DCOLOR color, IDirect3DDevice9* pDevice )
{

	D3DRECT rec = { x, y, x + w, y + h };
	pDevice->Clear( 1, &rec, D3DCLEAR_TARGET, color, 0, 0 );
}
void DrawBorder( int x, int y, int w, int h, int px, D3DCOLOR BorderColor, IDirect3DDevice9* pDevice )
{
	FillRGB( x, (y + h - px), w, px,    BorderColor, pDevice );
	FillRGB( x, y, px, h,                BorderColor, pDevice );
	FillRGB( x, y, w, px,                BorderColor, pDevice );
	FillRGB( (x + w - px), y, px, h,    BorderColor, pDevice );
}
void DrawBox( int x, int y, int w, int h, D3DCOLOR BoxColor, D3DCOLOR BorderColor, IDirect3DDevice9* pDevice )
{
	FillRGB( x, y, w, h,        BoxColor, pDevice );
	DrawBorder( x, y, w, h, 2,    BorderColor, pDevice );
}
How to use them :
Code:
//Global\\
cButton myButton(2, 2, 100, 50, false, "Super Jump", D3DCOLOR_XRGB(0, 255, 0), D3DCOLOR_XRGB(0, 0, 0), D3DCOLOR_XRGB(255, 255, 255), D3DCOLOR_XRGB(0, 0, 0), D3DCOLOR_XRGB(100, 100, 100), D3DCOLOR_XRGB(0, 255, 0), D3DCOLOR_XRGB(0, 255, 0));

//In your hooked D3D function\\

//(After initializing your font)
myButton.setFont( <font here> );

//For drawing
myButton.drawButton(pDevice);

//Wherever you do your hacks\\
if(myButton.isOn){
//It's on
} else {
//It's off
}

//===================================

//Global\\
cSlider mySlider(10, 250, 100, 0, 10, 100, 10, 10, D3DCOLOR_XRGB(192, 192, 192), D3DCOLOR_XRGB(0, 0, 0), D3DCOLOR_XRGB(255, 255, 255));

//In your hooked D3D function\\

//( After initializing your font)
mySlider.setFont( <font here> );

//When drawing
mySlider.drawSlider(pDevice);

//Wherever you do your stuff
double sliderValue = mySlider.curVal;
//Do stuff with it
Credits :
Whoever for box drawing functions
Me for the rest

Don't like ? kthxbai
Bug or something you think I should change ?
Post away.

Thank you very Much sir, testing now
Man, awesome work!! thank you for helping the community.

Keep up the good work!
I edited a few things for testing so there might be a bug.
Nice but most people who isnt leechers wont use this (if they are determined)
Ok I added on the constructors.
You asshole I said no. >.>
Quote Originally Posted by Stephen View Post
You asshole I said no. >.>
DIP


Blood is waiting.
ofc you are... Im still in TV and im leaving soon. so prob tomorrow
Nice job crash and you just helped me alot. i had messed up my slider but its fixed thanks to you (:
Nice work crash ^^
Hmm looks nice my sliders are smaller
Quote Originally Posted by topblast View Post
Hmm looks nice my sliders are smaller
Just like something else of yours
Quote Originally Posted by IcySeal View Post


Just like something else of yours
That made me
nice work thanked
Posts 115 of 22 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?