ScriptEditorControl.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using InABox.Core;
  8. namespace InABox.DynamicGrid
  9. {
  10. public class ScriptEditorControl : DynamicEditorControl<string, ScriptEditor>
  11. {
  12. static ScriptEditorControl()
  13. {
  14. //DynamicEditorControlFactory.Register<ScriptEditorControl, ScriptEditor>();
  15. }
  16. private Button Editor;
  17. private string script = "";
  18. public ScriptEditorControl()
  19. {
  20. SyntaxLanguage = SyntaxLanguage.CSharp;
  21. }
  22. public SyntaxLanguage SyntaxLanguage { get; set; }
  23. public event OnScriptEditorClickedEvent OnEditorClicked;
  24. public override void Configure()
  25. {
  26. SyntaxLanguage = EditorDefinition.SyntaxLanguage;
  27. }
  28. protected override FrameworkElement CreateEditor()
  29. {
  30. Editor = new Button
  31. {
  32. Content = "Edit..",
  33. HorizontalAlignment = HorizontalAlignment.Left,
  34. VerticalAlignment = VerticalAlignment.Stretch,
  35. VerticalContentAlignment = VerticalAlignment.Center,
  36. Width = 50
  37. };
  38. Editor.Click += Editor_Click;
  39. return Editor;
  40. }
  41. private void Editor_Click(object sender, RoutedEventArgs e)
  42. {
  43. if (EditorDefinition.GetType() == typeof(ScriptEditor))
  44. {
  45. CheckTemplateEditor();
  46. }
  47. else
  48. ShowEditor();
  49. }
  50. private void CheckTemplateEditor()
  51. {
  52. var edt = EditorDefinition as ScriptEditor;
  53. if (edt.Type == ScriptEditorType.TemplateEditor)
  54. edt.InvokeEvent();
  55. else
  56. ShowEditor();
  57. }
  58. private void ShowEditor()
  59. {
  60. var editor = new ScriptEditorWindow(script, SyntaxLanguage);
  61. if (editor.ShowDialog() == true)
  62. {
  63. script = editor.Script;
  64. CheckChanged();
  65. }
  66. }
  67. public override int DesiredHeight()
  68. {
  69. return 25;
  70. }
  71. public override int DesiredWidth()
  72. {
  73. return 100;
  74. }
  75. protected override string RetrieveValue()
  76. {
  77. return script;
  78. }
  79. protected override void UpdateValue(string value)
  80. {
  81. script = value.NotWhiteSpaceOr("");
  82. }
  83. public override void SetFocus()
  84. {
  85. Editor.Focus();
  86. }
  87. public override void SetColor(Color color)
  88. {
  89. //Editor.Background = new SolidColorBrush(color);
  90. }
  91. }
  92. }