Skip to content

Commit b18d336

Browse files
author
Christian Ohle
committed
mini script editor
1 parent a61c7f6 commit b18d336

File tree

5 files changed

+424
-296
lines changed

5 files changed

+424
-296
lines changed

bin/csg3mf.exe

6 KB
Binary file not shown.

csg3mf/CDXInterface.3mf.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ void add(INode group, XElement dest)
136136
{
137137
obj.SetAttributeValue("script", Convert.ToBase64String(Encoding.UTF8.GetBytes(xn.code)));
138138
var props = xn.getprops(); if (props != null) obj.SetAttributeValue("props", Convert.ToBase64String(Encoding.UTF8.GetBytes(props.ToString())));
139+
//obj.SetAttributeValue("script", xn.code);
140+
//var props = xn.getprops(); if (props != null) obj.SetAttributeValue("props", props.ToString());
139141
}
140142
};
141143
if (path == null) return doc;//doc.Save("C:\\Users\\cohle\\Desktop\\test2.xml");

csg3mf/Program.cs

Lines changed: 5 additions & 237 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.IO;
1111
using System.Linq;
1212
using System.Reflection;
13+
using System.Runtime.CompilerServices;
1314
using System.Runtime.InteropServices;
1415
using System.Windows.Forms;
1516
using System.Xml;
@@ -359,7 +360,7 @@ int OnScript(object test)
359360
var a = view.view.Scene.Selection();
360361
if (a.Take(2).Count() != 1) return 0;
361362
if (test != null) return 1;
362-
ShowView(typeof(ScriptView2), XNode.From(a.First()), DockStyle.Left);
363+
ShowView(typeof(ScriptEditor), XNode.From(a.First()), DockStyle.Left);
363364
return 1;
364365
}
365366

@@ -550,6 +551,7 @@ bool IExchange.Exchange<T>(string name, ref T value, string fmt)
550551
case 4:
551552
{
552553
if (fmt != null && fmt.Contains("r;")) return false;
554+
var tc = TypeDescriptor.GetConverter(typeof(T)); if (!tc.CanConvertFrom(typeof(string))) return false;
553555
var s = getval((XElement)param, name); if (s == null) return false;
554556
var p = TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(null, CultureInfo.InvariantCulture, s);
555557
if (p != null) value = (T)p; return false;
@@ -568,9 +570,9 @@ class PD : PropertyDescriptor
568570
public PD(int id, string name, Type t, string c, string fmt) : base(name, null) { this.id = id; type = t; category = c; this.fmt = fmt; }
569571
int id; Type type; string category, fmt;
570572
public override string Category => category ?? base.Category;
571-
public override string Description => fmt != null ? fmt.Substring(fmt.LastIndexOf(';')+1) : base.Description;
573+
public override string Description => fmt != null ? fmt.Substring(fmt.LastIndexOf(';') + 1) : base.Description;
572574
public override Type ComponentType => typeof(XNode);
573-
public override bool IsReadOnly => fmt != null && fmt.Contains("r;");// false;
575+
public override bool IsReadOnly => fmt != null && fmt.Contains("r;");
574576
public override Type PropertyType => type;
575577
public override bool CanResetValue(object component) => false;
576578
public override void ResetValue(object component) { }
@@ -655,238 +657,4 @@ protected override void OnPropertyValueChanged(PropertyValueChangedEventArgs e)
655657
}
656658
}
657659

658-
class ScriptView2 : UserControl, UIForm.ICommandTarget
659-
{
660-
public override string Text { get => $"Script {((XNode)Tag).Name}"; set { } }
661-
MyCodeEditor edit; UIForm.ToolStrip tb;
662-
protected override void OnPaintBackground(PaintEventArgs e) { }
663-
protected override void OnLoad(EventArgs e)
664-
{
665-
tb = new UIForm.ToolStrip() { Margin = new Padding(15), ImageScalingSize = new Size(24, 24), GripStyle = ToolStripGripStyle.Hidden };
666-
tb.Items.Add(new UIForm.Button(5011, "Run", Resources.run) { Tag = this });
667-
//tb.Items.Add(new ToolStripSeparator());
668-
//tb.Items.Add(new Button(5010, "Run Debug", Resources.rund) { Tag = edit });
669-
//tb.Items.Add(new Button(5016, "Step", Resources.stepover) { Tag = edit });
670-
//tb.Items.Add(new Button(5015, "Step Into", Resources.stepin) { Tag = edit });
671-
//tb.Items.Add(new Button(5017, "Step Out", Resources.stepout) { Tag = edit });
672-
//tb.Items.Add(new ToolStripSeparator());
673-
tb.Items.Add(new UIForm.Button(5013, "Stop", Resources.stop) { Tag = this });
674-
edit = new MyCodeEditor { Dock = DockStyle.Fill };
675-
edit.EditText = ((XNode)Tag).code ?? string.Empty;
676-
Visible = false;
677-
Controls.Add(edit);
678-
Controls.Add(tb);
679-
Visible = true;
680-
}
681-
int UIForm.ICommandTarget.OnCommand(int id, object test)
682-
{
683-
switch (id)
684-
{
685-
case 5011: return OnRun(test);
686-
}
687-
return edit.OnCommand(id, test);
688-
}
689-
int OnRun(object test)
690-
{
691-
if (test != null) return 1;
692-
try
693-
{
694-
//var expr = Script.Compile(typeof(XNode), edit.EditText);
695-
//var ctor = expr.Compile(); //data = ctor(this);
696-
var node = (XNode)Tag;
697-
var code = edit.EditText; if (node.Code == code) return 1;
698-
var view = ((MainFrame)FindForm()).view;
699-
view.Execute(() => { var t = node.Code; node.Code = code; code = t; });
700-
node.Code = edit.EditText;
701-
}
702-
catch (Exception e)
703-
{
704-
var p = Script.GetErrorPos(); edit.Select(p.i, p.i + p.n);
705-
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
706-
}
707-
return 1;
708-
}
709-
710-
class MyCodeEditor : CodeEditor
711-
{
712-
protected override void UpdateSyntaxColors()
713-
{
714-
if (colormap == null) colormap = new int[] { 0, 0x00007000, 0x00000088, 0x00ff0000, 0x11463a96, 0x2200ddff, 0x00af912b, 0x000000ff };
715-
int n = text.Length; Array.Resize(ref charcolor, n + 1);
716-
for (int i = 0; i < n; i++)
717-
{
718-
var c = text[i];
719-
if (c <= ' ') { charcolor[i] = 0; continue; }
720-
if (c == '/' && i + 1 < n)
721-
{
722-
if (text[i + 1] == '/') { for (; i < n && text[i] != 10; i++) charcolor[i] = 1; continue; }
723-
if (text[i + 1] == '*') { var t = text.IndexOf("*/", i + 2); t = t >= 0 ? t + 2 : n; for (; i < t; i++) charcolor[i] = 1; i = t - 1; continue; }
724-
}
725-
if (c == '"' || c == '\'')
726-
{
727-
var x = i; for (++i; i < n; i++) { var t = text[i]; if (t == '\\') { i++; continue; } if (t == c) break; }
728-
for (; x <= i; x++) charcolor[x] = 2; continue;
729-
}
730-
if (c == '<') //inline xml
731-
{
732-
int k = i - 1; for (; k >= 0 && (text[k] <= ' ' || charcolor[k] == 1); k--) ;
733-
if (k < 0 || "=+(,;}?".Contains(text[k]))
734-
{
735-
for (int z = 0, g, d = 0; i < n; i++)
736-
{
737-
if ((c = text[i]) == '<')
738-
{
739-
for (k = i + 1; k < n && text[k] != '>'; k++) ; if (k == n) break;
740-
charcolor[i++] = charcolor[g = k--] = 3;
741-
if (text[i] == '!')
742-
{
743-
for (k = g; k < n && (text[k] != '>' || text[k - 1] != '-' || text[k - 2] != '-'); k++) ; charcolor[g = k--] = 3;
744-
charcolor[i++] = 3;
745-
for (int f = 0; f < 2; f++) { if (text[i] == '-') charcolor[i++] = 3; if (text[k] == '-') charcolor[k--] = 3; }
746-
for (; i <= k; i++) charcolor[i] = 1;
747-
i = g; continue;
748-
}
749-
if (text[i] == '/') { z--; charcolor[i++] = 3; }
750-
else if (text[k] != '/') z++; else charcolor[k--] = 3;
751-
for (; i <= k && text[i] > ' '; i++) charcolor[i] = 2;
752-
for (; i <= k; i++)
753-
{
754-
if ((c = text[i]) == '"')
755-
{
756-
charcolor[i++] = 0; d = 3;
757-
for (; i < k && text[i] != '"'; i++)
758-
{
759-
if (text[i] == '&') d = 7;
760-
charcolor[i] = (byte)d; if (d == 7 && (text[i] == ';' || text[i] <= ' ')) d = 3;
761-
}
762-
if (i <= k) charcolor[i] = 0; continue;
763-
}
764-
charcolor[i] = (byte)(c == '=' ? 0 : 7);
765-
}
766-
i = g; d = 0; continue;
767-
}
768-
if (z == 0 && c > ' ') { i--; break; }
769-
if (c == '&') d = 7; charcolor[i] = (byte)d; if (d == 7 && (c == ';' || c <= ' ')) d = 0;
770-
}
771-
continue;
772-
}
773-
}
774-
775-
var l = i; for (; l < n && IsLetter(text[l]); l++) ;
776-
var r = l - i; if (r == 0) { charcolor[i] = 0; continue; }
777-
byte color = 0;
778-
if (r > 1 && !(l < text.Length && char.IsDigit(text[l])))
779-
for (int t = 0; t < Compiler.keywords.Length; t++)
780-
{
781-
var kw = Compiler.keywords[t];
782-
if (kw.Length == r && kw[0] == text[i] && string.Compare(text, i, kw, 0, kw.Length, true) == 0) { color = 3; break; }
783-
}
784-
for (; i < l; i++) charcolor[i] = color; i--;
785-
}
786-
//if (typemap == null) return;
787-
//
788-
//for (int i = 0; i < typemap.Length; i++)
789-
//{
790-
// var p = typemap[i]; if ((p.v & 0xf) == 0 && charcolor[p.i] != 3) color(p.i, p.n, 6);
791-
// if (overid != 0 && (overid & 0x0f) < 8 && overid == (p.v & ~0xf0)) color2(p.i, p.n, 0x80);
792-
//}
793-
//for (int i = 0; i < spots.Length; i++) { var p = spots[i]; if ((p.v & 1) != 0) color(p.i, p.n, 4); }
794-
//for (int i = 0; i < spots.Length; i++) { var p = spots[i]; if ((p.v & 2) != 0) color(p.i, p.n, 5); }
795-
//for (int i = 0; i < errors.Length; i++) { var p = errors[i]; color2(p.i, p.n, (errors[i].v & 4) != 0 ? 0x70 : 0x10); }
796-
}
797-
protected override int GetRange(int x)
798-
{
799-
for (int i = x, n = text.Length; i < n; i++)
800-
{
801-
if (text[i] == 10 || text[i] == ';') break;
802-
if (text[i] == '/' && i + 1 < n)
803-
{
804-
if (text[i + 1] == '*') { for (i += 2; i < n - 1; i++) if (text[i] == '*' && text[i + 1] == '/') break; return -i; }
805-
if (text[i + 1] == '/') { for (int t; (t = nextls(i)) < n - 1 && text[t] == '/' && text[t + 1] == '/'; i = t) ; return -i; }
806-
}
807-
if (text[i] == '[') { for (int t; (t = nextls(i)) < n && text[t] == '['; i = t) ; return -i; }
808-
if (IsLetter(text[i]))
809-
{
810-
if (startsw(i, "using")) { for (int t; (t = nextls(i)) < n && startsw(t, "using"); i = t) ; return -i; }
811-
for (; i < n; i++)
812-
{
813-
if (text[i] == ';') break;
814-
if (text[i] == '{')
815-
{
816-
for (int k = 0; i < n; i++)
817-
{
818-
if (text[i] == '{') { k++; continue; }
819-
if (text[i] == '}') { if (--k == 0) return i; }
820-
}
821-
break;
822-
}
823-
}
824-
break;
825-
}
826-
if (text[i] == '<') //inline xml
827-
{
828-
if (i + 1 == n || text[i + 1] == '/') break;
829-
for (int z = 0, k; i < n; i++)
830-
{
831-
if (text[i] == '<')
832-
{
833-
for (k = i + 1; k < n && text[k] != '>'; k++) ; if (k == n) break;
834-
if (text[i + 1] == '!') { i = k; continue; }
835-
if (text[i + 1] == '/') z--; else if (text[k - 1] != '/') z++;
836-
i = k; continue;
837-
}
838-
if (z == 0) return i;
839-
}
840-
}
841-
}
842-
return 0;
843-
}
844-
int nextls(int i)
845-
{
846-
for (; i < text.Length && text[i] != 10; i++) ;
847-
for (; i < text.Length && text[i] <= 32; i++) ;
848-
return i;
849-
}
850-
bool startsw(int i, string s)
851-
{
852-
return i + s.Length <= text.Length && string.CompareOrdinal(s, 0, text, i, s.Length) == 0;
853-
}
854-
855-
int rebuild = 5; (int i, int n) epos; string error;
856-
protected override void Replace(int i, int n, string s)
857-
{
858-
base.Replace(i, n, s);
859-
rebuild = s.Length == 1 && (s[0] == '.' || s[0] == '(') ? 2 : 5;
860-
}
861-
protected override void WndProc(ref Message m)
862-
{
863-
base.WndProc(ref m);
864-
if (m.Msg == 0x0113)//WM_TIMER
865-
{
866-
if (rebuild != 0 && --rebuild == 0) build();
867-
}
868-
}
869-
void build()
870-
{
871-
try { error = null; var expr = Script.Compile(typeof(XNode), EditText); }
872-
catch (Exception e)
873-
{
874-
epos = Script.GetErrorPos();
875-
for (int i = 0; i < epos.n; i++) { charcolor[epos.i + i] |= 0x70; }
876-
Invalidate(); error = e.Message;
877-
}
878-
}
879-
protected override void OnMouseMove(MouseEventArgs e)
880-
{
881-
base.OnMouseMove(e);
882-
var i = PosFromPoint(e.Location);
883-
if (error != null && i >= epos.i && i <= epos.i + epos.n)
884-
{
885-
var s = error; SetToolTip(TextBox(epos.i, epos.n), t => t == 1 ? "Error" : t == 0 ? s : null);
886-
return;
887-
}
888-
EndToolTip();
889-
}
890-
}
891-
}
892660
}

0 commit comments

Comments
 (0)