/cc2.0/src/cc/creativecomputing/ui/CCUISlider.java
http://creativecomputing.googlecode.com/ · Java · 268 lines · 194 code · 45 blank · 29 comment · 30 complexity · c3e05ed45b16c7863b59ff2221e8e7fc MD5 · raw file
- package cc.creativecomputing.ui;
- import java.awt.event.KeyEvent;
- import cc.creativecomputing.control.CCControlUI;
- import cc.creativecomputing.events.CCKeyEvent;
- import cc.creativecomputing.events.CCMouseEvent;
- import cc.creativecomputing.graphics.CCColor;
- import cc.creativecomputing.graphics.CCGraphics;
- import cc.creativecomputing.graphics.font.text.CCText;
- import cc.creativecomputing.math.CCMath;
- import cc.creativecomputing.math.CCVecMath;
- import cc.creativecomputing.math.CCVector2f;
- import cc.creativecomputing.xml.CCXMLElement;
- public class CCUISlider extends CCUIValueElement<CCUISlider,Float>{
-
- private float _myMin;
- private float _myMax;
- private boolean _myIsHorizontal;
-
- private float _myForeGroundSize;
-
- private boolean _myIsInteger = false;
-
- private CCText _myValueText;
-
- public CCUISlider(
- String theLabel, CCVector2f thePosition, CCVector2f theDimension,
- final float theMin, final float theMax
- ) {
- super(theLabel, thePosition, theDimension,theMin);
- initSlider(theDimension.x, theDimension.y, theMin, theMax);
- }
- /**
- * @param theLabel
- * @param theX
- * @param theY
- * @param theWidth
- * @param theHeight
- */
- public CCUISlider(
- String theLabel,
- float theX, float theY,
- float theWidth, float theHeight,
- final float theMin, final float theMax, final float theValue
- ) {
- super(theLabel, theX, theY, theWidth, theHeight,theValue);
- initSlider(theX, theY, theMin, theMax);
- }
- public CCUISlider(
- String theLabel,
- float theX, float theY,
- float theWidth, float theHeight,
- final float theMin, final float theMax
- ) {
- super(theLabel, theX, theY, theWidth, theHeight,theMin);
- initSlider(theX, theY, theMin, theMax);
- }
-
- private void initSlider(
- final float theX, final float theY,
- final float theMin, final float theMax
- ){
- _myMin = theMin;
- _myMax = theMax;
- constrainValues();
- _myIsHorizontal = theX >= theY;
-
- if(_myIsInteger)_myValueText.text(value().intValue());
- else _myValueText.text(value());
-
- updateRepresentation();
- }
-
- @Override
- public void updateRepresentation() {
- _myForeGroundSize = valueToPostion(_myValues.get(_myPreset));
-
- };
-
- @Override
- public void setupText() {
- float textY = _myPosition.y+_myDimension.y/2 + _myLabel.font().size()/2;
-
- _myLabel.position(_myPosition.x + _myDimension.x + 3, textY);
-
- _myValueText = new CCText(CCControlUI.FONT);
- _myValueText.position(_myPosition.x + 3,textY);
-
- _myBounds.min().set(_myPosition);
- _myBounds.width(_myDimension.x+_myLabel.width() +3);
- _myBounds.height(_myDimension.y);
-
- super.setupText();
- }
- public void isInteger(final boolean theIsInteger){
- _myIsInteger = theIsInteger;
- }
-
- private void constrainValues(){
- for(int i = 0; i < _myValues.size();i++){
- _myValues.set(i, CCMath.constrain(_myValues.get(i),_myMin,_myMax));
- }
- }
-
- // private float positionToValue(CCVector2f thePosition){
- // return positionToValue(thePosition.x, thePosition.y);
- // }
-
- private float positionToValue(final float theX, final float theY){
- if(_myIsHorizontal){
- return CCMath.constrain(CCMath.map(theX, _myPosition.x, _myPosition.x + _myDimension.x, _myMin, _myMax), _myMin, _myMax);
- }else{
- return CCMath.constrain(CCMath.map(theY, _myPosition.y, _myPosition.y + _myDimension.y, _myMin, _myMax), _myMin, _myMax);
- }
- }
-
- private float valueToPostion(float theValue){
- if(_myIsHorizontal){
- return CCMath.map(theValue, _myMin, _myMax, 0, _myDimension.x);
- }else{
- return CCMath.map(theValue, _myMin, _myMax, 0, _myDimension.y);
- }
- }
-
- private CCVector2f _myStartPosition = new CCVector2f();
-
- @Override
- protected void onDragg(CCMouseEvent theEvent) {
-
- CCVector2f myEventPosition = CCVecMath.subtract(theEvent.position(), _myStartPosition);
-
- if(theEvent.isShiftDown()) {
- myEventPosition.scale(0.1f);
- myEventPosition = CCVecMath.add(_myStartPosition, myEventPosition);
- }else {
- _myStartPosition = theEvent.position().clone();
- myEventPosition = _myStartPosition;
- }
-
-
- changeValue(positionToValue(myEventPosition.x, myEventPosition.y));
-
- if(_myIsHorizontal){
- _myForeGroundSize = CCMath.constrain(myEventPosition.x - _myPosition.x, 0, _myDimension.x);
- }else{
- _myForeGroundSize = CCMath.constrain(myEventPosition.y - _myPosition.y, 0, _myDimension.y);
- }
- }
- @Override
- protected void onPress(CCMouseEvent theEvent) {
- super.onPress(theEvent);
- _myStartPosition = theEvent.position().clone();
- changeValue(positionToValue(_myStartPosition.x,_myStartPosition.y));
- if(_myIsHorizontal){
- _myForeGroundSize = _myStartPosition.x - _myPosition.x;
- }else{
- _myForeGroundSize = _myStartPosition.y - _myPosition.y;
- }
- }
-
- @Override
- public void onMove( CCMouseEvent theEvent ) {
- super.onMove(theEvent);
- _myInputPosition.x = theEvent.x();
- _myInputPosition.y = theEvent.y() - 4;
- }
-
- /**
- * When selected the value can be typed into the slider. Any keyboard
- * input will be appended to the value string.
- * Pressing return tries to set the value to the typed number.
- * Wrong input won't result in any changes.
- * Hit "backspace" to cancel input!
- */
- @Override
- public void onKey( CCKeyEvent theEvent ) {
- int myKeyCode = theEvent.keyCode();
- if (theEvent.keyChar() == '\n' && _myKeyboardInput.length() > 0) {
- Float myValue = Float.parseFloat(_myKeyboardInput.toString());
- this.changeValue(CCMath.constrain(myValue, _myMin, _myMax));
- _myKeyboardInput.setLength(0);
- } else if (myKeyCode == KeyEvent.VK_BACK_SPACE) {
- _myKeyboardInput.setLength(0);
- } else if ((myKeyCode >= KeyEvent.VK_0 && myKeyCode <= KeyEvent.VK_9)
- || (myKeyCode == KeyEvent.VK_MINUS && _myKeyboardInput.length() == 0)) {
- _myKeyboardInput.append(theEvent.keyChar());
- } else if (myKeyCode == KeyEvent.VK_PERIOD) {
- if (_myKeyboardInput.indexOf(".") == -1) {
- _myKeyboardInput.append(theEvent.keyChar());
- }
- } else {
- return;
- }
- _myInputText.text(_myKeyboardInput.toString());
- }
- @Override
- public void draw(CCGraphics g) {
- if(_myIsHorizontal){
- g.color(_myForeGround);
- g.rect(
- _myPosition.x,_myPosition.y,
- _myForeGroundSize, _myDimension.y
- );
- g.color(_myBackGround);
- g.rect(
- _myPosition.x+_myForeGroundSize,_myPosition.y,
- _myDimension.x-_myForeGroundSize, _myDimension.y
- );
- g.color(0f,0.5f);
- g.rect(_myLabel.position().x-2, _myLabel.position().y-_myLabel.height() - 2, _myLabel.width()+4, _myLabel.height()+4);
- g.color(_myUIColor.colorLabel);
- _myLabel.draw(g);
- g.color(_myUIColor.colorValue);
- _myValueText.draw(g);
-
- // if (_myKeyboardInput.length() > 0) {
- // g.color(new CCColor(0.8f, 0.8f, 0.8f));
- //
- // g.rect(_myInputPosition.x-2, _myInputPosition.y+2,
- // _myInputText.width()+3, -_myDimension.y);
- // _myInputText.position(_myInputPosition);
- // g.color(_myInputColor);
- // _myInputText.draw(g);
- // }
- }
- super.draw(g);
- }
-
- @Override
- public Float relativeValue(final float theValue) {
- return CCMath.blend(_myMin, _myMax, theValue);
- }
-
-
- @Override
- public float changeFloatValue() {
- return CCMath.norm(value(), _myMin, _myMax);
- }
-
- public void value(final Float theValue){
- super.value(theValue);
- if(_myIsInteger)_myValueText.text(value().intValue());
- else _myValueText.text(value());
- _myForeGroundSize = valueToPostion(value());
- }
-
- public void preset(final int thePreset){
- super.preset(thePreset);
- _myForeGroundSize = valueToPostion(value());
- }
- /* (non-Javadoc)
- * @see cc.creativecomputing.control.CCUIValueElement#valueForXML(cc.creativecomputing.xml.CCXMLElement)
- */
- @Override
- public Float valueForXML(CCXMLElement theXMLElement) {
- return theXMLElement.floatAttribute("value",0);
- }
-
- }