A Windows Classic style button library.

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

A Windows Classic style button library.

Post by Lua Hal »

Just a little project I did.

The code for the window shown in the video:


Code: Select all

function love.load()
	love.filesystem.load("hal.lua")()
	love.graphics.setBackgroundColor(255,192,192)
	h=hal.button.new("5",0,0,100,18)
	h:addText("Toggle"):align(2,2)
	h.pressed:connect(bpressed)
	hal.button.new("6",200,200,30,40)
	hal.button.new("7",0,19,100,18):addText("Hide"):align(2,2).pressed:connect(cpressed)
	hal.button.new("8",0,38,100,18):addText("Show"):align(2,2).pressed:connect(dpressed)
end

function bpressed()
	hal.button.get("6"):toggle()
end
function cpressed()
	hal.button.get("6"):hide()
end
function dpressed()
	hal.button.get("6"):show()
end
function love.mousepressed(x,y,button)
	hal.buttons.clicked(x,y,button)
end

function love.draw()
	hal.buttons.refresh()
end
Tell me what you think!
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: A Windows Classic style button library.

Post by coffee »

The video shows things working well but we can't comment much further because you forgot to post "hal.lua" code so we can judge the quality of your library code. As functionality is ok. The windows look also seems good. I'm only not so impressed because the number of libraries doing close or more than that is common.
Wouldn't be better require than love.filesystem.load("hal.lua")()?
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

Re: A Windows Classic style button library.

Post by Lua Hal »

Sorry.
I'm really young, and I'm not too good at programming, so excuse the stupid things like meaningless variable names.

Code: Select all

img=love.graphics.newImage("font.png")
borders=love.graphics.newImage("borders.png")
curs={1,1}
norm={
topleft=love.graphics.newQuad(7,0,3,3,borders:getWidth(),borders:getHeight()),
top=love.graphics.newQuad(10,0,1,3,borders:getWidth(),borders:getHeight()),
topright=love.graphics.newQuad(11,0,3,3,borders:getWidth(),borders:getHeight()),
midleft=love.graphics.newQuad(7,3,3,1,borders:getWidth(),borders:getHeight()),
midright=love.graphics.newQuad(11,3,3,1,borders:getWidth(),borders:getHeight()),
bottomleft=love.graphics.newQuad(7,4,3,3,borders:getWidth(),borders:getHeight()),
bottom=love.graphics.newQuad(10,4,1,3,borders:getWidth(),borders:getHeight()),
bottomright=love.graphics.newQuad(11,4,3,3,borders:getWidth(),borders:getHeight()),
}
pressed={
topleft=love.graphics.newQuad(14,0,3,3,borders:getWidth(),borders:getHeight()),
top=love.graphics.newQuad(17,0,1,3,borders:getWidth(),borders:getHeight()),
topright=love.graphics.newQuad(18,0,3,3,borders:getWidth(),borders:getHeight()),
midleft=love.graphics.newQuad(14,3,3,1,borders:getWidth(),borders:getHeight()),
midright=love.graphics.newQuad(18,3,3,1,borders:getWidth(),borders:getHeight()),
bottomleft=love.graphics.newQuad(14,4,3,3,borders:getWidth(),borders:getHeight()),
bottom=love.graphics.newQuad(17,4,1,3,borders:getWidth(),borders:getHeight()),
bottomright=love.graphics.newQuad(18,4,3,3,borders:getWidth(),borders:getHeight()),
}
hovered={
topleft=love.graphics.newQuad(0,0,3,3,borders:getWidth(),borders:getHeight()),
top=love.graphics.newQuad(3,0,1,3,borders:getWidth(),borders:getHeight()),
topright=love.graphics.newQuad(4,0,3,3,borders:getWidth(),borders:getHeight()),
midleft=love.graphics.newQuad(0,3,3,1,borders:getWidth(),borders:getHeight()),
midright=love.graphics.newQuad(4,3,3,1,borders:getWidth(),borders:getHeight()),
bottomleft=love.graphics.newQuad(0,4,3,3,borders:getWidth(),borders:getHeight()),
bottom=love.graphics.newQuad(3,4,1,3,borders:getWidth(),borders:getHeight()),
bottomright=love.graphics.newQuad(4,4,3,3,borders:getWidth(),borders:getHeight()),
}
function _print(str,x,y)
	for i=1,string.len(str) do
	char=string.sub(str,i,i)
	quad=love.graphics.newQuad(char:byte()*8,0,8,12,img:getWidth(),img:getHeight())
	love.graphics.drawq(img,quad,x+((i-1)*8),y)
	end
end
buttons={}
function button_normal(x,y,h,w)
love.graphics.drawq(borders,norm.topleft,x,y)
love.graphics.drawq(borders,norm.top,x+3,y,0,w-6,1)
love.graphics.drawq(borders,norm.midleft,x,y+3,0,1,h-6)
love.graphics.drawq(borders,norm.topright,x+3+w-6,y)
love.graphics.drawq(borders,norm.midright,x+3+w-6,y+3,0,1,h-6)
love.graphics.drawq(borders,norm.bottomleft,x,y+h-6+3)
love.graphics.drawq(borders,norm.bottom,x+3,y+3+h-6,0,w-6,1)
love.graphics.drawq(borders,norm.bottomright,x+3+w-6,y+h-6+3)
r,g,b,a=love.graphics.getColor()
love.graphics.setColor(192,192,192)
love.graphics.rectangle("fill",x+3,y+3,w-6,h-6)
love.graphics.setColor(r,g,b,a)
end
function button_pressed(x,y,h,w)
love.graphics.drawq(borders,pressed.topleft,x,y)
love.graphics.drawq(borders,pressed.top,x+3,y,0,w-6,1)
love.graphics.drawq(borders,pressed.midleft,x,y+3,0,1,h-6)
love.graphics.drawq(borders,pressed.topright,x+3+w-6,y)
love.graphics.drawq(borders,pressed.midright,x+3+w-6,y+3,0,1,h-6)
love.graphics.drawq(borders,pressed.bottomleft,x,y+h-6+3)
love.graphics.drawq(borders,pressed.bottom,x+3,y+3+h-6,0,w-6,1)
love.graphics.drawq(borders,pressed.bottomright,x+3+w-6,y+h-6+3)
end
function button_hovered(x,y,h,w)
love.graphics.drawq(borders,hovered.topleft,x,y)
love.graphics.drawq(borders,hovered.top,x+3,y,0,w-6,1)
love.graphics.drawq(borders,hovered.midleft,x,y+3,0,1,h-6)         
love.graphics.drawq(borders,hovered.topright,x+3+w-6,y)            
love.graphics.drawq(borders,hovered.midright,x+3+w-6,y+3,0,1,h-6) 
love.graphics.drawq(borders,hovered.bottomleft,x,y+h-6+3)          
love.graphics.drawq(borders,hovered.bottom,x+3,y+3+h-6,0,w-6,1)    
love.graphics.drawq(borders,hovered.bottomright,x+3+w-6,y+h-6+3)
end
function _new(n,x,y,w,h)
cat={}
cat.x=x
cat.y=y
cat.h=h
cat.name=n
cat.down=false
cat.visible=true
cat.w=w
cat.text=""
cat.pressed={}
cat.pressed.functions={}
cat.pressed.connect=function(self,funct)
	table.insert(self.functions,funct)
end
cat.addText=function(self,text)
	self.text=text
	return self
end
cat.toggle=function(self)
	self.visible=not self.visible
	return self
end
cat.show=function(self)
	self.visible=true
end
cat.hide=function(self)
	self.visible=false
end
cat.align=function(self,ax,ay)
	self.alignment=tostring(ax)..tostring(ay)
	return self
end
buttons[#buttons+1]=cat
return buttons[#buttons]
end
-- package
function _get(name)
	for i=1,#buttons do
		if buttons[i].name == name then
			return buttons[i]
		end
	end
end
function _clicked(x,y,button)
	if button == "l" then
		for i=1,#buttons do
			if buttons[i].visible then
				if x >= buttons[i].x and x <= buttons[i].x+buttons[i].w then
					if y >= buttons[i].y and y <= buttons[i].y+buttons[i].h then
						for k=1,#buttons[i].pressed.functions do
							buttons[i].pressed.functions[k]()
						end
					end
				end
			end
		end
	end
end
function _refresh()
	for i=1,#buttons do
		if buttons[i].visible then
			button_normal(buttons[i].x,buttons[i].y,buttons[i].h,buttons[i].w)
			if love.mouse.getX() >= buttons[i].x and love.mouse.getX() <= buttons[i].x+buttons[i].w then
				if love.mouse.getY() >= buttons[i].y and love.mouse.getY() <= buttons[i].y+buttons[i].h then
					if love.mouse.isDown("l") then
						button_pressed(buttons[i].x,buttons[i].y,buttons[i].h,buttons[i].w)
						buttons[i].down=true
					else
						button_hovered(buttons[i].x,buttons[i].y,buttons[i].h,buttons[i].w)
						buttons[i].down=false
					end
				else
					buttons[i].down=false
				end
			else
			buttons[i].down=false
			end
			if buttons[i].text ~= nil then
				if buttons[i].alignment == nil or buttons[i].alignment == "11" then
				_print(buttons[i].text,buttons[i].x+3,buttons[i].y+3)
				else
					midx=buttons[i].x+math.floor((buttons[i].w-(8*buttons[i].text:len()))/2)
					midy=(buttons[i].y+math.floor((buttons[i].h-12)/2))
					if buttons[i].alignment == "21" then
						_print(buttons[i].text,midx,buttons[i].y+3)
					elseif buttons[i].alignment == "31" then
						_print(buttons[i].text,buttons[i].x-(8*buttons[i].text:len())+buttons[i].w,buttons[i].y+3)
					elseif buttons[i].alignment == "12" then
						_print(buttons[i].text,buttons[i].x+3,midy)
					elseif buttons[i].alignment == "22" then
						_print(buttons[i].text,midx,midy)
					elseif buttons[i].alignment == "32" then
						_print(buttons[i].text,buttons[i].x-(8*buttons[i].text:len())+buttons[i].w,midy)
					elseif buttons[i].alignment == "13" then
						_print(buttons[i].text,buttons[i].x+3,buttons[i].y+buttons[i].h-15)
					elseif buttons[i].alignment == "23" then
						_print(buttons[i].text,midx,buttons[i].y+buttons[i].h-15)
					elseif buttons[i].alignment == "33" then
						_print(buttons[i].text,buttons[i].x-(8*buttons[i].text:len())+buttons[i].w,buttons[i].y+buttons[i].h-15)
					end
				end
			end
		end
	end
end
hal={
buttons={
	refresh=_refresh,
	getButton=_get,
	clicked=_clicked,
	},
button={
	new=_new,
	get=_get,
	},
bmp={
	print=_print,
	}
}
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: A Windows Classic style button library.

Post by veethree »

You can actually add the file as an attachment instead of posting it like that.
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

Re: A Windows Classic style button library.

Post by Lua Hal »

WinTest.love
(5.96 KiB) Downloaded 255 times
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: A Windows Classic style button library.

Post by coffee »

Lua Hal wrote:
WinTest.love
I think you would be better without the png graphics and quads. You could instead "vector" draw with rectangles, since are very basic and simple shapes to be simulated/draw. That way you could get flexibility of easily create color themes for your gui without have to paint images later.
User avatar
Lua Hal
Citizen
Posts: 58
Joined: Tue Jul 12, 2011 10:30 pm

Re: A Windows Classic style button library.

Post by Lua Hal »

Much of the point is that you can customize the corners/sides of the 3 (4 soon) button states.

Also, I stole much of the ideas from Roblox, a crappy game I quit a year or two ago.

New Dynamic Buttons (buttons with an editable state), and many of the controls have changed.

Planned Scroll bars; horizontal and vertical. Another button state, Support for custom fonts in the buttons, and more.

New code:

Code: Select all

function love.load()
	love.filesystem.load("hal.lua")()
	love.graphics.setBackgroundColor(255,192,192)
	b1=hal.instance.new("Button")
	b1.name="1"
	b1.x,b1.y=20,30
	b1.w,b1.h=80,20
	b1:addText("Toggle"):align(2,2)
	b1.pressed:connect(apressed)

	b2=hal.instance.new("Button")
	b2.name="2"
	b2.x,b2.y=100,130
	b2.w,b2.h=16,16
	b2.pressed:connect(bpressed)

	b3=hal.instance.new("DButton")
	b3.name="3"
	b3.x,b3.y=200,200
	b3.w,b3.h=50,50
end

function apressed()
	hal.button.get("2"):toggle()
end

function bpressed()
	hal.button.get("3").state=math.random(0,2)
end

function love.mousepressed(x,y,button)
	hal.buttons.clicked(x,y,button)
end

function love.draw()
	hal.buttons.refresh()
end
Untitled.png
Untitled.png (17.5 KiB) Viewed 3189 times
visio
Prole
Posts: 2
Joined: Thu May 03, 2012 6:33 am

Re: A Windows Classic style button library.

Post by visio »

Lua Hal you did the great job, but there are low quality(means according to good programming practices), there are some holes. Keep doing well. I'll keep in touch with you.
Post Reply

Who is online

Users browsing this forum: No registered users and 90 guests