PageRenderTime 22ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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 ***********************************************************************/
 18package org.mt4j.components.visibleComponents.widgets;
 19
 20import org.mt4j.components.TransformSpace;
 21import org.mt4j.components.visibleComponents.font.IFont;
 22import org.mt4j.components.visibleComponents.font.IFontCharacter;
 23
 24import processing.core.PApplet;
 25
 26/**
 27 * The Class MTTextField. This is a modifed text area, that
 28 * keeps a fixed size and ignores new lines ("\n").
 29 * So this is for single line of text with a fixed size no matter
 30 * how long the actual text is.
 31 * 
 32 * @author Christopher Ruff
 33 */
 34public class MTTextField extends MTTextArea {
 35
 36	
 37	/**
 38	 * Instantiates a new mT text field.
 39	 *
 40	 * @param x the x
 41	 * @param y the y
 42	 * @param width the width
 43	 * @param height the height
 44	 * @param font the font
 45	 * @param applet the applet
 46	 * @deprecated constructor will be deleted! Please , use the constructor with the PApplet instance as the first parameter.
 47	 */
 48	public MTTextField(float x, float y, float width, float height, IFont font, PApplet applet) {
 49		this(applet, x, y, width, height, font);
 50	}
 51	
 52	
 53	/**
 54	 * Instantiates a new mT text field.
 55	 * @param applet the applet
 56	 * @param x the x
 57	 * @param y the y
 58	 * @param width the width
 59	 * @param height the height
 60	 * @param font the font
 61	 */
 62	public MTTextField(PApplet applet, float x, float y, float width, float height, IFont font) {
 63		super(applet, x, y, width,height, font);
 64	}
 65	
 66	
 67	/* (non-Javadoc)
 68	 * @see org.mt4j.components.visibleComponents.widgets.MTTextArea#characterAdded(org.mt4j.components.visibleComponents.font.IFontCharacter)
 69	 */
 70	protected void characterAdded(IFontCharacter character){
 71		//Intercept new line characters
 72		if (character.getUnicode().equalsIgnoreCase("\n")){
 73			this.removeLastCharacter(); 
 74		}else{
 75			//Scroll the text to the left if end of field reached
 76			float localWidth = this.getWidthXY(TransformSpace.LOCAL);
 77			if (this.getText().length() > 0 && getLastCharEndPos() > localWidth) {
 78			float diff = getLastCharEndPos() - localWidth;
 79			this.scrollTextX(-diff);
 80			}
 81		}
 82	}
 83	
 84	/* (non-Javadoc)
 85	 * @see org.mt4j.components.visibleComponents.widgets.MTTextArea#characterRemoved(org.mt4j.components.visibleComponents.font.IFontCharacter)
 86	 */
 87	@Override
 88	protected void characterRemoved(IFontCharacter character) {
 89		//Scroll the text to the right if scrolled
 90		if (this.getText().length() > 0 && this.getScrollTextX() < 0){
 91			if (this.getMaxLineWidth() < this.getWidthXY(TransformSpace.LOCAL)){
 92				this.scrollTextX(Math.abs(this.getScrollTextX()));	
 93			}else{
 94				this.scrollTextX(this.getWidthXY(TransformSpace.LOCAL) - this.getLastCharEndPos());	
 95			}
 96		}
 97	}
 98	
 99	/**
100	 * Gets the last char end pos.
101	 * 
102	 * @return the last char end pos
103	 */
104	private float getLastCharEndPos(){
105		return this.getMaxLineWidth() + this.getScrollTextX();
106	}
107	
108
109}
110