ImageEditor.cpp 11 KB

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