Painter.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #include "Color.h"
  3. #include "Point.h"
  4. #include "Rect.h"
  5. #include "Size.h"
  6. #include <AK/AKString.h>
  7. class CharacterBitmap;
  8. class GlyphBitmap;
  9. class GraphicsBitmap;
  10. class Font;
  11. #ifdef USERLAND
  12. class GWidget;
  13. class GWindow;
  14. #endif
  15. enum class TextAlignment { TopLeft, CenterLeft, Center, CenterRight };
  16. class Painter {
  17. public:
  18. #ifdef USERLAND
  19. explicit Painter(GWidget&);
  20. #endif
  21. explicit Painter(GraphicsBitmap&);
  22. ~Painter();
  23. void fill_rect(const Rect&, Color);
  24. void fill_rect_with_gradient(const Rect&, Color gradient_start, Color gradient_end);
  25. void draw_rect(const Rect&, Color, bool rough = false);
  26. void draw_bitmap(const Point&, const CharacterBitmap&, Color = Color());
  27. void draw_bitmap(const Point&, const GlyphBitmap&, Color = Color());
  28. void set_pixel(const Point&, Color);
  29. void draw_line(const Point&, const Point&, Color);
  30. void draw_focus_rect(const Rect&);
  31. void blit(const Point&, const GraphicsBitmap&, const Rect& src_rect);
  32. void blit_with_opacity(const Point&, const GraphicsBitmap&, const Rect& src_rect, float opacity);
  33. void draw_text(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, Color = Color());
  34. void draw_glyph(const Point&, char, Color);
  35. const Font& font() const { return *m_font; }
  36. void set_font(Font& font) { m_font = &font; }
  37. enum class DrawOp { Copy, Xor };
  38. void set_draw_op(DrawOp op) { m_draw_op = op; }
  39. DrawOp draw_op() const { return m_draw_op; }
  40. void set_clip_rect(const Rect& rect);
  41. void clear_clip_rect();
  42. Rect clip_rect() const { return m_clip_rect; }
  43. void translate(int dx, int dy) { m_translation.move_by(dx, dy); }
  44. void translate(const Point& delta) { m_translation.move_by(delta); }
  45. GraphicsBitmap* target() { return m_target.ptr(); }
  46. private:
  47. void set_pixel_with_draw_op(dword& pixel, const Color&);
  48. void fill_rect_with_draw_op(const Rect&, Color);
  49. void blit_with_alpha(const Point&, const GraphicsBitmap&, const Rect& src_rect);
  50. const Font* m_font;
  51. Point m_translation;
  52. Rect m_clip_rect;
  53. GWindow* m_window { nullptr };
  54. Retained<GraphicsBitmap> m_target;
  55. DrawOp m_draw_op { DrawOp::Copy };
  56. };