瀏覽代碼

create WebImage from existing http request

[email protected] 8 年之前
父節點
當前提交
856f9803a1
共有 4 個文件被更改,包括 68 次插入16 次删除
  1. 5 0
      oxygine/src/HttpRequestTask.cpp
  2. 1 0
      oxygine/src/HttpRequestTask.h
  3. 56 16
      oxygine/src/WebImage.cpp
  4. 6 0
      oxygine/src/WebImage.h

+ 5 - 0
oxygine/src/HttpRequestTask.cpp

@@ -116,6 +116,11 @@ namespace oxygine
         return _fname;
     }
 
+    const std::string& HttpRequestTask::getUrl() const
+    {
+        return _url;
+    }
+
     bool HttpRequestTask::_prerun()
     {
         _progressDeltaDelayed = 0;

+ 1 - 0
oxygine/src/HttpRequestTask.h

@@ -44,6 +44,7 @@ namespace oxygine
         const std::vector<unsigned char>&   getResponse() const;
         const std::vector<unsigned char>&   getPostData() const;
         const std::string&                  getFileName() const;
+        const std::string&                  getUrl() const;
 
 
         /**swap version of getResponse if you want to modify result buffer inplace*/

+ 56 - 16
oxygine/src/WebImage.cpp

@@ -15,7 +15,7 @@ namespace oxygine
     }
 
 
-    WebImage::WebImage() : _http(0)
+    WebImage::WebImage() : _http(0), _allowSwap(false)
     {
         setSize(64, 64);
         _image = new Sprite;
@@ -30,39 +30,65 @@ namespace oxygine
 
     void WebImage::load(const std::string& url)
     {
+        _allowSwap = true;
         _image->setResAnim(0);
 
-        _http = HttpRequestTask::create();
-        if (!_http)
+        spHttpRequestTask task = HttpRequestTask::create();
+        if (!task)
             return;
-        _http->setUrl(url);
-        _http->run();
+        task->setUrl(url);
+        task->run();
 
-        addRef();//protect actor for delete
-        _http->addEventListener(AsyncTask::COMPLETE, CLOSURE(this, &WebImage::loaded));
-        _http->addEventListener(AsyncTask::ERROR, CLOSURE(this, &WebImage::error));
+        _load(task);
     }
 
-    void WebImage::error(Event* e)
+    void WebImage::load(spHttpRequestTask task)
     {
-        dispatchEvent(e);
-        releaseRef();
-        _http = 0;
+        _allowSwap = false;
+        _image->setResAnim(0);
+        _load(task);
     }
 
-    void WebImage::loaded(Event* e)
+    void WebImage::_load(spHttpRequestTask task)
     {
-        if (_ref_counter <= 1)//if it is already dead
+        _http = task;
+
+        if (task->getStatus() == HttpRequestTask::status_completed)
         {
-            releaseRef();
+            init();
             return;
         }
 
+        if (task->getStatus() == HttpRequestTask::status_failed)
+        {
+            return;
+        }
+
+
+        addRef();//protect actor from delete
+        _http->addEventListener(AsyncTask::COMPLETE, CLOSURE(this, &WebImage::loaded));
+        _http->addEventListener(AsyncTask::ERROR, CLOSURE(this, &WebImage::error));
+    }
 
+    void WebImage::error(Event* e)
+    {
         dispatchEvent(e);
+        releaseRef();
+        _http = 0;
+    }
 
+    void WebImage::init()
+    {
         file::buffer bf;
-        _http->getResponseSwap(bf.data);
+        if (_allowSwap)
+        {
+            _http->getResponseSwap(bf.data);
+            _allowSwap = false;
+        }
+        else
+        {
+            bf.data = _http->getResponse();
+        }
 
         Image mt;
         if (mt.init(bf, true))
@@ -72,7 +98,21 @@ namespace oxygine
 
             fit();
         }
+        _http = 0;
+    }
+
+    void WebImage::loaded(Event* e)
+    {
+        if (_ref_counter <= 1)//if it is already dead
+        {
+            releaseRef();
+            return;
+        }
+
+
+        dispatchEvent(e);
 
+        init();
         _http = 0;
         releaseRef();
     }

+ 6 - 0
oxygine/src/WebImage.h

@@ -17,19 +17,25 @@ namespace oxygine
         ~WebImage();
 
         void load(const std::string& url);
+        void load(spHttpRequestTask task);
         void unload();
 
     private:
+        void _load(spHttpRequestTask task);
+
         void loaded(Event*);
         void error(Event*);
         void sizeChanged(const Vector2& size) override;
         void fit();
 
+        void init();
+
         ResAnim _rs;
 
         spSprite _image;
 
         spHttpRequestTask _http;
+        bool _allowSwap;
     };
 }