editor_inspector.cpp 62 KB

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