summaryrefslogtreecommitdiff
path: root/examples/raygui_extensions/property_list.lua
blob: e05c84c0f48794042cb09692a804d270dd34a066 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
local PropertyList = {}
PropertyList.__index = PropertyList

function PropertyList:new( bounds, text, content, scroll, callback, grabCallback, dragCallback )
    local object = setmetatable( {}, self )
	object._parent = nil

    object.bounds = bounds:clone()
    object.text = text
    object.content = content:clone()
    object.scroll = scroll:clone()
	object.view = Rect:new()
    object.callback = callback
	object.grabCallback = grabCallback
	object.dragCallback = dragCallback

	object.gui = Raygui:new() -- Contains full independent gui system.

	-- Set initial view and scroll.
	local _, scrollP, view = RL.GuiScrollPanel( object.bounds, object.text, object.content, object.scroll, object.view )
	object.scroll = Vec2:new( scrollP )
	object.view = Rect:new( view )

	object.gui.view = Rect:new( 0, 0, object.view.width, object.view.height )
	object.framebuffer = RL.LoadRenderTexture( { object.view.width, object.view.height } )

	object.visible = true
	object.disabled = false
	object.draggable = true
	object.mouseScale = 1

	object:updateMouseOffset()

	return object
end

function PropertyList:process()
	if not RL.CheckCollisionRecs( self.view, RL.GetMousePosition() ) then
		self.gui.locked = true
	else
		self.gui.locked = false
	end

	self.gui:process()

	RL.BeginTextureMode( self.framebuffer )
		RL.ClearBackground( RL.BLANK )
		RL.rlTranslatef( { self.scroll.x, self.scroll.y, 0 } )
		self.gui:draw()
	RL.EndTextureMode()

    return self._parent:drag( self )
end

function PropertyList:draw()
	local oldScroll = self.scroll:clone()
	local _, scroll, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view )

	self.view = Rect:new( view )
	self.scroll = Vec2:new( scroll )

	if self.scroll ~= oldScroll then
		self._parent:checkScrolling()

		self.gui.view:set( -self.scroll.x, -self.scroll.y, self.view.width, self.view.height )
		self:updateMouseOffset()

		if self.callback ~= nil then
			self.callback( self )
		end
	end

	RL.DrawTexturePro(
		RL.GetRenderTextureTexture( self.framebuffer ),
		{ 0, 0, self.view.width, -self.view.height },
		{ math.floor( self.view.x ), math.floor( self.view.y ), self.view.width, self.view.height },
		{ 0, 0 },
		0.0,
		RL.WHITE 
	)
end

function PropertyList:updateMouseOffset()
	self.gui.mouseOffset = Vec2:new( -self.view.x - self.scroll.x, -self.view.y - self.scroll.y ):scale( self.mouseScale )
end

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

	self:updateMouseOffset()
end

function PropertyList:register( gui )
	function gui:PropertyList( bounds, text, texture, nPatchNormal, nPatchPressed, callback )
		return self:addElement( PropertyList:new( bounds, text, texture, nPatchNormal, nPatchPressed, callback ) )
	end
end

return PropertyList