mirror of
https://github.com/OMGeeky/ATCS.git
synced 2026-01-07 03:56:55 +01:00
Initial commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.model.gamedata.Droplist;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
public class ContainerArea extends MapObject {
|
||||
|
||||
public Droplist droplist = null;
|
||||
|
||||
public ContainerArea(tiled.core.MapObject obj) {}
|
||||
|
||||
@Override
|
||||
public void link() {
|
||||
droplist = parentMap.getProject().getDroplist(name);
|
||||
if (droplist != null) {
|
||||
droplist.addBacklink(parentMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
if (droplist != null) return DefaultIcons.getContainerIcon();
|
||||
else return DefaultIcons.getNullifyIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
if (oldOne == droplist) {
|
||||
droplist = (Droplist) newOne;
|
||||
newOne.addBacklink(parentMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePropertiesInTmxObject(tiled.core.MapObject tmxObject) {
|
||||
if (droplist != null) {
|
||||
tmxObject.setName(droplist.id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
84
src/com/gpl/rpg/atcontentstudio/model/maps/KeyArea.java
Normal file
84
src/com/gpl/rpg/atcontentstudio/model/maps/KeyArea.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.model.gamedata.Dialogue;
|
||||
import com.gpl.rpg.atcontentstudio.model.gamedata.Requirement;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
public class KeyArea extends MapObject {
|
||||
|
||||
public String dialogue_id = null;
|
||||
public Dialogue dialogue = null;
|
||||
public Requirement requirement = null;
|
||||
|
||||
public KeyArea(tiled.core.MapObject obj) {
|
||||
dialogue_id = obj.getProperties().getProperty("phrase");
|
||||
String requireType = obj.getProperties().getProperty("requireType");
|
||||
String requireId = obj.getProperties().getProperty("requireId");
|
||||
String requireValue = obj.getProperties().getProperty("requireValue");
|
||||
if (requireId == null) {
|
||||
String[] fields = obj.getName().split(":");
|
||||
if (fields.length == 2) {
|
||||
requireType = Requirement.RequirementType.questProgress.toString();
|
||||
requireValue = fields[1];
|
||||
requireId = fields[0];
|
||||
} else if (fields.length == 3) {
|
||||
requireValue = fields[2];
|
||||
requireType = fields[0];
|
||||
requireId = fields[1];
|
||||
}
|
||||
}
|
||||
requirement = new Requirement();
|
||||
requirement.type = Requirement.RequirementType.valueOf(requireType);
|
||||
requirement.required_obj_id = requireId;
|
||||
if (requireValue != null) requirement.required_value = Integer.parseInt(requireValue);
|
||||
requirement.state = GameDataElement.State.parsed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void link() {
|
||||
if (dialogue_id != null) dialogue = parentMap.getProject().getDialogue(dialogue_id);
|
||||
if (dialogue != null) {
|
||||
dialogue.addBacklink(parentMap);
|
||||
}
|
||||
requirement.parent = parentMap;
|
||||
requirement.link();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
if (dialogue != null && requirement != null) return DefaultIcons.getKeyIcon();
|
||||
return DefaultIcons.getNullifyIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
if (oldOne == dialogue) {
|
||||
dialogue = (Dialogue) newOne;
|
||||
newOne.addBacklink(parentMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePropertiesInTmxObject(tiled.core.MapObject tmxObject) {
|
||||
if (dialogue != null) {
|
||||
tmxObject.getProperties().setProperty("phrase", dialogue.id);
|
||||
} else if (dialogue_id != null) {
|
||||
tmxObject.getProperties().setProperty("phrase", dialogue_id);
|
||||
}
|
||||
if (requirement != null) {
|
||||
tmxObject.getProperties().setProperty("requireType", requirement.type.toString());
|
||||
if (requirement.required_obj != null) {
|
||||
tmxObject.getProperties().setProperty("requireId", requirement.required_obj.id);
|
||||
} else if (requirement.required_obj_id != null) {
|
||||
tmxObject.getProperties().setProperty("requireId", requirement.required_obj_id);
|
||||
}
|
||||
if (requirement.required_value != null) {
|
||||
tmxObject.getProperties().setProperty("requireValue", requirement.required_value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
57
src/com/gpl/rpg/atcontentstudio/model/maps/MapChange.java
Normal file
57
src/com/gpl/rpg/atcontentstudio/model/maps/MapChange.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
|
||||
|
||||
public class MapChange extends MapObject {
|
||||
|
||||
public String map_id = null;
|
||||
public TMXMap map = null;
|
||||
public String place_id = null;
|
||||
|
||||
public MapChange(tiled.core.MapObject obj) {
|
||||
this.map_id = obj.getProperties().getProperty("map");
|
||||
this.place_id = obj.getProperties().getProperty("place");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void link() {
|
||||
if (map_id != null) this.map = parentMap.getProject().getMap(map_id);
|
||||
if (map != null) {
|
||||
map.addBacklink(parentMap);
|
||||
}
|
||||
//TODO reinstate this if data validation system ever exist.
|
||||
// else Notification.addWarn("Incomplete mapchange area \""+name+"\" in map \""+parentMap.id+"\". This is OK if it's an arrival only (no exit through this point).");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
if (name != null) return DefaultIcons.getTiledIconIcon();
|
||||
else return DefaultIcons.getNullifyIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
if (oldOne == map) {
|
||||
map = (TMXMap) newOne;
|
||||
newOne.addBacklink(parentMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePropertiesInTmxObject(tiled.core.MapObject tmxObject) {
|
||||
if (map != null) {
|
||||
tmxObject.getProperties().setProperty("map", map.id);
|
||||
} else if (map_id != null) {
|
||||
tmxObject.getProperties().setProperty("map", map_id);
|
||||
}
|
||||
if (place_id != null) {
|
||||
tmxObject.getProperties().setProperty("place", place_id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
163
src/com/gpl/rpg/atcontentstudio/model/maps/MapObject.java
Normal file
163
src/com/gpl/rpg/atcontentstudio/model/maps/MapObject.java
Normal file
@@ -0,0 +1,163 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.Notification;
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
|
||||
public abstract class MapObject {
|
||||
|
||||
public int x, y, w, h;
|
||||
public String name;
|
||||
|
||||
public TMXMap parentMap;
|
||||
|
||||
public Types type;
|
||||
|
||||
protected static enum Types {
|
||||
mapchange,
|
||||
spawn,
|
||||
rest,
|
||||
key,
|
||||
replace,
|
||||
script,
|
||||
container,
|
||||
sign
|
||||
}
|
||||
|
||||
|
||||
public static MapObject buildObject(tiled.core.MapObject obj, TMXMap parentMap) {
|
||||
MapObject result = null;
|
||||
if (obj.getType() != null && !obj.getType().equals("") && Types.valueOf(obj.getType()) != null) {
|
||||
switch (Types.valueOf(obj.getType())) {
|
||||
case key:
|
||||
result = new KeyArea(obj);
|
||||
result.type = Types.key;
|
||||
break;
|
||||
case mapchange:
|
||||
result = new MapChange(obj);
|
||||
result.type = Types.mapchange;
|
||||
break;
|
||||
case replace:
|
||||
result = new ReplaceArea(obj);
|
||||
result.type = Types.replace;
|
||||
break;
|
||||
case rest:
|
||||
result = new RestArea(obj);
|
||||
result.type = Types.rest;
|
||||
break;
|
||||
case script:
|
||||
result = new ScriptArea(obj);
|
||||
result.type = Types.script;
|
||||
break;
|
||||
case sign:
|
||||
result = new SignArea(obj);
|
||||
result.type = Types.sign;
|
||||
break;
|
||||
case spawn:
|
||||
result = new SpawnArea(obj);
|
||||
result.type = Types.spawn;
|
||||
break;
|
||||
case container:
|
||||
result = new ContainerArea(obj);
|
||||
result.type = Types.container;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
Notification.addWarn("Unknown map object type: "+obj.getType()+"with name "+obj.getName()+" in map "+parentMap.id);
|
||||
}
|
||||
if (result != null) {
|
||||
result.x = obj.getX();
|
||||
result.y = obj.getY();
|
||||
result.w = obj.getWidth();
|
||||
result.h = obj.getHeight();
|
||||
result.name = obj.getName();
|
||||
result.parentMap = parentMap;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public abstract void link();
|
||||
|
||||
public abstract Image getIcon();
|
||||
|
||||
public abstract void elementChanged(GameDataElement oldOne, GameDataElement newOne);
|
||||
|
||||
public tiled.core.MapObject toTmxObject() {
|
||||
tiled.core.MapObject tmxObject = new tiled.core.MapObject(x, y, w, h);
|
||||
tmxObject.setName(name);
|
||||
tmxObject.setType(type.toString());
|
||||
savePropertiesInTmxObject(tmxObject);
|
||||
return tmxObject;
|
||||
}
|
||||
|
||||
public abstract void savePropertiesInTmxObject(tiled.core.MapObject tmxObject);
|
||||
|
||||
public static MapObject newMapchange(tiled.core.MapObject obj, TMXMap parentMap) {
|
||||
MapObject result = new MapChange(obj);
|
||||
result.type = Types.mapchange;
|
||||
initObj(result, obj, parentMap);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static MapObject newSpawnArea(tiled.core.MapObject obj, TMXMap parentMap) {
|
||||
MapObject result = new SpawnArea(obj);
|
||||
result.type = Types.spawn;
|
||||
initObj(result, obj, parentMap);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static MapObject newRest(tiled.core.MapObject obj, TMXMap parentMap) {
|
||||
MapObject result = new RestArea(obj);
|
||||
result.type = Types.rest;
|
||||
initObj(result, obj, parentMap);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static MapObject newKey(tiled.core.MapObject obj, TMXMap parentMap) {
|
||||
MapObject result = new KeyArea(obj);
|
||||
result.type = Types.key;
|
||||
initObj(result, obj, parentMap);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static MapObject newReplace(tiled.core.MapObject obj, TMXMap parentMap) {
|
||||
MapObject result = new ReplaceArea(obj);
|
||||
result.type = Types.replace;
|
||||
initObj(result, obj, parentMap);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static MapObject newScript(tiled.core.MapObject obj, TMXMap parentMap) {
|
||||
MapObject result = new ScriptArea(obj);
|
||||
result.type = Types.script;
|
||||
initObj(result, obj, parentMap);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static MapObject newContainer(tiled.core.MapObject obj, TMXMap parentMap) {
|
||||
MapObject result = new ContainerArea(obj);
|
||||
result.type = Types.container;
|
||||
initObj(result, obj, parentMap);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static MapObject newSign(tiled.core.MapObject obj, TMXMap parentMap) {
|
||||
MapObject result = new SignArea(obj);
|
||||
result.type = Types.sign;
|
||||
initObj(result, obj, parentMap);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static MapObject initObj(MapObject result, tiled.core.MapObject obj, TMXMap parentMap) {
|
||||
result.x = obj.getX();
|
||||
result.y = obj.getY();
|
||||
result.w = obj.getWidth();
|
||||
result.h = obj.getHeight();
|
||||
result.name = obj.getName();
|
||||
result.parentMap = parentMap;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
|
||||
public class MapObjectGroup {
|
||||
|
||||
|
||||
public tiled.core.ObjectGroup tmxGroup;
|
||||
public TMXMap parentMap;
|
||||
public String name;
|
||||
public boolean visible;
|
||||
public List<MapObject> mapObjects = new ArrayList<MapObject>();
|
||||
|
||||
public MapObjectGroup(tiled.core.ObjectGroup layer, TMXMap map) {
|
||||
this.tmxGroup = layer;
|
||||
this.name = layer.getName();
|
||||
this.visible = layer.isVisible();
|
||||
this.parentMap = map;
|
||||
for (tiled.core.MapObject obj : layer.getObjectsList()) {
|
||||
mapObjects.add(MapObject.buildObject(obj, map));
|
||||
}
|
||||
}
|
||||
|
||||
public void link() {
|
||||
for (MapObject obj : mapObjects) {
|
||||
obj.link();
|
||||
}
|
||||
}
|
||||
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
for (MapObject object : mapObjects) {
|
||||
object.elementChanged(oldOne, newOne);
|
||||
}
|
||||
}
|
||||
|
||||
public void pushBackToTiledProperties() {
|
||||
if (tmxGroup != null) {
|
||||
tmxGroup.clear();
|
||||
} else {
|
||||
tmxGroup = new tiled.core.ObjectGroup();
|
||||
}
|
||||
tmxGroup.setVisible(visible);
|
||||
tmxGroup.setName(name);
|
||||
for (MapObject object : mapObjects) {
|
||||
tmxGroup.addObject(object.toTmxObject());
|
||||
}
|
||||
}
|
||||
}
|
||||
38
src/com/gpl/rpg/atcontentstudio/model/maps/ReplaceArea.java
Normal file
38
src/com/gpl/rpg/atcontentstudio/model/maps/ReplaceArea.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
|
||||
public class ReplaceArea extends MapObject {
|
||||
|
||||
public ReplaceArea(tiled.core.MapObject obj) {
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
@Override
|
||||
public void link() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
return DefaultIcons.getReplaceIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePropertiesInTmxObject(tiled.core.MapObject tmxObject) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
31
src/com/gpl/rpg/atcontentstudio/model/maps/RestArea.java
Normal file
31
src/com/gpl/rpg/atcontentstudio/model/maps/RestArea.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
|
||||
public class RestArea extends MapObject {
|
||||
|
||||
public RestArea(tiled.core.MapObject obj) {}
|
||||
|
||||
@Override
|
||||
public void link() {}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
return DefaultIcons.getRestIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePropertiesInTmxObject(tiled.core.MapObject tmxObject) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
60
src/com/gpl/rpg/atcontentstudio/model/maps/ScriptArea.java
Normal file
60
src/com/gpl/rpg/atcontentstudio/model/maps/ScriptArea.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.model.gamedata.Dialogue;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
public class ScriptArea extends MapObject {
|
||||
|
||||
public Dialogue dialogue = null;
|
||||
public EvaluationTrigger trigger_type = null;
|
||||
|
||||
public enum EvaluationTrigger {
|
||||
enter,
|
||||
step,
|
||||
round,
|
||||
always
|
||||
}
|
||||
|
||||
public ScriptArea(tiled.core.MapObject obj) {
|
||||
String triggerTypeId = obj.getProperties().getProperty("when");
|
||||
if (triggerTypeId != null) {
|
||||
trigger_type = EvaluationTrigger.valueOf(triggerTypeId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void link() {
|
||||
if (name != null) dialogue = parentMap.getProject().getDialogue(name);
|
||||
if (dialogue != null) {
|
||||
dialogue.addBacklink(parentMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
if (dialogue != null) return DefaultIcons.getScriptIcon();
|
||||
else return DefaultIcons.getNullifyIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
if (oldOne == dialogue) {
|
||||
dialogue = (Dialogue) newOne;
|
||||
newOne.addBacklink(parentMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePropertiesInTmxObject(tiled.core.MapObject tmxObject) {
|
||||
if (dialogue != null) {
|
||||
tmxObject.setName(dialogue.id);
|
||||
}
|
||||
if (trigger_type != null) {
|
||||
tmxObject.getProperties().setProperty("when", trigger_type.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
46
src/com/gpl/rpg/atcontentstudio/model/maps/SignArea.java
Normal file
46
src/com/gpl/rpg/atcontentstudio/model/maps/SignArea.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.model.gamedata.Dialogue;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
public class SignArea extends MapObject {
|
||||
|
||||
public Dialogue dialogue = null;
|
||||
|
||||
public SignArea(tiled.core.MapObject obj) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void link() {
|
||||
if (name != null) dialogue = parentMap.getProject().getDialogue(name);
|
||||
if (dialogue != null) {
|
||||
dialogue.addBacklink(parentMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
if (dialogue != null) return DefaultIcons.getSignIcon();
|
||||
else return DefaultIcons.getNullifyIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
if (oldOne == dialogue) {
|
||||
dialogue = (Dialogue) newOne;
|
||||
newOne.addBacklink(parentMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePropertiesInTmxObject(tiled.core.MapObject tmxObject) {
|
||||
if (dialogue != null) {
|
||||
tmxObject.setName(dialogue.id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
72
src/com/gpl/rpg/atcontentstudio/model/maps/SpawnArea.java
Normal file
72
src/com/gpl/rpg/atcontentstudio/model/maps/SpawnArea.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.model.gamedata.NPC;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
public class SpawnArea extends MapObject {
|
||||
|
||||
public int quantity = 1;
|
||||
public int spawnchance = 10;
|
||||
public List<NPC> spawnGroup = new ArrayList<NPC>();
|
||||
|
||||
public SpawnArea(tiled.core.MapObject obj) {
|
||||
if (obj.getProperties().getProperty("quantity") != null) {
|
||||
this.quantity = Integer.parseInt(obj.getProperties().getProperty("quantity"));
|
||||
}
|
||||
if (obj.getProperties().getProperty("spawnchance") != null) {
|
||||
this.spawnchance = Integer.parseInt(obj.getProperties().getProperty("spawnchance"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void link() {
|
||||
if (name != null) {
|
||||
spawnGroup = parentMap.getProject().getSpawnGroup(name);
|
||||
} else {
|
||||
spawnGroup = new ArrayList<NPC>();
|
||||
}
|
||||
if (spawnGroup != null) {
|
||||
for (NPC npc : spawnGroup) {
|
||||
npc.addBacklink(parentMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
if (spawnGroup != null && !spawnGroup.isEmpty()) {
|
||||
return spawnGroup.get(0).getIcon();
|
||||
}
|
||||
return DefaultIcons.getNullifyIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
int replacedIndex = -1;
|
||||
for (NPC npc : spawnGroup) {
|
||||
if (npc == oldOne) {
|
||||
replacedIndex = spawnGroup.indexOf(npc);
|
||||
}
|
||||
}
|
||||
if (replacedIndex >= 0) {
|
||||
spawnGroup.set(replacedIndex, (NPC) newOne);
|
||||
newOne.addBacklink(parentMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void savePropertiesInTmxObject(tiled.core.MapObject tmxObject) {
|
||||
if (quantity != 1) {
|
||||
tmxObject.getProperties().setProperty("quantity", Integer.toString(quantity));
|
||||
}
|
||||
if (spawnchance != 10) {
|
||||
tmxObject.getProperties().setProperty("spawnchance", Integer.toString(spawnchance));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
338
src/com/gpl/rpg/atcontentstudio/model/maps/TMXMap.java
Normal file
338
src/com/gpl/rpg/atcontentstudio/model/maps/TMXMap.java
Normal file
@@ -0,0 +1,338 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.swing.tree.TreeNode;
|
||||
|
||||
import tiled.io.TMXMapReader;
|
||||
import tiled.io.TMXMapWriter;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.Notification;
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.model.GameSource;
|
||||
import com.gpl.rpg.atcontentstudio.model.SaveEvent;
|
||||
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
|
||||
import com.gpl.rpg.atcontentstudio.model.Project;
|
||||
import com.gpl.rpg.atcontentstudio.model.ProjectTreeNode;
|
||||
import com.gpl.rpg.atcontentstudio.model.gamedata.GameDataSet;
|
||||
import com.gpl.rpg.atcontentstudio.model.sprites.Spritesheet;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
public class TMXMap extends GameDataElement {
|
||||
|
||||
private static final long serialVersionUID = 1609502879500898837L;
|
||||
|
||||
public File tmxFile = null;
|
||||
public tiled.core.Map tmxMap = null;
|
||||
public Set<Spritesheet> usedSpritesheets = null;
|
||||
public List<MapObjectGroup> groups = null;
|
||||
|
||||
public ProjectTreeNode parent;
|
||||
public Integer outside = null;
|
||||
|
||||
public boolean writable = false;
|
||||
|
||||
public TMXMap(TMXMapSet parent, File f) {
|
||||
this.parent = parent;
|
||||
this.tmxFile = f;
|
||||
String name = f.getName();
|
||||
id = name.substring(0, name.length() - 4);
|
||||
}
|
||||
|
||||
public void parse() {
|
||||
if (this.state == GameDataElement.State.init) {
|
||||
if (tmxMap != null) return;
|
||||
usedSpritesheets = new HashSet<Spritesheet>();
|
||||
try {
|
||||
tmxMap = new TMXMapReader().readMap(tmxFile.getAbsolutePath(), this);
|
||||
if (tmxMap.getProperties().get("outside") != null) {
|
||||
outside = new Integer(((String) tmxMap.getProperties().get("outside")));
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
Notification.addError("Impossible to load TMX map file "+tmxFile.getAbsolutePath());
|
||||
} catch (Exception e) {
|
||||
Notification.addError("Error while loading TMX map file "+tmxFile.getAbsolutePath()+": "+e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
for (tiled.core.MapLayer layer : tmxMap.getLayers()) {
|
||||
if (layer instanceof tiled.core.ObjectGroup) {
|
||||
if (groups == null) {
|
||||
groups = new ArrayList<MapObjectGroup>();
|
||||
}
|
||||
MapObjectGroup group = new MapObjectGroup((tiled.core.ObjectGroup) layer, this);
|
||||
groups.add(group);
|
||||
}
|
||||
}
|
||||
for (Spritesheet s : usedSpritesheets) {
|
||||
s.addBacklink(this);
|
||||
}
|
||||
state = State.parsed;
|
||||
}
|
||||
}
|
||||
|
||||
public void create() {
|
||||
if (tmxMap != null) return;
|
||||
tmxMap = new tiled.core.Map(30, 30);
|
||||
}
|
||||
|
||||
public TMXMap clone() {
|
||||
TMXMap clone = new TMXMap((TMXMapSet) this.parent, this.tmxFile);
|
||||
try {
|
||||
clone.usedSpritesheets = new HashSet<Spritesheet>();
|
||||
clone.tmxMap = new TMXMapReader().readMap(new StringReader(this.toXml()), clone);
|
||||
if (clone.tmxMap.getProperties().get("outside") != null) {
|
||||
clone.outside = new Integer(((String) clone.tmxMap.getProperties().get("outside")));
|
||||
}
|
||||
for (tiled.core.MapLayer layer : clone.tmxMap.getLayers()) {
|
||||
if (layer instanceof tiled.core.ObjectGroup) {
|
||||
if (clone.groups == null) {
|
||||
clone.groups = new ArrayList<MapObjectGroup>();
|
||||
}
|
||||
MapObjectGroup group = new MapObjectGroup((tiled.core.ObjectGroup) layer, this);
|
||||
clone.groups.add(group);
|
||||
}
|
||||
}
|
||||
for (Spritesheet s : usedSpritesheets) {
|
||||
s.addBacklink(clone);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Notification.addError("Error while cloning map "+this.id+" : "+e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<ProjectTreeNode> children() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getAllowsChildren() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeNode getChildAt(int arg0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChildCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex(TreeNode arg0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeNode getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLeaf() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void childrenAdded(List<ProjectTreeNode> path) {
|
||||
path.add(0,this);
|
||||
parent.childrenAdded(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void childrenChanged(List<ProjectTreeNode> path) {
|
||||
path.add(0,this);
|
||||
parent.childrenChanged(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void childrenRemoved(List<ProjectTreeNode> path) {
|
||||
path.add(0,this);
|
||||
parent.childrenRemoved(path);
|
||||
}
|
||||
@Override
|
||||
public void notifyCreated() {
|
||||
childrenAdded(new ArrayList<ProjectTreeNode>());
|
||||
}
|
||||
@Override
|
||||
public String getDesc() {
|
||||
return (this.state == State.modified ? "*" : "")+id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Project getProject() {
|
||||
return parent.getProject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
return DefaultIcons.getTiledIconIcon();
|
||||
}
|
||||
@Override
|
||||
public Image getLeafIcon() {
|
||||
return DefaultIcons.getTiledIconIcon();
|
||||
}
|
||||
@Override
|
||||
public Image getClosedIcon() {return null;}
|
||||
@Override
|
||||
public Image getOpenIcon() {return null;}
|
||||
|
||||
@Override
|
||||
public GameDataSet getDataSet() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getDataType() {
|
||||
return parent.getDataType();
|
||||
}
|
||||
|
||||
public String toXml() {
|
||||
for (MapObjectGroup group : groups) {
|
||||
group.pushBackToTiledProperties();
|
||||
if (!tmxMap.containsLayer(group.tmxGroup)) {
|
||||
tmxMap.addLayer(group.tmxGroup);
|
||||
}
|
||||
}
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
TMXMapWriter writer = new TMXMapWriter();
|
||||
writer.settings.layerCompressionMethod = TMXMapWriter.Settings.LAYER_COMPRESSION_METHOD_ZLIB;
|
||||
if (getDataType() == GameSource.Type.source) {
|
||||
writer.writeMap(tmxMap, baos, tmxFile.getAbsolutePath());
|
||||
} else {
|
||||
writer.writeMap(tmxMap, baos, getProject().baseContent.gameMaps.mapFolder.getAbsolutePath()+File.separator+"placeholder.tmx");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Notification.addError("Error while converting map "+getDesc()+" to XML: "+e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return baos.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
if (writable) {
|
||||
try {
|
||||
FileWriter w = new FileWriter(tmxFile);
|
||||
w.write(toXml());
|
||||
w.close();
|
||||
this.state = State.saved;
|
||||
Notification.addSuccess("TMX file "+tmxFile.getAbsolutePath()+" saved.");
|
||||
} catch (IOException e) {
|
||||
Notification.addError("Error while writing TMX file "+tmxFile.getAbsolutePath()+" : "+e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SaveEvent> attemptSave() {
|
||||
//TODO check cases where map should be moved from altered/created to created/altered....
|
||||
save();
|
||||
return null;
|
||||
}
|
||||
|
||||
public void delete() {
|
||||
if (writable) {
|
||||
if (tmxFile.exists()) {
|
||||
if (tmxFile.delete()) {
|
||||
Notification.addSuccess("TMX file "+tmxFile.getAbsolutePath()+" deleted.");
|
||||
} else {
|
||||
Notification.addError("Error while deleting TMX file "+tmxFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
((TMXMapSet)parent).tmxMaps.remove(this);
|
||||
//TODO clear blacklinks ?
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void link() {
|
||||
if (this.state == GameDataElement.State.init) {
|
||||
parse();
|
||||
}
|
||||
if (this.state == GameDataElement.State.parsed || this.state == GameDataElement.State.created) {
|
||||
for (MapObjectGroup group : groups) {
|
||||
group.link();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
for (MapObjectGroup group : groups) {
|
||||
group.elementChanged(oldOne, newOne);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProjectFilename() {
|
||||
return tmxFile.getName();
|
||||
}
|
||||
|
||||
public void addLayer(tiled.core.MapLayer layer) {
|
||||
tmxMap.addLayer(layer);
|
||||
if (layer instanceof tiled.core.ObjectGroup) {
|
||||
groups.add(new MapObjectGroup((tiled.core.ObjectGroup) layer, this));
|
||||
}
|
||||
}
|
||||
|
||||
public void removeLayer(tiled.core.MapLayer layer) {
|
||||
tmxMap.removeLayer(tmxMap.getLayerIndex(layer));
|
||||
if (layer instanceof tiled.core.ObjectGroup) {
|
||||
MapObjectGroup toRemove = null;
|
||||
for (MapObjectGroup group : groups) {
|
||||
if (group.tmxGroup == layer) {
|
||||
toRemove = group;
|
||||
}
|
||||
}
|
||||
if (toRemove != null) {
|
||||
groups.remove(toRemove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MapObjectGroup getGroup(tiled.core.ObjectGroup selectedLayer) {
|
||||
for (MapObjectGroup group : groups) {
|
||||
if (group.tmxGroup == selectedLayer) {
|
||||
return group;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<String> getMapchangesNames() {
|
||||
List<String> result = new ArrayList<String>();
|
||||
result.add(null);
|
||||
for (MapObjectGroup group : groups) {
|
||||
for (MapObject obj : group.mapObjects) {
|
||||
if (obj.type == MapObject.Types.mapchange) {
|
||||
result.add(obj.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
184
src/com/gpl/rpg/atcontentstudio/model/maps/TMXMapSet.java
Normal file
184
src/com/gpl/rpg/atcontentstudio/model/maps/TMXMapSet.java
Normal file
@@ -0,0 +1,184 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.tree.TreeNode;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameSource;
|
||||
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
|
||||
import com.gpl.rpg.atcontentstudio.model.Project;
|
||||
import com.gpl.rpg.atcontentstudio.model.ProjectTreeNode;
|
||||
import com.gpl.rpg.atcontentstudio.model.gamedata.GameDataSet;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
public class TMXMapSet implements ProjectTreeNode {
|
||||
|
||||
public static final String DEFAULT_REL_PATH_IN_SOURCE = "res/xml/";
|
||||
public static final String DEFAULT_REL_PATH_IN_PROJECT = "maps/";
|
||||
|
||||
public File mapFolder = null;
|
||||
public List<TMXMap> tmxMaps;
|
||||
|
||||
public ProjectTreeNode parent;
|
||||
|
||||
public TMXMapSet(GameSource source) {
|
||||
this.parent = source;
|
||||
if (source.type == GameSource.Type.source) this.mapFolder = new File(source.baseFolder, DEFAULT_REL_PATH_IN_SOURCE);
|
||||
else if (source.type == GameSource.Type.created | source.type == GameSource.Type.altered) {
|
||||
this.mapFolder = new File(source.baseFolder, DEFAULT_REL_PATH_IN_PROJECT);
|
||||
if (!this.mapFolder.exists()) {
|
||||
this.mapFolder.mkdirs();
|
||||
}
|
||||
}
|
||||
this.tmxMaps = new ArrayList<TMXMap>();
|
||||
|
||||
if (this.mapFolder != null) {
|
||||
for (File f : this.mapFolder.listFiles()) {
|
||||
if (f.getName().endsWith(".tmx") || f.getName().endsWith(".TMX")) {
|
||||
TMXMap map = new TMXMap(this, f);
|
||||
if (source.type == GameSource.Type.created | source.type == GameSource.Type.altered) {
|
||||
map.writable = true;
|
||||
}
|
||||
tmxMaps.add(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<TMXMap> children() {
|
||||
return Collections.enumeration(tmxMaps);
|
||||
}
|
||||
@Override
|
||||
public boolean getAllowsChildren() {
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public TreeNode getChildAt(int arg0) {
|
||||
return tmxMaps.get(arg0);
|
||||
}
|
||||
@Override
|
||||
public int getChildCount() {
|
||||
return tmxMaps.size();
|
||||
}
|
||||
@Override
|
||||
public int getIndex(TreeNode arg0) {
|
||||
return tmxMaps.indexOf(arg0);
|
||||
}
|
||||
@Override
|
||||
public TreeNode getParent() {
|
||||
return parent;
|
||||
}
|
||||
@Override
|
||||
public boolean isLeaf() {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public void childrenAdded(List<ProjectTreeNode> path) {
|
||||
path.add(0, this);
|
||||
parent.childrenAdded(path);
|
||||
}
|
||||
@Override
|
||||
public void childrenChanged(List<ProjectTreeNode> path) {
|
||||
path.add(0, this);
|
||||
parent.childrenChanged(path);
|
||||
}
|
||||
@Override
|
||||
public void childrenRemoved(List<ProjectTreeNode> path) {
|
||||
if (path.size() == 1 && this.getChildCount() == 1) {
|
||||
childrenRemoved(new ArrayList<ProjectTreeNode>());
|
||||
} else {
|
||||
path.add(0, this);
|
||||
parent.childrenRemoved(path);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void notifyCreated() {
|
||||
childrenAdded(new ArrayList<ProjectTreeNode>());
|
||||
for (TMXMap map : tmxMaps) {
|
||||
map.notifyCreated();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public String getDesc() {
|
||||
return "TMX Maps";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Project getProject() {
|
||||
return parent.getProject();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
return getOpenIcon();
|
||||
}
|
||||
@Override
|
||||
public Image getClosedIcon() {
|
||||
return DefaultIcons.getTmxClosedIcon();
|
||||
}
|
||||
@Override
|
||||
public Image getLeafIcon() {
|
||||
return DefaultIcons.getTmxClosedIcon();
|
||||
}
|
||||
@Override
|
||||
public Image getOpenIcon() {
|
||||
return DefaultIcons.getTmxOpenIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameDataSet getDataSet() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getDataType() {
|
||||
return parent.getDataType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return tmxMaps.isEmpty();
|
||||
}
|
||||
|
||||
public TMXMap getMap(String id) {
|
||||
if (tmxMaps == null) return null;
|
||||
for (TMXMap map : tmxMaps) {
|
||||
if (id.equals(map.id)){
|
||||
return map;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addMap(TMXMap node) {
|
||||
ProjectTreeNode higherEmptyParent = this;
|
||||
while (higherEmptyParent != null) {
|
||||
if (higherEmptyParent.getParent() != null && ((ProjectTreeNode)higherEmptyParent.getParent()).isEmpty()) higherEmptyParent = (ProjectTreeNode)higherEmptyParent.getParent();
|
||||
else break;
|
||||
}
|
||||
if (higherEmptyParent == this && !this.isEmpty()) higherEmptyParent = null;
|
||||
tmxMaps.add(node);
|
||||
if (node.tmxFile != null) {
|
||||
//Altered node.
|
||||
node.tmxFile = new File(this.mapFolder, node.tmxFile.getName());
|
||||
} else {
|
||||
//Created node.
|
||||
node.tmxFile = new File(this.mapFolder, node.id+".tmx");
|
||||
}
|
||||
node.parent = this;
|
||||
if (higherEmptyParent != null) higherEmptyParent.notifyCreated();
|
||||
else node.notifyCreated();
|
||||
}
|
||||
|
||||
public TMXMap get(int index) {
|
||||
return tmxMaps.get(index);
|
||||
}
|
||||
|
||||
}
|
||||
281
src/com/gpl/rpg/atcontentstudio/model/maps/Worldmap.java
Normal file
281
src/com/gpl/rpg/atcontentstudio/model/maps/Worldmap.java
Normal file
@@ -0,0 +1,281 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.Point;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.tree.TreeNode;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.TransformerFactoryConfigurationError;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameSource;
|
||||
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
|
||||
import com.gpl.rpg.atcontentstudio.model.Project;
|
||||
import com.gpl.rpg.atcontentstudio.model.ProjectTreeNode;
|
||||
import com.gpl.rpg.atcontentstudio.model.gamedata.GameDataSet;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
public class Worldmap extends ArrayList<WorldmapSegment> implements ProjectTreeNode {
|
||||
|
||||
private static final long serialVersionUID = 4590409256594556179L;
|
||||
|
||||
public static final String DEFAULT_REL_PATH_IN_SOURCE = "res/xml/worldmap.xml";
|
||||
public static final String DEFAULT_REL_PATH_IN_PROJECT = "maps"+File.separator+"worldmap.xml";
|
||||
|
||||
public File worldmapFile;
|
||||
public GameSource parent;
|
||||
|
||||
public Map<String, Map<String, Point>> segments = new HashMap<String, Map<String,Point>>();
|
||||
|
||||
public Worldmap(GameSource gameSource) {
|
||||
this.parent = gameSource;
|
||||
if (getDataType() == Type.source) {
|
||||
worldmapFile = new File(parent.baseFolder, DEFAULT_REL_PATH_IN_SOURCE);
|
||||
} else {
|
||||
worldmapFile = new File(parent.baseFolder, DEFAULT_REL_PATH_IN_PROJECT);
|
||||
}
|
||||
if (worldmapFile.exists()) {
|
||||
loadFromFile(worldmapFile);
|
||||
}
|
||||
if (getDataType() == Type.source) {
|
||||
for (WorldmapSegment segment : this) {
|
||||
segment.writable = false;
|
||||
}
|
||||
} else {
|
||||
for (WorldmapSegment segment : this) {
|
||||
segment.writable = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadFromFile(File file) {
|
||||
if (!file.exists()) return;
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
Document doc;
|
||||
try {
|
||||
factory.setIgnoringComments(true);
|
||||
factory.setIgnoringElementContentWhitespace(true);
|
||||
factory.setExpandEntityReferences(false);
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
InputSource insrc = new InputSource(new FileInputStream(file));
|
||||
insrc.setSystemId("http://worldmap/");
|
||||
insrc.setEncoding("UTF-8");
|
||||
doc = builder.parse(insrc);
|
||||
|
||||
Element root = (Element) doc.getElementsByTagName("worldmap").item(0);
|
||||
if (root != null) {
|
||||
NodeList segmentsList = root.getElementsByTagName("segment");
|
||||
if (segmentsList != null) {
|
||||
for (int i = 0; i < segmentsList.getLength(); i++) {
|
||||
Element segmentNode = (Element) segmentsList.item(i);
|
||||
String name = segmentNode.getAttribute("id");
|
||||
add(new WorldmapSegment(this, name, segmentNode));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SAXException e) {
|
||||
e.printStackTrace();
|
||||
} catch (FileNotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (ParserConfigurationException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<WorldmapSegment> children() {
|
||||
return Collections.enumeration(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getAllowsChildren() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeNode getChildAt(int arg0) {
|
||||
return get(arg0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChildCount() {
|
||||
return size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIndex(TreeNode arg0) {
|
||||
return indexOf(arg0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeNode getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLeaf() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void childrenAdded(List<ProjectTreeNode> path) {
|
||||
path.add(0,this);
|
||||
parent.childrenAdded(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void childrenChanged(List<ProjectTreeNode> path) {
|
||||
path.add(0,this);
|
||||
parent.childrenChanged(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void childrenRemoved(List<ProjectTreeNode> path) {
|
||||
if (path.size() == 1 && this.getChildCount() == 1) {
|
||||
childrenRemoved(new ArrayList<ProjectTreeNode>());
|
||||
} else {
|
||||
path.add(0, this);
|
||||
parent.childrenRemoved(path);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDesc() {
|
||||
return "Worldmap";
|
||||
}
|
||||
@Override
|
||||
public void notifyCreated() {
|
||||
childrenAdded(new ArrayList<ProjectTreeNode>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Project getProject() {
|
||||
return parent.getProject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
return DefaultIcons.getMapClosedIcon();
|
||||
}
|
||||
@Override
|
||||
public Image getLeafIcon() {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Image getClosedIcon() {
|
||||
return DefaultIcons.getMapClosedIcon();
|
||||
}
|
||||
@Override
|
||||
public Image getOpenIcon() {
|
||||
return DefaultIcons.getMapOpenIcon();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameDataSet getDataSet() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getDataType() {
|
||||
return parent.getDataType();
|
||||
}
|
||||
|
||||
public void save() {
|
||||
|
||||
try {
|
||||
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
||||
doc.setXmlVersion("1.0");
|
||||
Element root = doc.createElement("worldmap");
|
||||
doc.appendChild(root);
|
||||
|
||||
for (WorldmapSegment segment : this) {
|
||||
root.appendChild(segment.toXmlElement(doc));
|
||||
}
|
||||
|
||||
saveDocToFile(doc, worldmapFile);
|
||||
} catch (ParserConfigurationException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public WorldmapSegment getWorldmapSegment(String id) {
|
||||
for (WorldmapSegment s : this) {
|
||||
if (s.id.equals(id)) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addSegment(WorldmapSegment node) {
|
||||
ProjectTreeNode higherEmptyParent = this;
|
||||
while (higherEmptyParent != null) {
|
||||
if (higherEmptyParent.getParent() != null && ((ProjectTreeNode)higherEmptyParent.getParent()).isEmpty()) higherEmptyParent = (ProjectTreeNode)higherEmptyParent.getParent();
|
||||
else break;
|
||||
}
|
||||
if (higherEmptyParent == this && !this.isEmpty()) higherEmptyParent = null;
|
||||
add(node);
|
||||
node.parent = this;
|
||||
if (higherEmptyParent != null) higherEmptyParent.notifyCreated();
|
||||
else node.notifyCreated();
|
||||
}
|
||||
|
||||
|
||||
public static void saveDocToFile(Document doc, File f) {
|
||||
try {
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
Result output = new StreamResult(new FileOutputStream(f));
|
||||
Source input = new DOMSource(doc);
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
transformer.transform(input, output);
|
||||
} catch (TransformerConfigurationException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (TransformerFactoryConfigurationError e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (FileNotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (TransformerException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
149
src/com/gpl/rpg/atcontentstudio/model/maps/WorldmapSegment.java
Normal file
149
src/com/gpl/rpg/atcontentstudio/model/maps/WorldmapSegment.java
Normal file
@@ -0,0 +1,149 @@
|
||||
package com.gpl.rpg.atcontentstudio.model.maps;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.Point;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||
import com.gpl.rpg.atcontentstudio.model.SaveEvent;
|
||||
import com.gpl.rpg.atcontentstudio.model.gamedata.GameDataSet;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
|
||||
public class WorldmapSegment extends GameDataElement {
|
||||
|
||||
private static final long serialVersionUID = 2658610076889592723L;
|
||||
|
||||
public int segmentX;
|
||||
public int segmentY;
|
||||
public Map<String, Point> mapLocations = new HashMap<String, Point>();
|
||||
public Map<String, NamedArea> labelLocations = new HashMap<String, NamedArea>();
|
||||
public Element xmlNode;
|
||||
|
||||
public WorldmapSegment(Worldmap parent, String name, Element xmlNode) {
|
||||
this.parent = parent;
|
||||
this.id = name;
|
||||
this.xmlNode = xmlNode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameDataSet getDataSet() {
|
||||
return parent.getDataSet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDesc() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse() {
|
||||
segmentX = Integer.parseInt(xmlNode.getAttribute("x"));
|
||||
segmentY = Integer.parseInt(xmlNode.getAttribute("y"));
|
||||
NodeList mapsList = xmlNode.getElementsByTagName("map");
|
||||
for (int j = 0; j < mapsList.getLength(); j++) {
|
||||
Element mapNode = (Element) mapsList.item(j);
|
||||
mapLocations.put(mapNode.getAttribute("id"), new Point(Integer.parseInt(mapNode.getAttribute("x")) - segmentX, Integer.parseInt(mapNode.getAttribute("y")) - segmentY));
|
||||
}
|
||||
NodeList namedAreasNodeList = xmlNode.getElementsByTagName("namedarea");
|
||||
for (int j = 0; j < namedAreasNodeList.getLength(); j++) {
|
||||
Element namedAreaNode = (Element) namedAreasNodeList.item(j);
|
||||
labelLocations.put(namedAreaNode.getAttribute("id"), new NamedArea(namedAreaNode.getAttribute("id"), namedAreaNode.getAttribute("name"), namedAreaNode.getAttribute("type")));
|
||||
}
|
||||
this.state = State.parsed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void link() {
|
||||
if (this.state == State.init) {
|
||||
this.parse();
|
||||
} else if (this.state == State.linked) {
|
||||
return;
|
||||
}
|
||||
for (String mapName : mapLocations.keySet()) {
|
||||
getProject().getMap(mapName).addBacklink(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public WorldmapSegment clone() {
|
||||
WorldmapSegment clone = new WorldmapSegment((Worldmap)parent, id, (Element) xmlNode.cloneNode(true));
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elementChanged(GameDataElement oldOne, GameDataElement newOne) {
|
||||
oldOne.removeBacklink(this);
|
||||
newOne.addBacklink(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProjectFilename() {
|
||||
return "worldmap.xml";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
((Worldmap)parent).save();
|
||||
}
|
||||
|
||||
public Element toXmlElement(Document doc) {
|
||||
Element element = doc.createElement("segment");
|
||||
element.setAttribute("id", id);
|
||||
element.setAttribute("x", Integer.toString(segmentX));
|
||||
element.setAttribute("y", Integer.toString(segmentY));
|
||||
|
||||
for (String s : mapLocations.keySet()) {
|
||||
Element map = doc.createElement("map");
|
||||
map.setAttribute("id", s);
|
||||
map.setAttribute("x", Integer.toString(mapLocations.get(s).x + segmentX));
|
||||
map.setAttribute("y", Integer.toString(mapLocations.get(s).y + segmentY));
|
||||
element.appendChild(map);
|
||||
}
|
||||
|
||||
for (NamedArea area : labelLocations.values()) {
|
||||
Element namedArea = doc.createElement("namedarea");
|
||||
namedArea.setAttribute("id", area.id);
|
||||
namedArea.setAttribute("name", area.name);
|
||||
namedArea.setAttribute("type", area.type);
|
||||
element.appendChild(namedArea);
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SaveEvent> attemptSave() {
|
||||
// TODO Auto-generated method stub
|
||||
save();
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class NamedArea {
|
||||
String id;
|
||||
String name;
|
||||
String type;
|
||||
|
||||
public NamedArea(String id, String name, String type) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image getIcon() {
|
||||
return DefaultIcons.getUIMapIcon();
|
||||
}
|
||||
@Override
|
||||
public Image getLeafIcon() {
|
||||
return DefaultIcons.getUIMapIcon();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user