Selaa lähdekoodia

Generalize out effects

Change-Id: I196df6f037f153bf84952dae7a6e86256671dc39
Donald Carr 9 vuotta sitten
vanhempi
commit
2075b37c7a
6 muutettua tiedostoa jossa 64 lisäystä ja 14 poistoa
  1. 10 4
      qml/Gravity.qml
  2. 11 9
      qml/VisualEffect.qml
  3. 38 0
      qml/effects/Effects.qml
  4. 1 0
      qml/effects/qmldir
  5. 2 1
      qml/main.qml
  6. 2 0
      qml/qml.qrc

+ 10 - 4
qml/Gravity.qml

@@ -1,5 +1,6 @@
 import QtQuick 2.5
 import Box2D 2.0
+import "effects"
 
 Item {
     id: root
@@ -22,7 +23,7 @@ Item {
         property int columnCount: settings.columnCount
         property bool running: primedColumns >= columnCount
         property bool globalWorld: settings.globalWorld
-        property bool embossEffect: settings.embossEffect
+        property string effect: settings.effect
 
         function reset() {
             itemCount = 0
@@ -68,10 +69,10 @@ Item {
             function addImage() {
                 var image = pictureDelegate.createObject(column, { x: -1000, y: -1000 })
 
-                if (d.embossEffect) {
-                  image.effect = effectDelegate.createObject(column)
-                  image.effect.target = image
+                if (d.effect !== "" && Effects.validate(d.effect)) {
+                    image.effect = effectDelegate.createObject(column, { target: image, effect: d.effect })
                 }
+
                 image.beyondThePale.connect(removeImage)
                 image.world = d.globalWorld ? world : isolatedWorld
                 image.x = xOffset
@@ -230,4 +231,9 @@ Item {
 
     Keys.onUpPressed: root.togglePause()
     Keys.onDownPressed: root.toggleChaos() //root.next()
+
+    Component.onCompleted: {
+        pictureDelegate.status !== Component.Ready && console.log('Component failed with:' + pictureDelegate.errorString())
+        effectDelegate.status !== Component.Ready && console.log('Component failed with:' + effectDelegate.errorString())
+    }
 }

+ 11 - 9
qml/VisualEffect.qml

@@ -1,21 +1,23 @@
 import QtQuick 2.5
-import "effects" as Effects
+import "effects"
 
 Item {
-    property alias target: source.sourceItem
+    id: root
+    property alias target: effectSource.sourceItem
+
+    property string effect: "Random"
+    property var effectObject
 
     anchors.fill: target
+
     ShaderEffectSource {
-        id: source
-        smooth: true
+        id: effectSource
+        smooth: settings.smoothedEffects
         hideSource: true
         sourceItem: target
     }
 
-    Effects.Emboss {
-        source: source
-        anchors.fill: parent
-        targetWidth: parent.width
-        targetHeight: parent.height
+    Component.onCompleted: {
+        effectObject = Effects.getComponent(effect).createObject(root, { "source": effectSource, "anchors.fill": root, "targetWidth": root.width, "targetHeight": root.height })
     }
 }

+ 38 - 0
qml/effects/Effects.qml

@@ -0,0 +1,38 @@
+pragma Singleton
+
+import QtQuick 2.5
+
+Item {
+    property var names: ["Emboss", "Billboard", "GaussianBlur"]
+    property var components: []
+    property string randomEffectText: "Random"
+
+    function indexOf(name) {
+        if (name === randomEffectText)
+            return Math.floor(Math.random()*components.length)
+        else
+            return names.indexOf(name)
+    }
+
+    function validate(name) {
+        var valid = (name === randomEffectText) || (indexOf(name) !== -1)
+        if (!valid) console.log('Requested effect: ' + name + ' does not exist')
+        return valid
+    }
+
+    function getComponent(name) {
+        var i = indexOf(name)
+        return components[i]
+    }
+
+    Component.onCompleted: {
+        names.forEach(function(name) {
+            var comp = Qt.createComponent(name + ".qml")
+            if (comp.status !== Component.Ready) {
+                console.log('Component failed with:' + comp.errorString())
+            } else {
+                components.push(comp)
+            }
+        })
+    }
+}

+ 1 - 0
qml/effects/qmldir

@@ -0,0 +1 @@
+singleton Effects 1.0 Effects.qml

+ 2 - 1
qml/main.qml

@@ -14,11 +14,12 @@ Window {
         property int columnCount: 5
         property int interval: 5
         property real pace: 1
+        property bool smoothedEffects: false
         property bool viewItemCount: false
         property bool globalWorld: false
         // Very computationally heavy: 40% vs 20% for 0.1 vs 0
         property real restitution: 0
-        property bool embossEffect: false
+        property string effect: ""
     }
 
     Rectangle {

+ 2 - 0
qml/qml.qrc

@@ -12,5 +12,7 @@
         <file>effects/Emboss.qml</file>
         <file>effects/GaussianBlur.qml</file>
         <file>VisualEffect.qml</file>
+        <file>effects/Effects.qml</file>
+        <file>effects/qmldir</file>
     </qresource>
 </RCC>