<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tonegod.gui.controls.lists;

import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector4f;
import java.util.ArrayList;
import java.util.List;
import tonegod.gui.controls.buttons.Button;
import tonegod.gui.controls.text.TextField;
import tonegod.gui.core.Screen;

/**
 *
 * @author t0neg0d
 */
public class Spinner extends TextField {
	public static enum Orientation {
		VERTICAL,
		HORIZONTAL
	}
	
	protected List<String> stepValues = new ArrayList();
	private boolean cycle = false;
	private int selectedIndex = -1;
	private Orientation orientation;
	
	Button btnInc, btnDec;
	
	public Spinner(Screen screen, String UID, Vector2f position, Spinner.Orientation orientation, boolean cycle) {
		this(screen, UID, position,
			screen.getStyle("TextField").getVector2f("defaultSize"),
			screen.getStyle("TextField").getVector4f("resizeBorders"),
			screen.getStyle("TextField").getString("defaultImg"),
			orientation,
			cycle
		);
	}
	
	public Spinner(Screen screen, String UID, Vector2f position, Vector2f dimensions, Spinner.Orientation orientation, boolean cycle) {
		this(screen, UID, position, dimensions,
			screen.getStyle("TextField").getVector4f("resizeBorders"),
			screen.getStyle("TextField").getString("defaultImg"),
			orientation,
			cycle
		);
	}
	
	public Spinner(Screen screen, String UID, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg, Spinner.Orientation orientation, boolean cycle) {
		super(screen, UID, position, dimensions, resizeBorders, defaultImg
		);
		
		this.orientation = orientation;
		this.cycle = cycle;
		
		btnInc = new Button(
			screen,
			UID + ":btnInc",
			new Vector2f(getWidth(), 0),
			new Vector2f(getHeight(), getHeight())
		) {
			@Override
			public void onMouseLeftDown(MouseButtonEvent evt, boolean toggled) {
				((Spinner)getElementParent()).incStep();
			}
			@Override
			public void onMouseRightDown(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onMouseLeftUp(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onMouseRightUp(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onButtonFocus(MouseMotionEvent evt) {  }
			@Override
			public void onButtonLostFocus(MouseMotionEvent evt) {  }
			@Override
			public void onStillPressedInterval() {
				((Spinner)getElementParent()).incStep();
			}
		};
	//	if (screen.getStyle("ScrollArea#VScrollBar").getBoolean("useBtnDownArrowIcon")) {
			btnInc.setButtonIcon(18, 18, screen.getStyle("Common").getString("arrowRight"));
	//	}
		addChild(btnInc);
		
		btnDec = new Button(
			screen,
			UID + ":btnDec",
			new Vector2f(-getHeight(), 0),
			new Vector2f(getHeight(), getHeight())
		) {
			@Override
			public void onMouseLeftDown(MouseButtonEvent evt, boolean toggled) {
				((Spinner)getElementParent()).decStep();
			}
			@Override
			public void onMouseRightDown(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onMouseLeftUp(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onMouseRightUp(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onButtonFocus(MouseMotionEvent evt) {  }
			@Override
			public void onButtonLostFocus(MouseMotionEvent evt) {  }
			@Override
			public void onStillPressedInterval() {
				((Spinner)getElementParent()).decStep();
			}
		};
	//	if (screen.getStyle("ScrollArea#VScrollBar").getBoolean("useBtnDownArrowIcon")) {
			btnDec.setButtonIcon(18, 18, screen.getStyle("Common").getString("arrowLeft"));
	//	}
		addChild(btnDec);
		
		setIsEnabled(false);
	}
	
	public int getSelectedIndex() {
		return this.selectedIndex;
	}
	
	public void setInterval(float callsPerSecond) {
		btnInc.setInterval(callsPerSecond);
		btnDec.setInterval(callsPerSecond);
	}
	
	public void addStepValue(String value) {
		stepValues.add(value);
	}
	
	public void removeStepValue(String value) {
		stepValues.remove(value);
	}
	
	public void setStepIntegerRange(int min, int max, int inc) {
		stepValues.clear();
		for (int i = min; i <= max; i += inc) {
			stepValues.add(String.valueOf(i));
		}
	}
	
	public void setStepFloatRange(float min, float max, float inc) {
		stepValues.clear();
		for (float i = min; i <= max; i += inc) {
			stepValues.add(String.valueOf(i));
		}
	}
	
	private void incStep() {
		selectedIndex++;
		if (selectedIndex == stepValues.size()) {
			if (cycle) {
				selectedIndex = 0;
			} else {
				selectedIndex--;
			}
		}
		displaySelectedStep();
	}
	
	private void decStep() {
		selectedIndex--;
		if (selectedIndex == -1) {
			if (cycle) {
				selectedIndex = stepValues.size()-1;
			} else {
				selectedIndex = 0;
			}
		}
		displaySelectedStep();
	}
	
	private void displaySelectedStep() {
		this.setText(stepValues.get(selectedIndex));
	}
}<commit_msg>Addition of abstract onChange event<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tonegod.gui.controls.lists;

import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector4f;
import java.util.ArrayList;
import java.util.List;
import tonegod.gui.controls.buttons.Button;
import tonegod.gui.controls.text.TextField;
import tonegod.gui.core.Screen;

/**
 *
 * @author t0neg0d
 */
public abstract class Spinner extends TextField {
	public static enum Orientation {
		VERTICAL,
		HORIZONTAL
	}
	
	protected List<String> stepValues = new ArrayList();
	private boolean cycle = false;
	private int selectedIndex = -1;
	private Orientation orientation;
	
	Button btnInc, btnDec;
	
	public Spinner(Screen screen, String UID, Vector2f position, Spinner.Orientation orientation, boolean cycle) {
		this(screen, UID, position,
			screen.getStyle("TextField").getVector2f("defaultSize"),
			screen.getStyle("TextField").getVector4f("resizeBorders"),
			screen.getStyle("TextField").getString("defaultImg"),
			orientation,
			cycle
		);
	}
	
	public Spinner(Screen screen, String UID, Vector2f position, Vector2f dimensions, Spinner.Orientation orientation, boolean cycle) {
		this(screen, UID, position, dimensions,
			screen.getStyle("TextField").getVector4f("resizeBorders"),
			screen.getStyle("TextField").getString("defaultImg"),
			orientation,
			cycle
		);
	}
	
	public Spinner(Screen screen, String UID, Vector2f position, Vector2f dimensions, Vector4f resizeBorders, String defaultImg, Spinner.Orientation orientation, boolean cycle) {
		super(screen, UID, position, dimensions, resizeBorders, defaultImg
		);
		
		this.orientation = orientation;
		this.cycle = cycle;
		
		btnInc = new Button(
			screen,
			UID + ":btnInc",
			new Vector2f(getWidth(), 0),
			new Vector2f(getHeight(), getHeight())
		) {
			@Override
			public void onMouseLeftDown(MouseButtonEvent evt, boolean toggled) {
				((Spinner)getElementParent()).incStep();
			}
			@Override
			public void onMouseRightDown(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onMouseLeftUp(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onMouseRightUp(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onButtonFocus(MouseMotionEvent evt) {  }
			@Override
			public void onButtonLostFocus(MouseMotionEvent evt) {  }
			@Override
			public void onStillPressedInterval() {
				((Spinner)getElementParent()).incStep();
			}
		};
	//	if (screen.getStyle("ScrollArea#VScrollBar").getBoolean("useBtnDownArrowIcon")) {
			btnInc.setButtonIcon(18, 18, screen.getStyle("Common").getString("arrowRight"));
	//	}
		addChild(btnInc);
		
		btnDec = new Button(
			screen,
			UID + ":btnDec",
			new Vector2f(-getHeight(), 0),
			new Vector2f(getHeight(), getHeight())
		) {
			@Override
			public void onMouseLeftDown(MouseButtonEvent evt, boolean toggled) {
				((Spinner)getElementParent()).decStep();
			}
			@Override
			public void onMouseRightDown(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onMouseLeftUp(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onMouseRightUp(MouseButtonEvent evt, boolean toggled) {  }
			@Override
			public void onButtonFocus(MouseMotionEvent evt) {  }
			@Override
			public void onButtonLostFocus(MouseMotionEvent evt) {  }
			@Override
			public void onStillPressedInterval() {
				((Spinner)getElementParent()).decStep();
			}
		};
	//	if (screen.getStyle("ScrollArea#VScrollBar").getBoolean("useBtnDownArrowIcon")) {
			btnDec.setButtonIcon(18, 18, screen.getStyle("Common").getString("arrowLeft"));
	//	}
		addChild(btnDec);
		
		setIsEnabled(false);
	}
	
	public int getSelectedIndex() {
		return this.selectedIndex;
	}
	
	public void setInterval(float callsPerSecond) {
		btnInc.setInterval(callsPerSecond);
		btnDec.setInterval(callsPerSecond);
	}
	
	public void addStepValue(String value) {
		stepValues.add(value);
	}
	
	public void removeStepValue(String value) {
		stepValues.remove(value);
	}
	
	public void setStepIntegerRange(int min, int max, int inc) {
		stepValues.clear();
		for (int i = min; i <= max; i += inc) {
			stepValues.add(String.valueOf(i));
		}
	}
	
	public void setStepFloatRange(float min, float max, float inc) {
		stepValues.clear();
		for (float i = min; i <= max; i += inc) {
			stepValues.add(String.valueOf(i));
		}
	}
	
	private void incStep() {
		selectedIndex++;
		if (selectedIndex == stepValues.size()) {
			if (cycle) {
				selectedIndex = 0;
			} else {
				selectedIndex--;
			}
		}
		displaySelectedStep();
		onChange(selectedIndex, stepValues.get(selectedIndex));
	}
	
	private void decStep() {
		selectedIndex--;
		if (selectedIndex == -1) {
			if (cycle) {
				selectedIndex = stepValues.size()-1;
			} else {
				selectedIndex = 0;
			}
		}
		displaySelectedStep();
		onChange(selectedIndex, stepValues.get(selectedIndex));
	}
	
	private void displaySelectedStep() {
		this.setText(stepValues.get(selectedIndex));
	}
	
	public abstract void onChange(int selectedIndex, String value);
}
{'fraction_non_alphanumeric': 0.1206828885400314, 'fraction_numerical': 0.00706436420722135, 'mean_word_length': 4.336649214659686, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4544650', 'n_tokens_mistral': 3649, 'n_tokens_neox': 3180, 'n_words': 714}