Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using CefSharp;
- using CefSharp.WinForms;
- using MoonSharp.Interpreter;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Windows.Input;
- using Timer = System.Windows.Forms.Timer;
- using Cursor = System.Windows.Forms.Cursor;
- using Gma.System.MouseKeyHook;
- namespace Lua_To_HTML
- {
- public partial class WebForm : Form
- {
- private Script script;
- private Timer timer = new Timer();
- private string tickPath;
- private string lastHTML = "";
- public WebForm(string startPath, string tickPath)
- {
- InitializeComponent();
- var settings = new CefSettings();
- Cef.Initialize(settings);
- Cef.EnableHighDPISupport();
- // So, this project is meant to be relatively simple.
- // Step 1, setup a web browser in a panel, something fast.
- // Step 2, start up a lua environment
- // Step 3, create a 'screen' object in lua that has an 'html' field
- // Step 4, load a start lua file and run it
- // Step 5, load a tick lua file and run it at specified tickrate
- // Step 6, after each tick, get our lua environment to return the contents of screen.html
- // Step 7, if the contents aren't the same as what's in the browser, change the browser contents with browser.DocumentText
- // So, I'd like to support more than just, a single object named screen
- // I have nmare's Element API.
- // This form should be the main form, but not the one for screens. It just lets you select things, reload, etc
- // When the program starts, we open a separate ScreenForm for each screen
- // Which means we need a way for the user to define what elements should exist
- // I guess that's init.lua
- this.Size = new Size(1024, 612);
- script = new Script();
- script.Options.DebugPrint = s => Console.WriteLine(s);
- // Find all of the DU lua files that we're stealing from nmare
- //var files = Directory.GetFiles("duElementAPI");
- //foreach (var file in files)
- //{
- // loadLua(file);
- //}
- loadLua("init.lua"); // If we need to init things
- // Load the 'start' funcs
- loadLua(startPath);
- this.tickPath = tickPath;
- // And then setup a timer that runs at, let's say 60 fps
- // And it needs to run on the same thread as the form
- timer.Tick += Timer_Tick;
- timer.Interval = 1000 / 60;
- timer.Start();
- //browser.Dock = DockStyle.Fill;
- browser.Size = new Size(1024, 613);
- this.AutoSize = true;
- this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
- var globalHook = Hook.GlobalEvents();
- globalHook.MouseDownExt += GlobalHook_MouseDownExt;
- globalHook.MouseUpExt += GlobalHook_MouseUpExt;
- globalHook.MouseMoveExt += GlobalHook_MouseMoveExt;
- this.FormClosing += WebForm_FormClosing;
- }
- private void WebForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- Cef.Shutdown(); // Avoid leaks
- }
- private void GlobalHook_MouseMoveExt(object sender, MouseEventExtArgs e)
- {
- var mousePos = browser.PointToClient(Cursor.Position);
- mousePos.X = Clamp(mousePos.X, 0, 1024);
- mousePos.Y = Clamp(mousePos.Y, 0, 612);
- string lua = $"screen.x = {mousePos.X} screen.y = {mousePos.Y}";
- script.DoString(lua);
- }
- private void GlobalHook_MouseUpExt(object sender, MouseEventExtArgs e)
- {
- script.DoString("screen.mouseState = 0");
- }
- private void GlobalHook_MouseDownExt(object sender, MouseEventExtArgs e)
- {
- script.DoString("screen.mouseState = 1");
- }
- private int Clamp(int a, int min, int max)
- {
- if (a.CompareTo(min) < 0) return min;
- else if (a.CompareTo(max) > 0) return max;
- else return a;
- }
- private void Timer_Tick(object sender, EventArgs e)
- {
- loadLua(tickPath);
- var htmlContent = script.Call(script.Globals["_getHtmlContent"]).CastToString();
- if (htmlContent != lastHTML)
- {
- htmlContent = "<body style=\"overflow: hidden; padding:0px; margin:0px;\">" + htmlContent + "</body>";
- browser.LoadHtml(htmlContent);
- lastHTML = htmlContent;
- }
- }
- private DynValue loadLua(string filepath)
- {
- return script.DoString(File.ReadAllText(filepath));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment