Package ru.vagrant_ai.questionmarkgame.util

Source Code of ru.vagrant_ai.questionmarkgame.util.Util

package ru.vagrant_ai.questionmarkgame.util;

import java.util.Random;

import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import ru.vagrant_ai.questionmarkgame.main.Game;

public class Util {
 
  static Input input;
  static boolean m_lb = false; //left mouse button is pressed or not
  static boolean m_rb = false; //right mouse button is pressed or not
  static byte m_lb_state = 0; //current left mouse button state: 0 - null, 1 - just pressed, 2 - pressed, 3 - released
  static byte m_rb_state = 0; //current right mouse button state:
  final static int y_offs = 14; //offset includes height of the panel with game name string
 
  /**
   * Checks if mouse has just been clicked in specified rectangle
   * @param btn Button number (1 for left button, 2 for right button)
   * @param x X coordinate center
   * @param y Y coordinate center
   * @param width Width of rectangle to check
   * @param height Height of rectangle to check
   * @return true, if mouse has just been clicked in this rectangle
   */
  public static boolean MousePressed(int btn, int x, int y, int width, int height) {
    input = Game.app.getInput();
    return (((btn == 1 && m_lb_state == 1) || (btn == 2 && m_rb_state == 1) || (btn == 3 && (m_lb_state == 1 || m_rb_state == 1))) && input.getMouseX() > x && input.getMouseY() > y+y_offs && input.getMouseX() < x+width && input.getMouseY() < y+height+y_offs);
  }
   
  /**
   * Checks, if mouse has just been clicked
   * @param btn Button number (1 for left button, 2 for right button, 3 for both)
   * @return
   */
    public static boolean MousePressed(int btn) {
      input = Game.app.getInput();
      return ((btn == 1 && m_lb_state == 1) || (btn == 2 && m_rb_state == 1) || (btn == 3 && (m_lb_state == 1 || m_rb_state == 1)));
    }
 
  /**
   * Checks if mouse currently pressed in specified rectangle
   * @param btn Button number (1 for left button, 2 for right button)
   * @param x X coordinate center
   * @param y Y coordinate center
   * @param width Width of rectangle to check
   * @param height Height of rectangle to check
   * @return true, if mouse is pressed in this rectangle
   */
  public static boolean MouseIsPressed(int btn, int x, int y, int width, int height) {
    input = Game.app.getInput();
    return (((btn == 1 && m_lb_state == 2) || (btn == 2 && m_rb_state == 2) || (btn == 3 && (m_lb_state == 2 || m_rb_state == 2))) && input.getMouseX() > x && input.getMouseY() > y+y_offs && input.getMouseX() < x+width && input.getMouseY() < y+height+y_offs);
  }
 
  /**
   * Checks, if mouse if currently pressed
   * @param btn Button number (1 for left button, 2 for right button)
   * @return
   */
    public static boolean MouseIsPressed(int btn)
    {
      input = Game.app.getInput();
      return (btn == 1?input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON):input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON));
    }
 
  /**
   * Checks if mouse has just been released in specified rectangle
   * @param btn Button number (1 for left button, 2 for right button)
   * @param x X coordinate center
   * @param y Y coordinate center
   * @param width Width of rectangle to check
   * @param height Height of rectangle to check
   * @return true, if mouse has just been released in this rectangle
   */
  public static boolean MouseReleased(int btn, int x, int y, int width, int height) {
    input = Game.app.getInput();
    return (((btn == 1 && m_lb_state == 3) || (btn == 2 && m_rb_state == 3) || (btn == 3 && (m_lb_state == 3 || m_rb_state == 3))) && input.getMouseX() > x && input.getMouseY() > y+y_offs && input.getMouseX() < x+width && input.getMouseY() < y+height+y_offs);
  }
 
  /**
   * Checks if mouse coordinates are in specified rectangle
   * @param x X coordinate center
   * @param y Y coordinate center
   * @param width Width of rectangle to check
   * @param height Height of rectangle to check
   * @return true, if mouse is in specified rectangle
   */
  public static boolean MouseOver(int x, int y, int width, int height) {
    input = Game.app.getInput();
    return (input.getMouseX() > x && input.getMouseY() > y+y_offs && input.getMouseX() < x+width && input.getMouseY() < y+height+y_offs);
  }
 

  public static void MouseUpdate()
  {
    input = Game.app.getInput();
    if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) //sets state for left button
    { //check pressed or just pressed
      m_lb_state = (byte) (m_lb?2:1);
      m_lb = true;
    }
    else //check released or not pressed
    {
      m_lb_state = (byte) (m_lb?3:0);
      m_lb = false;
    }
    if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON)) //sets state for right button
    { //check pressed or just pressed
      m_rb_state = (byte) (m_rb?2:1);
      m_rb = true;
    }
    else //check released or not pressed
    {
      m_rb_state = (byte) (m_rb?0:3);
      m_rb = false;
    }
  }
 
  /**
   * Obtain mouse X coordinate
   * @return Mouse X coordinate
   */
  public static int MouseX()
  {
    input = Game.app.getInput();
    return input.getMouseX();
  }
 
  /**
   * Obtain mouse Y coordinate
   * @return Mouse Y coordinate
   */
  public static int MouseY()
  {
    input = Game.app.getInput();
    return input.getMouseY(); //mouse offset included
  }
   
  /**
   * Loads image from project (if running from IDE) or from external /res/* folder
   * @param string Full image name
   * @return Image
   * @throws SlickException
   */
  public static Image loadImage(String string)
  {
    Image img = null;
    String path = new java.io.File("").getAbsolutePath()+"/res/";
    try
    {
      img = new Image(path+string);
    }
    catch (java.lang.RuntimeException | SlickException e)
    {
      try //feature to skip .png ending without errors
      {
        img = new Image(path+string+".png");
      }
      catch(java.lang.RuntimeException | SlickException e1)
      {
        try { return new Image("res/particle/no_image.png"); } catch (SlickException e3) {} //return blank image
      }
    }
    return img;
  }
   
  /**
   * Used for menu buttons. Returns random number [-2.5f..2.5f]
   * @param i
   * @return
   */
    static int rand(int i)
    {
      return (int) (i+(float)new Random().nextInt(500)/100-2.5f);
    }
 
}
TOP

Related Classes of ru.vagrant_ai.questionmarkgame.util.Util

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.