/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

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