ImageWidget.cpp 4.2 KB

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