ImageEditor.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ImageEditor.h"
  8. #include "Image.h"
  9. #include "Layer.h"
  10. #include "MoveTool.h"
  11. #include "Tool.h"
  12. #include <LibGUI/Command.h>
  13. #include <LibGUI/Painter.h>
  14. #include <LibGfx/DisjointRectSet.h>
  15. #include <LibGfx/Palette.h>
  16. #include <LibGfx/Rect.h>
  17. namespace PixelPaint {
  18. ImageEditor::ImageEditor(NonnullRefPtr<Image> image)
  19. : m_image(move(image))
  20. , m_undo_stack(make<GUI::UndoStack>())
  21. , m_selection(*this)
  22. {
  23. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  24. m_undo_stack = make<GUI::UndoStack>();
  25. m_undo_stack->push(make<ImageUndoCommand>(*m_image));
  26. m_image->add_client(*this);
  27. }
  28. ImageEditor::~ImageEditor()
  29. {
  30. m_image->remove_client(*this);
  31. }
  32. void ImageEditor::did_complete_action()
  33. {
  34. m_undo_stack->push(make<ImageUndoCommand>(*m_image));
  35. }
  36. bool ImageEditor::undo()
  37. {
  38. if (m_undo_stack->can_undo()) {
  39. m_undo_stack->undo();
  40. layers_did_change();
  41. return true;
  42. }
  43. return false;
  44. }
  45. bool ImageEditor::redo()
  46. {
  47. if (m_undo_stack->can_redo()) {
  48. m_undo_stack->redo();
  49. layers_did_change();
  50. return true;
  51. }
  52. return false;
  53. }
  54. void ImageEditor::paint_event(GUI::PaintEvent& event)
  55. {
  56. GUI::Frame::paint_event(event);
  57. GUI::Painter painter(*this);
  58. painter.add_clip_rect(event.rect());
  59. painter.add_clip_rect(frame_inner_rect());
  60. {
  61. Gfx::DisjointRectSet background_rects;
  62. background_rects.add(frame_inner_rect());
  63. background_rects.shatter(m_editor_image_rect);
  64. for (auto& rect : background_rects.rects())
  65. painter.fill_rect(rect, palette().color(Gfx::ColorRole::Tray));
  66. }
  67. Gfx::StylePainter::paint_transparency_grid(painter, m_editor_image_rect, palette());
  68. painter.draw_rect(m_editor_image_rect.inflated(2, 2), Color::Black);
  69. m_image->paint_into(painter, m_editor_image_rect);
  70. if (m_active_layer) {
  71. painter.draw_rect(enclosing_int_rect(image_rect_to_editor_rect(m_active_layer->relative_rect())).inflated(2, 2), Color::Black);
  72. }
  73. if (m_show_guides) {
  74. for (auto& guide : m_guides) {
  75. if (guide.orientation() == Guide::Orientation::Horizontal) {
  76. int y_coordinate = (int)image_position_to_editor_position({ 0.0f, guide.offset() }).y();
  77. painter.draw_line({ 0, y_coordinate }, { rect().width(), y_coordinate }, Color::Cyan, 1, Gfx::Painter::LineStyle::Dashed, Color::LightGray);
  78. } else if (guide.orientation() == Guide::Orientation::Vertical) {
  79. int x_coordinate = (int)image_position_to_editor_position({ guide.offset(), 0.0f }).x();
  80. painter.draw_line({ x_coordinate, 0 }, { x_coordinate, rect().height() }, Color::Cyan, 1, Gfx::Painter::LineStyle::Dashed, Color::LightGray);
  81. }
  82. }
  83. }
  84. if (!m_selection.is_empty())
  85. m_selection.paint(painter);
  86. const float pixel_grid_threshold = 15.0f;
  87. if (m_scale > pixel_grid_threshold) {
  88. auto grid_rect = m_editor_image_rect.intersected(event.rect());
  89. auto image_rect = enclosing_int_rect(editor_rect_to_image_rect(grid_rect)).inflated(1, 1);
  90. for (auto i = image_rect.left(); i < image_rect.right(); i++) {
  91. auto start_point = image_position_to_editor_position({ i, image_rect.top() }).to_type<int>();
  92. auto end_point = image_position_to_editor_position({ i, image_rect.bottom() }).to_type<int>();
  93. painter.draw_line(start_point, end_point, Color::LightGray);
  94. }
  95. for (auto i = image_rect.top(); i < image_rect.bottom(); i++) {
  96. auto start_point = image_position_to_editor_position({ image_rect.left(), i }).to_type<int>();
  97. auto end_point = image_position_to_editor_position({ image_rect.right(), i }).to_type<int>();
  98. painter.draw_line(start_point, end_point, Color::LightGray);
  99. }
  100. }
  101. }
  102. Gfx::FloatRect ImageEditor::layer_rect_to_editor_rect(Layer const& layer, Gfx::IntRect const& layer_rect) const
  103. {
  104. return image_rect_to_editor_rect(layer_rect.translated(layer.location()));
  105. }
  106. Gfx::FloatRect ImageEditor::image_rect_to_editor_rect(Gfx::IntRect const& image_rect) const
  107. {
  108. Gfx::FloatRect editor_rect;
  109. editor_rect.set_location(image_position_to_editor_position(image_rect.location()));
  110. editor_rect.set_width((float)image_rect.width() * m_scale);
  111. editor_rect.set_height((float)image_rect.height() * m_scale);
  112. return editor_rect;
  113. }
  114. Gfx::FloatRect ImageEditor::editor_rect_to_image_rect(Gfx::IntRect const& editor_rect) const
  115. {
  116. Gfx::FloatRect image_rect;
  117. image_rect.set_location(editor_position_to_image_position(editor_rect.location()));
  118. image_rect.set_width((float)editor_rect.width() / m_scale);
  119. image_rect.set_height((float)editor_rect.height() / m_scale);
  120. return image_rect;
  121. }
  122. Gfx::FloatPoint ImageEditor::layer_position_to_editor_position(Layer const& layer, Gfx::IntPoint const& layer_position) const
  123. {
  124. return image_position_to_editor_position(layer_position.translated(layer.location()));
  125. }
  126. Gfx::FloatPoint ImageEditor::image_position_to_editor_position(Gfx::IntPoint const& image_position) const
  127. {
  128. Gfx::FloatPoint editor_position;
  129. editor_position.set_x(m_editor_image_rect.x() + ((float)image_position.x() * m_scale));
  130. editor_position.set_y(m_editor_image_rect.y() + ((float)image_position.y() * m_scale));
  131. return editor_position;
  132. }
  133. Gfx::FloatPoint ImageEditor::editor_position_to_image_position(Gfx::IntPoint const& editor_position) const
  134. {
  135. Gfx::FloatPoint image_position;
  136. image_position.set_x(((float)editor_position.x() - m_editor_image_rect.x()) / m_scale);
  137. image_position.set_y(((float)editor_position.y() - m_editor_image_rect.y()) / m_scale);
  138. return image_position;
  139. }
  140. void ImageEditor::second_paint_event(GUI::PaintEvent& event)
  141. {
  142. if (m_active_tool)
  143. m_active_tool->on_second_paint(m_active_layer, event);
  144. }
  145. GUI::MouseEvent ImageEditor::event_with_pan_and_scale_applied(GUI::MouseEvent const& event) const
  146. {
  147. auto image_position = editor_position_to_image_position(event.position());
  148. return {
  149. static_cast<GUI::Event::Type>(event.type()),
  150. Gfx::IntPoint(image_position.x(), image_position.y()),
  151. event.buttons(),
  152. event.button(),
  153. event.modifiers(),
  154. event.wheel_delta()
  155. };
  156. }
  157. GUI::MouseEvent ImageEditor::event_adjusted_for_layer(GUI::MouseEvent const& event, Layer const& layer) const
  158. {
  159. auto image_position = editor_position_to_image_position(event.position());
  160. image_position.translate_by(-layer.location().x(), -layer.location().y());
  161. return {
  162. static_cast<GUI::Event::Type>(event.type()),
  163. Gfx::IntPoint(image_position.x(), image_position.y()),
  164. event.buttons(),
  165. event.button(),
  166. event.modifiers(),
  167. event.wheel_delta()
  168. };
  169. }
  170. void ImageEditor::mousedown_event(GUI::MouseEvent& event)
  171. {
  172. if (event.button() == GUI::MouseButton::Middle) {
  173. m_click_position = event.position();
  174. m_saved_pan_origin = m_pan_origin;
  175. set_override_cursor(Gfx::StandardCursor::Drag);
  176. return;
  177. }
  178. if (!m_active_tool)
  179. return;
  180. if (is<MoveTool>(*m_active_tool)) {
  181. if (auto* other_layer = layer_at_editor_position(event.position())) {
  182. set_active_layer(other_layer);
  183. }
  184. }
  185. auto layer_event = m_active_layer ? event_adjusted_for_layer(event, *m_active_layer) : event;
  186. auto image_event = event_with_pan_and_scale_applied(event);
  187. Tool::MouseEvent tool_event(Tool::MouseEvent::Action::MouseDown, layer_event, image_event, event);
  188. m_active_tool->on_mousedown(m_active_layer.ptr(), tool_event);
  189. }
  190. void ImageEditor::mousemove_event(GUI::MouseEvent& event)
  191. {
  192. if (event.buttons() & GUI::MouseButton::Middle) {
  193. auto delta = event.position() - m_click_position;
  194. m_pan_origin = m_saved_pan_origin.translated(
  195. -delta.x() / m_scale,
  196. -delta.y() / m_scale);
  197. relayout();
  198. return;
  199. }
  200. auto image_event = event_with_pan_and_scale_applied(event);
  201. if (on_image_mouse_position_change) {
  202. on_image_mouse_position_change(image_event.position());
  203. }
  204. if (!m_active_tool)
  205. return;
  206. auto layer_event = m_active_layer ? event_adjusted_for_layer(event, *m_active_layer) : event;
  207. Tool::MouseEvent tool_event(Tool::MouseEvent::Action::MouseDown, layer_event, image_event, event);
  208. m_active_tool->on_mousemove(m_active_layer.ptr(), tool_event);
  209. }
  210. void ImageEditor::mouseup_event(GUI::MouseEvent& event)
  211. {
  212. set_override_cursor(m_active_cursor);
  213. if (!m_active_tool)
  214. return;
  215. auto layer_event = m_active_layer ? event_adjusted_for_layer(event, *m_active_layer) : event;
  216. auto image_event = event_with_pan_and_scale_applied(event);
  217. Tool::MouseEvent tool_event(Tool::MouseEvent::Action::MouseDown, layer_event, image_event, event);
  218. m_active_tool->on_mouseup(m_active_layer.ptr(), tool_event);
  219. }
  220. void ImageEditor::mousewheel_event(GUI::MouseEvent& event)
  221. {
  222. auto scale_delta = -event.wheel_delta() * 0.1f;
  223. scale_centered_on_position(event.position(), scale_delta);
  224. }
  225. void ImageEditor::context_menu_event(GUI::ContextMenuEvent& event)
  226. {
  227. if (!m_active_tool)
  228. return;
  229. m_active_tool->on_context_menu(m_active_layer, event);
  230. }
  231. void ImageEditor::resize_event(GUI::ResizeEvent& event)
  232. {
  233. relayout();
  234. GUI::Frame::resize_event(event);
  235. }
  236. void ImageEditor::keydown_event(GUI::KeyEvent& event)
  237. {
  238. if (m_active_tool)
  239. m_active_tool->on_keydown(event);
  240. }
  241. void ImageEditor::keyup_event(GUI::KeyEvent& event)
  242. {
  243. if (m_active_tool)
  244. m_active_tool->on_keyup(event);
  245. }
  246. void ImageEditor::enter_event(Core::Event&)
  247. {
  248. set_override_cursor(m_active_cursor);
  249. }
  250. void ImageEditor::leave_event(Core::Event&)
  251. {
  252. set_override_cursor(Gfx::StandardCursor::None);
  253. if (on_leave)
  254. on_leave();
  255. }
  256. void ImageEditor::set_active_layer(Layer* layer)
  257. {
  258. if (m_active_layer == layer)
  259. return;
  260. m_active_layer = layer;
  261. if (m_active_layer) {
  262. VERIFY(&m_active_layer->image() == m_image.ptr());
  263. size_t index = 0;
  264. for (; index < m_image->layer_count(); ++index) {
  265. if (&m_image->layer(index) == layer)
  266. break;
  267. }
  268. if (on_active_layer_change)
  269. on_active_layer_change(layer);
  270. } else {
  271. if (on_active_layer_change)
  272. on_active_layer_change({});
  273. }
  274. layers_did_change();
  275. }
  276. void ImageEditor::set_active_tool(Tool* tool)
  277. {
  278. if (m_active_tool == tool)
  279. return;
  280. if (m_active_tool)
  281. m_active_tool->clear();
  282. m_active_tool = tool;
  283. if (m_active_tool) {
  284. m_active_tool->setup(*this);
  285. m_active_tool->on_tool_activation();
  286. m_active_cursor = m_active_tool->cursor();
  287. set_override_cursor(m_active_cursor);
  288. }
  289. }
  290. void ImageEditor::set_guide_visibility(bool show_guides)
  291. {
  292. if (m_show_guides == show_guides)
  293. return;
  294. m_show_guides = show_guides;
  295. if (on_set_guide_visibility)
  296. on_set_guide_visibility(m_show_guides);
  297. update();
  298. }
  299. void ImageEditor::layers_did_change()
  300. {
  301. update();
  302. }
  303. Color ImageEditor::color_for(GUI::MouseButton button) const
  304. {
  305. if (button == GUI::MouseButton::Left)
  306. return m_primary_color;
  307. if (button == GUI::MouseButton::Right)
  308. return m_secondary_color;
  309. VERIFY_NOT_REACHED();
  310. }
  311. Color ImageEditor::color_for(GUI::MouseEvent const& event) const
  312. {
  313. if (event.buttons() & GUI::MouseButton::Left)
  314. return m_primary_color;
  315. if (event.buttons() & GUI::MouseButton::Right)
  316. return m_secondary_color;
  317. VERIFY_NOT_REACHED();
  318. }
  319. void ImageEditor::set_primary_color(Color color)
  320. {
  321. if (m_primary_color == color)
  322. return;
  323. m_primary_color = color;
  324. if (on_primary_color_change)
  325. on_primary_color_change(color);
  326. }
  327. void ImageEditor::set_secondary_color(Color color)
  328. {
  329. if (m_secondary_color == color)
  330. return;
  331. m_secondary_color = color;
  332. if (on_secondary_color_change)
  333. on_secondary_color_change(color);
  334. }
  335. Layer* ImageEditor::layer_at_editor_position(Gfx::IntPoint const& editor_position)
  336. {
  337. auto image_position = editor_position_to_image_position(editor_position);
  338. for (ssize_t i = m_image->layer_count() - 1; i >= 0; --i) {
  339. auto& layer = m_image->layer(i);
  340. if (!layer.is_visible())
  341. continue;
  342. if (layer.relative_rect().contains(Gfx::IntPoint(image_position.x(), image_position.y())))
  343. return const_cast<Layer*>(&layer);
  344. }
  345. return nullptr;
  346. }
  347. void ImageEditor::clamped_scale(float scale_delta)
  348. {
  349. m_scale *= AK::exp2(scale_delta);
  350. if (m_scale < 0.1f)
  351. m_scale = 0.1f;
  352. if (m_scale > 100.0f)
  353. m_scale = 100.0f;
  354. }
  355. void ImageEditor::scale_centered_on_position(Gfx::IntPoint const& position, float scale_delta)
  356. {
  357. auto old_scale = m_scale;
  358. clamped_scale(scale_delta);
  359. Gfx::FloatPoint focus_point {
  360. m_pan_origin.x() - (position.x() - width() / 2.0f) / old_scale,
  361. m_pan_origin.y() - (position.y() - height() / 2.0f) / old_scale
  362. };
  363. m_pan_origin = Gfx::FloatPoint(
  364. focus_point.x() - m_scale / old_scale * (focus_point.x() - m_pan_origin.x()),
  365. focus_point.y() - m_scale / old_scale * (focus_point.y() - m_pan_origin.y()));
  366. if (old_scale != m_scale)
  367. relayout();
  368. }
  369. void ImageEditor::scale_by(float scale_delta)
  370. {
  371. if (scale_delta != 0) {
  372. clamped_scale(scale_delta);
  373. relayout();
  374. }
  375. }
  376. void ImageEditor::fit_image_to_view()
  377. {
  378. const float border_ratio = 0.95f;
  379. auto image_size = image().size();
  380. auto height_ratio = rect().height() / (float)image_size.height();
  381. auto width_ratio = rect().width() / (float)image_size.width();
  382. m_scale = border_ratio * min(height_ratio, width_ratio);
  383. m_pan_origin = Gfx::FloatPoint(0, 0);
  384. relayout();
  385. }
  386. void ImageEditor::reset_scale_and_position()
  387. {
  388. if (m_scale != 1.0f)
  389. m_scale = 1.0f;
  390. m_pan_origin = Gfx::FloatPoint(0, 0);
  391. relayout();
  392. }
  393. void ImageEditor::relayout()
  394. {
  395. Gfx::IntSize new_size;
  396. new_size.set_width(image().size().width() * m_scale);
  397. new_size.set_height(image().size().height() * m_scale);
  398. m_editor_image_rect.set_size(new_size);
  399. Gfx::IntPoint new_location;
  400. new_location.set_x((width() / 2) - (new_size.width() / 2) - (m_pan_origin.x() * m_scale));
  401. new_location.set_y((height() / 2) - (new_size.height() / 2) - (m_pan_origin.y() * m_scale));
  402. m_editor_image_rect.set_location(new_location);
  403. update();
  404. }
  405. void ImageEditor::image_did_change(Gfx::IntRect const& modified_image_rect)
  406. {
  407. update(m_editor_image_rect.intersected(enclosing_int_rect(image_rect_to_editor_rect(modified_image_rect))));
  408. }
  409. void ImageEditor::image_did_change_rect(Gfx::IntRect const& new_image_rect)
  410. {
  411. m_editor_image_rect = enclosing_int_rect(image_rect_to_editor_rect(new_image_rect));
  412. update(m_editor_image_rect);
  413. }
  414. void ImageEditor::image_did_change_title(String const& path)
  415. {
  416. if (on_image_title_change)
  417. on_image_title_change(path);
  418. }
  419. void ImageEditor::image_select_layer(Layer* layer)
  420. {
  421. set_active_layer(layer);
  422. }
  423. Result<void, String> ImageEditor::save_project_to_fd_and_close(int fd) const
  424. {
  425. StringBuilder builder;
  426. JsonObjectSerializer json(builder);
  427. m_image->serialize_as_json(json);
  428. auto json_guides = json.add_array("guides");
  429. for (const auto& guide : m_guides) {
  430. auto json_guide = json_guides.add_object();
  431. json_guide.add("offset"sv, (double)guide.offset());
  432. if (guide.orientation() == Guide::Orientation::Vertical)
  433. json_guide.add("orientation", "vertical");
  434. else if (guide.orientation() == Guide::Orientation::Horizontal)
  435. json_guide.add("orientation", "horizontal");
  436. json_guide.finish();
  437. }
  438. json_guides.finish();
  439. json.finish();
  440. auto file = Core::File::construct();
  441. file->open(fd, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate, Core::File::ShouldCloseFileDescriptor::Yes);
  442. if (file->has_error())
  443. return String { file->error_string() };
  444. if (!file->write(builder.string_view()))
  445. return String { file->error_string() };
  446. return {};
  447. }
  448. }