MCPcopy Index your code
hub / github.com/SasPes/key-decoding / Key

Function Key

KeyDecoding.js:193–299  ·  view source on GitHub ↗
(type, outline, show)

Source from the content-addressed store, hash-verified

191};
192
193function Key(type, outline, show) {
194 this.type = type;
195 this.outline = outline;
196 this.show = show;
197 this.pins = [];
198
199 // Initialize pins
200 if (typeof outline === "string" && typeof show === "string") {
201 var pinCount = parseInt(outline.substring(0, 2), 10);
202 if (!isNaN(pinCount)) {
203 if (show === "decode") {
204 for (var i = 0; i < pinCount; i++) {
205 this.pins.push(0);
206 }
207 } else {
208 for (var i = 0; i < pinCount; i++) {
209 var maxKeyCut = (keys[this.type] && keys[this.type].maxKeyCut) || 9;
210 this.pins.push(Math.floor(Math.random() * maxKeyCut - 1) + 1);
211 }
212 // Ensure last disk shows as 1 for disk detainer (internal index 0)
213 if (keys[this.type] && keys[this.type].isDiskDetainer && pinCount > 0) {
214 this.pins[pinCount - 1] = 0;
215 }
216 }
217 }
218 }
219
220 this.updatePins = function () {
221 var pinCount = parseInt(outline.substring(0, 2), 10);
222 this.pins = [];
223 for (var i = 0; i < pinCount; i++) {
224 var maxKeyCut = (keys[this.type] && keys[this.type].maxKeyCut) || 9;
225 this.pins.push(Math.floor(Math.random() * maxKeyCut - 1) + 1);
226 }
227 // Ensure last disk shows as 1 for disk detainer (internal index 0)
228 if (keys[this.type] && keys[this.type].isDiskDetainer && pinCount > 0) {
229 this.pins[pinCount - 1] = 0;
230 }
231 };
232
233 this.draw = function () {
234 var numberOfActions = 2; // Save, Load
235 if (selectedPinIndex >= this.pins.length + numberOfActions) {
236 selectedPinIndex = 0;
237 }
238
239 display.fill(bgColor);
240 display.drawRoundRect(1, 1, displayWidth - 2, displayHeight - 2, 4, priColor);
241 display.setTextSize(2);
242
243 var displayName = keys[this.type] && keys[this.type].displayName || this.type;
244 if (displayName.length > 12) {
245 display.drawString(displayName, 10, 10);
246 display.drawString(this.outline, 10, 28);
247 } else {
248 display.drawString(displayName + " - " + this.outline, 10, 10);
249 }
250

Callers

nothing calls this directly

Calls 1

drawPinsWithUnderlineFunction · 0.85

Tested by

no test coverage detected