Skip to content

Commit

Permalink
Merge pull request #100 from avivbeeri/allKeys
Browse files Browse the repository at this point in the history
Added allPressed and reset options to the input module
  • Loading branch information
avivbeeri authored Oct 14, 2020
2 parents 78318f7 + cd5d877 commit 03ed70e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
29 changes: 24 additions & 5 deletions docs/modules/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ It contains the following classes:

An instance of `DigitalInput` represents an input such as a keyboard key, mouse button or controller button, which can be either "pressed" or "unpressed".

### Instance Methods
#### `repeat(): Void`
Causes the current state of the input to be repeated, which can affect the `justPressed` property. This is useful in certain scenarios where an input's state may be tested repeatedly, but before the user has had a chance to release the input.

#### `reset(): Void`
Resets the input state, as if the input was just set to no longer be down. This is useful in certain scenarios where an input's state may be tested repeatedly, but before the user has had a chance to release the input.

### Instance Fields

#### `static down: Boolean`
Expand All @@ -32,6 +39,11 @@ This counts the number of ticks that an input has been engaged for. If the input

## Keyboard

### Static Fields

#### `static allPressed: Map<string, DigitalInput>`
This returns a map containing the key names and corresponding `DigitalInput` objects, for all keys which are currently "down".

### Static Methods

#### `static [name]: DigitalInput`
Expand All @@ -45,18 +57,21 @@ Returns true if the named key is pressed. The key uses the SDL key name, which c

### Static Fields

#### `static x: Number`
The x position relative to the Canvas. This accounts for the window being resized and the viewport moving. If `Mouse.relative` is set, this will be the relative change of the mouse x position since the previous tick.

#### `static y: Number`
The y position relative to the Canvas. This accounts for the window being resized and the viewport moving. If `Mouse.relative` is set, this will be the relative change of the mouse y position since the previous tick.
#### `static allPressed: Map<string, DigitalInput>`
This returns a map containing the key names and corresponding `DigitalInput` objects, for all keys which are currently "down".

#### `static hidden: Boolean`
Controls whether the mouse cursor is shown or hidden. You can set and read from this field.

#### `static relative: Boolean`
If set to true, the mouse is placed into relative mode. In this mode, the mouse will be fixed to the center of the screen. You can set and read from this field. This changes the behaviour of the `x` and `y` fields.

#### `static x: Number`
The x position relative to the Canvas. This accounts for the window being resized and the viewport moving. If `Mouse.relative` is set, this will be the relative change of the mouse x position since the previous tick.

#### `static y: Number`
The y position relative to the Canvas. This accounts for the window being resized and the viewport moving. If `Mouse.relative` is set, this will be the relative change of the mouse y position since the previous tick.

### Static Methods

#### `static [name]: DigitalInput`
Expand Down Expand Up @@ -91,6 +106,10 @@ This will return an object representing a GamePad. You can then read the state o
If no gamepads are attached, you will receive a "dummy" object which will report null or empty values.

### Instance Fields

#### `allPressed: Map<string, DigitalInput>`
This returns a map containing the key names and corresponding `DigitalInput` objects, for all keys which are currently "down".

#### `attached: Boolean`
This returns true if the gamepad is still attached to the system.
#### `id: Number`
Expand Down
26 changes: 26 additions & 0 deletions src/modules/input.wren
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ class DigitalInput {
_current = state
}

reset() {
_down = false
commit()
}
repeat() {
commit()
}

down { _down }
previous { _previous }
repeats { _repeats }
justPressed { _down && _repeats == 0 }
}


class Keyboard {

static isButtonPressed(key) { isKeyDown(key) }
Expand All @@ -51,6 +60,12 @@ class Keyboard {
return __keys[name]
}

static allPressed {
var pressed = {}
__keys.keys.where {|key| __keys[key].down }.each {|key| pressed[key] = __keys[key] }
return pressed
}

// PRIVATE, called by game loop
static update(keyName, state) {
if (!__keys.containsKey(keyName)) {
Expand Down Expand Up @@ -88,6 +103,11 @@ class Mouse {
}
return __buttons[name]
}
static allPressed {
var pressed = {}
__buttons.keys.where {|key| __buttons[key].down }.each {|key| pressed[key] = __buttons[key] }
return pressed
}

// PRIVATE, called by game loop
static update(keyName, state) {
Expand Down Expand Up @@ -145,6 +165,12 @@ class GamePad {
return _buttons[name]
}

allPressed {
var pressed = {}
__buttons.keys.where {|key| __buttons[key].down }.each {|key| pressed[key] = __buttons[key] }
return pressed
}

rumble(strength, length) {
_pad.rumble(strength, length)
}
Expand Down
32 changes: 19 additions & 13 deletions src/util/embed.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//Using SDL and standard IO
//Using standard IO only
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -39,31 +39,37 @@ int main(int argc, char* args[])
printf("./embed [WrenFile] [VariableName] [OutputFile]\n");
printf("Not enough arguments.\n");
return EXIT_FAILURE;
}
}
size_t length;
char* fileToConvert = readEntireFile(args[1], &length);
char* moduleName = args[2];
FILE *fp;
fp = fopen(args[3], "w+");
fputs("// auto-generated file, do not modify\n", fp);
fputs("const char* ", fp);
fputs("const char ", fp);
fputs(moduleName, fp);
fputs(" = \"", fp);
fputs("[", fp);
fprintf(fp, "%li", length + 1);
fputs("] = {", fp);
for (size_t i = 0; i < length; i++ ) {
char* ptr = fileToConvert + i;
if (*ptr == '\"') {
fputs("\\\"", fp);
} else if (*ptr == '\n') {
fputs("\\n\"", fp);
if (*ptr == '\n') {
fputs("'\\n',", fp);
fputs("\n", fp);
fputs("\"", fp);
} else {
fwrite(ptr, sizeof(char), 1, fp);
fputs("'", fp);
if (*ptr == '\'') {
fputs("\\\'", fp);
} else {
fwrite(ptr, sizeof(char), 1, fp);
}
fputs("', ", fp);
}
}

fputs("\";\n", fp);

fputs("\0", fp);

fputs(" };\n", fp);

fclose(fp);
free(fileToConvert);
return EXIT_SUCCESS;
Expand Down

0 comments on commit 03ed70e

Please sign in to comment.