Package Hexel.rendering.overlays

Source Code of Hexel.rendering.overlays.Menu

package Hexel.rendering.overlays;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.MouseEvent;
import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.Queue;

import com.jogamp.common.net.PiggybackURLConnection;

import Hexel.Hexel;
import Hexel.Config;
import Hexel.Engine;
import Hexel.PCInput;
import Hexel.Session;
import Hexel.Stage;
import Hexel.blocks.types.Block;
import Hexel.blocks.types.BlockDirt;
import Hexel.math.HexGeometry;
import Hexel.math.Vector2d;
import Hexel.rendering.TextureAtlas;

public class Menu {

  private int activeIndex = 0;
  private int lastActiveIndex = -1;

  private MenuItem[] menuItems;

  private int w;
  private int h;

  private BufferedImage tex;
  private Config config = new Config();

  public Menu(MenuItem[] menuItems){
    this.tex = TextureAtlas.getAtlas();
    this.menuItems = menuItems;
  }

  private boolean secondaryEnabled = false;
  private String[] secondaryText;

  private Block db = BlockDirt.Make(BlockDirt.MAX_HEALTH, false);
  private Block gb = BlockDirt.Make(BlockDirt.MAX_HEALTH, true);

  private final double[][] MI_X = new double[][]{
      new double[]{ -1, -1},
      new double[]{ -1, 0},
      new double[]{ 0, 1},
      new double[]{ 1, 1},
      new double[]{ 0, 1},
      new double[]{ 0, -1},
  };
  private final double[][] MI_Y = new double[][]{
      new double[]{ 0, -1},
      new double[]{ -1, -1},
      new double[]{ -1, 0},
      new double[]{ 1, 0},
      new double[]{ 1, 1},
      new double[]{ 1, 0},
  };

  public void enableSecondary(String[] text){
    this.secondaryEnabled = true;
    this.secondaryText = text;
  }

  public void disableSecondary(){
    this.secondaryEnabled = false;
  }

  public void updateSize(int w, int h) {
    this.w = w;
    this.h = h;
  }

  public Queue<Integer> sendInput(PCInput input){
    Queue<Integer> extra = new LinkedList<Integer>();
    while (!input.keysTapped.isEmpty()) {
      int keyTapped = input.keysTapped.poll();
      if (config.controls.get("right").contains(keyTapped)){
        activeIndex++;
      }
      else if (config.controls.get("left").contains(keyTapped)){
        activeIndex--;
      }
      else if (config.controls.get("jump").contains(keyTapped)){
        this.menuItems[activeIndex].select();
      }
      else {
        extra.add(keyTapped);
      }
    }
    for (int i = 0; i < 6; i++){
      GeneralPath p = getMenuItemPolygon(i);
      if (p.contains(input.x, input.y)){
        if (i >= this.menuItems.length)
          activeIndex = 0;
        else
          activeIndex = i;
      }
    }
    while (!input.mouseClicks.isEmpty()){
      int mouseClick = input.mouseClicks.poll();
      if (mouseClick == MouseEvent.BUTTON1){
        GeneralPath p = getMenuItemPolygon(activeIndex);
        if (p.contains(input.x, input.y))
          this.menuItems[activeIndex].select();
      }
    }
    return extra;
  }

  public void draw(Graphics2D page){

    activeIndex = ((activeIndex%menuItems.length)+menuItems.length)%menuItems.length;
    if (lastActiveIndex != activeIndex){
      this.menuItems[activeIndex].enter();
      if (lastActiveIndex >= 0)
        this.menuItems[lastActiveIndex].exit();
    }
    for (int i = 0; i < 6; i++){
      boolean active = i == activeIndex;
      drawMenuItem(page, i, active);
    }
    drawTitle(page);
    if (secondaryEnabled)
      drawSecondary(page);
    lastActiveIndex = activeIndex;
  }

  public void drawTitle(Graphics2D page){
    Font font = new Font("Arial", Font.PLAIN, w/8);
    page.setFont(font);
    page.setColor(Color.WHITE);
    FontMetrics fm = page.getFontMetrics();
    String title = "Hexel";
    page.drawString(title, 25, fm.getHeight());
  }

  public void drawSecondary(Graphics2D page){
    Font font = new Font("Arial", Font.PLAIN, w/32);
    page.setFont(font);
    page.setColor(Color.WHITE);
    FontMetrics fm = page.getFontMetrics();
    for (int i = 0; i < secondaryText.length; i++){
      page.drawString(secondaryText[i], 25, h*1/3+i*fm.getHeight());
    }
  }

  public GeneralPath getMenuItemPolygon(int index){
    GeneralPath polygon = new GeneralPath();
    Vector2d p = new Vector2d();
    int size = (int) Math.min(w/2/2-50, h/2.3333-50);
    double[] xs = MI_X[index];
    double[] ys = MI_Y[index];
    int cx = w*3/4;
    int cy = h/2;
    polygon.moveTo(cx, cy);
    for (int i = 0; i < xs.length; i++){
      HexGeometry.cartesianToHex(xs[i], ys[i], p);
      double x = p.x*size;
      double y = p.y*size/1.3333;
      x += cx;
      y += cy;
      polygon.lineTo(x, y);
    }
    polygon.closePath();
    return polygon;
  }

  public void drawMenuItem(Graphics2D page, int index, boolean active){

    GeneralPath polygon = getMenuItemPolygon(index);
    int bcx = 0;
    int bcy = 0;
    int n = 0;
    float[] p = new float[2];
    for (PathIterator pi = polygon.getPathIterator(null); !pi.isDone(); pi.next()) {
      pi.currentSegment(p);
      if (n < 3){
        bcx += p[0];
        bcy += p[1];
        n += 1;
      }
    }
    bcx /= n;
    bcy /= n;

    Block b;
    if (index < menuItems.length)
      b = gb;
    else
      b = db;

    int i = b.getTopTextureIndex();
    int texW = this.tex.getWidth() / TextureAtlas.HOR;
    int texH = this.tex.getHeight() / TextureAtlas.VER;
    int texX = i % TextureAtlas.HOR * texW;
    int texY = i / TextureAtlas.HOR * texH;
    BufferedImage subtex = this.tex.getSubimage(texX, texY, texW, texH);

    page.setPaint(new TexturePaint(subtex, new Rectangle(0, 0, 1, 1)));//).getBounds2D()));
    page.fill(polygon);
    if (active){
      page.setPaint(Color.WHITE);
      page.setStroke(new BasicStroke(3));
      page.draw(polygon);
    }
    if (index < menuItems.length){
      Font font = new Font("Arial", Font.PLAIN, 24);
      page.setFont(font);
      page.setColor(Color.WHITE);
      FontMetrics fm = page.getFontMetrics();
      page.drawString(menuItems[index].title, bcx - fm.stringWidth(menuItems[index].title)/2, bcy + fm.getHeight()/2);
    }
  }

}
TOP

Related Classes of Hexel.rendering.overlays.Menu

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.