Thanks for the replys.
> I can think of two ways to do this.
>
> You can add an accessor to CppObjects that gets called whenever a
> value is assigned to 'onmousedown'. It creates a persistent handle to
> the object and stores it in the c++ object. When there is a mousedown
> event in c++ you can check if an event handler has been stored and
> call it if there is one.
Trying this approach, I have a C++ object like so:
class CppObject {
public:
v8::Persistent<v8::Function> onmousedown;
}
// and then later do all the stuff to set up the CppObject so it is
// available in javascript, then
func_inst->SetAccessor(String::New("onmousedown"), GetMouseDown, SetMouseDown);
// where SetMouseDown looks like:
void SetMouseDown(Local<String> property, Local<Value> value, const
AccessorInfo& info) {
Local<Object> self = info.Holder();
: Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* ptr = wrap->Value();
if (value->IsFunction()) {
Handle<Function> fun = Handle<Function>::Cast( value );
static_cast<CppObject *>(ptr)->onmousedown =
Persistent<Function>::New(fun);
}
}
This will tie everything in nicely. But, now when I try to Call the
function from my C++ code that polls events, I'm stuck on how to create
the receiver Object Handle. Could someone help me fill in the blanks
here?
In my PollEvents code, I have something like:
void PollEvents( ) {
Event * e = getEvent();
CppObject * tmp = findMouseDown(e);
if ( tmp !== NULL ) {
int argc = 2;
v8::Handle<v8::Value> argv[2];
argv[0] = v8::Number::New( e.x );
argv[1] = v8::Number::New( e.y );
v8::Handle<v8::Object> recv = ????
tmp->onmousedown->Call( recv, argc, argv);
}
}
Is this the right approach? Can I cast my "real" C++ object into a
Handle<Object> for passing into the Call function? If so, how?
many thanks -august.
>
> Alternatively you can store a reference to the javascript object
> itself in the c++ object and look up the 'onmousedown' property
> whenever an event occurs. If the property contains a function you
> call it. This gives you a bit more overhead when there is an event
> but may be simpler.
>
>
> -- Christian
>
> On Fri, Nov 21, 2008 at 1:44 PM, august <[EMAIL PROTECTED]> wrote:
> >
> >
> > hi,
> >
> > I'm looking to create something similar to the event model in a web
> > browser's DOM.
> >
> > I have a C++ object that can blit stuff to screen and I'd like to tie
> > in a callback from the javascript side for mouse events like so:
> >
> > var obj = CppObject();
> > obj.onmousedown = function ( e ) { // do stuff with e };
> >
> >
> > any ideas on how to do this? The "obj.onmousedown" will be called
> > from the C++ side of things as that is where the events are polled.
> >
> > any tips would be much appreciated. thank you - august.
> >
> > >
> >
--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---