Advertisement
Guest User

Untitled

a guest
May 28th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package org.psnb.states;
  2.  
  3. import org.newdawn.slick.Color;
  4. import org.newdawn.slick.GameContainer;
  5. import org.newdawn.slick.Graphics;
  6. import org.newdawn.slick.Image;
  7. import org.newdawn.slick.Input;
  8. import org.newdawn.slick.KeyListener;
  9. import org.newdawn.slick.SlickException;
  10. import org.newdawn.slick.state.BasicGameState;
  11. import org.newdawn.slick.state.StateBasedGame;
  12. import org.psnb.Engine;
  13. import org.psnb.components.Textbox;
  14.  
  15. public class MainMenuState extends BasicGameState implements KeyListener {
  16.    
  17.     /**
  18.      * The game's logo.
  19.      */
  20.     private Image logo;
  21.    
  22.     /**
  23.      * The username textbox.
  24.      */
  25.     private Textbox usernameTextbox;
  26.    
  27.     /**
  28.      * The password textbox.
  29.      */
  30.     private Textbox passwordTextbox;
  31.    
  32.     /**
  33.      * Determines whether or not the username field is focused.
  34.      */
  35.     private boolean usernameFocused;
  36.  
  37.     @Override
  38.     public int getID() {
  39.         return Engine.MAIN_MENU_STATE;
  40.     }
  41.  
  42.     @Override
  43.     public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
  44.         this.logo = new Image("./res/images/mainmenu/logo.png");
  45.         this.usernameTextbox = new Textbox(135, 100, 175, 15);
  46.         this.passwordTextbox = new Textbox(135, 130, 175, 20);
  47.        
  48.         gc.getInput().enableKeyRepeat();
  49.         gc.getInput().addKeyListener(this);
  50.     }
  51.    
  52.     @Override
  53.     public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
  54.         int centerScreen = gc.getWidth() / 2;
  55.         int centerLogo = logo.getWidth() / 2;
  56.         logo.draw(centerScreen - centerLogo, 20);
  57.        
  58.         g.setColor(Color.white);
  59.         g.drawString("Username: ", 50, 100);
  60.         g.drawString("Password: ", 50, 130);
  61.  
  62.         usernameTextbox.draw(g);
  63.         passwordTextbox.draw(g);
  64.     }
  65.    
  66.     @Override
  67.     public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
  68.         Input input = gc.getInput();
  69.        
  70.         int x = input.getMouseX();
  71.         int y = input.getMouseY();
  72.        
  73.         usernameTextbox.checkHover(x, y);
  74.         passwordTextbox.checkHover(x, y);
  75.        
  76.         if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
  77.             if(usernameTextbox.isWithinComponent(x, y))
  78.                 usernameFocused = true;
  79.             if(passwordTextbox.isWithinComponent(x, y))
  80.                 usernameFocused = false;
  81.         }
  82.        
  83.     }
  84.    
  85.     private long lastCharacterTime;
  86.    
  87.     public void keyPressed(int key, char c) {
  88.         long currentCharacterTime = System.currentTimeMillis();
  89.                
  90.         if(currentCharacterTime - lastCharacterTime < 10)
  91.             return;
  92.        
  93.         if(usernameFocused)
  94.             usernameTextbox.append(c);
  95.         else
  96.             passwordTextbox.append(c);
  97.        
  98.         lastCharacterTime = currentCharacterTime;
  99.     }
  100.    
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement