|
| 1 | +/***************************************************************************** |
| 2 | +* |
| 3 | +* PROJECT: Multi Theft Auto v1.0 |
| 4 | +* LICENSE: See LICENSE in the top level directory |
| 5 | +* FILE: gui/CGUIWebBrowser_Impl.cpp |
| 6 | +* PURPOSE: WebBrowser widget class |
| 7 | +* |
| 8 | +* Multi Theft Auto is available from http://www.multitheftauto.com/ |
| 9 | +* |
| 10 | +*****************************************************************************/ |
| 11 | +#include "StdInc.h" |
| 12 | +#include <core/CWebViewInterface.h> |
| 13 | + |
| 14 | +// Use StaticImage here as we'd have to add the same definition twice to the Falagard definition file otherwise |
| 15 | +#define CGUIWEBBROWSER_NAME "CGUI/StaticImage" |
| 16 | + |
| 17 | +CGUIWebBrowser_Impl::CGUIWebBrowser_Impl ( CGUI_Impl* pGUI, CGUIElement* pParent ) |
| 18 | +{ |
| 19 | + // Initialize |
| 20 | + m_pImagesetManager = pGUI->GetImageSetManager (); |
| 21 | + m_pImageset = nullptr; |
| 22 | + m_pImage = nullptr; |
| 23 | + m_pGUI = pGUI; |
| 24 | + m_pManager = pGUI; |
| 25 | + m_pWebView = nullptr; |
| 26 | + |
| 27 | + // Get an unique identifier for CEGUI |
| 28 | + char szUnique [CGUI_CHAR_SIZE]; |
| 29 | + pGUI->GetUniqueName ( szUnique ); |
| 30 | + |
| 31 | + // Create the control and set default properties |
| 32 | + m_pWindow = pGUI->GetWindowManager ()->createWindow ( CGUIWEBBROWSER_NAME, szUnique ); |
| 33 | + m_pWindow->setDestroyedByParent ( false ); |
| 34 | + m_pWindow->setRect ( CEGUI::Relative, CEGUI::Rect ( 0.0f, 0.0f, 1.0f, 1.0f ) ); |
| 35 | + reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow ) -> setBackgroundEnabled ( false ); |
| 36 | + |
| 37 | + // Store the pointer to this CGUI element in the CEGUI element |
| 38 | + m_pWindow->setUserData ( reinterpret_cast < void* > ( this ) ); |
| 39 | + |
| 40 | + AddEvents (); |
| 41 | + |
| 42 | + // Apply browser events |
| 43 | + m_pWindow->subscribeEvent ( CEGUI::Window::EventMouseButtonDown, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_MouseButtonDown, this ) ); |
| 44 | + m_pWindow->subscribeEvent ( CEGUI::Window::EventMouseButtonUp, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_MouseButtonUp, this ) ); |
| 45 | + m_pWindow->subscribeEvent ( CEGUI::Window::EventMouseMove, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_MouseMove, this ) ); |
| 46 | + m_pWindow->subscribeEvent ( CEGUI::Window::EventMouseWheel, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_MouseWheel, this ) ); |
| 47 | + m_pWindow->subscribeEvent ( CEGUI::Window::EventActivated, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_Activated, this ) ); |
| 48 | + m_pWindow->subscribeEvent ( CEGUI::Window::EventDeactivated, CEGUI::Event::Subscriber ( &CGUIWebBrowser_Impl::Event_Deactivated, this ) ); |
| 49 | + |
| 50 | + // If a parent is specified, add it to it's children list, if not, add it as a child to the pManager |
| 51 | + if ( pParent ) |
| 52 | + { |
| 53 | + SetParent ( pParent ); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + pGUI->AddChild ( this ); |
| 58 | + SetParent ( nullptr ); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +CGUIWebBrowser_Impl::~CGUIWebBrowser_Impl () |
| 63 | +{ |
| 64 | + Clear(); |
| 65 | + |
| 66 | + DestroyElement (); |
| 67 | +} |
| 68 | + |
| 69 | +void CGUIWebBrowser_Impl::Clear () |
| 70 | +{ |
| 71 | + // Stop the control from using it |
| 72 | + reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow )->setImage ( nullptr ); |
| 73 | + |
| 74 | + // Kill the images |
| 75 | + if ( m_pImageset ) |
| 76 | + { |
| 77 | + m_pImageset->undefineAllImages (); |
| 78 | + m_pImagesetManager->destroyImageset ( m_pImageset ); |
| 79 | + m_pImage = nullptr; |
| 80 | + m_pImageset = nullptr; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +bool CGUIWebBrowser_Impl::LoadFromTexture ( IDirect3DTexture9* pTexture ) |
| 85 | +{ |
| 86 | + if ( m_pImageset && m_pImage ) |
| 87 | + { |
| 88 | + m_pImageset->undefineAllImages (); |
| 89 | + } |
| 90 | + |
| 91 | + CGUIWebBrowserTexture* pCEGUITexture = new CGUIWebBrowserTexture ( m_pGUI->GetRenderer (), pTexture ); |
| 92 | + |
| 93 | + // Get an unique identifier for CEGUI for the imageset |
| 94 | + char szUnique [CGUI_CHAR_SIZE]; |
| 95 | + m_pGUI->GetUniqueName ( szUnique ); |
| 96 | + |
| 97 | + // Create an imageset |
| 98 | + if ( !m_pImageset ) |
| 99 | + { |
| 100 | + while ( m_pImagesetManager->isImagesetPresent( szUnique ) ) |
| 101 | + m_pGUI->GetUniqueName ( szUnique ); |
| 102 | + m_pImageset = m_pImagesetManager->createImageset ( szUnique, pCEGUITexture, true ); |
| 103 | + } |
| 104 | + |
| 105 | + // Get an unique identifier for CEGUI for the image |
| 106 | + m_pGUI->GetUniqueName ( szUnique ); |
| 107 | + |
| 108 | + // Define an image and get its pointer |
| 109 | + m_pImageset->defineImage ( szUnique, CEGUI::Point ( 0, 0 ), CEGUI::Size ( pCEGUITexture->getWidth (), pCEGUITexture->getHeight () ), CEGUI::Point ( 0, 0 ) ); |
| 110 | + m_pImage = &m_pImageset->getImage ( szUnique ); |
| 111 | + |
| 112 | + // Set the image just loaded as the image to be drawn for the widget |
| 113 | + reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow )->setImage ( m_pImage ); |
| 114 | + |
| 115 | + // Success |
| 116 | + return true; |
| 117 | +} |
| 118 | + |
| 119 | +void CGUIWebBrowser_Impl::LoadFromWebView ( CWebViewInterface* pWebView ) |
| 120 | +{ |
| 121 | + m_pWebView = pWebView; |
| 122 | + |
| 123 | + // Load webview texture |
| 124 | + LoadFromTexture ( pWebView->GetTexture () ); |
| 125 | +} |
| 126 | + |
| 127 | +void CGUIWebBrowser_Impl::SetFrameEnabled ( bool bFrameEnabled ) |
| 128 | +{ |
| 129 | + reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow ) -> setFrameEnabled ( bFrameEnabled ); |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +bool CGUIWebBrowser_Impl::IsFrameEnabled ( void ) |
| 134 | +{ |
| 135 | + return reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow ) -> isFrameEnabled (); |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +CEGUI::Image* CGUIWebBrowser_Impl::GetDirectImage ( void ) |
| 140 | +{ |
| 141 | + return const_cast < CEGUI::Image* > ( reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow ) ->getImage () ); |
| 142 | +} |
| 143 | + |
| 144 | +void CGUIWebBrowser_Impl::Render ( void ) |
| 145 | +{ |
| 146 | + return reinterpret_cast < CEGUI::StaticImage* > ( m_pWindow ) -> render (); |
| 147 | +} |
| 148 | + |
| 149 | +bool CGUIWebBrowser_Impl::Event_MouseButtonDown ( const CEGUI::EventArgs& e ) |
| 150 | +{ |
| 151 | + const CEGUI::MouseEventArgs& args = reinterpret_cast < const CEGUI::MouseEventArgs& > ( e ); |
| 152 | + |
| 153 | + if ( args.button == CEGUI::MouseButton::LeftButton ) |
| 154 | + m_pWebView->InjectMouseDown ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_LEFT ); |
| 155 | + else if ( args.button == CEGUI::MouseButton::MiddleButton ) |
| 156 | + m_pWebView->InjectMouseDown ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_MIDDLE ); |
| 157 | + else if ( args.button == CEGUI::MouseButton::RightButton ) |
| 158 | + m_pWebView->InjectMouseDown ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_RIGHT ); |
| 159 | + |
| 160 | + return true; |
| 161 | +} |
| 162 | + |
| 163 | +bool CGUIWebBrowser_Impl::Event_MouseButtonUp ( const CEGUI::EventArgs& e ) |
| 164 | +{ |
| 165 | + const CEGUI::MouseEventArgs& args = reinterpret_cast < const CEGUI::MouseEventArgs& > ( e ); |
| 166 | + |
| 167 | + if ( args.button == CEGUI::MouseButton::LeftButton ) |
| 168 | + m_pWebView->InjectMouseUp ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_LEFT ); |
| 169 | + else if ( args.button == CEGUI::MouseButton::MiddleButton ) |
| 170 | + m_pWebView->InjectMouseUp ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_MIDDLE ); |
| 171 | + else if ( args.button == CEGUI::MouseButton::RightButton ) |
| 172 | + m_pWebView->InjectMouseUp ( eWebBrowserMouseButton::BROWSER_MOUSEBUTTON_RIGHT ); |
| 173 | + |
| 174 | + return true; |
| 175 | +} |
| 176 | + |
| 177 | +bool CGUIWebBrowser_Impl::Event_MouseMove ( const CEGUI::EventArgs& e ) |
| 178 | +{ |
| 179 | + const CEGUI::MouseEventArgs& args = reinterpret_cast < const CEGUI::MouseEventArgs& > ( e ); |
| 180 | + |
| 181 | + m_pWebView->InjectMouseMove ( (int)args.position.d_x - m_pWindow->windowToScreenX ( 0.0f ), (int)args.position.d_y - m_pWindow->windowToScreenY ( 0.0f ) ); |
| 182 | + return true; |
| 183 | +} |
| 184 | + |
| 185 | +bool CGUIWebBrowser_Impl::Event_MouseWheel ( const CEGUI::EventArgs& e ) |
| 186 | +{ |
| 187 | + const CEGUI::MouseEventArgs& args = reinterpret_cast < const CEGUI::MouseEventArgs& > ( e ); |
| 188 | + |
| 189 | + m_pWebView->InjectMouseWheel ( args.wheelChange * 40, 0 ); |
| 190 | + return true; |
| 191 | +} |
| 192 | + |
| 193 | +bool CGUIWebBrowser_Impl::Event_Activated ( const CEGUI::EventArgs& e ) |
| 194 | +{ |
| 195 | + m_pWebView->Focus ( true ); |
| 196 | + return true; |
| 197 | +} |
| 198 | + |
| 199 | +bool CGUIWebBrowser_Impl::Event_Deactivated ( const CEGUI::EventArgs& e ) |
| 200 | +{ |
| 201 | + m_pWebView->Focus ( false ); |
| 202 | + return true; |
| 203 | +} |
| 204 | + |
| 205 | + |
| 206 | +CGUIWebBrowserTexture::CGUIWebBrowserTexture ( CEGUI::Renderer* pOwner, IDirect3DTexture9* pTexture ) : CEGUI::DirectX9Texture ( pOwner ) |
| 207 | +{ |
| 208 | + m_pTexture = pTexture; |
| 209 | + |
| 210 | + // Get width and height |
| 211 | + D3DSURFACE_DESC SurfaceDesc; |
| 212 | + if ( pTexture->GetLevelDesc ( 0, &SurfaceDesc ) == ERROR_SUCCESS ) |
| 213 | + { |
| 214 | + m_Width = SurfaceDesc.Width; |
| 215 | + m_Height = SurfaceDesc.Height; |
| 216 | + } |
| 217 | +} |
0 commit comments