ScriptEditorControl.cs 2.8 KB

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