/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.preui.objects.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Widget;
import java.util.Iterator;
import transientlibs.maps.entities.Landmark;
import transientlibs.maps.tiles.Tile;
import transientlibs.maps.implementation.TilelessMap;
import transientlibs.slick2d.util.Log;
import transientlibs.processors.misc.Detonator;
import transientlibs.preui.agents.rendering.GDXGenericStateDrawAgent;
import transientlibs.preui.gdx.game.TransientInput;
import transientlibs.preui.gdx.gui.MarkerList;
import transientlibs.objects.general.ModeList;
import transientlibs.objects.general.ModeSelector;
import transientlibs.objects.primitives.SmallPoints;
import transientlibs.preui.agents.misc.StoryAgent;
import transientlibs.preui.gdx.gui.WidgetLink;
import transientlibs.preui.objects.gui.elements.ButtonGroup;
import transientlibs.preui.objects.gui.interfaces.IFont;
import transientlibs.preui.objects.gui.interfaces.IImage;
import transientlibs.preui.objects.gui.interfaces.IMarker;
import transientlibs.preui.objects.gui.elements.InfoGroup;
import transientlibs.preui.objects.gui.elements.Marker;
import transientlibs.preui.objects.gui.elements.Menu;
import transientlibs.preui.objects.gui.elements.TextMarker;
import transientlibs.preui.objects.gui.elements.TransientStage;
import transientlibs.preui.objects.gui.interfaces.TextOutput;
import transientlibs.preui.objects.gui.misc.ButtonEffect;
import transientlibs.preui.objects.gui.misc.ButtonInterval;
import transientlibs.preui.objects.gui.misc.KeyboardCatcher;
import transientlibs.preui.objects.gui.misc.RenderList;
import transientlibs.preui.objects.gui.overlays.MapOverlay;
import transientlibs.tex.StoryPoint;
/**
*
* @author kibertoad
*/
public abstract class TransientState implements IState {
public KeyboardCatcher focusedKeyboardCatcher = null;
//public boolean interactionBlocked = false;
public IMarker hoveredMarker = null;
public IMarker hoverCheck;
public boolean clickedNotMarker = false;
public boolean clickedAnywhere = false;
public IMarker lastHoveredMarker = null;
public InfoGroup hoverInfo = null;
public StateElementManager elementManager;
public ButtonGroup actionButtons;
public InfoGroup test;
public IFont textFont;
public StoryAgent story = null;
public IFont buttonFont;
public GDXGenericStateDrawAgent drawAgent = null;
public int ID;
private boolean isInitted = false;
public TransientStage stage = new TransientStage();
public SpriteBatch spriteBatch = null;
private final String[] chars = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", ".,!?:;\"'+-=/\\< "};
public MarkerList drawStorage = new MarkerList(false);
public MarkerList UIHoverStorage = new MarkerList(true); //used for priority hover checks
public String stringID = "-";
public TransientInput input;
public TransientState(int setID, String setStringID) {
ID = setID;
stringID = setStringID;
textFont = Detonator.INSTANCE.fontProvider.getDefaultFont();
buttonFont = Detonator.INSTANCE.fontProvider.getDefaultFont();
modeList = new ModeList();
modeSelector = new ModeSelector(modeList);
input = Detonator.INSTANCE.currentTransientGame.input;
}
@Override
public void enter() {
Detonator.INSTANCE.currentState = this;
}
@Override
public boolean isInitted() {
return isInitted;
}
@Override
public void setInitted(boolean setValue) {
isInitted = setValue;
}
@Override
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public boolean interactionBlocked() {
//return (!(animations.isEmpty()));
return false;
}
public IMarker returnHoveredMarker() {
if (interactionBlocked()) {
return null;
}
//Log.info ("Markers: UIHover: "+UIHoverStorage.size());
Iterator<IMarker> i = UIHoverStorage.iterator();
while (i.hasNext()) {
hoverCheck = i.next();
//Log.info("C1:hovered "+hoverCheck.isHovered());
//Log.info("C2:clickable "+hoverCheck.isClickable());
//Log.info("C3:drawn "+ hoverCheck.isDrawn());
if ((hoverCheck.isHovered())
&& (hoverCheck.isClickable())
&& (hoverCheck.isDrawn())) {
return hoverCheck;
}
}
//Log.info ("Markers: hover: "+hoverStorage.size());
i = hoverStorage.iterator();
while (i.hasNext()) {
hoverCheck = i.next();
//Log.info("C1:hovered "+hoverCheck.isHovered());
//Log.info("C2:clickable "+hoverCheck.isClickable());
//Log.info("C3:drawn "+ hoverCheck.isDrawn());
if ((hoverCheck.isHovered())
&& (hoverCheck.isClickable())
&& (hoverCheck.isDrawn())) {
//Log.info("HOVERED");
return hoverCheck;
}
}
i = minorHoverStorage.iterator();
while (i.hasNext()) {
hoverCheck = i.next();
if ((hoverCheck != null) && (hoverCheck.isHovered())
&& (hoverCheck.isClickable()) && (hoverCheck.isDrawn())) {
return hoverCheck;
}
}
return null;
}
public void processHoverChange() {
}
public Marker.MarkerType hoveredMarkerType() {
if (hoveredMarker == null) {
return Marker.MarkerType.NullMarker;
} else {
return hoveredMarker.markerID();
}
}
public void updateHoverInfo() {
if (hoverInfo != null) {
//Log.info("start hover check");
if (lastHoveredMarker != hoveredMarker) {
processHoverChange();
//Log.info("Not null: "+(hoveredMarkerType() != Marker.MarkerType.NullMarker));
//if (hoveredMarker != null) {Log.info("Returns: "+(hoveredMarker.returnsInfoOnHover()));}
if ((hoveredMarkerType() != Marker.MarkerType.NullMarker) && (hoveredMarker.returnsInfoOnHover())) {
hoverInfo.restartFilling();
//is actionbutton
if ((hoveredMarker.getInGroup() != null) && (hoveredMarker.getInGroup() == actionButtons)
&& (hoveredMarker.getRelatedNode() != null)) {
//Log.info("is action button");
hoveredMarker.getRelatedNode().reEvaluate();
hoveredMarker.getRelatedNode().fillInfo(hoverInfo);
} //everything else
else {
//Log.info("cas click");
hoveredMarker.reEvaluate();
hoveredMarker.fillInfo(hoverInfo);
}
hoverInfo.complete();
hoverInfo.setAll(true);
} else {
hoverInfo.setAll(false);
}
lastHoveredMarker = hoveredMarker;
}
}
}
@Override
public void processInput() {
hoveredMarker = returnHoveredMarker();
//if (hoveredMarker != null) {Log.info("M"+hoveredMarker.markerID());}
if (Detonator.INSTANCE.inputProvider.returnLMBClicked()) {
Log.info("Process click");
//if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
lastHoveredMarker = null;
clickedNotMarker = true;
clickedAnywhere = true;
if ((story != null)
&& (story.isEnabled)) {
story.processClickAnywhere();
}
if (!(hoveredMarker == null)) {
Log.info("Marker not null, process it now");
processClickedMarker(hoveredMarker);
if ((story != null)
&& (story.isEnabled)) {
story.processClickedMarker(hoveredMarker);
}
Log.info("NUKED");
clickedNotMarker = false;
}
} else {
clickedNotMarker = false;
clickedAnywhere = false;
}
updateHoverInfo();
}
@Override
public String getStringID() {
return stringID;
}
@Override
public void setStringID(String stringID) {
this.stringID = stringID;
}
public ModeList modeList;
public ModeSelector modeSelector;
public MarkerList hoverStorage = new MarkerList(true);
public MarkerList minorHoverStorage = new MarkerList(true);
public TransientGame game;
@Override
public GDXGenericStateDrawAgent getStateDrawAgent() {
return drawAgent;
}
@Override
public void setStateDrawAgent(GDXGenericStateDrawAgent setAgent) {
drawAgent = setAgent;
}
@Override
public void removed() {
if (spriteBatch != null) {
spriteBatch.dispose();
}
}
public void setup() {
}
@Override
public final void init(TransientGame setGame) {
game = setGame;
//Matrix4 projection = new Matrix4();
//projection.setToOrtho(0, game.screenSizeX, game.screenSizeY, 0, -1, 1);
spriteBatch = new SpriteBatch(100);
//spriteBatch.setProjectionMatrix(projection);
isInitted = true;
//drawAgent = new GDXGenericStateDrawAgent(this);
setup();
}
public void initGDXUI() {
Gdx.input.setInputProcessor(stage.stage);
for (IMarker element : drawStorage) {
element.setIsDrawn(false);
Actor newActor = new WidgetLink(element).actor;
if (newActor != null) {
stage.stage.addActor(newActor);
}
}
}
@Override
public void postInit() {
//initGDXUI();
}
@Override
public void init() {
this.setStateDrawAgent(new GDXGenericStateDrawAgent(this));
elementManager = new StateElementManager(this);
}
@Override
public void passMarker(IMarker marker) {
//stage.add(marker);
//If you are getting a crash here, probably you aren't calling super.init() on your state!
this.elementManager.passMarker(marker);
}
@Override
public void passMarkerNoHover(IMarker getMarker) {
drawStorage.add(getMarker);
Marker.lastMarker = drawStorage.get(drawStorage.size() - 1);
}
@Override
public void passMarkerHoverOnly(IMarker marker) {
this.elementManager.passMarkerHoverOnly(marker);
}
protected void setState(TransientState screen) {
game.changeState(screen);
}
public void draw(TextureRegion region, int x, int y) {
int width = region.getRegionWidth();
if (width < 0) {
width = -width;
}
spriteBatch.draw(region, x, y, width, region.getRegionHeight());
}
public void drawString(String string, int x, int y) {
string = string.toUpperCase();
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
for (int ys = 0; ys < chars.length; ys++) {
int xs = chars[ys].indexOf(ch);
//if (xs >= 0) {
// draw(Art.guys[xs][ys + 9], x + i * 6, y);
//}
}
}
}
@Override
public void render() {
//stage.act(Gdx.graphics.getDeltaTime());
//stage.draw();
if (drawAgent != null) {
drawAgent.process();
}
}
/**
* Process input
*/
@Override
public void tick(TransientInput input) {
input.processedKeypress = false;
if (this.focusedKeyboardCatcher != null) {
focusedKeyboardCatcher.catchText();
}
input.process();
}
@Override
public void setFocusedKeyboardCatcher(KeyboardCatcher listenEffect) {
focusedKeyboardCatcher = listenEffect;
}
@Override
public KeyboardCatcher getFocusedKeyboardCatcher() {
return focusedKeyboardCatcher;
}
@Override
public IMarker addInfo(int x, int y, String setText) {
return this.elementManager.addInfo(x, y, setText);
}
@Override
public IMarker addTextMarker(IFont setFont, IImage setImage, IImage setHoveredImage, String setText, int setX, int setY, int setID) {
return this.elementManager.addTextMarker(setFont, setImage, setHoveredImage, setText, setX, setY, setID);
}
@Override
public IMarker addPlusMinusMarker(IFont buttonFont, IImage setImage, String setText, int setX, int setY, int setID, SmallPoints usePool, int maxValue) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public IMarker addMarker(IImage imageByCode, IImage imageByCode0, int x, int y) {
return this.elementManager.addMarker(imageByCode, imageByCode0, x, y);
}
@Override
public void removeMarker(IMarker m) {
drawStorage.remove(m);
hoverStorage.remove(m);
UIHoverStorage.remove(m);
minorHoverStorage.remove(m);
}
@Override
public void removeMarkerMinorHoverOnly(IMarker m) {
minorHoverStorage.remove(m);
}
@Override
public TextMarker addStateButton(int i, int i0, String details, int OrphanOverviewStateID) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public InfoGroup addInfoGroup(int setX, int setY) {
return elementManager.addInfoGroup(setX, setY);
}
@Override
public void dispose() {
this.stage.stage.dispose();
}
@Override
public void resize(int width, int height) {
this.stage.stage.setViewport(width, height, true);
}
@Override
public MarkerList getDrawStorage() {
return drawStorage;
}
public void setDrawStorage(MarkerList drawStorage) {
this.drawStorage = drawStorage;
}
@Override
public MarkerList getHoverStorage() {
return hoverStorage;
}
public void setHoverStorage(MarkerList hoverStorage) {
this.hoverStorage = hoverStorage;
}
@Override
public MarkerList getMinorHoverStorage() {
return minorHoverStorage;
}
public void setMinorHoverStorage(MarkerList minorHoverStorage) {
this.minorHoverStorage = minorHoverStorage;
}
@Override
public ModeSelector getModeSelector() {
return this.modeSelector;
}
@Override
public void removeFromDrawStorage(RenderList r) {
elementManager.removeFromDrawStorage(r);
}
@Override
public void removeMarkers(RenderList r) {
elementManager.removeFromDrawStorage(r);
elementManager.removeFromHoverStorage(r);
}
@Override
public void removeChildrenFromDrawStorage(Menu m) {
elementManager.removeChildrenFromDrawStorage(m);
}
@Override
public TextMarker addTextMarker(int setx, int sety, String setText) {
return elementManager.addTextMarker(setx, sety, setText);
}
public boolean checkClickEffect(IMarker clickedMarker) {
//this part is borked
ButtonInterval clickedInterval = clickedMarker.returnClickedInterval();
if (clickedInterval != null) {
//System.out.println("Yo, dawg, not null");
if (clickedInterval.linkedEffect == null) {
Log.error("No effect described for this interval");
}
if (clickedInterval.linkedEffect.ofType == ButtonEffect.EffectType.StateChange) {
//Log.info("Yo, dawg, gonna detonate new state");
Detonator.INSTANCE.SwitchStateDetonator = clickedInterval.linkedEffect.effectTargetID;
}
}
Log.info("GOT THERE");
return clickedMarker.processClick();
}
@Override
public void processClickedMarker(IMarker clickedMarker) {
Log.info("Clicked marker");
checkClickEffect(clickedMarker);
}
@Override
public IFont getTextFont() {
return textFont;
}
public void setTextFont(IFont textFont) {
this.textFont = textFont;
}
@Override
public IFont getButtonFont() {
return buttonFont;
}
public void setButtonFont(IFont buttonFont) {
this.buttonFont = buttonFont;
}
@Override
public void queueStory(String theStory) {
//StoryPoint.queueStory(theStory);
Detonator.INSTANCE.currentTransientGame.getStateByCode("story").getStoryAgent().queueStory(theStory);
}
@Override
public void queueStory(int theStory) {
StoryPoint.queueStory(theStory);
}
public boolean hoveredMapObject() {
return ((hoveredMarkerType() == Marker.MarkerType.TileMarker)
|| (hoveredMarkerType() == Marker.MarkerType.UnitMarker)
|| (hoveredMarkerType() == Marker.MarkerType.LandmarkMarker));
}
@Override
public void enterState(int theState) {
Detonator.INSTANCE.currentGenericGame.changeState(theState);
}
@Override
public void enterState(String stateCode) {
Detonator.INSTANCE.currentGenericGame.changeState(stateCode);
}
public void playMusic(String theOne) {
Detonator.INSTANCE.musicPlayer.playMusic(theOne);
}
@Override
public ButtonGroup addGroup(int x, int y) {
return elementManager.addGroup(x, y);
}
public IImage getImage(String byID) {
return Detonator.INSTANCE.imageProvider.getImage(byID);
}
@Override
public void removeFromDrawStorage(IMarker city1) {
elementManager.removeFromDrawStorage(city1);
}
@Override
public StoryAgent getStoryAgent() {
return story;
}
@Override
public SpriteBatch getSpriteBatch() {
return spriteBatch;
}
@Override
public void passMarkerMinorHoverOnly(IMarker passedMarker) {
elementManager.passMarkerMinorHoverOnly(passedMarker);
}
@Override
public void minimizeMapHoverPriority(TilelessMap setMap) {
Log.notImplemented();
// for (Tile t : setMap.tileList) {
// t.minimizeCompositionPriority(hoverStorage);
// }
for (Landmark l : setMap.landmarks) {
//l.maximizeCompositionPriority(hoverStorage);
l.minimizeCompositionPriority(hoverStorage);
}
}
@Override
public TextOutput getMessageLog() {
return Detonator.INSTANCE.eventLog;
}
@Override
public MapOverlay getMapOverlay (){
return null;
}
@Override
public ButtonGroup convertMarkerToButtonGroup(IMarker temp) {
return addGroup(temp.getScreenCoords().getIntX(), temp.getScreenCoords().getIntY());
}
@Override
public StateElementManager getElementManager() {
return elementManager;
}
}