-
Notifications
You must be signed in to change notification settings - Fork 12
/
keypad.lua
229 lines (219 loc) · 6.45 KB
/
keypad.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
local visible = false
local BUTTONS = {
key_1 = {
at = vector2(0.4555,0.542),
size = vector2(0.025,0.025),
action = function(code)
code = code .. '1'
return code
end,
},
key_2 = {
at = vector2(0.4855, 0.542),
size = vector2(0.025, 0.025),
action = function(code)
code = code .. '2'
return code
end,
},
key_3 = {
at = vector2(0.5145, 0.542),
size = vector2(0.025, 0.025),
action = function(code)
code = code .. '3'
return code
end,
},
key_red = {
at = vector2(0.5445, 0.542),
size = vector2(0.025, 0.025),
action = function(code)
return false
end,
},
key_4 = {
at = vector2(0.4555, 0.593),
size = vector2(0.025,0.025),
action = function(code)
code = code .. '4'
return code
end,
},
key_5 = {
at = vector2(0.4855, 0.593),
size = vector2(0.025, 0.025),
action = function(code)
code = code .. '5'
return code
end,
},
key_6 = {
at = vector2(0.5145, 0.593),
size = vector2(0.025, 0.025),
action = function(code)
code = code .. '6'
return code
end,
},
key_green = {
at = vector2(0.5445, 0.593),
size = vector2(0.025, 0.025),
action = function(code)
return true
end,
},
key_7 = {
at = vector2(0.4555, 0.642),
size = vector2(0.025,0.025),
action = function(code)
code = code .. '7'
return code
end,
},
key_8 = {
at = vector2(0.4855, 0.642),
size = vector2(0.025, 0.025),
action = function(code)
code = code .. '8'
return code
end,
},
key_9 = {
at = vector2(0.5145, 0.642),
size = vector2(0.025, 0.025),
action = function(code)
code = code .. '9'
return code
end,
},
key_blue = {
at = vector2(0.5445, 0.642),
size = vector2(0.025, 0.025),
action = function(code)
code = string.sub(code, 1, -2)
return code
end,
},
key_asterisk = {
at = vector2(0.4555, 0.693),
size = vector2(0.025,0.025),
action = function(code)
code = code .. '*'
return code
end,
},
key_0 = {
at = vector2(0.4855, 0.693),
size = vector2(0.025, 0.025),
action = function(code)
code = code .. '0'
return code
end,
},
key_pound = {
at = vector2(0.5145, 0.693),
size = vector2(0.025, 0.025),
action = function(code)
code = code .. '#'
return code
end,
},
key_yellow = {
at = vector2(0.5445, 0.693),
size = vector2(0.025, 0.025),
action = function(code)
return ''
end,
},
}
function loadTxd(txdName)
if not HasStreamedTextureDictLoaded(txdName) then
RequestStreamedTextureDict(txdName, true)
while not HasStreamedTextureDictLoaded(txdName) do
Citizen.Wait(0)
end
end
end
function disableControlsExcept(...)
for group=0, 31 do
DisableAllControlActions(group)
end
for _,control in ipairs({...}) do
EnableControlAction(0, control, true)
end
end
function IsKeypadShown()
return visible
end
function ShowKeypad(area, door, lastKey, nextState)
if visible then
return
end
visible = true
Citizen.CreateThread(function()
local code = lastKey or ''
while visible do
disableControlsExcept(245, 249) -- Enable chat and voice PTT. TODO: Anything else?
SetMouseCursorActiveThisFrame()
loadTxd('demmylock')
local aspect_ratio = GetAspectRatio(true)
DrawSprite(
'demmylock',
'keypad',
0.5, -- X
0.5, -- Y
0.5 / aspect_ratio, -- Width
0.5, -- Height
0.0, -- Rotation
255, 255, 255, 255, -- Color
true -- Dunno
)
local cursorX = GetDisabledControlNormal(0, 239)
local cursorY = GetDisabledControlNormal(0, 240)
local action = nil
for name, data in pairs(BUTTONS) do
local adjustedSize = vector2(data.size.x, data.size.y*aspect_ratio) / 2
if
cursorX > data.at.x - adjustedSize.x
and cursorX < data.at.x + adjustedSize.x
and cursorY > data.at.y - adjustedSize.y
and cursorY < data.at.y + adjustedSize.y
then
--DrawRect(data.at, adjustedSize * 2, 255, 0, 0, 128)
action = data.action
else
--DrawRect(data.at, adjustedSize * 2, 255, 255, 255, 128)
end
end
if IsDisabledControlPressed(0, 177) then -- INPUT_CELLPHONE_CANCEL, Backspace
visible = false
elseif IsDisabledControlJustPressed(0, 24) then
-- Citizen.Trace(cursorX..'/'..cursorY.."\n")
if action then
local result = action(code)
if (type(result) == 'string') then
code = result
code = string.sub(code,-4)
elseif (type(result) == 'boolean') then
setLastKey(area, door, code)
TriggerServerEvent('demmylock:entered-pin', area, door, code, not result)
visible = false
end
end
elseif IsDisabledControlJustPressed(0, 51) then
setLastKey(area, door,code)
TriggerServerEvent('demmylock:entered-pin', area, door, code, nextState)
visible = false
end
BeginTextCommandDisplayText('STRING')
SetTextColour(0,0,0,200)
SetTextScale(1.0,1.0)
SetTextFont(0)
SetTextCentre(true)
AddTextComponentSubstringPlayerName(code)
EndTextCommandDisplayText(0.5,0.37)
Citizen.Wait(0)
end
SetStreamedTextureDictAsNoLongerNeeded('demmylock')
end)
end