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