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,
}
}