/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

  1. package cc.creativecomputing.ui;
  2. import java.awt.event.KeyEvent;
  3. import cc.creativecomputing.control.CCControlUI;
  4. import cc.creativecomputing.events.CCKeyEvent;
  5. import cc.creativecomputing.events.CCMouseEvent;
  6. import cc.creativecomputing.graphics.CCColor;
  7. import cc.creativecomputing.graphics.CCGraphics;
  8. import cc.creativecomputing.graphics.font.text.CCText;
  9. import cc.creativecomputing.math.CCMath;
  10. import cc.creativecomputing.math.CCVecMath;
  11. import cc.creativecomputing.math.CCVector2f;
  12. import cc.creativecomputing.xml.CCXMLElement;
  13. public class CCUISlider extends CCUIValueElement<CCUISlider,Float>{
  14. private float _myMin;
  15. private float _myMax;
  16. private boolean _myIsHorizontal;
  17. private float _myForeGroundSize;
  18. private boolean _myIsInteger = false;
  19. private CCText _myValueText;
  20. public CCUISlider(
  21. String theLabel, CCVector2f thePosition, CCVector2f theDimension,
  22. final float theMin, final float theMax
  23. ) {
  24. super(theLabel, thePosition, theDimension,theMin);
  25. initSlider(theDimension.x, theDimension.y, theMin, theMax);
  26. }
  27. /**
  28. * @param theLabel
  29. * @param theX
  30. * @param theY
  31. * @param theWidth
  32. * @param theHeight
  33. */
  34. public CCUISlider(
  35. String theLabel,
  36. float theX, float theY,
  37. float theWidth, float theHeight,
  38. final float theMin, final float theMax, final float theValue
  39. ) {
  40. super(theLabel, theX, theY, theWidth, theHeight,theValue);
  41. initSlider(theX, theY, theMin, theMax);
  42. }
  43. public CCUISlider(
  44. String theLabel,
  45. float theX, float theY,
  46. float theWidth, float theHeight,
  47. final float theMin, final float theMax
  48. ) {
  49. super(theLabel, theX, theY, theWidth, theHeight,theMin);
  50. initSlider(theX, theY, theMin, theMax);
  51. }
  52. private void initSlider(
  53. final float theX, final float theY,
  54. final float theMin, final float theMax
  55. ){
  56. _myMin = theMin;
  57. _myMax = theMax;
  58. constrainValues();
  59. _myIsHorizontal = theX >= theY;
  60. if(_myIsInteger)_myValueText.text(value().intValue());
  61. else _myValueText.text(value());
  62. updateRepresentation();
  63. }
  64. @Override
  65. public void updateRepresentation() {
  66. _myForeGroundSize = valueToPostion(_myValues.get(_myPreset));
  67. };
  68. @Override
  69. public void setupText() {
  70. float textY = _myPosition.y+_myDimension.y/2 + _myLabel.font().size()/2;
  71. _myLabel.position(_myPosition.x + _myDimension.x + 3, textY);
  72. _myValueText = new CCText(CCControlUI.FONT);
  73. _myValueText.position(_myPosition.x + 3,textY);
  74. _myBounds.min().set(_myPosition);
  75. _myBounds.width(_myDimension.x+_myLabel.width() +3);
  76. _myBounds.height(_myDimension.y);
  77. super.setupText();
  78. }
  79. public void isInteger(final boolean theIsInteger){
  80. _myIsInteger = theIsInteger;
  81. }
  82. private void constrainValues(){
  83. for(int i = 0; i < _myValues.size();i++){
  84. _myValues.set(i, CCMath.constrain(_myValues.get(i),_myMin,_myMax));
  85. }
  86. }
  87. // private float positionToValue(CCVector2f thePosition){
  88. // return positionToValue(thePosition.x, thePosition.y);
  89. // }
  90. private float positionToValue(final float theX, final float theY){
  91. if(_myIsHorizontal){
  92. return CCMath.constrain(CCMath.map(theX, _myPosition.x, _myPosition.x + _myDimension.x, _myMin, _myMax), _myMin, _myMax);
  93. }else{
  94. return CCMath.constrain(CCMath.map(theY, _myPosition.y, _myPosition.y + _myDimension.y, _myMin, _myMax), _myMin, _myMax);
  95. }
  96. }
  97. private float valueToPostion(float theValue){
  98. if(_myIsHorizontal){
  99. return CCMath.map(theValue, _myMin, _myMax, 0, _myDimension.x);
  100. }else{
  101. return CCMath.map(theValue, _myMin, _myMax, 0, _myDimension.y);
  102. }
  103. }
  104. private CCVector2f _myStartPosition = new CCVector2f();
  105. @Override
  106. protected void onDragg(CCMouseEvent theEvent) {
  107. CCVector2f myEventPosition = CCVecMath.subtract(theEvent.position(), _myStartPosition);
  108. if(theEvent.isShiftDown()) {
  109. myEventPosition.scale(0.1f);
  110. myEventPosition = CCVecMath.add(_myStartPosition, myEventPosition);
  111. }else {
  112. _myStartPosition = theEvent.position().clone();
  113. myEventPosition = _myStartPosition;
  114. }
  115. changeValue(positionToValue(myEventPosition.x, myEventPosition.y));
  116. if(_myIsHorizontal){
  117. _myForeGroundSize = CCMath.constrain(myEventPosition.x - _myPosition.x, 0, _myDimension.x);
  118. }else{
  119. _myForeGroundSize = CCMath.constrain(myEventPosition.y - _myPosition.y, 0, _myDimension.y);
  120. }
  121. }
  122. @Override
  123. protected void onPress(CCMouseEvent theEvent) {
  124. super.onPress(theEvent);
  125. _myStartPosition = theEvent.position().clone();
  126. changeValue(positionToValue(_myStartPosition.x,_myStartPosition.y));
  127. if(_myIsHorizontal){
  128. _myForeGroundSize = _myStartPosition.x - _myPosition.x;
  129. }else{
  130. _myForeGroundSize = _myStartPosition.y - _myPosition.y;
  131. }
  132. }
  133. @Override
  134. public void onMove( CCMouseEvent theEvent ) {
  135. super.onMove(theEvent);
  136. _myInputPosition.x = theEvent.x();
  137. _myInputPosition.y = theEvent.y() - 4;
  138. }
  139. /**
  140. * When selected the value can be typed into the slider. Any keyboard
  141. * input will be appended to the value string.
  142. * Pressing return tries to set the value to the typed number.
  143. * Wrong input won't result in any changes.
  144. * Hit "backspace" to cancel input!
  145. */
  146. @Override
  147. public void onKey( CCKeyEvent theEvent ) {
  148. int myKeyCode = theEvent.keyCode();
  149. if (theEvent.keyChar() == '\n' && _myKeyboardInput.length() > 0) {
  150. Float myValue = Float.parseFloat(_myKeyboardInput.toString());
  151. this.changeValue(CCMath.constrain(myValue, _myMin, _myMax));
  152. _myKeyboardInput.setLength(0);
  153. } else if (myKeyCode == KeyEvent.VK_BACK_SPACE) {
  154. _myKeyboardInput.setLength(0);
  155. } else if ((myKeyCode >= KeyEvent.VK_0 && myKeyCode <= KeyEvent.VK_9)
  156. || (myKeyCode == KeyEvent.VK_MINUS && _myKeyboardInput.length() == 0)) {
  157. _myKeyboardInput.append(theEvent.keyChar());
  158. } else if (myKeyCode == KeyEvent.VK_PERIOD) {
  159. if (_myKeyboardInput.indexOf(".") == -1) {
  160. _myKeyboardInput.append(theEvent.keyChar());
  161. }
  162. } else {
  163. return;
  164. }
  165. _myInputText.text(_myKeyboardInput.toString());
  166. }
  167. @Override
  168. public void draw(CCGraphics g) {
  169. if(_myIsHorizontal){
  170. g.color(_myForeGround);
  171. g.rect(
  172. _myPosition.x,_myPosition.y,
  173. _myForeGroundSize, _myDimension.y
  174. );
  175. g.color(_myBackGround);
  176. g.rect(
  177. _myPosition.x+_myForeGroundSize,_myPosition.y,
  178. _myDimension.x-_myForeGroundSize, _myDimension.y
  179. );
  180. g.color(0f,0.5f);
  181. g.rect(_myLabel.position().x-2, _myLabel.position().y-_myLabel.height() - 2, _myLabel.width()+4, _myLabel.height()+4);
  182. g.color(_myUIColor.colorLabel);
  183. _myLabel.draw(g);
  184. g.color(_myUIColor.colorValue);
  185. _myValueText.draw(g);
  186. // if (_myKeyboardInput.length() > 0) {
  187. // g.color(new CCColor(0.8f, 0.8f, 0.8f));
  188. //
  189. // g.rect(_myInputPosition.x-2, _myInputPosition.y+2,
  190. // _myInputText.width()+3, -_myDimension.y);
  191. // _myInputText.position(_myInputPosition);
  192. // g.color(_myInputColor);
  193. // _myInputText.draw(g);
  194. // }
  195. }
  196. super.draw(g);
  197. }
  198. @Override
  199. public Float relativeValue(final float theValue) {
  200. return CCMath.blend(_myMin, _myMax, theValue);
  201. }
  202. @Override
  203. public float changeFloatValue() {
  204. return CCMath.norm(value(), _myMin, _myMax);
  205. }
  206. public void value(final Float theValue){
  207. super.value(theValue);
  208. if(_myIsInteger)_myValueText.text(value().intValue());
  209. else _myValueText.text(value());
  210. _myForeGroundSize = valueToPostion(value());
  211. }
  212. public void preset(final int thePreset){
  213. super.preset(thePreset);
  214. _myForeGroundSize = valueToPostion(value());
  215. }
  216. /* (non-Javadoc)
  217. * @see cc.creativecomputing.control.CCUIValueElement#valueForXML(cc.creativecomputing.xml.CCXMLElement)
  218. */
  219. @Override
  220. public Float valueForXML(CCXMLElement theXMLElement) {
  221. return theXMLElement.floatAttribute("value",0);
  222. }
  223. }