FormWidget.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "FormWidget.h"
  2. #include "FormEditorWidget.h"
  3. #include "Tool.h"
  4. #include <LibGUI/GPainter.h>
  5. FormWidget::FormWidget(FormEditorWidget& parent)
  6. : GWidget(&parent)
  7. {
  8. set_fill_with_background_color(true);
  9. set_background_color(SystemColor::Window);
  10. set_relative_rect(5, 5, 400, 300);
  11. set_greedy_for_hits(true);
  12. }
  13. FormWidget::~FormWidget()
  14. {
  15. }
  16. FormEditorWidget& FormWidget::editor()
  17. {
  18. return static_cast<FormEditorWidget&>(*parent());
  19. }
  20. const FormEditorWidget& FormWidget::editor() const
  21. {
  22. return static_cast<const FormEditorWidget&>(*parent());
  23. }
  24. void FormWidget::paint_event(GPaintEvent& event)
  25. {
  26. GPainter painter(*this);
  27. painter.add_clip_rect(event.rect());
  28. for (int y = 0; y < height(); y += m_grid_size) {
  29. for (int x = 0; x < width(); x += m_grid_size) {
  30. painter.set_pixel({ x, y }, Color::from_rgb(0x404040));
  31. }
  32. }
  33. }
  34. void FormWidget::second_paint_event(GPaintEvent& event)
  35. {
  36. GPainter painter(*this);
  37. painter.add_clip_rect(event.rect());
  38. if (!editor().selection().is_empty()) {
  39. for_each_child_widget([&](auto& child) {
  40. if (editor().selection().contains(child)) {
  41. painter.draw_rect(child.relative_rect(), Color::Blue);
  42. }
  43. return IterationDecision::Continue;
  44. });
  45. }
  46. editor().tool().on_second_paint(painter, event);
  47. }
  48. void FormWidget::mousedown_event(GMouseEvent& event)
  49. {
  50. editor().tool().on_mousedown(event);
  51. }
  52. void FormWidget::mouseup_event(GMouseEvent& event)
  53. {
  54. editor().tool().on_mouseup(event);
  55. }
  56. void FormWidget::mousemove_event(GMouseEvent& event)
  57. {
  58. editor().tool().on_mousemove(event);
  59. }
  60. void FormWidget::keydown_event(GKeyEvent& event)
  61. {
  62. editor().tool().on_keydown(event);
  63. }