ImageEditor.cpp 24 KB

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