]> git.mxchange.org Git - simgear.git/commitdiff
Allow to change texture buffer name on the fly
authorFrederic Bouvier <fredfgfs01@free.fr>
Fri, 18 May 2012 10:27:08 +0000 (12:27 +0200)
committerFrederic Bouvier <fredfgfs01@free.fr>
Fri, 18 May 2012 10:27:08 +0000 (12:27 +0200)
simgear/scene/material/TextureBuilder.cxx

index a361fee1ab8e8cc5ebb508a7cfef3e14be14169f..da788d6e62adc089f552416dcb264b2456b22446 100644 (file)
@@ -839,6 +839,38 @@ private:
     string buffer;
 };
 
+class BufferNameChangeListener : public SGPropertyChangeListener, public InitializeWhenAdded,
+      public Effect::Updater {
+public:
+    BufferNameChangeListener(Pass* p, int u, const std::string& pn) : pass(p), unit(u)
+    {
+        propName = new std::string(pn);
+    }
+    ~BufferNameChangeListener()
+    {
+        delete propName;
+        propName = 0;
+    }
+    void valueChanged(SGPropertyNode* node)
+    {
+        const char* buffer = node->getStringValue();
+        pass->setBufferUnit(unit, buffer);
+    }
+    void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
+    {
+        SGPropertyNode* listenProp = makeNode(propRoot, *propName);
+        delete propName;
+        propName = 0;
+        if (listenProp)
+            listenProp->addChangeListener(this, true);
+    }
+
+private:
+    ref_ptr<Pass> pass;
+    int unit;
+    std::string* propName;
+};
+
 Texture* GBufferBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* prop,
                                     const SGReaderWriterOptions* options)
 {
@@ -853,9 +885,15 @@ Texture* GBufferBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode*
                                                             "name");
     if (!nameProp)
         return 0;
-    buffer = nameProp->getStringValue();
 
-    pass->setBufferUnit( unit, buffer );
+    if (nameProp->nChildren() == 0) {
+        buffer = nameProp->getStringValue();
+        pass->setBufferUnit( unit, buffer );
+    } else {
+        std::string propName = getGlobalProperty(nameProp, options);
+        BufferNameChangeListener* listener = new BufferNameChangeListener(pass, unit, propName);
+        effect->addUpdater(listener);
+    }
 
     // Return white for now. Would be overridden in Technique::ProcessDrawable
     return StateAttributeFactory::instance()->getWhiteTexture();