1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Gustavo Barbosa Libotte
4  * Copyright (C) 2014 - Scilab Enterprises - Calixte DENIZET
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16 
17 package org.scilab.modules.gui.datatip;
18 
19 import org.scilab.modules.graphic_objects.PolylineData;
20 import org.scilab.modules.renderer.utils.DatatipCommon;
21 import org.scilab.modules.gui.datatip.DatatipOrientation;
22 import org.scilab.modules.renderer.CallRenderer;
23 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
24 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.*;
25 import org.scilab.modules.renderer.utils.CommonHandler;
26 import org.scilab.modules.renderer.utils.EntityPicker;
27 
28 /**
29  * Move a datatip along the curve
30  * @author Gustavo Barbosa Libotte
31  */
32 public class DatatipMove {
33 
34     /**
35      * Move a datatip to the right using keyboard
36      *
37      * @param markerUid datatip marker unique identifier
38      */
moveRight(Integer datatipUid)39     public static void moveRight(Integer datatipUid) {
40         move(datatipUid, 1);
41     }
42 
43     /**
44      * Move a datatip to the left using keyboard
45      *
46      * @param datatipUid datatip unique identifier
47      */
moveLeft(Integer datatipUid)48     public static void moveLeft(Integer datatipUid) {
49         move(datatipUid, -1);
50     }
51 
52     /*
53      * moves the datatip in the given dir(X axis)
54      * seg_offset is used when interp_mode is off so the
55      * datatip is shifted in the right direction (0 -> left, 2 -> right,
56      * the number 0 and 2 is because getSegment returns one segment more
57      * the right would be -1, 1. Probably this is because float round error.
58      */
move(Integer datatipUid, int dir)59     private static void move(Integer datatipUid, int dir) {
60         Integer parentPolyline = DatatipCommon.getParentPolyline(datatipUid);
61         if (parentPolyline != null) {
62             GraphicController controller = GraphicController.getController();
63             Integer axes = (Integer) controller.getProperty(parentPolyline, __GO_PARENT_AXES__);
64             Double[] data = (Double[]) controller.getProperty(datatipUid, __GO_DATATIP_DATA__);
65             int index = (Integer) controller.getProperty(datatipUid, __GO_DATATIP_INDEXES__);
66             DatatipCommon.Segment seg = EntityPicker.getSegment(parentPolyline, index);
67             double[] datax = (double[]) PolylineData.getDataX(parentPolyline);
68             Boolean useInterp = (Boolean) controller.getProperty(datatipUid, __GO_DATATIP_INTERP_MODE__) && CommonHandler.isLineEnabled(parentPolyline);
69             Boolean AutoOrientation = (Boolean) controller.getProperty(datatipUid, __GO_DATATIP_AUTOORIENTATION__);
70 
71             if (useInterp) {
72                 double[] start = CallRenderer.getPixelFrom3dCoordinates(axes, new double[] {seg.x0, seg.y0, seg.z0});
73                 double[] end = CallRenderer.getPixelFrom3dCoordinates(axes, new double[] {seg.x1, seg.y1, seg.z1});
74                 final double ns = Math.hypot(end[0] - start[0], end[1] - start[1]);
75                 final double nsx = (2 * dir * (end[0] - start[0])) / ns;
76                 final double nsy = (2 * dir * (end[1] - start[1])) / ns;
77 
78                 double[] pix_pos = CallRenderer.getPixelFrom3dCoordinates(axes, new double[] {data[0], data[1], data[2]});
79                 double[] info = EntityPicker.getNearestSegmentIndex(axes, parentPolyline, (int) (pix_pos[0] + nsx), (int) (pix_pos[1] + nsy));
80 
81                 controller.setProperty(datatipUid, __GO_DATATIP_INDEXES__, new Double[] {info[0], info[1]});
82                 if (AutoOrientation) {
83                     DatatipOrientation.setOrientation(datatipUid, EntityPicker.getSegment(parentPolyline, (int) info[0]));
84                 }
85             } else {
86                 double ind = Math.min(Math.max(index + dir, 0), datax.length - 1);
87                 controller.setProperty(datatipUid, __GO_DATATIP_INDEXES__, new Double[] {ind, 0.});
88                 if (AutoOrientation) {
89                     DatatipOrientation.setOrientation(datatipUid, EntityPicker.getSegment(parentPolyline, (int) ind));
90                 }
91             }
92         }
93     }
94 }
95