Browse Source

Better view handling and creation

Josh Engebretson 10 years ago
parent
commit
31b0619f1e

+ 16 - 9
Source/AtomicWebView/UIWebView.cpp

@@ -42,7 +42,7 @@ public:
     // For safe typecasting
     // For safe typecasting
     TBOBJECT_SUBCLASS(WebViewWidget, tb::TBWidget);
     TBOBJECT_SUBCLASS(WebViewWidget, tb::TBWidget);
 
 
-    WebViewWidget()
+    WebViewWidget() : browserCreated_(false)
     {
     {
         vertexData_.Resize(6 * UI_VERTEX_SIZE);
         vertexData_.Resize(6 * UI_VERTEX_SIZE);
         float color;
         float color;
@@ -73,12 +73,16 @@ public:
 
 
         UI* ui = webView_->GetSubsystem<UI>();
         UI* ui = webView_->GetSubsystem<UI>();
 
 
-        WebTexture2D* tex = webView_->GetWebTexture2D();
+        if (!browserCreated_)
+        {
+            browserCreated_ = true;
+            webView_->webClient_->CreateBrowser(webView_->initialURL_, rect.w, rect.h);
+        }
 
 
-        tex->SetCurrentSize(rect.w, rect.h);
+        webView_->webClient_->SetSize(rect.w, rect.h);
 
 
-        float umax = (float)tex->GetCurrentWidth()/(float)tex->GetMaxWidth();
-        float vmax = (float)tex->GetCurrentHeight()/(float)tex->GetMaxHeight();
+        float umax = 1.0f;
+        float vmax = 1.0f;
 
 
         float color;
         float color;
         float fopacity = GetOpacity() * ui->GetRenderer()->GetOpacity();
         float fopacity = GetOpacity() * ui->GetRenderer()->GetOpacity();
@@ -119,19 +123,20 @@ public:
         data[30] = rect.x;
         data[30] = rect.x;
         data[31] = rect.y + rect.h;
         data[31] = rect.y + rect.h;
 
 
-        ui->SubmitBatchVertexData(tex->GetTexture2D(), vertexData_);
+        ui->SubmitBatchVertexData(webView_->GetWebTexture2D()->GetTexture2D(), vertexData_);
 
 
     }
     }
 
 
 private:
 private:
 
 
+    bool browserCreated_;
+
     WeakPtr<UIWebView> webView_;
     WeakPtr<UIWebView> webView_;
     PODVector<float> vertexData_;
     PODVector<float> vertexData_;
 
 
 };
 };
 
 
-UIWebView::UIWebView(Context* context) : UIWidget(context, false),
-    resizeRequired_(false)
+UIWebView::UIWebView(Context* context, const String &initialURL) : UIWidget(context, false)
 {
 {
     widget_ = new WebViewWidget();
     widget_ = new WebViewWidget();
     widget_->SetDelegate(this);
     widget_->SetDelegate(this);
@@ -144,7 +149,9 @@ UIWebView::UIWebView(Context* context) : UIWidget(context, false),
     webClient_ = new WebClient(context);
     webClient_ = new WebClient(context);
     webTexture_ = new WebTexture2D(context);
     webTexture_ = new WebTexture2D(context);
     webClient_->SetWebRenderHandler(webTexture_);
     webClient_->SetWebRenderHandler(webTexture_);
-    webClient_->CreateBrowser();
+
+    initialURL_ = initialURL;
+
 }
 }
 
 
 UIWebView::~UIWebView()
 UIWebView::~UIWebView()

+ 5 - 5
Source/AtomicWebView/UIWebView.h

@@ -37,15 +37,15 @@ class WebTexture2D;
 
 
 class UIWebView : public UIWidget
 class UIWebView : public UIWidget
 {
 {
+    friend class WebViewWidget;
+
     OBJECT(UIWebView)
     OBJECT(UIWebView)
 
 
 public:
 public:
 
 
-    UIWebView(Context* context);
+    UIWebView(Context* context, const String& initialURL = String::EMPTY);
     virtual ~UIWebView();
     virtual ~UIWebView();
 
 
-    void SetResizeRequired() { resizeRequired_  = true; }
-
     WebTexture2D* GetWebTexture2D() const;
     WebTexture2D* GetWebTexture2D() const;
 
 
 protected:
 protected:
@@ -54,11 +54,11 @@ protected:
 
 
 private:
 private:
 
 
-    bool resizeRequired_;
-
     SharedPtr<WebClient> webClient_;
     SharedPtr<WebClient> webClient_;
     SharedPtr<WebTexture2D> webTexture_;
     SharedPtr<WebTexture2D> webTexture_;
 
 
+    String initialURL_;
+
 };
 };
 
 
 }
 }

+ 43 - 40
Source/AtomicWebView/WebClient.cpp

@@ -61,11 +61,20 @@ public:
         return false;
         return false;
     }
     }
 
 
-    bool CreateBrowser()
+    bool CreateBrowser(const String& initialURL, int width, int height)
     {
     {
+        if (webClient_->renderHandler_.Null())
+        {
+            LOGERROR("WebClient::CreateBrowser - No render handler specified");
+            return false;
+        }
+
         CefWindowInfo windowInfo;
         CefWindowInfo windowInfo;
         CefBrowserSettings browserSettings;
         CefBrowserSettings browserSettings;
 
 
+        windowInfo.width = width;
+        windowInfo.height = height;
+
         Graphics* graphics = webClient_->GetSubsystem<Graphics>();
         Graphics* graphics = webClient_->GetSubsystem<Graphics>();
 
 
         SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
         SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
@@ -77,13 +86,14 @@ public:
             NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
             NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
             windowInfo.SetAsWindowless(view, false);
             windowInfo.SetAsWindowless(view, false);
 
 
+            webClient_->renderHandler_->SetSize(width, height);
             CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
             CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
-                                                                              "https://youtube.com/", browserSettings, nullptr);
+                                                                              initialURL.CString(), browserSettings, nullptr);
 
 
             if (!browser.get())
             if (!browser.get())
                 return false;
                 return false;
 
 
-            browsers_.Push(browser);
+            browser_ = browser;
 
 
             return true;
             return true;
         }
         }
@@ -107,53 +117,33 @@ public:
     {
     {
         CEF_REQUIRE_UI_THREAD();
         CEF_REQUIRE_UI_THREAD();
 
 
-        // Remove from the list of existing browsers.
-        Vector<CefRefPtr<CefBrowser>>::Iterator itr = browsers_.Begin();
-        while (itr != browsers_.End())
-        {
-            if ((*itr)->IsSame(browser))
-            {
-                browsers_.Erase(itr);
-                break;
-            }
+        if (browser->IsSame(browser_))
+            browser_ = nullptr;
 
 
-            itr++;
-        }
     }
     }
 
 
-    void CloseAllBrowsers(bool force_close)
+    void CloseBrowser(bool force_close)
     {
     {
         if (!CefCurrentlyOn(TID_UI))
         if (!CefCurrentlyOn(TID_UI))
         {
         {
             // Execute on the UI thread.
             // Execute on the UI thread.
             CefPostTask(TID_UI,
             CefPostTask(TID_UI,
-                        base::Bind(&WebClientPrivate::CloseAllBrowsers, this, force_close));
+                        base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
 
 
             return;
             return;
         }
         }
 
 
-        if (!browsers_.Size())
+        if (!browser_.get())
             return;
             return;
 
 
-        // make a copy of vector, as we'll be erasing as we go
-        Vector<CefRefPtr<CefBrowser>> browsers = browsers_;
-
-        Vector<CefRefPtr<CefBrowser>>::Iterator itr = browsers.Begin();
-
-        while (itr != browsers.End())
-        {
-            (*itr)->GetHost()->CloseBrowser(force_close);
-            itr++;
-        }
-
-        browsers_.Clear();
+        browser_->GetHost()->CloseBrowser(force_close);
     }
     }
 
 
     IMPLEMENT_REFCOUNTING(WebClientPrivate);
     IMPLEMENT_REFCOUNTING(WebClientPrivate);
 
 
 private:
 private:
 
 
-    Vector<CefRefPtr<CefBrowser>> browsers_;
+    CefRefPtr<CefBrowser> browser_;
     WeakPtr<WebBrowserHost> webBrowserHost_;
     WeakPtr<WebBrowserHost> webBrowserHost_;
     WeakPtr<WebClient> webClient_;
     WeakPtr<WebClient> webClient_;
 
 
@@ -173,11 +163,10 @@ WebClient::~WebClient()
 
 
 void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const
 void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const
 {
 {
-    if (!d_->browsers_.Size())
+    if (!d_->browser_.get())
         return;
         return;
 
 
-    CefRefPtr<CefBrowser> browser = d_->browsers_[0];
-    CefRefPtr<CefBrowserHost> host = browser->GetHost();
+    CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
 
 
     CefMouseEvent mevent;
     CefMouseEvent mevent;
     mevent.x = x;
     mevent.x = x;
@@ -194,11 +183,10 @@ void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp,
 
 
 void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
 void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
 {
 {
-    if (!d_->browsers_.Size())
+    if (!d_->browser_.get())
         return;
         return;
 
 
-    CefRefPtr<CefBrowser> browser = d_->browsers_[0];
-    CefRefPtr<CefBrowserHost> host = browser->GetHost();
+    CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
 
 
     CefMouseEvent mevent;
     CefMouseEvent mevent;
     mevent.x = x;
     mevent.x = x;
@@ -215,15 +203,30 @@ void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLe
 
 
 void WebClient::WasResized()
 void WebClient::WasResized()
 {
 {
-    if (!d_->browsers_.Size())
+    if (!d_->browser_.get())
         return;
         return;
 
 
-    d_->browsers_[0]->GetHost()->WasResized();;
+    CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
+    host->WasResized();;
 }
 }
 
 
-bool WebClient::CreateBrowser()
+bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
 {
 {
-    return d_->CreateBrowser();
+    return d_->CreateBrowser(initialURL, width, height);
+}
+
+void WebClient::SetSize(int width, int height)
+{
+    if (renderHandler_.Null())
+        return;
+
+    if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
+        return;
+
+    renderHandler_->SetSize(width, height);
+
+    WasResized();
+
 }
 }
 
 
 void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
 void WebClient::SetWebRenderHandler(WebRenderHandler* handler)

+ 6 - 4
Source/AtomicWebView/WebClient.h

@@ -25,20 +25,22 @@ public:
     /// Destruct.
     /// Destruct.
     virtual ~WebClient();
     virtual ~WebClient();
 
 
-    // call once initialized with handlers
-    bool CreateBrowser();
+    /// call once initialized with handlers
+    bool CreateBrowser(const String& initialURL, int width, int height);
+
+    void SetSize(int width, int height);
 
 
     void SetWebRenderHandler(WebRenderHandler* handler);
     void SetWebRenderHandler(WebRenderHandler* handler);
 
 
     CefClient* GetCefClient();
     CefClient* GetCefClient();
 
 
-    void WasResized();
-
     void SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const;
     void SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const;
     void SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave = false) const;
     void SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave = false) const;
 
 
 private:
 private:
 
 
+    void WasResized();
+
     SharedPtr<WebRenderHandler> renderHandler_;
     SharedPtr<WebRenderHandler> renderHandler_;
 
 
     WebClientPrivate* d_;
     WebClientPrivate* d_;

+ 1 - 5
Source/AtomicWebView/WebRenderHandler.cpp

@@ -10,11 +10,7 @@ namespace Atomic
 {
 {
 
 
 
 
-WebRenderHandler::WebRenderHandler(Context* context) : Object (context),
-    currentWidth_(0),
-    currentHeight_(0),
-    maxHeight_(0),
-    maxWidth_(0)
+WebRenderHandler::WebRenderHandler(Context* context) : Object (context)
 {
 {
 }
 }
 
 

+ 3 - 10
Source/AtomicWebView/WebRenderHandler.h

@@ -22,11 +22,10 @@ public:
     /// Destruct.
     /// Destruct.
     virtual ~WebRenderHandler();
     virtual ~WebRenderHandler();
 
 
-    unsigned GetCurrentWidth() const { return currentWidth_; }
-    unsigned GetCurrentHeight() const { return currentHeight_; }
+    virtual int GetWidth() const = 0;
+    virtual int GetHeight() const = 0;
 
 
-    unsigned GetMaxWidth() const { return maxWidth_; }
-    unsigned GetMaxHeight() const { return maxHeight_; }
+    virtual void SetSize(int width, int height) = 0;
 
 
     void SetWebClient(WebClient* webClient);
     void SetWebClient(WebClient* webClient);
     WebClient* GetWebClient() const;
     WebClient* GetWebClient() const;
@@ -35,12 +34,6 @@ public:
 
 
 protected:
 protected:
 
 
-    unsigned currentWidth_;
-    unsigned currentHeight_;
-
-    unsigned maxHeight_;
-    unsigned maxWidth_;
-
     WeakPtr<WebClient> webClient_;
     WeakPtr<WebClient> webClient_;
 
 
 
 

+ 16 - 34
Source/AtomicWebView/WebTexture2D.cpp

@@ -28,7 +28,7 @@ public:
 
 
     bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) OVERRIDE
     bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) OVERRIDE
     {
     {
-        rect = CefRect(0, 0, webTexture2D_->GetCurrentWidth(), webTexture2D_->GetCurrentHeight());
+        rect = CefRect(0, 0, webTexture2D_->GetWidth(), webTexture2D_->GetHeight());
         return true;
         return true;
     }
     }
 
 
@@ -38,7 +38,11 @@ public:
 
 
         if (type == PET_VIEW)
         if (type == PET_VIEW)
         {
         {
-            webTexture2D_->texture_->SetData(0, 0, 0, width, height, buffer);
+            if (width == webTexture2D_->GetWidth() && height == webTexture2D_->GetHeight())
+            {
+                Texture2D* tex = webTexture2D_->texture_;
+                tex->SetData(0, 0, 0, width, height, buffer);
+            }
         }
         }
 
 
     }
     }
@@ -49,7 +53,7 @@ private:
 
 
 };
 };
 
 
-WebTexture2D::WebTexture2D(Context* context, int width, int height) : WebRenderHandler(context)
+WebTexture2D::WebTexture2D(Context* context) : WebRenderHandler(context)
 {
 {
     d_ = new WebTexture2DPrivate(this);
     d_ = new WebTexture2DPrivate(this);
     d_->AddRef();
     d_->AddRef();
@@ -58,8 +62,6 @@ WebTexture2D::WebTexture2D(Context* context, int width, int height) : WebRenderH
     texture_->SetNumLevels(1);
     texture_->SetNumLevels(1);
     texture_->SetFilterMode(FILTER_BILINEAR);
     texture_->SetFilterMode(FILTER_BILINEAR);
 
 
-    SetCurrentSize(width, height);
-
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     ResourceCache* cache = GetSubsystem<ResourceCache>();
     material_ = new Material(context_);
     material_ = new Material(context_);
     material_->SetTechnique(0, cache->GetResource<Technique>("Techniques/DiffUnlit.xml"));
     material_->SetTechnique(0, cache->GetResource<Technique>("Techniques/DiffUnlit.xml"));
@@ -76,42 +78,22 @@ CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
     return d_;
     return d_;
 }
 }
 
 
-
-void WebTexture2D::SetCurrentSize(unsigned width, unsigned height)
+int WebTexture2D::GetWidth() const
 {
 {
-    if (currentWidth_ == width && currentHeight_ == height)
-        return;
-
-    currentWidth_ = width;
-    currentHeight_ = height;
-
-    unsigned newMaxWidth = NextPowerOfTwo(width);
-    unsigned newMaxHeight = NextPowerOfTwo(height);
-
-    SetMaxSize(newMaxWidth, newMaxHeight);
-
-    if (webClient_.NotNull())
-        webClient_->WasResized();
-
+    return texture_->GetWidth();
 }
 }
 
 
-void WebTexture2D::SetMaxSize(unsigned width, unsigned height)
+int WebTexture2D::GetHeight() const
 {
 {
-    if (maxWidth_ == width && height == maxHeight_)
-        return;
+    return texture_->GetHeight();
+}
 
 
-    if (!IsPowerOfTwo(width) || !IsPowerOfTwo(height))
-    {
-        LOGERRORF("WebTexture2D::SetMaxSize - attempted to set size to %ux%u (must be a power of two)", width, height );
+void WebTexture2D::SetSize(int width, int height)
+{
+    if (width == texture_->GetWidth() && height == texture_->GetHeight())
         return;
         return;
-    }
-
-    maxWidth_ = width;
-    maxHeight_ = height;
-
-    texture_->SetSize(maxWidth_, maxHeight_, Graphics::GetBGRAFormat(), TEXTURE_DYNAMIC);
 
 
-    LOGDEBUGF("WebTexture2D max size set: %u %u", maxWidth_, maxHeight_);
+    texture_->SetSize(width, height, Graphics::GetBGRAFormat(), TEXTURE_DYNAMIC);
 
 
 }
 }
 
 

+ 5 - 3
Source/AtomicWebView/WebTexture2D.h

@@ -20,12 +20,14 @@ class ATOMIC_API WebTexture2D : public WebRenderHandler
 public:
 public:
 
 
     /// Construct.
     /// Construct.
-    WebTexture2D(Context* context, int width = 1024, int height = 1024);
+    WebTexture2D(Context* context);
     /// Destruct.
     /// Destruct.
     virtual ~WebTexture2D();
     virtual ~WebTexture2D();
 
 
-    virtual void SetCurrentSize(unsigned width, unsigned height);
-    virtual void SetMaxSize(unsigned width, unsigned height);
+    int GetWidth() const;
+    int GetHeight() const;
+
+    void SetSize(int width, int height);
 
 
     CefRenderHandler* GetCEFRenderHandler();
     CefRenderHandler* GetCEFRenderHandler();