ImageWidget.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // Same as QSWidget::animate(), you probably want to keep any changes in sync
  62. void ImageWidget::animate()
  63. {
  64. m_current_frame_index = (m_current_frame_index + 1) % m_image_decoder->frame_count();
  65. const auto& current_frame = m_image_decoder->frame(m_current_frame_index);
  66. set_bitmap(current_frame.image);
  67. if (current_frame.duration != m_timer->interval()) {
  68. m_timer->restart(current_frame.duration);
  69. }
  70. if (m_current_frame_index == m_image_decoder->frame_count() - 1) {
  71. ++m_loops_completed;
  72. if (m_loops_completed > 0 && m_loops_completed == m_image_decoder->loop_count()) {
  73. m_timer->stop();
  74. }
  75. }
  76. }
  77. void ImageWidget::load_from_file(const StringView& path)
  78. {
  79. auto file_or_error = MappedFile::map(path);
  80. if (file_or_error.is_error())
  81. return;
  82. auto& mapped_file = *file_or_error.value();
  83. m_image_decoder = Gfx::ImageDecoder::create((const u8*)mapped_file.data(), mapped_file.size());
  84. auto bitmap = m_image_decoder->bitmap();
  85. VERIFY(bitmap);
  86. set_bitmap(bitmap);
  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. void ImageWidget::mousedown_event(GUI::MouseEvent&)
  95. {
  96. if (on_click)
  97. on_click();
  98. }
  99. void ImageWidget::paint_event(PaintEvent& event)
  100. {
  101. Frame::paint_event(event);
  102. if (!m_bitmap) {
  103. return;
  104. }
  105. Painter painter(*this);
  106. painter.add_clip_rect(event.rect());
  107. if (m_should_stretch) {
  108. painter.draw_scaled_bitmap(frame_inner_rect(), *m_bitmap, m_bitmap->rect(), (float)opacity_percent() / 100.0f);
  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(), (float)opacity_percent() / 100.0f);
  112. }
  113. }
  114. void ImageWidget::set_opacity_percent(int percent)
  115. {
  116. if (m_opacity_percent == percent)
  117. return;
  118. m_opacity_percent = percent;
  119. update();
  120. }
  121. }