Skip to content

CEF CEGUI widget #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions MTA10/core/CWebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,18 @@ void CWebView::SetRenderingPaused ( bool bPaused )
m_pWebView->GetHost ()->WasHidden ( bPaused );
}

void CWebView::Focus ()
void CWebView::Focus ( bool state )
{
if ( m_pWebView )
{
m_pWebView->GetHost ()->SetFocus ( true );
m_pWebView->GetHost ()->SendFocusEvent ( true );
m_pWebView->GetHost ()->SetFocus ( state );
m_pWebView->GetHost ()->SendFocusEvent ( state );
}
g_pCore->GetWebCore()->SetFocusedWebView ( this );

if ( state )
g_pCore->GetWebCore ()->SetFocusedWebView ( this );
else if ( g_pCore->GetWebCore ()->GetFocusedWebView () == this )
g_pCore->GetWebCore ()->SetFocusedWebView ( nullptr );
}

void CWebView::ClearTexture ()
Expand Down
3 changes: 2 additions & 1 deletion MTA10/core/CWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class CWebView : public CWebViewInterface, private CefClient, private CefRenderH
void GetURL ( SString& outURL );
void GetTitle ( SString& outTitle );
void SetRenderingPaused ( bool bPaused );
void Focus ();
void Focus ( bool state = true );
IDirect3DTexture9* GetTexture () { return static_cast<IDirect3DTexture9*>(m_pWebBrowserRenderItem->m_pD3DTexture); }
void ClearTexture ();
inline void NotifyPaint () { m_PaintCV.notify_one (); }

Expand Down
217 changes: 217 additions & 0 deletions MTA10/gui/CGUIWebBrowser_Impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: gui/CGUIWebBrowser_Impl.cpp
* PURPOSE: WebBrowser widget class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
#include <core/CWebViewInterface.h>

// Use StaticImage here as we'd have to add the same definition twice to the Falagard definition file otherwise
#define CGUIWEBBROWSER_NAME "CGUI/StaticImage"

CGUIWebBrowser_Impl::CGUIWebBrowser_Impl ( CGUI_Impl* pGUI, CGUIElement* pParent )
{
// Initialize
m_pImagesetManager = pGUI->GetImageSetManager ();
m_pImageset = nullptr;
m_pImage = nullptr;
m_pGUI = pGUI;
m_pManager = pGUI;
m_pWebView = nullptr;

// Get an unique identifier for CEGUI
char szUnique [CGUI_CHAR_SIZE];
pGUI->GetUniqueName ( szUnique );

// Create the control and set default properties
m_pWindow = pGUI->GetWindowManager ()->createWindow ( CGUIWEBBROWSER_NAME, szUnique );
m_pWindow->setDestroyedByParent ( false );
m_pWindow->setRect ( CEGUI::Relative, CEGUI::Rect ( 0.0f, 0.0f, 1.0f, 1.0f ) );
reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow ) -> setBackgroundEnabled ( false );

// Store the pointer to this CGUI element in the CEGUI element
m_pWindow->setUserData ( reinterpret_cast < void* > ( this ) );

AddEvents ();

// Apply browser events
m_pWindow->subscribeEvent ( CEGUI::Window::EventMouseButtonDown, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_MouseButtonDown, this ) );
m_pWindow->subscribeEvent ( CEGUI::Window::EventMouseButtonUp, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_MouseButtonUp, this ) );
m_pWindow->subscribeEvent ( CEGUI::Window::EventMouseMove, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_MouseMove, this ) );
m_pWindow->subscribeEvent ( CEGUI::Window::EventMouseWheel, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_MouseWheel, this ) );
m_pWindow->subscribeEvent ( CEGUI::Window::EventActivated, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_Activated, this ) );
m_pWindow->subscribeEvent ( CEGUI::Window::EventDeactivated, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_Deactivated, this ) );

// If a parent is specified, add it to it's children list, if not, add it as a child to the pManager
if ( pParent )
{
SetParent ( pParent );
}
else
{
pGUI->AddChild ( this );
SetParent ( nullptr );
}
}

CGUIWebBrowser_Impl::~CGUIWebBrowser_Impl ()
{
Clear();

DestroyElement ();
}

void CGUIWebBrowser_Impl::Clear ()
{
// Stop the control from using it
reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow )->setImage ( nullptr );

// Kill the images
if ( m_pImageset )
{
m_pImageset->undefineAllImages ();
m_pImagesetManager->destroyImageset ( m_pImageset );
m_pImage = nullptr;
m_pImageset = nullptr;
}
}

bool CGUIWebBrowser_Impl::LoadFromTexture ( IDirect3DTexture9* pTexture )
{
if ( m_pImageset && m_pImage )
{
m_pImageset->undefineAllImages ();
}

CGUIWebBrowserTexture* pCEGUITexture = new CGUIWebBrowserTexture ( m_pGUI->GetRenderer (), pTexture );

// Get an unique identifier for CEGUI for the imageset
char szUnique [CGUI_CHAR_SIZE];
m_pGUI->GetUniqueName ( szUnique );

// Create an imageset
if ( !m_pImageset )
{
while ( m_pImagesetManager->isImagesetPresent( szUnique ) )
m_pGUI->GetUniqueName ( szUnique );
m_pImageset = m_pImagesetManager->createImageset ( szUnique, pCEGUITexture, true );
}

// Get an unique identifier for CEGUI for the image
m_pGUI->GetUniqueName ( szUnique );

// Define an image and get its pointer
m_pImageset->defineImage ( szUnique, CEGUI::Point ( 0, 0 ), CEGUI::Size ( pCEGUITexture->getWidth (), pCEGUITexture->getHeight () ), CEGUI::Point ( 0, 0 ) );
m_pImage = &m_pImageset->getImage ( szUnique );

// Set the image just loaded as the image to be drawn for the widget
reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow )->setImage ( m_pImage );

// Success
return true;
}

void CGUIWebBrowser_Impl::LoadFromWebView ( CWebViewInterface* pWebView )
{
m_pWebView = pWebView;

// Load webview texture
LoadFromTexture ( pWebView->GetTexture () );
}

void CGUIWebBrowser_Impl::SetFrameEnabled ( bool bFrameEnabled )
{
reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow ) -> setFrameEnabled ( bFrameEnabled );
}


bool CGUIWebBrowser_Impl::IsFrameEnabled ( void )
{
return reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow ) -> isFrameEnabled ();
}


CEGUI::Image* CGUIWebBrowser_Impl::GetDirectImage ( void )
{
return const_cast < CEGUI::Image* > ( reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow ) ->getImage () );
}

void CGUIWebBrowser_Impl::Render ( void )
{
return reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow ) -> render ();
}

bool CGUIWebBrowser_Impl::Event_MouseButtonDown ( const CEGUI::EventArgs& e )
{
const CEGUI::MouseEventArgs& args = reinterpret_cast < const CEGUI::MouseEventArgs& > ( e );

if ( args.button == CEGUI::MouseButton::LeftButton )
m_pWebView->InjectMouseDown ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_LEFT );
else if ( args.button == CEGUI::MouseButton::MiddleButton )
m_pWebView->InjectMouseDown ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_MIDDLE );
else if ( args.button == CEGUI::MouseButton::RightButton )
m_pWebView->InjectMouseDown ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_RIGHT );

return true;
}

bool CGUIWebBrowser_Impl::Event_MouseButtonUp ( const CEGUI::EventArgs& e )
{
const CEGUI::MouseEventArgs& args = reinterpret_cast < const CEGUI::MouseEventArgs& > ( e );

if ( args.button == CEGUI::MouseButton::LeftButton )
m_pWebView->InjectMouseUp ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_LEFT );
else if ( args.button == CEGUI::MouseButton::MiddleButton )
m_pWebView->InjectMouseUp ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_MIDDLE );
else if ( args.button == CEGUI::MouseButton::RightButton )
m_pWebView->InjectMouseUp ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_RIGHT );

return true;
}

bool CGUIWebBrowser_Impl::Event_MouseMove ( const CEGUI::EventArgs& e )
{
const CEGUI::MouseEventArgs& args = reinterpret_cast < const CEGUI::MouseEventArgs& > ( e );

m_pWebView->InjectMouseMove ( (int)args.position.d_x - m_pWindow->windowToScreenX ( 0.0f ), (int)args.position.d_y - m_pWindow->windowToScreenY ( 0.0f ) );
return true;
}

bool CGUIWebBrowser_Impl::Event_MouseWheel ( const CEGUI::EventArgs& e )
{
const CEGUI::MouseEventArgs& args = reinterpret_cast < const CEGUI::MouseEventArgs& > ( e );

m_pWebView->InjectMouseWheel ( args.wheelChange * 40, 0 );
return true;
}

bool CGUIWebBrowser_Impl::Event_Activated ( const CEGUI::EventArgs& e )
{
m_pWebView->Focus ( true );
return true;
}

bool CGUIWebBrowser_Impl::Event_Deactivated ( const CEGUI::EventArgs& e )
{
m_pWebView->Focus ( false );
return true;
}


CGUIWebBrowserTexture::CGUIWebBrowserTexture ( CEGUI::Renderer* pOwner, IDirect3DTexture9* pTexture ) : CEGUI::DirectX9Texture ( pOwner )
{
m_pTexture = pTexture;

// Get width and height
D3DSURFACE_DESC SurfaceDesc;
if ( pTexture->GetLevelDesc ( 0, &SurfaceDesc ) == ERROR_SUCCESS )
{
m_Width = SurfaceDesc.Width;
m_Height = SurfaceDesc.Height;
}
}
85 changes: 85 additions & 0 deletions MTA10/gui/CGUIWebBrowser_Impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: gui/CGUIWebBrowser_Impl.h
* PURPOSE: WebBrowser CGUI class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#ifndef __CGUIWEBBROWSER_IMPL_H
#define __CGUIWEBBROWSER_IMPL_H

#include <gui/CGUIWebBrowser.h>
#include "CGUITexture_Impl.h"
#include <renderers/directx9GUIRenderer/d3d9texture.h>

class CGUITexture;
class CGUITexture_Impl;
class CGUI_Impl;
class CWebViewInterface;

class CGUIWebBrowser_Impl : public CGUIWebBrowser, public CGUIElement_Impl
{
public:
CGUIWebBrowser_Impl ( CGUI_Impl* pGUI, CGUIElement* pParent = nullptr );
~CGUIWebBrowser_Impl ();
void Clear ();

void LoadFromWebView ( CWebViewInterface* pWebView );
bool LoadFromTexture ( IDirect3DTexture9* pD3DTexture );

void SetFrameEnabled ( bool bFrameEnabled );
bool IsFrameEnabled ();

CEGUI::Image* GetDirectImage ();

void Render ();

eCGUIType GetType () { return CGUI_WEBBROWSER; }

protected:
bool Event_MouseButtonDown ( const CEGUI::EventArgs& e );
bool Event_MouseButtonUp ( const CEGUI::EventArgs& e );
bool Event_MouseWheel ( const CEGUI::EventArgs& e );
bool Event_MouseMove ( const CEGUI::EventArgs& e );
bool Event_Activated ( const CEGUI::EventArgs& e );
bool Event_Deactivated ( const CEGUI::EventArgs& e );

private:
CGUI_Impl* m_pGUI;
CEGUI::ImagesetManager* m_pImagesetManager;
CEGUI::Imageset* m_pImageset;
const CEGUI::Image* m_pImage;

CWebViewInterface* m_pWebView;

#include "CGUIElement_Inc.h"
};


// The purpose of this class is to provide an externally managed DirectX texture
class CGUIWebBrowserTexture : public CEGUI::DirectX9Texture
{
public:
CGUIWebBrowserTexture ( CEGUI::Renderer* pOwner, IDirect3DTexture9* pTexture );

virtual ushort getWidth () const override { return m_Width; };
virtual ushort getHeight () const override { return m_Height; };

// Override with empty function (--> eliminate the functinions from DirectX9Texture)
virtual void loadFromFile ( const CEGUI::String& filename, const CEGUI::String& resourceGroup ) override {};
virtual void loadFromMemory ( const void* buffPtr, uint buffWidth, uint buffHeight ) override {};

virtual LPDIRECT3DTEXTURE9 getD3DTexture () const override { return m_pTexture; };
virtual void preD3DReset () {};
virtual void postD3DReset () {};

private:
IDirect3DTexture9* m_pTexture;
ushort m_Width;
ushort m_Height;
};

#endif
21 changes: 21 additions & 0 deletions MTA10/gui/CGUI_Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,12 @@ CGUIComboBox* CGUI_Impl::_CreateComboBox ( CGUIElement_Impl* pParent, const char
}


CGUIWebBrowser* CGUI_Impl::_CreateWebBrowser ( CGUIElement_Impl* pParent )
{
return new CGUIWebBrowser_Impl ( this, pParent );
}


CGUITexture* CGUI_Impl::CreateTexture ( void )
{
return new CGUITexture_Impl ( this );
Expand Down Expand Up @@ -1651,6 +1657,21 @@ CGUIComboBox* CGUI_Impl::CreateComboBox ( CGUIComboBox* pParent, const char* szC
return _CreateComboBox ( wnd, szCaption );
}


CGUIWebBrowser* CGUI_Impl::CreateWebBrowser ( CGUIElement* pParent )
{
CGUIWindow_Impl* wnd = reinterpret_cast < CGUIWindow_Impl* > ( pParent );
return _CreateWebBrowser ( wnd );
}


CGUIWebBrowser* CGUI_Impl::CreateWebBrowser ( CGUITab* pParent )
{
CGUITab_Impl* wnd = reinterpret_cast < CGUITab_Impl* > ( pParent );
return _CreateWebBrowser ( wnd );
}


void CGUI_Impl::CleanDeadPool ()
{
if ( m_pWindowManager )
Expand Down
4 changes: 4 additions & 0 deletions MTA10/gui/CGUI_Impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class CGUI_Impl : public CGUI, public CGUITabList
CGUIComboBox* CreateComboBox ( CGUIElement* pParent = NULL, const char* szCaption = "" );
CGUIComboBox* CreateComboBox ( CGUIComboBox* pParent = NULL, const char* szCaption = "" );

CGUIWebBrowser* CreateWebBrowser ( CGUIElement* pParent = nullptr );
CGUIWebBrowser* CreateWebBrowser ( CGUITab* pParent = nullptr );

CGUIWindow* CreateWnd ( CGUIElement* pParent = NULL, const char* szCaption = "" );
//

Expand Down Expand Up @@ -234,6 +237,7 @@ class CGUI_Impl : public CGUI, public CGUITabList
CGUIScrollPane* _CreateScrollPane ( CGUIElement_Impl* pParent = NULL );
CGUIScrollBar* _CreateScrollBar ( bool bHorizontal, CGUIElement_Impl* pParent = NULL );
CGUIComboBox* _CreateComboBox ( CGUIElement_Impl* pParent = NULL, const char* szCaption = "" );
CGUIWebBrowser* _CreateWebBrowser ( CGUIElement_Impl* pParent = nullptr );

void SubscribeToMouseEvents();
CGUIFont* CreateFntFromWinFont ( const char* szFontName, const char* szFontWinReg, const char* szFontWinFile, unsigned int uSize = 8, unsigned int uFlags = 0, bool bAutoScale = false );
Expand Down
1 change: 1 addition & 0 deletions MTA10/gui/StdInc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
#include "CGUIScrollPane_Impl.h"
#include "CGUITabPanel_Impl.h"
#include "CGUITexture_Impl.h"
#include "CGUIWebBrowser_Impl.h"
#include "CGUIWindow_Impl.h"
#include "CGUIComboBox_Impl.h"
Loading