Skip to content

Commit

Permalink
1.0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
paissaheavyindustries committed Aug 22, 2024
1 parent 9b3e7ab commit 4e8130a
Show file tree
Hide file tree
Showing 10 changed files with 682 additions and 42 deletions.
165 changes: 165 additions & 0 deletions Telesto/Form.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using ImGuiNET;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text.Json;
using Telesto.FormElements;

namespace Telesto
{

internal class Form
{

internal Plugin plug = null;
internal Guid Guid { get; set; } = Guid.Empty;
internal bool Finished { get; set; } = false;
internal bool _firstShow = true;

public string Title { get; set; }
public string Id { get; set; }
public string Callback { get; set; }
public List<FormElement> Elements { get; set; } = new List<FormElement>();

public int Width { get; set; } = 500;
public int Height { get; set; } = 500;

public Form()
{
Guid = Guid.NewGuid();
}

internal static Form Deserialize(Dictionary<string, object> d)
{
Form f = new Form();
f.Id = d["id"].ToString();
f.Title = d["title"].ToString();
f.Callback = d["callback"].ToString();
List<object> eles = (List<object>)d["elements"];
foreach (var rele in eles)
{
FormElement fe = null;
Dictionary<string, object> ele = (Dictionary<string, object>)rele;
string type = ele["type"].ToString();
switch (type.ToLower())
{
case "button":
fe = new FormElements.Button();
break;
case "checkbox":
fe = new FormElements.Checkbox();
break;
case "hidden":
fe = new FormElements.Hidden();
break;
case "inputint":
fe = new FormElements.InputInt();
break;
case "inputtext":
fe = new FormElements.InputText();
break;
case "label":
fe = new FormElements.Label();
break;
}
if (fe != null)
{
fe.Owner = f;
fe.Initialize(ele);
f.Elements.Add(fe);
}
}
return f;
}

public void Cancel()
{
Dictionary<string, string> result = new Dictionary<string, string>();
Finished = true;
result["_formid"] = Id;
Plugin.Response r = Plugin.WrapToResponse(result, "FormCancel");
if (Callback != "")
{
plug.QueueSendTelegram(Callback, JsonSerializer.Serialize<Plugin.Response>(r));
}
}

public void Submit()
{
Dictionary<string, string> result = new Dictionary<string, string>();
foreach(FormElement e in Elements)
{
if (e.Reported == false)
{
continue;
}
result[e.Id] = e.Value;
}
Finished = true;
result["_formid"] = Id;
Plugin.Response r = Plugin.WrapToResponse(result, "FormSubmit");
if (Callback != "")
{
plug.QueueSendTelegram(Callback, JsonSerializer.Serialize<Plugin.Response>(r));
}
}

public void Render()
{
if (Finished == true)
{
return;
}
if (Width < 100)
{
Width = 100;
}
if (Height < 100)
{
Height = 100;
}
ImGui.SetNextWindowSize(new Vector2(Width, Height), ImGuiCond.FirstUseEver);
if (_firstShow == true)
{
Vector2 szx = ImGui.GetIO().DisplaySize;
Vector2 pt = new Vector2((szx.X / 2) - (Width / 2), (szx.Y / 2) - (Height / 2));
ImGui.SetNextWindowPos(pt);
_firstShow = false;
}
bool open = true;
if (ImGui.Begin(Title + "##" + Guid.ToString(), ref open, ImGuiWindowFlags.NoCollapse) == false)
{
ImGui.End();
return;
}
if (open == false)
{
Finished = true;
ImGui.End();
Cancel();
return;
}
Plugin.KeepWindowInSight();
Vector2 szy = ImGui.GetWindowSize();
if (szy.X < 100 || szy.Y < 100)
{
if (szy.X < 100)
{
szy.X = 100;
}
if (szy.Y < 100)
{
szy.Y = 100;
}
ImGui.SetWindowSize(szy);
}
foreach (FormElement e in Elements)
{
e.Render();
}
ImGui.End();
}

}

}
68 changes: 68 additions & 0 deletions Telesto/FormElements/Button.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using ImGuiNET;
using System;
using System.Collections.Generic;
using static Telesto.Plugin;

namespace Telesto.FormElements
{

internal class Button : FormElement
{

private bool _Reported { get; set; } = false;
internal override bool Reported => _Reported;
public override string Value { get; set; }

public string Text { get; set; }
public string Payload { get; set; }

public enum ActionEnum
{
None,
Payload,
Submit,
Cancel,
}

public ActionEnum Action { get; set; } = ActionEnum.None;

internal override void Initialize(Dictionary<string, object> d)
{
base.Initialize(d);
Text = (d.ContainsKey("text") == true) ? d["text"].ToString() : "";
Payload = (d.ContainsKey("payload") == true) ? d["payload"].ToString() : "";
if (Enum.TryParse(typeof(ActionEnum), (d.ContainsKey("action") == true) ? d["action"].ToString() : "", out object temp))
{
Action = (ActionEnum)temp;
}
}

public override void Render()
{
if (ImGui.Button(Text + "##" + Guid.ToString()))
{
switch (Action)
{
case ActionEnum.None:
break;
case ActionEnum.Payload:
using (PendingRequest pr = new PendingRequest())
{
pr.Request = Payload;
Owner.plug.ProcessRequest(pr);
}
break;
case ActionEnum.Submit:
_Reported = true;
Owner.Submit();
break;
case ActionEnum.Cancel:
Owner.Cancel();
break;
}
}
}

}

}
47 changes: 47 additions & 0 deletions Telesto/FormElements/Checkbox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using ImGuiNET;
using System.Collections.Generic;
using System;

namespace Telesto.FormElements
{

internal class Checkbox : FormElement
{

internal override bool Reported => true;
private bool _Value { get; set; } = false;

public override string Value
{
get
{
return _Value.ToString();
}
set
{
if (bool.TryParse(value, out bool temp))
{
_Value = temp;
}
}
}
public string Text { get; set; }

internal override void Initialize(Dictionary<string, object> d)
{
base.Initialize(d);
Text = (d.ContainsKey("text") == true) ? d["text"].ToString() : "";
}

public override void Render()
{
bool val = _Value;
if (ImGui.Checkbox(Text + "##" + Guid.ToString(), ref val) == true)
{
_Value = val;
}
}

}

}
35 changes: 35 additions & 0 deletions Telesto/FormElements/FormElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;

namespace Telesto.FormElements
{

internal abstract class FormElement
{

internal Form Owner { get; set; }

internal Guid Guid { get; set; } = Guid.Empty;
public string Id { get; set; }
internal abstract bool Reported { get; }
public abstract string Value { get; set; }

public FormElement()
{
Guid = Guid.NewGuid();
}

internal virtual void Initialize(Dictionary<string, object> d)
{
Id = d["id"].ToString();
if (d.ContainsKey("value") == true)
{
Value = d["value"].ToString();
}
}

public abstract void Render();

}

}
16 changes: 16 additions & 0 deletions Telesto/FormElements/Hidden.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Telesto.FormElements
{

internal class Hidden : FormElement
{

internal override bool Reported => true;
public override string Value { get; set; }

public override void Render()
{
}

}

}
Loading

0 comments on commit 4e8130a

Please sign in to comment.