using Awesomium.Core;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using OpenWorld.Engine;
using System;
namespace WebDemo
{
class Program : Game
{
static void Main(string[] args)
{
Program pgm = new Program();
pgm.Run();
}
protected override PresentationParameters GetPresentationParameters()
{
var def = base.GetPresentationParameters();
def.Title = "OpenWorld goes Web";
return def;
}
Texture2D webTexture;
WebSession session;
WebView view;
protected override void OnLoad()
{
base.OnLoad();
FrameBuffer.ClearColor = Color.White;
this.webTexture = new Texture2D(
this.Width,
this.Height,
PixelInternalFormat.Rgba,
PixelFormat.Rgba,
PixelType.UnsignedByte);
WebConfig config = new WebConfig()
{
};
WebCore.Initialize(config, true);
this.session = WebCore.CreateWebSession(new WebPreferences()
{
WebAudio = true,
WebGL = true,
AllowInsecureContent = true,
WebSecurity = false,
AppCache = false,
CanScriptsOpenWindows = false,
CanScriptsCloseWindows = false,
CanScriptsAccessClipboard = true,
Plugins = false,
});
this.view = WebCore.CreateWebView(this.webTexture.Width, this.webTexture.Height, this.session, WebViewType.Offscreen);
this.view.IsTransparent = true; // Website background is transparent
this.view.CreateSurface += (s, e) =>
{
// Create Awesomium default bitmap surface
var bmpSurface = new BitmapSurface(e.Width, e.Height);
bmpSurface.Updated += (s1, e1) =>
{
// If the surface changes, update the OpenGL texture
this.webTexture.Load(bmpSurface.Buffer, bmpSurface.Width, bmpSurface.Height);
};
e.Surface = bmpSurface;
};
// Set injection events
Input.Mouse.Move += Mouse_Move;
Input.Mouse.ButtonDown += Mouse_ButtonDown;
Input.Mouse.ButtonUp += Mouse_ButtonUp;
Input.Mouse.WheelChanged += Mouse_WheelChanged;
view.Source = new Uri("http://masterq32.de/");
}
void Mouse_ButtonDown(object sender, OpenTK.Input.MouseButtonEventArgs e)
{
switch (e.Button)
{
case OpenTK.Input.MouseButton.Left:
view.InjectMouseDown(MouseButton.Left);
break;
case OpenTK.Input.MouseButton.Middle:
view.InjectMouseDown(MouseButton.Middle);
break;
case OpenTK.Input.MouseButton.Right:
view.InjectMouseDown(MouseButton.Right);
break;
}
}
void Mouse_ButtonUp(object sender, OpenTK.Input.MouseButtonEventArgs e)
{
switch (e.Button)
{
case OpenTK.Input.MouseButton.Left:
view.InjectMouseUp(MouseButton.Left);
break;
case OpenTK.Input.MouseButton.Middle:
view.InjectMouseUp(MouseButton.Middle);
break;
case OpenTK.Input.MouseButton.Right:
view.InjectMouseUp(MouseButton.Right);
break;
}
}
void Mouse_WheelChanged(object sender, OpenTK.Input.MouseWheelEventArgs e)
{
if (Input.Keyboard[OpenTK.Input.Key.ShiftLeft])
view.InjectMouseWheel(0, 20 * e.Delta);
else
view.InjectMouseWheel(20 * e.Delta, 0);
}
void Mouse_Move(object sender, OpenTK.Input.MouseMoveEventArgs e)
{
view.InjectMouseMove(e.X, e.Y);
}
protected override void OnUpdate(GameTime time)
{
// WebCore.Update must be called in OpenGL-Thread
OpenGL.Invoke(WebCore.Update);
base.OnUpdate(time);
}
protected override void OnDrawPreState(GameTime time)
{
// Clears the screen
FrameBuffer.Clear();
}
protected override void OnDrawPostState(GameTime time)
{
// Draws the web interface over everything else
this.Utilities.Draw(new Box2(0, 0, this.Width, this.Height), this.webTexture);
}
}
}