PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/NekoKun.Editor/RPGMaker/EventCommand/EventCommandListbox.cs

https://bitbucket.org/nekokun/nekokun
C# | 181 lines | 158 code | 23 blank | 0 comment | 26 complexity | 42f5352318be585a3b82a77549cf21fd MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using NekoKun.Serialization.RubyMarshal;
  5. namespace NekoKun.RPGMaker
  6. {
  7. public class EventCommandListbox : AbstractObjectEditor
  8. {
  9. protected EventCommandListboxInternal list;
  10. public EventCommandListbox(Dictionary<string, object> Params)
  11. : base(Params)
  12. {
  13. this.list = new EventCommandListboxInternal(this);
  14. this.list.source = ProjectManager.Components[Params["Source"] as string] as EventCommandProvider;
  15. }
  16. protected override void InitControl()
  17. {
  18. this.list.obj = new List<object>((this.selectedItem as RubyArray).ToArray());
  19. this.list.Load();
  20. }
  21. public override System.Windows.Forms.Control Control
  22. {
  23. get { return list; }
  24. }
  25. public override void Commit()
  26. {
  27. this.selectedItem = new RubyArray(this.list.obj);
  28. }
  29. protected class EventCommandListboxInternal : UI.LynnListbox
  30. {
  31. public EventCommandListbox parent;
  32. public EventCommandProvider source;
  33. public List<object> obj;
  34. protected string strCommand = "?";
  35. protected string strIndent = "·";
  36. protected string strUnknown = "??????";
  37. public EventCommandListboxInternal(EventCommandListbox parent)
  38. {
  39. this.parent = parent;
  40. this.SelectedItem = new List<object>();
  41. this.ContextMenuStrip = new EditContextMenuStrip(this);
  42. this.ContextMenuStrip.Items.Add(new System.Windows.Forms.ToolStripSeparator());
  43. this.ContextMenuStrip.Items.Add(new System.Windows.Forms.ToolStripMenuItem("????? UBB(&B)", null, delegate
  44. {
  45. System.Windows.Forms.Clipboard.Clear();
  46. System.Windows.Forms.Clipboard.SetText(ExportUBB(), System.Windows.Forms.TextDataFormat.UnicodeText);
  47. }));
  48. }
  49. protected override void DrawText(int id, string str, System.Drawing.Font font, System.Drawing.Brush fc, System.Drawing.Rectangle bounds, System.Drawing.StringFormat sf, System.Drawing.Graphics g, bool selected)
  50. {
  51. float fix = g.MeasureString("?", font).Width * 2 - g.MeasureString("??", font).Width;
  52. int indentw = (int)(g.MeasureString(strCommand, font).Width - fix);
  53. int indent = GetIndent(id);
  54. int x, height, y;
  55. x = bounds.X; y = bounds.Y;
  56. height = bounds.Height;
  57. EventCommand cmd = GetCodeCommand(id);
  58. x += indent * indentw + indentw;
  59. if (cmd != null && cmd.IsGenerated)
  60. {
  61. int iw = (int)(g.MeasureString(strIndent, font).Width - fix);
  62. g.DrawString(strIndent, font, fc, new System.Drawing.Rectangle(x - iw, y, bounds.Right - x + iw, height));
  63. }
  64. else
  65. {
  66. g.DrawString(strCommand, font, fc, new System.Drawing.Rectangle(x - indentw, y, bounds.Right - x + indentw, height));
  67. }
  68. if (GetCode(id) == "0")
  69. return;
  70. if (cmd != null && !selected)
  71. fc = new System.Drawing.SolidBrush(cmd.Group.ForeColor);
  72. string drawing;
  73. if (cmd == null)
  74. drawing = strUnknown;
  75. else
  76. {
  77. drawing = cmd.FormatParams(this.Items[id] as RubyObject);
  78. }
  79. string draw;
  80. while (drawing.Length > 0)
  81. {
  82. int pos = drawing.IndexOf("{hide}");
  83. if (pos > 0)
  84. {
  85. draw = drawing.Substring(0, pos);
  86. drawing = drawing.Substring(pos);
  87. g.DrawString(draw, font, fc, new System.Drawing.Rectangle(x, y, bounds.Right - x, height), sf);
  88. x += (int)(g.MeasureString(draw, font).Width - fix);
  89. }
  90. else if (pos == 0)
  91. {
  92. pos = drawing.IndexOf("{/hide}");
  93. draw = drawing.Substring(6, pos - 6);
  94. drawing = drawing.Substring(pos + 7);
  95. x += (int)(g.MeasureString(draw, font).Width - fix);
  96. }
  97. else
  98. {
  99. g.DrawString(drawing, font, fc, new System.Drawing.Rectangle(x, y, bounds.Right - x, height), sf);
  100. drawing = "";
  101. }
  102. }
  103. }
  104. protected string GetCode(int id)
  105. {
  106. return (this.Items[id] as RubyObject).InstanceVariable["@code"].ToString();
  107. }
  108. protected EventCommand GetCodeCommand(int id)
  109. {
  110. string code = GetCode(id);
  111. if (this.source.Commands.ContainsKey(code))
  112. {
  113. return this.source.Commands[code];
  114. }
  115. else
  116. {
  117. return null;
  118. }
  119. }
  120. protected int GetIndent(int id)
  121. {
  122. object o = (this.Items[id] as RubyObject).InstanceVariable["@indent"];
  123. if (o == null)
  124. return 0;
  125. else
  126. return (int)o;
  127. }
  128. public void Load()
  129. {
  130. this.Items.Clear();
  131. this.Items.AddRange(this.obj.ToArray());
  132. }
  133. public string ExportUBB()
  134. {
  135. StringBuilder sb = new StringBuilder();
  136. for (int id = 0; id < this.Items.Count; id++)
  137. {
  138. EventCommand cmd = GetCodeCommand(id);
  139. int indent = GetIndent(id);
  140. sb.Append("[font=??]");
  141. if (indent > 0) sb.Append(new String(' ', indent * 2));
  142. sb.Append((cmd != null && cmd.IsGenerated) ? "·" : "?");
  143. sb.Append("[/font]");
  144. if (GetCode(id) != "0")
  145. {
  146. System.Drawing.Color color = cmd == null ? System.Drawing.Color.Black : cmd.Group.ForeColor;
  147. sb.Append(String.Format("[color=#{0:x2}{1:x2}{2:x2}]", color.R, color.G, color.B));
  148. string drawing = cmd == null ? strUnknown : cmd.FormatParams(this.Items[id] as RubyObject);
  149. sb.Append(drawing.Replace("{hide}", "[color=white]").Replace("{/hide}", "[/color]"));
  150. sb.Append("[/color]");
  151. }
  152. sb.AppendLine();
  153. }
  154. return sb.ToString();
  155. }
  156. }
  157. }
  158. }