1
0
Fork 0
mirror of https://github.com/Reuh/ubiquitousse.git synced 2025-10-27 09:09:30 +00:00
ubiquitousse/docs/modules/signal.html

795 lines
21 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
<title>Ubiquitousse reference</title>
<link rel="stylesheet" href="../ldoc_new.css" type="text/css" />
</head>
<body>
<div id="container">
<div id="product">
<div id="product_logo"></div>
<div id="product_name"><big><b></b></big></div>
<div id="product_description"></div>
</div> <!-- id="product" -->
<div id="main">
<!-- Menu -->
<div id="navigation">
<br/>
<h1>Ubiquitousse</h1>
<ul>
<li><a href="../index.html">Index</a></li>
</ul>
<h2>Contents</h2>
<ul>
<li><a href="#Module">Module </a></li>
<li><a href="#SignalRegistry_objects">SignalRegistry objects </a></li>
<li><a href="#SignalGroup_objects">SignalGroup objects </a></li>
</ul>
<h2>Modules</h2>
<ul class="nowrap">
<li><a href="../modules/ubiquitousse.html">ubiquitousse</a></li>
<li><a href="../modules/asset.html">asset</a></li>
<li><a href="../modules/ecs.html">ecs</a></li>
<li><a href="../modules/input.html">input</a></li>
<li><a href="../modules/ldtk.html">ldtk</a></li>
<li><a href="../modules/scene.html">scene</a></li>
<li><strong>signal</strong></li>
<li><a href="../modules/timer.html">timer</a></li>
<li><a href="../modules/util.html">util</a></li>
</ul>
<h2>Topics</h2>
<ul class="">
<li><a href="../topics/README.md.html">README</a></li>
<li><a href="../topics/LICENSE.html">LICENSE</a></li>
</ul>
</div>
<div id="content">
<h1>Module <code>signal</code></h1>
<p>Simple signal / observer pattern implementation for Lua.</p>
<p>No dependency.
Optional dependency: LÖVE to hook into LÖVE events.</p>
<p>The returned module also acts as a global <a href="../modules/signal.html#SignalRegistry_objects">SignalRegistry</a>, so you can call the <code>:bind</code>, <code>:emit</code>, etc. methods directly on the module
if you don&rsquo;t need to isolate your signals in separate registries.</p>
<h3>Usage:</h3>
<ul>
<pre class="example">local signal = require(&quot;ubiquitousse.signal&quot;)
-- Bind a function to a &quot;hit&quot; signal
signal:bind(&quot;hit&quot;, function(enemy)
print(enemy..&quot; was hit!&quot;)
end)
-- Somewhere else in your code: will call every function bound to &quot;hit&quot; signal with &quot;invader&quot; argument
signal:emit(&quot;hit&quot;, &quot;invader&quot;)
-- We also provides a predefined SignalRegistry (signal.event) which emit signals on LÖVE callbacks
-- You can initialize it with:
signal.registerEvents()
signal.event:bind(&quot;update&quot;, function(dt) print(&quot;called every update&quot;) end)
signal.event:bind(&quot;keypressed&quot;, function(key, scancode) print(&quot;pressed key &quot;..key) end)
-- etc., for every LÖVE callback
</pre>
</ul>
<h2><a href="#Module">Module </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#new">new ()</a></td>
<td class="summary">Creates and return a new SignalRegistry.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#group">group ()</a></td>
<td class="summary">Creates and return a new SignalGroup.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#event">event</a></td>
<td class="summary"><a href="../modules/signal.html#SignalRegistry_objects">SignalRegistry</a> which will be used to bind signals that need to be called on LÖVE events; other ubiquitousse modules may bind to this registry
if avaible.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#registerEvents">registerEvents ()</a></td>
<td class="summary">Call this function to hook <a href="../modules/signal.html#event">signal.event</a> signals to LÖVE events.</td>
</tr>
</table>
<h2><a href="#SignalRegistry_objects">SignalRegistry objects </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#SignalRegistry.signals">SignalRegistry.signals</a></td>
<td class="summary">Map of signals to list of listeners.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry.chained">SignalRegistry.chained</a></td>
<td class="summary">List of registries chained to this registry.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry:bind">SignalRegistry:bind (name, fn)</a></td>
<td class="summary">Bind a function to a signal name.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry:has">SignalRegistry:has (name, fn)</a></td>
<td class="summary">Returns true if fn is bound to the signal.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry:unbind">SignalRegistry:unbind (name, fn)</a></td>
<td class="summary">Unbind a function from a signal name.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry:unbindPattern">SignalRegistry:unbindPattern (pat, fn)</a></td>
<td class="summary">Unbind a function from every signal whose name match the pattern.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry:clear">SignalRegistry:clear (name)</a></td>
<td class="summary">Remove every bound function to a signal name.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry:clearPattern">SignalRegistry:clearPattern (pat)</a></td>
<td class="summary">Remove every bound function to every signal whose name match the pattern.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry:emit">SignalRegistry:emit (name, ...)</a></td>
<td class="summary">Emit a signal, i.e.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry:emitPattern">SignalRegistry:emitPattern (pat, ...)</a></td>
<td class="summary">Emit to every signal whose name match the pattern.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry:chain">SignalRegistry:chain (registry)</a></td>
<td class="summary">Chain another regsitry to this registry.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalRegistry:unchain">SignalRegistry:unchain (registry)</a></td>
<td class="summary">Unchain a specific registry from the registry chaining list.</td>
</tr>
</table>
<h2><a href="#SignalGroup_objects">SignalGroup objects </a></h2>
<table class="function_list">
<tr>
<td class="name" nowrap><a href="#SignalGroup.paused">SignalGroup.paused</a></td>
<td class="summary">Indicate if the signal group if currently paused or not.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalGroup.binds">SignalGroup.binds</a></td>
<td class="summary">List of triplets in the group.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalGroup:bind">SignalGroup:bind (registry, name, fn)</a></td>
<td class="summary">Bind a function to a signal name in the given registry.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalGroup:clear">SignalGroup:clear ()</a></td>
<td class="summary">Remove every bound triplet in the group.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalGroup:pause">SignalGroup:pause ()</a></td>
<td class="summary">Pause the group.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#SignalGroup:resume">SignalGroup:resume ()</a></td>
<td class="summary">Resume the group.</td>
</tr>
</table>
<br/>
<br/>
<h2 class="section-header "><a name="Module"></a>Module </h2>
<dl class="function">
<dt>
<a name = "new"></a>
<strong>new ()</strong>
</dt>
<dd>
Creates and return a new SignalRegistry.
</ul>
</ul>
</ul>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="../modules/signal.html#SignalRegistry_objects">SignalRegistry</a></span>
</ol>
</dd>
<dt>
<a name = "group"></a>
<strong>group ()</strong>
</dt>
<dd>
Creates and return a new SignalGroup.
</ul>
</ul>
</ul>
</ul>
<h3>Returns:</h3>
<ol>
<span class="types"><a class="type" href="../modules/signal.html#SignalGroup_objects">SignalGroup</a></span>
</ol>
</dd>
<dt>
<a name = "event"></a>
<strong>event</strong>
</dt>
<dd>
<a href="../modules/signal.html#SignalRegistry_objects">SignalRegistry</a> which will be used to bind signals that need to be called on LÖVE events; other ubiquitousse modules may bind to this registry
if avaible. </p>
<p> For example, every ubiquitousse module with a &ldquo;update&rdquo; function will bind it to the &ldquo;update&rdquo; signal in the registry;
you can then call this signal on each game update to update every ubiquitousse module easily.</p>
<p> You will need to call <a href="../modules/signal.html#registerEvents">registerEvents</a> for the signal to be called on LÖVE callbacks automatically (otherwise you will have to emit the events
from the LÖVE callbacks manually).</p>
<p> List of signals available: &ldquo;displayrotated&rdquo;, &ldquo;draw&rdquo;, &ldquo;load&rdquo;, &ldquo;lowmemory&rdquo;, &ldquo;quit&rdquo;, &ldquo;update&rdquo;,
&ldquo;directorydropped&rdquo;, &ldquo;filedropped&rdquo;, &ldquo;focus&rdquo;, &ldquo;mousefocus&rdquo;, &ldquo;resize&rdquo;, &ldquo;visible&rdquo;,
&ldquo;keypressed&rdquo;, &ldquo;keyreleased&rdquo;, &ldquo;textedited&rdquo;, &ldquo;textinput&rdquo;,
&ldquo;mousemoved&rdquo;, &ldquo;mousepressed&rdquo;, &ldquo;mousereleased&rdquo;, &ldquo;wheelmoved&rdquo;,
&ldquo;gamepadaxis&rdquo;, &ldquo;gamepadpressed&rdquo;, &ldquo;gamepadreleased&rdquo;,
&ldquo;joystickadded&rdquo;, &ldquo;joystickaxis&rdquo;, &ldquo;joystickhat&rdquo;, &ldquo;joystickpressed&rdquo;, &ldquo;joystickreleased&rdquo;, &ldquo;joystickremoved&rdquo;,
&ldquo;touchmoved&rdquo;, &ldquo;touchpressed&rdquo;, &ldquo;touchreleased&rdquo;.
</ul>
<h3>Type:</h3>
<ul>
<code>SignalRegistry</code>
</ul>
</ul>
</ul>
</dd>
<dt>
<a name = "registerEvents"></a>
<strong>registerEvents ()</strong>
</dt>
<dd>
Call this function to hook <a href="../modules/signal.html#event">signal.event</a> signals to LÖVE events.
This means overriding every existing LÖVE callback. If a callback is already defined, the new one will call the old function along with the signal:emit.
<h3>Requires:</h3>
<ul>
love
</ul>
</ul>
</ul>
</ul>
</dd>
</dl>
<h2 class="section-header has-description"><a name="SignalRegistry_objects"></a>SignalRegistry objects </h2>
<div class="section-description">
Signal registry. </p>
<p> A SignalRegistry is a separate ubiquitousse.signal instance: its signals will be independant from other registries.
</div>
<dl class="function">
<dt>
<a name = "SignalRegistry.signals"></a>
<strong>SignalRegistry.signals</strong>
</dt>
<dd>
Map of signals to list of listeners.
</ul>
<h3>Type:</h3>
<ul>
<code>{["name"]={fn,[fn]=1,...}}</code>
</ul>
</ul>
</ul>
</dd>
<dt>
<a name = "SignalRegistry.chained"></a>
<strong>SignalRegistry.chained</strong>
</dt>
<dd>
List of registries chained to this registry.
</ul>
<h3>Type:</h3>
<ul>
<code>{</code> registry, ... }
</ul>
</ul>
</ul>
</dd>
<dt>
<a name = "SignalRegistry:bind"></a>
<strong>SignalRegistry:bind (name, fn)</strong>
</dt>
<dd>
Bind a function to a signal name.
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">name</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
the name of the signal
</li>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">function</span></span>
the function to bind to the signal
</li>
</ul>
</dd>
<dt>
<a name = "SignalRegistry:has"></a>
<strong>SignalRegistry:has (name, fn)</strong>
</dt>
<dd>
Returns true if fn is bound to the signal.
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">name</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
the name of the signal
</li>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">function</span></span>
the function
</li>
</ul>
</dd>
<dt>
<a name = "SignalRegistry:unbind"></a>
<strong>SignalRegistry:unbind (name, fn)</strong>
</dt>
<dd>
Unbind a function from a signal name.
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">name</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
the name of the signal
</li>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">function</span></span>
the function to unbind to the signal
</li>
</ul>
</dd>
<dt>
<a name = "SignalRegistry:unbindPattern"></a>
<strong>SignalRegistry:unbindPattern (pat, fn)</strong>
</dt>
<dd>
Unbind a function from every signal whose name match the pattern.
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">pat</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
Lua pattern string
</li>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">function</span></span>
the function to unbind to the signals
</li>
</ul>
</dd>
<dt>
<a name = "SignalRegistry:clear"></a>
<strong>SignalRegistry:clear (name)</strong>
</dt>
<dd>
Remove every bound function to a signal name.
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">name</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
the name of the signal
</li>
</ul>
</dd>
<dt>
<a name = "SignalRegistry:clearPattern"></a>
<strong>SignalRegistry:clearPattern (pat)</strong>
</dt>
<dd>
Remove every bound function to every signal whose name match the pattern.
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">pat</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
Lua string pattern
</li>
</ul>
</dd>
<dt>
<a name = "SignalRegistry:emit"></a>
<strong>SignalRegistry:emit (name, ...)</strong>
</dt>
<dd>
Emit a signal, i.e. call every function bound to it, with the given arguments.
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">name</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
the name of the signal
</li>
<li><span class="parameter">...</span>
arguments to pass to the functions bound to this signal
</li>
</ul>
</dd>
<dt>
<a name = "SignalRegistry:emitPattern"></a>
<strong>SignalRegistry:emitPattern (pat, ...)</strong>
</dt>
<dd>
Emit to every signal whose name match the pattern.
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">pat</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
Lua pattern string
</li>
<li><span class="parameter">...</span>
arguments to pass to the functions bound to each signal
</li>
</ul>
</dd>
<dt>
<a name = "SignalRegistry:chain"></a>
<strong>SignalRegistry:chain (registry)</strong>
</dt>
<dd>
Chain another regsitry to this registry.
I.e., after an event is emitted in this registry it will be automatically emitted in the other registry.
Several registries can be chained to a single registry.
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">registry</span>
<span class="types"><a class="type" href="../modules/signal.html#SignalRegistry_objects">SignalRegistry</a></span>
</li>
</ul>
</dd>
<dt>
<a name = "SignalRegistry:unchain"></a>
<strong>SignalRegistry:unchain (registry)</strong>
</dt>
<dd>
Unchain a specific registry from the registry chaining list.
Will error if the regsitry is not in the chaining list.
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">registry</span>
<span class="types"><a class="type" href="../modules/signal.html#SignalRegistry_objects">SignalRegistry</a></span>
</li>
</ul>
</dd>
</dl>
<h2 class="section-header has-description"><a name="SignalGroup_objects"></a>SignalGroup objects </h2>
<div class="section-description">
Signal group. </p>
<p> A SignalGroup is a list of (registry, signal name, function) triplets.
When the group is active, all of these triplets will bind the specified signal name to the specified function in the specified registry.
When the group is paused, all of these triplets are unbound.</p>
<p> This can be used to maintain a list of signal bindings where every one should be either disabled or enabled at the same time.
For example you may maintain a signal group of signals you want to be emitted when your game is running, and disabled when the game is paused
(like inputs, update, simulation step, etc. signals).
</div>
<dl class="function">
<dt>
<a name = "SignalGroup.paused"></a>
<strong>SignalGroup.paused</strong>
</dt>
<dd>
Indicate if the signal group if currently paused or not.
</ul>
<h3>Type:</h3>
<ul>
<code>boolean</code>
</ul>
</ul>
</ul>
</dd>
<dt>
<a name = "SignalGroup.binds"></a>
<strong>SignalGroup.binds</strong>
</dt>
<dd>
List of triplets in the group.
</ul>
<h3>Type:</h3>
<ul>
<code>{</code> {registry, "signal name", function}, ... }
</ul>
</ul>
</ul>
</dd>
<dt>
<a name = "SignalGroup:bind"></a>
<strong>SignalGroup:bind (registry, name, fn)</strong>
</dt>
<dd>
Bind a function to a signal name in the given registry.
This handles binding the function on its own; you do not need to call <a href="../modules/signal.html#SignalRegistry:bind">SignalRegistry:bind</a> manually.
If the group is paused, this will not bind the function immediately but only on the next time this group is resumed (as expected).
</ul>
</ul>
</ul>
</ul>
<h3>Parameters:</h3>
<ul>
<li><span class="parameter">registry</span>
<span class="types"><a class="type" href="../modules/signal.html#SignalRegistry_objects">SignalRegistry</a></span>
to bind the signal in
</li>
<li><span class="parameter">name</span>
<span class="types"><a class="type" href="https://www.lua.org/manual/5.1/manual.html#5.4">string</a></span>
the name of the signal
</li>
<li><span class="parameter">fn</span>
<span class="types"><span class="type">function</span></span>
the function to bind to the signal
</li>
</ul>
</dd>
<dt>
<a name = "SignalGroup:clear"></a>
<strong>SignalGroup:clear ()</strong>
</dt>
<dd>
Remove every bound triplet in the group.
</ul>
</ul>
</ul>
</ul>
</dd>
<dt>
<a name = "SignalGroup:pause"></a>
<strong>SignalGroup:pause ()</strong>
</dt>
<dd>
Pause the group.
The signals bound to this group will be disabled in their given registries.
</ul>
</ul>
</ul>
</ul>
</dd>
<dt>
<a name = "SignalGroup:resume"></a>
<strong>SignalGroup:resume ()</strong>
</dt>
<dd>
Resume the group.
The signals bound to this group will be enabled in their given registries.
</ul>
</ul>
</ul>
</ul>
</dd>
</dl>
</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2022-09-20 13:52:55 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>