ImageWidget.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/MappedFile.h>
  27. #include <LibGUI/ImageWidget.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGfx/Bitmap.h>
  30. #include <LibGfx/ImageDecoder.h>
  31. namespace GUI {
  32. ImageWidget::ImageWidget(const StringView&)
  33. : m_timer(Core::Timer::construct())
  34. {
  35. set_frame_thickness(0);
  36. set_frame_shadow(Gfx::FrameShadow::Plain);
  37. set_frame_shape(Gfx::FrameShape::NoFrame);
  38. set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  39. set_auto_resize(true);
  40. }
  41. ImageWidget::~ImageWidget()
  42. {
  43. }
  44. void ImageWidget::set_bitmap(const Gfx::Bitmap* bitmap)
  45. {
  46. if (m_bitmap == bitmap)
  47. return;
  48. m_bitmap = bitmap;
  49. if (m_bitmap && m_auto_resize)
  50. set_preferred_size(m_bitmap->width(), m_bitmap->height());
  51. update();
  52. }
  53. void ImageWidget::set_auto_resize(bool value)
  54. {
  55. m_auto_resize = value;
  56. if (m_bitmap)
  57. set_preferred_size(m_bitmap->width(), m_bitmap->height());
  58. }
  59. void ImageWidget::animate()
  60. {
  61. m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();
  62. const auto& current_frame = m_image_decoder->frame(m_current_frame_index);
  63. set_bitmap(current_frame.image);
  64. if (current_frame.duration != m_timer->interval()) {
  65. m_timer->restart(current_frame.duration);
  66. }
  67. if (m_current_frame_index == m_image_decoder->frame_count() - 1) {
  68. ++m_loops_completed;
  69. if (m_loops_completed > 0 && m_loops_completed == m_image_decoder->loop_count()) {
  70. m_timer->stop();
  71. }
  72. }
  73. }
  74. void ImageWidget::load_from_file(const StringView& path)
  75. {
  76. MappedFile mapped_file(path);
  77. if (!mapped_file.is_valid())
  78. return;
  79. m_image_decoder = Gfx::ImageDecoder::create((const u8*)mapped_file.data(), mapped_file.size());
  80. auto bitmap = m_image_decoder->bitmap();
  81. ASSERT(bitmap);
  82. set_bitmap(bitmap);
  83. if (path.ends_with(".gif")) {
  84. if (m_image_decoder->is_animated() && m_image_decoder->frame_count() > 1) {
  85. const auto& first_frame = m_image_decoder->frame(0);
  86. m_timer->set_interval(first_frame.duration);
  87. m_timer->on_timeout = [this] { animate(); };
  88. m_timer->start();
  89. }
  90. }
  91. }
  92. void ImageWidget::mousedown_event(GUI::MouseEvent&)
  93. {
  94. if (on_click)
  95. on_click();
  96. }
  97. void ImageWidget::paint_event(PaintEvent& event)
  98. {
  99. Frame::paint_event(event);
  100. if (!m_bitmap) {
  101. return;
  102. }
  103. Painter painter(*this);
  104. if (m_should_stretch) {
  105. painter.draw_scaled_bitmap(frame_inner_rect(), *m_bitmap, m_bitmap->rect());
  106. } else {
  107. auto location = frame_inner_rect().center().translated(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2));
  108. painter.blit(location, *m_bitmap, m_bitmap->rect());
  109. }
  110. }
  111. }