forked from tyt2y3/F.core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller-changer.js
More file actions
127 lines (113 loc) · 2.54 KB
/
Copy pathcontroller-changer.js
File metadata and controls
127 lines (113 loc) · 2.54 KB
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
//a key changer utility
define(['F.core/controller','F.core/util'], function (Fcontroller,Futil)
{
/*\
* controller_changer
[ class ]
* a simplistic key changer for @controller
- config (object)
| config=
| {
| div, //DOM node
| controller, //can be a single F.controller or an array of F.controllers
| //if it is an array, will create player1, player2 etc
| onchange //function be called on change key
| }
\*/
function keychanger (config)
{
var change_active=false;
var div = config.div;
var controllers = config.controller;
div.style.textAlign='center';
controllers = Futil.make_array(controllers);
for( var i=0; i<controllers.length; i++)
{
add_player(controllers[i], i);
}
//the ok button
var rule = create_at(div,'div');
rule.style.clear='both';
/* var ok = create_at(div,'button');
ok.innerHTML='close';
ok.style.width='120px';
ok.onclick= function()
{
div.style.display='none';
}*/
function add_player(con, num)
{
if( num!==0)
{
var sep=create_at(div, 'div');
sep.style.float='left';
sep.innerHTML=' ';
}
var table=create_at(div, 'table');
table.style.float='left';
var row=[];
row[0]=create_at(table, 'tr');
var head= add_cell(row[0],'player '+(num+1));
head.colSpan='2';
var i=1;
for( var I in con.config)
{
row[i]=create_at(table, 'tr');
add_pair(row[i],I);
i++;
}
function add_pair(R,name)
{
add_cell(R,name);
var cell=add_cell(R, con.config[name]);
cell.style.cursor='pointer';
cell.onclick=function()
{
if( !change_active)
{
change_active=true;
var This=this;
This.style.backgroundColor= "#FAA";
var hold=window.onkeydown;
window.onkeydown=function(e)
{
if (!e) e = window.event;
var keycode=e.keyCode;
var keyname=Fcontroller.keycode_to_keyname(keycode);
window.onkeydown=hold;
cell.innerHTML=keyname;
con.config[name]=keyname;
con.keycode[name]=keycode;
This.style.backgroundColor= "#EEE";
change_active=false;
if( config.onchange)
{
config.onchange(con,name,keyname,keycode);
}
}
}
}
}
}
function create_at(parent, tag, id)
{
var E = document.createElement(tag);
parent.appendChild(E);
if( id)
E.id = id;
return E;
}
function add_cell(row, content)
{
var td = create_at(row, 'td')
td.innerHTML= content;
td.style.border="1px solid #AAA";
td.style.backgroundColor= "#EEE";
td.style.fontFamily="monospace";
td.style.width='40px';
td.style.textAlign='center';
return td;
}
}
return keychanger;
});