Skip to content

Commit 6141188

Browse files
committed
Add 'id' attribute to SoundFX.
1 parent 82d4f1c commit 6141188

File tree

5 files changed

+54
-24
lines changed

5 files changed

+54
-24
lines changed

adventure-editor/src/main/java/com/bladecoder/engineeditor/ui/EditSoundDialog.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.File;
1919
import java.io.FilenameFilter;
2020
import java.util.Arrays;
21+
import java.util.HashMap;
2122

2223
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
2324
import com.bladecoder.engine.actions.Param;
@@ -29,59 +30,64 @@
2930
import com.bladecoder.engineeditor.ui.components.InputPanel;
3031
import com.bladecoder.engineeditor.ui.components.InputPanelFactory;
3132
import com.bladecoder.engineeditor.undo.UndoCreateSound;
33+
import com.bladecoder.engineeditor.utils.ElementUtils;
3234

3335
public class EditSoundDialog extends EditModelDialog<InteractiveActor, SoundFX> {
3436

37+
private InputPanel id;
3538
private InputPanel filename;
3639
private InputPanel loop;
3740
private InputPanel volume;
41+
private InputPanel pan;
3842

3943
public EditSoundDialog(Skin skin, InteractiveActor parent, SoundFX e) {
4044
super(skin);
4145

42-
// id = InputPanelFactory.createInputPanel(skin, "Sound ID", "The id of the sound", true);
46+
id = InputPanelFactory.createInputPanel(skin, "Sound ID", "The id of the sound", true);
4347
filename = InputPanelFactory.createInputPanel(skin, "Filename", "Filename of the sound", getSoundList(), true);
4448
loop = InputPanelFactory.createInputPanel(skin, "Loop", "True if the sound is looping", Param.Type.BOOLEAN,
4549
true, "false");
4650
volume = InputPanelFactory.createInputPanel(skin, "Volume", "Select the volume between 0 and 1", Param.Type.FLOAT, true, "1.0");
51+
pan = InputPanelFactory.createInputPanel(skin, "Pan", "panning in the range -1 (full left) to 1 (full right). 0 is center position", Param.Type.FLOAT, true, "0.0");
4752

4853
setInfo("Actors can have a list of sounds that can be associated to Sprites or played with the 'sound' action");
4954

50-
init(parent, e, new InputPanel[] { filename, loop, volume });
55+
init(parent, e, new InputPanel[] { id, filename, loop, volume, pan });
5156
}
5257

5358
@Override
5459
protected void inputsToModel(boolean create) {
5560

5661
if(create) {
5762
e = new SoundFX();
63+
64+
// UNDO OP
65+
Ctx.project.getUndoStack().add(new UndoCreateSound(parent, e));
66+
} else {
67+
HashMap<String, SoundFX> sounds = parent.getSounds();
68+
sounds.remove(e.getId());
5869
}
5970

71+
String checkedId = parent.getSounds() == null? id.getText():ElementUtils.getCheckedId(id.getText(), parent.getSounds().keySet().toArray(new String[parent.getSounds().size()]));
72+
73+
e.setId(checkedId);
6074
e.setFilename(filename.getText());
6175
e.setLoop(Boolean.parseBoolean(loop.getText()));
6276
e.setVolume(Float.parseFloat(volume.getText()));
77+
e.setPan(Float.parseFloat(pan.getText()));
6378

64-
if(create) {
65-
parent.addSound(e);
66-
67-
// UNDO OP
68-
Ctx.project.getUndoStack().add(new UndoCreateSound(parent, e));
69-
} else {
70-
71-
}
79+
parent.addSound(e);
7280

7381
Ctx.project.setModified();
7482
}
7583

7684
@Override
7785
protected void modelToInputs() {
78-
79-
// TODO SEARCH FOR ID
80-
// parent.getSounds().containsValue(arg0)
81-
// id.setText("");
86+
id.setText(e.getId());
8287
filename.setText(e.getFilename());
8388
loop.setText(Boolean.toString(e.getLoop()));
8489
volume.setText(Float.toString(e.getVolume()));
90+
pan.setText(Float.toString(e.getPan()));
8591
}
8692

8793
private String[] getSoundList() {

adventure-editor/src/main/java/com/bladecoder/engineeditor/ui/SoundList.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected void delete() {
5656

5757
SoundFX s = removeSelected();
5858

59-
parent.getSounds().remove(s.getFilename());
59+
parent.getSounds().remove(s.getId());
6060

6161
// UNDO
6262
Ctx.project.getUndoStack().add(new UndoDeleteSound(parent, s));
@@ -82,6 +82,8 @@ protected void paste() {
8282

8383
list.getItems().insert(pos, newElement);
8484

85+
newElement.setId(ElementUtils.getCheckedId(newElement.getId(), parent.getSounds().keySet().toArray(new String[parent.getSounds().size()])));
86+
8587
parent.addSound(newElement);
8688

8789
list.setSelectedIndex(pos);
@@ -97,7 +99,7 @@ protected void paste() {
9799

98100
@Override
99101
protected String getCellTitle(SoundFX e) {
100-
return e.getFilename();
102+
return e.getId();
101103
}
102104

103105
StringBuilder sb = new StringBuilder();

adventure-editor/src/main/java/com/bladecoder/engineeditor/undo/UndoDeleteSound.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.bladecoder.engine.model.SoundFX;
55
import com.bladecoder.engineeditor.Ctx;
66
import com.bladecoder.engineeditor.model.Project;
7+
import com.bladecoder.engineeditor.utils.ElementUtils;
78

89

910
public class UndoDeleteSound implements UndoOp {
@@ -17,6 +18,7 @@ public UndoDeleteSound(InteractiveActor a, SoundFX s) {
1718

1819
@Override
1920
public void undo() {
21+
s.setId(ElementUtils.getCheckedId(s.getFilename(), a.getSounds().keySet().toArray(new String[a.getSounds().size()])));
2022
a.addSound(s);
2123
Ctx.project.setModified(this, Project.NOTIFY_ELEMENT_CREATED, null, s);
2224
}

blade-engine/src/com/bladecoder/engine/model/InteractiveActor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,18 @@ public void runVerb(String id, String target) {
128128
verbs.runVerb(id, state, target);
129129
}
130130

131-
public void addSound(String id, String filename, boolean loop, float volume) {
131+
public void addSound(String id, String filename, boolean loop, float volume, float pan) {
132132
if (sounds == null)
133133
sounds = new HashMap<String, SoundFX>();
134134

135-
sounds.put(id, new SoundFX(filename, loop, volume));
135+
sounds.put(id, new SoundFX(id, filename, loop, volume, pan));
136136
}
137137

138138
public void addSound(SoundFX s) {
139139
if (sounds == null)
140140
sounds = new HashMap<String, SoundFX>();
141141

142-
sounds.put(s.getFilename(), s);
142+
sounds.put(s.getId(), s);
143143
}
144144

145145
public void playSound(String id) {
@@ -157,7 +157,7 @@ public void playSound(String id) {
157157
s.play();
158158
playingSound = id;
159159
} else {
160-
EngineLogger.debug("Sound Not Found: " + s);
160+
EngineLogger.debug("Sound Not Found: " + id);
161161
}
162162
}
163163

blade-engine/src/com/bladecoder/engine/model/SoundFX.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,30 @@
2121

2222
public class SoundFX implements AssetConsumer {
2323
transient private Sound s;
24+
private String id;
2425
private boolean loop;
2526
private String filename;
2627
private float volume = 1f;
28+
private float pan = 0f;
2729

2830
public SoundFX() {
2931

3032
}
3133

32-
public SoundFX(String filename, boolean loop, float volume) {
33-
this.setFilename(filename);
34+
public SoundFX(String id, String filename, boolean loop, float volume, float pan) {
35+
this.id = id;
36+
this.filename = filename;
3437
this.loop = loop;
35-
this.setVolume(volume);
38+
this.volume = volume;
39+
this.pan = pan;
3640
}
3741

3842
public void play() {
3943
if(s==null)
4044
return;
4145

4246
if(loop) s.loop();
43-
else s.play(getVolume());
47+
else s.play(volume, 1, pan);
4448
}
4549

4650
public void stop() {
@@ -68,6 +72,22 @@ public boolean getLoop() {
6872
return loop;
6973
}
7074

75+
public String getId() {
76+
return id != null? id: filename;
77+
}
78+
79+
public void setId(String id) {
80+
this.id = id;
81+
}
82+
83+
public float getPan() {
84+
return pan;
85+
}
86+
87+
public void setPan(float pan) {
88+
this.pan = pan;
89+
}
90+
7191
public void setLoop(boolean loop) {
7292
this.loop = loop;
7393
}

0 commit comments

Comments
 (0)