Advertisement

XML and angelscript UI

Started by September 05, 2012 05:00 AM
2 comments, last by WitchLord 12 years, 3 months ago
I have created an xml ui library and I originally was going to link v8 up to it so it kind of works like html. Though v8 has issues with mingw and I always planed to use Angel-script for everything else.

The problem is I was planning to have separate script file the defined behavior of different elements. Which is something I always would have liked in html. Of course there would be a script in each xml ui document for the specific needs of that page like changing settings.

I am stuck on how I would implement scripts for each xml element. It seems like the game example is kind of what I want but I have no idea how I could pass callback functions out of a as script and into c++ to be called later by some c++. It would need to be specific to each object.

For example(not necessarily exactly like this)

class Dialog: IController
{
const ElementObj @self;
const ElementObj @button;
Dialog(ElementObj @obj)
{
// Keep the owner for later reference
@self = obj;
button = document.create("button");
self.append(button);
button.addEvent(@self.ClickCallback,"click");
}

void ClickCallback(ElementObj @element)
{
self.setProperty("backgroundColor","red");
}
}



Also any other ideas about this general problem and what is the best way to implement it would be much appreciated.
It's currently not possible to take the address of a class method, so doing it exactly how you wrote it will not work. In a future version of AngelScript I'll add support for taking the address of class methods (probably with something similar to delegates in C#), but until then you have two different alternatives:

1. Use global functions as event handlers. The global function can receive the object pointer and call the desired class method.

2. Inform the name of the class method, and then the application can determine the correct method to call with asIObjectType::GetMethodByName.


I really can't say what the best way is, it will mostly depend on your needs and preferred ways of doing things. However if I were you I would try to provide a more flexible solution. Most of the event handlers that you'll tie to the xml nodes probably won't do much more than call a single function from the application, so implementing a full script class just to be able to bind one of the class methods as the event handlers is overkill. On the other hand, in some cases you'll likely have need for a more a complex implementation that a single function cannot do.

I would probably do it something like this:


<doc>
<button text="simple button" onClick="StartGame();" />
<button text="animated button" controller="ButtonAnimator" />
</doc>


The onClick attribute has an inline script that can be executed directly with something similar to the ExecuteString helper function.

The controller attribute in the second button gives the name of the script class that will be instanciated to control the animation of the button. That controller could be implemented similarly to your own example.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Thanks a lot Andreas.

I will probably have the features you mentioned as it makes writing xml easier. Though I still want to be able to make events like I mentioned as-well.

It sounds like I can do it with a function like

[source lang="java"]button.addEvent(self,"myClickCallback","click");[/source]

Which sounds great and pretty much functionally the same as what i wanted.
Yes, that should work well. The only drawback is that you don't have a compile time check for the existance of the method, but that should just be a minor issue.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement