ImageEditor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ImageEditor.h"
  27. #include "Image.h"
  28. #include "Layer.h"
  29. #include "MoveTool.h"
  30. #include "Tool.h"
  31. #include <LibGUI/Command.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibGfx/Palette.h>
  34. #include <LibGfx/Rect.h>
  35. namespace PixelPaint {
  36. ImageEditor::ImageEditor()
  37. : m_undo_stack(make<GUI::UndoStack>())
  38. {
  39. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  40. }
  41. ImageEditor::~ImageEditor()
  42. {
  43. if (m_image)
  44. m_image->remove_client(*this);
  45. }
  46. void ImageEditor::set_image(RefPtr<Image> image)
  47. {
  48. if (m_image)
  49. m_image->remove_client(*this);
  50. m_image = move(image);
  51. m_active_layer = nullptr;
  52. m_undo_stack = make<GUI::UndoStack>();
  53. m_undo_stack->push(make<ImageUndoCommand>(*m_image));
  54. update();
  55. relayout();
  56. if (m_image)
  57. m_image->add_client(*this);
  58. }
  59. void ImageEditor::did_complete_action()
  60. {
  61. if (!m_image)
  62. return;
  63. m_undo_stack->finalize_current_combo();
  64. m_undo_stack->push(make<ImageUndoCommand>(*m_image));
  65. }
  66. bool ImageEditor::undo()
  67. {
  68. if (!m_image)
  69. return false;
  70. if (m_undo_stack->can_undo()) {
  71. m_undo_stack->undo();
  72. layers_did_change();
  73. return true;
  74. }
  75. return false;
  76. }
  77. bool ImageEditor::redo()
  78. {
  79. if (!m_image)
  80. return false;
  81. if (m_undo_stack->can_redo()) {
  82. m_undo_stack->redo();
  83. layers_did_change();
  84. return true;
  85. }
  86. return false;
  87. }
  88. void ImageEditor::paint_event(GUI::PaintEvent& event)
  89. {
  90. GUI::Frame::paint_event(event);
  91. GUI::Painter painter(*this);
  92. painter.add_clip_rect(event.rect());
  93. painter.add_clip_rect(frame_inner_rect());
  94. Gfx::StylePainter::paint_transparency_grid(painter, rect(), palette());
  95. if (m_image) {
  96. painter.draw_rect(m_editor_image_rect.inflated(2, 2), Color::Black);
  97. m_image->paint_into(painter, m_editor_image_rect);
  98. }
  99. if (m_active_layer) {
  100. painter.draw_rect(enclosing_int_rect(image_rect_to_editor_rect(m_active_layer->relative_rect())).inflated(2, 2), Color::Black);
  101. }
  102. }
  103. Gfx::FloatRect ImageEditor::layer_rect_to_editor_rect(const Layer& layer, const Gfx::IntRect& layer_rect) const
  104. {
  105. return image_rect_to_editor_rect(layer_rect.translated(layer.location()));
  106. }
  107. Gfx::FloatRect ImageEditor::image_rect_to_editor_rect(const Gfx::IntRect& image_rect) const
  108. {
  109. Gfx::FloatRect editor_rect;
  110. editor_rect.set_location(image_position_to_editor_position(image_rect.location()));
  111. editor_rect.set_width((float)image_rect.width() * m_scale);
  112. editor_rect.set_height((float)image_rect.height() * m_scale);
  113. return editor_rect;
  114. }
  115. Gfx::FloatRect ImageEditor::editor_rect_to_image_rect(const Gfx::IntRect& editor_rect) const
  116. {
  117. Gfx::FloatRect image_rect;
  118. image_rect.set_location(editor_position_to_image_position(editor_rect.location()));
  119. image_rect.set_width((float)editor_rect.width() / m_scale);
  120. image_rect.set_height((float)editor_rect.height() / m_scale);
  121. return image_rect;
  122. }
  123. Gfx::FloatPoint ImageEditor::layer_position_to_editor_position(const Layer& layer, const Gfx::IntPoint& layer_position) const
  124. {
  125. return image_position_to_editor_position(layer_position.translated(layer.location()));
  126. }
  127. Gfx::FloatPoint ImageEditor::image_position_to_editor_position(const Gfx::IntPoint& image_position) const
  128. {
  129. Gfx::FloatPoint editor_position;
  130. editor_position.set_x(m_editor_image_rect.x() + ((float)image_position.x() * m_scale));
  131. editor_position.set_y(m_editor_image_rect.y() + ((float)image_position.y() * m_scale));
  132. return editor_position;
  133. }
  134. Gfx::FloatPoint ImageEditor::editor_position_to_image_position(const Gfx::IntPoint& editor_position) const
  135. {
  136. Gfx::FloatPoint image_position;
  137. image_position.set_x(((float)editor_position.x() - m_editor_image_rect.x()) / m_scale);
  138. image_position.set_y(((float)editor_position.y() - m_editor_image_rect.y()) / m_scale);
  139. return image_position;
  140. }
  141. void ImageEditor::second_paint_event(GUI::PaintEvent& event)
  142. {
  143. if (m_active_tool && m_active_layer)
  144. m_active_tool->on_second_paint(*m_active_layer, event);
  145. }
  146. GUI::MouseEvent ImageEditor::event_with_pan_and_scale_applied(const GUI::MouseEvent& event) const
  147. {
  148. auto image_position = editor_position_to_image_position(event.position());
  149. return {
  150. static_cast<GUI::Event::Type>(event.type()),
  151. Gfx::IntPoint(image_position.x(), image_position.y()),
  152. event.buttons(),
  153. event.button(),
  154. event.modifiers(),
  155. event.wheel_delta()
  156. };
  157. }
  158. GUI::MouseEvent ImageEditor::event_adjusted_for_layer(const GUI::MouseEvent& event, const Layer& layer) const
  159. {
  160. auto image_position = editor_position_to_image_position(event.position());
  161. image_position.move_by(-layer.location().x(), -layer.location().y());
  162. return {
  163. static_cast<GUI::Event::Type>(event.type()),
  164. Gfx::IntPoint(image_position.x(), image_position.y()),
  165. event.buttons(),
  166. event.button(),
  167. event.modifiers(),
  168. event.wheel_delta()
  169. };
  170. }
  171. void ImageEditor::mousedown_event(GUI::MouseEvent& event)
  172. {
  173. if (event.button() == GUI::MouseButton::Middle) {
  174. m_click_position = event.position();
  175. m_saved_pan_origin = m_pan_origin;
  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. if (!m_active_layer)
  186. return;
  187. auto layer_event = event_adjusted_for_layer(event, *m_active_layer);
  188. auto image_event = event_with_pan_and_scale_applied(event);
  189. m_active_tool->on_mousedown(*m_active_layer, layer_event, image_event);
  190. }
  191. void ImageEditor::mousemove_event(GUI::MouseEvent& event)
  192. {
  193. if (event.buttons() & GUI::MouseButton::Middle) {
  194. auto delta = event.position() - m_click_position;
  195. m_pan_origin = m_saved_pan_origin.translated(
  196. -delta.x() / m_scale,
  197. -delta.y() / m_scale);
  198. relayout();
  199. return;
  200. }
  201. if (!m_active_layer || !m_active_tool)
  202. return;
  203. auto layer_event = event_adjusted_for_layer(event, *m_active_layer);
  204. auto image_event = event_with_pan_and_scale_applied(event);
  205. m_active_tool->on_mousemove(*m_active_layer, layer_event, image_event);
  206. }
  207. void ImageEditor::mouseup_event(GUI::MouseEvent& event)
  208. {
  209. if (!m_active_layer || !m_active_tool)
  210. return;
  211. auto layer_event = event_adjusted_for_layer(event, *m_active_layer);
  212. auto image_event = event_with_pan_and_scale_applied(event);
  213. m_active_tool->on_mouseup(*m_active_layer, layer_event, image_event);
  214. }
  215. void ImageEditor::mousewheel_event(GUI::MouseEvent& event)
  216. {
  217. auto scale_delta = -event.wheel_delta() * 0.1f;
  218. scale_centered_on_position(event.position(), scale_delta);
  219. }
  220. void ImageEditor::context_menu_event(GUI::ContextMenuEvent& event)
  221. {
  222. if (!m_active_layer || !m_active_tool)
  223. return;
  224. m_active_tool->on_context_menu(*m_active_layer, event);
  225. }
  226. void ImageEditor::resize_event(GUI::ResizeEvent& event)
  227. {
  228. relayout();
  229. GUI::Frame::resize_event(event);
  230. }
  231. void ImageEditor::keydown_event(GUI::KeyEvent& event)
  232. {
  233. if (m_active_tool)
  234. m_active_tool->on_keydown(event);
  235. }
  236. void ImageEditor::keyup_event(GUI::KeyEvent& event)
  237. {
  238. if (m_active_tool)
  239. m_active_tool->on_keyup(event);
  240. }
  241. void ImageEditor::set_active_layer(Layer* layer)
  242. {
  243. if (m_active_layer == layer)
  244. return;
  245. m_active_layer = layer;
  246. if (m_active_layer) {
  247. size_t index = 0;
  248. for (; index < m_image->layer_count(); ++index) {
  249. if (&m_image->layer(index) == layer)
  250. break;
  251. }
  252. if (on_active_layer_change)
  253. on_active_layer_change(layer);
  254. } else {
  255. if (on_active_layer_change)
  256. on_active_layer_change({});
  257. }
  258. layers_did_change();
  259. }
  260. void ImageEditor::set_active_tool(Tool* tool)
  261. {
  262. if (m_active_tool == tool)
  263. return;
  264. if (m_active_tool)
  265. m_active_tool->clear();
  266. m_active_tool = tool;
  267. if (m_active_tool)
  268. m_active_tool->setup(*this);
  269. }
  270. void ImageEditor::layers_did_change()
  271. {
  272. update();
  273. }
  274. Color ImageEditor::color_for(GUI::MouseButton button) const
  275. {
  276. if (button == GUI::MouseButton::Left)
  277. return m_primary_color;
  278. if (button == GUI::MouseButton::Right)
  279. return m_secondary_color;
  280. VERIFY_NOT_REACHED();
  281. }
  282. Color ImageEditor::color_for(const GUI::MouseEvent& event) const
  283. {
  284. if (event.buttons() & GUI::MouseButton::Left)
  285. return m_primary_color;
  286. if (event.buttons() & GUI::MouseButton::Right)
  287. return m_secondary_color;
  288. VERIFY_NOT_REACHED();
  289. }
  290. void ImageEditor::set_primary_color(Color color)
  291. {
  292. if (m_primary_color == color)
  293. return;
  294. m_primary_color = color;
  295. if (on_primary_color_change)
  296. on_primary_color_change(color);
  297. }
  298. void ImageEditor::set_secondary_color(Color color)
  299. {
  300. if (m_secondary_color == color)
  301. return;
  302. m_secondary_color = color;
  303. if (on_secondary_color_change)
  304. on_secondary_color_change(color);
  305. }
  306. Layer* ImageEditor::layer_at_editor_position(const Gfx::IntPoint& editor_position)
  307. {
  308. if (!m_image)
  309. return nullptr;
  310. auto image_position = editor_position_to_image_position(editor_position);
  311. for (ssize_t i = m_image->layer_count() - 1; i >= 0; --i) {
  312. auto& layer = m_image->layer(i);
  313. if (!layer.is_visible())
  314. continue;
  315. if (layer.relative_rect().contains(Gfx::IntPoint(image_position.x(), image_position.y())))
  316. return const_cast<Layer*>(&layer);
  317. }
  318. return nullptr;
  319. }
  320. void ImageEditor::scale_centered_on_position(const Gfx::IntPoint& position, float scale_delta)
  321. {
  322. auto old_scale = m_scale;
  323. m_scale += scale_delta;
  324. if (m_scale < 0.1f)
  325. m_scale = 0.1f;
  326. if (m_scale > 100.0f)
  327. m_scale = 100.0f;
  328. Gfx::FloatPoint focus_point {
  329. m_pan_origin.x() - (position.x() - width() / 2.0f) / old_scale,
  330. m_pan_origin.y() - (position.y() - height() / 2.0f) / old_scale
  331. };
  332. m_pan_origin = Gfx::FloatPoint(
  333. focus_point.x() - m_scale / old_scale * (focus_point.x() - m_pan_origin.x()),
  334. focus_point.y() - m_scale / old_scale * (focus_point.y() - m_pan_origin.y()));
  335. if (old_scale != m_scale)
  336. relayout();
  337. }
  338. void ImageEditor::relayout()
  339. {
  340. if (!image())
  341. return;
  342. auto& image = *this->image();
  343. Gfx::IntSize new_size;
  344. new_size.set_width(image.size().width() * m_scale);
  345. new_size.set_height(image.size().height() * m_scale);
  346. m_editor_image_rect.set_size(new_size);
  347. Gfx::IntPoint new_location;
  348. new_location.set_x((width() / 2) - (new_size.width() / 2) - (m_pan_origin.x() * m_scale));
  349. new_location.set_y((height() / 2) - (new_size.height() / 2) - (m_pan_origin.y() * m_scale));
  350. m_editor_image_rect.set_location(new_location);
  351. update();
  352. }
  353. void ImageEditor::image_did_change()
  354. {
  355. update();
  356. }
  357. void ImageEditor::image_select_layer(Layer* layer)
  358. {
  359. set_active_layer(layer);
  360. }
  361. }