AttributeEditor.as 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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_QUATERNION)
  235. numCoords = 3;
  236. else if (type == VAR_VECTOR4 || type == VAR_COLOR || type == VAR_INTRECT)
  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) == -1)
  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, "Pick");
  320. SubscribeToEvent(pickButton, "Released", "PickResource");
  321. }
  322. if ((picker.actions & ACTION_OPEN) != 0)
  323. {
  324. Button@ openButton = CreateResourcePickerButton(container, serializables, index, subIndex, "Open");
  325. SubscribeToEvent(openButton, "Released", "OpenResource");
  326. }
  327. if ((picker.actions & ACTION_EDIT) != 0)
  328. {
  329. Button@ editButton = CreateResourcePickerButton(container, serializables, index, subIndex, "Edit");
  330. SubscribeToEvent(editButton, "Released", "EditResource");
  331. }
  332. if ((picker.actions & ACTION_TEST) != 0)
  333. {
  334. Button@ testButton = CreateResourcePickerButton(container, serializables, index, subIndex, "Test");
  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. return button;
  355. }
  356. UIElement@ CreateAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex, bool suppressedSeparatedLabel = false)
  357. {
  358. UIElement@ parent;
  359. VariantType type = info.type;
  360. if (type == VAR_STRING || type == VAR_BUFFER)
  361. parent = CreateStringAttributeEditor(list, serializables, info, index, subIndex);
  362. else if (type == VAR_BOOL)
  363. parent = CreateBoolAttributeEditor(list, serializables, info, index, subIndex);
  364. else if ((type >= VAR_FLOAT && type <= VAR_VECTOR4) || type == VAR_QUATERNION || type == VAR_COLOR || type == VAR_INTVECTOR2 || type == VAR_INTRECT || type == VAR_DOUBLE)
  365. parent = CreateNumAttributeEditor(list, serializables, info, index, subIndex);
  366. else if (type == VAR_INT)
  367. parent = CreateIntAttributeEditor(list, serializables, info, index, subIndex);
  368. else if (type == VAR_RESOURCEREF)
  369. parent = CreateResourceRefAttributeEditor(list, serializables, info, index, subIndex, suppressedSeparatedLabel);
  370. else if (type == VAR_RESOURCEREFLIST)
  371. {
  372. uint numRefs = serializables[0].attributes[index].GetResourceRefList().length;
  373. // Straightly speaking the individual resource reference in the list is not an attribute of the serializable
  374. // However, the AttributeInfo structure is used here to reduce the number of parameters being passed in the function
  375. AttributeInfo refInfo;
  376. refInfo.name = info.name;
  377. refInfo.type = VAR_RESOURCEREF;
  378. for (uint i = 0; i < numRefs; ++i)
  379. CreateAttributeEditor(list, serializables, refInfo, index, i, i > 0);
  380. }
  381. else if (type == VAR_VARIANTVECTOR)
  382. {
  383. VectorStruct@ vectorStruct = GetVectorStruct(serializables, index);
  384. if (vectorStruct is null)
  385. return null;
  386. uint nameIndex = 0;
  387. Array<Variant>@ vector = serializables[0].attributes[index].GetVariantVector();
  388. for (uint i = 0; i < vector.length; ++i)
  389. {
  390. // The individual variant in the vector is not an attribute of the serializable, the structure is reused for convenience
  391. AttributeInfo vectorInfo;
  392. vectorInfo.name = vectorStruct.variableNames[nameIndex];
  393. vectorInfo.type = vector[i].type;
  394. CreateAttributeEditor(list, serializables, vectorInfo, index, i);
  395. ++nameIndex;
  396. if (nameIndex >= vectorStruct.variableNames.length)
  397. nameIndex = vectorStruct.restartIndex;
  398. }
  399. }
  400. else if (type == VAR_VARIANTMAP)
  401. {
  402. VariantMap map = serializables[0].attributes[index].GetVariantMap();
  403. Array<StringHash>@ keys = map.keys;
  404. for (uint i = 0; i < keys.length; ++i)
  405. {
  406. String varName = GetVarName(keys[i]);
  407. if (varName.empty)
  408. {
  409. // UIElements will contain internal vars, which do not have known mappings. Skip these
  410. if (cast<UIElement>(serializables[0]) !is null)
  411. continue;
  412. // Else, for scene nodes, show as hexadecimal hashes if nothing else is available
  413. varName = keys[i].ToString();
  414. }
  415. Variant value = map[keys[i]];
  416. // The individual variant in the map is not an attribute of the serializable, the structure is reused for convenience
  417. AttributeInfo mapInfo;
  418. mapInfo.name = varName + " (Var)";
  419. mapInfo.type = value.type;
  420. parent = CreateAttributeEditor(list, serializables, mapInfo, index, i);
  421. // Add the variant key to the parent. We may fail to add the editor in case it is unsupported
  422. if (parent !is null)
  423. {
  424. parent.vars["Key"] = keys[i].value;
  425. // If variable name is not registered (i.e. it is an editor internal variable) then hide it
  426. if (varName.empty)
  427. parent.visible = false;
  428. }
  429. }
  430. }
  431. return parent;
  432. }
  433. uint GetAttributeEditorCount(Array<Serializable@>@ serializables)
  434. {
  435. uint count = 0;
  436. if (!serializables.empty)
  437. {
  438. /// \todo When multi-editing, this only counts the editor count of the first serializable
  439. bool isUIElement = cast<UIElement>(serializables[0]) !is null;
  440. for (uint i = 0; i < serializables[0].numAttributes; ++i)
  441. {
  442. AttributeInfo info = serializables[0].attributeInfos[i];
  443. if (!showNonEditableAttribute && info.mode & AM_NOEDIT != 0)
  444. continue;
  445. // "Is Enabled" is not inserted into the main attribute list, so do not count
  446. // Similarly, for UIElement, "Is Visible" is not inserted
  447. if (info.name == (isUIElement ? "Is Visible" : "Is Enabled"))
  448. continue;
  449. if (info.type == VAR_RESOURCEREFLIST)
  450. count += serializables[0].attributes[i].GetResourceRefList().length;
  451. else if (info.type == VAR_VARIANTVECTOR && GetVectorStruct(serializables, i) !is null)
  452. count += serializables[0].attributes[i].GetVariantVector().length;
  453. else if (info.type == VAR_VARIANTMAP)
  454. count += serializables[0].attributes[i].GetVariantMap().length;
  455. else
  456. ++count;
  457. }
  458. }
  459. return count;
  460. }
  461. UIElement@ GetAttributeEditorParent(UIElement@ parent, uint index, uint subIndex)
  462. {
  463. return parent.GetChild("Edit" + String(index) + "_" + String(subIndex), true);
  464. }
  465. void LoadAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index)
  466. {
  467. bool editable = info.mode & AM_NOEDIT == 0;
  468. UIElement@ parent = GetAttributeEditorParent(list, index, 0);
  469. if (parent is null)
  470. return;
  471. inLoadAttributeEditor = true;
  472. bool sameName = true;
  473. bool sameValue = true;
  474. Variant value = serializables[0].attributes[index];
  475. Array<Variant> values;
  476. for (uint i = 0; i < serializables.length; ++i)
  477. {
  478. if (index >= serializables[i].numAttributes || serializables[i].attributeInfos[index].name != info.name)
  479. {
  480. sameName = false;
  481. break;
  482. }
  483. Variant val = serializables[i].attributes[index];
  484. if (val != value)
  485. sameValue = false;
  486. values.Push(val);
  487. }
  488. // Attribute with different values from multiple-select is loaded with default/empty value and non-editable
  489. if (sameName)
  490. LoadAttributeEditor(parent, value, info, editable, sameValue, values);
  491. else
  492. parent.visible = false;
  493. inLoadAttributeEditor = false;
  494. }
  495. void LoadAttributeEditor(UIElement@ parent, const Variant&in value, const AttributeInfo&in info, bool editable, bool sameValue, const Array<Variant>&in values)
  496. {
  497. uint index = parent.vars["Index"].GetUInt();
  498. // Assume the first child is always a text label element or a container that containing a text label element
  499. UIElement@ label = parent.children[0];
  500. if (label.type == UI_ELEMENT_TYPE && label.numChildren > 0)
  501. label = label.children[0];
  502. if (label.type == TEXT_TYPE)
  503. {
  504. bool modified;
  505. if (info.defaultValue.type == VAR_NONE || info.defaultValue.type == VAR_RESOURCEREFLIST)
  506. modified = !value.zero;
  507. else
  508. modified = value != info.defaultValue;
  509. cast<Text>(label).color = (editable ? (modified ? modifiedTextColor : normalTextColor) : nonEditableTextColor);
  510. }
  511. VariantType type = info.type;
  512. if (type == VAR_FLOAT || type == VAR_DOUBLE || type == VAR_STRING || type == VAR_BUFFER)
  513. SetEditable(SetValue(parent.children[1], value.ToString(), sameValue), editable && sameValue);
  514. else if (type == VAR_BOOL)
  515. SetEditable(SetValue(parent.children[1], value.GetBool(), sameValue), editable && sameValue);
  516. else if (type == VAR_INT)
  517. {
  518. if (bitSelectionAttrs.Find(info.name) > -1)
  519. SetEditable(SetValue(parent.GetChild("LineEdit", true), value.ToString(), sameValue), editable && sameValue);
  520. else if (info.enumNames is null || info.enumNames.empty)
  521. SetEditable(SetValue(parent.children[1], value.ToString(), sameValue), editable && sameValue);
  522. else
  523. SetEditable(SetValue(parent.children[1], value.GetInt(), sameValue), editable && sameValue);
  524. }
  525. else if (type == VAR_RESOURCEREF)
  526. {
  527. SetEditable(SetValue(parent.children[1].children[0], value.GetResourceRef().name, sameValue), editable && sameValue);
  528. SetEditable(parent.children[1].children[1], editable && sameValue); // If editable then can pick
  529. for (uint i = 2; i < parent.children[1].numChildren; ++i)
  530. SetEditable(parent.children[1].children[i], sameValue); // If same value then can open/edit/test
  531. }
  532. else if (type == VAR_RESOURCEREFLIST)
  533. {
  534. UIElement@ list = parent.parent;
  535. ResourceRefList refList = value.GetResourceRefList();
  536. for (uint subIndex = 0; subIndex < refList.length; ++subIndex)
  537. {
  538. parent = GetAttributeEditorParent(list, index, subIndex);
  539. if (parent is null)
  540. break;
  541. String firstName = refList.names[subIndex];
  542. bool nameSameValue = true;
  543. if (!sameValue)
  544. {
  545. // Reevaluate each name in the list
  546. for (uint i = 0; i < values.length; ++i)
  547. {
  548. ResourceRefList refList = values[i].GetResourceRefList();
  549. if (subIndex >= refList.length || refList.names[subIndex] != firstName)
  550. {
  551. nameSameValue = false;
  552. break;
  553. }
  554. }
  555. }
  556. SetEditable(SetValue(parent.children[1].children[0], firstName, nameSameValue), editable && nameSameValue);
  557. }
  558. }
  559. else if (type == VAR_VARIANTVECTOR)
  560. {
  561. UIElement@ list = parent.parent;
  562. Array<Variant>@ vector = value.GetVariantVector();
  563. for (uint subIndex = 0; subIndex < vector.length; ++subIndex)
  564. {
  565. parent = GetAttributeEditorParent(list, index, subIndex);
  566. if (parent is null)
  567. break;
  568. Variant firstValue = vector[subIndex];
  569. bool sameValue = true;
  570. Array<Variant> varValues;
  571. // Reevaluate each variant in the vector
  572. for (uint i = 0; i < values.length; ++i)
  573. {
  574. Array<Variant>@ vector = values[i].GetVariantVector();
  575. if (subIndex < vector.length)
  576. {
  577. Variant value = vector[subIndex];
  578. varValues.Push(value);
  579. if (value != firstValue)
  580. sameValue = false;
  581. }
  582. else
  583. sameValue = false;
  584. }
  585. // The individual variant in the list is not an attribute of the serializable, the structure is reused for convenience
  586. AttributeInfo info;
  587. info.type = firstValue.type;
  588. LoadAttributeEditor(parent, firstValue, info, editable, sameValue, varValues);
  589. }
  590. }
  591. else if (type == VAR_VARIANTMAP)
  592. {
  593. UIElement@ list = parent.parent;
  594. VariantMap map = value.GetVariantMap();
  595. Array<StringHash>@ keys = map.keys;
  596. for (uint subIndex = 0; subIndex < keys.length; ++subIndex)
  597. {
  598. parent = GetAttributeEditorParent(list, index, subIndex);
  599. if (parent is null)
  600. break;
  601. String varName = GetVarName(keys[subIndex]);
  602. if (varName.empty)
  603. varName = keys[subIndex].ToString(); // Use hexadecimal if nothing else is available
  604. Variant firstValue = map[keys[subIndex]];
  605. bool sameValue = true;
  606. Array<Variant> varValues;
  607. // Reevaluate each variant in the map
  608. for (uint i = 0; i < values.length; ++i)
  609. {
  610. VariantMap map = values[i].GetVariantMap();
  611. if (map.Contains(keys[subIndex]))
  612. {
  613. Variant value = map[keys[subIndex]];
  614. varValues.Push(value);
  615. if (value != firstValue)
  616. sameValue = false;
  617. }
  618. else
  619. sameValue = false;
  620. }
  621. // The individual variant in the map is not an attribute of the serializable, the structure is reused for convenience
  622. AttributeInfo info;
  623. info.type = firstValue.type;
  624. LoadAttributeEditor(parent, firstValue, info, editable, sameValue, varValues);
  625. }
  626. }
  627. else
  628. {
  629. Array<Array<String> > coordinates;
  630. for (uint i = 0; i < values.length; ++i)
  631. {
  632. Variant value = values[i];
  633. // Convert Quaternion value to Vector3 value first
  634. if (type == VAR_QUATERNION)
  635. value = value.GetQuaternion().eulerAngles;
  636. coordinates.Push(value.ToString().Split(' '));
  637. }
  638. for (uint i = 0; i < coordinates[0].length; ++i)
  639. {
  640. String value = coordinates[0][i];
  641. bool coordinateSameValue = true;
  642. if (!sameValue)
  643. {
  644. // Reevaluate each coordinate
  645. for (uint j = 1; j < coordinates.length; ++j)
  646. {
  647. if (coordinates[j][i] != value)
  648. {
  649. coordinateSameValue = false;
  650. break;
  651. }
  652. }
  653. }
  654. SetEditable(SetValue(parent.children[i + 1], value, coordinateSameValue), editable && coordinateSameValue);
  655. }
  656. }
  657. }
  658. void StoreAttributeEditor(UIElement@ parent, Array<Serializable@>@ serializables, uint index, uint subIndex, uint coordinate)
  659. {
  660. AttributeInfo info = serializables[0].attributeInfos[index];
  661. if (info.type == VAR_RESOURCEREFLIST)
  662. {
  663. for (uint i = 0; i < serializables.length; ++i)
  664. {
  665. ResourceRefList refList = serializables[i].attributes[index].GetResourceRefList();
  666. Variant[] values(1);
  667. GetEditorValue(parent, VAR_RESOURCEREF, null, coordinate, values);
  668. ResourceRef ref = values[0].GetResourceRef();
  669. refList.names[subIndex] = ref.name;
  670. serializables[i].attributes[index] = Variant(refList);
  671. }
  672. }
  673. else if (info.type == VAR_VARIANTVECTOR)
  674. {
  675. for (uint i = 0; i < serializables.length; ++i)
  676. {
  677. Array<Variant>@ vector = serializables[i].attributes[index].GetVariantVector();
  678. Variant[] values;
  679. values.Push(vector[subIndex]); // Each individual variant may have multiple coordinates itself
  680. GetEditorValue(parent, vector[subIndex].type, null, coordinate, values);
  681. vector[subIndex] = values[0];
  682. serializables[i].attributes[index] = Variant(vector);
  683. }
  684. }
  685. else if (info.type == VAR_VARIANTMAP)
  686. {
  687. VariantMap map = serializables[0].attributes[index].GetVariantMap();
  688. StringHash key(parent.vars["Key"].GetUInt());
  689. for (uint i = 0; i < serializables.length; ++i)
  690. {
  691. VariantMap map = serializables[i].attributes[index].GetVariantMap();
  692. Variant[] values;
  693. values.Push(map[key]); // Each individual variant may have multiple coordinates itself
  694. GetEditorValue(parent, map[key].type, null, coordinate, values);
  695. map[key] = values[0];
  696. serializables[i].attributes[index] = Variant(map);
  697. }
  698. }
  699. else
  700. {
  701. Array<Variant> values;
  702. for (uint i = 0; i < serializables.length; ++i)
  703. values.Push(serializables[i].attributes[index]);
  704. GetEditorValue(parent, info.type, info.enumNames, coordinate, values);
  705. for (uint i = 0; i < serializables.length; ++i)
  706. serializables[i].attributes[index] = values[i];
  707. }
  708. }
  709. void FillValue(Array<Variant>& values, const Variant&in value)
  710. {
  711. for (uint i = 0; i < values.length; ++i)
  712. values[i] = value;
  713. }
  714. void SanitizeNumericalValue(VariantType type, String& value)
  715. {
  716. if (type >= VAR_FLOAT && type <= VAR_COLOR)
  717. value = String(value.ToFloat());
  718. else if (type == VAR_INT || type == VAR_INTRECT || type == VAR_INTVECTOR2)
  719. value = String(value.ToInt());
  720. else if (type == VAR_DOUBLE)
  721. value = String(value.ToDouble());
  722. }
  723. void GetEditorValue(UIElement@ parent, VariantType type, Array<String>@ enumNames, uint coordinate, Array<Variant>& values)
  724. {
  725. LineEdit@ attrEdit = parent.children[coordinate + 1];
  726. if (attrEdit is null)
  727. attrEdit = parent.GetChild("LineEdit", true);
  728. if (type == VAR_STRING)
  729. FillValue(values, Variant(attrEdit.text.Trimmed()));
  730. else if (type == VAR_BOOL)
  731. {
  732. CheckBox@ attrEdit = parent.children[1];
  733. FillValue(values, Variant(attrEdit.checked));
  734. }
  735. else if (type == VAR_FLOAT)
  736. FillValue(values, Variant(attrEdit.text.ToFloat()));
  737. else if (type == VAR_DOUBLE)
  738. FillValue(values, Variant(attrEdit.text.ToDouble()));
  739. else if (type == VAR_QUATERNION)
  740. {
  741. float value = attrEdit.text.ToFloat();
  742. for (uint i = 0; i < values.length; ++i)
  743. {
  744. float[] data = values[i].GetQuaternion().eulerAngles.data;
  745. data[coordinate] = value;
  746. values[i] = Quaternion(Vector3(data));
  747. }
  748. }
  749. else if (type == VAR_INT)
  750. {
  751. if (enumNames is null || enumNames.empty)
  752. FillValue(values, Variant(attrEdit.text.ToInt()));
  753. else
  754. {
  755. DropDownList@ attrEdit = parent.children[1];
  756. FillValue(values, Variant(attrEdit.selection));
  757. }
  758. }
  759. else if (type == VAR_RESOURCEREF)
  760. {
  761. LineEdit@ attrEdit = parent.children[0];
  762. ResourceRef ref;
  763. ref.name = attrEdit.text.Trimmed();
  764. ref.type = StringHash(attrEdit.vars[TYPE_VAR].GetUInt());
  765. FillValue(values, Variant(ref));
  766. }
  767. else
  768. {
  769. String value = attrEdit.text;
  770. SanitizeNumericalValue(type, value);
  771. for (uint i = 0; i < values.length; ++i)
  772. {
  773. String[] data = values[i].ToString().Split(' ');
  774. data[coordinate] = value;
  775. values[i] = Variant(type, Join(data, " "));
  776. }
  777. }
  778. }
  779. void UpdateAttributes(Array<Serializable@>@ serializables, ListView@ list, bool& fullUpdate)
  780. {
  781. // If attributes have changed structurally, do a full update
  782. uint count = GetAttributeEditorCount(serializables);
  783. if (fullUpdate == false)
  784. {
  785. if (list.contentElement.numChildren != count)
  786. fullUpdate = true;
  787. }
  788. // Remember the old scroll position so that a full update does not feel as jarring
  789. IntVector2 oldViewPos = list.viewPosition;
  790. if (fullUpdate)
  791. {
  792. list.RemoveAllItems();
  793. Array<UIElement@> children = list.GetChildren();
  794. for (uint i = 0; i < children.length; ++i)
  795. {
  796. if (!children[i].internal)
  797. children[i].Remove();
  798. }
  799. }
  800. if (serializables.empty)
  801. return;
  802. // If there are many serializables, they must share same attribute structure (up to certain number if not all)
  803. for (uint i = 0; i < serializables[0].numAttributes; ++i)
  804. {
  805. AttributeInfo info = serializables[0].attributeInfos[i];
  806. if (!showNonEditableAttribute && info.mode & AM_NOEDIT != 0)
  807. continue;
  808. // Use the default value (could be instance's default value) of the first serializable as the default for all
  809. info.defaultValue = serializables[0].attributeDefaults[i];
  810. if (fullUpdate)
  811. CreateAttributeEditor(list, serializables, info, i, 0);
  812. LoadAttributeEditor(list, serializables, info, i);
  813. }
  814. if (fullUpdate)
  815. list.viewPosition = oldViewPos;
  816. }
  817. void EditScriptAttributes(Component@ component, uint index)
  818. {
  819. if (component !is null && component.typeName.Contains("ScriptInstance"))
  820. {
  821. String hash = GetComponentAttributeHash(component, index);
  822. if (!hash.empty)
  823. scriptAttributes[hash] = component.attributes[index];
  824. }
  825. }
  826. void CreateDragSlider(LineEdit@ parent)
  827. {
  828. Button@ dragSld = Button();
  829. dragSld.style = "EditorDragSlider";
  830. dragSld.SetFixedHeight(ATTR_HEIGHT - 3);
  831. dragSld.SetFixedWidth(dragSld.height);
  832. dragSld.SetAlignment(HA_RIGHT, VA_TOP);
  833. dragSld.focusMode = FM_NOTFOCUSABLE;
  834. parent.AddChild(dragSld);
  835. SubscribeToEvent(dragSld, "DragBegin", "LineDragBegin");
  836. SubscribeToEvent(dragSld, "DragMove", "LineDragMove");
  837. SubscribeToEvent(dragSld, "DragEnd", "LineDragEnd");
  838. SubscribeToEvent(dragSld, "DragCancel", "LineDragCancel");
  839. }
  840. void EditAttribute(StringHash eventType, VariantMap& eventData)
  841. {
  842. // Changing elements programmatically may cause events to be sent. Stop possible infinite loop in that case.
  843. if (inLoadAttributeEditor)
  844. return;
  845. UIElement@ attrEdit = eventData["Element"].GetPtr();
  846. UIElement@ parent = attrEdit.parent;
  847. Array<Serializable@>@ serializables = GetAttributeEditorTargets(attrEdit);
  848. if (serializables.empty)
  849. return;
  850. uint index = attrEdit.vars["Index"].GetUInt();
  851. uint subIndex = attrEdit.vars["SubIndex"].GetUInt();
  852. uint coordinate = attrEdit.vars["Coordinate"].GetUInt();
  853. bool intermediateEdit = eventType == TEXT_CHANGED_EVENT_TYPE;
  854. // Do the editor pre logic before attribute is being modified
  855. if (!PreEditAttribute(serializables, index))
  856. return;
  857. inEditAttribute = true;
  858. Array<Variant> oldValues;
  859. if (!dragEditAttribute)
  860. {
  861. // Store old values so that PostEditAttribute can create undo actions
  862. for (uint i = 0; i < serializables.length; ++i)
  863. oldValues.Push(serializables[i].attributes[index]);
  864. }
  865. StoreAttributeEditor(parent, serializables, index, subIndex, coordinate);
  866. for (uint i = 0; i < serializables.length; ++i)
  867. serializables[i].ApplyAttributes();
  868. if (!dragEditAttribute)
  869. {
  870. // Do the editor post logic after attribute has been modified.
  871. PostEditAttribute(serializables, index, oldValues);
  872. }
  873. // Update the stored script attributes if this is a ScriptInstance
  874. EditScriptAttributes(serializables[0], index);
  875. inEditAttribute = false;
  876. // If not an intermediate edit, reload the editor fields with validated values
  877. // (attributes may have interactions; therefore we load everything, not just the value being edited)
  878. if (!intermediateEdit)
  879. attributesDirty = true;
  880. }
  881. void LineDragBegin(StringHash eventType, VariantMap& eventData)
  882. {
  883. UIElement@ label = eventData["Element"].GetPtr();
  884. int x = eventData["X"].GetInt();
  885. label.vars["posX"] = x;
  886. // Store the old value before dragging
  887. dragEditAttribute = false;
  888. LineEdit@ selectedNumEditor = label.parent;
  889. selectedNumEditor.vars["DragBeginValue"] = selectedNumEditor.text;
  890. selectedNumEditor.cursorPosition = 0;
  891. // Set mouse mode to user preference
  892. SetMouseMode(true);
  893. }
  894. void LineDragMove(StringHash eventTypem, VariantMap& eventData)
  895. {
  896. UIElement@ label = eventData["Element"].GetPtr();
  897. LineEdit@ selectedNumEditor = label.parent;
  898. // Prevent undo
  899. dragEditAttribute = true;
  900. int x = eventData["X"].GetInt();
  901. int posx = label.vars["posX"].GetInt();
  902. float val = input.mouseMoveX;
  903. float fieldVal = selectedNumEditor.text.ToFloat();
  904. fieldVal += val/100;
  905. label.vars["posX"] = x;
  906. selectedNumEditor.text = fieldVal;
  907. selectedNumEditor.cursorPosition = 0;
  908. }
  909. void LineDragEnd(StringHash eventType, VariantMap& eventData)
  910. {
  911. UIElement@ label = eventData["Element"].GetPtr();
  912. LineEdit@ selectedNumEditor = label.parent;
  913. // Prepare the attributes to store an undo with:
  914. // - old value = drag begin value
  915. // - new value = final value
  916. String finalValue = selectedNumEditor.text;
  917. // Reset attribute to begin value, and prevent undo
  918. dragEditAttribute = true;
  919. selectedNumEditor.text = selectedNumEditor.vars["DragBeginValue"].GetString();
  920. // Store final value, allow undo
  921. dragEditAttribute = false;
  922. selectedNumEditor.text = finalValue;
  923. selectedNumEditor.cursorPosition = 0;
  924. // Revert mouse to normal behaviour
  925. SetMouseMode(false);
  926. }
  927. void LineDragCancel(StringHash eventType, VariantMap& eventData)
  928. {
  929. UIElement@ label = eventData["Element"].GetPtr();
  930. // Reset value to what it was when drag edit began, preventing undo.
  931. dragEditAttribute = true;
  932. LineEdit@ selectedNumEditor = label.parent;
  933. selectedNumEditor.text = selectedNumEditor.vars["DragBeginValue"].GetString();
  934. selectedNumEditor.cursorPosition = 0;
  935. // Revert mouse to normal behaviour
  936. SetMouseMode(false);
  937. }
  938. // Resource picker functionality
  939. const uint ACTION_PICK = 1;
  940. const uint ACTION_OPEN = 2;
  941. const uint ACTION_EDIT = 4;
  942. const uint ACTION_TEST = 8;
  943. class ResourcePicker
  944. {
  945. String typeName;
  946. StringHash type;
  947. String lastPath;
  948. uint lastFilter;
  949. Array<String> filters;
  950. uint actions;
  951. ResourcePicker(const String&in typeName_, const String&in filter_, uint actions_ = ACTION_PICK | ACTION_OPEN)
  952. {
  953. typeName = typeName_;
  954. type = StringHash(typeName_);
  955. actions = actions_;
  956. filters.Push(filter_);
  957. filters.Push("*.*");
  958. lastFilter = 0;
  959. }
  960. ResourcePicker(const String&in typeName_, const Array<String>@ filters_, uint actions_ = ACTION_PICK | ACTION_OPEN)
  961. {
  962. typeName = typeName_;
  963. type = StringHash(typeName_);
  964. filters = filters_;
  965. actions = actions_;
  966. filters.Push("*.*");
  967. lastFilter = 0;
  968. }
  969. };
  970. Array<ResourcePicker@> resourcePickers;
  971. Array<Serializable@> resourceTargets;
  972. uint resourcePickIndex = 0;
  973. uint resourcePickSubIndex = 0;
  974. ResourcePicker@ resourcePicker = null;
  975. void InitResourcePicker()
  976. {
  977. // Fill resource picker data
  978. Array<String> fontFilters = {"*.ttf", "*.otf", "*.fnt", "*.xml"};
  979. Array<String> imageFilters = {"*.png", "*.jpg", "*.bmp", "*.tga"};
  980. Array<String> luaFileFilters = {"*.lua", "*.luc"};
  981. Array<String> scriptFilters = {"*.as", "*.asc"};
  982. Array<String> soundFilters = {"*.wav","*.ogg"};
  983. Array<String> textureFilters = {"*.dds", "*.png", "*.jpg", "*.bmp", "*.tga", "*.ktx", "*.pvr"};
  984. Array<String> materialFilters = {"*.xml", "*.material"};
  985. Array<String> anmSetFilters = {"*.scml"};
  986. Array<String> pexFilters = {"*.pex"};
  987. Array<String> tmxFilters = {"*.tmx"};
  988. resourcePickers.Push(ResourcePicker("Animation", "*.ani", ACTION_PICK | ACTION_TEST));
  989. resourcePickers.Push(ResourcePicker("Font", fontFilters));
  990. resourcePickers.Push(ResourcePicker("Image", imageFilters));
  991. resourcePickers.Push(ResourcePicker("LuaFile", luaFileFilters));
  992. resourcePickers.Push(ResourcePicker("Material", materialFilters, ACTION_PICK | ACTION_OPEN | ACTION_EDIT));
  993. resourcePickers.Push(ResourcePicker("Model", "*.mdl", ACTION_PICK));
  994. resourcePickers.Push(ResourcePicker("ParticleEffect", "*.xml", ACTION_PICK | ACTION_OPEN | ACTION_EDIT));
  995. resourcePickers.Push(ResourcePicker("ScriptFile", scriptFilters));
  996. resourcePickers.Push(ResourcePicker("Sound", soundFilters));
  997. resourcePickers.Push(ResourcePicker("Technique", "*.xml"));
  998. resourcePickers.Push(ResourcePicker("Texture2D", textureFilters));
  999. resourcePickers.Push(ResourcePicker("TextureCube", "*.xml"));
  1000. resourcePickers.Push(ResourcePicker("Texture3D", "*.xml"));
  1001. resourcePickers.Push(ResourcePicker("XMLFile", "*.xml"));
  1002. resourcePickers.Push(ResourcePicker("Sprite2D", textureFilters, ACTION_PICK | ACTION_OPEN));
  1003. resourcePickers.Push(ResourcePicker("AnimationSet2D", anmSetFilters, ACTION_PICK | ACTION_OPEN));
  1004. resourcePickers.Push(ResourcePicker("ParticleEffect2D", pexFilters, ACTION_PICK | ACTION_OPEN));
  1005. resourcePickers.Push(ResourcePicker("TmxFile2D", tmxFilters, ACTION_PICK | ACTION_OPEN));
  1006. }
  1007. ResourcePicker@ GetResourcePicker(StringHash resourceType)
  1008. {
  1009. for (uint i = 0; i < resourcePickers.length; ++i)
  1010. {
  1011. if (resourcePickers[i].type == resourceType)
  1012. return resourcePickers[i];
  1013. }
  1014. return null;
  1015. }
  1016. void PickResource(StringHash eventType, VariantMap& eventData)
  1017. {
  1018. UIElement@ button = eventData["Element"].GetPtr();
  1019. LineEdit@ attrEdit = button.parent.children[0];
  1020. Array<Serializable@>@ targets = GetAttributeEditorTargets(attrEdit);
  1021. if (targets.empty)
  1022. return;
  1023. resourcePickIndex = attrEdit.vars["Index"].GetUInt();
  1024. resourcePickSubIndex = attrEdit.vars["SubIndex"].GetUInt();
  1025. AttributeInfo info = targets[0].attributeInfos[resourcePickIndex];
  1026. StringHash resourceType;
  1027. if (info.type == VAR_RESOURCEREF)
  1028. resourceType = targets[0].attributes[resourcePickIndex].GetResourceRef().type;
  1029. else if (info.type == VAR_RESOURCEREFLIST)
  1030. resourceType = targets[0].attributes[resourcePickIndex].GetResourceRefList().type;
  1031. else if (info.type == VAR_VARIANTVECTOR)
  1032. resourceType = targets[0].attributes[resourcePickIndex].GetVariantVector()[resourcePickSubIndex].GetResourceRef().type;
  1033. @resourcePicker = GetResourcePicker(resourceType);
  1034. if (resourcePicker is null)
  1035. return;
  1036. resourceTargets.Clear();
  1037. for (uint i = 0; i < targets.length; ++i)
  1038. resourceTargets.Push(targets[i]);
  1039. String lastPath = resourcePicker.lastPath;
  1040. if (lastPath.empty)
  1041. lastPath = sceneResourcePath;
  1042. CreateFileSelector("Pick " + resourcePicker.typeName, "OK", "Cancel", lastPath, resourcePicker.filters, resourcePicker.lastFilter);
  1043. SubscribeToEvent(uiFileSelector, "FileSelected", "PickResourceDone");
  1044. }
  1045. void PickResourceDone(StringHash eventType, VariantMap& eventData)
  1046. {
  1047. StoreResourcePickerPath();
  1048. CloseFileSelector();
  1049. if (!eventData["OK"].GetBool())
  1050. {
  1051. resourceTargets.Clear();
  1052. @resourcePicker = null;
  1053. return;
  1054. }
  1055. if (resourcePicker is null)
  1056. return;
  1057. // Validate the resource. It must come from within a registered resource directory, and be loaded successfully
  1058. String resourceName = eventData["FileName"].GetString();
  1059. Resource@ res = GetPickedResource(resourceName);
  1060. if (res is null)
  1061. {
  1062. @resourcePicker = null;
  1063. return;
  1064. }
  1065. // Store old values so that PostEditAttribute can create undo actions
  1066. Array<Variant> oldValues;
  1067. for (uint i = 0; i < resourceTargets.length; ++i)
  1068. oldValues.Push(resourceTargets[i].attributes[resourcePickIndex]);
  1069. for (uint i = 0; i < resourceTargets.length; ++i)
  1070. {
  1071. Serializable@ target = resourceTargets[i];
  1072. AttributeInfo info = target.attributeInfos[resourcePickIndex];
  1073. if (info.type == VAR_RESOURCEREF)
  1074. {
  1075. ResourceRef ref = target.attributes[resourcePickIndex].GetResourceRef();
  1076. ref.type = res.type;
  1077. ref.name = res.name;
  1078. target.attributes[resourcePickIndex] = Variant(ref);
  1079. target.ApplyAttributes();
  1080. }
  1081. else if (info.type == VAR_RESOURCEREFLIST)
  1082. {
  1083. ResourceRefList refList = target.attributes[resourcePickIndex].GetResourceRefList();
  1084. if (resourcePickSubIndex < refList.length)
  1085. {
  1086. refList.names[resourcePickSubIndex] = res.name;
  1087. target.attributes[resourcePickIndex] = Variant(refList);
  1088. target.ApplyAttributes();
  1089. }
  1090. }
  1091. else if (info.type == VAR_VARIANTVECTOR)
  1092. {
  1093. Array<Variant>@ attrs = target.attributes[resourcePickIndex].GetVariantVector();
  1094. ResourceRef ref = attrs[resourcePickSubIndex].GetResourceRef();
  1095. ref.type = res.type;
  1096. ref.name = res.name;
  1097. attrs[resourcePickSubIndex] = ref;
  1098. target.attributes[resourcePickIndex] = Variant(attrs);
  1099. target.ApplyAttributes();
  1100. }
  1101. EditScriptAttributes(target, resourcePickIndex);
  1102. }
  1103. PostEditAttribute(resourceTargets, resourcePickIndex, oldValues);
  1104. UpdateAttributeInspector(false);
  1105. resourceTargets.Clear();
  1106. @resourcePicker = null;
  1107. }
  1108. void StoreResourcePickerPath()
  1109. {
  1110. // Store filter and directory for next time
  1111. if (resourcePicker !is null && uiFileSelector !is null)
  1112. {
  1113. resourcePicker.lastPath = uiFileSelector.path;
  1114. resourcePicker.lastFilter = uiFileSelector.filterIndex;
  1115. }
  1116. }
  1117. Resource@ GetPickedResource(String resourceName)
  1118. {
  1119. resourceName = GetResourceNameFromFullName(resourceName);
  1120. String type = resourcePicker.typeName;
  1121. // Cube and 3D textures both use .xml extension. In that case interrogate the proper resource type
  1122. // from the file itself
  1123. if (type == "Texture3D" || type == "TextureCube")
  1124. {
  1125. XMLFile@ xmlRes = cache.GetResource("XMLFile", resourceName);
  1126. if (xmlRes !is null)
  1127. {
  1128. if (xmlRes.root.name.Compare("cubemap", false) == 0 || xmlRes.root.name.Compare("texturecube", false) == 0)
  1129. type = "TextureCube";
  1130. else if (xmlRes.root.name.Compare("texture3d", false) == 0)
  1131. type = "Texture3D";
  1132. }
  1133. }
  1134. Resource@ res = cache.GetResource(type, resourceName);
  1135. if (res is null)
  1136. log.Warning("Cannot find resource type: " + type + " Name:" + resourceName);
  1137. return res;
  1138. }
  1139. String GetResourceNameFromFullName(const String&in resourceName)
  1140. {
  1141. Array<String>@ resourceDirs = cache.resourceDirs;
  1142. for (uint i = 0; i < resourceDirs.length; ++i)
  1143. {
  1144. if (!resourceName.ToLower().StartsWith(resourceDirs[i].ToLower()))
  1145. continue;
  1146. return resourceName.Substring(resourceDirs[i].length);
  1147. }
  1148. return ""; // Not found
  1149. }
  1150. void OpenResource(StringHash eventType, VariantMap& eventData)
  1151. {
  1152. UIElement@ button = eventData["Element"].GetPtr();
  1153. LineEdit@ attrEdit = button.parent.children[0];
  1154. String fileName = attrEdit.text.Trimmed();
  1155. if (fileName.empty)
  1156. return;
  1157. OpenResource(fileName);
  1158. }
  1159. void OpenResource(String fileName)
  1160. {
  1161. Array<String>@ resourceDirs = cache.resourceDirs;
  1162. for (uint i = 0; i < resourceDirs.length; ++i)
  1163. {
  1164. String fullPath = resourceDirs[i] + fileName;
  1165. if (fileSystem.FileExists(fullPath))
  1166. {
  1167. fileSystem.SystemOpen(fullPath, "");
  1168. return;
  1169. }
  1170. }
  1171. }
  1172. void EditResource(StringHash eventType, VariantMap& eventData)
  1173. {
  1174. UIElement@ button = eventData["Element"].GetPtr();
  1175. LineEdit@ attrEdit = button.parent.children[0];
  1176. String fileName = attrEdit.text.Trimmed();
  1177. if (fileName.empty)
  1178. return;
  1179. StringHash resourceType(attrEdit.vars[TYPE_VAR].GetUInt());
  1180. Resource@ resource = cache.GetResource(resourceType, fileName);
  1181. if (resource !is null)
  1182. {
  1183. // For now only Materials can be edited
  1184. if (resource.typeName == "Material")
  1185. EditMaterial(cast<Material>(resource));
  1186. else if (resource.typeName == "ParticleEffect")
  1187. EditParticleEffect(cast<ParticleEffect>(resource));
  1188. }
  1189. }
  1190. void TestResource(StringHash eventType, VariantMap& eventData)
  1191. {
  1192. UIElement@ button = eventData["Element"].GetPtr();
  1193. LineEdit@ attrEdit = button.parent.children[0];
  1194. StringHash resourceType(attrEdit.vars[TYPE_VAR].GetUInt());
  1195. // For now only Animations can be tested
  1196. StringHash animType("Animation");
  1197. if (resourceType == animType)
  1198. TestAnimation(attrEdit);
  1199. }
  1200. void TestAnimation(UIElement@ attrEdit)
  1201. {
  1202. // Note: only supports the AnimationState array in AnimatedModel, and if only 1 model selected
  1203. Array<Serializable@>@ targets = GetAttributeEditorTargets(attrEdit);
  1204. if (targets.length != 1)
  1205. return;
  1206. AnimatedModel@ model = cast<AnimatedModel>(targets[0]);
  1207. if (model is null)
  1208. return;
  1209. uint animStateIndex = (attrEdit.vars["SubIndex"].GetUInt() - 1) / 6;
  1210. if (testAnimState.Get() is null)
  1211. {
  1212. testAnimState = model.GetAnimationState(animStateIndex);
  1213. AnimationState@ animState = testAnimState.Get();
  1214. if (animState !is null)
  1215. animState.time = 0; // Start from beginning
  1216. }
  1217. else
  1218. testAnimState = null;
  1219. }
  1220. void UpdateTestAnimation(float timeStep)
  1221. {
  1222. AnimationState@ animState = testAnimState.Get();
  1223. if (animState !is null)
  1224. {
  1225. // If has also an AnimationController, and scene update is enabled, check if it is also driving the animation
  1226. // and skip in that case (avoid double speed animation)
  1227. if (runUpdate)
  1228. {
  1229. AnimatedModel@ model = animState.model;
  1230. if (model !is null)
  1231. {
  1232. Node@ node = model.node;
  1233. if (node !is null)
  1234. {
  1235. AnimationController@ ctrl = node.GetComponent("AnimationController");
  1236. Animation@ anim = animState.animation;
  1237. if (ctrl !is null && anim !is null)
  1238. {
  1239. if (ctrl.IsPlaying(anim.name))
  1240. return;
  1241. }
  1242. }
  1243. }
  1244. }
  1245. animState.AddTime(timeStep);
  1246. }
  1247. }
  1248. // VariantVector decoding & editing for certain components
  1249. class VectorStruct
  1250. {
  1251. String componentTypeName;
  1252. String attributeName;
  1253. Array<String> variableNames;
  1254. uint restartIndex;
  1255. VectorStruct(const String&in componentTypeName_, const String&in attributeName_, const Array<String>@ variableNames_, uint restartIndex_)
  1256. {
  1257. componentTypeName = componentTypeName_;
  1258. attributeName = attributeName_;
  1259. variableNames = variableNames_;
  1260. restartIndex = restartIndex_;
  1261. }
  1262. };
  1263. Array<VectorStruct@> vectorStructs;
  1264. void InitVectorStructs()
  1265. {
  1266. // Fill vector structure data
  1267. Array<String> billboardVariables = {
  1268. "Billboard Count",
  1269. " Position",
  1270. " Size",
  1271. " UV Coordinates",
  1272. " Color",
  1273. " Rotation",
  1274. " Is Enabled"
  1275. };
  1276. vectorStructs.Push(VectorStruct("BillboardSet", "Billboards", billboardVariables, 1));
  1277. Array<String> animationStateVariables = {
  1278. "Anim State Count",
  1279. " Animation",
  1280. " Start Bone",
  1281. " Is Looped",
  1282. " Weight",
  1283. " Time",
  1284. " Layer"
  1285. };
  1286. vectorStructs.Push(VectorStruct("AnimatedModel", "Animation States", animationStateVariables, 1));
  1287. Array<String> staticModelGroupInstanceVariables = {
  1288. "Instance Count",
  1289. " NodeID"
  1290. };
  1291. vectorStructs.Push(VectorStruct("StaticModelGroup", "Instance Nodes", staticModelGroupInstanceVariables, 1));
  1292. Array<String> splinePathInstanceVariables = {
  1293. "Control Point Count",
  1294. " NodeID"
  1295. };
  1296. vectorStructs.Push(VectorStruct("SplinePath", "Control Points", splinePathInstanceVariables, 1));
  1297. }
  1298. VectorStruct@ GetVectorStruct(Array<Serializable@>@ serializables, uint index)
  1299. {
  1300. AttributeInfo info = serializables[0].attributeInfos[index];
  1301. for (uint i = 0; i < vectorStructs.length; ++i)
  1302. {
  1303. if (vectorStructs[i].componentTypeName == serializables[0].typeName && vectorStructs[i].attributeName == info.name)
  1304. return vectorStructs[i];
  1305. }
  1306. return null;
  1307. }
  1308. int GetAttributeIndex(Serializable@ serializable, const String&in attrName)
  1309. {
  1310. for (uint i = 0; i < serializable.numAttributes; ++i)
  1311. {
  1312. if (serializable.attributeInfos[i].name.Compare(attrName, false) == 0)
  1313. return i;
  1314. }
  1315. return -1;
  1316. }