Skip to content

Commit

Permalink
New UiObjects and mouseManager refactored
Browse files Browse the repository at this point in the history
1. Mouse manager extends from MouseAdapter
2. UiSlider added
3. SettingsSate created with a basic UiSilder
4. UiElements added to the states and to res folder
  • Loading branch information
davixcky committed May 16, 2021
1 parent 8b20b01 commit c7ed4c9
Show file tree
Hide file tree
Showing 18 changed files with 379 additions and 67 deletions.
8 changes: 7 additions & 1 deletion src/main/java/com/uninorte/base/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.uninorte.base.game.input.MouseManager;
import com.uninorte.base.game.states.GameOverState;
import com.uninorte.base.game.states.GameState;
import com.uninorte.base.game.states.SettingsState;
import com.uninorte.base.game.states.State;
import com.uninorte.base.game.display.Display;
import com.uninorte.base.game.gfx.Assets;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class Game implements Runnable {

public State gameSate;
public State gameOverState;
public State settingsState;

private Image background;

Expand All @@ -54,12 +56,16 @@ private void init() {
display.getFrame().addMouseMotionListener(mouseManager);
display.getGameCanvas().addMouseListener(mouseManager);
display.getGameCanvas().addMouseMotionListener(mouseManager);
display.getGameCanvas().addMouseWheelListener(mouseManager);
display.getFrame().addMouseWheelListener(mouseManager);

Assets.init();

handler = new Handler(this);
gameSate = new GameState(handler);
gameOverState = new GameOverState(handler);
State.setCurrentState(gameSate);
settingsState = new SettingsState(handler);
State.setCurrentState(settingsState);

background = new ImageIcon(ContentLoader.loadImage(Filenames.BACKGROUND_IMAGES[3])).getImage();
}
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/com/uninorte/base/game/display/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.event.*;

public class Display {

Expand All @@ -17,8 +14,6 @@ public class Display {
private String title;
private Dimension windowSize;



public Display(String title, Dimension windowSize) {
this.title = title;
this.windowSize = windowSize;
Expand Down
63 changes: 58 additions & 5 deletions src/main/java/com/uninorte/base/game/gfx/Assets.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,29 @@ public enum ExplosionColor {
}

public enum Fonts {
SLKSCR_100
SLKSCR_100,
SLKSCR_10,
}

public enum FontsName {
SLKSCR
}

public enum UI_ELEMENTS {
BUTTONS,
SLIDER
}

private static ArrayList<BufferedImage> playerAssets;
private static ArrayList<BufferedImage> aliensAssets;
private static ArrayList<BufferedImage> bulletAssets;
private static HashMap<String, ArrayList<BufferedImage>> explosions;

private static HashMap<String, ArrayList<BufferedImage>> uiComponents;

private static HashMap<String, Font> fonts;

public static BufferedImage[] btn_start;
public static ArrayList<BufferedImage> btn_start;

public static void init() {
playerAssets = loadSprites(60, 60, 3, "/textures/ships.png");
Expand All @@ -59,13 +71,25 @@ public static void init() {
explosions.put(getColorString(ExplosionColor.RED), loadSprites(100, 100, 56, "/textures/explosions/red.png"));
explosions.put(getColorString(ExplosionColor.YELLOW), loadSprites(100, 100, 56, "/textures/explosions/yellow.png"));

uiComponents = new HashMap<>();
uiComponents.put(getUiString(UI_ELEMENTS.BUTTONS), loadSprites(105, 21, 4, "/ui/labels.png"));

SpriteSheet slidersSheet = new SpriteSheet(ContentLoader.loadImage("/ui/sheet-slider.png"));
ArrayList<BufferedImage> sliders = new ArrayList<>();
sliders.add(slidersSheet.crop(0, 0, 245, 18));
sliders.add(slidersSheet.crop(245, 0, 22, 17));
uiComponents.put(getUiString(UI_ELEMENTS.SLIDER), sliders);


fonts = new HashMap<>();
fonts.put(getFontString(Fonts.SLKSCR_100), ContentLoader.loadFont("/fonts/slkscr.ttf", 100));
fonts.put(getFontString(Fonts.SLKSCR_10), ContentLoader.loadFont("/fonts/slkscr.ttf", 10));


SpriteSheet sheet = new SpriteSheet(ContentLoader.loadImage("/textures/sheet.png"));
btn_start = new BufferedImage[2];
btn_start[0] = sheet.crop(32 * 6, 32 * 4, 32 * 2, 32);
btn_start[1] = sheet.crop(32 * 6, 32 * 5, 32 * 2, 32);
btn_start = new ArrayList<>();
btn_start.add(sheet.crop(32 * 6, 32 * 4, 32 * 2, 32));
btn_start.add(sheet.crop(32 * 6, 32 * 5, 32 * 2, 32));
}

public static ArrayList<BufferedImage> getPlayerAssets() {
Expand Down Expand Up @@ -95,6 +119,14 @@ public static Font getFont(Fonts font) {
return fonts.get(getFontString(font));
}

public static Font getFont(FontsName fontName, int size) {
return ContentLoader.loadFont(getFontName(fontName), size);
}

public static ArrayList<BufferedImage> getUiComponents(UI_ELEMENTS uiElement) {
return uiComponents.get(getUiString(uiElement));
}

public static BufferedImage rotate(BufferedImage bimg, double angle) {

int w = bimg.getWidth();
Expand Down Expand Up @@ -151,4 +183,25 @@ private static String getFontString(Fonts font) {
return fontStr;
}

private static String getFontName(FontsName name) {
String fontStr = "";

switch (name) {
case SLKSCR -> fontStr = "/fonts/slkscr.ttf";
}

return fontStr;
}

private static String getUiString(UI_ELEMENTS uiElement) {
String uiElementStr = "";

switch (uiElement) {
case BUTTONS -> uiElementStr = "buttons";
case SLIDER -> uiElementStr = "slider";
}

return uiElementStr;
}

}
25 changes: 19 additions & 6 deletions src/main/java/com/uninorte/base/game/input/MouseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.uninorte.base.game.ui.UIManager;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;

public class MouseManager implements MouseListener, MouseMotionListener {
public class MouseManager extends MouseAdapter {

private boolean leftPressed, rightPressed;
private int mouseX, mouseY;
Expand Down Expand Up @@ -41,6 +41,9 @@ public void mousePressed(MouseEvent e) {
leftPressed = true;
else if(e.getButton() == MouseEvent.BUTTON3)
rightPressed = true;

if(uiManager != null)
uiManager.onMousePressed(e);
}

@Override
Expand All @@ -51,7 +54,7 @@ else if(e.getButton() == MouseEvent.BUTTON3)
rightPressed = false;

if(uiManager != null)
uiManager.onMouseRelease(e);
uiManager.onMouseReleased(e);
}

@Override
Expand All @@ -60,12 +63,13 @@ public void mouseMoved(MouseEvent e) {
mouseY = e.getY();

if(uiManager != null)
uiManager.onMouseMove(e);
uiManager.onMouseMoved(e);
}

@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
if (uiManager != null)
uiManager.onMouseDragged(e);
}

@Override
Expand All @@ -83,4 +87,13 @@ public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getWheelRotation() < 0) {
System.out.println("mouse wheel Up");
} else {
System.out.println("mouse wheel Down");
}
}

}
11 changes: 2 additions & 9 deletions src/main/java/com/uninorte/base/game/states/GameOverState.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@
import com.uninorte.base.game.Handler;
import com.uninorte.base.game.gfx.Assets;
import com.uninorte.base.game.gfx.Text;
import com.uninorte.base.game.ui.UIImageButton;
import com.uninorte.base.game.ui.UIManager;
import com.uninorte.base.game.ui.UIButton;

import java.awt.*;

public class GameOverState extends State {

private UIManager uiManager;

public GameOverState(Handler handler) {
super(handler);

uiManager = new UIManager(handler);
handler.getMouseManager().setUIManager(uiManager);


int x = (int) (handler.boardDimensions().width * 0.5f - 128 / 2);
int y = (int) (handler.boardDimensions().height / 2 + 101);
uiManager.addObject(new UIImageButton(this, x, y, 128, 64, Assets.btn_start, () -> {
uiManager.addObjects(new UIButton(this, x, y, 128, 64, Assets.btn_start, () -> {
((GameState) handler.getGame().gameSate).reset();
State.setCurrentState(handler.getGame().gameSate);
}));
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/uninorte/base/game/states/SettingsState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.uninorte.base.game.states;

import com.uninorte.base.game.Handler;
import com.uninorte.base.game.ui.UISlider;

import java.awt.*;

public class SettingsState extends State {

public SettingsState(Handler handler) {
super(handler);

initComponents();
}

private void initComponents() {
int x = (int) (handler.boardDimensions().width * 0.5f - 128 / 2);
int y = (int) (handler.boardDimensions().height / 2 + 101);

UISlider uiSlider = new UISlider(this, x + 30, y / 2, 30, 40, (System.out::println));
uiSlider.setValue(20);

uiManager.addObjects(uiSlider);
}

@Override
public void update() {
uiManager.update();
}

@Override
public void render(Graphics g) {
uiManager.render(g);
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/uninorte/base/game/states/State.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.uninorte.base.game.states;

import com.uninorte.base.game.Handler;
import com.uninorte.base.game.ui.UIManager;

import java.awt.*;

Expand All @@ -10,12 +11,21 @@ public abstract class State {

protected Handler handler;

protected UIManager uiManager;

public State(Handler handler) {
this.handler = handler;

uiManager = new UIManager(handler);
}

public static void setCurrentState(State state) {
currentState = state;
state.setUiManager();
}

protected void setUiManager() {
handler.getGame().getMouseManager().setUIManager(uiManager);
}

public static State getCurrentState() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/uninorte/base/game/ui/MouseListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.uninorte.base.game.ui;

public interface MouseListener {
void onMouseChanged(float value);
}
Loading

0 comments on commit c7ed4c9

Please sign in to comment.