1 package org.simantics.g3d.vtk.utils;
3 import javax.vecmath.Point2d;
4 import javax.vecmath.Point3d;
6 import org.simantics.g3d.scenegraph.RenderListener;
7 import org.simantics.g3d.vtk.common.VtkView;
9 import vtk.vtkTextActor;
10 import vtk.vtkTextProperty;
12 public class vtkToolTip<T> implements RenderListener{
15 long tooltipDelay = 1000;
17 double backgroundColor[] = new double[] {0.8,0.8,0.8};
18 double backgroundOpacity = 0.8;
19 double color[] = new double[] {0.0,0.0,0.0};
20 double frameColor[] = new double[] {0.0,0.0,0.0};
25 String tooltip = null;
26 boolean showing = false;
27 boolean changed = false;
32 public vtkToolTip(VtkView view) {
34 view.addListener(this);
37 public long getTooltipDelay() {
42 * Sets tooltip delay. Default is 1000 = 1s.
45 public void setTooltipDelay(long tooltipDelay) {
46 this.tooltipDelay = tooltipDelay;
50 * Sets background color. Default is light grey {0.8,0.8,0.8}
51 * @param backgroundColor
53 public void setBackgroundColor(double[] backgroundColor) {
54 this.backgroundColor = backgroundColor;
58 * Sets background opacity. Default is 0.8. 0.0 is fully transparent.
59 * @param backgroundOpacity
61 public void setBackgroundOpacity(double backgroundOpacity) {
62 this.backgroundOpacity = backgroundOpacity;
66 * Sets text color. Default is black {0.0,0.0,0.0}
69 public void setColor(double[] color) {
74 * Sets frame/border color. Default is black {0.0,0.0,0.0}
75 * Use null for no frame.
79 public void setFrameColor(double[] frameColor) {
80 this.frameColor = frameColor;
84 * Removes/hides current tooltip.
86 public void remove() {
94 //System.out.println("remove");
98 * Sets current tooltip.
100 * Position of the tooltip is based on projected 3d coordinate.
106 public void setHoverObject(T obj, Point3d pos, String tooltip) {
107 if (this.obj != null && this.obj == obj) {
114 this.tooltip = tooltip;
115 this.setTime = System.currentTimeMillis();
117 //System.out.println("setHoverObj " + obj + " " + pos + " " + tooltip);
122 * Sets current tooltip.
124 * Position of the tooltip is based given screen coordinate.
130 public void setHoverObject(T obj, Point2d pos, String tooltip) {
131 if (this.obj != null && this.obj == obj) {
138 this.tooltip = tooltip;
139 this.setTime = System.currentTimeMillis();
142 //System.out.println("setHoverObj " + obj + " " + pos + " " + tooltip);
147 public void preRender() {
148 if (tooltip == null) {
166 public void postRender() {
167 if (!showing && tooltip != null)
171 vtkTextActor textActor = null;
173 protected void addTooltip() {
174 //System.out.println("addTooltip");
177 long currentTime = System.currentTimeMillis();
178 if (currentTime-setTime < tooltipDelay)
181 if (textActor == null) {
182 textActor = new vtkTextActor();
183 vtkTextProperty prop = textActor.GetTextProperty();
184 prop.SetBackgroundColor(backgroundColor);
185 prop.SetBackgroundOpacity(backgroundOpacity);
186 prop.SetColor(color);
187 if (frameColor != null) {
188 prop.SetFrameColor(frameColor);
195 view.addDeletable(textActor);
197 textActor.SetInput(tooltip);
199 view.getRenderer().AddActor2D(textActor);
204 protected void removeTooltip() {
205 //System.out.println("removeTooltip");
208 if (textActor == null)
210 view.getRenderer().RemoveActor2D(textActor);
216 protected void updateTooltip() {
222 p = vtkUtil.getScreenCoordinates(view.getRenderer(), pos3d);
225 p = new Point2d(pos2d);
228 textActor.SetDisplayPosition((int)p.x, (int)p.y);
232 * Adjusts screen coordinates so that the tooltip fits into the window.
235 private void screenPoint(Point2d pos) {
236 double bounds[] = new double[4];
237 textActor.GetBoundingBox(view.getRenderer(), bounds);
238 int size[] = view.getRenderer().GetRenderWindow().GetSize();
242 Point2d min = new Point2d(pos.x-bounds[0], pos.y-bounds[2]);
243 Point2d max = new Point2d(pos.x+bounds[1]+1.0, pos.y+bounds[3]+1.0);
246 } else if (max.x > sw) {
247 pos.x -= (max.x - sw);
251 } else if (max.y > sh) {
252 pos.y -= (max.y - sh);