Search code examples
c#xnaawesomium

detecting onClick with Awesomium in XNA


I'm building a menu system for an XNA framework game with Awesomium. The menus are visible and mouseState events are getting injected to my webViews successfully (have some CSS rollover stuff working). But I cant seem to execute methods in my C# code when the onClick event is triggered in my menu buttons.

C#

        private void OnViewProcessCreated(object sender, EventArgs e)
    {
        JSObject menu = webView.CreateGlobalJavascriptObject("menu");

        if (menu == null)
            return;

        using (menu)
            menu.BindAsync("onButtonClick", myJSMethodHandler);
    }

    private void myJSMethodHandler(object sender, JavascriptMethodEventArgs e)
    {
        if (e.MethodName == "onButtonClick")
        {
            WebCore.Shutdown();
        }
    }

JS/HTML

<!DOCTYPE html>
<html>
<head>
  <title>Main UI</title>
  <link rel="stylesheet" type="text/css" href="style.css">
    <script>
    {
        object.onclick = menu.onButtonClick;
    }
    </script>

</head>

<body>
    <div class="d1">
        <p><h1 class="button1" id="b1" onclick="menu.onButtonClick();">Agents</h1></p>
        <p><h1 class="button1" id="b2" onclick="">Research</h1></p>
        <p><h1 class="button1" id="b3" onclick="">Infrastructure</h1></p>
        <p><h1 class="button1" id="b4" onclick="">Financial</h1></p>
    </div>
</body>

</html>

Solution

  • Removed unnecessary JS function and called bound function directly from element.

        <!DOCTYPE html>
    <html>
    <head>
      <title>Main UI</title>
      <link rel="stylesheet" type="text/css" href="style.css">
    
    </head>
    
    <body>
        <div class="d1">
            <p><h1 class="button1" id="b1" onclick="menu.onButtonClick();">Agents</h1></p>
            <p><h1 class="button1" id="b2" onclick="">Research</h1></p>
            <p><h1 class="button1" id="b3" onclick="">Infrastructure</h1></p>
            <p><h1 class="button1" id="b4" onclick="">Financial</h1></p>
        </div>
    </body>
    
    </html>
    

    Also realised the OnViewProcessCreated function is never getting called, not sure why. Created a JSObject in the main Awesomium function and it ran fine.

        public AwesomiumMenu(string Source, Microsoft.Xna.Framework.Rectangle rectangle)
        {
            // CSS styling
            const string SCROLLBAR_CSS = "::-webkit-scrollbar { visibility: hidden; }";
            WebCore.Initialize(new WebConfig()
            {
                CustomCSS = SCROLLBAR_CSS
            });
    
            webView = WebCore.CreateWebView(rectangle.Width, rectangle.Height);
            webView.ReduceMemoryUsage();
            webView.Source = Source.ToUri();
            webView.IsTransparent = true;
    
            while (webView.IsLoading)
                WebCore.Update();
    
            Rectangle = rectangle;
            JSObject menu = webView.CreateGlobalJavascriptObject("menu");
            menu.BindAsync("onButtonClick", myJSMethodHandler);
        }