JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
BaseButton.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.graph.ui.shapes;
29
30import com.jogamp.graph.curve.OutlineShape;
31import com.jogamp.graph.curve.Region;
32import com.jogamp.graph.curve.opengl.GLRegion;
33import com.jogamp.graph.ui.GraphShape;
34import com.jogamp.graph.ui.Shape;
35import com.jogamp.opengl.GL2ES2;
36import com.jogamp.opengl.GLProfile;
37import com.jogamp.opengl.util.texture.TextureSequence;
38
39/**
40 * An abstract GraphUI filled base button {@link GraphShape},
41 * usually used as a backdrop or base shape for more informative button types.
42 * <p>
43 * GraphUI is GPU based and resolution independent.
44 * </p>
45 * <p>
46 * This button is rendered with a round oval shape {@link #ROUND_CORNER by default},
47 * but can be set to any roundness or {@link #PERP_CORNER rectangular shape} via {#link {@link #setCorner(float)} or {@link #setPerp()}.
48 * </p>
49 */
50public class BaseButton extends GraphShape {
51
52 /** {@link #setCorner(float) Round corner}, value {@value}. This is the default value. */
53 public static final float ROUND_CORNER = 1f;
54 /** {@link #setCorner(float) Perpendicular corner} for a rectangular shape, value {@value}. */
55 public static final float PERP_CORNER = 0f;
56
57 protected float width;
58 protected float height;
59 protected float corner = ROUND_CORNER;
60
61 /**
62 * Create a base button Graph based {@link GLRegion} UI {@link Shape} with a {@link #ROUND_CORNER}.
63 * <p>
64 * Call {#link {@link #setCorner(float)} or {@link #setPerp()} to modify the corner shape.
65 * </p>
66 * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
67 * @param width
68 * @param height
69 */
70 public BaseButton(final int renderModes, final float width, final float height) {
71 super(renderModes);
72 this.width = width;
73 this.height = height;
74 }
75
76 public final float getWidth() { return width; }
77
78 public final float getHeight() { return height; }
79
80 public final float getCorner() { return corner; }
81
82 /**
83 * Set corner size with range [0.01 .. 1.00] for round corners
84 * or `zero` for perpendicular corners.
85 * <p>
86 * Default is {@link #ROUND_CORNER round corner},
87 * alternative a {@link #PERP_CORNER perpendicular corner} for a rectangular shape is available.
88 * </p>
89 * @param corner corner size with range [0.01 .. 1.00 ] for round corners or `zero` for perpendicular corners.
90 * @return this instance for chaining
91 * @see #ROUND_CORNER
92 * @see #PERP_CORNER
93 * @see #setPerp()
94 */
95 public BaseButton setCorner(final float corner) {
96 if( corner > 1.0f ){
97 this.corner = ROUND_CORNER;
98 } else if( corner < 0.01f ){
99 this.corner = PERP_CORNER;
100 } else {
101 this.corner = corner;
102 }
104 return this;
105 }
106 /**
107 * Sets a {@link #PERP_CORNER perpendicular} {@link #setCorner(float) corner}.
108 * @return this instance for chaining
109 * @see #setCorner(float)
110 */
112 this.corner = PERP_CORNER;
114 return this;
115 }
116
117 public BaseButton setSize(final float width, final float height) {
118 if( this.width != width || this.height != height ) {
119 this.width = width;
120 this.height = height;
122 }
123 return this;
124 }
125
126 @Override
127 protected void addShapeToRegion(final GLProfile glp, final GL2ES2 gl) {
128 final OutlineShape shape = createBaseShape(0f);
129 resetGLRegion(glp, gl, null, shape);
130 region.addOutlineShape(shape, null, rgbaColor);
131 box.resize(shape.getBounds());
133 }
134
135 protected OutlineShape createBaseShape(final float zOffset) {
136 final OutlineShape shape = new OutlineShape();
137 if(corner == 0.0f) {
138 createSharpOutline(shape, zOffset);
139 } else {
140 createCurvedOutline(shape, zOffset);
141 }
142 shape.setIsQuadraticNurbs();
144 if( DEBUG_DRAW ) {
145 System.err.println("GraphShape.RoundButton: Shape: "+shape+", "+box);
146 }
147 return shape;
148 }
149
150 protected void createSharpOutline(final OutlineShape shape, final float zOffset) {
151 final float tw = getWidth();
152 final float th = getHeight();
153
154 final float minX = 0;
155 final float minY = 0;
156 final float minZ = zOffset;
157
158 shape.addVertex(minX, minY, minZ, true);
159 shape.addVertex(minX+tw, minY, minZ, true);
160 shape.addVertex(minX+tw, minY + th, minZ, true);
161 shape.addVertex(minX, minY + th, minZ, true);
162 shape.closeLastOutline(true);
163 }
164
165 protected void createCurvedOutline(final OutlineShape shape, final float zOffset) {
166 final float tw = getWidth();
167 final float th = getHeight();
168 final float dC = 0.5f*corner*Math.min(tw, th);
169
170 final float minX = 0;
171 final float minY = 0;
172 final float minZ = zOffset;
173
174 shape.addVertex(minX, minY + dC, minZ, true);
175 shape.addVertex(minX, minY, minZ, false);
176
177 shape.addVertex(minX + dC, minY, minZ, true);
178
179 shape.addVertex(minX + tw - dC, minY, minZ, true);
180 shape.addVertex(minX + tw, minY, minZ, false);
181 shape.addVertex(minX + tw, minY + dC, minZ, true);
182 shape.addVertex(minX + tw, minY + th- dC, minZ, true);
183 shape.addVertex(minX + tw, minY + th, minZ, false);
184 shape.addVertex(minX + tw - dC, minY + th, minZ, true);
185 shape.addVertex(minX + dC, minY + th, minZ, true);
186 shape.addVertex(minX, minY + th, minZ, false);
187 shape.addVertex(minX, minY + th - dC, minZ, true);
188
189 shape.closeLastOutline(true);
190 }
191
192 @Override
193 public String getSubString() {
194 return super.getSubString()+", dim "+getWidth() + " x " + getHeight() + ", corner " + corner;
195 }
196}
A Generic shape objects which is defined by a list of Outlines.
final void addVertex(final Vertex v)
Adds a vertex to the last open outline to the shape's tail.
final void setIsQuadraticNurbs()
Claim this outline's vertices are all OutlineShape.VerticesState#QUADRATIC_NURBS, hence no cubic tran...
final void setSharpness(final float s)
Sets sharpness, defaults to DEFAULT_SHARPNESS.
final void closeLastOutline(final boolean closeTail)
Closes the last outline in the shape.
final void addOutlineShape(final OutlineShape shape, final AffineTransform t, final Vec4f rgbaColor)
Add the given OutlineShape to this region with the given optional AffineTransform.
Definition: Region.java:616
Graph based GLRegion Shape.
Definition: GraphShape.java:55
final void resetGLRegion(final GLProfile glp, final GL2ES2 gl, final TextureSequence colorTexSeq, int vertexCount, int indexCount)
Reset the GLRegion and reserving its buffers to have a free capacity for vertexCount and indexCount e...
static final boolean DEBUG_DRAW
Definition: Shape.java:238
final void markShapeDirty()
Marks the shape dirty, causing next draw() to recreate the Graph shape and reset the region.
Definition: Shape.java:688
final Vec4f rgbaColor
Default base-color w/o color channel, will be modulated w/ pressed- and toggle color.
Definition: Shape.java:261
final Shape setRotationPivot(final float px, final float py, final float pz)
Set unscaled rotation origin, aka pivot.
Definition: Shape.java:620
An abstract GraphUI filled base button GraphShape, usually used as a backdrop or base shape for more ...
Definition: BaseButton.java:50
void createSharpOutline(final OutlineShape shape, final float zOffset)
static final float ROUND_CORNER
Round corner, value {@value}.
Definition: BaseButton.java:53
void addShapeToRegion(final GLProfile glp, final GL2ES2 gl)
BaseButton(final int renderModes, final float width, final float height)
Create a base button Graph based GLRegion UI Shape with a ROUND_CORNER.
Definition: BaseButton.java:70
BaseButton setPerp()
Sets a perpendicular corner.
OutlineShape createBaseShape(final float zOffset)
static final float PERP_CORNER
Perpendicular corner for a rectangular shape, value {@value}.
Definition: BaseButton.java:55
BaseButton setSize(final float width, final float height)
BaseButton setCorner(final float corner)
Set corner size with range [0.01 .
Definition: BaseButton.java:95
void createCurvedOutline(final OutlineShape shape, final float zOffset)
final AABBox resize(final AABBox newBox)
Resize the AABBox to encapsulate another AABox.
Definition: AABBox.java:274
final Vec3f getCenter()
Returns computed center of this AABBox of getLow() and getHigh().
Definition: AABBox.java:737
Specifies the the OpenGL profile.
Definition: GLProfile.java:77