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
|
draw_text(refs[j], FONTH / 3,
(FONTH / 4 * 5) * (nd - j - 1) + FONTH / 3, 2);
};
};
// keymap is defined externally in keymap.cfg
struct keym {
int code;
char *name;
char *action;
} keyms[256];
int numkm = 0;
void
keymap(OFString *code, OFString *key, OFString *action)
{
@autoreleasepool {
keyms[numkm].code = (int)code.longLongValue;
keyms[numkm].name = newstring(key.UTF8String);
keyms[numkm++].action = newstringbuf(action.UTF8String);
}
}
COMMAND(keymap, ARG_3STR)
void
bindkey(OFString *key_, OFString *action)
{
@autoreleasepool {
std::unique_ptr<char> key(strdup(key_.UTF8String));
for (char *x = key.get(); *x; x++)
*x = toupper(*x);
loopi(numkm) if (strcmp(keyms[i].name, key.get()) == 0)
{
strcpy_s(keyms[i].action, action.UTF8String);
return;
}
conoutf(@"unknown key \"%s\"", key.get());
}
}
COMMANDN(bind, bindkey, ARG_2STR)
void
saycommand(char *init) // turns input to the command line on or off
{
saycommandon = (init != NULL);
|
>
>
>
>
|
>
>
>
>
>
>
>
>
|
|
|
>
>
|
|
>
<
<
|
<
>
|
>
>
>
>
|
<
<
|
>
|
<
<
|
<
>
>
|
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
|
draw_text(refs[j], FONTH / 3,
(FONTH / 4 * 5) * (nd - j - 1) + FONTH / 3, 2);
};
};
// keymap is defined externally in keymap.cfg
@interface KeyMapping : OFObject
@property (readonly) int code;
@property (readonly, nonatomic) OFString *name;
@property (copy, nonatomic) OFString *action;
- (instancetype)initWithCode:(int)code name:(OFString *)name;
@end
@implementation KeyMapping
- (instancetype)initWithCode:(int)code name:(OFString *)name
{
self = [super init];
_code = code;
_name = [name copy];
return self;
}
@end
static OFMutableArray<KeyMapping *> *keyMappings = nil;
void
keymap(OFString *code, OFString *key, OFString *action)
{
if (keyMappings == nil)
keyMappings = [[OFMutableArray alloc] init];
KeyMapping *mapping =
[[KeyMapping alloc] initWithCode:(int)code.longLongValue name:key];
mapping.action = action;
[keyMappings addObject:mapping];
}
COMMAND(keymap, ARG_3STR)
void
bindkey(OFString *key, OFString *action)
{
for (KeyMapping *mapping in keyMappings) {
if ([mapping.name caseInsensitiveCompare:key] ==
OFOrderedSame) {
mapping.action = action;
return;
}
}
conoutf(@"unknown key \"%@\"", key);
}
COMMANDN(bind, bindkey, ARG_2STR)
void
saycommand(char *init) // turns input to the command line on or off
{
saycommandon = (init != NULL);
|
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
toserver(commandbuf);
};
saycommand(NULL);
} else if (code == SDLK_ESCAPE) {
saycommand(NULL);
};
};
} else if (!menukey(code, isdown)) // keystrokes go to menu
{
loopi(numkm) if (keyms[i].code ==
code) // keystrokes go to game, lookup in
// keymap and execute
{
string temp;
strcpy_s(temp, keyms[i].action);
execute(temp, isdown);
return;
};
};
};
char *
getcurcommand()
{
return saycommandon ? commandbuf : NULL;
};
void
writebinds(FILE *f)
{
loopi(numkm)
{
if (*keyms[i].action)
fprintf(f, "bind \"%s\" [%s]\n", keyms[i].name,
keyms[i].action);
};
};
|
|
>
|
>
|
|
|
<
|
|
|
|
<
<
<
>
>
>
>
<
|
>
|
|
<
|
|
|
<
<
>
|
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
toserver(commandbuf);
};
saycommand(NULL);
} else if (code == SDLK_ESCAPE) {
saycommand(NULL);
};
};
} else if (!menukey(code, isdown)) {
// keystrokes go to menu
for (KeyMapping *mapping in keyMappings) {
if (mapping.code == code) {
// keystrokes go to game, lookup in keymap and
// execute
string temp;
strcpy_s(temp, mapping.action.UTF8String);
execute(temp, isdown);
return;
}
}
}
}
char *
getcurcommand()
{
return saycommandon ? commandbuf : NULL;
}
void
writebinds(OFStream *stream)
{
for (KeyMapping *mapping in keyMappings)
if (mapping.action.length > 0)
[stream writeFormat:@"bind \"%@\" [%@]\n", mapping.name,
mapping.action];
}
|