/src/org/mt4j/components/visibleComponents/widgets/MTTextField.java

http://mt4j.googlecode.com/ · Java · 110 lines · 37 code · 14 blank · 59 comment · 6 complexity · bdfce7d6ca39932277cd3d40f43045a5 MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2010 Christopher Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. ***********************************************************************/
  18. package org.mt4j.components.visibleComponents.widgets;
  19. import org.mt4j.components.TransformSpace;
  20. import org.mt4j.components.visibleComponents.font.IFont;
  21. import org.mt4j.components.visibleComponents.font.IFontCharacter;
  22. import processing.core.PApplet;
  23. /**
  24. * The Class MTTextField. This is a modifed text area, that
  25. * keeps a fixed size and ignores new lines ("\n").
  26. * So this is for single line of text with a fixed size no matter
  27. * how long the actual text is.
  28. *
  29. * @author Christopher Ruff
  30. */
  31. public class MTTextField extends MTTextArea {
  32. /**
  33. * Instantiates a new mT text field.
  34. *
  35. * @param x the x
  36. * @param y the y
  37. * @param width the width
  38. * @param height the height
  39. * @param font the font
  40. * @param applet the applet
  41. * @deprecated constructor will be deleted! Please , use the constructor with the PApplet instance as the first parameter.
  42. */
  43. public MTTextField(float x, float y, float width, float height, IFont font, PApplet applet) {
  44. this(applet, x, y, width, height, font);
  45. }
  46. /**
  47. * Instantiates a new mT text field.
  48. * @param applet the applet
  49. * @param x the x
  50. * @param y the y
  51. * @param width the width
  52. * @param height the height
  53. * @param font the font
  54. */
  55. public MTTextField(PApplet applet, float x, float y, float width, float height, IFont font) {
  56. super(applet, x, y, width,height, font);
  57. }
  58. /* (non-Javadoc)
  59. * @see org.mt4j.components.visibleComponents.widgets.MTTextArea#characterAdded(org.mt4j.components.visibleComponents.font.IFontCharacter)
  60. */
  61. protected void characterAdded(IFontCharacter character){
  62. //Intercept new line characters
  63. if (character.getUnicode().equalsIgnoreCase("\n")){
  64. this.removeLastCharacter();
  65. }else{
  66. //Scroll the text to the left if end of field reached
  67. float localWidth = this.getWidthXY(TransformSpace.LOCAL);
  68. if (this.getText().length() > 0 && getLastCharEndPos() > localWidth) {
  69. float diff = getLastCharEndPos() - localWidth;
  70. this.scrollTextX(-diff);
  71. }
  72. }
  73. }
  74. /* (non-Javadoc)
  75. * @see org.mt4j.components.visibleComponents.widgets.MTTextArea#characterRemoved(org.mt4j.components.visibleComponents.font.IFontCharacter)
  76. */
  77. @Override
  78. protected void characterRemoved(IFontCharacter character) {
  79. //Scroll the text to the right if scrolled
  80. if (this.getText().length() > 0 && this.getScrollTextX() < 0){
  81. if (this.getMaxLineWidth() < this.getWidthXY(TransformSpace.LOCAL)){
  82. this.scrollTextX(Math.abs(this.getScrollTextX()));
  83. }else{
  84. this.scrollTextX(this.getWidthXY(TransformSpace.LOCAL) - this.getLastCharEndPos());
  85. }
  86. }
  87. }
  88. /**
  89. * Gets the last char end pos.
  90. *
  91. * @return the last char end pos
  92. */
  93. private float getLastCharEndPos(){
  94. return this.getMaxLineWidth() + this.getScrollTextX();
  95. }
  96. }