Advertisement
Guest User

Untitled

a guest
May 28th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. package com.harvesttownship.game.ui;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  6. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  7.  
  8. public class CircularMenu {
  9.    
  10.     public static final int
  11.         LEFT = 0,
  12.         RIGHT = 1;
  13.    
  14.     public interface CircularMenuItemListener {
  15.         void onSelect();
  16.     }
  17.    
  18.     class CircularMenuItem {
  19.        
  20.         private float x, y;
  21.         private TextureRegion tex;
  22.         private CircularMenuItemListener listener;
  23.        
  24.         public CircularMenuItem(float x, float y, TextureRegion tex, CircularMenuItemListener listener) {
  25.             this.x = x;
  26.             this.y = y;
  27.             this.tex = tex;
  28.             this.listener = listener;
  29.         }
  30.        
  31.         public void select() {
  32.             listener.onSelect();
  33.         }
  34.        
  35.         public void render(SpriteBatch batch, float centerX, float centerY) {
  36.             batch.draw(tex, x + centerX, y + centerY);
  37.         }
  38.        
  39.         public float getX() {
  40.             return x;
  41.         }
  42.        
  43.         public void setX(float x) {
  44.             this.x = x;
  45.         }
  46.        
  47.         public float getY() {
  48.             return y;
  49.         }
  50.        
  51.         public void setY(float y) {
  52.             this.y = y;
  53.         }
  54.        
  55.         public void setPosition(float x, float y) {
  56.             setX(x);
  57.             setY(y);
  58.         }
  59.        
  60.         public TextureRegion getTexture() {
  61.             return tex;
  62.         }
  63.     }
  64.    
  65.     private ArrayList<CircularMenuItem> items;
  66.     private int x, y;
  67.     private int topItem = 0;
  68.     private int direction;
  69.     private int radius;
  70.     private boolean moving;
  71.    
  72.     public CircularMenu(int x, int y, int radius) {
  73.         items = new ArrayList<CircularMenuItem>();
  74.         this.x = x;
  75.         this.y = y;
  76.         this.radius = radius;
  77.     }
  78.    
  79.     private double getCirclularItemAngle(int item) {
  80.         // 2pi = 360 degrees
  81.         // 360 degrees / number of items = degree of each item
  82.         double angle = ((Math.PI * 2) / items.size());
  83.        
  84.         // 90 degrees - (degree of item * item index) = 90 degrees - item render degree
  85.         angle = (Math.PI / 2) - (angle * item);
  86.        
  87.         // item render degree
  88.         return (angle < 0) ? (Math.PI * 2) + angle : angle;
  89.     }
  90.    
  91.     private double getCartesianX(double angle) {
  92.         // polar: (r, angle) cartesian: (x, y)
  93.         // cos(angle) => x
  94.         return radius * Math.cos(angle);
  95.     }
  96.    
  97.     private double getCartesianY(double angle) {
  98.         // polar: (r, angle) cartesian: (x, y)
  99.         // sin(angle) => y
  100.         return radius * Math.sin(angle);
  101.     }
  102.    
  103.     private double getCircularItemX(int item) {
  104.         return getCartesianX(getCirclularItemAngle(item));
  105.     }
  106.    
  107.     private double getCircularItemY(int item) {
  108.         return getCartesianY(getCirclularItemAngle(item));
  109.     }
  110.    
  111.     public void move(int direction) {
  112.         // Shift items left
  113.         if (direction == LEFT) {
  114.             this.direction = LEFT;
  115.            
  116.             if (topItem + 1 >= items.size())
  117.                 topItem = 0;
  118.             else
  119.                 topItem++;
  120.         // Shift items right
  121.         } else if (direction == RIGHT) {
  122.             this.direction = RIGHT;
  123.            
  124.             if (topItem - 1 < 0)
  125.                 topItem = items.size() - 1;
  126.             else
  127.                 topItem--;
  128.         }
  129.     }
  130.    
  131.     public void update(float delta) {
  132.         // Check if defined top item is at the top of the menu, if not move it there
  133.         double diffY = items.get(topItem).y - getCircularItemY(0);
  134.        
  135.         // Round diff down
  136.         diffY = Math.floor(diffY * 100) / 100;
  137.        
  138.         moving = true;
  139.        
  140.         if (diffY >= -1 && diffY <= 1)
  141.             moving = false;
  142.        
  143.         // Move all menu items ±1 angle each frame until top item is at the top of the menu
  144.         if (moving) {
  145.             for (CircularMenuItem item : items) {
  146.                 // Convert current (x, y) to angle
  147.                 double angle = Math.atan2(item.getY(), item.getX());
  148.                
  149.                 // Convert (angle ± 1) to (x, y), left (+1) by default
  150.                 float x = (float) (radius * Math.cos(angle + 0.1d));
  151.                 float y = (float) (radius * Math.sin(angle + 0.1d));
  152.                
  153.                 if (direction == RIGHT) {
  154.                     x = (float) (radius * Math.cos(angle - 0.1d));
  155.                     y = (float) (radius * Math.sin(angle - 0.1d));
  156.                 }
  157.                
  158.                 // Set position
  159.                 item.setPosition(x, y);
  160.             }
  161.         }
  162.     }
  163.    
  164.     public void select() {
  165.         int item = -1;
  166.         int highY = -1;
  167.        
  168.         for (int i = 0; i < items.size(); i++) {
  169.             if (items.get(i).getY() > highY) {
  170.                 highY = (int) items.get(i).getY();
  171.                 item = i;
  172.             }
  173.         }
  174.        
  175.         items.get(item).select();
  176.     }
  177.    
  178.     public void render(SpriteBatch batch, float delta) {
  179.         update(delta);
  180.        
  181.         for (CircularMenuItem item : items)
  182.             item.render(batch, x, y);
  183.     }
  184.    
  185.     public void addItem(TextureRegion tex, CircularMenuItemListener listener) {
  186.         addItem(new CircularMenuItem(0, 0, tex, listener));
  187.     }
  188.    
  189.     public void addItem(CircularMenuItem item) {
  190.         items.add(item);
  191.        
  192.         // Update item positions to be evened out
  193.         for (int i = 0; i < items.size(); i++) {
  194.             float x = (float) getCircularItemX(i);
  195.             float y = (float) getCircularItemY(i);
  196.            
  197.             items.get(i).setPosition(x, y);
  198.         }
  199.     }
  200.    
  201.     public int getRadius() {
  202.         return radius;
  203.     }
  204.    
  205.     public void setRadius(int radius) {
  206.         this.radius = radius;
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement