Coherent UI allows easy communication between your application and the UI via several mechanisms.
To trigger an event for the view use
For the view to be able to catch this event it must have executed:
engine, see JavaScript.Registering C++ functions for events triggered by JavaScript should happend the in the handler of Coherent::UI::ViewListener::OnReadyForBindings event.
Event handlers are registered with the Coherent::UI::View::RegisterForEvent method. They cannot return any value to JavaScript, but may trigger an event for JavaScript. There may be more than one C++ handler for a given event. There also may be both C++ and JavaScript handlers for the same event.
Triggering the event from JavaScript looks like:
Coherent::UI::MakeHandler uses template argument deduction to guess the signature of the handler. This requires several specializations of Coherent::UI::FunctorTraits and overloads of Coherent::UI::EventHandler::InvokeStub. To extend the number of arguments for event handlers supported by Coherent UI, you have to add additional specializations and overloads.
Call handlers are registered with the Coherent::UI::View::BindCall method. There may be only one handler for a given call and the handler may return a result.
To get the player name in the view is:
There are two context events:
Ready - triggered when the page loading has finished and Coherent::UI::ViewListener::OnReadyForBindings as been executed. Triggering events for C++ before Ready will result in strange 'disappearance' of events.Error - triggered when there was an error converting arguments from JavaScript to C++ (i.e. the C++ function expects a number, but JavaScript gives a string)You can remove a particular event or call hander using Coherent::UI::View::UnregisterFromEvent and Coherent::UI::View::UnbindCall using the Coherent::UI::BoundEventHandle returned from Coherent::UI::View::RegisterForEvent and Coherent::UI::View::BindCall. You can also remove all handlers that are methods of a particular object at once using Coherent::UI::View::UnbindObject.
To be able to use your C++ types as arguments or results for events and call handlers, the C++ type must be exposed to Coherent UI. Once exposed, Coherent UI will treat them just like any builtin type. To expose a C++ type to Coherent UI use the following pattern:
Note: If you want to use a user-defined type T, you must provide an overload of
otherwise you'll receive a compile-time error complaining about a missing overload of CoherentBind for the type T.
Coherent UI has built-in support for most STL containers and std:: classes.
| C++ Type | JavaScript Type | Header |
|---|---|---|
std::string | String | <Coherent\UI\Binding\String.h> |
std::vector | Array | <Coherent\UI\Binding\Vector.h> |
std::map | Object | <Coherent\UI\Binding\Map.h> |
std::pair | Object | <Coherent\UI\Binding\Pair.h> |
C style array | Array | <Coherent\UI\Binding\Array.h> |
Support for additional containers can be added in a similar way.
Registering type Player triggering events in both directions with instances of Player as argument and as a result of a call handler.
Then in JavaScript, we receive an object with the specified properties. The value of each property is the same as in the moment of triggering the event. We may store a reference to this object, but its properties WILL NOT be synchronised with g_Game.m_Player.
If you want to call C++ handler with an instance of Player created in JavaScript there is one important detail - the object must have a property __Type with value Player (the same name of the type we gave to Coherent::UI::Binder::RegisterType in CoherentBind for Player. Otherwise Coherent UI cannot treat the object as an instance of Player.
For some calls it's possible that there is no meaningful value to return. For example -