Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Ink / InkLibs / InkCompiler / ParsedHierarchy / ListDefinition.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Ink.Parsed
  5. {
  6. public class ListDefinition : Parsed.Object
  7. {
  8. public Identifier identifier;
  9. public List<ListElementDefinition> itemDefinitions;
  10. public VariableAssignment variableAssignment;
  11. public Runtime.ListDefinition runtimeListDefinition {
  12. get {
  13. var allItems = new Dictionary<string, int> ();
  14. foreach (var e in itemDefinitions) {
  15. if( !allItems.ContainsKey(e.name) )
  16. allItems.Add (e.name, e.seriesValue);
  17. else
  18. Error("List '"+identifier+"' contains dupicate items called '"+e.name+"'");
  19. }
  20. return new Runtime.ListDefinition (identifier?.name, allItems);
  21. }
  22. }
  23. public ListElementDefinition ItemNamed (string itemName)
  24. {
  25. if (_elementsByName == null) {
  26. _elementsByName = new Dictionary<string, ListElementDefinition> ();
  27. foreach (var el in itemDefinitions) {
  28. _elementsByName [el.name] = el;
  29. }
  30. }
  31. ListElementDefinition foundElement;
  32. if (_elementsByName.TryGetValue (itemName, out foundElement))
  33. return foundElement;
  34. return null;
  35. }
  36. public ListDefinition (List<ListElementDefinition> elements)
  37. {
  38. this.itemDefinitions = elements;
  39. int currentValue = 1;
  40. foreach (var e in this.itemDefinitions) {
  41. if (e.explicitValue != null)
  42. currentValue = e.explicitValue.Value;
  43. e.seriesValue = currentValue;
  44. currentValue++;
  45. }
  46. AddContent (elements);
  47. }
  48. public override Runtime.Object GenerateRuntimeObject ()
  49. {
  50. var initialValues = new Runtime.InkList ();
  51. foreach (var itemDef in itemDefinitions) {
  52. if (itemDef.inInitialList) {
  53. var item = new Runtime.InkListItem (this.identifier?.name, itemDef.name);
  54. initialValues [item] = itemDef.seriesValue;
  55. }
  56. }
  57. // Set origin name, so
  58. initialValues.SetInitialOriginName (identifier?.name);
  59. return new Runtime.ListValue (initialValues);
  60. }
  61. public override void ResolveReferences (Story context)
  62. {
  63. base.ResolveReferences (context);
  64. context.CheckForNamingCollisions (this, identifier, Story.SymbolType.List);
  65. }
  66. public override string typeName {
  67. get {
  68. return "List definition";
  69. }
  70. }
  71. Dictionary<string, ListElementDefinition> _elementsByName;
  72. }
  73. public class ListElementDefinition : Parsed.Object
  74. {
  75. public string name
  76. {
  77. get { return identifier?.name; }
  78. }
  79. public Identifier identifier;
  80. public int? explicitValue;
  81. public int seriesValue;
  82. public bool inInitialList;
  83. public string fullName {
  84. get {
  85. var parentList = parent as ListDefinition;
  86. if (parentList == null)
  87. throw new System.Exception ("Can't get full name without a parent list");
  88. return parentList.identifier + "." + name;
  89. }
  90. }
  91. public ListElementDefinition (Identifier identifier, bool inInitialList, int? explicitValue = null)
  92. {
  93. this.identifier = identifier;
  94. this.inInitialList = inInitialList;
  95. this.explicitValue = explicitValue;
  96. }
  97. public override Runtime.Object GenerateRuntimeObject ()
  98. {
  99. throw new System.NotImplementedException ();
  100. }
  101. public override void ResolveReferences (Story context)
  102. {
  103. base.ResolveReferences (context);
  104. context.CheckForNamingCollisions (this, identifier, Story.SymbolType.ListItem);
  105. }
  106. public override string typeName {
  107. get {
  108. return "List element";
  109. }
  110. }
  111. }
  112. }