Skip to content

Commit

Permalink
Added overlay event handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
HackusatePvP committed May 16, 2024
1 parent 63e321d commit df7c1dd
Show file tree
Hide file tree
Showing 16 changed files with 442 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import javafx.application.Platform;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import me.piitex.renjava.RenJava;

import me.piitex.renjava.events.types.*;
import me.piitex.renjava.gui.exceptions.ImageNotFoundException;
import me.piitex.renjava.gui.overlay.ButtonOverlay;
import me.piitex.renjava.gui.overlay.Overlay;
import me.piitex.renjava.loggers.RenLogger;
import me.piitex.renjava.api.saves.Save;
import me.piitex.renjava.events.EventListener;
import me.piitex.renjava.events.Listener;
import me.piitex.renjava.events.types.ButtonClickEvent;
import me.piitex.renjava.events.types.GameStartEvent;
import me.piitex.renjava.gui.Menu;
import me.piitex.renjava.gui.StageType;

Expand Down Expand Up @@ -110,4 +113,53 @@ public void onButtonClick(ButtonClickEvent event) {
}
}

@Listener
public void onOverlayClick(OverlayClickEvent event) {
Overlay overlay = event.getOverlay();

if (overlay.getOnClick() != null) {
overlay.getOnClick().onClick(event);
}
}

@Listener
public void onOverlayHover(OverlayHoverEvent event) {
Overlay overlay = event.getOverlay();

if (overlay.getOnHover() != null) {
overlay.getOnHover().onHover(event);
}

if (overlay instanceof ButtonOverlay buttonOverlay) {
Button button = buttonOverlay.getButton();

if (buttonOverlay.isHover()) {
if (buttonOverlay.getHoverColor() != null) {
button.setTextFill(buttonOverlay.getHoverColor());
}
if (buttonOverlay.getHoverImage() != null) {
try {
button.setGraphic(new ImageView(buttonOverlay.getHoverImage().build()));
} catch (ImageNotFoundException e) {
RenLogger.LOGGER.error(e.getMessage());
}
}
}
}
}

@Listener
public void onOverlayExit(OverlayExitEvent event) {
Overlay overlay = event.getOverlay();

if (overlay instanceof ButtonOverlay buttonOverlay) {
Button button = buttonOverlay.getButton();
button.setTextFill(buttonOverlay.getTextFill());
button.setStyle(button.getStyle()); // Re-init the style (hopefully this forces the button back to normal.)
if (buttonOverlay.getImage() != null) {
button.setGraphic(new ImageView(buttonOverlay.getImage().getImage()));
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.piitex.renjava.events.types;

import javafx.scene.input.MouseEvent;
import me.piitex.renjava.events.Event;
import me.piitex.renjava.gui.overlay.Overlay;

public class OverlayClickEvent extends Event {
private final Overlay overlay;
private final MouseEvent event;


public OverlayClickEvent(Overlay overlay, MouseEvent event) {
this.overlay = overlay;
this.event = event;
}

public Overlay getOverlay() {
return overlay;
}

public MouseEvent getHandler() {
return event;
}
}
23 changes: 23 additions & 0 deletions src/main/java/me/piitex/renjava/events/types/OverlayExitEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.piitex.renjava.events.types;

import javafx.scene.input.MouseEvent;
import me.piitex.renjava.events.Event;
import me.piitex.renjava.gui.overlay.Overlay;

public class OverlayExitEvent extends Event {
private final Overlay overlay;
private final MouseEvent event;

public OverlayExitEvent(Overlay overlay, MouseEvent event) {
this.overlay = overlay;
this.event = event;
}

public Overlay getOverlay() {
return overlay;
}

public MouseEvent getHandler() {
return event;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.piitex.renjava.events.types;

import javafx.scene.input.MouseEvent;
import me.piitex.renjava.events.Event;
import me.piitex.renjava.gui.overlay.Overlay;

public class OverlayHoverEvent extends Event {
private final Overlay overlay;
private final MouseEvent mouseEvent;

public OverlayHoverEvent(Overlay overlay, MouseEvent event) {
this.overlay = overlay;
this.mouseEvent = event;
}

public Overlay getOverlay() {
return overlay;
}

public MouseEvent getHandler() {
return mouseEvent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ public RenScene getRenScene() {
public Scene getScene() {
return scene;
}
}
}
26 changes: 25 additions & 1 deletion src/main/java/me/piitex/renjava/gui/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import javafx.scene.Node;

import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import me.piitex.renjava.RenJava;
import me.piitex.renjava.api.scenes.transitions.Transitions;
import me.piitex.renjava.events.types.OverlayClickEvent;
import me.piitex.renjava.events.types.OverlayExitEvent;
import me.piitex.renjava.events.types.OverlayHoverEvent;
import me.piitex.renjava.gui.overlay.*;
import me.piitex.renjava.loggers.RenLogger;

Expand All @@ -25,7 +30,6 @@ public Element(Overlay overlay) {
double scaleY = overlay.scaleY();
if (scaleX > 0) {
overlay.setWidth(overlay.width() * scaleX);

overlay.setX(overlay.x() * scaleX);
}
if (scaleY > 0) {
Expand Down Expand Up @@ -65,8 +69,16 @@ public Element(Overlay overlay) {
textFlow.setTranslateX(textFlowOverlay.x());
textFlow.setTranslateY(textFlowOverlay.y());
this.node = textFlow;
} else if (overlay instanceof SliderOverlay sliderOverlay) {
Slider slider = new Slider(sliderOverlay.width(), sliderOverlay.height(), 4);
slider.setPrefSize(sliderOverlay.width(), sliderOverlay.height());
slider.setBlockIncrement(sliderOverlay.getBlockIncrement());
this.node = slider;
}

// Handle specific input
handleInput();

// Render transition for specific overlay.
if (overlay.getTransition() != null) {
setTransition(overlay.getTransition());
Expand All @@ -89,6 +101,18 @@ public void render(Pane root) {
root.getChildren().add(node);
}

private void handleInput() {
node.setOnMouseEntered(event -> {
RenJava.callEvent(new OverlayHoverEvent(overlay, event));
});
node.setOnMouseClicked(event -> {
RenJava.callEvent(new OverlayClickEvent(overlay, event));
});
node.setOnMouseExited(event -> {
RenJava.callEvent(new OverlayExitEvent(overlay, event));
});
}

public Node getNode() {
return node;
}
Expand Down
77 changes: 38 additions & 39 deletions src/main/java/me/piitex/renjava/gui/overlay/ButtonOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import javafx.scene.text.Font;
import me.piitex.renjava.RenJava;
import me.piitex.renjava.gui.Menu;
import me.piitex.renjava.gui.overlay.events.IOverlayClick;
import me.piitex.renjava.gui.overlay.events.IOverlayHover;
import me.piitex.renjava.loggers.RenLogger;
import me.piitex.renjava.api.loaders.ImageLoader;
import me.piitex.renjava.api.scenes.transitions.Transitions;
Expand All @@ -36,6 +38,9 @@ public class ButtonOverlay implements Overlay {
private int borderWidth = 0;
private int backgroundRadius = 0;

private IOverlayHover iOverlayHover;
private IOverlayClick iOverlayClick;

private double x = 1, y = 1;
private double maxHeight, maxWidth;
private final double xScale, yScale;
Expand All @@ -48,15 +53,6 @@ public ButtonOverlay(String id, String text, Color textFill, double xScale, doub
this.yScale = y;
}

public ButtonOverlay(String id, String text, Color textFill, Font font, double xScale, double yScale) {
this.id = id;
this.text = text;
this.textFill = textFill;
this.font = font;
this.xScale = x;
this.yScale = y;
}

/**
* Create a button with only text.
*
Expand Down Expand Up @@ -90,7 +86,7 @@ public ButtonOverlay(String id, String text, Color textFill, double x, double y,
* @param xScale X-Axis scale of the button.
* @param yScale Y-Axis scale of the button.
*/
public ButtonOverlay(String id, String text, Font font, Color textFill, double x, double y, double xScale, double yScale) {
public ButtonOverlay(String id, String text, Color textFill, Font font, double x, double y, double xScale, double yScale) {
this.id = id;
this.text = text;
this.font = font;
Expand All @@ -111,7 +107,7 @@ public ButtonOverlay(String id, String text, Font font, Color textFill, double x
* @param xScale X-Axis scale of the button.
* @param yScale Y-Axis scale of the button.
*/
public ButtonOverlay(String id, String text, Font font, Color textFill, double xScale, double yScale) {
public ButtonOverlay(String id, String text, Color textFill, Font font, double xScale, double yScale) {
this.id = id;
this.text = text;
this.font = font;
Expand All @@ -120,7 +116,7 @@ public ButtonOverlay(String id, String text, Font font, Color textFill, double x
this.yScale = yScale;
}

public ButtonOverlay(String id, String text, Font font, Color textFill, Color backgroundColor, Color borderColor, double xScale, double yScale) {
public ButtonOverlay(String id, String text, Color textFill, Font font, Color backgroundColor, Color borderColor, double xScale, double yScale) {
this.id = id;
this.text = text;
this.font = font;
Expand All @@ -131,7 +127,7 @@ public ButtonOverlay(String id, String text, Font font, Color textFill, Color ba
this.borderColor = borderColor;
}

public ButtonOverlay(String id, String text, Font font, Color textFill, Color backgroundColor, Color borderColor, boolean hover, double xScale, double yScale) {
public ButtonOverlay(String id, String text, Color textFill, Font font, Color backgroundColor, Color borderColor, boolean hover, double xScale, double yScale) {
this.id = id;
this.text = text;
this.font = font;
Expand All @@ -143,7 +139,7 @@ public ButtonOverlay(String id, String text, Font font, Color textFill, Color ba
this.hover = hover;
}

public ButtonOverlay(String id, String text, Font font, Color textFill, Color backgroundColor, Color borderColor, Color hoverColor, double xScale, double yScale) {
public ButtonOverlay(String id, String text, Color textFill, Font font, Color backgroundColor, Color borderColor, Color hoverColor, double xScale, double yScale) {
this.id = id;
this.text = text;
this.font = font;
Expand Down Expand Up @@ -237,6 +233,10 @@ public ButtonOverlay(Button button) {
this.yScale = button.getScaleY();
}

public Button getButton() {
return button;
}

public String getId() {
return id;
}
Expand Down Expand Up @@ -396,6 +396,27 @@ public Transitions getTransition() {
return transitions;
}

@Override
public void setOnclick(IOverlayClick iOverlayClick) {
this.iOverlayClick = iOverlayClick;
}

@Override
public void setOnHover(IOverlayHover iOverlayHover) {
this.iOverlayHover = iOverlayHover;
}

@Override
public IOverlayClick getOnClick() {
return iOverlayClick;
}

@Override
public IOverlayHover getOnHover() {
return iOverlayHover;
}


public void setTransitions(Transitions transitions) {
this.transitions = transitions;
}
Expand Down Expand Up @@ -491,31 +512,6 @@ public Button build() {
inLine += "-fx-border-width: " + borderWidth + "; ";
inLine += "-fx-background-radius: " + backgroundRadius + ";";

// https://stackoverflow.com/questions/30680570/javafx-button-border-and-hover
if (hover) {
AtomicReference<String> atomicInLine = new AtomicReference<>(inLine);
button.setOnMouseEntered(mouseEvent -> {
if (hoverColor != null) {
button.setTextFill(hoverColor);
button.setStyle(atomicInLine.get());
}
if (hoverImage != null) {
try {
button.setGraphic(new ImageView(hoverImage.build()));
} catch (ImageNotFoundException e) {
RenLogger.LOGGER.error(e.getMessage());
}
}
});
button.setOnMouseExited(mouseEvent -> {
button.setTextFill(textFill);
button.setStyle(atomicInLine.get());
if (image != null) {
button.setGraphic(new ImageView(image.getImage()));
}
});
}

button.setStyle(inLine);

if (x != 0 && y != 0) {
Expand All @@ -529,6 +525,9 @@ public Button build() {
ButtonClickEvent event = new ButtonClickEvent(RenJava.getInstance().getPlayer().getCurrentScene(), button);
RenJava.callEvent(event);
});

this.button = button;

return button;
}

Expand Down
Loading

0 comments on commit df7c1dd

Please sign in to comment.