/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.bindedobjects.core;
import transientlibs.preui.objects.gui.elements.TextInfo;
import transientlibs.preui.objects.gui.elements.Marker;
import transientlibs.maps.entities.Landmark;
import transientlibs.tex.Branch;
import transientlibs.tex.BranchList;
import transientlibs.processors.misc.Detonator;
import transientlibs.tex.StoryList;
import transientlibs.tex.StringAnalyst;
import transientlibs.tex.TexBlock;
import transientlibs.tex.TextChunk;
import transientlibs.bindedobjects.core.Binding;
import java.io.*;
import transientlibs.slick2d.util.Log;
import transientlibs.preui.agents.factories.ElementFactory;
import transientlibs.preui.objects.gui.interfaces.IMarker;
import transientlibs.preui.objects.gui.elements.TextMarker;
/**
*
* @author Elwin
*/
public class Tex {
public static boolean texBlockActive = false;
public static TexBlock mainTexBlock = null;
public static void runGlobalInit() {
Log.info("Working directory: "+System.getProperty("user.dir"));
Branch initBranch = new Branch();
loadGlobalText("data/init/config.dat");
initBranch.processSilently();
}
public static void runGameInit(String fromFolder) {
Branch initBranch = new Branch();
Log.info("Load init data from: " + Detonator.INSTANCE.gameDataDir + "gameinit.dat");
loadText(fromFolder+"/gameinit.dat");
initBranch.processSilently();
}
public static void runInit() {
Branch initBranch = new Branch();
Log.info("Load init data from: " + Detonator.INSTANCE.gameDataDir + "init.dat");
loadText("init.dat");
initBranch.processSilently();
}
public static void loadTexts() {
File dir = new File(Detonator.INSTANCE.currentModule.dirName + "tex");
String[] fileList = dir.list();
if (fileList == null) {
Log.error(Detonator.INSTANCE.currentModule.dirName + "tex");
Log.error("Specified directory does not exist!");
} else {
for (int x = 0; x < fileList.length; x++) {
if ((!(fileList[x].equals(".svn"))) && (fileList[x].endsWith(".tex"))) {
Log.info(fileList[x]);
loadText("tex/" + fileList[x]);
}
}
}
}
public static void loadText(String fromFile) {
StringAnalyst reader = new StringAnalyst();
reader.openFile(fromFile);
while (!reader.eof) {
reader.readRow();
analyzeStringForTex(reader);
}
reader.closeFile();
}
public static void loadGlobalText(String fromFile) {
StringAnalyst reader = new StringAnalyst();
reader.openGlobalFile(fromFile);
while (!reader.eof) {
reader.readRow();
analyzeStringForTex(reader);
}
reader.closeFile();
}
public static void analyzeStringForTex(StringAnalyst analyst) {
analyzeStringForTex("-NO-", analyst, false);
}
public static void continousAnalysisForTex(StringAnalyst analyst) {
texBlockActive = true;
StoryList.lastStory = null;
//Branch.lastBranch = null;
while ((!analyst.eof) && (texBlockActive)) {
analyst.readRow();
analyzeStringForTex(analyst);
}
//Log.info("End tex analysis");
}
public static boolean analyzeStringForTex(String getString, StringAnalyst analyst, boolean keepAnalyzing) {
if (!"-NO-".equals(getString)) {
analyst.fullStr = getString;
}
//Log.info("FS>"+analyst.fullStr);
analyst.checkForAliases();
//Log.info("Full: "+analyst.fullStr);
boolean withResult = false;
StringBuilder tmpStr = new StringBuilder();
String nowStr;
//Log.info("Tex analysis iteration");
analyst.getNextString();
//Log.info("Last: "+analyst.lastStr);
nowStr = analyst.lastStr;
//analyst.checkForBadChars(nowStr);
Log.debug("Tex analysis string: " + nowStr);
//if (nowStr.equals("tex")) {
// texBlockActive = true;
// }
if (nowStr.equals("{")) {
//Log.info(Branch.lastBranch.name());
mainTexBlock = Branch.lastBranch.currentBlock;
Branch.lastBranch.addBlock();
}
if (nowStr.equals("}")) {
TexBlock.setAsLast(mainTexBlock);
withResult = true;
}
if ((nowStr.equals("branch")) || (nowStr.equals("br"))) {
// if (keepAnalyzing) {
// texBlockActive = true;
// }
if (BranchList.lastBranchList != null) {
BranchList.lastBranchList.addBranch(analyst.getBinding(Binding.branchBinding));
} else {
BranchList.lastBranchList = new BranchList();
}
withResult = true;
}
if (nowStr.equals("runningtext")) {
Detonator.INSTANCE.runningTextDelay = analyst.getNextNumber();
if (Detonator.INSTANCE.runningTextDelay == 0) {
Detonator.INSTANCE.runningText = false;
}
withResult = true;
}
if (nowStr.equals("imagepoint")) {
String markerCode = analyst.getNextString();
int setID = Binding.getBinding(Binding.interfaceTypeBinding, markerCode);
int setX = analyst.getNextNumber();
int setY = analyst.getNextNumber();
Marker marker = new Marker(null, setX, setY);
marker.ID = setID;
//REIMPLEMENT LATER
//StoryState.imagePoints.add(marker);
//REIMPLEMENT LATER
Detonator.INSTANCE.interfaceElements.put(markerCode, marker);
Detonator.INSTANCE.interfaceElementList.add(marker);
withResult = true;
}
if (nowStr.equals("setelementtext")) {
String toElement = analyst.getNextString();
String setString = analyst.restOfRow();
((TextMarker) Detonator.INSTANCE.interfaceElements.get(toElement)).setText(setString);
withResult = true;
}
if (nowStr.equals("setelementwidth")) {
String toElement = analyst.getNextString();
int toWidth = analyst.getNextNumber();
((TextMarker) Detonator.INSTANCE.interfaceElements.get(toElement)).maxWidth = toWidth;
((TextMarker) Detonator.INSTANCE.interfaceElements.get(toElement)).textCapsule.maxWidth = toWidth;
withResult = true;
}
if (nowStr.equals("disableelement")) {
String toElement = analyst.getNextString();
((TextMarker) Detonator.INSTANCE.interfaceElements.get(toElement)).isDrawn = false;
((TextMarker) Detonator.INSTANCE.interfaceElements.get(toElement)).isActive = false;
if (Detonator.INSTANCE.interfaceElements.get(toElement).markerID() == Marker.MarkerType.TextLine) {
((TextInfo) Detonator.INSTANCE.interfaceElements.get(toElement)).isInvisible.value = true;
}
withResult = true;
}
if (nowStr.equals("loadlayer")) {
String fromFile = analyst.getNextString();
loadText("gui/" + fromFile + ".gui");
}
if (nowStr.equals("element")) {
//Log.info("Load element: "+analyst.fullStr);
//1 Type
int setType = Binding.readBinding(Binding.interfaceTypeBinding, analyst.getNextString());
IMarker setMarker;
//2 ID
String markerCode = analyst.getNextString();
Binding.getBinding(Binding.interfaceElementBinding, markerCode);
//3 X
int CoordsX = analyst.getNextNumber();
//4 Y
int CoordsY = analyst.getNextNumber();
int fontID = -1;
int SizeX = -1;
int setImage;
//67948999
//5 image
analyst.getNextString();
if (!"-".equals(analyst.lastStr)) {
if (setType != ElementFactory.AnimationMarkerType) {
if (Detonator.INSTANCE.useGDXImages == false) {
setImage = Binding.readBinding(Binding.transientImageBinding, analyst.lastStr);
} else {
setImage = Binding.readBinding(Binding.transientImageBinding, analyst.lastStr);
}
} else {
setImage = Binding.readBinding(Binding.animationBinding, analyst.lastStr);
}
//Log.info("attached image " + analyst.lastStr);
} else {
setImage = -1;
}
//String markerText = "";
if (setType != ElementFactory.MarkerType) {
//6 font
String fontName = analyst.getNextString();
//Log.info("read font: "+fontName);
//fontID = Binding.readBinding(Binding.fontBinding, analyst.getNextString());
fontID = Binding.readBinding(Binding.fontBinding, fontName);
//7 size
//Log.info("read size");
SizeX = analyst.getNextNumber();
//markerText = analyst.restOfRow();
}
//if (setType == TextInfoType) {
//}
IMarker newMarker = Detonator.INSTANCE.elementFactory.spawnMarker
(setType, CoordsX, CoordsY, Detonator.INSTANCE.fontProvider.getFont(fontID), setImage);
//if (setType != MarkerType) && () {
Detonator.INSTANCE.bindGlobalInterfaceElement(markerCode, newMarker);
//}
/*
if (setType == TextBoxType) {
setMarker = new TextBox(CoordsX, CoordsY, null, " ", Fonts.getFontByID(fontID).font);
} else
if (setType == ButtonGroupType) {
setMarker = new ButtonGroup(CoordsX, CoordsY, Fonts.getFontByID(fontID).font, null);
//Log.info("image: "+setImage);
if (setImage != -1) {
setMarker.setImage(Images.getImageByID(setImage));
}
} else if (setType == InfoGroupType) {
setMarker = new InfoGroup(CoordsX, CoordsY, Fonts.getFontByID(fontID).font, null);
if (setImage != -1) {
setMarker.setImage(Images.getImageByID(setImage));
}
} else if (setType == TextInfoType) {
setMarker = new TextInfo(CoordsX, CoordsY, " ", -1, Fonts.getFontByID(fontID).font);
((TextInfo) setMarker).fontReference = Fonts.getFontByID(setType);
((TextInfo) setMarker).canBeClicked = true;
//((TextInfo)setMarker).setText(markerText);
} else if (setType == MarkerType) {
setMarker = new Marker(Images.getImageByID(setImage), Images.getImageByID(setImage), CoordsX, CoordsY);
setMarker.originalImage = Images.getImagesByID(setImage);
} else if (setType == TextMarkerType) {
setMarker = new TextMarker(Images.getImageByID(setImage), Images.getImageByID(setImage), CoordsX, CoordsY, -1);
setMarker.originalImage = Images.getImagesByID(setImage);
((TextMarker) setMarker).fontReference = Fonts.getFontByID(setType);
} if (setType == AnimationMarkerType) {
//setMarker = ElementFactory.GenericFactory.generateMarker(setType, CoordsX, CoordsY, Fonts.getFontByID(fontID), setImage);
setMarker = newMarker;
}
else {
setMarker = null;
}
*/
setMarker = newMarker;
if (setImage == -1) {
((Marker)setMarker).imageWidth = SizeX;
((Marker)setMarker).maxWidth = SizeX;
} else {
((Marker)setMarker).maxWidth = ((Marker)setMarker).imageWidth;
//Log.info("max");
}
//Detonator.INSTANCE.interfaceElements.put(markerCode, setMarker);
//Detonator.INSTANCE.interfaceElementList.add(setMarker);
//setMarker.stringID = markerCode;
Detonator.INSTANCE.interfaceElements.put(markerCode, newMarker);
Detonator.INSTANCE.interfaceElementList.add(newMarker);
newMarker.setStringID (markerCode);
//Log.info("Element " + markerCode + " was added.");
if (setImage == -1) {
((Marker)newMarker).imageWidth = SizeX;
((Marker)newMarker).maxWidth = SizeX;
}
withResult = true;
}
if (nowStr.equals("screensize")) {
Log.info("SCREEN IS NOW SET");
Detonator.INSTANCE.ScreenSizeX = analyst.getNextNumber();
Detonator.INSTANCE.ScreenSizeY = analyst.getNextNumber();
withResult = true;
}
if (nowStr.equals("define")) {
Log.info("define alias");
String String1 = analyst.getNextString();
String String2 = analyst.restOfRow();
//if (!(String1.startsWith(":"))) {
// Log.error("Aliases have to begin with ':'");
//}
Binding.newAlias("::" + String1, String2);
withResult = true;
}
if (nowStr.equals("story")) {
StoryList.lastStoryList.addStory(analyst.getBinding(Binding.storyBinding));
StoryList.lastStory.LName.value = analyst.lastStr;
withResult = true;
}
if (nowStr.equals("endtex")) {
texBlockActive = false;
withResult = true;
}
if (nowStr.equals("+")) {
analyst.checkForBadChars(analyst.fullStr);
BranchList.lastBranchList.checkNotEmpty();
//tmpStr.append(TexBlock.lastTexBlock.textChunks.get(TexBlock.lastTexBlock.textChunks.size() - 1).ofText);
//tmpStr.append("\n ");
//tmpStr.append(analyst.restOfRow());
//TexBlock.lastTexBlock.textChunks.get(TexBlock.lastTexBlock.textChunks.size() - 1).ofText = tmpStr.toString();
TexBlock.lastTexBlock.addLine(" " + analyst.restOfRow(), TextChunk.ChunkType.textParagraph);
}
if ((nowStr.equals("text")) || (nowStr.equals("t"))) {
analyst.checkForBadChars(analyst.fullStr);
//Log.info("Added new text line!");
BranchList.lastBranchList.checkNotEmpty();
TexBlock.lastTexBlock.addLine(" " + analyst.restOfRow(), TextChunk.ChunkType.mainText);
withResult = true;
}
if ((nowStr.equals("choice")) || (nowStr.equals("c"))) {
analyst.checkForBadChars(analyst.fullStr);
BranchList.lastBranchList.checkNotEmpty();
int value = analyst.getBinding(Binding.branchBinding);
TexBlock.lastTexBlock.addChoice(value, analyst.restOfRow());
withResult = true;
}
if (nowStr.equals("creature")) {
int coordX = analyst.getNextNumber();
int coordY = analyst.getNextNumber();
int ID = analyst.getBinding(Binding.creatureBinding);
if (Maps.currentMap == null) {
Log.error("Map not set!");
}
//Unit placedUnit = new Unit(coordX, coordY, ID, Maps.currentMap);
//Log.info("generated ok");
//Maps.currentMap.tile[coordX][coordY].units.add(placedUnit);
Detonator.INSTANCE.creatureFactory.produceUnit(ID, coordX, coordY, Maps.currentMap);
//Log.info("finished placing unit");
withResult = true;
}
if (nowStr.equals("globallandmark")) {
int coordX = analyst.getNextNumber();
int coordY = analyst.getNextNumber();
int setID = Binding.readBinding(Binding.landmarkBinding, analyst.getNextString());
Landmark newLandmark = Detonator.INSTANCE.creatureFactory.produceLandmark(setID, coordX, coordY, null);
newLandmark.bindValues();
withResult = true;
}
if ((nowStr.equals("landmark")) || (nowStr.equals("placelandmark"))) {
//Log.info(nowStr);
Log.info("SPAWN LANDMARK");
Log.info(analyst.fullStr);
int coordX = analyst.getNextNumber();
int coordY = analyst.getNextNumber();
int setID = Binding.readBinding(Binding.landmarkBinding, analyst.getNextString());
Landmark newLandmark = Detonator.INSTANCE.creatureFactory.produceLandmark(setID, coordX, coordY, Maps.currentMap);
//newLandmark = new Landmark(coordX, coordY, ID, Maps.currentMap);
//Maps.currentMap.placeLandmark(coordX, coordY, newLandmark);
newLandmark.bindValues();
withResult = true;
}
if (nowStr.equals("placecreature")) {
int setX = analyst.getNextNumber();
int setY = analyst.getNextNumber();
int setID = analyst.getBinding(Binding.creatureBinding);
Detonator.INSTANCE.creatureFactory.produceUnit(setID, setX, setY, Maps.currentMap);
//Log.info("produced ok");
withResult = true;
}
//if ((texBlockActive) && (!analyst.eof)) {
// analyst.readRow();
// }
if (withResult == false) {
withResult = Req.analyzeStringForReqs(analyst.fullStr, analyst);
}
if (withResult == false) {
withResult = Activator.analyzeStringForActs(analyst.fullStr, analyst);
}
//Log.info("End trying");
if ((withResult == false) && (nowStr.length() > 2) && (!nowStr.startsWith("//"))
&& (!nowStr.startsWith("#"))) {
Log.error("Unknown string: " + nowStr);
}
//Log.info("End tex iteration");
return withResult;
}
}