NPAPI drawPlugin 从 NPP 实例检索 cgContext
我尝试在基于 basic-plugin。
我想在 NPCocoaEventMouseDown
上重绘插件,但我需要检索 cgContextRef
。
下面的方法适用于 NPCocoaEventDrawRect
但不适用于 NPCocoaEventMouseDown
因为这样我就无法使用 event->data.draw.context
>。我尝试检索 cgContextRef
CGContextRef cgContext = (NP_CGContext*)currentInstance->window.window
但似乎不起作用。这是我的函数:
void drawPlugin(NPP instance, NPCocoaEvent* event)
{
char* path = "/shot.png";
if(!instance || !event)
return;
PluginInstance* currentInstance = (PluginInstance*)(instance->pdata);
//CGContextRef cgContext = event->data.draw.context; //works with DrawRect
CGContextRef cgContext = (NP_CGContext*)currentInstance->window.window;
if (!cgContext) {
return;
}
float windowWidth = currentInstance->window.width;
float windowHeight = currentInstance->window.height;
CGContextSaveGState(cgContext);
//.....
CGContextRestoreGState(cgContext);
}
函数在此处调用:
int16_t NPP_HandleEvent(NPP instance, void* event)
{
NPCocoaEvent* cocoaEvent = (NPCocoaEvent*)event;
if (cocoaEvent && (cocoaEvent->type == NPCocoaEventDrawRect)) {
return 1;
}
if(cocoaEvent)
{
switch (cocoaEvent->type) {
case NPCocoaEventDrawRect:
drawPlugin(instance, (NPCocoaEvent*)event);
break;
case NPCocoaEventMouseDown:
drawPlugin(instance, (NPCocoaEvent*)event);
break;
default:
break;
}
return 1;
}
return 0;
}
如何在 NPCocoaEventMouseDown
中检索 cgContextRef
?
Im trying to draw a png in my (NPAPI)webplugin for Mac based on basic-plugin.
I would like to redraw the plugin on a NPCocoaEventMouseDown
but I'm having trouwens to retrieve the cgContextRef
.
The methode below works for the NPCocoaEventDrawRect
but doesn't for the NPCocoaEventMouseDown
because then I can't use event->data.draw.context
. I tried to retrieve the cgContextRef
with
CGContextRef cgContext = (NP_CGContext*)currentInstance->window.window
but that didn't seem to work. Here's my function:
void drawPlugin(NPP instance, NPCocoaEvent* event)
{
char* path = "/shot.png";
if(!instance || !event)
return;
PluginInstance* currentInstance = (PluginInstance*)(instance->pdata);
//CGContextRef cgContext = event->data.draw.context; //works with DrawRect
CGContextRef cgContext = (NP_CGContext*)currentInstance->window.window;
if (!cgContext) {
return;
}
float windowWidth = currentInstance->window.width;
float windowHeight = currentInstance->window.height;
CGContextSaveGState(cgContext);
//.....
CGContextRestoreGState(cgContext);
}
And the function is called here:
int16_t NPP_HandleEvent(NPP instance, void* event)
{
NPCocoaEvent* cocoaEvent = (NPCocoaEvent*)event;
if (cocoaEvent && (cocoaEvent->type == NPCocoaEventDrawRect)) {
return 1;
}
if(cocoaEvent)
{
switch (cocoaEvent->type) {
case NPCocoaEventDrawRect:
drawPlugin(instance, (NPCocoaEvent*)event);
break;
case NPCocoaEventMouseDown:
drawPlugin(instance, (NPCocoaEvent*)event);
break;
default:
break;
}
return 1;
}
return 0;
}
How can I retrieve the cgContextRef
in a NPCocoaEventMouseDown
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这两件事你都不能做。您在鼠标按下处理程序中调用 NPN_InvalidateRect,然后等待绘制回调。
因为该字段在 Cocoa 事件模型下始终为 NULL,如 Cocoa 事件规范中所述。仅在绘制调用期间明确向您提供 CGContextRef,并且仅要求它在该调用期间有效。 (如果您正在考虑缓存它以供以后使用:不要这样做。结果将是完全未定义的行为,可能无法工作,当然不能依赖它工作,并且几乎肯定会在某些时候导致崩溃在某些浏览器中。)
You can't do either of those things. You call NPN_InvalidateRect in your mouse-down handler, and wait to get a draw call-back.
Because that field is always NULL under the Cocoa event model, as documented in the Cocoa event spec. You are explicitly only provided a CGContextRef during a paint call, and it is only required to be valid for the duration of that call. (In case you are thinking of caching it for later use: don't. The results will be totally undefined behavior, probably won't work, certainly can't be relied on to work, and will almost certainly cause crashes at some point in some browser.)