Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: DesertCoockie (posted 2018-05-13 18:23:11, viewed 1956 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242  
/*
 * Copyright (c) 2018 Ruben Hahn (DesertCookie),
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
 *    and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package glfw4j.input;


import glfw4j.Window;
import glfw4j.input.listeners.*;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWImage;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;


public final class Mouse {
   
   
   // @formatter:off
   public static final int BUTTON_LEFT = GLFW.GLFW_MOUSE_BUTTON_LEFT,
                     BUTTON_MIDDLE = GLFW.GLFW_MOUSE_BUTTON_MIDDLE,
                     BUTTON_RIGHT = GLFW.GLFW_MOUSE_BUTTON_RIGHT;
   public static final int BUTTON_1 = GLFW.GLFW_MOUSE_BUTTON_1,
                     BUTTON_2 = GLFW.GLFW_MOUSE_BUTTON_2,
                     BUTTON_3 = GLFW.GLFW_MOUSE_BUTTON_3,
                     BUTTON_4 = GLFW.GLFW_MOUSE_BUTTON_4,
                     BUTTON_5 = GLFW.GLFW_MOUSE_BUTTON_5,
                     BUTTON_6 = GLFW.GLFW_MOUSE_BUTTON_6,
                     BUTTON_7 = GLFW.GLFW_MOUSE_BUTTON_7,
                     BUTTON_8 = GLFW.GLFW_MOUSE_BUTTON_8;
   
   public static final int CURSOR_MODE_NORMAL = GLFW.GLFW_CURSOR_NORMAL,
                     CURSOR_MODE_DISABLED = GLFW.GLFW_CURSOR_DISABLED,
                     CURSOR_MODE_HIDDEN = GLFW.GLFW_CURSOR_HIDDEN;
   // @formatter:on
   
   private static long glfwWindowID;
   
   private static boolean[] buttonSates = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST];
   private static double x, y;
   private static double scrollXOffset, scrollYOffset;
   private static boolean inWindowArea;
   
   private static HashMap<Long,ArrayList<MouseButtonListener>> mouseButtonListeners = new HashMap<>();
   private static HashMap<Long,ArrayList<MouseMotionListener>> mouseMotionListeners = new HashMap<>();
   private static HashMap<Long,ArrayList<MouseScrollListener>> mouseScrollListeners = new HashMap<>();
   private static HashMap<Long,ArrayList<MouseEnterListener>> mouseEnterListeners = new HashMap<>();
   
   
   private Mouse() {}
   
   
   public static boolean isButtonDown( int button ) {
      return buttonSates[button];
   }
   
   public static double getX() {
      return x;
   }
   
   public static double getY() {
      return y;
   }
   
   public static void setPos( double x,double y ) {
      GLFW.glfwSetCursorPos( glfwWindowID,x,y );
   }
   
   public static double getScrollXOffset() {
      double temp = scrollXOffset;
      scrollXOffset = 0;
      return temp;
   }
   
   public static double getScrollYOffset() {
      double temp = scrollYOffset;
      scrollYOffset = 0;
      return temp;
   }
   
   public static boolean isInWindowArea() {
      return inWindowArea;
   }
   
   public static void addListener( MouseListener mouseListener ) {
      if( glfwWindowID == 0 ) {
         System.err.println( "Listener could not be added: 'Mouse' is not registered" );
         return;
      }
      
      Class<?>[] interfaces = mouseListener.getClass().getInterfaces();
      for( Class<?> i : interfaces ) {
         String iName = i.getName();
         if( iName.equals( MouseButtonListener.class.getName() ) ) {
            mouseButtonListeners.get( glfwWindowID ).add( (MouseButtonListener)mouseListener );
            return;
         } else if( iName.equals( MouseMotionListener.class.getName() ) ) {
            mouseMotionListeners.get( glfwWindowID ).add( (MouseMotionListener)mouseListener );
            return;
         } else if( iName.equals( MouseScrollListener.class.getName() ) ) {
            mouseScrollListeners.get( glfwWindowID ).add( (MouseScrollListener)mouseListener );
            return;
         } else if( iName.equals( MouseEnterListener.class.getName() ) ) {
            mouseEnterListeners.get( glfwWindowID ).add( (MouseEnterListener)mouseListener );
            return;
         }
      }
   }
   
   public static void removeListener( MouseListener mouseListener ) {
      Class<?>[] interfaces = mouseListener.getClass().getInterfaces();
      for( Class<?> i : interfaces ) {
         String iName = i.getName();
         if( iName.equals( MouseButtonListener.class.getName() ) ) {
            mouseButtonListeners.remove( (MouseButtonListener)mouseListener );
            return;
         } else if( iName.equals( MouseMotionListener.class.getName() ) ) {
            mouseMotionListeners.remove( (MouseMotionListener)mouseListener );
            return;
         } else if( iName.equals( MouseScrollListener.class.getName() ) ) {
            mouseScrollListeners.remove( (MouseScrollListener)mouseListener );
            return;
         } else if( iName.equals( MouseEnterListener.class.getName() ) ) {
            mouseEnterListeners.remove( (MouseEnterListener)mouseListener );
            return;
         }
      }
   }
   
   public static void setInputMode( int inputMode ) {
      GLFW.glfwSetInputMode( glfwWindowID,GLFW.GLFW_CURSOR,inputMode );
   }
   
   public static void setCursorObject( BufferedImage image ) throws IOException {
      int width = image.getWidth();
      int height = image.getHeight();
      
      int[] pixels = new int[width*height];
      image.getRGB( 0,0,width,height,pixels,0,width );
      
      ByteBuffer buffer = BufferUtils.createByteBuffer( width*height*4 );
      for( int y = 0; y < height; y++ ) {
         for( int x = 0; x < width; x++ ) {
            int pixel = pixels[y*width+x];
            
            buffer.put( (byte)((pixel >> 16)&0xFF) );  // red
            buffer.put( (byte)((pixel >> 8)&0xFF) );   // green
            buffer.put( (byte)(pixel&0xFF) );          // blue
            buffer.put( (byte)((pixel >> 24)&0xFF) );  // alpha
         }
      }
      buffer.flip();
      
      GLFWImage cursorImg = GLFWImage.create();
      cursorImg.width( width );
      cursorImg.height( height );
      cursorImg.pixels( buffer );
      
      long cursorID = GLFW.glfwCreateCursor( cursorImg,0,0 );
      GLFW.glfwSetCursor( glfwWindowID,cursorID );
   }
   
   public static void register( Window glfwWindow ) {
      glfwWindowID = glfwWindow.getGlfwWindowID();
      if( !mouseButtonListeners.containsKey( glfwWindowID ) ) {
         ArrayList<MouseButtonListener> arrayList = new ArrayList<>();
         mouseButtonListeners.put( glfwWindowID,arrayList );
      }
      if( !mouseMotionListeners.containsKey( glfwWindowID ) ) {
         ArrayList<MouseMotionListener> arrayList = new ArrayList<>();
         mouseMotionListeners.put( glfwWindowID,arrayList );
      }
      if( !mouseScrollListeners.containsKey( glfwWindowID ) ) {
         ArrayList<MouseScrollListener> arrayList = new ArrayList<>();
         mouseScrollListeners.put( glfwWindowID,arrayList );
      }
      if( !mouseEnterListeners.containsKey( glfwWindowID ) ) {
         ArrayList<MouseEnterListener> arrayList = new ArrayList<>();
         mouseEnterListeners.put( glfwWindowID,arrayList );
      }
      
      GLFW.glfwSetMouseButtonCallback( glfwWindowID,( window,button,action,mods ) -> {
         if( window != glfwWindowID )
            return;
         if( button == GLFW.GLFW_KEY_UNKNOWN )
            return;
         for( MouseButtonListener mouseButtonListener : mouseButtonListeners.get( glfwWindow ) )
            mouseButtonListener.invoke( button );
         buttonSates[button] = action != GLFW.GLFW_RELEASE;
      } );
      GLFW.glfwSetCursorPosCallback( glfwWindowID,( window,xpos,ypos ) -> {
         if( window != glfwWindowID )
            return;
         for( MouseMotionListener mouseMotionListener : mouseMotionListeners.get( glfwWindow ) )
            mouseMotionListener.invoke( xpos,ypos );
         x = xpos;
         y = ypos;
      } );
      GLFW.glfwSetScrollCallback( glfwWindowID,( window,xoffset,yoffset ) -> {
         if( window != glfwWindowID )
            return;
         for( MouseScrollListener mouseScrollListener : mouseScrollListeners.get( glfwWindow ) )
            mouseScrollListener.invoke( xoffset,yoffset );
         scrollXOffset += xoffset;
         scrollYOffset += yoffset;
      } );
      GLFW.glfwSetCursorEnterCallback( glfwWindowID,( window,entered ) -> {
         if( window != glfwWindowID )
            return;
         for( MouseEnterListener mouseEnterListener : mouseEnterListeners.get( glfwWindow ) )
            mouseEnterListener.invoke( entered );
         inWindowArea = entered;
      } );
   }
   
   
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
hadezbladez (276 views)
2018-11-16 13:46:03

hadezbladez (153 views)
2018-11-16 13:41:33

hadezbladez (286 views)
2018-11-16 13:35:35

hadezbladez (68 views)
2018-11-16 13:32:03

EgonOlsen (2123 views)
2018-06-10 19:43:48

EgonOlsen (2147 views)
2018-06-10 19:43:44

EgonOlsen (1360 views)
2018-06-10 19:43:20

DesertCoockie (1955 views)
2018-05-13 18:23:11

nelsongames (1598 views)
2018-04-24 18:15:36

nelsongames (2245 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!