Help With Button Text Centering

Feel free to showcase your own projects!
Post Reply
User avatar
rockstar24
Posts: 13
Joined: 03 Sep 2012, 18:29

Post » 06 Sep 2012, 05:03

Hay Anyone that programs in Lua and Love2D i made a box appear and what should happen is that if you go over it, it gets the mouse position then highlights the box. but when i go over it, it only works on have the box please respond if you know how to fix it

Heres The DownLoad For It "You Will Need To Update Main.lua"
http://www.mediafire.com/?gq6rnsdp19pt5li

Heres The Code If You Dont Want To Download
It All Runs In main.lua No Other Files Needed "Beside Fonts"

Code: Select all

function love.load()
	love.graphics.setCaption			("Hello World")
	Icon = love.graphics.newImage		("Icon.png")
	love.graphics.setIcon				(Icon)
	love.graphics.setBackgroundColor	(237,145,0)
	
	Error = love.graphics.newImage			( "ErrorScreen.png" )
	DropArrow = love.graphics.newImage		( "DropDown.png" )
	Font = love.graphics.newImageFont		( "Font.png", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 " )
	FontTwo = love.graphics.newImageFont	( "FontTwo.png", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789" )
	love.graphics.setFont					( Font )
	
end

function HighLight(Title,RecPosX,RecPosY,RecWidth,RecHeight)
	--All the local variable get initialize in this area
	local Title 			= Title
	local TitleLength		= string.len(Title)
	local TitleDivided		= TitleLength/2
	local RecPosX 			= RecPosX
	local RecWidth 			= RecWidth
	local RecPosY 			= RecPosY
	local RecHeight 		= RecHeight
	local MousePosX			= love.mouse.getX()
	local MousePosY			= love.mouse.getY()
	
		--This is for detecting if the mouse is inside the rectangle. Then hilights the box	
	if MousePosY > RecPosY and MousePosY < RecPosY+RecHeight and MousePosX > RecPosX and MousePosX < RecPosX+RecWidth then
		love.graphics.setColor	(50,50,50)
		love.graphics.rectangle	("fill",RecPosX+1,RecPosY+1,RecWidth-2,RecHeight-2)
	else end
	
	love.graphics.setColor		(0,0,0)
	love.graphics.rectangle		("line",0,0,180,40)
	love.graphics.setColor		(150,100,50,220)
	love.graphics.print			("Mouse Pos X "..MousePosX,0,0,0,2,2)
	love.graphics.print			("Mouse Pos Y "..MousePosY,0,20,0,2,2)
	love.graphics.setColor		(0,0,0)
	love.graphics.rectangle		("fill",RecPosX,RecPosY,RecWidth,RecHeight)
	
	--This is for detecting if the mouse is inside the rectangle. Then hilights the box	
	if MousePosY > RecPosY and MousePosY < RecPosY+RecHeight and MousePosX > RecPosX and MousePosX < RecPosX+RecWidth then
		love.graphics.setColor	(50,50,50)
		love.graphics.rectangle	("fill",RecPosX+1,RecPosY+1,RecWidth-2,RecHeight-2)
	else end
	
	--The title displayed in the rectangle
	love.graphics.setColor				(255,255,255)	
	love.graphics.print					(Title,RecPosX+RecWidth-TitleDivided,RecPosY+RecHeight/2,0,RecWidth/50,RecHeight/50)
  --love.graphics.draw					(DropArrow,RecPosX+RecWidth,RecPosY,0,RecWidth/11,RecHeight/11)
	
end

function love.draw()
	--Calling the HighLight
	HighLight("Hello",180,0,400,40)	
	HighLight("Play",300,100,400,400)	
end
Last edited by rockstar24 on 07 Sep 2012, 06:02, edited 2 times in total.

Maurice
Stabyourself.net
Posts: 2145
Joined: 01 Feb 2012, 20:19

Post » 06 Sep 2012, 08:38

My first guess is this line:
if MousePosY > RecPosY and MousePosY < RecHeight and MousePosX > RecPosX and MousePosX < RecWidth then
It should be
if MousePosY > RecPosY and MousePosY < RecPosY+RecHeight and MousePosX > RecPosX and MousePosX < RecPosX+RecWidth then

User avatar
rockstar24
Posts: 13
Joined: 03 Sep 2012, 18:29

Post » 06 Sep 2012, 15:26

Thanks Maurice it works better now i see what i did wrong with it

User avatar
rockstar24
Posts: 13
Joined: 03 Sep 2012, 18:29

Post » 07 Sep 2012, 05:30

Maurice wrote:My first guess is this line:
if MousePosY > RecPosY and MousePosY < RecHeight and MousePosX > RecPosX and MousePosX < RecWidth then
It should be
if MousePosY > RecPosY and MousePosY < RecPosY+RecHeight and MousePosX > RecPosX and MousePosX < RecPosX+RecWidth then
Thanks For Helping me with the high lighting but if you get a chance how would you make the function argument "Title" centered in the rectangle 'i updated the code'

User avatar
Qcode
Posts: 1479
Joined: 05 Feb 2012, 18:00
Contact:

Post » 07 Sep 2012, 15:29

...
Last edited by Qcode on 21 Oct 2021, 08:37, edited 1 time in total.

User avatar
rockstar24
Posts: 13
Joined: 03 Sep 2012, 18:29

Post » 08 Sep 2012, 01:17

So When i run it the text is not centered do i have to get something with the scaling
Picture Here ------>http://www.flickr.com/photos/86828948@N ... hotostream


Edit* Never mind about this i figured out the font i was using made it not able to get centered.

Post Reply