Merge commit 'e6bb1700749980e69b5e913acbfd276f129c24dc'
[nikiroo-utils.git] / src / jexer / event / TMouseEvent.java
index 2e953eb6a2cda0c3f702c999f67abb32d54ae999..e52989814005bec2d2d303372519276ed4e4410f 100644 (file)
-/**
+/*
  * Jexer - Java Text User Interface
  *
- * Version: $Id$
- *
- * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a>
+ * The MIT License (MIT)
  *
- * License: LGPLv3 or later
+ * Copyright (C) 2019 Kevin Lamonte
  *
- * Copyright: This module is licensed under the GNU Lesser General
- * Public License Version 3.  Please see the file "COPYING" in this
- * directory for more information about the GNU Lesser General Public
- * License Version 3.
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
  *
- *     Copyright (C) 2015  Kevin Lamonte
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 3 of
- * the License, or (at your option) any later version.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, see
- * http://www.gnu.org/licenses/, or write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA
+ * @author Kevin Lamonte [kevin.lamonte@gmail.com]
+ * @version 1
  */
 package jexer.event;
 
 /**
- * This class encapsulates several kinds of mouse input events.
+ * This class encapsulates several kinds of mouse input events.  Note that
+ * the relative (x,y) ARE MUTABLE: TWidget's onMouse() handlers perform that
+ * update during event dispatching.
  */
 public class TMouseEvent extends TInputEvent {
 
-    enum Type {
-       /**
-        * Mouse motion.  X and Y will have screen coordinates.
-        */
-       MOUSE_MOTION,
+    // ------------------------------------------------------------------------
+    // Constants --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * The type of event generated.
+     */
+    public enum Type {
+        /**
+         * Mouse motion.  X and Y will have screen coordinates.
+         */
+        MOUSE_MOTION,
+
+        /**
+         * Mouse button down.  X and Y will have screen coordinates.
+         */
+        MOUSE_DOWN,
 
-       /**
-        * Mouse button down.  X and Y will have screen coordinates.
-        */
-       MOUSE_DOWN,
+        /**
+         * Mouse button up.  X and Y will have screen coordinates.
+         */
+        MOUSE_UP,
 
-       /**
-        * Mouse button up.  X and Y will have screen coordinates.
-        */
-       MOUSE_UP
+        /**
+         * Mouse double-click.  X and Y will have screen coordinates.
+         */
+        MOUSE_DOUBLE_CLICK
     }
 
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Type of event, one of MOUSE_MOTION, MOUSE_UP, or MOUSE_DOWN.
+     */
+    private Type type;
+
+    /**
+     * Mouse X - relative coordinates.
+     */
+    private int x;
+
     /**
-     * Type of event, one of MOUSE_MOTION, MOUSE_UP, or MOUSE_DOWN, or
-     * KEYPRESS
+     * Mouse Y - relative coordinates.
      */
-    public Type type;
+    private int y;
 
     /**
-     * Mouse X - relative coordinates
+     * Mouse X - absolute screen coordinates.
      */
-    public int x;
+    private int absoluteX;
 
     /**
-     * Mouse Y - relative coordinates
+     * Mouse Y - absolute screen coordinate.
      */
-    public int y;
+    private int absoluteY;
 
     /**
-     * Mouse X - absolute screen coordinates
+     * Mouse button 1 (left button).
      */
-    public int absoluteX;
+    private boolean mouse1;
 
     /**
-     * Mouse Y - absolute screen coordinate
+     * Mouse button 2 (right button).
      */
-    public int absoluteY;
+    private boolean mouse2;
 
     /**
-     * Mouse button 1 (left button)
+     * Mouse button 3 (middle button).
      */
-    public boolean mouse1;
+    private boolean mouse3;
 
     /**
-     * Mouse button 2 (right button)
+     * Mouse wheel UP (button 4).
      */
-    public boolean mouse2;
+    private boolean mouseWheelUp;
 
     /**
-     * Mouse button 3 (middle button)
+     * Mouse wheel DOWN (button 5).
      */
-    public boolean mouse3;
+    private boolean mouseWheelDown;
 
     /**
-     * Mouse wheel UP (button 4)
+     * Keyboard modifier ALT.
      */
-    public boolean mouseWheelUp;
+    private boolean alt;
 
     /**
-     * Mouse wheel DOWN (button 5)
+     * Keyboard modifier CTRL.
      */
-    public boolean mouseWheelDown;
+    private boolean ctrl;
 
     /**
-     * Public contructor
+     * Keyboard modifier SHIFT.
+     */
+    private boolean shift;
+
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Public contructor.
      *
      * @param type the type of event, MOUSE_MOTION, MOUSE_DOWN, or MOUSE_UP
+     * @param x relative column
+     * @param y relative row
+     * @param absoluteX absolute column
+     * @param absoluteY absolute row
+     * @param mouse1 if true, left button is down
+     * @param mouse2 if true, right button is down
+     * @param mouse3 if true, middle button is down
+     * @param mouseWheelUp if true, mouse wheel (button 4) is down
+     * @param mouseWheelDown if true, mouse wheel (button 5) is down
+     * @param alt if true, ALT was pressed with this mouse event
+     * @param ctrl if true, CTRL was pressed with this mouse event
+     * @param shift if true, SHIFT was pressed with this mouse event
+     */
+    public TMouseEvent(final Type type, final int x, final int y,
+        final int absoluteX, final int absoluteY,
+        final boolean mouse1, final boolean mouse2, final boolean mouse3,
+        final boolean mouseWheelUp, final boolean mouseWheelDown,
+        final boolean alt, final boolean ctrl, final boolean shift) {
+
+        this.type               = type;
+        this.x                  = x;
+        this.y                  = y;
+        this.absoluteX          = absoluteX;
+        this.absoluteY          = absoluteY;
+        this.mouse1             = mouse1;
+        this.mouse2             = mouse2;
+        this.mouse3             = mouse3;
+        this.mouseWheelUp       = mouseWheelUp;
+        this.mouseWheelDown     = mouseWheelDown;
+        this.alt                = alt;
+        this.ctrl               = ctrl;
+        this.shift              = shift;
+    }
+
+    // ------------------------------------------------------------------------
+    // TMouseEvent ------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Get type.
+     *
+     * @return type
+     */
+    public Type getType() {
+        return type;
+    }
+
+    /**
+     * Get x.
+     *
+     * @return x
+     */
+    public int getX() {
+        return x;
+    }
+
+    /**
+     * Set x.
+     *
+     * @param x new relative X value
+     * @see jexer.TWidget#onMouseDown(TMouseEvent mouse)
+     * @see jexer.TWidget#onMouseDown(TMouseEvent mouse)
+     * @see jexer.TWidget#onMouseMotion(TMouseEvent mouse)
+     */
+    public void setX(final int x) {
+        this.x = x;
+    }
+
+    /**
+     * Get y.
+     *
+     * @return y
+     */
+    public int getY() {
+        return y;
+    }
+
+    /**
+     * Set y.
+     *
+     * @param y new relative Y value
+     * @see jexer.TWidget#onMouseDown(TMouseEvent mouse)
+     * @see jexer.TWidget#onMouseDown(TMouseEvent mouse)
+     * @see jexer.TWidget#onMouseMotion(TMouseEvent mouse)
+     */
+    public void setY(final int y) {
+        this.y = y;
+    }
+
+    /**
+     * Get absoluteX.
+     *
+     * @return absoluteX
+     */
+    public int getAbsoluteX() {
+        return absoluteX;
+    }
+
+    /**
+     * Set absoluteX.
+     *
+     * @param absoluteX the new value
+     */
+    public void setAbsoluteX(final int absoluteX) {
+        this.absoluteX = absoluteX;
+    }
+
+    /**
+     * Get absoluteY.
+     *
+     * @return absoluteY
+     */
+    public int getAbsoluteY() {
+        return absoluteY;
+    }
+
+    /**
+     * Set absoluteY.
+     *
+     * @param absoluteY the new value
+     */
+    public void setAbsoluteY(final int absoluteY) {
+        this.absoluteY = absoluteY;
+    }
+
+    /**
+     * Get mouse1.
+     *
+     * @return mouse1
+     */
+    public boolean isMouse1() {
+        return mouse1;
+    }
+
+    /**
+     * Get mouse2.
+     *
+     * @return mouse2
+     */
+    public boolean isMouse2() {
+        return mouse2;
+    }
+
+    /**
+     * Get mouse3.
+     *
+     * @return mouse3
+     */
+    public boolean isMouse3() {
+        return mouse3;
+    }
+
+    /**
+     * Get mouseWheelUp.
+     *
+     * @return mouseWheelUp
+     */
+    public boolean isMouseWheelUp() {
+        return mouseWheelUp;
+    }
+
+    /**
+     * Get mouseWheelDown.
+     *
+     * @return mouseWheelDown
+     */
+    public boolean isMouseWheelDown() {
+        return mouseWheelDown;
+    }
+
+    /**
+     * Getter for ALT.
+     *
+     * @return alt value
+     */
+    public boolean isAlt() {
+        return alt;
+    }
+
+    /**
+     * Getter for CTRL.
+     *
+     * @return ctrl value
+     */
+    public boolean isCtrl() {
+        return ctrl;
+    }
+
+    /**
+     * Getter for SHIFT.
+     *
+     * @return shift value
      */
-    public TMouseEvent(Type type) {
-       this.type = type;
+    public boolean isShift() {
+        return shift;
     }
 
     /**
-     * Make human-readable description of this event
+     * Create a duplicate instance.
+     *
+     * @return duplicate intance
+     */
+    public TMouseEvent dup() {
+        TMouseEvent mouse = new TMouseEvent(type, x, y, absoluteX, absoluteY,
+            mouse1, mouse2, mouse3, mouseWheelUp, mouseWheelDown,
+            alt, ctrl, shift);
+
+        return mouse;
+    }
+
+    /**
+     * Make human-readable description of this TMouseEvent.
+     *
+     * @return displayable String
      */
     @Override
     public String toString() {
-       return String.format("Mouse: %s x %d y %d absoluteX %d absoluteY %d 1 %s 2 %s 3 %s DOWN %s UP %s",
-           type,
-           x, y,
-           absoluteX, absoluteY,
-           mouse1,
-           mouse2,
-           mouse3,
-           mouseWheelUp,
-           mouseWheelDown);
+        return String.format("Mouse: %s x %d y %d absoluteX %d absoluteY %d 1 %s 2 %s 3 %s DOWN %s UP %s ALT %s CTRL %s SHIFT %s",
+            type,
+            x, y,
+            absoluteX, absoluteY,
+            mouse1,
+            mouse2,
+            mouse3,
+            mouseWheelUp,
+            mouseWheelDown,
+            alt, ctrl, shift);
     }
 
 }