ImageEditor.cpp 12 KB

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