summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/ImageWidget.cpp
blob: fd15840c07bf2234bb0be733903178b9aa1e8811 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/MappedFile.h>
#include <LibGUI/ImageWidget.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>

REGISTER_WIDGET(GUI, ImageWidget)

namespace GUI {

ImageWidget::ImageWidget(const StringView&)
    : m_timer(Core::Timer::construct())

{
    set_frame_thickness(0);
    set_frame_shadow(Gfx::FrameShadow::Plain);
    set_frame_shape(Gfx::FrameShape::NoFrame);
    set_auto_resize(true);

    REGISTER_BOOL_PROPERTY("auto_resize", auto_resize, set_auto_resize);
    REGISTER_BOOL_PROPERTY("should_stretch", should_stretch, set_should_stretch);
}

ImageWidget::~ImageWidget()
{
}

void ImageWidget::set_bitmap(const Gfx::Bitmap* bitmap)
{
    if (m_bitmap == bitmap)
        return;

    m_bitmap = bitmap;
    if (m_bitmap && m_auto_resize)
        set_fixed_size(m_bitmap->size());

    update();
}

void ImageWidget::set_auto_resize(bool value)
{
    m_auto_resize = value;

    if (m_bitmap)
        set_fixed_size(m_bitmap->size());
}

// Same as QSWidget::animate(), you probably want to keep any changes in sync
void ImageWidget::animate()
{
    m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();

    const auto& current_frame = m_image_decoder->frame(m_current_frame_index);
    set_bitmap(current_frame.image);

    if (current_frame.duration != m_timer->interval()) {
        m_timer->restart(current_frame.duration);
    }

    if (m_current_frame_index == m_image_decoder->frame_count() - 1) {
        ++m_loops_completed;
        if (m_loops_completed > 0 && m_loops_completed == m_image_decoder->loop_count()) {
            m_timer->stop();
        }
    }
}

void ImageWidget::load_from_file(const StringView& path)
{
    auto file_or_error = MappedFile::map(path);
    if (file_or_error.is_error())
        return;

    auto& mapped_file = *file_or_error.value();
    m_image_decoder = Gfx::ImageDecoder::create((const u8*)mapped_file.data(), mapped_file.size());
    auto bitmap = m_image_decoder->bitmap();
    VERIFY(bitmap);

    set_bitmap(bitmap);

    if (m_image_decoder->is_animated() && m_image_decoder->frame_count() > 1) {
        const auto& first_frame = m_image_decoder->frame(0);
        m_timer->set_interval(first_frame.duration);
        m_timer->on_timeout = [this] { animate(); };
        m_timer->start();
    }
}

void ImageWidget::mousedown_event(GUI::MouseEvent&)
{
    if (on_click)
        on_click();
}

void ImageWidget::paint_event(PaintEvent& event)
{
    Frame::paint_event(event);

    if (!m_bitmap) {
        return;
    }

    Painter painter(*this);
    painter.add_clip_rect(event.rect());

    if (m_should_stretch) {
        painter.draw_scaled_bitmap(frame_inner_rect(), *m_bitmap, m_bitmap->rect(), (float)opacity_percent() / 100.0f);
    } else {
        auto location = frame_inner_rect().center().translated(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2));
        painter.blit(location, *m_bitmap, m_bitmap->rect(), (float)opacity_percent() / 100.0f);
    }
}

void ImageWidget::set_opacity_percent(int percent)
{
    if (m_opacity_percent == percent)
        return;
    m_opacity_percent = percent;
    update();
}

}