PageRenderTime 78ms CodeModel.GetById 62ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/org/mt4jx/components/visibleComponents/widgets/MTOptionBox.java

http://mt4j.googlecode.com/
Java | 171 lines | 98 code | 39 blank | 34 comment | 26 complexity | 57fbd45fcc674c951fc8349884f9da25 MD5 | raw file
  1package org.mt4jx.components.visibleComponents.widgets;
  2
  3import org.mt4j.components.css.style.CSSStyle;
  4import org.mt4j.components.visibleComponents.shapes.MTEllipse;
  5import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
  6import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
  7import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleProcessor;
  8import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
  9import org.mt4j.input.inputProcessors.componentProcessors.zoomProcessor.ZoomProcessor;
 10import org.mt4j.util.MTColor;
 11import org.mt4j.util.math.Vector3D;
 12
 13import processing.core.PApplet;
 14
 15/**
 16 * The Class MTOptionBox.
 17 */
 18public class MTOptionBox extends MTForm implements BooleanForm {
 19	
 20	/** The boolean value. */
 21	private boolean booleanValue = false;
 22	
 23//	/** The background color. */
 24//	private MTColor backgroundColor;
 25//	
 26//	/** The stroke color. */
 27//	private MTColor strokeColor;
 28	
 29	/** The option box. */
 30	private MTEllipse optionBox;
 31	
 32	/** The group. */
 33	private OptionGroup group;
 34	
 35	/**
 36	 * Instantiates a new MTOptionBox
 37	 * @param app the PApplet
 38	 * @param size the size of the ellipse
 39	 * @param group the OptionGroup
 40	 */
 41	public MTOptionBox(PApplet app,
 42			float size, OptionGroup group) {
 43		super(app, 0, 0, size, size, MTForm.BOOLEAN);
 44		group.addOptionBox(this);
 45		this.setCssForceDisable(true);
 46		this.setNoStroke(true);
 47		this.setNoFill(true);
 48		this.group = group;
 49		
 50		optionBox = new MTEllipse(app, new Vector3D(size/2f,size/2f), size/2f, size/2f);
 51		optionBox.setCssForceDisable(true);
 52		this.addChild(optionBox);
 53		
 54		
 55		this.style();
 56		
 57		optionBox.setPickable(false);
 58		optionBox.setNoFill(true);
 59		
 60		this.setGestureAllowance(TapProcessor.class, true);
 61		this.registerInputProcessor(new TapProcessor(app));
 62		this.addGestureListener(TapProcessor.class, new BooleanTapListener());
 63		
 64		this.setGestureAllowance(DragProcessor.class, false);
 65		this.setGestureAllowance(ScaleProcessor.class, false);
 66		this.setGestureAllowance(ZoomProcessor.class, false);
 67		this.setGestureAllowance(RotateProcessor.class, false);
 68		
 69	}
 70
 71	private void style() {
 72		
 73		//Check if it's CSS styled
 74		if (this.isCSSStyled() && optionBox != null && this.getCssHelper() != null) {
 75			
 76			CSSStyle vss = this.getCssHelper().getVirtualStyleSheet();
 77			
 78			this.setStrokeWeight(vss.getBorderWidth());
 79			this.setLineStipple(vss.getBorderStylePattern());
 80			
 81			
 82			if (vss.isModifiedBorderColor()) optionBox.setStrokeColor(vss.getBorderColor());
 83			else optionBox.setStrokeColor(MTColor.WHITE);
 84			
 85			if (vss.isModifiedBackgroundColor() && brightEnough(vss.getBackgroundColor())) {
 86				if (vss.getBackgroundColor().getAlpha() < 220) {
 87					MTColor color = vss.getBackgroundColor().getCopy();
 88					color.setAlpha(220);
 89					optionBox.setFillColor(color);
 90				} else optionBox.setFillColor(vss.getBackgroundColor());
 91			}
 92			else optionBox.setFillColor(MTColor.YELLOW);
 93			
 94			if (vss.isModifiedBorderWidth()) optionBox.setStrokeWeight(vss.getBorderWidth());
 95			else optionBox.setStrokeWeight(2f);
 96			
 97			
 98		} else if (optionBox != null){
 99			//Else set default values
100			optionBox.setStrokeColor(MTColor.WHITE);
101			optionBox.setFillColor(MTColor.YELLOW);
102			optionBox.setStrokeWeight(2f);
103		}
104	}
105	
106	private boolean brightEnough(MTColor color) {
107		return color.getR() + color.getG() + color.getB() > 200 && color.getAlpha() > 200;
108		
109		
110		
111	}
112	
113	@Override
114	public void applyStyleSheet() {
115		super.applyStyleSheet();
116		System.out.println("Styling now. CSSID: " + this.getCSSID());
117		style();
118		
119	}
120	
121	
122	/* (non-Javadoc)
123	 * @see org.mt4jx.components.generic.MTForm#getBooleanValue()
124	 */
125	@Override
126	public boolean getBooleanValue() {
127		return booleanValue;
128	}
129
130	/* (non-Javadoc)
131	 * @see org.mt4jx.components.generic.MTForm#getStringValue()
132	 */
133	@Override
134	public String getStringValue() {
135		return String.valueOf(this.getBooleanValue());
136	}
137
138	/* (non-Javadoc)
139	 * @see org.mt4jx.components.generic.MTForm#getNumericValue()
140	 */
141	@Override
142	public float getNumericValue() {
143		if (this.getBooleanValue() == true) return 1;
144		else return 0;
145	}
146
147	/* (non-Javadoc)
148	 * @see org.mt4jx.components.generic.MTForm#setBooleanValue(boolean)
149	 */
150	@Override
151	public void setBooleanValue(boolean value) {
152		this.booleanValue = value;
153		if (this.booleanValue == true) {
154			optionBox.setNoFill(false);
155			if (group != null)
156			group.setEnabled(this);
157		} else {
158			optionBox.setNoFill(true);
159		}
160		
161	}
162	
163	/**
164	 * Disable.
165	 */
166	public void disable() {
167		this.booleanValue = false;
168		optionBox.setNoFill(true);
169	}
170
171}