AttributeEditor.as 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  1. // Attribute editor
  2. //
  3. // Functions that caller must implement:
  4. // - void SetAttributeEditorID(UIElement@ attrEdit, Array<Serializable@>@ serializables);
  5. // - bool PreEditAttribute(Array<Serializable@>@ serializables, uint index);
  6. // - void PostEditAttribute(Array<Serializable@>@ serializables, uint index, const Array<Variant>& oldValues);
  7. // - Array<Serializable@>@ GetAttributeEditorTargets(UIElement@ attrEdit);
  8. // - String GetVariableName(StringHash hash);
  9. const uint MIN_NODE_ATTRIBUTES = 4;
  10. const uint MAX_NODE_ATTRIBUTES = 8;
  11. const int ATTRNAME_WIDTH = 150;
  12. const int ATTR_HEIGHT = 19;
  13. const StringHash TEXT_CHANGED_EVENT_TYPE("TextChanged");
  14. bool inLoadAttributeEditor = false;
  15. bool inEditAttribute = false;
  16. bool inUpdateBitSelection = false;
  17. bool showNonEditableAttribute = false;
  18. Color normalTextColor(1.0f, 1.0f, 1.0f);
  19. Color modifiedTextColor(1.0f, 0.8f, 0.5f);
  20. Color nonEditableTextColor(0.7f, 0.7f, 0.7f);
  21. String sceneResourcePath = AddTrailingSlash(fileSystem.programDir + "Data");
  22. bool rememberResourcePath = true;
  23. // Exceptions for string attributes that should not be continuously edited
  24. Array<String> noTextChangedAttrs = {"Script File", "Class Name", "Script Object Type", "Script File Name"};
  25. // List of attributes that should be created with a bit selection editor
  26. const Array<String> bitSelectionAttrs = {"Collision Mask", "Collision Layer", "Light Mask", "Zone Mask", "View Mask", "Shadow Mask"};
  27. // Number of editable bits for bit selection editor
  28. const int MAX_BITMASK_BITS = 8;
  29. const int MAX_BITMASK_VALUE = (1 << MAX_BITMASK_BITS) - 1;
  30. Color nonEditableBitSelectorColor(0.5f, 0.5f, 0.5f);
  31. Color editableBitSelectorColor(1.0f, 1.0f, 1.0f);
  32. WeakHandle testAnimState;
  33. bool dragEditAttribute = false;
  34. UIElement@ SetEditable(UIElement@ element, bool editable)
  35. {
  36. if (element is null)
  37. return element;
  38. element.editable = editable;
  39. element.colors[C_TOPLEFT] = editable ? element.colors[C_BOTTOMRIGHT] : nonEditableTextColor;
  40. element.colors[C_BOTTOMLEFT] = element.colors[C_TOPLEFT];
  41. element.colors[C_TOPRIGHT] = element.colors[C_TOPLEFT];
  42. return element;
  43. }
  44. UIElement@ SetValue(LineEdit@ element, const String&in value, bool sameValue)
  45. {
  46. element.text = sameValue ? value : STRIKED_OUT;
  47. element.cursorPosition = 0;
  48. return element;
  49. }
  50. UIElement@ SetValue(CheckBox@ element, bool value, bool sameValue)
  51. {
  52. element.checked = sameValue ? value : false;
  53. return element;
  54. }
  55. UIElement@ SetValue(DropDownList@ element, int value, bool sameValue)
  56. {
  57. element.selection = sameValue ? value : M_MAX_UNSIGNED;
  58. return element;
  59. }
  60. UIElement@ CreateAttributeEditorParentWithSeparatedLabel(ListView@ list, const String&in name, uint index, uint subIndex, bool suppressedSeparatedLabel = false)
  61. {
  62. UIElement@ editorParent = UIElement("Edit" + String(index) + "_" + String(subIndex));
  63. editorParent.vars["Index"] = index;
  64. editorParent.vars["SubIndex"] = subIndex;
  65. editorParent.SetLayout(LM_VERTICAL, 2);
  66. list.AddItem(editorParent);
  67. if (suppressedSeparatedLabel)
  68. {
  69. UIElement@ placeHolder = UIElement(name);
  70. editorParent.AddChild(placeHolder);
  71. }
  72. else
  73. {
  74. Text@ attrNameText = Text();
  75. editorParent.AddChild(attrNameText);
  76. attrNameText.style = "EditorAttributeText";
  77. attrNameText.text = name;
  78. }
  79. return editorParent;
  80. }
  81. UIElement@ CreateAttributeEditorParentAsListChild(ListView@ list, const String&in name, uint index, uint subIndex)
  82. {
  83. UIElement@ editorParent = UIElement("Edit" + String(index) + "_" + String(subIndex));
  84. editorParent.vars["Index"] = index;
  85. editorParent.vars["SubIndex"] = subIndex;
  86. editorParent.SetLayout(LM_HORIZONTAL);
  87. list.AddChild(editorParent);
  88. UIElement@ placeHolder = UIElement(name);
  89. editorParent.AddChild(placeHolder);
  90. return editorParent;
  91. }
  92. UIElement@ CreateAttributeEditorParent(ListView@ list, const String&in name, uint index, uint subIndex)
  93. {
  94. UIElement@ editorParent = UIElement("Edit" + String(index) + "_" + String(subIndex));
  95. editorParent.vars["Index"] = index;
  96. editorParent.vars["SubIndex"] = subIndex;
  97. editorParent.SetLayout(LM_HORIZONTAL);
  98. editorParent.SetFixedHeight(ATTR_HEIGHT);
  99. list.AddItem(editorParent);
  100. Text@ attrNameText = Text();
  101. editorParent.AddChild(attrNameText);
  102. attrNameText.style = "EditorAttributeText";
  103. attrNameText.text = name;
  104. attrNameText.SetFixedWidth(ATTRNAME_WIDTH);
  105. return editorParent;
  106. }
  107. LineEdit@ CreateAttributeLineEdit(UIElement@ parent, Array<Serializable@>@ serializables, uint index, uint subIndex)
  108. {
  109. LineEdit@ attrEdit = LineEdit();
  110. parent.AddChild(attrEdit);
  111. attrEdit.dragDropMode = DD_TARGET;
  112. attrEdit.style = "EditorAttributeEdit";
  113. attrEdit.SetFixedHeight(ATTR_HEIGHT - 2);
  114. attrEdit.vars["Index"] = index;
  115. attrEdit.vars["SubIndex"] = subIndex;
  116. SetAttributeEditorID(attrEdit, serializables);
  117. return attrEdit;
  118. }
  119. LineEdit@ CreateAttributeBitSelector(UIElement@ parent, Array<Serializable@>@ serializables, uint index, uint subIndex)
  120. {
  121. UIElement@ container = UIElement();
  122. parent.AddChild(container);
  123. parent.SetFixedHeight(38);
  124. container.SetFixedWidth(16 * 4 + 4);
  125. for (int i = 0; i < 2; i++)
  126. {
  127. for (int j = 0; j < 4; j++)
  128. {
  129. CheckBox@ bitBox = CheckBox();
  130. bitBox.name = "BitSelect_" + String(i * 4 + j);
  131. container.AddChild(bitBox);
  132. bitBox.position = IntVector2(16 * j, 16 * i);
  133. bitBox.style = "CheckBox";
  134. bitBox.SetFixedHeight(16);
  135. SubscribeToEvent(bitBox,"Toggled", "HandleBitSelectionToggled");
  136. }
  137. }
  138. LineEdit@ attrEdit = CreateAttributeLineEdit(parent, serializables, index, subIndex);
  139. attrEdit.name = "LineEdit";
  140. SubscribeToEvent(attrEdit, "TextChanged", "HandleBitSelectionEdit");
  141. SubscribeToEvent(attrEdit, "TextFinished", "HandleBitSelectionEdit");
  142. return attrEdit;
  143. }
  144. void UpdateBitSelection(UIElement@ parent)
  145. {
  146. int mask = 0;
  147. for (int i = 0; i < MAX_BITMASK_BITS; i++)
  148. {
  149. CheckBox@ bitBox = parent.GetChild("BitSelect_" + String(i), true);
  150. mask = mask | (bitBox.checked ? 1 << i : 0);
  151. }
  152. if (mask == MAX_BITMASK_VALUE)
  153. mask = -1;
  154. inUpdateBitSelection = true;
  155. LineEdit@ attrEdit = parent.parent.GetChild("LineEdit", true);
  156. attrEdit.text = String(mask);
  157. inUpdateBitSelection = false;
  158. }
  159. void SetBitSelection(UIElement@ parent, int value)
  160. {
  161. int mask = value;
  162. bool enabled = true;
  163. if (mask == -1)
  164. mask = MAX_BITMASK_VALUE;
  165. else if (mask > MAX_BITMASK_VALUE)
  166. enabled = false;
  167. for (int i = 0; i < MAX_BITMASK_BITS; i++)
  168. {
  169. CheckBox@ bitBox = parent.GetChild("BitSelect_" + String(i), true);
  170. bitBox.enabled = enabled;
  171. if (!enabled)
  172. bitBox.color = nonEditableBitSelectorColor;
  173. else
  174. bitBox.color = editableBitSelectorColor;
  175. if ((1 << i) & mask != 0)
  176. bitBox.checked = true;
  177. else
  178. bitBox.checked = false;
  179. }
  180. }
  181. void HandleBitSelectionToggled(StringHash eventType, VariantMap& eventData)
  182. {
  183. if (inUpdateBitSelection)
  184. return;
  185. CheckBox@ bitBox = eventData["Element"].GetPtr();
  186. UpdateBitSelection(bitBox.parent);
  187. }
  188. void HandleBitSelectionEdit(StringHash eventType, VariantMap& eventData)
  189. {
  190. if (!inUpdateBitSelection)
  191. {
  192. LineEdit@ attrEdit = eventData["Element"].GetPtr();
  193. inUpdateBitSelection = true;
  194. SetBitSelection(attrEdit.parent, attrEdit.text.ToInt());
  195. inUpdateBitSelection = false;
  196. }
  197. EditAttribute(eventType, eventData);
  198. }
  199. UIElement@ CreateStringAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex)
  200. {
  201. UIElement@ parent = CreateAttributeEditorParent(list, info.name, index, subIndex);
  202. LineEdit@ attrEdit = CreateAttributeLineEdit(parent, serializables, index, subIndex);
  203. attrEdit.dragDropMode = DD_TARGET;
  204. // Do not subscribe to continuous edits of certain attributes (script class names) to prevent unnecessary errors getting printed
  205. if (noTextChangedAttrs.Find(info.name) == -1)
  206. SubscribeToEvent(attrEdit, "TextChanged", "EditAttribute");
  207. SubscribeToEvent(attrEdit, "TextFinished", "EditAttribute");
  208. return parent;
  209. }
  210. UIElement@ CreateBoolAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex)
  211. {
  212. bool isUIElement = cast<UIElement>(serializables[0]) !is null;
  213. UIElement@ parent;
  214. if (info.name == (isUIElement ? "Is Visible" : "Is Enabled"))
  215. parent = CreateAttributeEditorParentAsListChild(list, info.name, index, subIndex);
  216. else
  217. parent = CreateAttributeEditorParent(list, info.name, index, subIndex);
  218. CheckBox@ attrEdit = CheckBox();
  219. parent.AddChild(attrEdit);
  220. attrEdit.style = AUTO_STYLE;
  221. attrEdit.vars["Index"] = index;
  222. attrEdit.vars["SubIndex"] = subIndex;
  223. SetAttributeEditorID(attrEdit, serializables);
  224. SubscribeToEvent(attrEdit, "Toggled", "EditAttribute");
  225. return parent;
  226. }
  227. UIElement@ CreateNumAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex)
  228. {
  229. UIElement@ parent = CreateAttributeEditorParent(list, info.name, index, subIndex);
  230. VariantType type = info.type;
  231. uint numCoords = 1;
  232. if (type == VAR_VECTOR2 || type == VAR_INTVECTOR2)
  233. numCoords = 2;
  234. if (type == VAR_VECTOR3 || type == VAR_INTVECTOR3 || type == VAR_QUATERNION)
  235. numCoords = 3;
  236. else if (type == VAR_VECTOR4 || type == VAR_COLOR || type == VAR_INTRECT || type == VAR_RECT)
  237. numCoords = 4;
  238. for (uint i = 0; i < numCoords; ++i)
  239. {
  240. LineEdit@ attrEdit = CreateAttributeLineEdit(parent, serializables, index, subIndex);
  241. attrEdit.vars["Coordinate"] = i;
  242. CreateDragSlider(attrEdit);
  243. SubscribeToEvent(attrEdit, "TextChanged", "EditAttribute");
  244. SubscribeToEvent(attrEdit, "TextFinished", "EditAttribute");
  245. }
  246. return parent;
  247. }
  248. UIElement@ CreateIntAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex)
  249. {
  250. UIElement@ parent = CreateAttributeEditorParent(list, info.name, index, subIndex);
  251. // Check for masks and layers
  252. if (bitSelectionAttrs.Find(info.name) > -1)
  253. {
  254. LineEdit@ attrEdit = CreateAttributeBitSelector(parent, serializables, index, subIndex);
  255. }
  256. // Check for enums
  257. else if (info.enumNames is null || info.enumNames.empty)
  258. {
  259. // No enums, create a numeric editor
  260. LineEdit@ attrEdit = CreateAttributeLineEdit(parent, serializables, index, subIndex);
  261. CreateDragSlider(attrEdit);
  262. // If the attribute is a counter for things like billboards or animation states, disable apply at each change
  263. if (info.name.Find(" Count", 0, false) == String::NPOS)
  264. SubscribeToEvent(attrEdit, "TextChanged", "EditAttribute");
  265. SubscribeToEvent(attrEdit, "TextFinished", "EditAttribute");
  266. // If the attribute is a node ID, make it a drag/drop target
  267. if (info.name.Contains("NodeID", false) || info.name.Contains("Node ID", false) || (info.mode & AM_NODEID) != 0)
  268. attrEdit.dragDropMode = DD_TARGET;
  269. }
  270. else
  271. {
  272. DropDownList@ attrEdit = DropDownList();
  273. parent.AddChild(attrEdit);
  274. attrEdit.style = AUTO_STYLE;
  275. attrEdit.SetFixedHeight(ATTR_HEIGHT - 2);
  276. attrEdit.resizePopup = true;
  277. attrEdit.placeholderText = STRIKED_OUT;
  278. attrEdit.vars["Index"] = index;
  279. attrEdit.vars["SubIndex"] = subIndex;
  280. attrEdit.SetLayout(LM_HORIZONTAL, 0, IntRect(4, 1, 4, 1));
  281. SetAttributeEditorID(attrEdit, serializables);
  282. for (uint i = 0; i < info.enumNames.length; ++i)
  283. {
  284. Text@ choice = Text();
  285. attrEdit.AddItem(choice);
  286. choice.style = "EditorEnumAttributeText";
  287. choice.text = info.enumNames[i];
  288. }
  289. SubscribeToEvent(attrEdit, "ItemSelected", "EditAttribute");
  290. }
  291. return parent;
  292. }
  293. UIElement@ CreateResourceRefAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex, bool suppressedSeparatedLabel = false)
  294. {
  295. UIElement@ parent;
  296. StringHash resourceType;
  297. // Get the real attribute info from the serializable for the correct resource type
  298. AttributeInfo attrInfo = serializables[0].attributeInfos[index];
  299. if (attrInfo.type == VAR_RESOURCEREF)
  300. resourceType = serializables[0].attributes[index].GetResourceRef().type;
  301. else if (attrInfo.type == VAR_RESOURCEREFLIST)
  302. resourceType = serializables[0].attributes[index].GetResourceRefList().type;
  303. else if (attrInfo.type == VAR_VARIANTVECTOR)
  304. resourceType = serializables[0].attributes[index].GetVariantVector()[subIndex].GetResourceRef().type;
  305. ResourcePicker@ picker = GetResourcePicker(resourceType);
  306. // Create the attribute name on a separate non-interactive line to allow for more space
  307. parent = CreateAttributeEditorParentWithSeparatedLabel(list, info.name, index, subIndex, suppressedSeparatedLabel);
  308. UIElement@ container = UIElement();
  309. container.SetLayout(LM_HORIZONTAL, 4, IntRect(info.name.StartsWith(" ") ? 20 : 10, 0, 4, 0)); // Left margin is indented more when the name is so
  310. container.SetFixedHeight(ATTR_HEIGHT);
  311. parent.AddChild(container);
  312. LineEdit@ attrEdit = CreateAttributeLineEdit(container, serializables, index, subIndex);
  313. attrEdit.vars[TYPE_VAR] = resourceType.value;
  314. SubscribeToEvent(attrEdit, "TextFinished", "EditAttribute");
  315. if (picker !is null)
  316. {
  317. if ((picker.actions & ACTION_PICK) != 0)
  318. {
  319. Button@ pickButton = CreateResourcePickerButton(container, serializables, index, subIndex, "smallButtonPick");
  320. SubscribeToEvent(pickButton, "Released", "PickResource");
  321. }
  322. if ((picker.actions & ACTION_OPEN) != 0)
  323. {
  324. Button@ openButton = CreateResourcePickerButton(container, serializables, index, subIndex, "smallButtonOpen");
  325. SubscribeToEvent(openButton, "Released", "OpenResource");
  326. }
  327. if ((picker.actions & ACTION_EDIT) != 0)
  328. {
  329. Button@ editButton = CreateResourcePickerButton(container, serializables, index, subIndex, "smallButtonEdit");
  330. SubscribeToEvent(editButton, "Released", "EditResource");
  331. }
  332. if ((picker.actions & ACTION_TEST) != 0)
  333. {
  334. Button@ testButton = CreateResourcePickerButton(container, serializables, index, subIndex, "smallButtonTest");
  335. SubscribeToEvent(testButton, "Released", "TestResource");
  336. }
  337. }
  338. return parent;
  339. }
  340. Button@ CreateResourcePickerButton(UIElement@ container, Array<Serializable@>@ serializables, uint index, uint subIndex, const String&in text)
  341. {
  342. Button@ button = Button();
  343. container.AddChild(button);
  344. button.style = AUTO_STYLE;
  345. button.SetFixedSize(36, ATTR_HEIGHT - 2);
  346. button.vars["Index"] = index;
  347. button.vars["SubIndex"] = subIndex;
  348. SetAttributeEditorID(button, serializables);
  349. Text@ buttonText = Text();
  350. button.AddChild(buttonText);
  351. buttonText.style = "EditorAttributeText";
  352. buttonText.SetAlignment(HA_CENTER, VA_CENTER);
  353. buttonText.text = text;
  354. buttonText.autoLocalizable = true;
  355. return button;
  356. }
  357. // Use internally for nested variant vector
  358. uint nestedSubIndex;
  359. Array<Variant>@ nestedVector;
  360. UIElement@ CreateAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex, bool suppressedSeparatedLabel = false)
  361. {
  362. UIElement@ parent;
  363. VariantType type = info.type;
  364. if (type == VAR_STRING || type == VAR_BUFFER)
  365. parent = CreateStringAttributeEditor(list, serializables, info, index, subIndex);
  366. else if (type == VAR_BOOL)
  367. parent = CreateBoolAttributeEditor(list, serializables, info, index, subIndex);
  368. else if ((type >= VAR_FLOAT && type <= VAR_VECTOR4) || type == VAR_QUATERNION || type == VAR_COLOR || type == VAR_INTVECTOR2 || type == VAR_INTVECTOR3 || type == VAR_INTRECT || type == VAR_DOUBLE || type == VAR_RECT)
  369. parent = CreateNumAttributeEditor(list, serializables, info, index, subIndex);
  370. else if (type == VAR_INT)
  371. parent = CreateIntAttributeEditor(list, serializables, info, index, subIndex);
  372. else if (type == VAR_RESOURCEREF)
  373. parent = CreateResourceRefAttributeEditor(list, serializables, info, index, subIndex, suppressedSeparatedLabel);
  374. else if (type == VAR_RESOURCEREFLIST)
  375. {
  376. uint numRefs = serializables[0].attributes[index].GetResourceRefList().length;
  377. // Straightly speaking the individual resource reference in the list is not an attribute of the serializable
  378. // However, the AttributeInfo structure is used here to reduce the number of parameters being passed in the function
  379. AttributeInfo refInfo;
  380. refInfo.name = info.name;
  381. refInfo.type = VAR_RESOURCEREF;
  382. for (uint i = 0; i < numRefs; ++i)
  383. CreateAttributeEditor(list, serializables, refInfo, index, i, i > 0);
  384. }
  385. else if (type == VAR_VARIANTVECTOR)
  386. {
  387. uint nameIndex = 0;
  388. uint repeat = M_MAX_UNSIGNED;
  389. VectorStruct@ vectorStruct;
  390. Array<Variant>@ vector;
  391. bool emptyNestedVector = false;
  392. if (info.name.Contains('>'))
  393. {
  394. @vector = @nestedVector;
  395. vectorStruct = GetNestedVectorStruct(serializables, info.name);
  396. repeat = vector[subIndex].GetUInt(); // Nested VariantVector must have a predefined repeat count at the start of the vector
  397. emptyNestedVector = repeat == 0;
  398. }
  399. else
  400. {
  401. @vector = serializables[0].attributes[index].GetVariantVector();
  402. vectorStruct = GetVectorStruct(serializables, index);
  403. subIndex = 0;
  404. }
  405. if (vectorStruct is null)
  406. return null;
  407. for (uint i = subIndex; i < vector.length; ++i)
  408. {
  409. // The individual variant in the vector is not an attribute of the serializable, the structure is reused for convenience
  410. AttributeInfo vectorInfo;
  411. vectorInfo.name = vectorStruct.variableNames[nameIndex++];
  412. bool nested = vectorInfo.name.Contains('>');
  413. if (nested)
  414. {
  415. vectorInfo.type = VAR_VARIANTVECTOR;
  416. @nestedVector = @vector;
  417. }
  418. else
  419. vectorInfo.type = vector[i].type;
  420. CreateAttributeEditor(list, serializables, vectorInfo, index, i);
  421. if (nested)
  422. {
  423. i = nestedSubIndex;
  424. @nestedVector = null;
  425. }
  426. if (emptyNestedVector)
  427. {
  428. nestedSubIndex = i;
  429. break;
  430. }
  431. if (nameIndex >= vectorStruct.variableNames.length)
  432. {
  433. if (--repeat == 0)
  434. {
  435. nestedSubIndex = i;
  436. break;
  437. }
  438. nameIndex = vectorStruct.restartIndex;
  439. // Create small divider for repeated instances
  440. UIElement@ divider = UIElement();
  441. divider.SetFixedHeight(8);
  442. list.AddItem(divider);
  443. }
  444. }
  445. }
  446. else if (type == VAR_VARIANTMAP)
  447. {
  448. VariantMap map = serializables[0].attributes[index].GetVariantMap();
  449. Array<StringHash>@ keys = map.keys;
  450. for (uint i = 0; i < keys.length; ++i)
  451. {
  452. String varName = GetVarName(keys[i]);
  453. bool shouldHide = false;
  454. if (varName.empty)
  455. {
  456. // UIElements will contain internal vars, which do not have known mappings. Hide these
  457. if (cast<UIElement>(serializables[0]) !is null)
  458. shouldHide = true;
  459. // Else, for scene nodes, show as hexadecimal hashes if nothing else is available
  460. varName = keys[i].ToString();
  461. }
  462. Variant value = map[keys[i]];
  463. // The individual variant in the map is not an attribute of the serializable, the structure is reused for convenience
  464. AttributeInfo mapInfo;
  465. mapInfo.name = varName + " (Var)";
  466. mapInfo.type = value.type;
  467. parent = CreateAttributeEditor(list, serializables, mapInfo, index, i);
  468. // Add the variant key to the parent. We may fail to add the editor in case it is unsupported
  469. if (parent !is null)
  470. {
  471. parent.vars["Key"] = keys[i].value;
  472. // If variable name is not registered (i.e. it is an editor internal variable) then hide it
  473. if (varName.empty || shouldHide)
  474. parent.visible = false;
  475. }
  476. }
  477. }
  478. return parent;
  479. }
  480. uint GetAttributeEditorCount(Array<Serializable@>@ serializables)
  481. {
  482. uint count = 0;
  483. if (!serializables.empty)
  484. {
  485. /// \todo When multi-editing, this only counts the editor count of the first serializable
  486. bool isUIElement = cast<UIElement>(serializables[0]) !is null;
  487. for (uint i = 0; i < serializables[0].numAttributes; ++i)
  488. {
  489. AttributeInfo info = serializables[0].attributeInfos[i];
  490. if (!showNonEditableAttribute && info.mode & AM_NOEDIT != 0)
  491. continue;
  492. // "Is Enabled" is not inserted into the main attribute list, so do not count
  493. // Similarly, for UIElement, "Is Visible" is not inserted
  494. if (info.name == (isUIElement ? "Is Visible" : "Is Enabled"))
  495. continue;
  496. // Tags are also handled separately
  497. if (info.name == "Tags")
  498. continue;
  499. if (info.type == VAR_RESOURCEREFLIST)
  500. count += serializables[0].attributes[i].GetResourceRefList().length;
  501. else if (info.type == VAR_VARIANTVECTOR && GetVectorStruct(serializables, i) !is null)
  502. count += serializables[0].attributes[i].GetVariantVector().length;
  503. else if (info.type == VAR_VARIANTMAP)
  504. count += serializables[0].attributes[i].GetVariantMap().length;
  505. else
  506. ++count;
  507. }
  508. }
  509. return count;
  510. }
  511. UIElement@ GetAttributeEditorParent(UIElement@ parent, uint index, uint subIndex)
  512. {
  513. return parent.GetChild("Edit" + String(index) + "_" + String(subIndex), true);
  514. }
  515. void LoadAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index)
  516. {
  517. bool editable = info.mode & AM_NOEDIT == 0;
  518. UIElement@ parent = GetAttributeEditorParent(list, index, 0);
  519. if (parent is null)
  520. return;
  521. inLoadAttributeEditor = true;
  522. bool sameName = true;
  523. bool sameValue = true;
  524. Variant value = serializables[0].attributes[index];
  525. Array<Variant> values;
  526. for (uint i = 0; i < serializables.length; ++i)
  527. {
  528. if (index >= serializables[i].numAttributes || serializables[i].attributeInfos[index].name != info.name)
  529. {
  530. sameName = false;
  531. break;
  532. }
  533. Variant val = serializables[i].attributes[index];
  534. if (val != value)
  535. sameValue = false;
  536. values.Push(val);
  537. }
  538. // Attribute with different values from multiple-select is loaded with default/empty value and non-editable
  539. if (sameName)
  540. LoadAttributeEditor(parent, value, info, editable, sameValue, values);
  541. else
  542. parent.visible = false;
  543. inLoadAttributeEditor = false;
  544. }
  545. void LoadAttributeEditor(UIElement@ parent, const Variant&in value, const AttributeInfo&in info, bool editable, bool sameValue, const Array<Variant>&in values)
  546. {
  547. uint index = parent.vars["Index"].GetUInt();
  548. // Assume the first child is always a text label element or a container that containing a text label element
  549. UIElement@ label = parent.children[0];
  550. if (label.type == UI_ELEMENT_TYPE && label.numChildren > 0)
  551. label = label.children[0];
  552. if (label.type == TEXT_TYPE)
  553. {
  554. bool modified;
  555. if (info.defaultValue.type == VAR_NONE || info.defaultValue.type == VAR_RESOURCEREFLIST)
  556. modified = !value.zero;
  557. else
  558. modified = value != info.defaultValue;
  559. cast<Text>(label).color = (editable ? (modified ? modifiedTextColor : normalTextColor) : nonEditableTextColor);
  560. }
  561. VariantType type = info.type;
  562. if (type == VAR_FLOAT || type == VAR_DOUBLE || type == VAR_STRING || type == VAR_BUFFER)
  563. SetEditable(SetValue(parent.children[1], value.ToString(), sameValue), editable && sameValue);
  564. else if (type == VAR_BOOL)
  565. SetEditable(SetValue(parent.children[1], value.GetBool(), sameValue), editable && sameValue);
  566. else if (type == VAR_INT)
  567. {
  568. if (bitSelectionAttrs.Find(info.name) > -1)
  569. SetEditable(SetValue(parent.GetChild("LineEdit", true), value.ToString(), sameValue), editable && sameValue);
  570. else if (info.enumNames is null || info.enumNames.empty)
  571. SetEditable(SetValue(parent.children[1], value.ToString(), sameValue), editable && sameValue);
  572. else
  573. SetEditable(SetValue(parent.children[1], value.GetInt(), sameValue), editable && sameValue);
  574. }
  575. else if (type == VAR_RESOURCEREF)
  576. {
  577. SetEditable(SetValue(parent.children[1].children[0], value.GetResourceRef().name, sameValue), editable && sameValue);
  578. SetEditable(parent.children[1].children[1], editable && sameValue); // If editable then can pick
  579. for (uint i = 2; i < parent.children[1].numChildren; ++i)
  580. SetEditable(parent.children[1].children[i], sameValue); // If same value then can open/edit/test
  581. }
  582. else if (type == VAR_RESOURCEREFLIST)
  583. {
  584. UIElement@ list = parent.parent;
  585. ResourceRefList refList = value.GetResourceRefList();
  586. for (uint subIndex = 0; subIndex < refList.length; ++subIndex)
  587. {
  588. parent = GetAttributeEditorParent(list, index, subIndex);
  589. if (parent is null)
  590. break;
  591. String firstName = refList.names[subIndex];
  592. bool nameSameValue = true;
  593. if (!sameValue)
  594. {
  595. // Reevaluate each name in the list
  596. for (uint i = 0; i < values.length; ++i)
  597. {
  598. ResourceRefList rList = values[i].GetResourceRefList();
  599. if (subIndex >= rList.length || rList.names[subIndex] != firstName)
  600. {
  601. nameSameValue = false;
  602. break;
  603. }
  604. }
  605. }
  606. SetEditable(SetValue(parent.children[1].children[0], firstName, nameSameValue), editable && nameSameValue);
  607. }
  608. }
  609. else if (type == VAR_VARIANTVECTOR)
  610. {
  611. UIElement@ list = parent.parent;
  612. Array<Variant>@ vector = value.GetVariantVector();
  613. for (uint subIndex = 0; subIndex < vector.length; ++subIndex)
  614. {
  615. parent = GetAttributeEditorParent(list, index, subIndex);
  616. if (parent is null)
  617. break;
  618. Variant firstValue = vector[subIndex];
  619. bool sameVal = true;
  620. Array<Variant> varValues;
  621. // Reevaluate each variant in the vector
  622. for (uint i = 0; i < values.length; ++i)
  623. {
  624. Array<Variant>@ vec = values[i].GetVariantVector();
  625. if (subIndex < vec.length)
  626. {
  627. Variant v = vec[subIndex];
  628. varValues.Push(v);
  629. if (v != firstValue)
  630. sameVal = false;
  631. }
  632. else
  633. sameVal = false;
  634. }
  635. // The individual variant in the list is not an attribute of the serializable, the structure is reused for convenience
  636. AttributeInfo ai;
  637. ai.type = firstValue.type;
  638. LoadAttributeEditor(parent, firstValue, ai, editable, sameVal, varValues);
  639. }
  640. }
  641. else if (type == VAR_VARIANTMAP)
  642. {
  643. UIElement@ list = parent.parent;
  644. VariantMap map = value.GetVariantMap();
  645. Array<StringHash>@ keys = map.keys;
  646. for (uint subIndex = 0; subIndex < keys.length; ++subIndex)
  647. {
  648. parent = GetAttributeEditorParent(list, index, subIndex);
  649. if (parent is null)
  650. break;
  651. String varName = GetVarName(keys[subIndex]);
  652. if (varName.empty)
  653. varName = keys[subIndex].ToString(); // Use hexadecimal if nothing else is available
  654. Variant firstValue = map[keys[subIndex]];
  655. bool sameVal = true;
  656. Array<Variant> varValues;
  657. // Reevaluate each variant in the map
  658. for (uint i = 0; i < values.length; ++i)
  659. {
  660. VariantMap m = values[i].GetVariantMap();
  661. if (m.Contains(keys[subIndex]))
  662. {
  663. Variant v = m[keys[subIndex]];
  664. varValues.Push(v);
  665. if (v != firstValue)
  666. sameVal = false;
  667. }
  668. else
  669. sameVal = false;
  670. }
  671. // The individual variant in the map is not an attribute of the serializable, the structure is reused for convenience
  672. AttributeInfo ai;
  673. ai.type = firstValue.type;
  674. LoadAttributeEditor(parent, firstValue, ai, editable, sameVal, varValues);
  675. }
  676. }
  677. else
  678. {
  679. Array<Array<String> > coordinates;
  680. for (uint i = 0; i < values.length; ++i)
  681. {
  682. Variant v = values[i];
  683. // Convert Quaternion value to Vector3 value first
  684. if (type == VAR_QUATERNION)
  685. v = v.GetQuaternion().eulerAngles;
  686. coordinates.Push(v.ToString().Split(' '));
  687. }
  688. for (uint i = 0; i < coordinates[0].length; ++i)
  689. {
  690. String str = coordinates[0][i];
  691. bool coordinateSameValue = true;
  692. if (!sameValue)
  693. {
  694. // Reevaluate each coordinate
  695. for (uint j = 1; j < coordinates.length; ++j)
  696. {
  697. if (coordinates[j][i] != str)
  698. {
  699. coordinateSameValue = false;
  700. break;
  701. }
  702. }
  703. }
  704. SetEditable(SetValue(parent.children[i + 1], str, coordinateSameValue), editable && coordinateSameValue);
  705. }
  706. }
  707. }
  708. void StoreAttributeEditor(UIElement@ parent, Array<Serializable@>@ serializables, uint index, uint subIndex, uint coordinate)
  709. {
  710. AttributeInfo info = serializables[0].attributeInfos[index];
  711. if (info.type == VAR_RESOURCEREFLIST)
  712. {
  713. for (uint i = 0; i < serializables.length; ++i)
  714. {
  715. ResourceRefList refList = serializables[i].attributes[index].GetResourceRefList();
  716. Variant[] values(1);
  717. GetEditorValue(parent, VAR_RESOURCEREF, null, coordinate, values);
  718. ResourceRef ref = values[0].GetResourceRef();
  719. refList.names[subIndex] = ref.name;
  720. serializables[i].attributes[index] = Variant(refList);
  721. }
  722. }
  723. else if (info.type == VAR_VARIANTVECTOR)
  724. {
  725. for (uint i = 0; i < serializables.length; ++i)
  726. {
  727. Array<Variant>@ vector = serializables[i].attributes[index].GetVariantVector();
  728. Variant[] values;
  729. values.Push(vector[subIndex]); // Each individual variant may have multiple coordinates itself
  730. GetEditorValue(parent, vector[subIndex].type, null, coordinate, values);
  731. vector[subIndex] = values[0];
  732. serializables[i].attributes[index] = Variant(vector);
  733. }
  734. }
  735. else if (info.type == VAR_VARIANTMAP)
  736. {
  737. StringHash key(parent.vars["Key"].GetUInt());
  738. for (uint i = 0; i < serializables.length; ++i)
  739. {
  740. VariantMap map = serializables[i].attributes[index].GetVariantMap();
  741. Variant[] values;
  742. values.Push(map[key]); // Each individual variant may have multiple coordinates itself
  743. GetEditorValue(parent, map[key].type, null, coordinate, values);
  744. map[key] = values[0];
  745. serializables[i].attributes[index] = Variant(map);
  746. }
  747. }
  748. else
  749. {
  750. Array<Variant> values;
  751. for (uint i = 0; i < serializables.length; ++i)
  752. values.Push(serializables[i].attributes[index]);
  753. GetEditorValue(parent, info.type, info.enumNames, coordinate, values);
  754. for (uint i = 0; i < serializables.length; ++i)
  755. serializables[i].attributes[index] = values[i];
  756. }
  757. }
  758. void FillValue(Array<Variant>& values, const Variant&in value)
  759. {
  760. for (uint i = 0; i < values.length; ++i)
  761. values[i] = value;
  762. }
  763. void SanitizeNumericalValue(VariantType type, String& value)
  764. {
  765. if ((type >= VAR_FLOAT && type <= VAR_COLOR) || type == VAR_RECT)
  766. value = String(value.ToFloat());
  767. else if (type == VAR_INT || type == VAR_INTRECT || type == VAR_INTVECTOR2 || type == VAR_INTVECTOR3)
  768. value = String(value.ToInt());
  769. else if (type == VAR_DOUBLE)
  770. value = String(value.ToDouble());
  771. }
  772. void GetEditorValue(UIElement@ parent, VariantType type, Array<String>@ enumNames, uint coordinate, Array<Variant>& values)
  773. {
  774. LineEdit@ attrEdit = parent.children[coordinate + 1];
  775. if (attrEdit is null)
  776. attrEdit = parent.GetChild("LineEdit", true);
  777. if (type == VAR_STRING)
  778. FillValue(values, Variant(attrEdit.text.Trimmed()));
  779. else if (type == VAR_BOOL)
  780. {
  781. CheckBox@ cb = parent.children[1];
  782. FillValue(values, Variant(cb.checked));
  783. }
  784. else if (type == VAR_FLOAT)
  785. FillValue(values, Variant(attrEdit.text.ToFloat()));
  786. else if (type == VAR_DOUBLE)
  787. FillValue(values, Variant(attrEdit.text.ToDouble()));
  788. else if (type == VAR_QUATERNION)
  789. {
  790. float value = attrEdit.text.ToFloat();
  791. for (uint i = 0; i < values.length; ++i)
  792. {
  793. float[] data = values[i].GetQuaternion().eulerAngles.data;
  794. data[coordinate] = value;
  795. values[i] = Quaternion(Vector3(data));
  796. }
  797. }
  798. else if (type == VAR_INT)
  799. {
  800. if (enumNames is null || enumNames.empty)
  801. FillValue(values, Variant(attrEdit.text.ToInt()));
  802. else
  803. {
  804. DropDownList@ ddl = parent.children[1];
  805. FillValue(values, Variant(ddl.selection));
  806. }
  807. }
  808. else if (type == VAR_RESOURCEREF)
  809. {
  810. LineEdit@ le = parent.children[0];
  811. ResourceRef ref;
  812. ref.name = le.text.Trimmed();
  813. ref.type = StringHash(le.vars[TYPE_VAR].GetUInt());
  814. FillValue(values, Variant(ref));
  815. }
  816. else
  817. {
  818. String value = attrEdit.text;
  819. SanitizeNumericalValue(type, value);
  820. for (uint i = 0; i < values.length; ++i)
  821. {
  822. String[] data = values[i].ToString().Split(' ');
  823. data[coordinate] = value;
  824. values[i] = Variant(type, Join(data, " "));
  825. }
  826. }
  827. }
  828. void UpdateAttributes(Array<Serializable@>@ serializables, ListView@ list, bool& fullUpdate)
  829. {
  830. // If attributes have changed structurally, do a full update
  831. uint count = GetAttributeEditorCount(serializables);
  832. if (fullUpdate == false)
  833. {
  834. if (list.contentElement.numChildren != count)
  835. fullUpdate = true;
  836. }
  837. // Remember the old scroll position so that a full update does not feel as jarring
  838. IntVector2 oldViewPos = list.viewPosition;
  839. if (fullUpdate)
  840. {
  841. list.RemoveAllItems();
  842. Array<UIElement@> children = list.GetChildren();
  843. for (uint i = 0; i < children.length; ++i)
  844. {
  845. if (!children[i].internal)
  846. children[i].Remove();
  847. }
  848. }
  849. if (serializables.empty)
  850. return;
  851. // If there are many serializables, they must share same attribute structure (up to certain number if not all)
  852. for (uint i = 0; i < serializables[0].numAttributes; ++i)
  853. {
  854. AttributeInfo info = serializables[0].attributeInfos[i];
  855. if (!showNonEditableAttribute && info.mode & AM_NOEDIT != 0)
  856. continue;
  857. // Use the default value (could be instance's default value) of the first serializable as the default for all
  858. info.defaultValue = serializables[0].attributeDefaults[i];
  859. if (fullUpdate)
  860. CreateAttributeEditor(list, serializables, info, i, 0);
  861. LoadAttributeEditor(list, serializables, info, i);
  862. }
  863. if (fullUpdate)
  864. list.viewPosition = oldViewPos;
  865. }
  866. void CreateDragSlider(LineEdit@ parent)
  867. {
  868. Button@ dragSld = Button();
  869. dragSld.style = "EditorDragSlider";
  870. dragSld.SetFixedHeight(ATTR_HEIGHT - 3);
  871. dragSld.SetFixedWidth(dragSld.height);
  872. dragSld.SetAlignment(HA_RIGHT, VA_TOP);
  873. dragSld.focusMode = FM_NOTFOCUSABLE;
  874. parent.AddChild(dragSld);
  875. SubscribeToEvent(dragSld, "DragBegin", "LineDragBegin");
  876. SubscribeToEvent(dragSld, "DragMove", "LineDragMove");
  877. SubscribeToEvent(dragSld, "DragEnd", "LineDragEnd");
  878. SubscribeToEvent(dragSld, "DragCancel", "LineDragCancel");
  879. }
  880. void EditAttribute(StringHash eventType, VariantMap& eventData)
  881. {
  882. // Changing elements programmatically may cause events to be sent. Stop possible infinite loop in that case.
  883. if (inLoadAttributeEditor)
  884. return;
  885. UIElement@ attrEdit = eventData["Element"].GetPtr();
  886. UIElement@ parent = attrEdit.parent;
  887. Array<Serializable@>@ serializables = GetAttributeEditorTargets(attrEdit);
  888. if (serializables.empty)
  889. return;
  890. uint index = attrEdit.vars["Index"].GetUInt();
  891. uint subIndex = attrEdit.vars["SubIndex"].GetUInt();
  892. uint coordinate = attrEdit.vars["Coordinate"].GetUInt();
  893. bool intermediateEdit = eventType == TEXT_CHANGED_EVENT_TYPE;
  894. // Do the editor pre logic before attribute is being modified
  895. if (!PreEditAttribute(serializables, index))
  896. return;
  897. inEditAttribute = true;
  898. Array<Variant> oldValues;
  899. if (!dragEditAttribute)
  900. {
  901. // Store old values so that PostEditAttribute can create undo actions
  902. for (uint i = 0; i < serializables.length; ++i)
  903. oldValues.Push(serializables[i].attributes[index]);
  904. }
  905. StoreAttributeEditor(parent, serializables, index, subIndex, coordinate);
  906. for (uint i = 0; i < serializables.length; ++i)
  907. serializables[i].ApplyAttributes();
  908. if (!dragEditAttribute)
  909. {
  910. // Do the editor post logic after attribute has been modified.
  911. PostEditAttribute(serializables, index, oldValues);
  912. }
  913. inEditAttribute = false;
  914. // If not an intermediate edit, reload the editor fields with validated values
  915. // (attributes may have interactions; therefore we load everything, not just the value being edited)
  916. if (!intermediateEdit)
  917. attributesDirty = true;
  918. }
  919. void LineDragBegin(StringHash eventType, VariantMap& eventData)
  920. {
  921. UIElement@ label = eventData["Element"].GetPtr();
  922. int x = eventData["X"].GetInt();
  923. label.vars["posX"] = x;
  924. // Store the old value before dragging
  925. dragEditAttribute = false;
  926. LineEdit@ selectedNumEditor = label.parent;
  927. selectedNumEditor.vars["DragBeginValue"] = selectedNumEditor.text;
  928. selectedNumEditor.cursorPosition = 0;
  929. // Set mouse mode to user preference
  930. SetMouseMode(true);
  931. }
  932. void LineDragMove(StringHash eventTypem, VariantMap& eventData)
  933. {
  934. UIElement@ label = eventData["Element"].GetPtr();
  935. LineEdit@ selectedNumEditor = label.parent;
  936. // Prevent undo
  937. dragEditAttribute = true;
  938. int x = eventData["X"].GetInt();
  939. int posx = label.vars["posX"].GetInt();
  940. float val = input.mouseMoveX;
  941. float fieldVal = selectedNumEditor.text.ToFloat();
  942. fieldVal += val/100;
  943. label.vars["posX"] = x;
  944. selectedNumEditor.text = fieldVal;
  945. selectedNumEditor.cursorPosition = 0;
  946. }
  947. void LineDragEnd(StringHash eventType, VariantMap& eventData)
  948. {
  949. UIElement@ label = eventData["Element"].GetPtr();
  950. LineEdit@ selectedNumEditor = label.parent;
  951. // Prepare the attributes to store an undo with:
  952. // - old value = drag begin value
  953. // - new value = final value
  954. String finalValue = selectedNumEditor.text;
  955. // Reset attribute to begin value, and prevent undo
  956. dragEditAttribute = true;
  957. selectedNumEditor.text = selectedNumEditor.vars["DragBeginValue"].GetString();
  958. // Store final value, allow undo
  959. dragEditAttribute = false;
  960. selectedNumEditor.text = finalValue;
  961. selectedNumEditor.cursorPosition = 0;
  962. // Revert mouse to normal behaviour
  963. SetMouseMode(false);
  964. }
  965. void LineDragCancel(StringHash eventType, VariantMap& eventData)
  966. {
  967. UIElement@ label = eventData["Element"].GetPtr();
  968. // Reset value to what it was when drag edit began, preventing undo.
  969. dragEditAttribute = true;
  970. LineEdit@ selectedNumEditor = label.parent;
  971. selectedNumEditor.text = selectedNumEditor.vars["DragBeginValue"].GetString();
  972. selectedNumEditor.cursorPosition = 0;
  973. // Revert mouse to normal behaviour
  974. SetMouseMode(false);
  975. }
  976. // Resource picker functionality
  977. const uint ACTION_PICK = 1;
  978. const uint ACTION_OPEN = 2;
  979. const uint ACTION_EDIT = 4;
  980. const uint ACTION_TEST = 8;
  981. class ResourcePicker
  982. {
  983. String typeName;
  984. StringHash type;
  985. String lastPath;
  986. uint lastFilter;
  987. Array<String> filters;
  988. uint actions;
  989. ResourcePicker(const String&in typeName_, const String&in filter_, uint actions_ = ACTION_PICK | ACTION_OPEN)
  990. {
  991. typeName = typeName_;
  992. type = StringHash(typeName_);
  993. actions = actions_;
  994. filters.Push(filter_);
  995. filters.Push("*.*");
  996. lastFilter = 0;
  997. }
  998. ResourcePicker(const String&in typeName_, const Array<String>@ filters_, uint actions_ = ACTION_PICK | ACTION_OPEN)
  999. {
  1000. typeName = typeName_;
  1001. type = StringHash(typeName_);
  1002. filters = filters_;
  1003. actions = actions_;
  1004. filters.Push("*.*");
  1005. lastFilter = 0;
  1006. }
  1007. };
  1008. Array<ResourcePicker@> resourcePickers;
  1009. Array<Serializable@> resourceTargets;
  1010. uint resourcePickIndex = 0;
  1011. uint resourcePickSubIndex = 0;
  1012. ResourcePicker@ resourcePicker = null;
  1013. void InitResourcePicker()
  1014. {
  1015. // Fill resource picker data
  1016. Array<String> fontFilters = {"*.ttf", "*.otf", "*.fnt", "*.xml", "*.sdf"};
  1017. Array<String> imageFilters = {"*.png", "*.jpg", "*.bmp", "*.tga", "*.hdr"};
  1018. Array<String> luaFileFilters = {"*.lua", "*.luc"};
  1019. Array<String> scriptFilters = {"*.as", "*.asc"};
  1020. Array<String> soundFilters = {"*.wav","*.ogg"};
  1021. Array<String> textureFilters = {"*.dds", "*.png", "*.jpg", "*.bmp", "*.tga", "*.ktx", "*.pvr", "*.hdr"};
  1022. Array<String> materialFilters = {"*.xml", "*.material", "*.json"};
  1023. Array<String> anmSetFilters = {"*.scml"};
  1024. Array<String> pexFilters = {"*.pex"};
  1025. Array<String> tmxFilters = {"*.tmx"};
  1026. resourcePickers.Push(ResourcePicker("Animation", "*.ani", ACTION_PICK | ACTION_TEST));
  1027. resourcePickers.Push(ResourcePicker("Font", fontFilters));
  1028. resourcePickers.Push(ResourcePicker("Image", imageFilters));
  1029. resourcePickers.Push(ResourcePicker("LuaFile", luaFileFilters));
  1030. resourcePickers.Push(ResourcePicker("Material", materialFilters, ACTION_PICK | ACTION_OPEN | ACTION_EDIT));
  1031. resourcePickers.Push(ResourcePicker("Model", "*.mdl", ACTION_PICK));
  1032. resourcePickers.Push(ResourcePicker("ParticleEffect", "*.xml", ACTION_PICK | ACTION_OPEN | ACTION_EDIT));
  1033. resourcePickers.Push(ResourcePicker("ScriptFile", scriptFilters));
  1034. resourcePickers.Push(ResourcePicker("Sound", soundFilters));
  1035. resourcePickers.Push(ResourcePicker("Technique", "*.xml"));
  1036. resourcePickers.Push(ResourcePicker("Texture2D", textureFilters));
  1037. resourcePickers.Push(ResourcePicker("TextureCube", "*.xml"));
  1038. resourcePickers.Push(ResourcePicker("Texture3D", "*.xml"));
  1039. resourcePickers.Push(ResourcePicker("XMLFile", "*.xml"));
  1040. resourcePickers.Push(ResourcePicker("JSONFile", "*.json"));
  1041. resourcePickers.Push(ResourcePicker("Sprite2D", textureFilters, ACTION_PICK | ACTION_OPEN));
  1042. resourcePickers.Push(ResourcePicker("AnimationSet2D", anmSetFilters, ACTION_PICK | ACTION_OPEN));
  1043. resourcePickers.Push(ResourcePicker("ParticleEffect2D", pexFilters, ACTION_PICK | ACTION_OPEN));
  1044. resourcePickers.Push(ResourcePicker("TmxFile2D", tmxFilters, ACTION_PICK | ACTION_OPEN));
  1045. }
  1046. ResourcePicker@ GetResourcePicker(StringHash resourceType)
  1047. {
  1048. for (uint i = 0; i < resourcePickers.length; ++i)
  1049. {
  1050. // TODO: refactor to use dictionary instead
  1051. if (resourceType == resourcePickers[i].type)
  1052. return resourcePickers[i];
  1053. }
  1054. return null;
  1055. }
  1056. void PickResource(StringHash eventType, VariantMap& eventData)
  1057. {
  1058. UIElement@ button = eventData["Element"].GetPtr();
  1059. LineEdit@ attrEdit = button.parent.children[0];
  1060. Array<Serializable@>@ targets = GetAttributeEditorTargets(attrEdit);
  1061. if (targets.empty)
  1062. return;
  1063. resourcePickIndex = attrEdit.vars["Index"].GetUInt();
  1064. resourcePickSubIndex = attrEdit.vars["SubIndex"].GetUInt();
  1065. AttributeInfo info = targets[0].attributeInfos[resourcePickIndex];
  1066. StringHash resourceType;
  1067. if (info.type == VAR_RESOURCEREF)
  1068. resourceType = targets[0].attributes[resourcePickIndex].GetResourceRef().type;
  1069. else if (info.type == VAR_RESOURCEREFLIST)
  1070. resourceType = targets[0].attributes[resourcePickIndex].GetResourceRefList().type;
  1071. else if (info.type == VAR_VARIANTVECTOR)
  1072. resourceType = targets[0].attributes[resourcePickIndex].GetVariantVector()[resourcePickSubIndex].GetResourceRef().type;
  1073. @resourcePicker = GetResourcePicker(resourceType);
  1074. if (resourcePicker is null)
  1075. return;
  1076. resourceTargets.Clear();
  1077. for (uint i = 0; i < targets.length; ++i)
  1078. resourceTargets.Push(targets[i]);
  1079. String lastPath = resourcePicker.lastPath;
  1080. if (lastPath.empty)
  1081. lastPath = sceneResourcePath;
  1082. CreateFileSelector(localization.Get("Pick ") + resourcePicker.typeName, "OK", "Cancel", lastPath, resourcePicker.filters, resourcePicker.lastFilter, false);
  1083. SubscribeToEvent(uiFileSelector, "FileSelected", "PickResourceDone");
  1084. }
  1085. void PickResourceDone(StringHash eventType, VariantMap& eventData)
  1086. {
  1087. StoreResourcePickerPath();
  1088. CloseFileSelector();
  1089. if (!eventData["OK"].GetBool())
  1090. {
  1091. resourceTargets.Clear();
  1092. @resourcePicker = null;
  1093. return;
  1094. }
  1095. if (resourcePicker is null)
  1096. return;
  1097. // Validate the resource. It must come from within a registered resource directory, and be loaded successfully
  1098. String resourceName = eventData["FileName"].GetString();
  1099. Resource@ res = GetPickedResource(resourceName);
  1100. if (res is null)
  1101. {
  1102. @resourcePicker = null;
  1103. return;
  1104. }
  1105. // Store old values so that PostEditAttribute can create undo actions
  1106. Array<Variant> oldValues;
  1107. for (uint i = 0; i < resourceTargets.length; ++i)
  1108. oldValues.Push(resourceTargets[i].attributes[resourcePickIndex]);
  1109. for (uint i = 0; i < resourceTargets.length; ++i)
  1110. {
  1111. Serializable@ target = resourceTargets[i];
  1112. AttributeInfo info = target.attributeInfos[resourcePickIndex];
  1113. if (info.type == VAR_RESOURCEREF)
  1114. {
  1115. ResourceRef ref = target.attributes[resourcePickIndex].GetResourceRef();
  1116. ref.type = res.type;
  1117. ref.name = res.name;
  1118. target.attributes[resourcePickIndex] = Variant(ref);
  1119. target.ApplyAttributes();
  1120. }
  1121. else if (info.type == VAR_RESOURCEREFLIST)
  1122. {
  1123. ResourceRefList refList = target.attributes[resourcePickIndex].GetResourceRefList();
  1124. if (resourcePickSubIndex < refList.length)
  1125. {
  1126. refList.names[resourcePickSubIndex] = res.name;
  1127. target.attributes[resourcePickIndex] = Variant(refList);
  1128. target.ApplyAttributes();
  1129. }
  1130. }
  1131. else if (info.type == VAR_VARIANTVECTOR)
  1132. {
  1133. Array<Variant>@ attrs = target.attributes[resourcePickIndex].GetVariantVector();
  1134. ResourceRef ref = attrs[resourcePickSubIndex].GetResourceRef();
  1135. ref.type = res.type;
  1136. ref.name = res.name;
  1137. attrs[resourcePickSubIndex] = ref;
  1138. target.attributes[resourcePickIndex] = Variant(attrs);
  1139. target.ApplyAttributes();
  1140. }
  1141. }
  1142. PostEditAttribute(resourceTargets, resourcePickIndex, oldValues);
  1143. UpdateAttributeInspector(false);
  1144. resourceTargets.Clear();
  1145. @resourcePicker = null;
  1146. }
  1147. void StoreResourcePickerPath()
  1148. {
  1149. // Store filter and directory for next time
  1150. if (resourcePicker !is null && uiFileSelector !is null)
  1151. {
  1152. resourcePicker.lastPath = uiFileSelector.path;
  1153. resourcePicker.lastFilter = uiFileSelector.filterIndex;
  1154. }
  1155. }
  1156. Resource@ GetPickedResource(String resourceName)
  1157. {
  1158. resourceName = GetResourceNameFromFullName(resourceName);
  1159. String type = resourcePicker.typeName;
  1160. // Cube and 3D textures both use .xml extension. In that case interrogate the proper resource type
  1161. // from the file itself
  1162. if (type == "Texture3D" || type == "TextureCube")
  1163. {
  1164. XMLFile@ xmlRes = cache.GetResource("XMLFile", resourceName);
  1165. if (xmlRes !is null)
  1166. {
  1167. if (xmlRes.root.name.Compare("cubemap", false) == 0 || xmlRes.root.name.Compare("texturecube", false) == 0)
  1168. type = "TextureCube";
  1169. else if (xmlRes.root.name.Compare("texture3d", false) == 0)
  1170. type = "Texture3D";
  1171. }
  1172. }
  1173. Resource@ res = cache.GetResource(type, resourceName);
  1174. if (res is null)
  1175. log.Warning("Cannot find resource type: " + type + " Name:" + resourceName);
  1176. return res;
  1177. }
  1178. String GetResourceNameFromFullName(const String&in resourceName)
  1179. {
  1180. Array<String>@ resourceDirs = cache.resourceDirs;
  1181. for (uint i = 0; i < resourceDirs.length; ++i)
  1182. {
  1183. if (!resourceName.ToLower().StartsWith(resourceDirs[i].ToLower()))
  1184. continue;
  1185. return resourceName.Substring(resourceDirs[i].length);
  1186. }
  1187. return ""; // Not found
  1188. }
  1189. void OpenResource(StringHash eventType, VariantMap& eventData)
  1190. {
  1191. UIElement@ button = eventData["Element"].GetPtr();
  1192. LineEdit@ attrEdit = button.parent.children[0];
  1193. String fileName = attrEdit.text.Trimmed();
  1194. if (fileName.empty)
  1195. return;
  1196. OpenResource(fileName);
  1197. }
  1198. void OpenResource(String fileName)
  1199. {
  1200. Array<String>@ resourceDirs = cache.resourceDirs;
  1201. for (uint i = 0; i < resourceDirs.length; ++i)
  1202. {
  1203. String fullPath = resourceDirs[i] + fileName;
  1204. if (fileSystem.FileExists(fullPath))
  1205. {
  1206. fileSystem.SystemOpen(fullPath, "");
  1207. return;
  1208. }
  1209. }
  1210. }
  1211. void EditResource(StringHash eventType, VariantMap& eventData)
  1212. {
  1213. UIElement@ button = eventData["Element"].GetPtr();
  1214. LineEdit@ attrEdit = button.parent.children[0];
  1215. String fileName = attrEdit.text.Trimmed();
  1216. if (fileName.empty)
  1217. return;
  1218. StringHash resourceType(attrEdit.vars[TYPE_VAR].GetUInt());
  1219. Resource@ resource = cache.GetResource(resourceType, fileName);
  1220. if (resource !is null)
  1221. {
  1222. // For now only Materials can be edited
  1223. if (resource.typeName == "Material")
  1224. EditMaterial(cast<Material>(resource));
  1225. else if (resource.typeName == "ParticleEffect")
  1226. EditParticleEffect(cast<ParticleEffect>(resource));
  1227. }
  1228. }
  1229. void TestResource(StringHash eventType, VariantMap& eventData)
  1230. {
  1231. UIElement@ button = eventData["Element"].GetPtr();
  1232. LineEdit@ attrEdit = button.parent.children[0];
  1233. StringHash resourceType(attrEdit.vars[TYPE_VAR].GetUInt());
  1234. // For now only Animations can be tested
  1235. StringHash animType("Animation");
  1236. if (resourceType == animType)
  1237. TestAnimation(attrEdit);
  1238. }
  1239. void TestAnimation(UIElement@ attrEdit)
  1240. {
  1241. // Note: only supports the AnimationState array in AnimatedModel, and if only 1 model selected
  1242. Array<Serializable@>@ targets = GetAttributeEditorTargets(attrEdit);
  1243. if (targets.length != 1)
  1244. return;
  1245. AnimatedModel@ model = cast<AnimatedModel>(targets[0]);
  1246. if (model is null)
  1247. return;
  1248. uint animStateIndex = (attrEdit.vars["SubIndex"].GetUInt() - 1) / 6;
  1249. if (testAnimState.Get() is null)
  1250. {
  1251. testAnimState = model.GetAnimationState(animStateIndex);
  1252. AnimationState@ animState = testAnimState.Get();
  1253. if (animState !is null)
  1254. animState.time = 0; // Start from beginning
  1255. }
  1256. else
  1257. testAnimState = null;
  1258. }
  1259. void UpdateTestAnimation(float timeStep)
  1260. {
  1261. AnimationState@ animState = testAnimState.Get();
  1262. if (animState !is null)
  1263. {
  1264. // If has also an AnimationController, and scene update is enabled, check if it is also driving the animation
  1265. // and skip in that case (avoid double speed animation)
  1266. if (runUpdate)
  1267. {
  1268. AnimatedModel@ model = animState.model;
  1269. if (model !is null)
  1270. {
  1271. Node@ node = model.node;
  1272. if (node !is null)
  1273. {
  1274. AnimationController@ ctrl = node.GetComponent("AnimationController");
  1275. Animation@ anim = animState.animation;
  1276. if (ctrl !is null && anim !is null)
  1277. {
  1278. if (ctrl.IsPlaying(anim.name))
  1279. return;
  1280. }
  1281. }
  1282. }
  1283. }
  1284. animState.AddTime(timeStep);
  1285. }
  1286. }
  1287. // VariantVector decoding & editing for certain components
  1288. class VectorStruct
  1289. {
  1290. String componentTypeName;
  1291. String attributeName;
  1292. Array<String> variableNames;
  1293. uint restartIndex;
  1294. VectorStruct(const String&in componentTypeName_, const String&in attributeName_, const Array<String>@ variableNames_, uint restartIndex_)
  1295. {
  1296. componentTypeName = componentTypeName_;
  1297. attributeName = attributeName_;
  1298. variableNames = variableNames_;
  1299. restartIndex = restartIndex_;
  1300. }
  1301. };
  1302. Array<VectorStruct@> vectorStructs;
  1303. void InitVectorStructs()
  1304. {
  1305. Array<String> crowdManagerAreaCostVariables = {
  1306. " Area Count",
  1307. " Cost"
  1308. };
  1309. vectorStructs.Push(VectorStruct("CrowdManager", " >AreaCost", crowdManagerAreaCostVariables, 1));
  1310. Array<String> categories = GetObjectCategories();
  1311. for (uint categoryIndex = 0; categoryIndex < categories.length; categoryIndex++)
  1312. {
  1313. Array<String> objectsNames = GetObjectsByCategory(categories[categoryIndex]);
  1314. for (uint objectIndex = 0; objectIndex < objectsNames.length; objectIndex++)
  1315. {
  1316. String objectName = objectsNames[objectIndex];
  1317. Array<AttributeInfo> attributes = GetObjectAttributeInfos(objectName);
  1318. for (uint attributeIndex = 0; attributeIndex < attributes.length; attributeIndex++)
  1319. {
  1320. AttributeInfo attribute = attributes[attributeIndex];
  1321. if (attribute.type == VAR_VARIANTVECTOR and attribute.metadata.Contains("VectorStructElements"))
  1322. {
  1323. Array<String>@ elementsNames = attribute.metadata["VectorStructElements"].GetStringVector();
  1324. vectorStructs.Push(VectorStruct(objectName, attribute.name, elementsNames, 1));
  1325. }
  1326. }
  1327. }
  1328. }
  1329. }
  1330. VectorStruct@ GetVectorStruct(Array<Serializable@>@ serializables, uint index)
  1331. {
  1332. AttributeInfo info = serializables[0].attributeInfos[index];
  1333. for (uint i = 0; i < vectorStructs.length; ++i)
  1334. {
  1335. if (vectorStructs[i].componentTypeName == serializables[0].typeName && vectorStructs[i].attributeName == info.name)
  1336. return vectorStructs[i];
  1337. }
  1338. return null;
  1339. }
  1340. VectorStruct@ GetNestedVectorStruct(Array<Serializable@>@ serializables, const String&in name)
  1341. {
  1342. for (uint i = 0; i < vectorStructs.length; ++i)
  1343. {
  1344. if (vectorStructs[i].componentTypeName == serializables[0].typeName && vectorStructs[i].attributeName == name)
  1345. return vectorStructs[i];
  1346. }
  1347. return null;
  1348. }
  1349. int GetAttributeIndex(Serializable@ serializable, const String&in attrName)
  1350. {
  1351. for (uint i = 0; i < serializable.numAttributes; ++i)
  1352. {
  1353. if (serializable.attributeInfos[i].name.Compare(attrName, false) == 0)
  1354. return i;
  1355. }
  1356. return -1;
  1357. }