summaryrefslogtreecommitdiff
path: root/examples/raygui_extensions/sprite_button.lua
blob: 201e617b8601466109e4bc039f84e17e60998b4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
local SpriteButton = {}
SpriteButton.__index = SpriteButton

function SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callbacks, styles, tooltip )
	local object = setmetatable( {}, self )
	object._gui = nil

	object.bounds = bounds:clone()
	object.text = text
	object.buttonTexture = texture
	object.nPatchNormal = nPatchNormal
	object.nPatchPressed = nPatchPressed
	object.callbacks = callbacks -- pressed.
	object.styles = styles
	object.tooltip = tooltip

	object.visible = true
	object.disabled = false

	return object
end

function SpriteButton:update()
	return RL.CheckCollisionPointRec( RL.GetMousePosition(), self.bounds )
end

function SpriteButton:draw()
	if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_LEFT ) and self:update() and not RL.GuiIsLocked()
	and not RL.GuiGetSliderDragging() then
		RL.DrawTextureNPatchRepeat(
			self.buttonTexture, self.nPatchPressed, self.bounds,
			{ 0, 0 }, 0.0, RL.GetColor( RL.GuiGetStyle( RL.BUTTON, RL.BASE_COLOR_PRESSED ) )
		)
	else
		RL.DrawTextureNPatchRepeat(
			self.buttonTexture, self.nPatchNormal, self.bounds,
			{ 0, 0 }, 0.0, RL.GetColor( RL.GuiGetStyle( RL.BUTTON, RL.BASE_COLOR_NORMAL ) )
		)
	end

	local result = RL.GuiLabelButton( self.bounds, self.text )

	if result == 1 and self.callbacks.pressed ~= nil and self._gui:clickedInBounds( self.bounds ) then
		self.callbacks.pressed( self )
	end
end

function SpriteButton:setPosition( pos )
	self.bounds.x = pos.x
	self.bounds.y = pos.y
end

function SpriteButton:register( gui )
	function gui:SpriteButton( bounds, text, texture, nPatchNormal, nPatchPressed, callbacks, styles, tooltip )
		return self:addControl( SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callbacks, styles, tooltip ) )
	end
end

return SpriteButton