Dimencia

Sandbox Mock

Apr 1st, 2021
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.06 KB | None | 0 0
  1. using CefSharp;
  2. using CefSharp.WinForms;
  3.  
  4. using MoonSharp.Interpreter;
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17. using System.Windows.Input;
  18.  
  19. using Timer = System.Windows.Forms.Timer;
  20. using Cursor = System.Windows.Forms.Cursor;
  21. using Gma.System.MouseKeyHook;
  22.  
  23. namespace Lua_To_HTML
  24. {
  25.     public partial class WebForm : Form
  26.     {
  27.         private Script script;
  28.         private Timer timer = new Timer();
  29.         private string tickPath;
  30.  
  31.         private string lastHTML = "";
  32.         public WebForm(string startPath, string tickPath)
  33.         {
  34.             InitializeComponent();
  35.             var settings = new CefSettings();
  36.             Cef.Initialize(settings);
  37.             Cef.EnableHighDPISupport();
  38.             // So, this project is meant to be relatively simple.
  39.             // Step 1, setup a web browser in a panel, something fast.  
  40.             // Step 2, start up a lua environment
  41.             // Step 3, create a 'screen' object in lua that has an 'html' field
  42.             // Step 4, load a start lua file and run it
  43.             // Step 5, load a tick lua file and run it at specified tickrate
  44.             // Step 6, after each tick, get our lua environment to return the contents of screen.html
  45.             // Step 7, if the contents aren't the same as what's in the browser, change the browser contents with browser.DocumentText
  46.  
  47.             // So, I'd like to support more than just, a single object named screen
  48.             // I have nmare's Element API.  
  49.             // This form should be the main form, but not the one for screens. It just lets you select things, reload, etc
  50.             // When the program starts, we open a separate ScreenForm for each screen
  51.             // Which means we need a way for the user to define what elements should exist
  52.  
  53.             // I guess that's init.lua
  54.  
  55.             this.Size = new Size(1024, 612);
  56.  
  57.             script = new Script();
  58.             script.Options.DebugPrint = s => Console.WriteLine(s);
  59.  
  60.  
  61.             // Find all of the DU lua files that we're stealing from nmare
  62.             //var files = Directory.GetFiles("duElementAPI");
  63.             //foreach (var file in files)
  64.             //{
  65.             //    loadLua(file);
  66.             //}
  67.  
  68.  
  69.             loadLua("init.lua"); // If we need to init things
  70.  
  71.             // Load the 'start' funcs
  72.             loadLua(startPath);
  73.  
  74.             this.tickPath = tickPath;
  75.  
  76.             // And then setup a timer that runs at, let's say 60 fps
  77.             // And it needs to run on the same thread as the form
  78.             timer.Tick += Timer_Tick;
  79.             timer.Interval = 1000 / 60;
  80.             timer.Start();
  81.  
  82.             //browser.Dock = DockStyle.Fill;
  83.             browser.Size = new Size(1024, 613);
  84.            
  85.  
  86.             this.AutoSize = true;
  87.             this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
  88.  
  89.  
  90.             var globalHook = Hook.GlobalEvents();
  91.             globalHook.MouseDownExt += GlobalHook_MouseDownExt;
  92.             globalHook.MouseUpExt += GlobalHook_MouseUpExt;
  93.             globalHook.MouseMoveExt += GlobalHook_MouseMoveExt;
  94.  
  95.             this.FormClosing += WebForm_FormClosing;
  96.         }
  97.  
  98.         private void WebForm_FormClosing(object sender, FormClosingEventArgs e)
  99.         {
  100.             Cef.Shutdown(); // Avoid leaks
  101.         }
  102.  
  103.         private void GlobalHook_MouseMoveExt(object sender, MouseEventExtArgs e)
  104.         {
  105.             var mousePos = browser.PointToClient(Cursor.Position);
  106.             mousePos.X = Clamp(mousePos.X, 0, 1024);
  107.             mousePos.Y = Clamp(mousePos.Y, 0, 612);
  108.             string lua = $"screen.x = {mousePos.X} screen.y = {mousePos.Y}";
  109.  
  110.             script.DoString(lua);
  111.         }
  112.  
  113.         private void GlobalHook_MouseUpExt(object sender, MouseEventExtArgs e)
  114.         {
  115.             script.DoString("screen.mouseState = 0");
  116.         }
  117.  
  118.         private void GlobalHook_MouseDownExt(object sender, MouseEventExtArgs e)
  119.         {
  120.             script.DoString("screen.mouseState = 1");
  121.         }
  122.  
  123.  
  124.         private int Clamp(int a, int min, int max)
  125.         {
  126.             if (a.CompareTo(min) < 0) return min;
  127.             else if (a.CompareTo(max) > 0) return max;
  128.             else return a;
  129.         }
  130.  
  131.         private void Timer_Tick(object sender, EventArgs e)
  132.         {
  133.             loadLua(tickPath);
  134.             var htmlContent = script.Call(script.Globals["_getHtmlContent"]).CastToString();
  135.             if (htmlContent != lastHTML)
  136.             {
  137.                 htmlContent = "<body style=\"overflow: hidden; padding:0px; margin:0px;\">" + htmlContent + "</body>";
  138.                 browser.LoadHtml(htmlContent);
  139.                
  140.                 lastHTML = htmlContent;
  141.             }
  142.         }
  143.  
  144.         private DynValue loadLua(string filepath)
  145.         {
  146.             return script.DoString(File.ReadAllText(filepath));
  147.         }
  148.  
  149.  
  150.  
  151.     }
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment