Skip to content

Commit 73aa650

Browse files
authored
Merge pull request #40 from ChapelR/v2.6.1
v2.6.1
2 parents c363249 + 37438eb commit 73aa650

28 files changed

+184
-100
lines changed

docs/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Chapel's Custom Macro Collection (v2.6.0)
1+
## Chapel's Custom Macro Collection (v2.6.1)
22

33
- [Try the demo!](https://macros.twinelab.net/demo) ([Sausage](https://github.com/ChapelR/custom-macros-demo))
44
- [Downloads](./download ':ignore')
@@ -8,19 +8,19 @@
88
### Documentation
99
- **Gameplay Systems and Mechanics**
1010
- [The Simple Inventory System](simple-inventory.md)
11-
- Updated [The Cycles System](cycles-system.md)
11+
- [The Cycles System](cycles-system.md)
1212
- [The Playtime System](playtime-system.md)
1313
- **Interface and Style**
1414
- [The Dialog API Macro Set](dialog-api-macro-set.md)
15-
- New [The Popover Macro](popover.md)
15+
- [The Popover Macro](popover.md)
1616
- [The UI Macro](ui-macro.md)
1717
- [The Fading Macro Set](fading-macros.md)
1818
- [The CSS Macro](css-macro.md)
1919
- [The Notify Macro](notify-macro.md)
2020
- [The Meter Macro Set](meter-macros.md)
2121
- [The Speech Box System](speech-box-system.md)
2222
- **User Interaction and Events**
23-
- [The Event Macros](event-macros.md)
23+
- *Updated!* [The Event Macros](event-macros.md)
2424
- [The Continue Macro Set](continue-macro.md)
2525
- [The Swap Macro Set](swap-macro-set.md)
2626
- [The Mouseover Macro](mouseover-macro.md)
@@ -30,7 +30,7 @@
3030
- [The Pronoun Templates](pronoun-templates.md)
3131
- [The Articles (A/An) Macros](articles.md)
3232
- **Utilities and Other**
33-
- New [The Preload Macro](preload.md)
33+
- [The Preload Macro](preload.md)
3434
- [The Done Macro](done-macro.md)
3535
- [The File System Macro Set](file-system-macros.md)
3636
- [The First Macro](first-macro.md)

docs/changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
[Back to the main page](./README.md).
44

5+
### August 2, 2020 (v2.6.1)
6+
7+
- **[Update]** Updated the event macro set.
8+
- Added `<<on>>`,`<<one>>`, and `<<off>>`. Deprecated `<<event>>`.
9+
- Support for single-use event handlers.
10+
- Added default namespaces.
11+
- Internal improvements.
12+
513
### July 24, 2020 (v2.6.0)
614

715
- **[New]** New macros.

docs/event-macros.md

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
[Back to the main readme](./README.md).
44

5-
This macro set allows Twine authors to create event programming without needing to use JavaScript or jQuery.
5+
This macro set allows Twine authors to create event handlers without needing to use JavaScript or jQuery.
66

77
**THE CODE:** [Minified](https://github.com/ChapelR/custom-macros-for-sugarcube-2/blob/master/scripts/minified/events.min.js). [Pretty](https://github.com/ChapelR/custom-macros-for-sugarcube-2/blob/master/scripts/events.js).
88
**DEMO:** [Available](http://macros.twinelab.net/demo?macro=event).
99
**GUIDE:** Not available.
1010

11-
### Macro: `<<event>>`
11+
### Macros: `<<on>>` and `<<one>>`
1212

1313
**Syntax**:
1414
```
15-
<<event type [selector]>>
15+
<<on type [selector] [once]>>
1616
...
1717
<<which keycode>>
1818
...
@@ -21,27 +21,33 @@ This macro set allows Twine authors to create event programming without needing
2121
<</event>>
2222
```
2323

24-
This macro set can be used to add more interaction to your game; things like keyboard hotkeys, controls, clickable non-link elements, and more. Once registered, events are essentially permanent (though they can be removed via JavaScript and suppressed via code logic); therefore, **the best place to create events is your StoryInit special passage**. Note that the element the event is tied to does not need to be rendered (or currently on the page or in the passage) in order to attach an event to it.
24+
This macro be used to handle events in your game; things like keyboard hotkeys, controls, clickable non-link elements, and more. Note that the element the event is tied to does not need to be rendered (or currently on the page or in the passage) in order to attach an event to it.
2525

26-
* **type**: a valid jQuery event type. Some events that may be of interest:
26+
This macro has three aliases: `<<on>>` set recurring event handlers, while `<<one>>` creates a single-use event handler. If you want the handler to run each and every time the event occurs, use `<<on>>`. If you want the event to occur only once, the next time the event occurs, use `<<one>>`.
27+
28+
> [!NOTE]
29+
> The `<<event>>` macro exists as an alias for `<<on>>` for backwards-compatibility with code written for older version of this macro set. The `<<event>>` macro should be considered deprecated going forward.
30+
31+
* **type**: a valid jQuery event type. You may include a namespace with a dot, e.g., `click.my-namespace`. Some events that may be of interest:
2732
* `click`: fires when an element is clicked on.
2833
* `dblclick`: fires when an element is double-clicked on.
2934
* `keyup`: fires when an key is pressed and released.
3035
* `keydown`: fires immediately when a key is pressed.
3136
* `mouseup`: fires when a mouse button is pressed and released.
3237
* `mousedown`: fires when a mouse button is pressed.
3338
* **selector**: (optional) a valid jQuery/CSS selector. with some events (such as key presses), this checks for focus; with others it checks for the target element of the event (such as mouse clicks). if no selector is provided, the event is bound to the document element.
39+
* **once**: (optional) the keyword `once`. If included, overrides the normal behavior of `<<on>>` (and `<<event>>`) to create a single-use event handler.
3440
* **keycode**: an integer. allows you to determine which key or mouse button triggered the event and react accordingly. you can find keycodes [here](http://keycode.info/).
3541

3642
**Usage**:
3743
```
3844
/% stow/unstow the ui-bar on double-click %/
39-
<<event 'dblclick' '#ui-bar'>>
45+
<<on 'dblclick' '#ui-bar'>>
4046
<<toggleclass '#ui-bar' 'stowed'>>
41-
<</event>>
47+
<</on>>
4248
4349
/% set up some hotkeys %/
44-
<<event 'keyup'>>
50+
<<on 'keyup'>>
4551
<<which 67>> /% the c key %/
4652
<<if not tags().includes('menu')>> /% avoid menu loop %/
4753
<<goto 'character-menu'>>
@@ -52,17 +58,27 @@ This macro set can be used to add more interaction to your game; things like key
5258
<</if>>
5359
<<which 77>> /% the m key %/
5460
<<masteraudio mute>>
55-
<</event>>
61+
<</on>>
62+
63+
/% run one time %/
64+
<<one ':dialogclosed'>>
65+
<<run UI.alert("You closed a dialog!")>>
66+
<</one>>
67+
68+
/% the above could also be written: %/
69+
<<on ':dialogclosed' once>>
70+
<<run UI.alert("You closed a dialog!")>>
71+
<</on>>
5672
```
5773

5874
### Macro: `<<trigger>>`
5975

60-
**Syntax**:`<<trigger (type) (optional: selector)>>`
76+
**Syntax**:`<<trigger type [selector]>>`
6177

6278
Allows you to simulate any event on any element. This macro is useful for triggering events you may not otherwise have easy access to.
6379

6480
* **type**: a valid jQuery event type
65-
* **selector**: a valid jQuery/CSS selector. if omitted, defaults to the document element
81+
* **selector**: (optional) a valid jQuery/CSS selector. if omitted, defaults to the document element
6682

6783
**Usage**:
6884
```
@@ -71,4 +87,27 @@ Allows you to simulate any event on any element. This macro is useful for trigg
7187
<<which 27>>
7288
<<trigger 'click' '#ui-dialog-close'>>
7389
<</event>>
74-
```
90+
```
91+
92+
### Macro: `<<off>>`
93+
94+
**Syntax**:`<<off type [selector]>>`
95+
96+
Allows you to remove an event handler.
97+
98+
* **type**: a valid jQuery event type; may include namespaces
99+
* **selector**: (optional) a valid jQuery/CSS selector. if omitted, defaults to the document element
100+
101+
**Usage**:
102+
103+
```
104+
/% removes all events created through this macro set %/
105+
<<off '.macro-event'>>
106+
107+
/% remove all `dblclick` handlers from the `#ui-bar` element %/
108+
<<off 'dblclick' '#ui-bar'>>
109+
```
110+
111+
### Setting: `setup.eventMacroNamespace`
112+
113+
Handlers set up via this macro set are given a namespace automatically. The default value of this name space is `"macro-event"`. You may change this value to change the namespace if you want. Omit the dot.

scripts/events.js

Lines changed: 95 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,102 @@
11
// event macro set, by chapel; for sugarcube 2
2-
// version 1.1.1
2+
// version 2.0.0
33

4-
// the <<trigger>> macro
5-
Macro.add('trigger', {
6-
handler : function () {
7-
8-
// declare vars
9-
var evt, $el;
10-
11-
// check for errors
12-
if (this.args.length > 2 || this.args.length === 0) {
13-
return this.error('incorrect number of arguments');
14-
}
15-
if (typeof this.args[0] != 'string') {
16-
return this.error('first argument should be a string and a valid event type');
17-
}
18-
19-
// some setup
20-
evt = this.args[0];
21-
$el = (this.args.length === 1 ||
22-
(this.args[1] && typeof this.args[1] === 'string' &&
23-
this.args[1].toLowerCase().trim() === 'document')) ?
24-
$(document) : $(this.args[1]);
25-
26-
// fire the event
27-
$el.trigger(evt);
28-
29-
}
30-
});
4+
(function () {
5+
setup.eventMacroNamespace = 'macro-event';
316

32-
// the <<event>> macro
33-
Macro.add('event', {
34-
tags : ['which'],
35-
handler : function () {
36-
37-
var payload = this.payload;
38-
var evt, sel = '', code = '', i;
39-
40-
if (this.args.length > 2 || this.args.length === 0) {
41-
return this.error('incorrect number of arguments');
42-
}
43-
if (typeof this.args[0] != 'string') {
44-
return this.error('first argument should be a string and a valid event type');
45-
}
46-
if (this.args.length === 2 && typeof this.args[1] == 'string') {
47-
sel = this.args[1];
7+
// the <<trigger>> macro
8+
Macro.add('trigger', {
9+
handler : function () {
10+
11+
// declare vars
12+
var evt, $el;
13+
14+
// check for errors
15+
if (this.args.length > 2 || this.args.length === 0) {
16+
return this.error('incorrect number of arguments');
17+
}
18+
if (typeof this.args[0] != 'string') {
19+
return this.error('first argument should be a string and a valid event type');
20+
}
21+
22+
// some setup
23+
evt = this.args[0];
24+
$el = (this.args.length === 1 ||
25+
(this.args[1] && typeof this.args[1] === 'string' &&
26+
this.args[1].toLowerCase().trim() === 'document')) ?
27+
$(document) : $(this.args[1]);
28+
29+
// fire the event
30+
$el.trigger(evt);
31+
4832
}
49-
50-
evt = this.args[0];
51-
52-
$(document).on(evt, sel, function (e) {
53-
code = payload[0].contents;
54-
if (payload.length > 1) {
55-
for (i = 1; i < payload.length; i++) {
56-
if (payload[i].args.includes(e.which)) {
57-
code = code + payload[i].contents;
33+
});
34+
35+
// the <<event>> macro: <<event type [selector] [once]>>
36+
Macro.add(['event', 'on', 'one'], {
37+
tags : ['which'],
38+
handler : function () {
39+
40+
var payload = this.payload;
41+
var method = 'on';
42+
var evt, sel = '', code = '', i;
43+
44+
if (this.args.length > 3 || this.args.length === 0) {
45+
return this.error('incorrect number of arguments');
46+
}
47+
if (typeof this.args[0] != 'string') {
48+
return this.error('first argument should be a string and a valid event type');
49+
}
50+
if (this.args.length === 2 && typeof this.args[1] == 'string' && this.args[1] !== 'once') {
51+
sel = this.args[1];
52+
}
53+
54+
if (this.args.includes('once') || this.name === 'one') {
55+
method = 'one';
56+
}
57+
58+
evt = this.args[0];
59+
60+
$(document)[method](evt + '.' + setup.eventMacroNamespace, sel, function (e) {
61+
code = payload[0].contents;
62+
if (payload.length > 1) {
63+
for (i = 1; i < payload.length; i++) {
64+
if (payload[i].args.includes(e.which)) {
65+
code = code + payload[i].contents;
66+
}
5867
}
5968
}
69+
new Wikifier(null, code);
70+
});
71+
72+
}
73+
});
74+
75+
Macro.add('off', {
76+
handler : function () {
77+
78+
// declare vars
79+
var evt, $el;
80+
81+
// check for errors
82+
if (this.args.length > 2 || this.args.length === 0) {
83+
return this.error('incorrect number of arguments');
84+
}
85+
if (typeof this.args[0] != 'string') {
86+
return this.error('first argument should be a string and a valid event type or namespace');
6087
}
61-
new Wikifier(null, code);
62-
});
63-
64-
}
65-
});
88+
89+
// some setup
90+
evt = this.args[0];
91+
$el = (this.args.length === 1 ||
92+
(this.args[1] && typeof this.args[1] === 'string' &&
93+
this.args[1].toLowerCase().trim() === 'document')) ?
94+
$(document) : $(this.args[1]);
95+
96+
// fire the event
97+
$el.off(evt);
98+
99+
}
100+
});
101+
102+
}());

scripts/minified/articles.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/minified/continue.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/minified/css-macro.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)