package dev.llan.view.game.modals;
import dev.llan.view.RenderComponent;
import javafx.scene.Parent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
public abstract class BaseModal<E> implements RenderComponent {
private static final double SIDE_MARGIN = 25.0;
protected final AnchorPane container;
protected final Pane root;
protected Pane pane;
public BaseModal(Pane root) {
this.root = root;
container = new AnchorPane();
pane = new VBox();
pane.setOnMouseClicked(mouseEvent -> mouseEvent.consume());
container.getChildren().add(pane);
}
public void setView(ModalData<E> data) {
setData(data.data());
setPosition(data.x(), data.y());
}
protected void setPosition(double x, double y) {
double maxWidth = root.getWidth();
double maxHeight = root.getHeight();
double paneWidth = pane.prefWidth(-1);
double paneHeight = pane.prefHeight(-1);
if(x + paneWidth + SIDE_MARGIN > maxWidth) {
x -= paneWidth;
}
if(y + paneHeight + SIDE_MARGIN > maxHeight) {
y -= paneHeight;
}
AnchorPane.setTopAnchor(pane, y);
AnchorPane.setLeftAnchor(pane, x);
}
protected abstract void setData(E data);
@Override
public Parent render() {
return container;
}
}