import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * The simplest StackPane: three labels piled on top of each other.
 * The first child is at the bottom, the last on top.
 *
 *   setPadding    space INSIDE the StackPane, between its edge and the layers
 *   setMargin     space around ONE layer (set through StackPane)
 *   setAlignment  where a layer sits when it does not fill the stack (for all layers, or one)
 *   setMouseTransparent  a layer that is drawn but lets clicks fall through to the layer below
 *
 * Click on the layers and watch the console: each layer prints a message when it is clicked.
 * The topmost layer under the pointer gets the click.
 *
 * Edit a value, run again, resize the window.
 */

public class SimpleStackPaneDemo extends Application {

	// A colored label with the size you give it.
	private Label label ( String text, String color, double width,
	                      double height ) {
		Label label = new Label(text);
		label.setPrefSize(width,height);
		label.setAlignment(Pos.CENTER); // the text inside the label
		label.setStyle("-fx-background-color: " + color
		    + "; -fx-border-color: #333;");
		return label;
	}

	@Override
	public void start ( Stage stage ) {
		Label back = label("back (first child)","#c8f0c8",100,100);
		Label middle = label("middle","#cfe8ff",220,110);
		Label front = label("front","#ffe0b3",90,40);

		StackPane stack = new StackPane(back,middle,front); // later children are
		                                                    // drawn on top
		stack.setStyle("-fx-background-color: #eeeeee;");

		// The back layer should cover everything, so let it stretch.
		back.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);

		// ---- setPadding: space inside the StackPane's edge (the back layer stops
		// short of it) ----
		stack.setPadding(new Insets(15));
		// try: stack.setPadding(new Insets(40));

		// ---- setAlignment on the stack: where layers that do not fill it sit ----
		stack.setAlignment(Pos.CENTER); // the default
		// try: stack.setAlignment(Pos.TOP_LEFT);

		// ---- setAlignment on one layer: overrides the stack's setting for that
		// layer ----
		StackPane.setAlignment(front,Pos.BOTTOM_RIGHT);
		// try: StackPane.setAlignment(front, Pos.TOP_LEFT);

		// ---- setMargin: space around ONE layer (note: it is a StackPane method)
		// ----
		StackPane.setMargin(front,new Insets(10)); // top, right, bottom, left all
		                                           // 10
		// try: StackPane.setMargin(middle, new Insets(0, 0, 40, 0));

		// ---- clicks: each layer reports when it is clicked ----
		back.setOnMouseClicked(e -> System.out.println("clicked: back"));
		middle.setOnMouseClicked(e -> System.out.println("clicked: middle"));
		front.setOnMouseClicked(e -> System.out.println("clicked: front"));
		// Click on "front": only the front layer prints, because it is on top and
		// gets the click.

		// ---- setMouseTransparent: draw a layer but let clicks pass through it
		// ----
		front.setMouseTransparent(false); // the default: front takes clicks aimed
		                                  // at it
		// try: front.setMouseTransparent(true); // now a click on "front" prints
		// "clicked: back":
		// // front is still drawn, but the click falls through
		// // to the layer under that spot (here, back)
		// Use this for a highlight or message laid over the board that must not
		// steal clicks.

		// ---- hide a layer ----
		// try: front.setVisible(false); // not drawn, and clicks pass through it as
		// well

		stage.setScene(new Scene(stack,400,260));
		stage.setTitle("StackPane");
		stage.show();
	}

	public static void main ( String[] args ) {
		launch(args);
	}
}