2
0

AttributeEditor.as 58 KB

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