-
Notifications
You must be signed in to change notification settings - Fork 15
/
selectionList.lua
136 lines (117 loc) · 4.11 KB
/
selectionList.lua
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
local Component = require("component")
local SelectionList = {}
SelectionList.__index = SelectionList
setmetatable(SelectionList, Component)
function SelectionList.create(x, y, width, length, spacing, listener)
local self = setmetatable({}, SelectionList)
self.x, self.y = x, y
self.width = width
self.length = length
self.height = 28
self.spacing = spacing or 14
self.listener = listener
self.scroll = 1
self.selection = 0
self.id = ""
self.items = {}
self.labels = {}
self.backgroundColor = {0, 0, 0, 255}
self.selectionColor = {20, 20, 20, 255}
self.imgButton = ResMgr.getImage("selectionlist_buttons.png")
self.quadButtonLeft = love.graphics.newQuad(0, 0, 2, 14, 49, 14)
self.quadButtonRight = love.graphics.newQuad(3, 0, 2, 14, 49, 14)
self.quadButtonMiddle = love.graphics.newQuad(2, 0, 1, 14, 49, 14)
self.quadButtonUp = love.graphics.newQuad(5, 0, 22, 14, 49, 14)
self.quadButtonDown = love.graphics.newQuad(27, 0, 22, 14, 49, 14)
return self
end
function SelectionList:draw()
love.graphics.setColor(self.backgroundColor)
love.graphics.rectangle("fill", self.x, self.y+14, self.width, self.height-28)
love.graphics.setColor(255,255,255,255)
-- Draw buttons
love.graphics.draw(self.imgButton, self.quadButtonLeft, self.x, self.y)
love.graphics.draw(self.imgButton, self.quadButtonLeft, self.x, self.y+self.height-14)
love.graphics.draw(self.imgButton, self.quadButtonRight, self.x+self.width-2, self.y)
love.graphics.draw(self.imgButton, self.quadButtonRight, self.x+self.width-2, self.y+self.height-14)
love.graphics.draw(self.imgButton, self.quadButtonMiddle, self.x+2, self.y, 0, self.width-4, 1)
love.graphics.draw(self.imgButton, self.quadButtonMiddle, self.x+2, self.y+self.height-14, 0, self.width-4, 1)
if self.scroll > 1 then
love.graphics.draw(self.imgButton, self.quadButtonUp, self.x+self.width/2-11, self.y)
end
if self.scroll < #self.items-self.length+1 then
love.graphics.draw(self.imgButton, self.quadButtonDown, self.x+self.width/2-11, self.y+self.height-14)
end
-- Draw sides
love.graphics.setColor(255, 194, 49)
love.graphics.setLineWidth(1)
love.graphics.line(self.x+1, self.y+14, self.x+1, self.y+self.height-14)
love.graphics.line(self.x+self.width, self.y, self.x+self.width, self.y+self.height)
love.graphics.setColor(255, 255, 255)
love.graphics.setFont(ResMgr.getFont("bold"))
for i=1, self.length do
local index = i+self.scroll-1
if index > #self.labels then break end
if i > #self.labels then break end
if index == self.selection then
love.graphics.setColor(self.selectionColor)
love.graphics.rectangle("fill", self.x+1, self.y+(i-1)*self.spacing+14, self.width-2, self.spacing)
love.graphics.setColor(255, 255, 255, 255)
end
love.graphics.print(self.labels[index], self.x+5, self.y+(i-1)*self.spacing+(self.spacing-8)/2+14)
end
end
function SelectionList:click(x, y)
if x >= self.x and x <= self.x+self.width
and y >= self.y and y <= self.y+self.height then
if y <= self.y+14 then
self.scroll = math.max(1, self.scroll-1)
elseif y <= self.y+self.height-14 then
local sel = math.floor((y-self.y-14)/self.spacing)+self.scroll
if sel <= #self.items then
self:setSelection(sel)
end
else
self.scroll = math.min(self.scroll+1, #self.items-self.length+1)
self.scroll = math.max(1, self.scroll)
end
return true
end
return false
end
function SelectionList:setItems(items, labels)
self.items = items
self.labels = labels
self.height = self.length*self.spacing + 28
self.selection = 0
end
function SelectionList:setId(id)
self.id = id
end
function SelectionList:getText()
if self.selection > 0 then
return self.labels[self.selection]
else
return ""
end
end
function SelectionList:setBackgroundColor(r,g,b,a)
self.backgroundColor = {r,g,b,a}
end
function SelectionList:setSelectionColor(r, g, b, a)
self.selectionColor = {r,g,b,a}
end
function SelectionList:setSelection(index)
if index >= 1 and index <= #self.items then
self.selection = index
else
self.selection = 1
end
if self.listener then
self.listener:selectionChanged(self)
end
end
function SelectionList:getSelection()
return self.items[self.selection]
end
return SelectionList