| 1 |
/* |
| 2 |
* This file is part of Deliantra clientV. |
| 3 |
* |
| 4 |
* Copyright (©) 2012 Marek Olszewski <m.olszewski@wit.edu.pl> |
| 5 |
* |
| 6 |
* Deliantra clientV is free software: you can redistribute it and/or |
| 7 |
* modify it under the terms of the Affero GNU General Public License as |
| 8 |
* published by the Free Software Foundation, either version 3 of the |
| 9 |
* License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the Affero GNU General Public Licens |
| 17 |
* and the GNU General Public License along with this program. If not, see |
| 18 |
* <http://www.gnu.org/licenses/>. |
| 19 |
* |
| 20 |
* The authors can be reached via e-mail to <support@deliantra.net> |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
Application = { |
| 25 |
skillAdded : function ( name, id, value ) { |
| 26 |
} , |
| 27 |
spellAdded : function ( spell ) { |
| 28 |
commandsAll[0].push ( "invoke " + spell.name ); |
| 29 |
commandsAll[0].push ( "cast " + spell.name ); |
| 30 |
} |
| 31 |
|
| 32 |
}; |
| 33 |
|
| 34 |
function commandLineOnKeyDown(e) { |
| 35 |
if (e.keyCode == 13) { |
| 36 |
skipKeyTest = false; |
| 37 |
|
| 38 |
doSend("command " + xCommandLines.value); |
| 39 |
command = ""; |
| 40 |
lastCommand = xCommandLines.value; |
| 41 |
|
| 42 |
this.isShown == false; |
| 43 |
document.getElementsByTagName("body")[0].removeChild(xCommandContainer); |
| 44 |
|
| 45 |
} else if (e.keyCode == 27) { |
| 46 |
command = ""; |
| 47 |
skipKeyTest = false; |
| 48 |
this.isShown == false |
| 49 |
document.getElementsByTagName("body")[0].removeChild(xCommandContainer); |
| 50 |
|
| 51 |
} else if ( e.keyCode == 38 ){ |
| 52 |
|
| 53 |
xCommandLines.moveUp ( ); |
| 54 |
|
| 55 |
} else if ( e.keyCode == 40 ){ |
| 56 |
|
| 57 |
xCommandLines.moveDown ( ); |
| 58 |
|
| 59 |
} else { |
| 60 |
//messageBox(JSON.stringify(commandsAll)); |
| 61 |
return true; |
| 62 |
} |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
var lastCommandTmp = ""; |
| 67 |
|
| 68 |
// matches a word against a comamnd name/args. returns null if not |
| 69 |
// matched, otherwise an array with [cmd, score, result]. |
| 70 |
function command_match(input,cmd) { |
| 71 |
// the first character must always match, so quickly bail out if not |
| 72 |
if (input.charAt (0) != cmd.charAt (0)) |
| 73 |
return null; |
| 74 |
|
| 75 |
// 1. the command is a prefix of the user input, score is cmd length |
| 76 |
if (input.substr (0, cmd.length) == cmd) |
| 77 |
return [cmd, cmd.length, input]; |
| 78 |
|
| 79 |
// 2. user input is a prefix of the command, score, socre is input length |
| 80 |
if (cmd.substr (0, input.length) == input) |
| 81 |
return [cmd, input.length, cmd]; |
| 82 |
|
| 83 |
// split into initial word and arguments, not very elegant |
| 84 |
// to avoid problems with the regex later, we only allow |
| 85 |
// some characters as word character. |
| 86 |
var match = input.match (/^([a-zA-Z0-9_-]+)\S*\s*/); |
| 87 |
var word = match [1]; |
| 88 |
var args = input.substr (match [0].length); |
| 89 |
|
| 90 |
// 3. match first word as abbreviation |
| 91 |
// score is length of word + penalty if character are not initial |
| 92 |
var regex = "^" + word.charAt (0); |
| 93 |
for (var i = 1; i < word.length; ++i) |
| 94 |
regex += "(.*?[ _\-]|(.*?))" + word.charAt (i); |
| 95 |
|
| 96 |
var match = cmd.match (new RegExp (regex)); |
| 97 |
if (match) { |
| 98 |
var score = cmd.length + 1; |
| 99 |
|
| 100 |
for (var i = 2; i < match.length; i += 2) |
| 101 |
if (match [i] != null) |
| 102 |
score -= match [i].length + 3; |
| 103 |
|
| 104 |
return [cmd, score, cmd + (args ? " " + args : "")]; |
| 105 |
} |
| 106 |
|
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
function autoCompleteEx() { |
| 111 |
if (xCommandLine.isShown == false) |
| 112 |
return; |
| 113 |
|
| 114 |
var results = []; |
| 115 |
var input = xCommandLine.value; |
| 116 |
|
| 117 |
if ( input.length == 0 ) { |
| 118 |
xCommandLines.innerHTML = ""; |
| 119 |
setTimeout(autoCompleteEx, 300); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
if ( lastCommandTmp == input ) { |
| 124 |
setTimeout(autoCompleteEx, 300); |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
[["invoke small fireball","cast small fireball"], |
| 130 |
["accept-invitation","afk","apply","applymode always","applymode never","applymode nochoice","body","bowmode","bowmode bestarrow","bowmode normal","bowmode spreadshot","bowmode threewide","brace","bumpmsg","cast","chat","cointoss","drop","drop all","drop cursed","drop unlocked","drop unpaid","dropall","examine","follow","get","gsay","help","hintmode hide","hintmode mark","hintmode show","hiscore","ignore","inventory","invite","invoke","killpets","lock","mapinfo","maps","mark","me","motd","orcknuckle","output-count","output-rate","output-sync","party","peaceful","petmode arena","petmode defend","petmode normal","petmode sad","pickup","prepare","quit","range","ready_skill","rename","resistances","rotateshoottype","save","say","search-items","seen","shout","showpets","skills","sound","sourcecode","statistics","suicide","take","tell","throw","time","title","unignore","unlock","uptime","use_skill","usekeys containers","usekeys inventory","usekeys keyrings","version","weather","whereabouts","whereami","who","wimpy"],null,null,["bleed","blush","bounce","bow","burp","cackle","chuckle","clap","cough","cringe","cry","dance","flip","frown","gasp","giggle","glare","grin","groan","growl","hiccup","kiss","laugh","lick","nod","pout","puke","scream","shake","shiver","shrug","sigh","smile","smirk","snap","sneeze","snicker","sniff","snore","spit","strut","sulk","think","twiddle","wave","whistle","wink","yawn"]] |
| 131 |
*/ |
| 132 |
//messageBox ( JSON.stringify ( commandsAll ) ); |
| 133 |
var counter = 0; |
| 134 |
|
| 135 |
for ( var k = 0 ; k < commandsAll.length ; ++k ) { |
| 136 |
var j = ( k + 1 ) % commandsAll.length; //make my list (cast/invoke) to be treated as last |
| 137 |
if ( typeof (commandsAll [ j ]) != "undefined" ) |
| 138 |
for ( var i = 0 ; i < commandsAll [ j ].length ; ++i ) { |
| 139 |
|
| 140 |
var res = command_match ( input , commandsAll[ j ][ i ] ); |
| 141 |
if ( res != null ) { |
| 142 |
res[3] = counter ++; |
| 143 |
results.push ( res ); |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
results.sort( function ( a , b ) { return b[1] - a[1] || a[3] - b[3]; } ); |
| 150 |
|
| 151 |
|
| 152 |
xCommandLines.innerHTML = ""; |
| 153 |
for (i = 0; i < results.length; ++i) { |
| 154 |
var option = $_("option"); |
| 155 |
option.innerHTML = results[i][2] + "(" + results[i][1] + ")" + "(" + results [ i ][ 3 ] + ")"; |
| 156 |
option.value = results[i][2]; |
| 157 |
xCommandLines.appendChild(option); |
| 158 |
} |
| 159 |
xCommandContainer.removeChild(xCommandLines); |
| 160 |
xCommandContainer.appendChild(xCommandLines); |
| 161 |
xCommandLines.moveDown(); |
| 162 |
|
| 163 |
|
| 164 |
lastCommandTmp = xCommandLine.value; |
| 165 |
|
| 166 |
setTimeout(autoCompleteEx, 300); |
| 167 |
|
| 168 |
} |
| 169 |
function autoComplete(keyCode) { |
| 170 |
if (typeof(keyCode) == "undefined") keyCode = "c".charCodeAt(0); |
| 171 |
if (xCommandContainer == null) { |
| 172 |
xCommandContainer = $_("div", { |
| 173 |
css: { |
| 174 |
position: "absolute", |
| 175 |
left: (stage.stageWidth / 2) + "px", |
| 176 |
top: (stage.stageHeight / 2) + "px" |
| 177 |
} |
| 178 |
}); |
| 179 |
|
| 180 |
xCommandLine = $_("input"); |
| 181 |
xCommandLine.isShown == true; |
| 182 |
|
| 183 |
xCommandContainer.appendChild(xCommandLine); |
| 184 |
xCommandLine.onkeydown = commandLineOnKeyDown; |
| 185 |
xCommandContainer.appendChild($_("br")); |
| 186 |
|
| 187 |
xCommandLines = $_("select"); |
| 188 |
xCommandLines.size = 5; |
| 189 |
|
| 190 |
xCommandContainer.appendChild(xCommandLines); |
| 191 |
xCommandLines.selectedIndex = 0; |
| 192 |
|
| 193 |
xCommandLines.moveUp = function ( ) { |
| 194 |
if ( ! this.selectedIndex ) { |
| 195 |
this.selectedIndex = 0; |
| 196 |
} |
| 197 |
this.selectedIndex -= 1; |
| 198 |
if ( this.selectedIndex < 0 ) { |
| 199 |
this.selectedIndex = 0; |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
xCommandLines.moveDown = function ( ) { |
| 204 |
if ( ! this.selectedIndex ){ |
| 205 |
this.selectedIndex = 0; |
| 206 |
} |
| 207 |
this.selectedIndex += 1; |
| 208 |
}; |
| 209 |
setTimeout(autoCompleteEx, 300); |
| 210 |
|
| 211 |
} |
| 212 |
if (command == "") { |
| 213 |
skipKeyTest = true; |
| 214 |
appendToBody(xCommandContainer); |
| 215 |
xCommandLine.isShown == true; |
| 216 |
try { |
| 217 |
xCommandLine.focus(); |
| 218 |
} catch(e) { |
| 219 |
|
| 220 |
} |
| 221 |
} |
| 222 |
command += String.fromCharCode(keyCode).toLowerCase(); |
| 223 |
xCommandLine.value = command; |
| 224 |
autoCompleteEx(); |
| 225 |
} |