summaryrefslogtreecommitdiff
path: root/SharedGraphics/Painter.h
blob: a6482e89f27754810722b4348ad3e1ce0d8c2609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once

#include "Color.h"
#include "Point.h"
#include "Rect.h"
#include "Size.h"
#include <AK/AKString.h>

class CharacterBitmap;
class GlyphBitmap;
class GraphicsBitmap;
class Font;

#ifdef USERLAND
class GWidget;
class GWindow;
#endif

enum class TextAlignment { TopLeft, CenterLeft, Center, CenterRight };

class Painter {
public:
#ifdef USERLAND
    explicit Painter(GWidget&);
#endif
    explicit Painter(GraphicsBitmap&);
    ~Painter();
    void fill_rect(const Rect&, Color);
    void fill_rect_with_gradient(const Rect&, Color gradient_start, Color gradient_end);
    void draw_rect(const Rect&, Color, bool rough = false);
    void draw_bitmap(const Point&, const CharacterBitmap&, Color = Color());
    void draw_bitmap(const Point&, const GlyphBitmap&, Color = Color());
    void set_pixel(const Point&, Color);
    void draw_line(const Point&, const Point&, Color);
    void draw_focus_rect(const Rect&);
    void blit(const Point&, const GraphicsBitmap&, const Rect& src_rect);
    void blit_with_opacity(const Point&, const GraphicsBitmap&, const Rect& src_rect, float opacity);

    void draw_text(const Rect&, const String&, TextAlignment = TextAlignment::TopLeft, Color = Color());
    void draw_glyph(const Point&, char, Color);

    const Font& font() const { return *m_font; }
    void set_font(Font& font) { m_font = &font; }

    enum class DrawOp { Copy, Xor };
    void set_draw_op(DrawOp op) { m_draw_op = op; }
    DrawOp draw_op() const { return m_draw_op; }

    void set_clip_rect(const Rect& rect);
    void clear_clip_rect();
    Rect clip_rect() const { return m_clip_rect; }

    void translate(int dx, int dy) { m_translation.move_by(dx, dy); }
    void translate(const Point& delta) { m_translation.move_by(delta); }

    GraphicsBitmap* target() { return m_target.ptr(); }

private:
    void set_pixel_with_draw_op(dword& pixel, const Color&);
    void fill_rect_with_draw_op(const Rect&, Color);
    void blit_with_alpha(const Point&, const GraphicsBitmap&, const Rect& src_rect);

    const Font* m_font;
    Point m_translation;
    Rect m_clip_rect;
    RetainPtr<GraphicsBitmap> m_target;
#ifdef USERLAND
    GWindow* m_window { nullptr };
#endif
    DrawOp m_draw_op { DrawOp::Copy };
};