Concept of a button?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Ramcat
Prole
Posts: 18
Joined: Sun Jun 01, 2014 12:12 am

Re: Concept of a button?

Post by Ramcat »

Thank you all for your input. We did create our own buttons and toggle-able labels, as well as a whole micro-architecture framework to run Love2D projects in. It is yours to enjoy!
Ramcat
Prole
Posts: 18
Joined: Sun Jun 01, 2014 12:12 am

Re: Concept of a button?

Post by Ramcat »

Uh, yeah. Hmmmm.... Lua as a language has a few surprises to a classical OOP coder like myself. We have found some interesting... "defects" if you're of the mentality of Extreme Programming. Other people call them bugs. For one, it appears that the code we are using to create objects allows you to easily declare static variables at the class level so all instances of the class share the same variable. Oops.

We found that in our tank class and our button class. So here is a fixed version of our button class without the shared visibility variable.

Code: Select all

require 'class'

-- CONSTRUCTOR
local M=class(function(self, text, tooltip, x, y, width, height, color, textColor, 
 tooltipColor, tooltipTextColor, offsetX, offsetY, showDelay, func, args)
	-- Store arguments
	self.text = text
	self.tooltip = tooltip
	self.X = x
	self.Y = y
	self.width = width
	self.height = height
	self.color = color
	self.textColor = textColor
	self.tooltipColor = tooltipColor
	self.tooltipTextColor = tooltipTextColor
	self.offsetX = offsetX
	self.offsetY = offsetY
	self.showDelay = showDelay
	self.func = func
	self.args = args
	-- Set defaults
	self.visible = true
	self.hover = false
	self.startHoverTime = 0
	self.hoverTime = 0
	-- Calculate text size
	local font = love.graphics.newFont()
	self.textWidth = font:getWidth(text)
	self.textHeight = font:getHeight(text)
	self.tooltipWidth = font:getWidth(tooltip)
	self.tooltipHeight = font:getHeight(tooltip)
end)

-- STAGE FRAMEWORK
function M:update(dt)
	local mouseX = 0
	local mouseY = 0
	mouseX, mouseY = love.mouse.getPosition()
	if mouseX > self.X and mouseX < self.X + self.width and mouseY > self.Y and 
	 mouseY < self.Y + self.height then
		if self.startHoverTime == 0 then
			self.startHoverTime = os.time()
		elseif self.startHoverTime > 0 then
			self.hoverTime = self.hoverTime + (dt * 1000)
		end
	else
		self.hover = false
		self.startHoverTime = 0
		self.hoverTime = 0
	end
	if self.hoverTime > self.showDelay then
		self.hover = true
	end
end

function M:draw()
	if self.visible then
		love.graphics.setColor(self.color[1], self.color[2], self.color[3], self.color[4])
		love.graphics.rectangle("fill", self.X, self.Y, self.width, self.height)
		love.graphics.setColor(self.textColor[1], self.textColor[2], 
		 self.textColor[3], self.textColor[4])
		love.graphics.print(self.text, self.X + (self.width / 2) - (self.textWidth / 2), 
		 self.Y + (self.height / 2) - (self.textHeight / 2))
		if self.hover and self.tooltip then
			love.graphics.setColor(self.tooltipColor[1], self.tooltipColor[2], 
			 self.tooltipColor[3], self.tooltipColor[4])
			local tooltipX = self.X + self.offsetX
			local tooltipY = self.Y + self.offsetY
			love.graphics.rectangle("fill", tooltipX, tooltipY, 
			 self.tooltipWidth, self.tooltipHeight)
			love.graphics.setColor(self.tooltipTextColor[1], self.tooltipTextColor[2], 
			 self.tooltipTextColor[3], self.tooltipTextColor[4])
			love.graphics.print(self.tooltip, tooltipX, tooltipY)
		end
	end
end

function M:mousepressed(x, y, button)
	if x > self.X and x < self.X + self.width and y > self.Y and y < self.Y + self.height then
		if self.func then
			if self.args then
				self.func(unpack(self.args))
			else
				self.func()
			end
		end
	end
end

function M:mousereleased(x, y, button)
end

return M
It is a basic button (without Windows style code that causes the button to only fire on a MouseUp event (that could be easily added)) with features like, centered text, a tooltip, color for the button and the tooltip, tooltip position, and delay to show the tooltip. Because I am a classical oop guy I think in objects and this button is an object designed to be used with StageManager and a Controller. The required class (require 'class') can be found in LoveTank.

The usage is like:

Code: Select all

_StartButton = Button("Start", "Start the game with the selected tanks", 600, 550, 60, 20, 
	 {0, 200, 255, 255}, {0, 0, 0, 255}, {200, 200, 200, 255}, {0, 0, 0, 255}, 
	 -70, -20, 1000, onStartButtonClick)
User avatar
OttoRobba
Party member
Posts: 104
Joined: Mon Jan 06, 2014 5:02 am
Location: Sao Paulo, Brazil

Re: Concept of a button?

Post by OttoRobba »

Ramcat wrote:Uh, yeah. Hmmmm.... Lua as a language has a few surprises to a classical OOP coder like myself. We have found some interesting... "defects" if you're of the mentality of Extreme Programming. Other people call them bugs. For one, it appears that the code we are using to create objects allows you to easily declare static variables at the class level so all instances of the class share the same variable. Oops.
That happens with table-based class implementation because by default, in Lua, assigning a table to two variables is not copying it - the variables act as mere shortcuts to the same spot in memory. With classes this becomes the inheritance but yeah, it can be a weird "Gotcha" for those not used to it.

Code: Select all

t = {}
b = t
print(t) --both are gonna print the exact same table
print(b)
Like you said, you guys fixed it but if you want to copy a table, the simplest way (a shallow copy) is just

Code: Select all

for k,v in pairs(t) do
    b[k] = v
end
There are better ways, for more info> http://lua-users.org/wiki/CopyTable

:-)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 54 guests