Box2dMultiExamplesLoader.qml Example File

examples/physics/box2d-examples/qml/Box2dMultiExamplesLoader.qml
 import QtQuick 2.0
 import Felgo 3.0
 import QtQuick.Controls 1.1
 import QtQuick.Dialogs 1.0

 /*!
   This example uses the modified Box2d plugin and examples which is part of import Felgo 3.0.
   Compared to the normal Box2d examples, it adds entity structure and content scaling with GameWindow & Scene components.
   You can use the original qml-box2d sources by "import Box2D 1.1" - this makes it easy to combine existing QML games with \vp.

   NOTE: On Android, showing multiple GameWindows does NOT work! Also, the FileDialog points to the wrong directory on Android - so for Android only choose a single example to load.
  */
 GameWindowApplicationWindow {
   color: "white"

   Loader {
     id: loader

     source: "boxes/Boxes.qml"

     asynchronous: true

     // always prevent shutdown from the loaded item - a shutdown shall only occur if the outer window has focus!
     onLoaded: item.shutdownAppWithEscapeKeyEnabled = false

     Connections {
       target: loader.item
       onClosing: switchSource("")
     }
   }

   Flow {
     spacing: 10
     width: parent.width

     Button {
       text: "Select Box2D Example"

       onClicked: {
         fileDialog.open()
       }
     }
     Button {
       text: "Unload"

       onClicked: {
         switchSource("")
       }
     }
     Button {
       text: "Boxes"

       onClicked: {
         switchSource("boxes/Boxes.qml")
       }
     }
     Button {
       text: "Demolition"

       onClicked: {
         switchSource("demolition/Demolition.qml")
       }
     }
     Button {
       text: "Distance"
       onClicked: {
         switchSource("distance/Distance.qml")
       }
     }
     Button {
       text: "Filtering"
       onClicked: {
         switchSource("filtering/Filtering.qml")
       }
     }
     Button {
       text: "Impulse"
       onClicked: {
         switchSource("impulse/Impulse.qml")
       }
     }
     Button {
       text: "Monera"
       onClicked: {
         switchSource("monera/Monera.qml")
       }
     }
     Button {
       text: "MovingBox"
       onClicked: {
         switchSource("movingBox/MovingBox.qml")
       }
     }
     Button {
       text: "Polygons"
       onClicked: {
         switchSource("polygons/Polygons.qml")
       }
     }
     Button {
       text: "Prismatic"
       onClicked: {
         switchSource("prismatic/Prismatic.qml")
       }
     }
     Button {
       text: "Raycast"
       onClicked: {
         switchSource("raycast/Raycast.qml")
       }
     }
     Button {
       text: "Revolute"
       onClicked: {
         switchSource("revolute/Revolute.qml")
       }
     }
     Button {
       text: "Rope"
       onClicked: {
         switchSource("rope/Rope.qml")
       }
     }
     Button {
       text: "TwoWorlds"
       onClicked: {
         switchSource("twoWorlds/TwoWorlds.qml")
       }
     }
   }

   FileDialog {
     id: fileDialog
     title: "Please choose a qml file from a subfolder of the Box2D Examples"
     //selectedNameFilter: "Qml files (*.qml)" // doesnt work like that?
     onAccepted: {
       console.log("You chose: " + fileDialog.fileUrl)

       switchSource(fileDialog.fileUrl)
     }

     // open in the current folder, then there will be the subfolders like "boxes", "accelerometer", etc.
     folder: "."
   }

   function switchSource(newSource) {
     // clear the default world, so new bodies will find the new world
     // this is necessary because the loader first loads the new item and only then destroys the old one
     Box2D.defaultWorld = null

     // new the new PhysicsWorld will automatically be set as defaultWorld
     loader.source = newSource
   }

 }

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support