/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package transientlibs.preui.objects.gui.elements;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import transientlibs.preui.objects.gui.interfaces.TextOutput;
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.TextMarker;
import transientlibs.preui.objects.gui.elements.Marker;
import transientlibs.preui.utils.layout.HorizontalAlign;
import transientlibs.preui.utils.layout.VerticalAlign;
import java.util.ArrayList;
import java.util.Iterator;
import transientlibs.slick2d.util.Log;
import transientlibs.processors.misc.Detonator;
import transientlibs.preui.gdx.gui.MarkerList;
import transientlibs.preui.objects.gui.interfaces.IColour;
import transientlibs.preui.objects.gui.misc.ButtonEffect;
import transientlibs.preui.objects.gui.misc.ButtonInterval;
import transientlibs.preui.objects.states.IState;
/**
*
* @author kibertoad
*/
public class Menu extends Marker implements TextOutput {
public int currentPosition = 0; //from which entry show
public int maxShownPositions = 10; //how many entries draw at each time
public int scrollSize = 3;
public IMarker scrollUpButton = null;
public IMarker scrollDownButton = null;
public ArrayList<TextMarker> elements = new ArrayList<TextMarker>();
public IFont buttonFont = null;
public IFont infoFont = null;
public IFont textFont = null;
public static Menu LastMenu;
private ArrayList<Menu> exclusiveList = null; //access via fillExclusiveList and inExclusiveList
//public int buttonImageID;
//public int hoveredButtonImageID;
public IImage buttonImage = null;
public IImage hoveredButtonImage = null;
public int buttonYOffset = 10; //offset between buttons
public int ButtonStartYOffset = 30; //offset for a first button
public int ButtonStartXOffset = 20; //offset for a first button
public int TextStartYOffset = 0; //offset for a text inside a button
public int TextStartXOffset = 0; //offset for a text inside a button
public int lastY;
public MarkerList inList = null;
public int activeMarkerCount = 0; //how many buttons are actually drawn
public int setOptionsCount = 0;
public HorizontalAlign textHAlign = HorizontalAlign.center;
public VerticalAlign textVAlign = VerticalAlign.center;
public IColour colour = Detonator.INSTANCE.colourFactory.defaultButtonColour;
public IColour highlightedColour = Detonator.INSTANCE.colourFactory.defaultHoveredButtonColour;
public ArrayList<ButtonInterval> intervals = null;
@Override
public boolean isMenu() {
return true;
}
public void checkWasScrolled(Marker clickedMarker) {
if (clickedMarker.equals(scrollUpButton)) {
scrollUp();
}
if (clickedMarker.equals(scrollDownButton)) {
scrollDown();
}
}
public void setScrollUpMarker(Marker setMarker) {
scrollUpButton = setMarker;
scrollUpButton.addPayload(new ButtonEffect(this, ButtonEffect.EffectType.ScrollUpEffect));
}
public void setScrollDownMarker(Marker setMarker) {
scrollDownButton = setMarker;
scrollDownButton.addPayload(new ButtonEffect(this, ButtonEffect.EffectType.ScrollDownEffect));
}
public void scrollUp() {
currentPosition = currentPosition - scrollSize;
if (currentPosition < 0) {
currentPosition = 0;
}
hideUnseenButtons();
}
public void scrollDown() {
currentPosition = currentPosition + scrollSize;
//if (currentPosition > elements.size() - 1) {
//currentPosition = elements.size() - 1;
if (currentPosition >= activeMarkerCount - 1) {
currentPosition = activeMarkerCount - 1;
//if (currentPosition > maxShownPositions) {
// currentPosition = maxShownPositions;
}
Log.info("Position: " + currentPosition);
Log.info("Elements: " + activeMarkerCount);
hideUnseenButtons();
}
public void hideUnseenButtons() {
int i = 0;
int counter = 0;
activeMarkerCount = 0;
//Log.info("Processing elements, "+elements.size());
for (TextMarker t : elements) {
if (t.isOnScreen) {
activeMarkerCount++;
}
//Log.info("i: "+i);
if ((i < currentPosition) || (i >= currentPosition + maxShownPositions)) {
t.isDrawn = false;
} else {
t.isDrawn = t.isOnScreen;
//Log.info("Activate");
t.screenCoords.y = screenCoords.y + (t.imageHeight * counter);
t.textCapsule.y = t.screenCoords.getIntY();
counter++;
}
i++;
}
}
@Override
public boolean isClickable() {
return false;
}
public void removeElements(MarkerList fromList) {
for (TextMarker tm : elements) {
fromList.remove(tm);
}
fromList.remove(this);
}
public void relocateElements(MarkerList fromList, MarkerList toList) {
for (TextMarker tm : elements) {
if (fromList.contains(tm)) {
fromList.remove(tm);
//Log.info("removed");
}
if (!toList.contains(tm)) {
toList.add(tm);
//Log.info("added");
}
}
if (fromList.contains(this)) {
fromList.remove(this);
}
if (!toList.contains(this)) {
toList.add(this);
inList = toList;
}
}
public Menu(IImage setImage, int setx, int sety, IFont setFont) {
super(setImage, setImage, setx, sety);
buttonFont = setFont;
infoFont = setFont;
textFont = setFont;
}
public Menu(int setx, int sety) {
super(setx, sety);
buttonFont = Detonator.INSTANCE.fontProvider.getDefaultFont();
infoFont = Detonator.INSTANCE.fontProvider.getDefaultFont();
textFont = Detonator.INSTANCE.fontProvider.getDefaultFont();
}
public void deactivateAll() {
setAll(false);
}
public TextMarker nextElement(TextMarker nowElement) {
//Log.info("gonna get next element!");
//Log.info(nowElement.addressInGroup+"/"+(setOptionsCount - 1));
if (nowElement.addressInGroup >= setOptionsCount - 1) {
return null;
} else {
return elements.get(nowElement.addressInGroup + 1);
}
}
public void inExclusiveList() {
exclusiveList = new ArrayList<Menu>();
exclusiveList.add(this);
}
public void fillExclusiveList(Menu addMenu) {
exclusiveList.add(addMenu);
if (addMenu.exclusiveList == null) {
addMenu.exclusiveList = exclusiveList;
}
}
public void disableExclusiveList() {
Menu nowMenu;
Iterator<Menu> i = exclusiveList.iterator();
while (i.hasNext()) {
nowMenu = i.next();
if (nowMenu != this) {
nowMenu.setAll(false);
}
}
}
public void hideAll() {
TextMarker nowText;
if (elements.size() > 0) {
Iterator<TextMarker> i = elements.iterator();
while (i.hasNext()) {
nowText = i.next();
nowText.isDrawn = false;
}
}
}
public void showAll() {
TextMarker nowText;
if (elements.size() > 0) {
Iterator<TextMarker> i = elements.iterator();
while (i.hasNext()) {
nowText = i.next();
nowText.isDrawn = true;
}
}
if ((!(exclusiveList == null))) {
disableExclusiveList();
}
if ((!(inRenderList == null))) {
inRenderList.triggerDeactivationForRest();
}
}
public void setAllActive(boolean setSign) {
isDrawn = setSign;
isOnScreen = setSign;
for (TextMarker t : elements) {
t.isOnScreen = setSign;
t.isDrawn = setSign;
}
}
public void setAll(boolean setSign) {
isDrawn = true;
isOnScreen = true;
TextMarker nowText;
if (elements.size() > 0) {
Iterator<TextMarker> i = elements.iterator();
while (i.hasNext()) {
nowText = i.next();
if ((setSign == false) || (nowText.isOnScreen)) {
nowText.isDrawn = setSign;
}
}
}
if ((!(exclusiveList == null)) && (setSign)) {
disableExclusiveList();
}
if ((!(inRenderList == null)) && (setSign)) {
inRenderList.triggerDeactivationForRest();
}
}
@Override
public MarkerType markerID() {
return MarkerType.MenuMarker;
}
public void forceDrawElements() {
for (Marker m : elements) {
m.draw();
}
}
@Override
public void draw() {
//Log.info("Check menu");
if (!(getImage() == null)) {
//Log.info("Menu has image!");
super.draw();
}
}
@Override
public void draw(SpriteBatch spriteBatch) {
//Log.info("Check menu");
if (!(getImage() == null)) {
//Log.info("Menu has image!");
super.draw(spriteBatch);
}
}
public void addInterval(int from, int to, int setID) {
if (intervals == null) {
intervals = new ArrayList<ButtonInterval>();
}
intervals.add(new ButtonInterval(from, to, setID));
}
public void addInterval(int from, int to, Menu setToggleGroup) {
addInterval(from, to, -1);
intervals.get(intervals.size() - 1).linkedEffect = new ButtonEffect(setToggleGroup);
}
public void addStateInterval(int from, int to, int toStateEffect) {
addInterval(from, to, -1);
intervals.get(intervals.size() - 1).linkedEffect = new ButtonEffect(toStateEffect);
}
//process if Interval found
public void tryInterval(int gotID) {
ButtonInterval nowInterval = findInterval(gotID);
if (nowInterval != null) {
nowInterval.linkedEffect.processEffect();
}
}
public ButtonInterval findInterval(int gotID) {
if (intervals == null) {
return null;
} else {
ButtonInterval nowInterval;
Iterator<ButtonInterval> i = intervals.iterator();
while (i.hasNext()) {
nowInterval = i.next();
if (nowInterval.fits(gotID)) {
return nowInterval;
}
}
return null;
}
}
public int returnIntervalID(int gotID) {
ButtonInterval nowInterval = findInterval(gotID);
if (nowInterval != null) {
return nowInterval.id;
} else {
return -1;
}
}
@Override
public void restartFilling() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void complete() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public TextMarker addOption(String setText) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void clear() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void clear(IState inState) {
for (Marker m : elements) {
if (inState.getDrawStorage().contains(m)) {
inState.getDrawStorage().remove(m);
}
if (inState.getHoverStorage().contains(m)) {
inState.getHoverStorage().remove(m);
}
if (inState.getMinorHoverStorage().contains(m)) {
inState.getMinorHoverStorage().remove(m);
}
}
elements.clear();
}
@Override
public void showLastLine() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public TextMarker getLastLine() {
return elements.get(setOptionsCount - 1);
}
public void centerElementsInsideMarker(Marker insideMarker) {
int offsetX = ((insideMarker.screenCoords.getIntX()) + (insideMarker.getImageEndX() - elements.get(0).imageWidth)) / 2;
int offsetY = 0;
//int offsetY = (insideMarker.getImageEndY() - (elements.get(elements.size()-1).getImageEndY()-screenCoords.getIntY())) / 2;
offsetX = offsetX - screenCoords.getIntX();
//offsetY = offsetY - screenCoords.getIntY();
screenCoords.x = screenCoords.x + offsetX;
//screenCoords.y = screenCoords.y + offsetY;
for (Marker m : elements) {
Log.info("Before: " + m.screenCoords.x + "/" + m.screenCoords.y);
m.moveMarker(m.screenCoords.getIntX() + offsetX, m.screenCoords.getIntY() + offsetY);
Log.info("After: " + m.screenCoords.x + "/" + m.screenCoords.y);
}
}
public void centerElements() {
int offsetX = (Detonator.INSTANCE.ScreenSizeX - elements.get(0).imageWidth) / 2;
int offsetY = (Detonator.INSTANCE.ScreenSizeY - (elements.get(elements.size() - 1).getImageEndY() - screenCoords.getIntY())) / 2;
offsetX = offsetX - screenCoords.getIntX();
offsetY = offsetY - screenCoords.getIntY();
screenCoords.x = screenCoords.x + offsetX;
screenCoords.y = screenCoords.y + offsetY;
for (Marker m : elements) {
Log.info("Before: " + m.screenCoords.x + "/" + m.screenCoords.y);
m.moveMarker(m.screenCoords.getIntX() + offsetX, m.screenCoords.getIntY() + offsetY);
Log.info("After: " + m.screenCoords.x + "/" + m.screenCoords.y);
}
}
@Override
public void hideAllButFirst() {
for (int x = 0; x < elements.size(); x++) {
if (x != 0) {
elements.get(x).isDrawn = false;
}
}
}
public void drawChildren(SpriteBatch spriteBatch) {
for (Marker m : elements) {
if (m.isDrawn) {
m.draw(spriteBatch);
}
}
}
public void drawChildren() {
for (Marker m : elements) {
if (m.isDrawn) {
m.draw();
}
}
}
@Override
public ArrayList<TextMarker> getLatestEntries() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}