mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 06:33:34 +02:00
Renamed generator to dispatcher
This commit is contained in:
@@ -1,22 +1,27 @@
|
|||||||
package schule.ngb.zm.events;
|
package schule.ngb.zm.events;
|
||||||
|
|
||||||
|
import schule.ngb.zm.util.Validator;
|
||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
public class EventGenerator<E, L extends Listener<E>> {
|
public class EventDispatcher<E, L extends Listener<E>> {
|
||||||
|
|
||||||
private CopyOnWriteArraySet<L> listeners;
|
private CopyOnWriteArraySet<L> listeners;
|
||||||
|
|
||||||
private ConcurrentMap<String, BiConsumer<E, L>> eventRegistry;
|
private ConcurrentMap<String, BiConsumer<E, L>> eventRegistry;
|
||||||
|
|
||||||
public EventGenerator() {
|
public EventDispatcher() {
|
||||||
listeners = new CopyOnWriteArraySet<>();
|
listeners = new CopyOnWriteArraySet<>();
|
||||||
eventRegistry = new ConcurrentHashMap<>();
|
eventRegistry = new ConcurrentHashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerEventType( String eventKey, BiConsumer<E, L> dispatcher ) {
|
public void registerEventType( String eventKey, BiConsumer<E, L> dispatcher ) {
|
||||||
|
Validator.requireNotNull(eventKey);
|
||||||
|
Validator.requireNotNull(dispatcher);
|
||||||
|
|
||||||
if( !eventRegistered(eventKey) ) {
|
if( !eventRegistered(eventKey) ) {
|
||||||
eventRegistry.put(eventKey, dispatcher);
|
eventRegistry.put(eventKey, dispatcher);
|
||||||
}
|
}
|
||||||
@@ -39,9 +44,12 @@ public class EventGenerator<E, L extends Listener<E>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void dispatchEvent( String eventKey, final E event ) {
|
public void dispatchEvent( String eventKey, final E event ) {
|
||||||
|
Validator.requireNotNull(eventKey);
|
||||||
|
Validator.requireNotNull(event);
|
||||||
|
|
||||||
if( eventRegistered(eventKey) ) {
|
if( eventRegistered(eventKey) ) {
|
||||||
final BiConsumer<E, L> dispatcher = eventRegistry.get(eventKey);
|
final BiConsumer<E, L> dispatcher = eventRegistry.get(eventKey);
|
||||||
listeners.stream().forEach(( listener ) -> {
|
listeners.forEach(( listener ) -> {
|
||||||
dispatcher.accept(event, listener);
|
dispatcher.accept(event, listener);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,8 @@ package schule.ngb.zm.media;
|
|||||||
*/
|
*/
|
||||||
public interface Audio {
|
public interface Audio {
|
||||||
|
|
||||||
|
String getSource();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prüft, ob das Medium gerade abgespielt wird.
|
* Prüft, ob das Medium gerade abgespielt wird.
|
||||||
*
|
*
|
||||||
|
|||||||
11
src/schule/ngb/zm/media/AudioListener.java
Normal file
11
src/schule/ngb/zm/media/AudioListener.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package schule.ngb.zm.media;
|
||||||
|
|
||||||
|
import schule.ngb.zm.events.Listener;
|
||||||
|
|
||||||
|
public interface AudioListener extends Listener<Audio> {
|
||||||
|
|
||||||
|
void start( Audio source );
|
||||||
|
|
||||||
|
void stop( Audio source );
|
||||||
|
|
||||||
|
}
|
||||||
@@ -41,6 +41,10 @@ public class Mixer implements Audio {
|
|||||||
this.audios = new ArrayList<>(4);
|
this.audios = new ArrayList<>(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSource() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
public void add( Audio pAudio ) {
|
public void add( Audio pAudio ) {
|
||||||
add(pAudio, 1f);
|
add(pAudio, 1f);
|
||||||
}
|
}
|
||||||
@@ -50,6 +54,13 @@ public class Mixer implements Audio {
|
|||||||
pAudio.setVolume(pVolumeFactor * volume);
|
pAudio.setVolume(pVolumeFactor * volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void remove( Audio pAudio ) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAll() {
|
||||||
|
audios.clear();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPlaying() {
|
public boolean isPlaying() {
|
||||||
return audios.stream().anyMatch(aw -> aw.audio.isPlaying());
|
return audios.stream().anyMatch(aw -> aw.audio.isPlaying());
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package schule.ngb.zm.media;
|
package schule.ngb.zm.media;
|
||||||
|
|
||||||
|
import schule.ngb.zm.events.EventDispatcher;
|
||||||
import schule.ngb.zm.tasks.TaskRunner;
|
import schule.ngb.zm.tasks.TaskRunner;
|
||||||
import schule.ngb.zm.util.ResourceStreamProvider;
|
import schule.ngb.zm.util.ResourceStreamProvider;
|
||||||
|
|
||||||
@@ -27,10 +28,29 @@ public class Music implements Audio {
|
|||||||
|
|
||||||
private float volume = 0.8f;
|
private float volume = 0.8f;
|
||||||
|
|
||||||
|
EventDispatcher<Audio, AudioListener> events;
|
||||||
|
{
|
||||||
|
events = new EventDispatcher<>();
|
||||||
|
events.registerEventType("start", (a,l) -> l.start(a));
|
||||||
|
events.registerEventType("stop", (a,l) -> l.stop(a));
|
||||||
|
}
|
||||||
|
|
||||||
public Music( String source ) {
|
public Music( String source ) {
|
||||||
this.audioSource = source;
|
this.audioSource = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSource() {
|
||||||
|
return audioSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener( AudioListener listener ) {
|
||||||
|
events.addListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeListener( AudioListener listener ) {
|
||||||
|
events.removeListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isPlaying() {
|
public boolean isPlaying() {
|
||||||
return playing;
|
return playing;
|
||||||
@@ -119,6 +139,7 @@ public class Music implements Audio {
|
|||||||
private void stream() {
|
private void stream() {
|
||||||
audioLine.start();
|
audioLine.start();
|
||||||
playing = true;
|
playing = true;
|
||||||
|
events.dispatchEvent("start", Music.this);
|
||||||
|
|
||||||
byte[] bytesBuffer = new byte[BUFFER_SIZE];
|
byte[] bytesBuffer = new byte[BUFFER_SIZE];
|
||||||
int bytesRead = -1;
|
int bytesRead = -1;
|
||||||
@@ -146,6 +167,7 @@ public class Music implements Audio {
|
|||||||
|
|
||||||
playing = false;
|
playing = false;
|
||||||
streamingStopped();
|
streamingStopped();
|
||||||
|
events.dispatchEvent("stop", Music.this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean openLine() {
|
private boolean openLine() {
|
||||||
|
|||||||
@@ -25,6 +25,10 @@ public class Sound implements Audio {
|
|||||||
this.audioSource = source;
|
this.audioSource = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSource() {
|
||||||
|
return audioSource;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isPlaying() {
|
public boolean isPlaying() {
|
||||||
// return audioClip != null && audioClip.isRunning();
|
// return audioClip != null && audioClip.isRunning();
|
||||||
return playing;
|
return playing;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class EventGeneratorTest {
|
class EventDispatcherTest {
|
||||||
|
|
||||||
class TestEvent {
|
class TestEvent {
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ class EventGeneratorTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void eventRegistry() {
|
void eventRegistry() {
|
||||||
EventGenerator<TestEvent, TestListener> gen = new EventGenerator<>();
|
EventDispatcher<TestEvent, TestListener> gen = new EventDispatcher<>();
|
||||||
|
|
||||||
gen.registerEventType("start", ( event, listener ) -> listener.startEvent(event));
|
gen.registerEventType("start", ( event, listener ) -> listener.startEvent(event));
|
||||||
gen.registerEventType("stop", ( event, listener ) -> listener.stopEvent(event));
|
gen.registerEventType("stop", ( event, listener ) -> listener.stopEvent(event));
|
||||||
Reference in New Issue
Block a user