Commit 2220bbb8 authored by Eric Tobias's avatar Eric Tobias
Browse files

Merge branch 'dev' of git.list.lu:nui/TULIP-Scenario into dev

parents b9e3561e 7e196c62
......@@ -5,6 +5,7 @@
<name>Background</name>
<image>assets/checkered_tablecloth.jpg</image>
<triggerCondition>true</triggerCondition>
<drawPriority>1</drawPriority>
</scene>
</scenes>
......
......@@ -37,10 +37,18 @@ import org.jdom2.Element;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedHashSet;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
/**
......@@ -62,6 +70,9 @@ public class Scene extends Content {
private BooleanVariable triggerVariable = new BooleanVariable("Trigger", false); //$NON-NLS-1$
/** The image to show when painting. */
private ImageIcon scenery;
/** The sound to play when painting. */
private Clip audioClip;
private boolean triggered = false;
/**
* Constructor initializing all fields.
......@@ -72,6 +83,12 @@ public class Scene extends Content {
public Scene(Element element) throws BuildException {
super(element.getChildText(Externalization.NAME_NODE));
try {
drawPriority = Integer.parseInt(element.getChildText(Externalization.DRAW_PRIORITY_NODE));
} catch (final NumberFormatException e) {
}
final String triggerCondition = element.getChildText(Externalization.TRIGGER_CONDITION_NODE);
for (final String _name : Splitter.on(Externalization.SPACE).trimResults().omitEmptyStrings().split(triggerCondition)) {
......@@ -101,6 +118,34 @@ public class Scene extends Content {
} catch (final IOException ioe) {
throw new BuildException("The image could not be read!", ioe); //$NON-NLS-1$
}
// open the sound file as a Java input stream
try {
final Element soundNode = element.getChild(Externalization.SOUND_NODE);
if ((null != soundNode) && (soundNode.getText() != "")) {
final File audioFile = new File(soundNode.getText());
final AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
final AudioFormat format = audioStream.getFormat();
final DataLine.Info info = new DataLine.Info(Clip.class, format);
audioClip = (Clip) AudioSystem.getLine(info);
audioClip.open(audioStream);
}
}
catch (final FileNotFoundException e) {
throw new BuildException("The file could not be found!", e); //$NON-NLS-1$
} catch (final IOException e) {
throw new BuildException("The sound could not be read!", e); //$NON-NLS-1$
} catch (final UnsupportedAudioFileException e) {
throw new BuildException("The sound format is not supported!", e); //$NON-NLS-1$
} catch (final LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
......@@ -121,6 +166,11 @@ public class Scene extends Content {
*/
public boolean evaluateTrigger() {
trigger.evaluate();
if (!triggerVariable.getValue() && triggered) {
triggered = false;
}
return triggerVariable.getValue();
}
......@@ -128,7 +178,16 @@ public class Scene extends Content {
@Override
public void paint(Graphics2D canvas) {
// canvas.drawImage(scenery, 0, 0, null);
scenery.paintIcon(null, canvas, 0, 0);
if (scenery != null) {
scenery.paintIcon(null, canvas, 0, 0);
}
if ((!triggered) && (audioClip != null)) {
audioClip.setFramePosition(0);
audioClip.start();
triggered = true;
}
}
......@@ -137,4 +196,7 @@ public class Scene extends Content {
public void initialise() {
// Nothing to do here!
}
}
\ No newline at end of file
......@@ -25,9 +25,10 @@ import lu.list.itis.dkd.tui.TangibleContentManager;
import lu.list.itis.dkd.tui.content.Content;
import lu.list.itis.dkd.tui.event.ContentEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
......@@ -42,6 +43,7 @@ public class StageManager extends TangibleContentManager {
private static final StageManager INSTANCE = new StageManager();
private static Map<String, Scene> scenes;
private static ScriptEngine scriptEngine;
ArrayList<Content> drawableContents = new ArrayList<>();
private StageManager() {
scriptEngine = new ScriptEngineManager().getEngineByName("js"); //$NON-NLS-1$
......@@ -63,6 +65,7 @@ public class StageManager extends TangibleContentManager {
scenes = _scenes;
scenes.values().forEach(scene -> scene.getTrigger().setScriptEngine(scriptEngine));
}
/** {@inheritDoc} */
......@@ -76,6 +79,16 @@ public class StageManager extends TangibleContentManager {
/** {@inheritDoc} */
@Override
public Collection<Content> getDrawableContents() {
return scenes.values().stream().filter(scene -> scene.evaluateTrigger()).collect(Collectors.toList());
drawableContents.clear();
for (final Scene scene : scenes.values()) {
if (scene.evaluateTrigger()) {
drawableContents.add(scene);
}
}
Collections.sort(drawableContents);
return drawableContents;
}
}
\ No newline at end of file
......@@ -32,8 +32,8 @@ import org.jdom2.input.SAXBuilder;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -48,7 +48,7 @@ public class SceneBootstrapper {
private static final Logger logger = Logger.getLogger(SceneBootstrapper.class.getSimpleName());
private static Map<String, Scene> scenes = new HashMap<>();
private static Map<String, Scene> scenes = new TreeMap<>();
private static Document scenarioDescription;
/**
......@@ -102,6 +102,7 @@ public class SceneBootstrapper {
try {
final Scene scene = new Scene(element);
scenes.put(scene.getIdentifier(), scene);
} catch (final BuildException e) {
Logger.getLogger(SceneBootstrapper.class.getSimpleName()).log(Level.SEVERE, "Could not build scene!", e); //$NON-NLS-1$
}
......
......@@ -398,7 +398,6 @@
</variable>
<modifyValueOnRotation>True</modifyValueOnRotation>
</object>
<object>
<type>ValueWidget</type>
<handles>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment