editor_inspector.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342
  1. /*************************************************************************/
  2. /* editor_inspector.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "editor_inspector.h"
  31. #include "array_property_edit.h"
  32. #include "dictionary_property_edit.h"
  33. #include "editor_node.h"
  34. #include "editor_scale.h"
  35. #include "multi_node_edit.h"
  36. #include "scene/resources/packed_scene.h"
  37. Size2 EditorProperty::get_minimum_size() const {
  38. Size2 ms;
  39. Ref<Font> font = get_font("font", "Tree");
  40. ms.height = font->get_height();
  41. for (int i = 0; i < get_child_count(); i++) {
  42. Control *c = Object::cast_to<Control>(get_child(i));
  43. if (!c)
  44. continue;
  45. if (c->is_set_as_toplevel())
  46. continue;
  47. if (!c->is_visible())
  48. continue;
  49. if (c == bottom_editor)
  50. continue;
  51. Size2 minsize = c->get_combined_minimum_size();
  52. ms.width = MAX(ms.width, minsize.width);
  53. ms.height = MAX(ms.height, minsize.height);
  54. }
  55. if (keying) {
  56. Ref<Texture> key = get_icon("Key", "EditorIcons");
  57. ms.width += key->get_width() + get_constant("hseparator", "Tree");
  58. }
  59. if (checkable) {
  60. Ref<Texture> check = get_icon("checked", "CheckBox");
  61. ms.width += check->get_width() + get_constant("hseparator", "Tree");
  62. }
  63. if (bottom_editor != NULL && bottom_editor->is_visible()) {
  64. ms.height += get_constant("vseparation", "Tree");
  65. Size2 bems = bottom_editor->get_combined_minimum_size();
  66. //bems.width += get_constant("item_margin", "Tree");
  67. ms.height += bems.height;
  68. ms.width = MAX(ms.width, bems.width);
  69. }
  70. return ms;
  71. }
  72. void EditorProperty::emit_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field, bool p_changing) {
  73. Variant args[4] = { p_property, p_value, p_field, p_changing };
  74. const Variant *argptrs[4] = { &args[0], &args[1], &args[2], &args[3] };
  75. emit_signal("property_changed", (const Variant **)argptrs, 4);
  76. }
  77. void EditorProperty::_notification(int p_what) {
  78. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  79. Size2 size = get_size();
  80. Rect2 rect;
  81. Rect2 bottom_rect;
  82. right_child_rect = Rect2();
  83. bottom_child_rect = Rect2();
  84. {
  85. int child_room = size.width * (1.0 - split_ratio);
  86. Ref<Font> font = get_font("font", "Tree");
  87. int height = font->get_height();
  88. bool no_children = true;
  89. //compute room needed
  90. for (int i = 0; i < get_child_count(); i++) {
  91. Control *c = Object::cast_to<Control>(get_child(i));
  92. if (!c)
  93. continue;
  94. if (c->is_set_as_toplevel())
  95. continue;
  96. if (c == bottom_editor)
  97. continue;
  98. Size2 minsize = c->get_combined_minimum_size();
  99. child_room = MAX(child_room, minsize.width);
  100. height = MAX(height, minsize.height);
  101. no_children = false;
  102. }
  103. if (no_children) {
  104. text_size = size.width;
  105. rect = Rect2(size.width - 1, 0, 1, height);
  106. } else {
  107. text_size = MAX(0, size.width - (child_room + 4 * EDSCALE));
  108. rect = Rect2(size.width - child_room, 0, child_room, height);
  109. }
  110. if (bottom_editor) {
  111. int m = 0; //get_constant("item_margin", "Tree");
  112. bottom_rect = Rect2(m, rect.size.height + get_constant("vseparation", "Tree"), size.width - m, bottom_editor->get_combined_minimum_size().height);
  113. }
  114. if (keying) {
  115. Ref<Texture> key;
  116. if (use_keying_next()) {
  117. key = get_icon("KeyNext", "EditorIcons");
  118. } else {
  119. key = get_icon("Key", "EditorIcons");
  120. }
  121. rect.size.x -= key->get_width() + get_constant("hseparator", "Tree");
  122. if (no_children) {
  123. text_size -= key->get_width() + 4 * EDSCALE;
  124. }
  125. }
  126. }
  127. //set children
  128. for (int i = 0; i < get_child_count(); i++) {
  129. Control *c = Object::cast_to<Control>(get_child(i));
  130. if (!c)
  131. continue;
  132. if (c->is_set_as_toplevel())
  133. continue;
  134. if (c == bottom_editor)
  135. continue;
  136. fit_child_in_rect(c, rect);
  137. right_child_rect = rect;
  138. }
  139. if (bottom_editor) {
  140. fit_child_in_rect(bottom_editor, bottom_rect);
  141. bottom_child_rect = bottom_rect;
  142. }
  143. update(); //need to redraw text
  144. }
  145. if (p_what == NOTIFICATION_DRAW) {
  146. Ref<Font> font = get_font("font", "Tree");
  147. Color dark_color = get_color("dark_color_2", "Editor");
  148. Size2 size = get_size();
  149. if (bottom_editor) {
  150. size.height = bottom_editor->get_margin(MARGIN_TOP);
  151. } else if (label_reference) {
  152. size.height = label_reference->get_size().height;
  153. }
  154. if (selected) {
  155. Ref<StyleBox> sb = get_stylebox("selected", "Tree");
  156. draw_style_box(sb, Rect2(Vector2(), size));
  157. }
  158. if (draw_top_bg && right_child_rect != Rect2()) {
  159. draw_rect(right_child_rect, dark_color);
  160. }
  161. if (bottom_child_rect != Rect2()) {
  162. draw_rect(bottom_child_rect, dark_color);
  163. }
  164. Color color;
  165. if (draw_red) {
  166. color = get_color("error_color", "Editor");
  167. } else {
  168. color = get_color("property_color", "Editor");
  169. }
  170. if (label.find(".") != -1) {
  171. color.a = 0.5; //this should be un-hacked honestly, as it's used for editor overrides
  172. }
  173. int ofs = 0;
  174. int text_limit = text_size;
  175. if (checkable) {
  176. Ref<Texture> checkbox;
  177. if (checked)
  178. checkbox = get_icon("GuiChecked", "EditorIcons");
  179. else
  180. checkbox = get_icon("GuiUnchecked", "EditorIcons");
  181. Color color2(1, 1, 1);
  182. if (check_hover) {
  183. color2.r *= 1.2;
  184. color2.g *= 1.2;
  185. color2.b *= 1.2;
  186. }
  187. check_rect = Rect2(ofs, ((size.height - checkbox->get_height()) / 2), checkbox->get_width(), checkbox->get_height());
  188. draw_texture(checkbox, check_rect.position, color2);
  189. ofs += get_constant("hseparator", "Tree");
  190. ofs += checkbox->get_width();
  191. text_limit -= ofs;
  192. } else {
  193. check_rect = Rect2();
  194. }
  195. if (can_revert) {
  196. Ref<Texture> reload_icon = get_icon("ReloadSmall", "EditorIcons");
  197. text_limit -= reload_icon->get_width() + get_constant("hseparator", "Tree") * 2;
  198. revert_rect = Rect2(text_limit + get_constant("hseparator", "Tree"), (size.height - reload_icon->get_height()) / 2, reload_icon->get_width(), reload_icon->get_height());
  199. Color color2(1, 1, 1);
  200. if (revert_hover) {
  201. color2.r *= 1.2;
  202. color2.g *= 1.2;
  203. color2.b *= 1.2;
  204. }
  205. draw_texture(reload_icon, revert_rect.position, color2);
  206. } else {
  207. revert_rect = Rect2();
  208. }
  209. int v_ofs = (size.height - font->get_height()) / 2;
  210. draw_string(font, Point2(ofs, v_ofs + font->get_ascent()), label, color, text_limit);
  211. if (keying) {
  212. Ref<Texture> key;
  213. if (use_keying_next()) {
  214. key = get_icon("KeyNext", "EditorIcons");
  215. } else {
  216. key = get_icon("Key", "EditorIcons");
  217. }
  218. ofs = size.width - key->get_width() - get_constant("hseparator", "Tree");
  219. Color color2(1, 1, 1);
  220. if (keying_hover) {
  221. color2.r *= 1.2;
  222. color2.g *= 1.2;
  223. color2.b *= 1.2;
  224. }
  225. keying_rect = Rect2(ofs, ((size.height - key->get_height()) / 2), key->get_width(), key->get_height());
  226. draw_texture(key, keying_rect.position, color2);
  227. } else {
  228. keying_rect = Rect2();
  229. }
  230. }
  231. }
  232. void EditorProperty::set_label(const String &p_label) {
  233. label = p_label;
  234. update();
  235. }
  236. String EditorProperty::get_label() const {
  237. return label;
  238. }
  239. Object *EditorProperty::get_edited_object() {
  240. return object;
  241. }
  242. StringName EditorProperty::get_edited_property() {
  243. return property;
  244. }
  245. void EditorProperty::update_property() {
  246. if (get_script_instance())
  247. get_script_instance()->call("update_property");
  248. }
  249. void EditorProperty::set_read_only(bool p_read_only) {
  250. read_only = p_read_only;
  251. }
  252. bool EditorProperty::is_read_only() const {
  253. return read_only;
  254. }
  255. bool EditorPropertyRevert::may_node_be_in_instance(Node *p_node) {
  256. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  257. bool might_be = false;
  258. Node *node = p_node;
  259. while (node) {
  260. if (node->get_scene_instance_state().is_valid()) {
  261. might_be = true;
  262. break;
  263. }
  264. if (node == edited_scene) {
  265. if (node->get_scene_inherited_state().is_valid()) {
  266. might_be = true;
  267. break;
  268. }
  269. might_be = false;
  270. break;
  271. }
  272. node = node->get_owner();
  273. }
  274. return might_be; // or might not be
  275. }
  276. bool EditorPropertyRevert::get_instanced_node_original_property(Node *p_node, const StringName &p_prop, Variant &value) {
  277. Node *node = p_node;
  278. Node *orig = node;
  279. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  280. bool found = false;
  281. while (node) {
  282. Ref<SceneState> ss;
  283. if (node == edited_scene) {
  284. ss = node->get_scene_inherited_state();
  285. } else {
  286. ss = node->get_scene_instance_state();
  287. }
  288. if (ss.is_valid()) {
  289. NodePath np = node->get_path_to(orig);
  290. int node_idx = ss->find_node_by_path(np);
  291. if (node_idx >= 0) {
  292. bool lfound = false;
  293. Variant lvar;
  294. lvar = ss->get_property_value(node_idx, p_prop, lfound);
  295. if (lfound) {
  296. found = true;
  297. value = lvar;
  298. }
  299. }
  300. }
  301. if (node == edited_scene) {
  302. //just in case
  303. break;
  304. }
  305. node = node->get_owner();
  306. }
  307. if (!found) {
  308. //if not found, try default class value
  309. Variant attempt = ClassDB::class_get_default_property_value(node->get_class_name(), p_prop);
  310. if (attempt.get_type() != Variant::NIL) {
  311. found = true;
  312. value = attempt;
  313. }
  314. }
  315. return found;
  316. }
  317. bool EditorPropertyRevert::is_node_property_different(Node *p_node, const Variant &p_current, const Variant &p_orig) {
  318. // this is a pretty difficult function, because a property may not be saved but may have
  319. // the flag to not save if one or if zero
  320. //make sure there is an actual state
  321. {
  322. Node *node = p_node;
  323. if (!node)
  324. return false;
  325. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  326. bool found_state = false;
  327. while (node) {
  328. Ref<SceneState> ss;
  329. if (node == edited_scene) {
  330. ss = node->get_scene_inherited_state();
  331. } else {
  332. ss = node->get_scene_instance_state();
  333. }
  334. if (ss.is_valid()) {
  335. found_state = true;
  336. }
  337. if (node == edited_scene) {
  338. //just in case
  339. break;
  340. }
  341. node = node->get_owner();
  342. }
  343. if (!found_state)
  344. return false; //pointless to check if we are not comparing against anything.
  345. }
  346. if (p_current.get_type() == Variant::REAL && p_orig.get_type() == Variant::REAL) {
  347. float a = p_current;
  348. float b = p_orig;
  349. return !Math::is_equal_approx(a, b); //this must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error
  350. }
  351. return bool(Variant::evaluate(Variant::OP_NOT_EQUAL, p_current, p_orig));
  352. }
  353. bool EditorPropertyRevert::can_property_revert(Object *p_object, const StringName &p_property) {
  354. bool has_revert = false;
  355. Node *node = Object::cast_to<Node>(p_object);
  356. if (node && EditorPropertyRevert::may_node_be_in_instance(node)) {
  357. //check for difference including instantiation
  358. Variant vorig;
  359. if (EditorPropertyRevert::get_instanced_node_original_property(node, p_property, vorig)) {
  360. Variant v = p_object->get(p_property);
  361. if (EditorPropertyRevert::is_node_property_different(node, v, vorig)) {
  362. has_revert = true;
  363. }
  364. }
  365. } else {
  366. //check for difference against default class value instead
  367. Variant default_value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property);
  368. if (default_value != Variant() && default_value != p_object->get(p_property)) {
  369. has_revert = true;
  370. }
  371. }
  372. if (p_object->call("property_can_revert", p_property).operator bool()) {
  373. has_revert = true;
  374. }
  375. if (!has_revert && !p_object->get_script().is_null()) {
  376. Ref<Script> scr = p_object->get_script();
  377. if (scr.is_valid()) {
  378. Variant orig_value;
  379. if (scr->get_property_default_value(p_property, orig_value)) {
  380. if (orig_value != p_object->get(p_property)) {
  381. has_revert = true;
  382. }
  383. }
  384. }
  385. }
  386. return has_revert;
  387. }
  388. void EditorProperty::update_reload_status() {
  389. if (property == StringName())
  390. return; //no property, so nothing to do
  391. bool has_reload = EditorPropertyRevert::can_property_revert(object, property);
  392. if (has_reload != can_revert) {
  393. can_revert = has_reload;
  394. update();
  395. }
  396. }
  397. bool EditorProperty::use_keying_next() const {
  398. List<PropertyInfo> plist;
  399. object->get_property_list(&plist, true);
  400. for (List<PropertyInfo>::Element *I = plist.front(); I; I = I->next()) {
  401. PropertyInfo &p = I->get();
  402. if (p.name == property) {
  403. return p.hint == PROPERTY_HINT_SPRITE_FRAME;
  404. }
  405. }
  406. return false;
  407. }
  408. void EditorProperty::set_checkable(bool p_checkable) {
  409. checkable = p_checkable;
  410. update();
  411. queue_sort();
  412. }
  413. bool EditorProperty::is_checkable() const {
  414. return checkable;
  415. }
  416. void EditorProperty::set_checked(bool p_checked) {
  417. checked = p_checked;
  418. update();
  419. }
  420. bool EditorProperty::is_checked() const {
  421. return checked;
  422. }
  423. void EditorProperty::set_draw_red(bool p_draw_red) {
  424. draw_red = p_draw_red;
  425. update();
  426. }
  427. void EditorProperty::set_keying(bool p_keying) {
  428. keying = p_keying;
  429. update();
  430. queue_sort();
  431. }
  432. bool EditorProperty::is_keying() const {
  433. return keying;
  434. }
  435. bool EditorProperty::is_draw_red() const {
  436. return draw_red;
  437. }
  438. void EditorProperty::_focusable_focused(int p_index) {
  439. if (!selectable)
  440. return;
  441. bool already_selected = selected;
  442. selected = true;
  443. selected_focusable = p_index;
  444. update();
  445. if (!already_selected && selected) {
  446. emit_signal("selected", property, selected_focusable);
  447. }
  448. }
  449. void EditorProperty::add_focusable(Control *p_control) {
  450. p_control->connect("focus_entered", this, "_focusable_focused", varray(focusables.size()));
  451. focusables.push_back(p_control);
  452. }
  453. void EditorProperty::select(int p_focusable) {
  454. bool already_selected = selected;
  455. if (p_focusable >= 0) {
  456. ERR_FAIL_INDEX(p_focusable, focusables.size());
  457. focusables[p_focusable]->grab_focus();
  458. } else {
  459. selected = true;
  460. update();
  461. }
  462. if (!already_selected && selected) {
  463. emit_signal("selected", property, selected_focusable);
  464. }
  465. }
  466. void EditorProperty::deselect() {
  467. selected = false;
  468. selected_focusable = -1;
  469. update();
  470. }
  471. bool EditorProperty::is_selected() const {
  472. return selected;
  473. }
  474. void EditorProperty::_gui_input(const Ref<InputEvent> &p_event) {
  475. if (property == StringName())
  476. return;
  477. Ref<InputEventMouse> me = p_event;
  478. if (me.is_valid()) {
  479. bool button_left = me->get_button_mask() & BUTTON_MASK_LEFT;
  480. bool new_keying_hover = keying_rect.has_point(me->get_position()) && !button_left;
  481. if (new_keying_hover != keying_hover) {
  482. keying_hover = new_keying_hover;
  483. update();
  484. }
  485. bool new_revert_hover = revert_rect.has_point(me->get_position()) && !button_left;
  486. if (new_revert_hover != revert_hover) {
  487. revert_hover = new_revert_hover;
  488. update();
  489. }
  490. bool new_check_hover = check_rect.has_point(me->get_position()) && !button_left;
  491. if (new_check_hover != check_hover) {
  492. check_hover = new_check_hover;
  493. update();
  494. }
  495. }
  496. Ref<InputEventMouseButton> mb = p_event;
  497. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  498. if (!selected && selectable) {
  499. selected = true;
  500. emit_signal("selected", property, -1);
  501. update();
  502. }
  503. if (keying_rect.has_point(mb->get_position())) {
  504. emit_signal("property_keyed", property, use_keying_next());
  505. if (use_keying_next()) {
  506. call_deferred("emit_changed", property, object->get(property).operator int64_t() + 1, "", false);
  507. call_deferred("update_property");
  508. }
  509. }
  510. if (revert_rect.has_point(mb->get_position())) {
  511. Variant vorig;
  512. Node *node = Object::cast_to<Node>(object);
  513. if (node && EditorPropertyRevert::may_node_be_in_instance(node) && EditorPropertyRevert::get_instanced_node_original_property(node, property, vorig)) {
  514. emit_changed(property, vorig.duplicate(true));
  515. update_property();
  516. return;
  517. }
  518. if (object->call("property_can_revert", property).operator bool()) {
  519. Variant rev = object->call("property_get_revert", property);
  520. emit_changed(property, rev);
  521. update_property();
  522. return;
  523. }
  524. if (!object->get_script().is_null()) {
  525. Ref<Script> scr = object->get_script();
  526. if (scr.is_valid()) {
  527. Variant orig_value;
  528. if (scr->get_property_default_value(property, orig_value)) {
  529. emit_changed(property, orig_value);
  530. update_property();
  531. return;
  532. }
  533. }
  534. }
  535. Variant default_value = ClassDB::class_get_default_property_value(object->get_class_name(), property);
  536. if (default_value != Variant()) {
  537. emit_changed(property, default_value);
  538. update_property();
  539. return;
  540. }
  541. }
  542. if (check_rect.has_point(mb->get_position())) {
  543. checked = !checked;
  544. update();
  545. emit_signal("property_checked", property, checked);
  546. }
  547. }
  548. }
  549. void EditorProperty::set_label_reference(Control *p_control) {
  550. label_reference = p_control;
  551. }
  552. void EditorProperty::set_bottom_editor(Control *p_control) {
  553. bottom_editor = p_control;
  554. }
  555. Variant EditorProperty::get_drag_data(const Point2 &p_point) {
  556. if (property == StringName())
  557. return Variant();
  558. Dictionary dp;
  559. dp["type"] = "obj_property";
  560. dp["object"] = object;
  561. dp["property"] = property;
  562. dp["value"] = object->get(property);
  563. Label *label = memnew(Label);
  564. label->set_text(property);
  565. set_drag_preview(label);
  566. return dp;
  567. }
  568. void EditorProperty::set_use_folding(bool p_use_folding) {
  569. use_folding = p_use_folding;
  570. }
  571. bool EditorProperty::is_using_folding() const {
  572. return use_folding;
  573. }
  574. void EditorProperty::expand_all_folding() {
  575. }
  576. void EditorProperty::collapse_all_folding() {
  577. }
  578. void EditorProperty::set_selectable(bool p_selectable) {
  579. selectable = p_selectable;
  580. }
  581. bool EditorProperty::is_selectable() const {
  582. return selectable;
  583. }
  584. void EditorProperty::set_name_split_ratio(float p_ratio) {
  585. split_ratio = p_ratio;
  586. }
  587. float EditorProperty::get_name_split_ratio() const {
  588. return split_ratio;
  589. }
  590. void EditorProperty::set_object_and_property(Object *p_object, const StringName &p_property) {
  591. object = p_object;
  592. property = p_property;
  593. }
  594. Control *EditorProperty::make_custom_tooltip(const String &p_text) const {
  595. tooltip_text = p_text;
  596. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  597. help_bit->add_style_override("panel", get_stylebox("panel", "TooltipPanel"));
  598. help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
  599. String text = TTR("Property:") + " [u][b]" + p_text.get_slice("::", 0) + "[/b][/u]\n";
  600. text += p_text.get_slice("::", 1).strip_edges();
  601. help_bit->set_text(text);
  602. help_bit->call_deferred("set_text", text); //hack so it uses proper theme once inside scene
  603. return help_bit;
  604. }
  605. String EditorProperty::get_tooltip_text() const {
  606. return tooltip_text;
  607. }
  608. void EditorProperty::_bind_methods() {
  609. ClassDB::bind_method(D_METHOD("set_label", "text"), &EditorProperty::set_label);
  610. ClassDB::bind_method(D_METHOD("get_label"), &EditorProperty::get_label);
  611. ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorProperty::set_read_only);
  612. ClassDB::bind_method(D_METHOD("is_read_only"), &EditorProperty::is_read_only);
  613. ClassDB::bind_method(D_METHOD("set_checkable", "checkable"), &EditorProperty::set_checkable);
  614. ClassDB::bind_method(D_METHOD("is_checkable"), &EditorProperty::is_checkable);
  615. ClassDB::bind_method(D_METHOD("set_checked", "checked"), &EditorProperty::set_checked);
  616. ClassDB::bind_method(D_METHOD("is_checked"), &EditorProperty::is_checked);
  617. ClassDB::bind_method(D_METHOD("set_draw_red", "draw_red"), &EditorProperty::set_draw_red);
  618. ClassDB::bind_method(D_METHOD("is_draw_red"), &EditorProperty::is_draw_red);
  619. ClassDB::bind_method(D_METHOD("set_keying", "keying"), &EditorProperty::set_keying);
  620. ClassDB::bind_method(D_METHOD("is_keying"), &EditorProperty::is_keying);
  621. ClassDB::bind_method(D_METHOD("get_edited_property"), &EditorProperty::get_edited_property);
  622. ClassDB::bind_method(D_METHOD("get_edited_object"), &EditorProperty::get_edited_object);
  623. ClassDB::bind_method(D_METHOD("_gui_input"), &EditorProperty::_gui_input);
  624. ClassDB::bind_method(D_METHOD("_focusable_focused"), &EditorProperty::_focusable_focused);
  625. ClassDB::bind_method(D_METHOD("get_tooltip_text"), &EditorProperty::get_tooltip_text);
  626. ClassDB::bind_method(D_METHOD("add_focusable", "control"), &EditorProperty::add_focusable);
  627. ClassDB::bind_method(D_METHOD("set_bottom_editor", "editor"), &EditorProperty::set_bottom_editor);
  628. ClassDB::bind_method(D_METHOD("emit_changed", "property", "value", "field", "changing"), &EditorProperty::emit_changed, DEFVAL(StringName()), DEFVAL(false));
  629. ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
  630. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
  631. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checkable"), "set_checkable", "is_checkable");
  632. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checked"), "set_checked", "is_checked");
  633. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_red"), "set_draw_red", "is_draw_red");
  634. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keying"), "set_keying", "is_keying");
  635. ADD_SIGNAL(MethodInfo("property_changed", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  636. ADD_SIGNAL(MethodInfo("multiple_properties_changed", PropertyInfo(Variant::POOL_STRING_ARRAY, "properties"), PropertyInfo(Variant::ARRAY, "value")));
  637. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING, "property")));
  638. ADD_SIGNAL(MethodInfo("property_keyed_with_value", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  639. ADD_SIGNAL(MethodInfo("property_checked", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::STRING, "bool")));
  640. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  641. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::INT, "id")));
  642. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "focusable_idx")));
  643. MethodInfo vm;
  644. vm.name = "update_property";
  645. BIND_VMETHOD(vm);
  646. }
  647. EditorProperty::EditorProperty() {
  648. draw_top_bg = true;
  649. object = NULL;
  650. split_ratio = 0.5;
  651. selectable = true;
  652. text_size = 0;
  653. read_only = false;
  654. checkable = false;
  655. checked = false;
  656. draw_red = false;
  657. keying = false;
  658. keying_hover = false;
  659. revert_hover = false;
  660. check_hover = false;
  661. can_revert = false;
  662. use_folding = false;
  663. property_usage = 0;
  664. selected = false;
  665. selected_focusable = -1;
  666. label_reference = NULL;
  667. bottom_editor = NULL;
  668. }
  669. ////////////////////////////////////////////////
  670. ////////////////////////////////////////////////
  671. void EditorInspectorPlugin::add_custom_control(Control *control) {
  672. AddedEditor ae;
  673. ae.property_editor = control;
  674. added_editors.push_back(ae);
  675. }
  676. void EditorInspectorPlugin::add_property_editor(const String &p_for_property, Control *p_prop) {
  677. ERR_FAIL_COND(Object::cast_to<EditorProperty>(p_prop) == NULL);
  678. AddedEditor ae;
  679. ae.properties.push_back(p_for_property);
  680. ae.property_editor = p_prop;
  681. added_editors.push_back(ae);
  682. }
  683. void EditorInspectorPlugin::add_property_editor_for_multiple_properties(const String &p_label, const Vector<String> &p_properties, Control *p_prop) {
  684. AddedEditor ae;
  685. ae.properties = p_properties;
  686. ae.property_editor = p_prop;
  687. ae.label = p_label;
  688. added_editors.push_back(ae);
  689. }
  690. bool EditorInspectorPlugin::can_handle(Object *p_object) {
  691. if (get_script_instance()) {
  692. return get_script_instance()->call("can_handle", p_object);
  693. }
  694. return false;
  695. }
  696. void EditorInspectorPlugin::parse_begin(Object *p_object) {
  697. if (get_script_instance()) {
  698. get_script_instance()->call("parse_begin", p_object);
  699. }
  700. }
  701. void EditorInspectorPlugin::parse_category(Object *p_object, const String &p_parse_category) {
  702. if (get_script_instance()) {
  703. get_script_instance()->call("parse_category", p_object, p_parse_category);
  704. }
  705. }
  706. bool EditorInspectorPlugin::parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage) {
  707. if (get_script_instance()) {
  708. Variant arg[6] = {
  709. p_object, p_type, p_path, p_hint, p_hint_text, p_usage
  710. };
  711. const Variant *argptr[6] = {
  712. &arg[0], &arg[1], &arg[2], &arg[3], &arg[4], &arg[5]
  713. };
  714. Variant::CallError err;
  715. return get_script_instance()->call("parse_property", (const Variant **)&argptr, 6, err);
  716. }
  717. return false;
  718. }
  719. void EditorInspectorPlugin::parse_end() {
  720. if (get_script_instance()) {
  721. get_script_instance()->call("parse_end");
  722. }
  723. }
  724. void EditorInspectorPlugin::_bind_methods() {
  725. ClassDB::bind_method(D_METHOD("add_custom_control", "control"), &EditorInspectorPlugin::add_custom_control);
  726. ClassDB::bind_method(D_METHOD("add_property_editor", "property", "editor"), &EditorInspectorPlugin::add_property_editor);
  727. ClassDB::bind_method(D_METHOD("add_property_editor_for_multiple_properties", "label", "properties", "editor"), &EditorInspectorPlugin::add_property_editor_for_multiple_properties);
  728. MethodInfo vm;
  729. vm.name = "can_handle";
  730. vm.return_val.type = Variant::BOOL;
  731. vm.arguments.push_back(PropertyInfo(Variant::OBJECT, "object"));
  732. BIND_VMETHOD(vm);
  733. vm.name = "parse_begin";
  734. vm.return_val.type = Variant::NIL;
  735. BIND_VMETHOD(vm);
  736. vm.name = "parse_category";
  737. vm.arguments.push_back(PropertyInfo(Variant::STRING, "category"));
  738. BIND_VMETHOD(vm);
  739. vm.arguments.pop_back();
  740. vm.name = "parse_property";
  741. vm.return_val.type = Variant::BOOL;
  742. vm.arguments.push_back(PropertyInfo(Variant::INT, "type"));
  743. vm.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  744. vm.arguments.push_back(PropertyInfo(Variant::INT, "hint"));
  745. vm.arguments.push_back(PropertyInfo(Variant::STRING, "hint_text"));
  746. vm.arguments.push_back(PropertyInfo(Variant::INT, "usage"));
  747. BIND_VMETHOD(vm);
  748. vm.arguments.clear();
  749. vm.name = "parse_end";
  750. vm.return_val.type = Variant::NIL;
  751. BIND_VMETHOD(vm);
  752. }
  753. ////////////////////////////////////////////////
  754. ////////////////////////////////////////////////
  755. void EditorInspectorCategory::_notification(int p_what) {
  756. if (p_what == NOTIFICATION_DRAW) {
  757. draw_rect(Rect2(Vector2(), get_size()), bg_color);
  758. Ref<Font> font = get_font("font", "Tree");
  759. int hs = get_constant("hseparation", "Tree");
  760. int w = font->get_string_size(label).width;
  761. if (icon.is_valid()) {
  762. w += hs + icon->get_width();
  763. }
  764. int ofs = (get_size().width - w) / 2;
  765. if (icon.is_valid()) {
  766. draw_texture(icon, Point2(ofs, (get_size().height - icon->get_height()) / 2).floor());
  767. ofs += hs + icon->get_width();
  768. }
  769. Color color = get_color("font_color", "Tree");
  770. draw_string(font, Point2(ofs, font->get_ascent() + (get_size().height - font->get_height()) / 2).floor(), label, color, get_size().width);
  771. }
  772. }
  773. Control *EditorInspectorCategory::make_custom_tooltip(const String &p_text) const {
  774. tooltip_text = p_text;
  775. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  776. help_bit->add_style_override("panel", get_stylebox("panel", "TooltipPanel"));
  777. help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
  778. String text = "[u][b]" + p_text.get_slice("::", 0) + "[/b][/u]\n";
  779. text += p_text.get_slice("::", 1).strip_edges();
  780. help_bit->set_text(text);
  781. help_bit->call_deferred("set_text", text); //hack so it uses proper theme once inside scene
  782. return help_bit;
  783. }
  784. Size2 EditorInspectorCategory::get_minimum_size() const {
  785. Ref<Font> font = get_font("font", "Tree");
  786. Size2 ms;
  787. ms.width = 1;
  788. ms.height = font->get_height();
  789. if (icon.is_valid()) {
  790. ms.height = MAX(icon->get_height(), ms.height);
  791. }
  792. ms.height += get_constant("vseparation", "Tree");
  793. return ms;
  794. }
  795. void EditorInspectorCategory::_bind_methods() {
  796. ClassDB::bind_method(D_METHOD("get_tooltip_text"), &EditorInspectorCategory::get_tooltip_text);
  797. }
  798. String EditorInspectorCategory::get_tooltip_text() const {
  799. return tooltip_text;
  800. }
  801. EditorInspectorCategory::EditorInspectorCategory() {
  802. }
  803. ////////////////////////////////////////////////
  804. ////////////////////////////////////////////////
  805. void EditorInspectorSection::_test_unfold() {
  806. if (!vbox_added) {
  807. add_child(vbox);
  808. vbox_added = true;
  809. }
  810. }
  811. void EditorInspectorSection::_notification(int p_what) {
  812. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  813. Ref<Font> font = get_font("font", "Tree");
  814. Ref<Texture> arrow;
  815. #ifdef TOOLS_ENABLED
  816. if (foldable) {
  817. if (object->editor_is_section_unfolded(section)) {
  818. arrow = get_icon("arrow_up", "Tree");
  819. } else {
  820. arrow = get_icon("arrow", "Tree");
  821. }
  822. }
  823. #endif
  824. Size2 size = get_size();
  825. Point2 offset;
  826. offset.y = font->get_height();
  827. if (arrow.is_valid()) {
  828. offset.y = MAX(offset.y, arrow->get_height());
  829. }
  830. offset.y += get_constant("vseparation", "Tree");
  831. offset.x += get_constant("inspector_margin", "Editor");
  832. Rect2 rect(offset, size - offset);
  833. //set children
  834. for (int i = 0; i < get_child_count(); i++) {
  835. Control *c = Object::cast_to<Control>(get_child(i));
  836. if (!c)
  837. continue;
  838. if (c->is_set_as_toplevel())
  839. continue;
  840. if (!c->is_visible_in_tree())
  841. continue;
  842. fit_child_in_rect(c, rect);
  843. }
  844. update(); //need to redraw text
  845. }
  846. if (p_what == NOTIFICATION_DRAW) {
  847. Ref<Texture> arrow;
  848. #ifdef TOOLS_ENABLED
  849. if (foldable) {
  850. if (object->editor_is_section_unfolded(section)) {
  851. arrow = get_icon("arrow_up", "Tree");
  852. } else {
  853. arrow = get_icon("arrow", "Tree");
  854. }
  855. }
  856. #endif
  857. Ref<Font> font = get_font("font", "Tree");
  858. int h = font->get_height();
  859. if (arrow.is_valid()) {
  860. h = MAX(h, arrow->get_height());
  861. }
  862. h += get_constant("vseparation", "Tree");
  863. draw_rect(Rect2(Vector2(), Vector2(get_size().width, h)), bg_color);
  864. int hs = get_constant("hseparation", "Tree");
  865. Color color = get_color("font_color", "Tree");
  866. draw_string(font, Point2(hs, font->get_ascent() + (h - font->get_height()) / 2).floor(), label, color, get_size().width);
  867. if (arrow.is_valid()) {
  868. draw_texture(arrow, Point2(get_size().width - arrow->get_width(), (h - arrow->get_height()) / 2).floor());
  869. }
  870. }
  871. }
  872. Size2 EditorInspectorSection::get_minimum_size() const {
  873. Size2 ms;
  874. for (int i = 0; i < get_child_count(); i++) {
  875. Control *c = Object::cast_to<Control>(get_child(i));
  876. if (!c)
  877. continue;
  878. if (c->is_set_as_toplevel())
  879. continue;
  880. if (!c->is_visible())
  881. continue;
  882. Size2 minsize = c->get_combined_minimum_size();
  883. ms.width = MAX(ms.width, minsize.width);
  884. ms.height = MAX(ms.height, minsize.height);
  885. }
  886. Ref<Font> font = get_font("font", "Tree");
  887. ms.height += font->get_height() + get_constant("vseparation", "Tree");
  888. ms.width += get_constant("inspector_margin", "Editor");
  889. return ms;
  890. }
  891. void EditorInspectorSection::setup(const String &p_section, const String &p_label, Object *p_object, const Color &p_bg_color, bool p_foldable) {
  892. section = p_section;
  893. label = p_label;
  894. object = p_object;
  895. bg_color = p_bg_color;
  896. foldable = p_foldable;
  897. if (!foldable && !vbox_added) {
  898. add_child(vbox);
  899. vbox_added = true;
  900. }
  901. #ifdef TOOLS_ENABLED
  902. if (foldable) {
  903. _test_unfold();
  904. if (object->editor_is_section_unfolded(section)) {
  905. vbox->show();
  906. } else {
  907. vbox->hide();
  908. }
  909. }
  910. #endif
  911. }
  912. void EditorInspectorSection::_gui_input(const Ref<InputEvent> &p_event) {
  913. if (!foldable)
  914. return;
  915. #ifdef TOOLS_ENABLED
  916. Ref<InputEventMouseButton> mb = p_event;
  917. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  918. Ref<Font> font = get_font("font", "Tree");
  919. if (mb->get_position().y > font->get_height()) { //clicked outside
  920. return;
  921. }
  922. _test_unfold();
  923. bool unfold = !object->editor_is_section_unfolded(section);
  924. object->editor_set_section_unfold(section, unfold);
  925. if (unfold) {
  926. vbox->show();
  927. } else {
  928. vbox->hide();
  929. }
  930. }
  931. #endif
  932. }
  933. VBoxContainer *EditorInspectorSection::get_vbox() {
  934. return vbox;
  935. }
  936. void EditorInspectorSection::unfold() {
  937. if (!foldable)
  938. return;
  939. _test_unfold();
  940. #ifdef TOOLS_ENABLED
  941. object->editor_set_section_unfold(section, true);
  942. vbox->show();
  943. update();
  944. #endif
  945. }
  946. void EditorInspectorSection::fold() {
  947. if (!foldable)
  948. return;
  949. if (!vbox_added)
  950. return; //kinda pointless
  951. #ifdef TOOLS_ENABLED
  952. object->editor_set_section_unfold(section, false);
  953. vbox->hide();
  954. update();
  955. #endif
  956. }
  957. void EditorInspectorSection::_bind_methods() {
  958. ClassDB::bind_method(D_METHOD("setup", "section", "label", "object", "bg_color", "foldable"), &EditorInspectorSection::setup);
  959. ClassDB::bind_method(D_METHOD("get_vbox"), &EditorInspectorSection::get_vbox);
  960. ClassDB::bind_method(D_METHOD("unfold"), &EditorInspectorSection::unfold);
  961. ClassDB::bind_method(D_METHOD("fold"), &EditorInspectorSection::fold);
  962. ClassDB::bind_method(D_METHOD("_gui_input"), &EditorInspectorSection::_gui_input);
  963. }
  964. EditorInspectorSection::EditorInspectorSection() {
  965. object = NULL;
  966. foldable = false;
  967. vbox = memnew(VBoxContainer);
  968. vbox_added = false;
  969. }
  970. EditorInspectorSection::~EditorInspectorSection() {
  971. if (!vbox_added) {
  972. memdelete(vbox);
  973. }
  974. }
  975. ////////////////////////////////////////////////
  976. ////////////////////////////////////////////////
  977. Ref<EditorInspectorPlugin> EditorInspector::inspector_plugins[MAX_PLUGINS];
  978. int EditorInspector::inspector_plugin_count = 0;
  979. EditorProperty *EditorInspector::instantiate_property_editor(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage) {
  980. for (int i = inspector_plugin_count - 1; i >= 0; i--) {
  981. inspector_plugins[i]->parse_property(p_object, p_type, p_path, p_hint, p_hint_text, p_usage);
  982. if (inspector_plugins[i]->added_editors.size()) {
  983. for (int j = 1; j < inspector_plugins[i]->added_editors.size(); j++) { //only keep first one
  984. memdelete(inspector_plugins[i]->added_editors[j].property_editor);
  985. }
  986. EditorProperty *prop = Object::cast_to<EditorProperty>(inspector_plugins[i]->added_editors[0].property_editor);
  987. if (prop) {
  988. inspector_plugins[i]->added_editors.clear();
  989. return prop;
  990. } else {
  991. memdelete(inspector_plugins[i]->added_editors[0].property_editor);
  992. inspector_plugins[i]->added_editors.clear();
  993. }
  994. }
  995. }
  996. return NULL;
  997. }
  998. void EditorInspector::add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  999. ERR_FAIL_COND(inspector_plugin_count == MAX_PLUGINS);
  1000. for (int i = 0; i < inspector_plugin_count; i++) {
  1001. if (inspector_plugins[i] == p_plugin)
  1002. return; //already exists
  1003. }
  1004. inspector_plugins[inspector_plugin_count++] = p_plugin;
  1005. }
  1006. void EditorInspector::remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  1007. ERR_FAIL_COND(inspector_plugin_count == MAX_PLUGINS);
  1008. int idx = -1;
  1009. for (int i = 0; i < inspector_plugin_count; i++) {
  1010. if (inspector_plugins[i] == p_plugin) {
  1011. idx = i;
  1012. break;
  1013. }
  1014. }
  1015. for (int i = idx; i < inspector_plugin_count - 1; i++) {
  1016. inspector_plugins[i] = inspector_plugins[i + 1];
  1017. }
  1018. if (idx == inspector_plugin_count - 1)
  1019. inspector_plugins[idx] = Ref<EditorInspectorPlugin>();
  1020. inspector_plugin_count--;
  1021. }
  1022. void EditorInspector::cleanup_plugins() {
  1023. for (int i = 0; i < inspector_plugin_count; i++) {
  1024. inspector_plugins[i].unref();
  1025. }
  1026. inspector_plugin_count = 0;
  1027. }
  1028. void EditorInspector::set_undo_redo(UndoRedo *p_undo_redo) {
  1029. undo_redo = p_undo_redo;
  1030. }
  1031. String EditorInspector::get_selected_path() const {
  1032. return property_selected;
  1033. }
  1034. void EditorInspector::_parse_added_editors(VBoxContainer *current_vbox, Ref<EditorInspectorPlugin> ped) {
  1035. for (List<EditorInspectorPlugin::AddedEditor>::Element *F = ped->added_editors.front(); F; F = F->next()) {
  1036. EditorProperty *ep = Object::cast_to<EditorProperty>(F->get().property_editor);
  1037. current_vbox->add_child(F->get().property_editor);
  1038. if (ep) {
  1039. ep->object = object;
  1040. ep->connect("property_changed", this, "_property_changed");
  1041. ep->connect("property_keyed", this, "_property_keyed");
  1042. ep->connect("property_keyed_with_value", this, "_property_keyed_with_value");
  1043. ep->connect("property_checked", this, "_property_checked");
  1044. ep->connect("selected", this, "_property_selected");
  1045. ep->connect("multiple_properties_changed", this, "_multiple_properties_changed");
  1046. ep->connect("resource_selected", this, "_resource_selected", varray(), CONNECT_DEFERRED);
  1047. ep->connect("object_id_selected", this, "_object_id_selected", varray(), CONNECT_DEFERRED);
  1048. if (F->get().properties.size()) {
  1049. if (F->get().properties.size() == 1) {
  1050. //since it's one, associate:
  1051. ep->property = F->get().properties[0];
  1052. ep->property_usage = 0;
  1053. }
  1054. if (F->get().label != String()) {
  1055. ep->set_label(F->get().label);
  1056. }
  1057. for (int i = 0; i < F->get().properties.size(); i++) {
  1058. String prop = F->get().properties[i];
  1059. if (!editor_property_map.has(prop)) {
  1060. editor_property_map[prop] = List<EditorProperty *>();
  1061. }
  1062. editor_property_map[prop].push_back(ep);
  1063. }
  1064. }
  1065. ep->set_read_only(read_only);
  1066. ep->update_property();
  1067. ep->update_reload_status();
  1068. }
  1069. }
  1070. ped->added_editors.clear();
  1071. }
  1072. bool EditorInspector::_is_property_disabled_by_feature_profile(const StringName &p_property) {
  1073. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  1074. if (profile.is_null()) {
  1075. return false;
  1076. }
  1077. StringName class_name = object->get_class();
  1078. while (class_name != StringName()) {
  1079. if (profile->is_class_property_disabled(class_name, p_property)) {
  1080. return true;
  1081. }
  1082. if (profile->is_class_disabled(class_name)) {
  1083. //won't see properties of a disabled class
  1084. return true;
  1085. }
  1086. class_name = ClassDB::get_parent_class(class_name);
  1087. }
  1088. return false;
  1089. }
  1090. void EditorInspector::update_tree() {
  1091. //to update properly if all is refreshed
  1092. StringName current_selected = property_selected;
  1093. int current_focusable = -1;
  1094. if (property_focusable != -1) {
  1095. //check focusable is really focusable
  1096. bool restore_focus = false;
  1097. Control *focused = get_focus_owner();
  1098. if (focused) {
  1099. Node *parent = focused->get_parent();
  1100. while (parent) {
  1101. EditorInspector *inspector = Object::cast_to<EditorInspector>(parent);
  1102. if (inspector) {
  1103. restore_focus = inspector == this; //may be owned by another inspector
  1104. break; //exit after the first inspector is found, since there may be nested ones
  1105. }
  1106. parent = parent->get_parent();
  1107. }
  1108. }
  1109. if (restore_focus) {
  1110. current_focusable = property_focusable;
  1111. }
  1112. }
  1113. _clear();
  1114. if (!object)
  1115. return;
  1116. List<Ref<EditorInspectorPlugin> > valid_plugins;
  1117. for (int i = inspector_plugin_count - 1; i >= 0; i--) { //start by last, so lastly added can override newly added
  1118. if (!inspector_plugins[i]->can_handle(object))
  1119. continue;
  1120. valid_plugins.push_back(inspector_plugins[i]);
  1121. }
  1122. bool draw_red = false;
  1123. {
  1124. Node *nod = Object::cast_to<Node>(object);
  1125. Node *es = EditorNode::get_singleton()->get_edited_scene();
  1126. if (nod && es != nod && nod->get_owner() != es) {
  1127. draw_red = true;
  1128. }
  1129. }
  1130. // TreeItem *current_category = NULL;
  1131. String filter = search_box ? search_box->get_text() : "";
  1132. String group;
  1133. String group_base;
  1134. VBoxContainer *category_vbox = NULL;
  1135. List<PropertyInfo>
  1136. plist;
  1137. object->get_property_list(&plist, true);
  1138. HashMap<String, VBoxContainer *> item_path;
  1139. Map<VBoxContainer *, EditorInspectorSection *> section_map;
  1140. item_path[""] = main_vbox;
  1141. Color sscolor = get_color("prop_subsection", "Editor");
  1142. for (List<Ref<EditorInspectorPlugin> >::Element *E = valid_plugins.front(); E; E = E->next()) {
  1143. Ref<EditorInspectorPlugin> ped = E->get();
  1144. ped->parse_begin(object);
  1145. _parse_added_editors(main_vbox, ped);
  1146. }
  1147. for (List<PropertyInfo>::Element *I = plist.front(); I; I = I->next()) {
  1148. PropertyInfo &p = I->get();
  1149. //make sure the property can be edited
  1150. if (p.usage & PROPERTY_USAGE_GROUP) {
  1151. group = p.name;
  1152. group_base = p.hint_string;
  1153. continue;
  1154. } else if (p.usage & PROPERTY_USAGE_CATEGORY) {
  1155. group = "";
  1156. group_base = "";
  1157. if (!show_categories)
  1158. continue;
  1159. List<PropertyInfo>::Element *N = I->next();
  1160. bool valid = true;
  1161. //if no properties in category, skip
  1162. while (N) {
  1163. if (N->get().usage & PROPERTY_USAGE_EDITOR)
  1164. break;
  1165. if (N->get().usage & PROPERTY_USAGE_CATEGORY) {
  1166. valid = false;
  1167. break;
  1168. }
  1169. N = N->next();
  1170. }
  1171. if (!valid)
  1172. continue; //empty, ignore
  1173. EditorInspectorCategory *category = memnew(EditorInspectorCategory);
  1174. main_vbox->add_child(category);
  1175. category_vbox = NULL; //reset
  1176. String type = p.name;
  1177. category->icon = EditorNode::get_singleton()->get_class_icon(type, "Object");
  1178. category->label = type;
  1179. category->bg_color = get_color("prop_category", "Editor");
  1180. if (use_doc_hints) {
  1181. StringName type2 = p.name;
  1182. if (!class_descr_cache.has(type2)) {
  1183. String descr;
  1184. DocData *dd = EditorHelp::get_doc_data();
  1185. Map<String, DocData::ClassDoc>::Element *E = dd->class_list.find(type2);
  1186. if (E) {
  1187. descr = E->get().brief_description;
  1188. }
  1189. class_descr_cache[type2] = descr;
  1190. }
  1191. category->set_tooltip(p.name + "::" + (class_descr_cache[type2] == "" ? "" : class_descr_cache[type2]));
  1192. }
  1193. for (List<Ref<EditorInspectorPlugin> >::Element *E = valid_plugins.front(); E; E = E->next()) {
  1194. Ref<EditorInspectorPlugin> ped = E->get();
  1195. ped->parse_category(object, p.name);
  1196. _parse_added_editors(main_vbox, ped);
  1197. }
  1198. continue;
  1199. } else if (!(p.usage & PROPERTY_USAGE_EDITOR) || _is_property_disabled_by_feature_profile(p.name))
  1200. continue;
  1201. if (p.usage & PROPERTY_USAGE_HIGH_END_GFX && VS::get_singleton()->is_low_end())
  1202. continue; //do not show this property in low end gfx
  1203. if (p.name == "script" && (hide_script || bool(object->call("_hide_script_from_inspector")))) {
  1204. continue;
  1205. }
  1206. String basename = p.name;
  1207. if (group != "") {
  1208. if (group_base != "") {
  1209. if (basename.begins_with(group_base)) {
  1210. basename = basename.replace_first(group_base, "");
  1211. } else if (group_base.begins_with(basename)) {
  1212. //keep it, this is used pretty often
  1213. } else {
  1214. group = ""; //no longer using group base, clear
  1215. }
  1216. }
  1217. }
  1218. if (group != "") {
  1219. basename = group + "/" + basename;
  1220. }
  1221. String name = (basename.find("/") != -1) ? basename.right(basename.find_last("/") + 1) : basename;
  1222. if (capitalize_paths) {
  1223. int dot = name.find(".");
  1224. if (dot != -1) {
  1225. String ov = name.right(dot);
  1226. name = name.substr(0, dot);
  1227. name = name.camelcase_to_underscore().capitalize();
  1228. name += ov;
  1229. } else {
  1230. name = name.camelcase_to_underscore().capitalize();
  1231. }
  1232. }
  1233. String path = basename.left(basename.find_last("/"));
  1234. if (use_filter && filter != "") {
  1235. String cat = path;
  1236. if (capitalize_paths)
  1237. cat = cat.capitalize();
  1238. if (!filter.is_subsequence_ofi(cat) && !filter.is_subsequence_ofi(name))
  1239. continue;
  1240. }
  1241. if (category_vbox == NULL) {
  1242. category_vbox = memnew(VBoxContainer);
  1243. main_vbox->add_child(category_vbox);
  1244. }
  1245. VBoxContainer *current_vbox = main_vbox;
  1246. {
  1247. String acc_path = "";
  1248. int level = 1;
  1249. for (int i = 0; i < path.get_slice_count("/"); i++) {
  1250. String path_name = path.get_slice("/", i);
  1251. if (i > 0)
  1252. acc_path += "/";
  1253. acc_path += path_name;
  1254. if (!item_path.has(acc_path)) {
  1255. EditorInspectorSection *section = memnew(EditorInspectorSection);
  1256. current_vbox->add_child(section);
  1257. sections.push_back(section);
  1258. if (capitalize_paths)
  1259. path_name = path_name.capitalize();
  1260. Color c = sscolor;
  1261. c.a /= level;
  1262. section->setup(acc_path, path_name, object, c, use_folding);
  1263. VBoxContainer *vb = section->get_vbox();
  1264. item_path[acc_path] = vb;
  1265. section_map[vb] = section;
  1266. }
  1267. current_vbox = item_path[acc_path];
  1268. level = (MIN(level + 1, 4));
  1269. }
  1270. if (current_vbox == main_vbox) {
  1271. //do not add directly to the main vbox, given it has no spacing
  1272. if (category_vbox == NULL) {
  1273. category_vbox = memnew(VBoxContainer);
  1274. }
  1275. current_vbox = category_vbox;
  1276. }
  1277. }
  1278. bool checkable = false;
  1279. bool checked = false;
  1280. if (p.usage & PROPERTY_USAGE_CHECKABLE) {
  1281. checkable = true;
  1282. checked = p.usage & PROPERTY_USAGE_CHECKED;
  1283. }
  1284. if (p.usage & PROPERTY_USAGE_RESTART_IF_CHANGED) {
  1285. restart_request_props.insert(p.name);
  1286. }
  1287. String doc_hint;
  1288. if (use_doc_hints) {
  1289. StringName classname = object->get_class_name();
  1290. if (object_class != String()) {
  1291. classname = object_class;
  1292. }
  1293. StringName propname = property_prefix + p.name;
  1294. String descr;
  1295. bool found = false;
  1296. Map<StringName, Map<StringName, String> >::Element *E = descr_cache.find(classname);
  1297. if (E) {
  1298. Map<StringName, String>::Element *F = E->get().find(propname);
  1299. if (F) {
  1300. found = true;
  1301. descr = F->get();
  1302. }
  1303. }
  1304. if (!found) {
  1305. DocData *dd = EditorHelp::get_doc_data();
  1306. Map<String, DocData::ClassDoc>::Element *F = dd->class_list.find(classname);
  1307. while (F && descr == String()) {
  1308. for (int i = 0; i < F->get().properties.size(); i++) {
  1309. if (F->get().properties[i].name == propname.operator String()) {
  1310. descr = F->get().properties[i].description.strip_edges();
  1311. break;
  1312. }
  1313. }
  1314. if (!F->get().inherits.empty()) {
  1315. F = dd->class_list.find(F->get().inherits);
  1316. } else {
  1317. break;
  1318. }
  1319. }
  1320. descr_cache[classname][propname] = descr;
  1321. }
  1322. doc_hint = descr;
  1323. }
  1324. for (List<Ref<EditorInspectorPlugin> >::Element *E = valid_plugins.front(); E; E = E->next()) {
  1325. Ref<EditorInspectorPlugin> ped = E->get();
  1326. bool exclusive = ped->parse_property(object, p.type, p.name, p.hint, p.hint_string, p.usage);
  1327. List<EditorInspectorPlugin::AddedEditor> editors = ped->added_editors; //make a copy, since plugins may be used again in a sub-inspector
  1328. ped->added_editors.clear();
  1329. for (List<EditorInspectorPlugin::AddedEditor>::Element *F = editors.front(); F; F = F->next()) {
  1330. EditorProperty *ep = Object::cast_to<EditorProperty>(F->get().property_editor);
  1331. if (ep) {
  1332. //set all this before the control gets the ENTER_TREE notification
  1333. ep->object = object;
  1334. if (F->get().properties.size()) {
  1335. if (F->get().properties.size() == 1) {
  1336. //since it's one, associate:
  1337. ep->property = F->get().properties[0];
  1338. ep->property_usage = p.usage;
  1339. //and set label?
  1340. }
  1341. if (F->get().label != String()) {
  1342. ep->set_label(F->get().label);
  1343. } else {
  1344. //use existin one
  1345. ep->set_label(name);
  1346. }
  1347. for (int i = 0; i < F->get().properties.size(); i++) {
  1348. String prop = F->get().properties[i];
  1349. if (!editor_property_map.has(prop)) {
  1350. editor_property_map[prop] = List<EditorProperty *>();
  1351. }
  1352. editor_property_map[prop].push_back(ep);
  1353. }
  1354. }
  1355. ep->set_draw_red(draw_red);
  1356. ep->set_use_folding(use_folding);
  1357. ep->set_checkable(checkable);
  1358. ep->set_checked(checked);
  1359. ep->set_keying(keying);
  1360. ep->set_read_only(read_only);
  1361. }
  1362. current_vbox->add_child(F->get().property_editor);
  1363. if (ep) {
  1364. ep->connect("property_changed", this, "_property_changed");
  1365. if (p.usage & PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED) {
  1366. ep->connect("property_changed", this, "_property_changed_update_all", varray(), CONNECT_DEFERRED);
  1367. }
  1368. ep->connect("property_keyed", this, "_property_keyed");
  1369. ep->connect("property_keyed_with_value", this, "_property_keyed_with_value");
  1370. ep->connect("property_checked", this, "_property_checked");
  1371. ep->connect("selected", this, "_property_selected");
  1372. ep->connect("multiple_properties_changed", this, "_multiple_properties_changed");
  1373. ep->connect("resource_selected", this, "_resource_selected", varray(), CONNECT_DEFERRED);
  1374. ep->connect("object_id_selected", this, "_object_id_selected", varray(), CONNECT_DEFERRED);
  1375. if (doc_hint != String()) {
  1376. ep->set_tooltip(property_prefix + p.name + "::" + doc_hint);
  1377. } else {
  1378. ep->set_tooltip(property_prefix + p.name);
  1379. }
  1380. ep->update_property();
  1381. ep->update_reload_status();
  1382. if (current_selected && ep->property == current_selected) {
  1383. ep->select(current_focusable);
  1384. }
  1385. }
  1386. }
  1387. if (exclusive) {
  1388. break;
  1389. }
  1390. }
  1391. }
  1392. for (List<Ref<EditorInspectorPlugin> >::Element *E = valid_plugins.front(); E; E = E->next()) {
  1393. Ref<EditorInspectorPlugin> ped = E->get();
  1394. ped->parse_end();
  1395. _parse_added_editors(main_vbox, ped);
  1396. }
  1397. //see if this property exists and should be kept
  1398. }
  1399. void EditorInspector::update_property(const String &p_prop) {
  1400. if (!editor_property_map.has(p_prop))
  1401. return;
  1402. for (List<EditorProperty *>::Element *E = editor_property_map[p_prop].front(); E; E = E->next()) {
  1403. E->get()->update_property();
  1404. E->get()->update_reload_status();
  1405. }
  1406. }
  1407. void EditorInspector::_clear() {
  1408. while (main_vbox->get_child_count()) {
  1409. memdelete(main_vbox->get_child(0));
  1410. }
  1411. property_selected = StringName();
  1412. property_focusable = -1;
  1413. editor_property_map.clear();
  1414. sections.clear();
  1415. pending.clear();
  1416. restart_request_props.clear();
  1417. }
  1418. void EditorInspector::refresh() {
  1419. if (refresh_countdown > 0 || changing)
  1420. return;
  1421. refresh_countdown = EditorSettings::get_singleton()->get("docks/property_editor/auto_refresh_interval");
  1422. }
  1423. Object *EditorInspector::get_edited_object() {
  1424. return object;
  1425. }
  1426. void EditorInspector::edit(Object *p_object) {
  1427. if (object == p_object)
  1428. return;
  1429. if (object) {
  1430. _clear();
  1431. object->remove_change_receptor(this);
  1432. }
  1433. object = p_object;
  1434. if (object) {
  1435. update_scroll_request = 0; //reset
  1436. if (scroll_cache.has(object->get_instance_id())) { //if exists, set something else
  1437. update_scroll_request = scroll_cache[object->get_instance_id()]; //done this way because wait until full size is accommodated
  1438. }
  1439. object->add_change_receptor(this);
  1440. update_tree();
  1441. }
  1442. }
  1443. void EditorInspector::set_keying(bool p_active) {
  1444. if (keying == p_active)
  1445. return;
  1446. keying = p_active;
  1447. update_tree();
  1448. }
  1449. void EditorInspector::set_read_only(bool p_read_only) {
  1450. read_only = p_read_only;
  1451. update_tree();
  1452. }
  1453. bool EditorInspector::is_capitalize_paths_enabled() const {
  1454. return capitalize_paths;
  1455. }
  1456. void EditorInspector::set_enable_capitalize_paths(bool p_capitalize) {
  1457. capitalize_paths = p_capitalize;
  1458. update_tree();
  1459. }
  1460. void EditorInspector::set_autoclear(bool p_enable) {
  1461. autoclear = p_enable;
  1462. }
  1463. void EditorInspector::set_show_categories(bool p_show) {
  1464. show_categories = p_show;
  1465. update_tree();
  1466. }
  1467. void EditorInspector::set_use_doc_hints(bool p_enable) {
  1468. use_doc_hints = p_enable;
  1469. update_tree();
  1470. }
  1471. void EditorInspector::set_hide_script(bool p_hide) {
  1472. hide_script = p_hide;
  1473. update_tree();
  1474. }
  1475. void EditorInspector::set_use_filter(bool p_use) {
  1476. use_filter = p_use;
  1477. update_tree();
  1478. }
  1479. void EditorInspector::register_text_enter(Node *p_line_edit) {
  1480. search_box = Object::cast_to<LineEdit>(p_line_edit);
  1481. if (search_box)
  1482. search_box->connect("text_changed", this, "_filter_changed");
  1483. }
  1484. void EditorInspector::_filter_changed(const String &p_text) {
  1485. _clear();
  1486. update_tree();
  1487. }
  1488. void EditorInspector::set_use_folding(bool p_enable) {
  1489. use_folding = p_enable;
  1490. update_tree();
  1491. }
  1492. bool EditorInspector::is_using_folding() {
  1493. return use_folding;
  1494. }
  1495. void EditorInspector::collapse_all_folding() {
  1496. for (List<EditorInspectorSection *>::Element *E = sections.front(); E; E = E->next()) {
  1497. E->get()->fold();
  1498. }
  1499. for (Map<StringName, List<EditorProperty *> >::Element *F = editor_property_map.front(); F; F = F->next()) {
  1500. for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) {
  1501. E->get()->collapse_all_folding();
  1502. }
  1503. }
  1504. }
  1505. void EditorInspector::expand_all_folding() {
  1506. for (List<EditorInspectorSection *>::Element *E = sections.front(); E; E = E->next()) {
  1507. E->get()->unfold();
  1508. }
  1509. for (Map<StringName, List<EditorProperty *> >::Element *F = editor_property_map.front(); F; F = F->next()) {
  1510. for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) {
  1511. E->get()->expand_all_folding();
  1512. }
  1513. }
  1514. }
  1515. void EditorInspector::set_scroll_offset(int p_offset) {
  1516. set_v_scroll(p_offset);
  1517. }
  1518. int EditorInspector::get_scroll_offset() const {
  1519. return get_v_scroll();
  1520. }
  1521. void EditorInspector::set_sub_inspector(bool p_enable) {
  1522. sub_inspector = p_enable;
  1523. if (!is_inside_tree())
  1524. return;
  1525. if (sub_inspector) {
  1526. add_style_override("bg", get_stylebox("sub_inspector_bg", "Editor"));
  1527. } else {
  1528. add_style_override("bg", get_stylebox("bg", "Tree"));
  1529. }
  1530. }
  1531. void EditorInspector::_edit_request_change(Object *p_object, const String &p_property) {
  1532. if (object != p_object) //may be undoing/redoing for a non edited object, so ignore
  1533. return;
  1534. if (changing)
  1535. return;
  1536. if (p_property == String())
  1537. update_tree_pending = true;
  1538. else {
  1539. pending.insert(p_property);
  1540. }
  1541. }
  1542. void EditorInspector::_edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field) {
  1543. if (autoclear && editor_property_map.has(p_name)) {
  1544. for (List<EditorProperty *>::Element *E = editor_property_map[p_name].front(); E; E = E->next()) {
  1545. if (E->get()->is_checkable()) {
  1546. E->get()->set_checked(true);
  1547. }
  1548. }
  1549. }
  1550. if (!undo_redo || bool(object->call("_dont_undo_redo"))) {
  1551. object->set(p_name, p_value);
  1552. if (p_refresh_all)
  1553. _edit_request_change(object, "");
  1554. else
  1555. _edit_request_change(object, p_name);
  1556. emit_signal(_prop_edited, p_name);
  1557. } else if (Object::cast_to<MultiNodeEdit>(object)) {
  1558. Object::cast_to<MultiNodeEdit>(object)->set_property_field(p_name, p_value, p_changed_field);
  1559. _edit_request_change(object, p_name);
  1560. emit_signal(_prop_edited, p_name);
  1561. } else {
  1562. undo_redo->create_action(TTR("Set") + " " + p_name, UndoRedo::MERGE_ENDS);
  1563. undo_redo->add_do_property(object, p_name, p_value);
  1564. undo_redo->add_undo_property(object, p_name, object->get(p_name));
  1565. if (p_refresh_all) {
  1566. undo_redo->add_do_method(this, "_edit_request_change", object, "");
  1567. undo_redo->add_undo_method(this, "_edit_request_change", object, "");
  1568. } else {
  1569. undo_redo->add_do_method(this, "_edit_request_change", object, p_name);
  1570. undo_redo->add_undo_method(this, "_edit_request_change", object, p_name);
  1571. }
  1572. Resource *r = Object::cast_to<Resource>(object);
  1573. if (r) {
  1574. if (String(p_name) == "resource_local_to_scene") {
  1575. bool prev = object->get(p_name);
  1576. bool next = p_value;
  1577. if (next) {
  1578. undo_redo->add_do_method(r, "setup_local_to_scene");
  1579. }
  1580. if (prev) {
  1581. undo_redo->add_undo_method(r, "setup_local_to_scene");
  1582. }
  1583. }
  1584. }
  1585. undo_redo->add_do_method(this, "emit_signal", _prop_edited, p_name);
  1586. undo_redo->add_undo_method(this, "emit_signal", _prop_edited, p_name);
  1587. undo_redo->commit_action();
  1588. }
  1589. if (editor_property_map.has(p_name)) {
  1590. for (List<EditorProperty *>::Element *E = editor_property_map[p_name].front(); E; E = E->next()) {
  1591. E->get()->update_reload_status();
  1592. }
  1593. }
  1594. }
  1595. void EditorInspector::_property_changed(const String &p_path, const Variant &p_value, const String &p_name, bool changing) {
  1596. // The "changing" variable must be true for properties that trigger events as typing occurs,
  1597. // like "text_changed" signal. eg: Text property of Label, Button, RichTextLabel, etc.
  1598. if (changing)
  1599. this->changing++;
  1600. _edit_set(p_path, p_value, false, p_name);
  1601. if (changing)
  1602. this->changing--;
  1603. if (restart_request_props.has(p_path)) {
  1604. emit_signal("restart_requested");
  1605. }
  1606. }
  1607. void EditorInspector::_property_changed_update_all(const String &p_path, const Variant &p_value, const String &p_name, bool p_changing) {
  1608. update_tree();
  1609. }
  1610. void EditorInspector::_multiple_properties_changed(Vector<String> p_paths, Array p_values) {
  1611. ERR_FAIL_COND(p_paths.size() == 0 || p_values.size() == 0);
  1612. ERR_FAIL_COND(p_paths.size() != p_values.size());
  1613. String names;
  1614. for (int i = 0; i < p_paths.size(); i++) {
  1615. if (i > 0)
  1616. names += ",";
  1617. names += p_paths[i];
  1618. }
  1619. undo_redo->create_action(TTR("Set Multiple:") + " " + names, UndoRedo::MERGE_ENDS);
  1620. for (int i = 0; i < p_paths.size(); i++) {
  1621. _edit_set(p_paths[i], p_values[i], false, "");
  1622. if (restart_request_props.has(p_paths[i])) {
  1623. emit_signal("restart_requested");
  1624. }
  1625. }
  1626. changing++;
  1627. undo_redo->commit_action();
  1628. changing--;
  1629. }
  1630. void EditorInspector::_property_keyed(const String &p_path, bool p_advance) {
  1631. if (!object)
  1632. return;
  1633. emit_signal("property_keyed", p_path, object->get(p_path), p_advance); //second param is deprecated
  1634. }
  1635. void EditorInspector::_property_keyed_with_value(const String &p_path, const Variant &p_value, bool p_advance) {
  1636. if (!object)
  1637. return;
  1638. emit_signal("property_keyed", p_path, p_value, p_advance); //second param is deprecated
  1639. }
  1640. void EditorInspector::_property_checked(const String &p_path, bool p_checked) {
  1641. if (!object)
  1642. return;
  1643. //property checked
  1644. if (autoclear) {
  1645. if (!p_checked) {
  1646. object->set(p_path, Variant());
  1647. } else {
  1648. Variant to_create;
  1649. List<PropertyInfo> pinfo;
  1650. object->get_property_list(&pinfo);
  1651. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1652. if (E->get().name == p_path) {
  1653. Variant::CallError ce;
  1654. to_create = Variant::construct(E->get().type, NULL, 0, ce);
  1655. break;
  1656. }
  1657. }
  1658. object->set(p_path, to_create);
  1659. }
  1660. if (editor_property_map.has(p_path)) {
  1661. for (List<EditorProperty *>::Element *E = editor_property_map[p_path].front(); E; E = E->next()) {
  1662. E->get()->update_property();
  1663. E->get()->update_reload_status();
  1664. }
  1665. }
  1666. } else {
  1667. emit_signal("property_toggled", p_path, p_checked);
  1668. }
  1669. }
  1670. void EditorInspector::_property_selected(const String &p_path, int p_focusable) {
  1671. property_selected = p_path;
  1672. property_focusable = p_focusable;
  1673. //deselect the others
  1674. for (Map<StringName, List<EditorProperty *> >::Element *F = editor_property_map.front(); F; F = F->next()) {
  1675. if (F->key() == property_selected)
  1676. continue;
  1677. for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) {
  1678. if (E->get()->is_selected())
  1679. E->get()->deselect();
  1680. }
  1681. }
  1682. emit_signal("property_selected", p_path);
  1683. }
  1684. void EditorInspector::_object_id_selected(const String &p_path, ObjectID p_id) {
  1685. emit_signal("object_id_selected", p_id);
  1686. }
  1687. void EditorInspector::_resource_selected(const String &p_path, RES p_resource) {
  1688. emit_signal("resource_selected", p_resource, p_path);
  1689. }
  1690. void EditorInspector::_node_removed(Node *p_node) {
  1691. if (p_node == object) {
  1692. edit(NULL);
  1693. }
  1694. }
  1695. void EditorInspector::_notification(int p_what) {
  1696. if (p_what == NOTIFICATION_READY) {
  1697. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", this, "_feature_profile_changed");
  1698. }
  1699. if (p_what == NOTIFICATION_ENTER_TREE) {
  1700. if (sub_inspector) {
  1701. add_style_override("bg", get_stylebox("sub_inspector_bg", "Editor"));
  1702. } else {
  1703. add_style_override("bg", get_stylebox("bg", "Tree"));
  1704. get_tree()->connect("node_removed", this, "_node_removed");
  1705. }
  1706. }
  1707. if (p_what == NOTIFICATION_PREDELETE) {
  1708. edit(NULL); //just in case
  1709. }
  1710. if (p_what == NOTIFICATION_EXIT_TREE) {
  1711. if (!sub_inspector) {
  1712. get_tree()->disconnect("node_removed", this, "_node_removed");
  1713. }
  1714. edit(NULL);
  1715. }
  1716. if (p_what == NOTIFICATION_PROCESS) {
  1717. if (update_scroll_request >= 0) {
  1718. get_v_scrollbar()->call_deferred("set_value", update_scroll_request);
  1719. update_scroll_request = -1;
  1720. }
  1721. if (refresh_countdown > 0) {
  1722. refresh_countdown -= get_process_delta_time();
  1723. if (refresh_countdown <= 0) {
  1724. for (Map<StringName, List<EditorProperty *> >::Element *F = editor_property_map.front(); F; F = F->next()) {
  1725. for (List<EditorProperty *>::Element *E = F->get().front(); E; E = E->next()) {
  1726. E->get()->update_property();
  1727. E->get()->update_reload_status();
  1728. }
  1729. }
  1730. }
  1731. }
  1732. changing++;
  1733. if (update_tree_pending) {
  1734. update_tree();
  1735. update_tree_pending = false;
  1736. pending.clear();
  1737. } else {
  1738. while (pending.size()) {
  1739. StringName prop = pending.front()->get();
  1740. if (editor_property_map.has(prop)) {
  1741. for (List<EditorProperty *>::Element *E = editor_property_map[prop].front(); E; E = E->next()) {
  1742. E->get()->update_property();
  1743. E->get()->update_reload_status();
  1744. }
  1745. }
  1746. pending.erase(pending.front());
  1747. }
  1748. }
  1749. changing--;
  1750. }
  1751. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  1752. if (sub_inspector) {
  1753. add_style_override("bg", get_stylebox("sub_inspector_bg", "Editor"));
  1754. } else if (is_inside_tree()) {
  1755. add_style_override("bg", get_stylebox("bg", "Tree"));
  1756. }
  1757. update_tree();
  1758. }
  1759. }
  1760. void EditorInspector::_changed_callback(Object *p_changed, const char *p_prop) {
  1761. //this is called when property change is notified via _change_notify()
  1762. _edit_request_change(p_changed, p_prop);
  1763. }
  1764. void EditorInspector::_vscroll_changed(double p_offset) {
  1765. if (update_scroll_request >= 0) //waiting, do nothing
  1766. return;
  1767. if (object) {
  1768. scroll_cache[object->get_instance_id()] = p_offset;
  1769. }
  1770. }
  1771. void EditorInspector::set_property_prefix(const String &p_prefix) {
  1772. property_prefix = p_prefix;
  1773. }
  1774. String EditorInspector::get_property_prefix() const {
  1775. return property_prefix;
  1776. }
  1777. void EditorInspector::set_object_class(const String &p_class) {
  1778. object_class = p_class;
  1779. }
  1780. String EditorInspector::get_object_class() const {
  1781. return object_class;
  1782. }
  1783. void EditorInspector::_feature_profile_changed() {
  1784. update_tree();
  1785. }
  1786. void EditorInspector::_bind_methods() {
  1787. ClassDB::bind_method("_property_changed", &EditorInspector::_property_changed, DEFVAL(""), DEFVAL(false));
  1788. ClassDB::bind_method("_multiple_properties_changed", &EditorInspector::_multiple_properties_changed);
  1789. ClassDB::bind_method("_property_changed_update_all", &EditorInspector::_property_changed_update_all);
  1790. ClassDB::bind_method("_edit_request_change", &EditorInspector::_edit_request_change);
  1791. ClassDB::bind_method("_node_removed", &EditorInspector::_node_removed);
  1792. ClassDB::bind_method("_filter_changed", &EditorInspector::_filter_changed);
  1793. ClassDB::bind_method("_property_keyed", &EditorInspector::_property_keyed);
  1794. ClassDB::bind_method("_property_keyed_with_value", &EditorInspector::_property_keyed_with_value);
  1795. ClassDB::bind_method("_property_checked", &EditorInspector::_property_checked);
  1796. ClassDB::bind_method("_property_selected", &EditorInspector::_property_selected);
  1797. ClassDB::bind_method("_resource_selected", &EditorInspector::_resource_selected);
  1798. ClassDB::bind_method("_object_id_selected", &EditorInspector::_object_id_selected);
  1799. ClassDB::bind_method("_vscroll_changed", &EditorInspector::_vscroll_changed);
  1800. ClassDB::bind_method("_feature_profile_changed", &EditorInspector::_feature_profile_changed);
  1801. ClassDB::bind_method("refresh", &EditorInspector::refresh);
  1802. ADD_SIGNAL(MethodInfo("property_selected", PropertyInfo(Variant::STRING, "property")));
  1803. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING, "property")));
  1804. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "res"), PropertyInfo(Variant::STRING, "prop")));
  1805. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::INT, "id")));
  1806. ADD_SIGNAL(MethodInfo("property_edited", PropertyInfo(Variant::STRING, "property")));
  1807. ADD_SIGNAL(MethodInfo("property_toggled", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::BOOL, "checked")));
  1808. ADD_SIGNAL(MethodInfo("restart_requested"));
  1809. }
  1810. EditorInspector::EditorInspector() {
  1811. object = NULL;
  1812. undo_redo = NULL;
  1813. main_vbox = memnew(VBoxContainer);
  1814. main_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1815. main_vbox->add_constant_override("separation", 0);
  1816. add_child(main_vbox);
  1817. set_enable_h_scroll(false);
  1818. set_enable_v_scroll(true);
  1819. show_categories = false;
  1820. hide_script = true;
  1821. use_doc_hints = false;
  1822. capitalize_paths = true;
  1823. use_filter = false;
  1824. autoclear = false;
  1825. changing = 0;
  1826. use_folding = false;
  1827. update_all_pending = false;
  1828. update_tree_pending = false;
  1829. refresh_countdown = 0;
  1830. read_only = false;
  1831. search_box = NULL;
  1832. keying = false;
  1833. _prop_edited = "property_edited";
  1834. set_process(true);
  1835. property_focusable = -1;
  1836. sub_inspector = false;
  1837. get_v_scrollbar()->connect("value_changed", this, "_vscroll_changed");
  1838. update_scroll_request = -1;
  1839. }