FormWidget.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "FormWidget.h"
  7. #include "FormEditorWidget.h"
  8. #include "Tool.h"
  9. #include <LibGUI/Painter.h>
  10. namespace HackStudio {
  11. FormWidget::FormWidget()
  12. {
  13. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  14. set_fill_with_background_color(true);
  15. set_relative_rect(5, 5, 400, 300);
  16. set_greedy_for_hits(true);
  17. }
  18. FormWidget::~FormWidget()
  19. {
  20. }
  21. FormEditorWidget& FormWidget::editor()
  22. {
  23. return static_cast<FormEditorWidget&>(*parent());
  24. }
  25. const FormEditorWidget& FormWidget::editor() const
  26. {
  27. return static_cast<const FormEditorWidget&>(*parent());
  28. }
  29. void FormWidget::paint_event(GUI::PaintEvent& event)
  30. {
  31. GUI::Painter painter(*this);
  32. painter.add_clip_rect(event.rect());
  33. for (int y = 0; y < height(); y += m_grid_size) {
  34. for (int x = 0; x < width(); x += m_grid_size) {
  35. painter.set_pixel({ x, y }, Color::from_rgb(0x404040));
  36. }
  37. }
  38. }
  39. void FormWidget::second_paint_event(GUI::PaintEvent& event)
  40. {
  41. GUI::Painter painter(*this);
  42. painter.add_clip_rect(event.rect());
  43. if (!editor().selection().is_empty()) {
  44. for_each_child_widget([&](auto& child) {
  45. if (editor().selection().contains(child)) {
  46. painter.draw_rect(child.relative_rect(), Color::Blue);
  47. }
  48. return IterationDecision::Continue;
  49. });
  50. }
  51. editor().tool().on_second_paint(painter, event);
  52. }
  53. void FormWidget::mousedown_event(GUI::MouseEvent& event)
  54. {
  55. editor().tool().on_mousedown(event);
  56. }
  57. void FormWidget::mouseup_event(GUI::MouseEvent& event)
  58. {
  59. editor().tool().on_mouseup(event);
  60. }
  61. void FormWidget::mousemove_event(GUI::MouseEvent& event)
  62. {
  63. editor().tool().on_mousemove(event);
  64. }
  65. void FormWidget::keydown_event(GUI::KeyEvent& event)
  66. {
  67. editor().tool().on_keydown(event);
  68. }
  69. }