Skip to content

Commit

Permalink
Merge pull request #24 from danakj/master
Browse files Browse the repository at this point in the history
Add a way for javascript to call back to the overlay plugin.
  • Loading branch information
hibiyasleep authored Nov 8, 2017
2 parents 003cd17 + b123929 commit cb552b1
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 5 deletions.
18 changes: 18 additions & 0 deletions HtmlRenderer/BuiltinFunctionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ class BuiltinFunctionHandler : CefV8Handler
{
public event EventHandler<BroadcastMessageEventArgs> BroadcastMessage;
public event EventHandler<SendMessageEventArgs> SendMessage;
public event EventHandler<SendMessageEventArgs> OverlayMessage;
public event EventHandler<EndEncounterEventArgs> EndEncounter;

public const string BroadcastMessageFunctionName = "broadcastMessage";
public const string SendMessageFunctionName = "sendMessage";
public const string OverlayMessageFunctionName = "overlayMessage";
public const string EndEncounterFunctionName = "endEncounter";

protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
Expand Down Expand Up @@ -54,6 +56,22 @@ protected override bool Execute(string name, CefV8Value obj, CefV8Value[] argume

return true;
}
else if (name == OverlayMessageFunctionName)
{
if (arguments.Length > 1)
{
if (OverlayMessage != null)
{
OverlayMessage(obj, new SendMessageEventArgs(arguments[0].GetStringValue(), arguments[1].GetStringValue()));
}
}
else
{
exception = "Invalid argument count.";
}

return true;
}
else if (name == EndEncounterFunctionName)
{
if (EndEncounter != null)
Expand Down
1 change: 1 addition & 0 deletions HtmlRenderer/LoadHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected override void OnLoadStart(CefBrowser browser, CefFrame frame)

var message = CefProcessMessage.Create("SetOverlayAPI");
message.Arguments.SetString(0, frame.Name);
message.Arguments.SetString(1, this.renderer.OverlayName);
browser.SendProcessMessage(CefProcessId.Renderer, message);
}

Expand Down
14 changes: 14 additions & 0 deletions HtmlRenderer/RenderProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public RenderProcessHandler()
{
Renderer.OnSendMessage(o, e);
};
this.builtinFunctionHandler.OverlayMessage += (o, e) =>
{
Renderer.OnOverlayMessage(o, e);
};
this.builtinFunctionHandler.EndEncounter += (o, e) =>
{
Renderer.OnRendererFeatureRequest(o, new RendererFeatureRequestEventArgs("EndEncounter"));
Expand All @@ -35,6 +39,7 @@ protected override bool OnProcessMessageReceived(CefBrowser browser, CefProcessI
// 対象のフレームを取得
var frameName = message.Arguments.GetString(0);
var frame = GetFrameByName(browser, frameName);
var overlayName = message.Arguments.GetString(1);

// API を設定
if (frame != null && frame.V8Context.Enter())
Expand All @@ -47,10 +52,15 @@ protected override bool OnProcessMessageReceived(CefBrowser browser, CefProcessI
var sendMessageFunction = CefV8Value.CreateFunction(
BuiltinFunctionHandler.SendMessageFunctionName,
builtinFunctionHandler);
var overlayMessageFunction = CefV8Value.CreateFunction(
BuiltinFunctionHandler.OverlayMessageFunctionName,
builtinFunctionHandler);
var endEncounterFunction = CefV8Value.CreateFunction(
BuiltinFunctionHandler.EndEncounterFunctionName,
builtinFunctionHandler);

apiObject.SetValue("overlayName", CefV8Value.CreateString(overlayName), CefV8PropertyAttribute.ReadOnly);

apiObject.SetValue(
BuiltinFunctionHandler.BroadcastMessageFunctionName,
broadcastMessageFunction,
Expand All @@ -59,6 +69,10 @@ protected override bool OnProcessMessageReceived(CefBrowser browser, CefProcessI
BuiltinFunctionHandler.SendMessageFunctionName,
sendMessageFunction,
CefV8PropertyAttribute.ReadOnly);
apiObject.SetValue(
BuiltinFunctionHandler.OverlayMessageFunctionName,
overlayMessageFunction,
CefV8PropertyAttribute.ReadOnly);
apiObject.SetValue(
BuiltinFunctionHandler.EndEncounterFunctionName,
endEncounterFunction,
Expand Down
16 changes: 14 additions & 2 deletions HtmlRenderer/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Renderer : IDisposable

public static event EventHandler<BroadcastMessageEventArgs> BroadcastMessage;
public static event EventHandler<SendMessageEventArgs> SendMessage;
public static event EventHandler<SendMessageEventArgs> OverlayMessage;
public static event EventHandler<RendererFeatureRequestEventArgs> RendererFeatureRequest;

// Guards access to |allBrowsers| across threads.
Expand Down Expand Up @@ -43,17 +44,22 @@ public CefBrowser Browser
}
}

public string OverlayName {
get { return overlayName; }
}

private Client Client { get; set; }

private int clickCount;
private CefMouseButtonType lastClickButton;
private DateTime lastClickTime;
private int lastClickPosX;
private int lastClickPosY;
private string overlayName;

public Renderer()
public Renderer(string overlayName)
{

this.overlayName = overlayName;
}

public void BeginRender(int width, int height, string url, int maxFrameRate = 30)
Expand Down Expand Up @@ -286,6 +292,12 @@ internal static void OnSendMessage(object sender, SendMessageEventArgs e)
}
}

internal static void OnOverlayMessage(object sender, SendMessageEventArgs e) {
if (OverlayMessage != null) {
OverlayMessage(sender, e);
}
}

internal static void OnRendererFeatureRequest(object sender, RendererFeatureRequestEventArgs e)
{
if (RendererFeatureRequest != null)
Expand Down
6 changes: 6 additions & 0 deletions OverlayPlugin.Common/IOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,11 @@ public interface IOverlay : IDisposable
/// </summary>
/// <param name="message">メッセージの内容。</param>
void SendMessage(string message);

/// <summary>
/// A message from javascript for the overlay plugin to consume.
/// </summary>
/// <param name="message">A string message created by the plugin javascript.</param>
void OverlayMessage(string message);
}
}
6 changes: 5 additions & 1 deletion OverlayPlugin.Core/OverlayBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected virtual void InitializeOverlay()
{
try
{
this.Overlay = new OverlayForm("about:blank", this.Config.MaxFrameRate);
this.Overlay = new OverlayForm(this.Name, "about:blank", this.Config.MaxFrameRate);

// グローバルホットキーを設定
if (this.Config.GlobalHotkeyEnabled)
Expand Down Expand Up @@ -383,5 +383,9 @@ public void SendMessage(string message)
this.Overlay.Renderer.ExecuteScript(script);
}
}

public virtual void OverlayMessage(string message)
{
}
}
}
4 changes: 2 additions & 2 deletions OverlayPlugin.Core/OverlayForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ public bool IsClickThru

public bool Locked { get; set; }

public OverlayForm(string url, int maxFrameRate = 30)
public OverlayForm(string overlayName, string url, int maxFrameRate = 30)
{
InitializeComponent();
Renderer.Initialize();

this.maxFrameRate = maxFrameRate;
this.Renderer = new Renderer();
this.Renderer = new Renderer(overlayName);
this.Renderer.Render += renderer_Render;
this.MouseWheel += OverlayForm_MouseWheel;

Expand Down
10 changes: 10 additions & 0 deletions OverlayPlugin.Core/PluginMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public void InitPlugin(TabPage pluginScreenSpace, Label pluginStatusText)
}
});
};
RainbowMage.HtmlRenderer.Renderer.OverlayMessage += (o, e) =>
{
Task.Run(() =>
{
var targetOverlay = this.Overlays.FirstOrDefault(x => x.Name == e.Target);
if (targetOverlay != null) {
targetOverlay.OverlayMessage(e.Message);
}
});
};
RainbowMage.HtmlRenderer.Renderer.RendererFeatureRequest += (o, e) =>
{
Task.Run(() =>
Expand Down

0 comments on commit cb552b1

Please sign in to comment.