]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/function/TranslateFlag.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / function / TranslateFlag.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.function;
13
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Point2D;
16 import java.util.Arrays;
17 import java.util.Map;
18
19 public class TranslateFlag {
20         public static final float lineWidth = 0.3F;
21         private FlagInfo flagInfo = null;
22         private Map<String, FlagTableInfo> tables = null;
23         private double[] transform = null;
24         private double gridSize = 0.0;
25
26         public TranslateFlag(FlagInfo flagInfo, Map<String, FlagTableInfo> tables, double[] transform, double gridSize){
27                 this.flagInfo = flagInfo;
28                 this.tables = tables;
29                 this.transform = transform;
30                 this.gridSize = gridSize;
31         }
32
33         public double[] perform(){
34                 // flag transforming
35                 FlagTableInfo table = tables.get(flagInfo.flagTableName);
36                 if (table == null)
37                         return this.transform;
38
39 //              Double x = table.getPosition().getX()+((table.getAlignment()==TemplateInfo.ALIGN_RIGHT)?table.getWidth():0.0);
40 //              Double y = table.getPosition().getY()+table.getRowHeigth()*(flagInfo.getSlotTableRowIndex())-2.0F+TemplateInfo.BASELINE_VERTICAL_OFFSET*table.getRowHeigth();
41 //        double[] copy = Arrays.copyOf(transform, transform.length);
42 //        copy[4] = x;
43 //        copy[5] = y;
44 //        return copy;
45
46
47                 //double yy = table.getRowHeigth()*(flagInfo.getSlotTableRowIndex())-2.0F+TemplateInfo.BASELINE_VERTICAL_OFFSET*table.getRowHeigth();
48                 double[] tableTrans = table.getTransform();
49                 double scale = tableTrans[3];
50                 double scaledRowHeigth = scale*table.getRowHeight();
51
52                 // move to coordinate system of parent of iotable
53                 double rowMiddleY = tableTrans[5]+(flagInfo.flagTableRowIndex+0.5F)*scaledRowHeigth;
54                 if (gridSize != 0.0) {
55                         double modulo = rowMiddleY%gridSize;
56                         int count =(int) (rowMiddleY/gridSize);
57                         double gridOnRow = (modulo > (gridSize/2))?gridSize*(1+count):gridSize*count;
58                         double diff = (gridOnRow > rowMiddleY)? gridOnRow - rowMiddleY:rowMiddleY-gridOnRow;
59                         if (diff < (scaledRowHeigth/2))
60                                 rowMiddleY = gridOnRow; 
61                 }
62                 // move back to iotable coordinate system
63                 rowMiddleY = (rowMiddleY-tableTrans[5])/scale;
64
65 //              double yy = 0.0;
66 //              yy = (flagInfo.getSlotTableRowIndex()+0.5F)*table.getRowHeigth()-lineWidth/2;
67 //              double yy2 = 0.0;
68 //
69 //              // calculate flag position if it is snap to grid
70 //              if (gridSize != 0.0) {
71 //                      double modulo = yy%gridSize;
72 //                      int count =(int) (yy/gridSize);
73 //                      yy2 = ((yy%gridSize) > (gridSize/2))?gridSize*(1+ (int)(yy/gridSize))-lineWidth/2:gridSize*(int)(yy/gridSize)-lineWidth/2;
74 //              }
75 //
76 //              // check that the nearist grid layes on current row
77 //              if (yy2-yy < table.getRowHeigth()/2)
78 //                      yy = yy2;
79
80                 double xx = ((table.getAlignment()==DrawingTemplateInfo.ALIGN_RIGHT)?table.getWidth():0.0); 
81
82                 AffineTransform trans = new AffineTransform(new double[]{tableTrans[0],tableTrans[1],tableTrans[2],tableTrans[3],0.0,0.0});
83                 Point2D point = new Point2D.Double(xx, rowMiddleY);
84                 trans.transform(point, point);
85
86                 double[] copy = Arrays.copyOf(transform, transform.length);
87                 copy[4] = tableTrans[4] + point.getX();
88                 copy[5] = tableTrans[5] + point.getY();
89                 return copy;
90 //              Double x = table.getTransform()[4]+;
91 //              Double y = table.getTransform()[5]+;
92 //              
93 //        double[] copy = Arrays.copyOf(transform, transform.length);
94 //        copy[4] = x;//transform[4];
95 //        copy[5] = y;
96 //        return copy;
97
98 //        Session session = Simantics.peekSession();
99 //        if (session == null)
100 //              return;
101 //        session.asyncRequest(new WriteRequest(){
102 //                      @Override
103 //                      public void perform(WriteGraph g) throws DatabaseException {
104 //                              TranslateElement.absolute(flagInfo.getFlag(), position).perform(g);
105 //                      }});
106         }
107 }