1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.glass.ui.monocle.input.devices;
  27 
  28 import com.sun.glass.ui.monocle.TestApplication;
  29 import com.sun.glass.ui.monocle.TestLog;
  30 import javafx.geometry.Rectangle2D;
  31 
  32 import java.util.HashMap;
  33 import java.util.Map;
  34 
  35 public abstract class TestTouchDevice extends TestDevice {
  36 
  37     protected final int[] transformedXs;
  38     protected final int[] transformedYs;
  39     protected final double[] xs;
  40     protected final double[] ys;
  41     protected final boolean[] points;
  42     protected int pressedPoints = 0;
  43     protected int previousPressedPoints = 0;
  44     private double absXMax, absYMax;
  45     private Map<Integer, Integer> ids = new HashMap<>();
  46 
  47     public TestTouchDevice(int maxPointCount) {
  48         this.transformedXs = new int[maxPointCount];
  49         this.transformedYs = new int[maxPointCount];
  50         this.xs = new double[maxPointCount];
  51         this.ys = new double[maxPointCount];
  52         this.points = new boolean[maxPointCount];
  53     }
  54 
  55     public int getPointCount() {
  56         return points.length;
  57     }
  58 
  59     public int getPressedPoints() {
  60         return pressedPoints;
  61     }
  62 
  63     protected int getID(int p) {
  64         if (ids.containsKey(p)) {
  65             return ids.get(p);
  66         } else {
  67             // assign a new ID
  68             int id = 1;
  69             while (ids.containsValue(id)) {
  70                 id ++;
  71             }
  72             ids.put(p, id);
  73             return id;
  74         }
  75     }
  76 
  77     protected int transformX(double x) {
  78         if (absXMax == 0.0) {
  79             return (int) Math.round(x);
  80         } else {
  81             Rectangle2D r = TestApplication.getScreenBounds();
  82             return (int) Math.round(x * absXMax / r.getWidth());
  83         }
  84     }
  85 
  86     protected int transformY(double y) {
  87         if (absXMax == 0.0) {
  88             return (int) Math.round(y);
  89         } else {
  90             Rectangle2D r = TestApplication.getScreenBounds();
  91             return (int) Math.round(y * absYMax / r.getHeight());
  92         }
  93     }
  94 
  95     public int addPoint(double x, double y) {
  96         int point = -1;
  97         for (int i = 0; i < points.length; i++) {
  98             if (!points[i]) {
  99                 point = i;
 100                 break;
 101             }
 102         }
 103         if (point == -1) {
 104             throw new IllegalStateException("Cannot add any more points");
 105         }
 106         TestLog.format("TestTouchDevice: addPoint %d, %.0f, %.0f\n",
 107                        point, x, y);
 108         xs[point] = x;
 109         ys[point] = y;
 110         transformedXs[point] = transformX(x);
 111         transformedYs[point] = transformY(y);
 112         points[point] = true;
 113         pressedPoints ++;
 114         return point;
 115     }
 116 
 117     public void removePoint(int point) {
 118         TestLog.format("TestTouchDevice: removePoint %d\n", point);
 119         if (!points[point]) {
 120             throw new IllegalStateException("Point not pressed");
 121         }
 122         points[point] = false;
 123         pressedPoints --;
 124         ids.remove(point);
 125     }
 126 
 127     public void setPoint(int point, double x, double y) {
 128         TestLog.format("TestTouchDevice: setPoint %d, %.0f, %.0f\n",
 129                        point, x, y);
 130         if (!points[point]) {
 131             throw new IllegalStateException("Point not pressed");
 132         }
 133         xs[point] = x;
 134         ys[point] = y;
 135         transformedXs[point] = transformX(x);
 136         transformedYs[point] = transformY(y);
 137     }
 138 
 139     public void setAndRemovePoint(int point, double x, double y) {
 140         TestLog.format("TestTouchDevice: setAndRemovePoint %d, %.0f, %.0f\n",
 141                        point, x, y);
 142         setPoint(point, x, y);
 143         removePoint(point);
 144     }
 145 
 146     @Override
 147     public void sync() {
 148         TestLog.log("TestTouchDevice: sync");
 149         super.sync();
 150         previousPressedPoints = pressedPoints;
 151     }
 152 
 153     public void resendStateAndSync() {
 154         TestLog.log("TestTouchDevice: sync");
 155         sync();
 156     }
 157 
 158     protected void setAbsScale(int absXMax, int absYMax) {
 159         this.absXMax = (double) absXMax;
 160         this.absYMax = (double) absYMax;
 161     }
 162 
 163     public int getTapRadius() {
 164         return TestApplication.getTapRadius();
 165     }
 166 
 167     public String toString() {
 168         if (getClass().getPackage().equals(TestTouchDevice.class.getPackage())) {
 169             return getClass().getSimpleName();
 170         } else {
 171             return getClass().getName();
 172         }
 173     }
 174 
 175 }