aboutsummaryrefslogtreecommitdiffstats
path: root/src/graphui/classes/com/jogamp/graph/ui/Tooltip.java
blob: fd0bb6c2cc27c2d941403fba4a04ff4dbe842b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
 * Copyright 2023-2024 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */
package com.jogamp.graph.ui;

import com.jogamp.common.os.Clock;
import com.jogamp.graph.curve.Region;
import com.jogamp.graph.curve.opengl.GLRegion;
import com.jogamp.graph.curve.opengl.RegionRenderer;
import com.jogamp.math.Vec2f;
import com.jogamp.math.Vec4f;
import com.jogamp.math.geom.AABBox;
import com.jogamp.math.util.PMVMatrix4f;
import com.jogamp.opengl.GL2ES2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.util.texture.TextureSequence;

/** A HUD tooltip for {@link Shape}, see {@link Shape#setToolTip(Tooltip)}. */
public abstract class Tooltip {

    /** Default tooltip delay is {@value}ms */
    public static final long DEFAULT_DELAY = 1000;

    /** Delay in ms, zero implies no time based alarm */
    private final long delayMS;
    /** Alarm t1, time to show tooltip, i.e. t0 + delayMS, if delayMS > 0 */
    private volatile long alarmT1;
    /** Toggle for forced tooltip display */
    private volatile boolean forced;
    /** Shape 'tool' owning this tooltip. */
    private Shape tool;
    /** Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}. */
    protected final int renderModes;
    protected final Vec4f backColor = new Vec4f(1, 1, 0, 1);
    protected final Vec4f frontColor = new Vec4f(0.1f, 0.1f, 0.1f, 1);

    @Override
    public String toString() {
        return "Tooltip[d "+delayMS+", next "+alarmT1+", forced "+forced+"]";
    }
    /**
     *
     * @param backColor optional HUD tip background color
     * @param frontColor optional HUD tip front color
     * @param delayMS delay until HUD tip is visible after timer start (mouse moved), zero implies no time based alarm
     * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
     */
    protected Tooltip(final Vec4f backColor, final Vec4f frontColor, final long delayMS, final int renderModes) {
        this.delayMS = delayMS;
        this.alarmT1 = 0;
        this.forced = false;
        this.tool = null;
        this.renderModes = renderModes;
        if( null != backColor ) {
            this.backColor.set(backColor);
        }
        if( null != frontColor ) {
            this.frontColor.set(frontColor);
        }
    }
    /* pp */ final void setTool(final Shape tool) { this.tool = tool; }

    /** Returns {@link Shape} 'tool' owning this tooltip, set after {@link Shape#setToolTip(Tooltip)}. */
    public final Shape getTool() {
        return tool;
    }

    /**
     * Stops the timer if not enforced via {@link #now()} or {@code clearForced} is true.
     * @param clearForced if true, also clears enforced flag set by {@link #now()}
     * @return true if timer has been stopped, otherwise false
     */
    public final boolean stop(final boolean clearForced) {
        if( clearForced ) {
            this.alarmT1 = 0;
            this.forced = false;
            return true;
        } else if( !this.forced ) {
            this.alarmT1 = 0;
            return true;
        } else {
            return false;
        }
    }

    /** Starts the timer. */
    public final void start() {
        if( !this.forced && delayMS > 0 ) {
            this.alarmT1 = Clock.currentMillis() + delayMS;
        }
    }

    /** Enforce tooltip display with next {@link #tick()}. */
    public final void now() {
        this.forced = true;
        this.alarmT1 = Clock.currentMillis() - 1;
    }

    /** Returns true if display is enforced via {@link #now()}. */
    public final boolean forced() {
        return forced;
    }

    /**
     * Send tick to this tooltip
     * @return true if {@link #start() started} timer has been reached or is enforced via {@link #now()} to {@link #createTip(PMVMatrix4f)}, otherwise false
     */
    public final boolean tick() {
        if( 0 == alarmT1 ) {
            return false;
        }
        if( Clock.currentMillis() < alarmT1 ) {
            return false;
        }
        this.alarmT1 = 0;
        this.forced = false;
        return true;
    }

    /** Little helper for {@link #createTip(GLAutoDrawable, Scene, PMVMatrix4f, AABBox)} returning the Mv {@link AABBox} of the tool within {@link Scene} Mv space. */
    public AABBox getToolMvBounds(final PMVMatrix4f pmv) {
        return getTool().getBounds().transform(pmv.getMv(), new AABBox());
    }
    /** Little helper for {@link #createTip(GLAutoDrawable, Scene, PMVMatrix4f, AABBox)} returning the Mv position of the tip within {@link Scene} Mv space. */
    public Vec2f getTipMvPosition(final Scene scene, final PMVMatrix4f pmv, final float tipWidth, final float tipHeight) {
        return getTipMvPosition(scene, getToolMvBounds(pmv), tipWidth, tipHeight);
    }
    /** Little helper for {@link #createTip(GLAutoDrawable, Scene, PMVMatrix4f, AABBox)} returning the Mv position of the tip within {@link Scene} Mv space. */
    public Vec2f getTipMvPosition(final Scene scene, final AABBox toolMvBounds, final float tipWidth, final float tipHeight) {
        final AABBox sceneAABox = scene.getBounds();
        final Vec2f pos = new Vec2f();
        if( toolMvBounds.getCenter().x() - tipWidth/2 >= sceneAABox.getLow().x() ) {
            pos.setX( toolMvBounds.getCenter().x()-tipWidth/2 );
        } else {
            pos.setX( sceneAABox.getLow().x() );
        }
        if( toolMvBounds.getHigh().y() + tipHeight <= sceneAABox.getHigh().y()  ) {
            pos.setY( toolMvBounds.getHigh().y() );
        } else if( toolMvBounds.getHigh().y() >= tipHeight ) {
            pos.setY( toolMvBounds.getHigh().y() - tipHeight );
        } else {
            pos.setY( sceneAABox.getHigh().y() - tipHeight );
        }
        return pos;
    }

    /**
     * Create a new HUD tip shape, usually called by {@link Scene}
     * @param drawable current {@link GLAutoDrawable}
     * @param scene the {@link Scene} caller for which this HUD tip shape is created
     * @param pmv {@link PMVMatrix4f}, which shall be properly initialized, e.g. via {@link Scene#setupMatrix(PMVMatrix4f)}
     * @param toolMvBounds {@link AABBox} of the {@link #getTool()} in model-view (Mv) space of the given {@link Scene}
     * @return newly created HUD tip shape
     * @see #destroyTip(GL2ES2, RegionRenderer, Shape)
     */
    public abstract Shape createTip(final GLAutoDrawable drawable, final Scene scene, final PMVMatrix4f pmv, AABBox toolMvBounds);

    /**
     * Destroy a {@link #createTip(GLAutoDrawable, Scene, PMVMatrix4f, AABBox) created} HUD tip.
     * <p>
     * Called after {@link Scene#removeShape(Shape)}, allowing implementation to perform certain
     * resource cleanup tasks. Even keeping the {@link Shape} tip alive is possible.
     * </p>
     * <p>
     * This default implementation simply calls {@link Shape#destroy(GL2ES2, RegionRenderer)}.
     * </p>
     * @param gl current {@link GL2ES2}
     * @param renderModes Graph's {@link Region} render modes, see {@link GLRegion#create(GLProfile, int, TextureSequence) create(..)}.
     * @param tip
     * @see #createTip(GLAutoDrawable, Scene, PMVMatrix4f, AABBox)
     */
    public void destroyTip(final GL2ES2 gl, final RegionRenderer renderer, final Shape tip) {
        tip.destroy(gl, renderer);
    }

}