001package conexp.fx.gui.util;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2023 Francesco Kriegel
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025
026import javafx.geometry.Pos;
027import javafx.scene.Node;
028
029public final class CoordinateUtil {
030
031  public static final double getLocalX(Node node, final double sceneX) {
032    final double minX = node.localToScene(node.getBoundsInLocal()).getMinX();
033    final double localX = sceneX - minX;
034    return localX;
035  }
036
037  public static final double getLocalY(Node node, final double sceneY) {
038    final double minY = node.localToScene(node.getBoundsInLocal()).getMinY();
039    final double localY = sceneY - minY;
040    return localY;
041  }
042
043  public static final double getScreenX(Node node) {
044    final double windowX = node.getScene().getWindow().getX();
045    final double sceneX = node.getScene().getX();
046    final double nodeX = node.localToScene(node.getBoundsInLocal()).getMinX();
047    return windowX + sceneX + nodeX;
048  }
049
050  public static final double getScreenY(Node node) {
051    final double windowY = node.getScene().getWindow().getY();
052    final double sceneY = node.getScene().getY();
053    final double nodeY = node.localToScene(node.getBoundsInLocal()).getMinY();
054    return windowY + sceneY + nodeY;
055  }
056
057  public static final Pos contraryPosition(Pos position) {
058    switch (position) {
059    case BOTTOM_LEFT:
060      return Pos.TOP_RIGHT;
061    case BOTTOM_CENTER:
062      return Pos.TOP_CENTER;
063    case BOTTOM_RIGHT:
064      return Pos.TOP_LEFT;
065    case CENTER_LEFT:
066      return Pos.CENTER_RIGHT;
067    case CENTER:
068      return Pos.CENTER;
069    case CENTER_RIGHT:
070      return Pos.CENTER_LEFT;
071    case TOP_LEFT:
072      return Pos.BOTTOM_RIGHT;
073    case TOP_CENTER:
074      return Pos.BOTTOM_CENTER;
075    case TOP_RIGHT:
076      return Pos.BOTTOM_LEFT;
077    default:
078      return position;
079    }
080  }
081
082}