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};
26 String tooltip = null;
27 boolean showing = false;
28 boolean changed = false;
33 public vtkToolTip(VtkView view) {
35 view.addListener(this);
38 public long getTooltipDelay() {
43 * Sets tooltip delay. Default is 1000 = 1s.
46 public void setTooltipDelay(long tooltipDelay) {
47 this.tooltipDelay = tooltipDelay;
51 * Sets background color. Default is light grey {0.8,0.8,0.8}
52 * @param backgroundColor
54 public void setBackgroundColor(double[] backgroundColor) {
55 this.backgroundColor = backgroundColor;
59 * Sets background opacity. Default is 0.8. 0.0 is fully transparent.
60 * @param backgroundOpacity
62 public void setBackgroundOpacity(double backgroundOpacity) {
63 this.backgroundOpacity = backgroundOpacity;
67 * Sets text color. Default is black {0.0,0.0,0.0}
70 public void setColor(double[] color) {
75 * Sets frame/border color. Default is black {0.0,0.0,0.0}
76 * Use null for no frame.
80 public void setFrameColor(double[] frameColor) {
81 this.frameColor = frameColor;
84 public void setFontSize(int fontSize) {
85 this.fontSize = fontSize;
89 * Removes/hides current tooltip.
91 public void remove() {
99 //System.out.println("remove");
103 * Sets current tooltip.
105 * Position of the tooltip is based on projected 3d coordinate.
111 public void setHoverObject(T obj, Point3d pos, String tooltip) {
112 if (this.obj != null && this.obj == obj) {
119 this.tooltip = tooltip;
120 this.setTime = System.currentTimeMillis();
122 //System.out.println("setHoverObj " + obj + " " + pos + " " + tooltip);
127 * Sets current tooltip.
129 * Position of the tooltip is based given screen coordinate.
135 public void setHoverObject(T obj, Point2d pos, String tooltip) {
136 if (this.obj != null && this.obj == obj) {
143 this.tooltip = tooltip;
144 this.setTime = System.currentTimeMillis();
147 //System.out.println("setHoverObj " + obj + " " + pos + " " + tooltip);
152 public void preRender() {
153 if (tooltip == null) {
171 public void postRender() {
172 if (!showing && tooltip != null)
176 vtkTextActor textActor = null;
178 protected void addTooltip() {
179 //System.out.println("addTooltip");
182 long currentTime = System.currentTimeMillis();
183 if (currentTime-setTime < tooltipDelay)
186 if (textActor == null) {
187 textActor = new vtkTextActor();
188 vtkTextProperty prop = textActor.GetTextProperty();
189 prop.SetBackgroundColor(backgroundColor);
190 prop.SetBackgroundOpacity(backgroundOpacity);
191 prop.SetColor(color);
192 if (frameColor != null) {
193 prop.SetFrameColor(frameColor);
198 prop.SetFontSize(view.upscale(fontSize));
201 view.addDeletable(textActor);
203 textActor.SetInput(tooltip);
205 view.getRenderer().AddActor2D(textActor);
210 protected void removeTooltip() {
211 //System.out.println("removeTooltip");
214 if (textActor == null)
216 view.getRenderer().RemoveActor2D(textActor);
222 protected void updateTooltip() {
228 p = vtkUtil.getScreenCoordinates(view.getRenderer(), pos3d);
231 p = new Point2d(pos2d);
234 textActor.SetDisplayPosition((int)p.x, (int)p.y);
238 * Adjusts screen coordinates so that the tooltip fits into the window.
241 private void screenPoint(Point2d pos) {
242 double bounds[] = new double[4];
243 textActor.GetBoundingBox(view.getRenderer(), bounds);
244 int size[] = view.getRenderer().GetRenderWindow().GetSize();
248 Point2d min = new Point2d(pos.x-bounds[0], pos.y-bounds[2]);
249 Point2d max = new Point2d(pos.x+bounds[1]+1.0, pos.y+bounds[3]+1.0);
252 } else if (max.x > sw) {
253 pos.x -= (max.x - sw);
257 } else if (max.y > sh) {
258 pos.y -= (max.y - sh);