summaryrefslogtreecommitdiff
path: root/Userland/Services/WindowServer/Cursor.h
blob: e36ebd2945ce63360a575ad4dd37a6266a4a5e9a (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
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/HashMap.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/StandardCursor.h>

namespace WindowServer {

class CursorParams {
    friend class Cursor;

public:
    static CursorParams parse_from_filename(const StringView&, const Gfx::IntPoint&);
    CursorParams(const Gfx::IntPoint& hotspot)
        : m_hotspot(hotspot)
    {
    }
    CursorParams constrained(const Gfx::Bitmap&) const;

    const Gfx::IntPoint& hotspot() const { return m_hotspot; }
    unsigned frames() const { return m_frames; }
    unsigned frame_ms() const { return m_frame_ms; }

private:
    CursorParams() = default;
    Gfx::IntPoint m_hotspot;
    unsigned m_frames { 1 };
    unsigned m_frame_ms { 0 };
    bool m_have_hotspot { false };
};

class Cursor : public RefCounted<Cursor> {
public:
    static RefPtr<Cursor> create(const StringView&, const StringView&);
    static NonnullRefPtr<Cursor> create(NonnullRefPtr<Gfx::Bitmap>&&, int);
    static RefPtr<Cursor> create(Gfx::StandardCursor);
    ~Cursor() = default;

    const CursorParams& params() const { return m_params; }
    const Gfx::Bitmap& bitmap(int scale_factor) const
    {
        auto it = m_bitmaps.find(scale_factor);
        if (it == m_bitmaps.end()) {
            it = m_bitmaps.find(1);
            if (it == m_bitmaps.end())
                it = m_bitmaps.begin();
        }
        // We better found something
        if (it == m_bitmaps.end()) {
            dbgln("Could not find any bitmap in this Cursor");
            VERIFY_NOT_REACHED();
        }
        return it->value;
    }

    Gfx::IntRect source_rect(unsigned frame) const
    {
        return m_rect.translated(frame * m_rect.width(), 0);
    }

    Gfx::IntRect rect() const { return m_rect; }
    Gfx::IntSize size() const { return m_rect.size(); }

private:
    Cursor() { }
    Cursor(NonnullRefPtr<Gfx::Bitmap>&&, int, const CursorParams&);

    bool load(const StringView&, const StringView&);
    void update_rect_if_animated();

    HashMap<int, NonnullRefPtr<Gfx::Bitmap>> m_bitmaps;
    CursorParams m_params;
    Gfx::IntRect m_rect;
};

}