(label, options, callback, selectedIndex)
| 65 | signs.menu = signMenu; |
| 66 | |
| 67 | function signMenu(label, options, callback, selectedIndex) { |
| 68 | if (typeof selectedIndex == 'undefined') { |
| 69 | selectedIndex = 0; |
| 70 | } |
| 71 | // |
| 72 | // variables common to all instances of this menu can go here |
| 73 | // |
| 74 | var labelPadding = '---------------'; |
| 75 | var optionPadding = ' '; |
| 76 | var i; |
| 77 | var paddedLabel = (labelPadding + label + labelPadding).substr( |
| 78 | (label.length + 30) / 2 - 7, |
| 79 | 15 |
| 80 | ); |
| 81 | var optLen = options.length; |
| 82 | var displayOptions = []; |
| 83 | for (i = 0; i < options.length; i++) { |
| 84 | displayOptions[i] = (' ' + options[i] + optionPadding).substring(0, 15); |
| 85 | } |
| 86 | /* |
| 87 | this function is returned by signs.menu and when it is invoked it will |
| 88 | attach menu behaviour to an existing sign in the world. |
| 89 | signs.menu is for use by Plugin Authors. |
| 90 | The function returned by signs.menu is for use by admins/ops. |
| 91 | */ |
| 92 | var convertToMenuSign = function(/* Sign */ sign, save) { |
| 93 | if (typeof save == 'undefined') { |
| 94 | save = true; |
| 95 | } |
| 96 | // |
| 97 | // per-sign variables go here |
| 98 | // |
| 99 | var cSelectedIndex = selectedIndex; |
| 100 | setLine(sign, 0, paddedLabel.bold()); |
| 101 | var _updateSign = function(p_player, p_sign) { |
| 102 | cSelectedIndex = (cSelectedIndex + 1) % optLen; |
| 103 | _redrawMenuSign(p_sign, cSelectedIndex, displayOptions); |
| 104 | var signSelectionEvent = { |
| 105 | player: p_player, |
| 106 | sign: p_sign, |
| 107 | text: options[cSelectedIndex], |
| 108 | number: cSelectedIndex |
| 109 | }; |
| 110 | callback(signSelectionEvent); |
| 111 | }; |
| 112 | |
| 113 | /* |
| 114 | get a unique ID for this particular sign instance |
| 115 | */ |
| 116 | var signLoc = sign.block.location; |
| 117 | var menuSignSaveData = utils.locationToJSON(signLoc); |
| 118 | var menuSignUID = JSON.stringify(menuSignSaveData); |
| 119 | /* |
| 120 | keep a reference to the update function for use by the event handler |
| 121 | */ |
| 122 | _updaters[menuSignUID] = _updateSign; |
| 123 | |
| 124 | // initialize the sign |
nothing calls this directly
no test coverage detected