Guest User

EditorUI

a guest
Feb 16th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.59 KB | None | 0 0
  1. public class EditorUI {
  2.  
  3.     // Text field width.
  4.     public static int FIELD_WIDTH = 128;
  5.    
  6.     // Size of the scroll bar sprites.
  7.     public static int SPRITE_SIZE = 64;
  8.  
  9.     // Vars for table adjustment.
  10.     public static int RIGHT_WIDTH = 100;
  11.     public static int BOTTOM_HEIGHT = 32;
  12.  
  13.     // Bottom and right layout tables.
  14.     private Table bottomTable;
  15.     private Table rightTable;
  16.  
  17.     private Stage stage;   
  18.     private Skin uiSkin;
  19.  
  20.     // TODO fix up the button positions.
  21.     private ImageButton toggleGrid;
  22.     private ImageButton toggleEdit;
  23.     private ImageButton regenCollisions;
  24.     private ImageButton saveMap;
  25.  
  26.     // Is the mouse over UI.
  27.     private boolean over;
  28.  
  29.     // Clicked tiles ID on the scroll pane.
  30.     private int clickedID;
  31.  
  32.     // Current map which is loaded.
  33.     private String currentMap;
  34.    
  35.     private InputBox saveBox;
  36.    
  37.     private String saveMapString;
  38.  
  39.     public EditorUI(String currentMap) {
  40.  
  41.         this.currentMap = currentMap;
  42.  
  43.         stage = new Stage(new ExtendViewport(Game.WIDTH, Game.HEIGHT));
  44.  
  45.         Game.inputs.addProcessor(stage);
  46.  
  47.         // Skin for the buttons. TODO add more skins for other UI elements.
  48.         uiSkin = new Skin(Gdx.files.internal("./res/ui/ui_items.json"), Game.resources.getAltas("ui"));
  49.  
  50.         // Bottom layout table setup.
  51.         bottomTable = new Table();
  52.         bottomTable.setBackground(uiSkin.getDrawable("table"));
  53.         bottomTable.setHeight(BOTTOM_HEIGHT);
  54.         bottomTable.setWidth(Game.WIDTH - RIGHT_WIDTH);
  55.  
  56.         // Right layout table setup.
  57.         rightTable = new Table();
  58.         rightTable.setBackground(uiSkin.getDrawable("table"));
  59.         rightTable.setX(Game.WIDTH - RIGHT_WIDTH);
  60.         rightTable.setHeight(Game.HEIGHT);
  61.         rightTable.setWidth(RIGHT_WIDTH);
  62.  
  63.         // Extra setuping.
  64.         setupButtons();
  65.         setubScrollBar();      
  66.        
  67.         saveBox = new InputBox(uiSkin, Game.WIDTH / 2 - 80, Game.HEIGHT / 2 - 32, 160, 64, false);
  68.         saveBox.setBackground(uiSkin.getDrawable("table"));
  69.         saveBox.setText(currentMap);
  70.  
  71.         // Finalizing setups.
  72.         stage.addActor(saveBox.getTable());
  73.         stage.addActor(rightTable);
  74.         stage.addActor(bottomTable);
  75.     }
  76.  
  77.     public void render(SpriteBatch sb) {
  78.  
  79.         sb.setProjectionMatrix(sb.getProjectionMatrix());
  80.  
  81.         sb.begin();
  82.  
  83.         sb.end();
  84.  
  85.         //stage.setDebugAll(true);
  86.         stage.draw();
  87.     }
  88.  
  89.     public void update(float dt) {
  90.  
  91.         stage.act(dt);
  92.  
  93.         float x = GameMath.screenMouse().x;
  94.         float y = GameMath.screenMouse().y;
  95.        
  96.         // Set the save map string;    
  97.         saveMapString = saveBox.getSaveTextOnce();
  98.  
  99.         // Check if the mouse is over UI this tick.
  100.         over = false;
  101.        
  102.         for (Actor actor : stage.getActors()) {
  103.  
  104.             if (isOver(actor, x, y)) {
  105.  
  106.                 over = true;
  107.                 break;
  108.             }
  109.         }  
  110.        
  111.         if (toggleEdit.isChecked() == true) {
  112.            
  113.             rightTable.setVisible(true);
  114.            
  115.         } else {
  116.            
  117.             rightTable.setVisible(false);
  118.         }
  119.     }
  120.  
  121.     public void dispose() {
  122.  
  123.         stage.dispose();
  124.         uiSkin.dispose();
  125.     }
  126.  
  127.     private void setubScrollBar() {
  128.  
  129.         // Table inside the scroll pane.
  130.         Table scrollTable = new Table();   
  131.  
  132.         // Add the sprites from the level sheet. TODO use level specific sheet.
  133.         for (int i = 0; i < Game.resources.getSpriteSheet("test_sheet").size(); i ++) {
  134.  
  135.             ImageButtonStyle style = new ImageButtonStyle();
  136.  
  137.             style.up = uiSkin.getDrawable("button_basic_up");
  138.             style.down = uiSkin.getDrawable("button_basic_down");
  139.  
  140.             style.imageUp = new SpriteDrawable(Game.resources.getSpriteSheet("test_sheet").getSprite(i));
  141.             style.imageUp.setMinHeight(SPRITE_SIZE);
  142.             style.imageUp.setMinWidth(SPRITE_SIZE);
  143.  
  144.             ImageButton tile = new ImageButton(style);
  145.  
  146.             tile.setUserObject(i);
  147.             tile.addListener(new ClickListener() {
  148.  
  149.                 // Set the ID of the clicked tile.
  150.                 @Override
  151.                 public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
  152.  
  153.                     Game.resources.getSound("click_2").play(); 
  154.                     clickedID = (int)tile.getUserObject();
  155.                     return true;
  156.                 }
  157.             });
  158.  
  159.             scrollTable.add(tile).width(SPRITE_SIZE).height(SPRITE_SIZE).fill().expand();  
  160.             scrollTable.row();
  161.         }
  162.  
  163.         ScrollPane scroll = new ScrollPane(scrollTable, uiSkin);
  164.         scroll.setFadeScrollBars(false);
  165.        
  166.         rightTable.add(scroll).expand().fill();
  167.     }
  168.  
  169.     private void setupButtons() {
  170.  
  171.         // Grid button.
  172.         toggleGrid = new ImageButton(new ImageButtonStyle());      
  173.         toggleGrid.getStyle().imageUp = uiSkin.getDrawable("grid");
  174.         toggleGrid.getStyle().up = uiSkin.getDrawable("button_flat_up");
  175.         toggleGrid.getStyle().down = uiSkin.getDrawable("button_flat_down");
  176.         toggleGrid.getStyle().checked = uiSkin.getDrawable("button_flat_down");
  177.  
  178.         toggleGrid.addListener(new ClickListener(){
  179.  
  180.             @Override
  181.             public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
  182.  
  183.                 Game.resources.getSound("click_3").play();
  184.                 return true;
  185.             }
  186.         });
  187.  
  188.         // Edit button.
  189.         toggleEdit = new ImageButton(new ImageButtonStyle());
  190.         toggleEdit.getStyle().imageUp = uiSkin.getDrawable("edit");
  191.         toggleEdit.getStyle().up = uiSkin.getDrawable("button_flat_up");
  192.         toggleEdit.getStyle().down = uiSkin.getDrawable("button_flat_down");
  193.         toggleEdit.getStyle().checked = uiSkin.getDrawable("button_flat_down");
  194.  
  195.         toggleEdit.addListener(new ClickListener() {
  196.  
  197.             @Override
  198.             public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
  199.  
  200.                 Game.resources.getSound("click_1").play();
  201.                 return true;
  202.             }
  203.         });
  204.  
  205.         // Save map button.
  206.         saveMap = new ImageButton(new ImageButtonStyle());
  207.         saveMap.getStyle().imageUp = uiSkin.getDrawable("save");
  208.         saveMap.getStyle().up = uiSkin.getDrawable("button_flat_up");
  209.         saveMap.getStyle().down = uiSkin.getDrawable("button_flat_down");
  210.  
  211.         saveMap.addListener(new ClickListener() {
  212.            
  213.             public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
  214.                
  215.                 if (button != Buttons.LEFT) {
  216.                    
  217.                     return false;
  218.                 }
  219.                
  220.                 Game.resources.getSound("click_2").play();
  221.                
  222.                 if (saveBox.isVisible()) {
  223.                    
  224.                     saveBox.setVisible(false); 
  225.                     saveBox.setUnclicked();
  226.                     return false;
  227.                 }
  228.                 saveBox.setVisible(true);
  229.                 saveBox.setClicked();
  230.                
  231.                 return true;
  232.             }      
  233.         });
  234.  
  235.         // Regenerate collisions button.
  236.         regenCollisions = new ImageButton(new ImageButtonStyle());
  237.         regenCollisions.getStyle().imageUp = uiSkin.getDrawable("calculate");
  238.         regenCollisions.getStyle().up = uiSkin.getDrawable("button_flat_up");
  239.         regenCollisions.getStyle().down = uiSkin.getDrawable("button_flat_down");
  240.  
  241.         regenCollisions.addListener(new ClickListener() {
  242.  
  243.             @Override
  244.             public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
  245.  
  246.                 Game.resources.getSound("click_2").play();         
  247.                 return true;
  248.             }
  249.         });
  250.  
  251.         // Add the buttons to stage.
  252.         bottomTable.add(toggleGrid).left().height(32).width(32);
  253.         bottomTable.add(toggleEdit).left().height(32).width(32);
  254.         bottomTable.add(saveMap).left().height(32).width(32);
  255.         bottomTable.add(regenCollisions).width(32).height(32).expand().left();
  256.     }
  257.  
  258.     public boolean isOver(Actor actor, float x, float y) {
  259.  
  260.         if (actor.isVisible() == false) {
  261.            
  262.             return false;
  263.         }
  264.        
  265.         return actor.getX() <= x &&
  266.                 actor.getY() <= y &&
  267.                 actor.getX() + actor.getWidth()  >= x &&
  268.                 actor.getY() + actor.getHeight() >= y;
  269.     }
  270.  
  271.     public int getClickedID() {
  272.  
  273.         return clickedID;
  274.     }
  275.  
  276.     public ImageButton getRegenCollisions() {
  277.  
  278.         return regenCollisions;
  279.     }
  280.  
  281.     public ImageButton getToggleGrid() {
  282.  
  283.         return toggleGrid;
  284.     }
  285.  
  286.     public ImageButton getToggleEdit() {
  287.  
  288.         return toggleEdit;
  289.     }
  290.  
  291.     public ImageButton getSaveMap() {
  292.  
  293.         return saveMap;
  294.     }
  295.    
  296.     public String getSaveMapString() {
  297.        
  298.         return saveMapString;
  299.     }
  300.  
  301.     public boolean isOver() {
  302.  
  303.         return over;
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment