ImageEditor.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  5. * Copyright (c) 2021, David Isaksson <davidisaksson93@gmail.com>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "ImageEditor.h"
  10. #include "Image.h"
  11. #include "Layer.h"
  12. #include "Tools/MoveTool.h"
  13. #include "Tools/Tool.h"
  14. #include <LibConfig/Client.h>
  15. #include <LibGUI/Command.h>
  16. #include <LibGUI/Painter.h>
  17. #include <LibGfx/DisjointRectSet.h>
  18. #include <LibGfx/Palette.h>
  19. #include <LibGfx/Rect.h>
  20. namespace PixelPaint {
  21. ImageEditor::ImageEditor(NonnullRefPtr<Image> image)
  22. : m_image(move(image))
  23. , m_selection(*this)
  24. {
  25. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  26. m_undo_stack.push(make<ImageUndoCommand>(*m_image));
  27. m_image->add_client(*this);
  28. m_pixel_grid_threshold = (float)Config::read_i32("PixelPaint", "PixelGrid", "Threshold", 15);
  29. m_show_pixel_grid = Config::read_bool("PixelPaint", "PixelGrid", "Show", true);
  30. m_show_rulers = Config::read_bool("PixelPaint", "Rulers", "Show", true);
  31. m_show_guides = Config::read_bool("PixelPaint", "Guides", "Show", true);
  32. }
  33. ImageEditor::~ImageEditor()
  34. {
  35. m_image->remove_client(*this);
  36. }
  37. void ImageEditor::did_complete_action()
  38. {
  39. m_undo_stack.push(make<ImageUndoCommand>(*m_image));
  40. }
  41. bool ImageEditor::undo()
  42. {
  43. if (!m_undo_stack.can_undo())
  44. return false;
  45. /* Without this you need to undo twice to actually start undoing stuff.
  46. * This is due to the fact that the top of the UndoStack contains the snapshot of the currently
  47. * shown image but what we actually want to restore is the snapshot right below it.
  48. * Doing "undo->undo->redo" restores the 2nd topmost snapshot on the stack while lowering the
  49. * stack pointer only by 1. This is important because we want the UndoStack's pointer to always point
  50. * at the currently shown snapshot, otherwise doing 'undo->undo->draw something else' would delete
  51. * one of the snapshots.
  52. * This works because UndoStack::undo first decrements the stack pointer and then restores the snapshot,
  53. * while UndoStack::redo first restores the snapshot and then increments the stack pointer.
  54. */
  55. m_undo_stack.undo();
  56. m_undo_stack.undo();
  57. m_undo_stack.redo();
  58. layers_did_change();
  59. return true;
  60. }
  61. bool ImageEditor::redo()
  62. {
  63. if (!m_undo_stack.can_redo())
  64. return false;
  65. m_undo_stack.redo();
  66. layers_did_change();
  67. return true;
  68. }
  69. void ImageEditor::paint_event(GUI::PaintEvent& event)
  70. {
  71. GUI::Frame::paint_event(event);
  72. GUI::Painter painter(*this);
  73. painter.add_clip_rect(event.rect());
  74. painter.add_clip_rect(frame_inner_rect());
  75. {
  76. Gfx::DisjointRectSet background_rects;
  77. background_rects.add(frame_inner_rect());
  78. background_rects.shatter(m_editor_image_rect);
  79. for (auto& rect : background_rects.rects())
  80. painter.fill_rect(rect, palette().color(Gfx::ColorRole::Tray));
  81. }
  82. Gfx::StylePainter::paint_transparency_grid(painter, m_editor_image_rect, palette());
  83. painter.draw_rect(m_editor_image_rect.inflated(2, 2), Color::Black);
  84. m_image->paint_into(painter, m_editor_image_rect);
  85. if (m_active_layer && m_show_active_layer_boundary) {
  86. painter.draw_rect(enclosing_int_rect(image_rect_to_editor_rect(m_active_layer->relative_rect())).inflated(2, 2), Color::Black);
  87. }
  88. if (m_show_pixel_grid && m_scale > m_pixel_grid_threshold) {
  89. auto event_image_rect = enclosing_int_rect(editor_rect_to_image_rect(event.rect())).inflated(1, 1);
  90. auto image_rect = m_image->rect().inflated(1, 1).intersected(event_image_rect);
  91. for (auto i = image_rect.left(); i < image_rect.right(); i++) {
  92. auto start_point = image_position_to_editor_position({ i, image_rect.top() }).to_type<int>();
  93. auto end_point = image_position_to_editor_position({ i, image_rect.bottom() }).to_type<int>();
  94. painter.draw_line(start_point, end_point, Color::LightGray);
  95. }
  96. for (auto i = image_rect.top(); i < image_rect.bottom(); i++) {
  97. auto start_point = image_position_to_editor_position({ image_rect.left(), i }).to_type<int>();
  98. auto end_point = image_position_to_editor_position({ image_rect.right(), i }).to_type<int>();
  99. painter.draw_line(start_point, end_point, Color::LightGray);
  100. }
  101. }
  102. if (m_show_guides) {
  103. for (auto& guide : m_guides) {
  104. if (guide.orientation() == Guide::Orientation::Horizontal) {
  105. int y_coordinate = (int)image_position_to_editor_position({ 0.0f, guide.offset() }).y();
  106. painter.draw_line({ 0, y_coordinate }, { rect().width(), y_coordinate }, Color::Cyan, 1, Gfx::Painter::LineStyle::Dashed, Color::LightGray);
  107. } else if (guide.orientation() == Guide::Orientation::Vertical) {
  108. int x_coordinate = (int)image_position_to_editor_position({ guide.offset(), 0.0f }).x();
  109. painter.draw_line({ x_coordinate, 0 }, { x_coordinate, rect().height() }, Color::Cyan, 1, Gfx::Painter::LineStyle::Dashed, Color::LightGray);
  110. }
  111. }
  112. }
  113. if (!m_selection.is_empty())
  114. m_selection.paint(painter);
  115. if (m_show_rulers) {
  116. const auto ruler_bg_color = palette().color(Gfx::ColorRole::InactiveSelection);
  117. const auto ruler_fg_color = palette().color(Gfx::ColorRole::Ruler);
  118. const auto ruler_text_color = palette().color(Gfx::ColorRole::InactiveSelectionText);
  119. const auto mouse_indicator_color = Color::White;
  120. // Ruler background
  121. painter.fill_rect({ { 0, 0 }, { m_ruler_thickness, rect().height() } }, ruler_bg_color);
  122. painter.fill_rect({ { 0, 0 }, { rect().width(), m_ruler_thickness } }, ruler_bg_color);
  123. const auto ruler_step = calculate_ruler_step_size();
  124. const auto editor_origin_to_image = editor_position_to_image_position({ 0, 0 });
  125. const auto editor_max_to_image = editor_position_to_image_position({ width(), height() });
  126. // Horizontal ruler
  127. painter.draw_line({ 0, m_ruler_thickness }, { rect().width(), m_ruler_thickness }, ruler_fg_color);
  128. const auto x_start = floor(editor_origin_to_image.x()) - ((int)floor(editor_origin_to_image.x()) % ruler_step) - ruler_step;
  129. for (int x = x_start; x < editor_max_to_image.x(); x += ruler_step) {
  130. const int num_sub_divisions = min(ruler_step, 10);
  131. for (int x_sub = 0; x_sub < num_sub_divisions; ++x_sub) {
  132. const int x_pos = x + (int)(ruler_step * x_sub / num_sub_divisions);
  133. const int editor_x_sub = image_position_to_editor_position({ x_pos, 0 }).x();
  134. const int line_length = (x_sub % 2 == 0) ? m_ruler_thickness / 3 : m_ruler_thickness / 6;
  135. painter.draw_line({ editor_x_sub, m_ruler_thickness - line_length }, { editor_x_sub, m_ruler_thickness }, ruler_fg_color);
  136. }
  137. const int editor_x = image_position_to_editor_position({ x, 0 }).x();
  138. painter.draw_line({ editor_x, 0 }, { editor_x, m_ruler_thickness }, ruler_fg_color);
  139. painter.draw_text({ { editor_x + 2, 0 }, { m_ruler_thickness, m_ruler_thickness - 2 } }, String::formatted("{}", x), painter.font(), Gfx::TextAlignment::CenterLeft, ruler_text_color);
  140. }
  141. // Vertical ruler
  142. painter.draw_line({ m_ruler_thickness, 0 }, { m_ruler_thickness, rect().height() }, ruler_fg_color);
  143. const auto y_start = floor(editor_origin_to_image.y()) - ((int)floor(editor_origin_to_image.y()) % ruler_step) - ruler_step;
  144. for (int y = y_start; y < editor_max_to_image.y(); y += ruler_step) {
  145. const int num_sub_divisions = min(ruler_step, 10);
  146. for (int y_sub = 0; y_sub < num_sub_divisions; ++y_sub) {
  147. const int y_pos = y + (int)(ruler_step * y_sub / num_sub_divisions);
  148. const int editor_y_sub = image_position_to_editor_position({ 0, y_pos }).y();
  149. const int line_length = (y_sub % 2 == 0) ? m_ruler_thickness / 3 : m_ruler_thickness / 6;
  150. painter.draw_line({ m_ruler_thickness - line_length, editor_y_sub }, { m_ruler_thickness, editor_y_sub }, ruler_fg_color);
  151. }
  152. const int editor_y = image_position_to_editor_position({ 0, y }).y();
  153. painter.draw_line({ 0, editor_y }, { m_ruler_thickness, editor_y }, ruler_fg_color);
  154. painter.draw_text({ { 0, editor_y - m_ruler_thickness }, { m_ruler_thickness, m_ruler_thickness } }, String::formatted("{}", y), painter.font(), Gfx::TextAlignment::BottomRight, ruler_text_color);
  155. }
  156. // Mouse position indicator
  157. const Gfx::IntPoint indicator_x({ m_mouse_position.x(), m_ruler_thickness });
  158. const Gfx::IntPoint indicator_y({ m_ruler_thickness, m_mouse_position.y() });
  159. painter.draw_triangle(indicator_x, indicator_x + Gfx::IntPoint(-m_mouse_indicator_triangle_size, -m_mouse_indicator_triangle_size), indicator_x + Gfx::IntPoint(m_mouse_indicator_triangle_size, -m_mouse_indicator_triangle_size), mouse_indicator_color);
  160. painter.draw_triangle(indicator_y, indicator_y + Gfx::IntPoint(-m_mouse_indicator_triangle_size, -m_mouse_indicator_triangle_size), indicator_y + Gfx::IntPoint(-m_mouse_indicator_triangle_size, m_mouse_indicator_triangle_size), mouse_indicator_color);
  161. // Top left square
  162. painter.fill_rect({ { 0, 0 }, { m_ruler_thickness, m_ruler_thickness } }, ruler_bg_color);
  163. }
  164. }
  165. int ImageEditor::calculate_ruler_step_size() const
  166. {
  167. const auto step_target = 80 / m_scale;
  168. const auto max_factor = 5;
  169. for (int factor = 0; factor < max_factor; ++factor) {
  170. if (step_target <= 1 * (float)pow(10, factor))
  171. return 1 * pow(10, factor);
  172. if (step_target <= 2 * (float)pow(10, factor))
  173. return 2 * pow(10, factor);
  174. if (step_target <= 5 * (float)pow(10, factor))
  175. return 5 * pow(10, factor);
  176. }
  177. return 1 * pow(10, max_factor);
  178. }
  179. Gfx::IntRect ImageEditor::mouse_indicator_rect_x() const
  180. {
  181. const Gfx::IntPoint top_left({ m_ruler_thickness, m_ruler_thickness - m_mouse_indicator_triangle_size });
  182. const Gfx::IntSize size({ width() + 1, m_mouse_indicator_triangle_size + 1 });
  183. return Gfx::IntRect(top_left, size);
  184. }
  185. Gfx::IntRect ImageEditor::mouse_indicator_rect_y() const
  186. {
  187. const Gfx::IntPoint top_left({ m_ruler_thickness - m_mouse_indicator_triangle_size, m_ruler_thickness });
  188. const Gfx::IntSize size({ m_mouse_indicator_triangle_size + 1, height() + 1 });
  189. return Gfx::IntRect(top_left, size);
  190. }
  191. Gfx::FloatRect ImageEditor::layer_rect_to_editor_rect(Layer const& layer, Gfx::IntRect const& layer_rect) const
  192. {
  193. return image_rect_to_editor_rect(layer_rect.translated(layer.location()));
  194. }
  195. Gfx::FloatRect ImageEditor::image_rect_to_editor_rect(Gfx::IntRect const& image_rect) const
  196. {
  197. Gfx::FloatRect editor_rect;
  198. editor_rect.set_location(image_position_to_editor_position(image_rect.location()));
  199. editor_rect.set_width((float)image_rect.width() * m_scale);
  200. editor_rect.set_height((float)image_rect.height() * m_scale);
  201. return editor_rect;
  202. }
  203. Gfx::FloatRect ImageEditor::editor_rect_to_image_rect(Gfx::IntRect const& editor_rect) const
  204. {
  205. Gfx::FloatRect image_rect;
  206. image_rect.set_location(editor_position_to_image_position(editor_rect.location()));
  207. image_rect.set_width((float)editor_rect.width() / m_scale);
  208. image_rect.set_height((float)editor_rect.height() / m_scale);
  209. return image_rect;
  210. }
  211. Gfx::FloatPoint ImageEditor::layer_position_to_editor_position(Layer const& layer, Gfx::IntPoint const& layer_position) const
  212. {
  213. return image_position_to_editor_position(layer_position.translated(layer.location()));
  214. }
  215. Gfx::FloatPoint ImageEditor::image_position_to_editor_position(Gfx::IntPoint const& image_position) const
  216. {
  217. Gfx::FloatPoint editor_position;
  218. editor_position.set_x(m_editor_image_rect.x() + ((float)image_position.x() * m_scale));
  219. editor_position.set_y(m_editor_image_rect.y() + ((float)image_position.y() * m_scale));
  220. return editor_position;
  221. }
  222. Gfx::FloatPoint ImageEditor::editor_position_to_image_position(Gfx::IntPoint const& editor_position) const
  223. {
  224. Gfx::FloatPoint image_position;
  225. image_position.set_x(((float)editor_position.x() - m_editor_image_rect.x()) / m_scale);
  226. image_position.set_y(((float)editor_position.y() - m_editor_image_rect.y()) / m_scale);
  227. return image_position;
  228. }
  229. void ImageEditor::second_paint_event(GUI::PaintEvent& event)
  230. {
  231. if (m_active_tool)
  232. m_active_tool->on_second_paint(m_active_layer, event);
  233. }
  234. GUI::MouseEvent ImageEditor::event_with_pan_and_scale_applied(GUI::MouseEvent const& event) const
  235. {
  236. auto image_position = editor_position_to_image_position(event.position());
  237. return {
  238. static_cast<GUI::Event::Type>(event.type()),
  239. Gfx::IntPoint(image_position.x(), image_position.y()),
  240. event.buttons(),
  241. event.button(),
  242. event.modifiers(),
  243. event.wheel_delta()
  244. };
  245. }
  246. GUI::MouseEvent ImageEditor::event_adjusted_for_layer(GUI::MouseEvent const& event, Layer const& layer) const
  247. {
  248. auto image_position = editor_position_to_image_position(event.position());
  249. image_position.translate_by(-layer.location().x(), -layer.location().y());
  250. return {
  251. static_cast<GUI::Event::Type>(event.type()),
  252. Gfx::IntPoint(image_position.x(), image_position.y()),
  253. event.buttons(),
  254. event.button(),
  255. event.modifiers(),
  256. event.wheel_delta()
  257. };
  258. }
  259. void ImageEditor::mousedown_event(GUI::MouseEvent& event)
  260. {
  261. if (event.button() == GUI::MouseButton::Middle) {
  262. m_click_position = event.position();
  263. m_saved_pan_origin = m_pan_origin;
  264. set_override_cursor(Gfx::StandardCursor::Drag);
  265. return;
  266. }
  267. if (!m_active_tool)
  268. return;
  269. if (is<MoveTool>(*m_active_tool)) {
  270. if (auto* other_layer = layer_at_editor_position(event.position())) {
  271. set_active_layer(other_layer);
  272. }
  273. }
  274. auto layer_event = m_active_layer ? event_adjusted_for_layer(event, *m_active_layer) : event;
  275. auto image_event = event_with_pan_and_scale_applied(event);
  276. Tool::MouseEvent tool_event(Tool::MouseEvent::Action::MouseDown, layer_event, image_event, event);
  277. m_active_tool->on_mousedown(m_active_layer.ptr(), tool_event);
  278. }
  279. void ImageEditor::mousemove_event(GUI::MouseEvent& event)
  280. {
  281. m_mouse_position = event.position();
  282. if (m_show_rulers) {
  283. update(mouse_indicator_rect_x());
  284. update(mouse_indicator_rect_y());
  285. }
  286. if (event.buttons() & GUI::MouseButton::Middle) {
  287. auto delta = event.position() - m_click_position;
  288. m_pan_origin = m_saved_pan_origin.translated(
  289. -delta.x(),
  290. -delta.y());
  291. relayout();
  292. return;
  293. }
  294. auto image_event = event_with_pan_and_scale_applied(event);
  295. if (on_image_mouse_position_change) {
  296. on_image_mouse_position_change(image_event.position());
  297. }
  298. if (!m_active_tool)
  299. return;
  300. auto layer_event = m_active_layer ? event_adjusted_for_layer(event, *m_active_layer) : event;
  301. Tool::MouseEvent tool_event(Tool::MouseEvent::Action::MouseDown, layer_event, image_event, event);
  302. m_active_tool->on_mousemove(m_active_layer.ptr(), tool_event);
  303. }
  304. void ImageEditor::mouseup_event(GUI::MouseEvent& event)
  305. {
  306. set_override_cursor(m_active_cursor);
  307. if (!m_active_tool)
  308. return;
  309. auto layer_event = m_active_layer ? event_adjusted_for_layer(event, *m_active_layer) : event;
  310. auto image_event = event_with_pan_and_scale_applied(event);
  311. Tool::MouseEvent tool_event(Tool::MouseEvent::Action::MouseDown, layer_event, image_event, event);
  312. m_active_tool->on_mouseup(m_active_layer.ptr(), tool_event);
  313. }
  314. void ImageEditor::mousewheel_event(GUI::MouseEvent& event)
  315. {
  316. auto scale_delta = -event.wheel_delta() * 0.1f;
  317. scale_centered_on_position(event.position(), scale_delta);
  318. }
  319. void ImageEditor::context_menu_event(GUI::ContextMenuEvent& event)
  320. {
  321. if (!m_active_tool)
  322. return;
  323. m_active_tool->on_context_menu(m_active_layer, event);
  324. }
  325. void ImageEditor::resize_event(GUI::ResizeEvent& event)
  326. {
  327. relayout();
  328. GUI::Frame::resize_event(event);
  329. }
  330. void ImageEditor::keydown_event(GUI::KeyEvent& event)
  331. {
  332. if (m_active_tool)
  333. m_active_tool->on_keydown(event);
  334. }
  335. void ImageEditor::keyup_event(GUI::KeyEvent& event)
  336. {
  337. if (m_active_tool)
  338. m_active_tool->on_keyup(event);
  339. }
  340. void ImageEditor::enter_event(Core::Event&)
  341. {
  342. set_override_cursor(m_active_cursor);
  343. }
  344. void ImageEditor::leave_event(Core::Event&)
  345. {
  346. set_override_cursor(Gfx::StandardCursor::None);
  347. if (on_leave)
  348. on_leave();
  349. }
  350. void ImageEditor::set_active_layer(Layer* layer)
  351. {
  352. if (m_active_layer == layer)
  353. return;
  354. m_active_layer = layer;
  355. if (m_active_layer) {
  356. VERIFY(&m_active_layer->image() == m_image.ptr());
  357. size_t index = 0;
  358. for (; index < m_image->layer_count(); ++index) {
  359. if (&m_image->layer(index) == layer)
  360. break;
  361. }
  362. if (on_active_layer_change)
  363. on_active_layer_change(layer);
  364. } else {
  365. if (on_active_layer_change)
  366. on_active_layer_change({});
  367. }
  368. }
  369. void ImageEditor::set_active_tool(Tool* tool)
  370. {
  371. if (m_active_tool == tool)
  372. return;
  373. if (m_active_tool)
  374. m_active_tool->clear();
  375. m_active_tool = tool;
  376. if (m_active_tool) {
  377. m_active_tool->setup(*this);
  378. m_active_tool->on_tool_activation();
  379. m_active_cursor = m_active_tool->cursor();
  380. set_override_cursor(m_active_cursor);
  381. }
  382. }
  383. void ImageEditor::update_tool_cursor()
  384. {
  385. if (m_active_tool) {
  386. m_active_cursor = m_active_tool->cursor();
  387. set_override_cursor(m_active_cursor);
  388. }
  389. }
  390. void ImageEditor::set_guide_visibility(bool show_guides)
  391. {
  392. if (m_show_guides == show_guides)
  393. return;
  394. m_show_guides = show_guides;
  395. if (on_set_guide_visibility)
  396. on_set_guide_visibility(m_show_guides);
  397. update();
  398. }
  399. void ImageEditor::set_ruler_visibility(bool show_rulers)
  400. {
  401. if (m_show_rulers == show_rulers)
  402. return;
  403. m_show_rulers = show_rulers;
  404. if (on_set_ruler_visibility)
  405. on_set_ruler_visibility(m_show_rulers);
  406. update();
  407. }
  408. void ImageEditor::set_pixel_grid_visibility(bool show_pixel_grid)
  409. {
  410. if (m_show_pixel_grid == show_pixel_grid)
  411. return;
  412. m_show_pixel_grid = show_pixel_grid;
  413. update();
  414. }
  415. void ImageEditor::layers_did_change()
  416. {
  417. update();
  418. }
  419. Color ImageEditor::color_for(GUI::MouseButton button) const
  420. {
  421. if (button == GUI::MouseButton::Primary)
  422. return m_primary_color;
  423. if (button == GUI::MouseButton::Secondary)
  424. return m_secondary_color;
  425. VERIFY_NOT_REACHED();
  426. }
  427. Color ImageEditor::color_for(GUI::MouseEvent const& event) const
  428. {
  429. if (event.buttons() & GUI::MouseButton::Primary)
  430. return m_primary_color;
  431. if (event.buttons() & GUI::MouseButton::Secondary)
  432. return m_secondary_color;
  433. VERIFY_NOT_REACHED();
  434. }
  435. void ImageEditor::set_primary_color(Color color)
  436. {
  437. if (m_primary_color == color)
  438. return;
  439. m_primary_color = color;
  440. if (on_primary_color_change)
  441. on_primary_color_change(color);
  442. }
  443. void ImageEditor::set_secondary_color(Color color)
  444. {
  445. if (m_secondary_color == color)
  446. return;
  447. m_secondary_color = color;
  448. if (on_secondary_color_change)
  449. on_secondary_color_change(color);
  450. }
  451. Layer* ImageEditor::layer_at_editor_position(Gfx::IntPoint const& editor_position)
  452. {
  453. auto image_position = editor_position_to_image_position(editor_position);
  454. for (ssize_t i = m_image->layer_count() - 1; i >= 0; --i) {
  455. auto& layer = m_image->layer(i);
  456. if (!layer.is_visible())
  457. continue;
  458. if (layer.relative_rect().contains(Gfx::IntPoint(image_position.x(), image_position.y())))
  459. return const_cast<Layer*>(&layer);
  460. }
  461. return nullptr;
  462. }
  463. void ImageEditor::set_absolute_scale(float scale, bool do_relayout)
  464. {
  465. if (scale < 0.1f)
  466. scale = 0.1f;
  467. if (scale > 100.0f)
  468. scale = 100.0f;
  469. if (scale == m_scale)
  470. return;
  471. m_scale = scale;
  472. if (on_scale_changed)
  473. on_scale_changed(m_scale);
  474. if (do_relayout)
  475. relayout();
  476. }
  477. void ImageEditor::clamped_scale_by(float scale_delta, bool do_relayout)
  478. {
  479. set_absolute_scale(m_scale * AK::exp2(scale_delta), do_relayout);
  480. }
  481. void ImageEditor::scale_centered_on_position(Gfx::IntPoint const& position, float scale_delta)
  482. {
  483. auto old_scale = m_scale;
  484. auto image_coord_of_position = editor_position_to_image_position(position);
  485. auto image_size = m_image->size();
  486. Gfx::FloatPoint offset_from_center_in_image_coords = {
  487. image_coord_of_position.x() - image_size.width() / 2.0f,
  488. image_coord_of_position.y() - image_size.height() / 2.0f
  489. };
  490. Gfx::FloatPoint offset_from_center_in_editor_coords = {
  491. position.x() - width() / 2.0f,
  492. position.y() - height() / 2.0f
  493. };
  494. clamped_scale_by(scale_delta, false);
  495. m_pan_origin = {
  496. offset_from_center_in_image_coords.x() * m_scale - offset_from_center_in_editor_coords.x(),
  497. offset_from_center_in_image_coords.y() * m_scale - offset_from_center_in_editor_coords.y()
  498. };
  499. if (old_scale != m_scale)
  500. relayout();
  501. }
  502. void ImageEditor::scale_by(float scale_delta)
  503. {
  504. if (scale_delta == 0)
  505. return;
  506. clamped_scale_by(scale_delta, true);
  507. }
  508. void ImageEditor::set_pan_origin(Gfx::FloatPoint const& pan_origin)
  509. {
  510. if (m_pan_origin == pan_origin)
  511. return;
  512. m_pan_origin = pan_origin;
  513. relayout();
  514. }
  515. void ImageEditor::fit_image_to_view(FitType type)
  516. {
  517. auto viewport_rect = rect();
  518. m_pan_origin = Gfx::FloatPoint(0, 0);
  519. if (m_show_rulers) {
  520. viewport_rect = {
  521. viewport_rect.x() + m_ruler_thickness,
  522. viewport_rect.y() + m_ruler_thickness,
  523. viewport_rect.width() - m_ruler_thickness,
  524. viewport_rect.height() - m_ruler_thickness
  525. };
  526. }
  527. const float border_ratio = 0.95f;
  528. auto image_size = image().size();
  529. auto height_ratio = floorf(border_ratio * viewport_rect.height()) / (float)image_size.height();
  530. auto width_ratio = floorf(border_ratio * viewport_rect.width()) / (float)image_size.width();
  531. switch (type) {
  532. case FitType::Width:
  533. set_absolute_scale(width_ratio, false);
  534. break;
  535. case FitType::Height:
  536. set_absolute_scale(height_ratio, false);
  537. break;
  538. case FitType::Image:
  539. set_absolute_scale(min(height_ratio, width_ratio), false);
  540. break;
  541. }
  542. float offset = m_show_rulers ? -m_ruler_thickness / (m_scale * 2.0f) : 0.0f;
  543. m_pan_origin = Gfx::FloatPoint(offset, offset);
  544. relayout();
  545. }
  546. void ImageEditor::reset_scale_and_position()
  547. {
  548. set_absolute_scale(1.0f, false);
  549. m_pan_origin = Gfx::FloatPoint(0, 0);
  550. relayout();
  551. }
  552. void ImageEditor::relayout()
  553. {
  554. Gfx::IntSize new_size;
  555. new_size.set_width(image().size().width() * m_scale);
  556. new_size.set_height(image().size().height() * m_scale);
  557. m_editor_image_rect.set_size(new_size);
  558. Gfx::IntPoint new_location;
  559. new_location.set_x((width() / 2) - (new_size.width() / 2) - (m_pan_origin.x()));
  560. new_location.set_y((height() / 2) - (new_size.height() / 2) - (m_pan_origin.y()));
  561. m_editor_image_rect.set_location(new_location);
  562. update();
  563. }
  564. void ImageEditor::image_did_change(Gfx::IntRect const& modified_image_rect)
  565. {
  566. update(m_editor_image_rect.intersected(enclosing_int_rect(image_rect_to_editor_rect(modified_image_rect))));
  567. }
  568. void ImageEditor::image_did_change_rect(Gfx::IntRect const& new_image_rect)
  569. {
  570. m_editor_image_rect = enclosing_int_rect(image_rect_to_editor_rect(new_image_rect));
  571. update(m_editor_image_rect);
  572. }
  573. void ImageEditor::image_did_change_title(String const& path)
  574. {
  575. if (on_image_title_change)
  576. on_image_title_change(path);
  577. }
  578. void ImageEditor::image_select_layer(Layer* layer)
  579. {
  580. set_active_layer(layer);
  581. }
  582. Result<void, String> ImageEditor::save_project_to_fd_and_close(int fd) const
  583. {
  584. StringBuilder builder;
  585. JsonObjectSerializer json(builder);
  586. m_image->serialize_as_json(json);
  587. auto json_guides = json.add_array("guides");
  588. for (const auto& guide : m_guides) {
  589. auto json_guide = json_guides.add_object();
  590. json_guide.add("offset"sv, (double)guide.offset());
  591. if (guide.orientation() == Guide::Orientation::Vertical)
  592. json_guide.add("orientation", "vertical");
  593. else if (guide.orientation() == Guide::Orientation::Horizontal)
  594. json_guide.add("orientation", "horizontal");
  595. json_guide.finish();
  596. }
  597. json_guides.finish();
  598. json.finish();
  599. auto file = Core::File::construct();
  600. file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
  601. if (file->has_error())
  602. return String { file->error_string() };
  603. if (!file->write(builder.string_view()))
  604. return String { file->error_string() };
  605. return {};
  606. }
  607. void ImageEditor::set_show_active_layer_boundary(bool show)
  608. {
  609. if (m_show_active_layer_boundary == show)
  610. return;
  611. m_show_active_layer_boundary = show;
  612. update();
  613. }
  614. }