ImageWidget.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_auto_resize(true);
  39. REGISTER_BOOL_PROPERTY("auto_resize", auto_resize, set_auto_resize);
  40. REGISTER_BOOL_PROPERTY("should_stretch", should_stretch, set_should_stretch);
  41. }
  42. ImageWidget::~ImageWidget()
  43. {
  44. }
  45. void ImageWidget::set_bitmap(const Gfx::Bitmap* bitmap)
  46. {
  47. if (m_bitmap == bitmap)
  48. return;
  49. m_bitmap = bitmap;
  50. if (m_bitmap && m_auto_resize)
  51. set_fixed_size(m_bitmap->size());
  52. update();
  53. }
  54. void ImageWidget::set_auto_resize(bool value)
  55. {
  56. m_auto_resize = value;
  57. if (m_bitmap)
  58. set_fixed_size(m_bitmap->size());
  59. }
  60. void ImageWidget::animate()
  61. {
  62. m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();
  63. const auto& current_frame = m_image_decoder->frame(m_current_frame_index);
  64. set_bitmap(current_frame.image);
  65. if (current_frame.duration != m_timer->interval()) {
  66. m_timer->restart(current_frame.duration);
  67. }
  68. if (m_current_frame_index == m_image_decoder->frame_count() - 1) {
  69. ++m_loops_completed;
  70. if (m_loops_completed > 0 && m_loops_completed == m_image_decoder->loop_count()) {
  71. m_timer->stop();
  72. }
  73. }
  74. }
  75. void ImageWidget::load_from_file(const StringView& path)
  76. {
  77. MappedFile mapped_file(path);
  78. if (!mapped_file.is_valid())
  79. return;
  80. m_image_decoder = Gfx::ImageDecoder::create((const u8*)mapped_file.data(), mapped_file.size());
  81. auto bitmap = m_image_decoder->bitmap();
  82. ASSERT(bitmap);
  83. set_bitmap(bitmap);
  84. if (path.ends_with(".gif")) {
  85. if (m_image_decoder->is_animated() && m_image_decoder->frame_count() > 1) {
  86. const auto& first_frame = m_image_decoder->frame(0);
  87. m_timer->set_interval(first_frame.duration);
  88. m_timer->on_timeout = [this] { animate(); };
  89. m_timer->start();
  90. }
  91. }
  92. }
  93. void ImageWidget::mousedown_event(GUI::MouseEvent&)
  94. {
  95. if (on_click)
  96. on_click();
  97. }
  98. void ImageWidget::paint_event(PaintEvent& event)
  99. {
  100. Frame::paint_event(event);
  101. if (!m_bitmap) {
  102. return;
  103. }
  104. Painter painter(*this);
  105. if (m_should_stretch) {
  106. painter.draw_scaled_bitmap(frame_inner_rect(), *m_bitmap, m_bitmap->rect());
  107. } else {
  108. auto location = frame_inner_rect().center().translated(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2));
  109. painter.blit(location, *m_bitmap, m_bitmap->rect());
  110. }
  111. }
  112. }