Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.psnb.states;
- import org.newdawn.slick.Color;
- import org.newdawn.slick.GameContainer;
- import org.newdawn.slick.Graphics;
- import org.newdawn.slick.Image;
- import org.newdawn.slick.Input;
- import org.newdawn.slick.KeyListener;
- import org.newdawn.slick.SlickException;
- import org.newdawn.slick.state.BasicGameState;
- import org.newdawn.slick.state.StateBasedGame;
- import org.psnb.Engine;
- import org.psnb.components.Textbox;
- public class MainMenuState extends BasicGameState implements KeyListener {
- /**
- * The game's logo.
- */
- private Image logo;
- /**
- * The username textbox.
- */
- private Textbox usernameTextbox;
- /**
- * The password textbox.
- */
- private Textbox passwordTextbox;
- /**
- * Determines whether or not the username field is focused.
- */
- private boolean usernameFocused;
- @Override
- public int getID() {
- return Engine.MAIN_MENU_STATE;
- }
- @Override
- public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
- this.logo = new Image("./res/images/mainmenu/logo.png");
- this.usernameTextbox = new Textbox(135, 100, 175, 15);
- this.passwordTextbox = new Textbox(135, 130, 175, 20);
- gc.getInput().enableKeyRepeat();
- gc.getInput().addKeyListener(this);
- }
- @Override
- public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
- int centerScreen = gc.getWidth() / 2;
- int centerLogo = logo.getWidth() / 2;
- logo.draw(centerScreen - centerLogo, 20);
- g.setColor(Color.white);
- g.drawString("Username: ", 50, 100);
- g.drawString("Password: ", 50, 130);
- usernameTextbox.draw(g);
- passwordTextbox.draw(g);
- }
- @Override
- public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
- Input input = gc.getInput();
- int x = input.getMouseX();
- int y = input.getMouseY();
- usernameTextbox.checkHover(x, y);
- passwordTextbox.checkHover(x, y);
- if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
- if(usernameTextbox.isWithinComponent(x, y))
- usernameFocused = true;
- if(passwordTextbox.isWithinComponent(x, y))
- usernameFocused = false;
- }
- }
- private long lastCharacterTime;
- public void keyPressed(int key, char c) {
- long currentCharacterTime = System.currentTimeMillis();
- if(currentCharacterTime - lastCharacterTime < 10)
- return;
- if(usernameFocused)
- usernameTextbox.append(c);
- else
- passwordTextbox.append(c);
- lastCharacterTime = currentCharacterTime;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement