FormWidget.cpp 1.7 KB

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