#pragma once
#include "DrawEngine.h"
class HelloTutorialInstance
: public pp::Instance
, public pp::Graphics3DClient
{
public:
/// The constructor creates the plugin-side instance.
/// @param[in] instance the handle to the browser-side plugin instance.
explicit HelloTutorialInstance(PP_Instance instance)
: pp::Instance(instance)
, pp::Graphics3DClient(this)
, callback_factory_(this)
, m_bFocusMap_(false)
, m_bMapMove_(false)
{
this->RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | PP_INPUTEVENT_CLASS_WHEEL); //鼠标事件
this->RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); //键盘事件
m_DrawEngine_ = new DrawEngine();
m_Grapics3d_ = NULL;
pp::Module* module = pp::Module::Get();
gles2_if_ = static_cast<const PPB_OpenGLES2*>(module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE));
}
virtual ~HelloTutorialInstance();
virtual void Graphics3DContextLost()
{
PostMessage("Graphics3DContextLost");
}
//处理浏览器发给插件的消息
void HandleMessage(const pp::Var& var_message);
//处理改变浏览器插件大小的消息
void DidChangeView(const pp::Rect& position, const pp::Rect& clip);
//处理改变是否选中插件的消息
void DidChangeFocus(bool has_focus);
//输入事件的消息
bool HandleInputEvent (const pp::InputEvent &event);
//。。。init()
bool HandleDocumentLoad (const pp::URLLoader &url_loader);
//刷新
void CtrlRefresh(pp::Point theBeg, pp::Point theEnd);
private:
DrawEngine* m_DrawEngine_; //绘制
pp::Graphics3D* m_Grapics3d_; //3d
const PPB_OpenGLES2* gles2_if_;
private:
pp::CompletionCallbackFactory<HelloTutorialInstance> callback_factory_;
void Render(int32_t result);
};
#include "hello_world_test.h"
#include "GetUrl.h"
void HelloTutorialInstance::DidChangeView (const pp::Rect& position, const pp::Rect& clip)
{
if(m_Grapics3d_ != NULL)
if(!m_Grapics3d_->is_null())
return;
{
//当前3d环境的适配
m_DrawEngine_->m_CtrlWidth = position.size().width();
m_DrawEngine_->m_CtrlHeight = position.size().height();
//3d设备的属性
int32_t attribs[] = {
PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
PP_GRAPHICS3DATTRIB_SAMPLES, 0,
PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
PP_GRAPHICS3DATTRIB_WIDTH, m_DrawEngine_->m_CtrlWidth,
PP_GRAPHICS3DATTRIB_HEIGHT, m_DrawEngine_->m_CtrlHeight,
PP_GRAPHICS3DATTRIB_NONE
};
//创建3d设备
m_Grapics3d_ = new pp::Graphics3D(this, attribs);
if(!m_Grapics3d_ || m_Grapics3d_->is_null())
return;
if(BindGraphics(*m_Grapics3d_) == false)
{
delete m_Grapics3d_;
m_Grapics3d_ = NULL;
return ;
}
//初始化 OpenGlES的东西
glSetCurrentContextPPAPI(m_Grapics3d_->pp_resource());
/*m_DrawEngine_->m_Grapics3d_ = m_Grapics3d_;
if(!m_DrawEngine_->InitColorShader()) //颜色着色器失败
return ;
if(!m_DrawEngine_->InitTextureShader()) //纹理着色器
return;
m_DrawEngine_->SetRect(position.size().width(), position.size().height());*/
}
}
HelloTutorialInstance::~HelloTutorialInstance()
{
if (m_DrawEngine_ != NULL)
{
delete m_DrawEngine_;
m_DrawEngine_ = NULL;
}
if(m_Grapics3d_ != NULL)
{
delete m_Grapics3d_;
m_Grapics3d_ = NULL;
}
/*if(gles2_if_ != NULL)
{
delete gles2_if_;
gles2_if_ = NULL;
}*/
}
void HelloTutorialInstance::DidChangeFocus (bool has_focus)
{
}
bool HelloTutorialInstance::HandleInputEvent (const pp::InputEvent &event)
{
//事件处理
switch (event.GetType())
{
case PP_INPUTEVENT_TYPE_UNDEFINED:
break;
case PP_INPUTEVENT_TYPE_MOUSEDOWN:
{
//聚焦地图
m_bFocusMap_ = true;
pp::MouseInputEvent theInputEvent(event);
int32_t theClickCount = theInputEvent.GetClickCount();
//左击事件响应
if (theInputEvent.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT)
{
}
}
break;
case PP_INPUTEVENT_TYPE_MOUSEUP:
{
pp::MouseInputEvent theInputEvent(event);
int32_t theClickCount = theInputEvent.GetClickCount();
pp::Point theMouseUpPoint = theInputEvent.GetPosition();
if (theInputEvent.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT)
{
}
}
break;
case PP_INPUTEVENT_TYPE_MOUSEMOVE:
{
pp::MouseInputEvent theInputEvent(event);
pp::Point theMouseMovePoint = theInputEvent.GetPosition();
}
break;
case PP_INPUTEVENT_TYPE_MOUSEENTER:
break;
case PP_INPUTEVENT_TYPE_MOUSELEAVE:
m_bFocusMap_ = false;
break;
case PP_INPUTEVENT_TYPE_WHEEL:
{
pp::WheelInputEvent theInputEvent(event);
PP_FloatPoint thePointClick = theInputEvent.GetTicks();
}
break;
case PP_INPUTEVENT_TYPE_RAWKEYDOWN:
break;
case PP_INPUTEVENT_TYPE_KEYDOWN:
break;
case PP_INPUTEVENT_TYPE_KEYUP:
break;
case PP_INPUTEVENT_TYPE_CHAR:
break;
default:
return false;
break;
}
return true;
}
bool HelloTutorialInstance::HandleDocumentLoad (const pp::URLLoader &url_loader)
{
return true;
}
void HelloTutorialInstance::HandleMessage (const pp::Var& message)
{
std::string theReceiveMessage = message.AsString();
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(theReceiveMessage.c_str(), root);
if (parsingSuccessful)
{
std::string theCmdName = root["CmdName"].asString();
if(theCmdName == "CRTLREFRESH")
{
CtrlRefresh(pp::Point(0, 0), pp::Point(0, 0));
}
else
{
Json::Value theReturnInfo;
m_DrawEngine_->CommandParsing(theCmdName, root, theReturnInfo); //处理消息
pp::Var theReturnMessage(theReturnInfo.toStyledString()); //返回消息
PostMessage(theReturnMessage);
}
}
}
void HelloTutorialInstance::CtrlRefresh(pp::Point theBeg, pp::Point theEnd)
{
MapEngineES_Point beg;
MapEngineES_Point end;
beg.x = theBeg.x();
beg.y = theBeg.y();
end.x = theEnd.x();
end.y = theEnd.y();
/*glViewport(0, 0, m_DrawEngine_->m_CtrlWidth, m_DrawEngine_->m_CtrlHeight);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 1);*/
/*绘制代码*/
if(m_Grapics3d_ != NULL)
{
pp::CompletionCallback cb = callback_factory_.NewCallback(&HelloTutorialInstance::Render);
m_Grapics3d_->SwapBuffers(cb);
}
}
void HelloTutorialInstance::Render(int32_t result)
{
}
以上代码,详细解读了ppapi开发 c++ 使用的事件处理,消息处理,3d绘制初始化
以下几行代码是由浏览器主动调用来实现的
//处理浏览器发给插件的消息
void HandleMessage(const pp::Var& var_message);
//处理改变浏览器插件大小的消息
void DidChangeView(const pp::Rect& position, const pp::Rect& clip);
//处理改变是否选中插件的消息
void DidChangeFocus(bool has_focus);
//输入事件的消息
bool HandleInputEvent (const pp::InputEvent &event);
//。。。init()
bool HandleDocumentLoad (const pp::URLLoader &url_loader);
还是那句话,google相关使用文档已经说得很详细了,具体请参阅 ,开发前,花个几天时间读google文档真的很有必要