editor_spin_slider.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*************************************************************************/
  2. /* editor_spin_slider.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_spin_slider.h"
  31. #include "core/math/expression.h"
  32. #include "core/os/input.h"
  33. #include "editor_scale.h"
  34. String EditorSpinSlider::get_tooltip(const Point2 &p_pos) const {
  35. return rtos(get_value());
  36. }
  37. String EditorSpinSlider::get_text_value() const {
  38. int zeros = Math::step_decimals(get_step());
  39. return String::num(get_value(), zeros);
  40. }
  41. void EditorSpinSlider::_gui_input(const Ref<InputEvent> &p_event) {
  42. if (read_only)
  43. return;
  44. Ref<InputEventMouseButton> mb = p_event;
  45. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  46. if (mb->is_pressed()) {
  47. if (updown_offset != -1 && mb->get_position().x > updown_offset) {
  48. //there is an updown, so use it.
  49. if (mb->get_position().y < get_size().height / 2) {
  50. set_value(get_value() + get_step());
  51. } else {
  52. set_value(get_value() - get_step());
  53. }
  54. return;
  55. } else {
  56. grabbing_spinner_attempt = true;
  57. grabbing_spinner_dist_cache = 0;
  58. pre_grab_value = get_value();
  59. grabbing_spinner = false;
  60. grabbing_spinner_mouse_pos = Input::get_singleton()->get_mouse_position();
  61. }
  62. } else {
  63. if (grabbing_spinner_attempt) {
  64. if (grabbing_spinner) {
  65. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  66. Input::get_singleton()->warp_mouse_position(grabbing_spinner_mouse_pos);
  67. update();
  68. } else {
  69. _focus_entered();
  70. }
  71. grabbing_spinner = false;
  72. grabbing_spinner_attempt = false;
  73. }
  74. }
  75. }
  76. Ref<InputEventMouseMotion> mm = p_event;
  77. if (mm.is_valid()) {
  78. if (grabbing_spinner_attempt) {
  79. double diff_x = mm->get_relative().x;
  80. if (mm->get_shift() && grabbing_spinner) {
  81. diff_x *= 0.1;
  82. }
  83. grabbing_spinner_dist_cache += diff_x;
  84. if (!grabbing_spinner && ABS(grabbing_spinner_dist_cache) > 4 * EDSCALE) {
  85. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  86. grabbing_spinner = true;
  87. }
  88. if (grabbing_spinner) {
  89. if (mm->get_control() || updown_offset != -1) {
  90. set_value(Math::round(get_value()));
  91. if (ABS(grabbing_spinner_dist_cache) > 6) {
  92. set_value(get_value() + SGN(grabbing_spinner_dist_cache));
  93. grabbing_spinner_dist_cache = 0;
  94. pre_grab_value = get_value();
  95. }
  96. } else {
  97. set_value(pre_grab_value + get_step() * grabbing_spinner_dist_cache * 10);
  98. }
  99. }
  100. } else if (updown_offset != -1) {
  101. bool new_hover = (mm->get_position().x > updown_offset);
  102. if (new_hover != hover_updown) {
  103. hover_updown = new_hover;
  104. update();
  105. }
  106. }
  107. }
  108. Ref<InputEventKey> k = p_event;
  109. if (k.is_valid() && k->is_pressed() && k->is_action("ui_accept")) {
  110. _focus_entered();
  111. }
  112. }
  113. void EditorSpinSlider::_grabber_gui_input(const Ref<InputEvent> &p_event) {
  114. Ref<InputEventMouseButton> mb = p_event;
  115. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  116. if (mb->is_pressed()) {
  117. grabbing_grabber = true;
  118. grabbing_ratio = get_as_ratio();
  119. grabbing_from = grabber->get_transform().xform(mb->get_position()).x;
  120. } else {
  121. grabbing_grabber = false;
  122. }
  123. }
  124. Ref<InputEventMouseMotion> mm = p_event;
  125. if (mm.is_valid() && grabbing_grabber) {
  126. float grabbing_ofs = (grabber->get_transform().xform(mm->get_position()).x - grabbing_from) / float(grabber_range);
  127. set_as_ratio(grabbing_ratio + grabbing_ofs);
  128. update();
  129. }
  130. }
  131. void EditorSpinSlider::_notification(int p_what) {
  132. if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_OUT ||
  133. p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN ||
  134. p_what == NOTIFICATION_EXIT_TREE) {
  135. if (grabbing_spinner) {
  136. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  137. grabbing_spinner = false;
  138. grabbing_spinner_attempt = false;
  139. }
  140. }
  141. if (p_what == NOTIFICATION_DRAW) {
  142. updown_offset = -1;
  143. Ref<StyleBox> sb = get_stylebox("normal", "LineEdit");
  144. if (!flat) {
  145. draw_style_box(sb, Rect2(Vector2(), get_size()));
  146. }
  147. Ref<Font> font = get_font("font", "LineEdit");
  148. int sep_base = 4 * EDSCALE;
  149. int sep = sep_base + sb->get_offset().x; //make it have the same margin on both sides, looks better
  150. int string_width = font->get_string_size(label).width;
  151. int number_width = get_size().width - sb->get_minimum_size().width - string_width - sep;
  152. Ref<Texture> updown = get_icon("updown", "SpinBox");
  153. if (get_step() == 1) {
  154. number_width -= updown->get_width();
  155. }
  156. String numstr = get_text_value();
  157. int vofs = (get_size().height - font->get_height()) / 2 + font->get_ascent();
  158. Color fc = get_color("font_color", "LineEdit");
  159. Color lc;
  160. if (use_custom_label_color) {
  161. lc = custom_label_color;
  162. } else {
  163. lc = fc;
  164. }
  165. if (flat && label != String()) {
  166. Color label_bg_color = get_color("dark_color_3", "Editor");
  167. draw_rect(Rect2(Vector2(), Vector2(sb->get_offset().x * 2 + string_width, get_size().height)), label_bg_color);
  168. }
  169. if (has_focus()) {
  170. Ref<StyleBox> focus = get_stylebox("focus", "LineEdit");
  171. draw_style_box(focus, Rect2(Vector2(), get_size()));
  172. }
  173. draw_string(font, Vector2(sb->get_offset().x, vofs), label, lc * Color(1, 1, 1, 0.5));
  174. draw_string(font, Vector2(sb->get_offset().x + string_width + sep, vofs), numstr, fc, number_width);
  175. if (get_step() == 1) {
  176. Ref<Texture> updown = get_icon("updown", "SpinBox");
  177. int updown_vofs = (get_size().height - updown->get_height()) / 2;
  178. updown_offset = get_size().width - sb->get_margin(MARGIN_RIGHT) - updown->get_width();
  179. Color c(1, 1, 1);
  180. if (hover_updown) {
  181. c *= Color(1.2, 1.2, 1.2);
  182. }
  183. draw_texture(updown, Vector2(updown_offset, updown_vofs), c);
  184. if (grabber->is_visible()) {
  185. grabber->hide();
  186. }
  187. } else if (!hide_slider) {
  188. int grabber_w = 4 * EDSCALE;
  189. int width = get_size().width - sb->get_minimum_size().width - grabber_w;
  190. int ofs = sb->get_offset().x;
  191. int svofs = (get_size().height + vofs) / 2 - 1;
  192. Color c = fc;
  193. c.a = 0.2;
  194. draw_rect(Rect2(ofs, svofs + 1, width, 2 * EDSCALE), c);
  195. int gofs = get_as_ratio() * width;
  196. c.a = 0.9;
  197. Rect2 grabber_rect = Rect2(ofs + gofs, svofs + 1, grabber_w, 2 * EDSCALE);
  198. draw_rect(grabber_rect, c);
  199. bool display_grabber = (mouse_over_spin || mouse_over_grabber) && !grabbing_spinner && !value_input->is_visible();
  200. if (grabber->is_visible() != display_grabber) {
  201. if (display_grabber) {
  202. grabber->show();
  203. } else {
  204. grabber->hide();
  205. }
  206. }
  207. if (display_grabber) {
  208. Ref<Texture> grabber_tex;
  209. if (mouse_over_grabber) {
  210. grabber_tex = get_icon("grabber_highlight", "HSlider");
  211. } else {
  212. grabber_tex = get_icon("grabber", "HSlider");
  213. }
  214. if (grabber->get_texture() != grabber_tex) {
  215. grabber->set_texture(grabber_tex);
  216. }
  217. grabber->set_size(Size2(0, 0));
  218. grabber->set_position(get_global_position() + grabber_rect.position + grabber_rect.size * 0.5 - grabber->get_size() * 0.5);
  219. grabber_range = width;
  220. }
  221. }
  222. }
  223. if (p_what == NOTIFICATION_MOUSE_ENTER) {
  224. mouse_over_spin = true;
  225. update();
  226. }
  227. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  228. mouse_over_spin = false;
  229. update();
  230. }
  231. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  232. /* Sorry, I don't like this, it makes navigating the different fields with arrows more difficult.
  233. * Just press enter to edit.
  234. * if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && !value_input_just_closed) {
  235. _focus_entered();
  236. }*/
  237. if ((Input::get_singleton()->is_action_pressed("ui_focus_next") || Input::get_singleton()->is_action_pressed("ui_focus_prev")) && !value_input_just_closed) {
  238. _focus_entered();
  239. }
  240. value_input_just_closed = false;
  241. }
  242. }
  243. Size2 EditorSpinSlider::get_minimum_size() const {
  244. Ref<StyleBox> sb = get_stylebox("normal", "LineEdit");
  245. Ref<Font> font = get_font("font", "LineEdit");
  246. Size2 ms = sb->get_minimum_size();
  247. ms.height += font->get_height();
  248. return ms;
  249. }
  250. void EditorSpinSlider::set_hide_slider(bool p_hide) {
  251. hide_slider = p_hide;
  252. update();
  253. }
  254. bool EditorSpinSlider::is_hiding_slider() const {
  255. return hide_slider;
  256. }
  257. void EditorSpinSlider::set_label(const String &p_label) {
  258. label = p_label;
  259. update();
  260. }
  261. String EditorSpinSlider::get_label() const {
  262. return label;
  263. }
  264. void EditorSpinSlider::_evaluate_input_text() {
  265. String text = value_input->get_text();
  266. Ref<Expression> expr;
  267. expr.instance();
  268. Error err = expr->parse(text);
  269. if (err != OK) {
  270. return;
  271. }
  272. Variant v = expr->execute(Array(), NULL, false);
  273. if (v.get_type() == Variant::NIL)
  274. return;
  275. set_value(v);
  276. }
  277. //text_entered signal
  278. void EditorSpinSlider::_value_input_entered(const String &p_text) {
  279. value_input_just_closed = true;
  280. value_input->hide();
  281. }
  282. //modal_closed signal
  283. void EditorSpinSlider::_value_input_closed() {
  284. _evaluate_input_text();
  285. value_input_just_closed = true;
  286. }
  287. //focus_exited signal
  288. void EditorSpinSlider::_value_focus_exited() {
  289. _evaluate_input_text();
  290. // focus is not on the same element after the vlalue_input was exited
  291. // -> focus is on next element
  292. // -> TAB was pressed
  293. // -> modal_close was not called
  294. // -> need to close/hide manually
  295. if (!value_input_just_closed) { //value_input_just_closed should do the same
  296. value_input->hide();
  297. //tab was pressed
  298. } else {
  299. //enter, click, esc
  300. }
  301. }
  302. void EditorSpinSlider::_grabber_mouse_entered() {
  303. mouse_over_grabber = true;
  304. update();
  305. }
  306. void EditorSpinSlider::_grabber_mouse_exited() {
  307. mouse_over_grabber = false;
  308. update();
  309. }
  310. void EditorSpinSlider::set_read_only(bool p_enable) {
  311. read_only = p_enable;
  312. update();
  313. }
  314. bool EditorSpinSlider::is_read_only() const {
  315. return read_only;
  316. }
  317. void EditorSpinSlider::set_flat(bool p_enable) {
  318. flat = p_enable;
  319. update();
  320. }
  321. bool EditorSpinSlider::is_flat() const {
  322. return flat;
  323. }
  324. void EditorSpinSlider::set_custom_label_color(bool p_use_custom_label_color, Color p_custom_label_color) {
  325. use_custom_label_color = p_use_custom_label_color;
  326. custom_label_color = p_custom_label_color;
  327. }
  328. void EditorSpinSlider::_focus_entered() {
  329. Rect2 gr = get_global_rect();
  330. value_input->set_text(get_text_value());
  331. value_input->set_position(gr.position);
  332. value_input->set_size(gr.size);
  333. value_input->call_deferred("show_modal");
  334. value_input->call_deferred("grab_focus");
  335. value_input->call_deferred("select_all");
  336. value_input->set_focus_next(find_next_valid_focus()->get_path());
  337. value_input->set_focus_previous(find_prev_valid_focus()->get_path());
  338. }
  339. void EditorSpinSlider::_bind_methods() {
  340. ClassDB::bind_method(D_METHOD("set_label", "label"), &EditorSpinSlider::set_label);
  341. ClassDB::bind_method(D_METHOD("get_label"), &EditorSpinSlider::get_label);
  342. ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorSpinSlider::set_read_only);
  343. ClassDB::bind_method(D_METHOD("is_read_only"), &EditorSpinSlider::is_read_only);
  344. ClassDB::bind_method(D_METHOD("set_flat", "flat"), &EditorSpinSlider::set_flat);
  345. ClassDB::bind_method(D_METHOD("is_flat"), &EditorSpinSlider::is_flat);
  346. ClassDB::bind_method(D_METHOD("_gui_input"), &EditorSpinSlider::_gui_input);
  347. ClassDB::bind_method(D_METHOD("_grabber_mouse_entered"), &EditorSpinSlider::_grabber_mouse_entered);
  348. ClassDB::bind_method(D_METHOD("_grabber_mouse_exited"), &EditorSpinSlider::_grabber_mouse_exited);
  349. ClassDB::bind_method(D_METHOD("_grabber_gui_input"), &EditorSpinSlider::_grabber_gui_input);
  350. ClassDB::bind_method(D_METHOD("_value_input_closed"), &EditorSpinSlider::_value_input_closed);
  351. ClassDB::bind_method(D_METHOD("_value_input_entered"), &EditorSpinSlider::_value_input_entered);
  352. ClassDB::bind_method(D_METHOD("_value_focus_exited"), &EditorSpinSlider::_value_focus_exited);
  353. ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
  354. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
  355. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  356. }
  357. EditorSpinSlider::EditorSpinSlider() {
  358. flat = false;
  359. grabbing_spinner_attempt = false;
  360. grabbing_spinner = false;
  361. grabbing_spinner_dist_cache = 0;
  362. pre_grab_value = 0;
  363. set_focus_mode(FOCUS_ALL);
  364. updown_offset = -1;
  365. hover_updown = false;
  366. grabber = memnew(TextureRect);
  367. add_child(grabber);
  368. grabber->hide();
  369. grabber->set_as_toplevel(true);
  370. grabber->set_mouse_filter(MOUSE_FILTER_STOP);
  371. grabber->connect("mouse_entered", this, "_grabber_mouse_entered");
  372. grabber->connect("mouse_exited", this, "_grabber_mouse_exited");
  373. grabber->connect("gui_input", this, "_grabber_gui_input");
  374. mouse_over_spin = false;
  375. mouse_over_grabber = false;
  376. grabbing_grabber = false;
  377. grabber_range = 1;
  378. value_input = memnew(LineEdit);
  379. add_child(value_input);
  380. value_input->set_as_toplevel(true);
  381. value_input->hide();
  382. value_input->connect("modal_closed", this, "_value_input_closed");
  383. value_input->connect("text_entered", this, "_value_input_entered");
  384. value_input->connect("focus_exited", this, "_value_focus_exited");
  385. value_input_just_closed = false;
  386. hide_slider = false;
  387. read_only = false;
  388. use_custom_label_color = false;
  389. }