James
James

Reputation: 2066

JavaFX controls not receiving mouse events when also using Shape3D

I'm working on a ScalaFX application that has both controls and models in the same window (it is a game). Unfortunately, when I add the models, the controls stop receiving mouse events even though the two do not overlap. A smallest working example looks like this:

import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene._
import scalafx.scene.control._
import scalafx.scene.input.MouseEvent
import scalafx.scene.layout._
import scalafx.scene.shape._

object GameWindow extends JFXApp {
  stage = new PrimaryStage {
    scene = new Scene(800, 600, true, SceneAntialiasing.Balanced) {
      root = new VBox(
        new Button("Click me!") {
          handleEvent(MouseEvent.MouseClicked) {
            me: MouseEvent => Console println "clicked!"
          }
        },
        new Sphere() {
          radius = 100
        })
    }
  }
}

If I replace the sphere with a 2D shape, like Circle or Rectangle, the controls become responsive again; it is only a problem when I add 3D shapes. I have tried setting mouseTransparent and clearing pickOnBounds on the Sphere, but neither seem to work.

How can I let the controls continue to receive mouse events when they are clicked? It is acceptable for the models to not receive them.

Upvotes: 1

Views: 216

Answers (1)

Jarek
Jarek

Reputation: 1533

When mixing 2D (controls) and 3D content you should wrap the 3D content in a SubScene, for instance like this:

object GameWindow3D extends JFXApp { stage = new PrimaryStage { scene = new Scene(800, 600, true, SceneAntialiasing.Balanced) { root = new VBox( new Button("Click me!") { handleEvent(MouseEvent.MouseClicked) { me: MouseEvent => Console println "clicked!" } }, new SubScene(400, 400, true, SceneAntialiasing.Balanced) { root = new VBox { children = new Sphere() { radius = 100 } } } ) } } }

Upvotes: 1

Related Questions