playback.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* docks/playback.cpp - playback controls dock
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "playback.h"
  18. #include "ui_playback.h"
  19. namespace rainynite::studio {
  20. PlaybackDock::PlaybackDock(shared_ptr<EditorContext> context_, QWidget* parent) :
  21. DockWidget(parent),
  22. ContextListener(context_),
  23. ui(make_unique<Ui::PlaybackDock>()),
  24. timer(new QTimer(this))
  25. {
  26. ui->setupUi(this);
  27. connect(ui->move_start_button, SIGNAL(clicked()), this, SLOT(move_start()));
  28. connect(ui->move_end_button, SIGNAL(clicked()), this, SLOT(move_end()));
  29. connect(ui->play_button, SIGNAL(toggled(bool)), this, SLOT(toggle_playback(bool)));
  30. connect(ui->stop_button, SIGNAL(clicked()), this, SLOT(stop()));
  31. connect(ui->loop_button, &QAbstractButton::toggled, [this](bool loop) {
  32. is_looping = loop;
  33. });
  34. connect(ui->time_box, SIGNAL(editingFinished()), this, SLOT(change_time()));
  35. connect(ui->fps_box, SIGNAL(editingFinished()), this, SLOT(change_fps()));
  36. // TODO: move actual playing out of dock
  37. connect(timer, SIGNAL(timeout()), this, SLOT(next_frame()));
  38. set_context(get_context());
  39. }
  40. PlaybackDock::~PlaybackDock() {
  41. }
  42. void PlaybackDock::time_changed(core::Time time) {
  43. ui->time_box->setValue(time.get_frames());
  44. }
  45. void PlaybackDock::fps_changed(core::Time::fps_type fps) {
  46. ui->fps_box->setValue(fps);
  47. }
  48. void PlaybackDock::change_time() {
  49. if (auto context = get_core_context())
  50. context->set_time(core::Time(0, context->get_fps(), ui->time_box->value()));
  51. }
  52. void PlaybackDock::change_fps() {
  53. if (auto context = get_core_context())
  54. context->set_fps(ui->fps_box->value());
  55. }
  56. void PlaybackDock::toggle_playback(bool play) {
  57. if (auto context = get_core_context()) {
  58. if (play)
  59. timer->start(1000/context->get_fps());
  60. else
  61. timer->stop();
  62. get_context()->playback_change(play);
  63. }
  64. }
  65. void PlaybackDock::stop() {
  66. ui->play_button->setChecked(false);
  67. move_start();
  68. }
  69. void PlaybackDock::next_frame() {
  70. if (auto context = get_core_context()) {
  71. auto time = context->get_time();
  72. time.set_fps(context->get_fps());
  73. ++time;
  74. if (time == context->get_period().get_last()) {
  75. if (!is_looping) {
  76. ui->play_button->setChecked(false);
  77. return;
  78. }
  79. time = context->get_period().get_first();
  80. }
  81. context->set_time(time);
  82. }
  83. }
  84. void PlaybackDock::move_start() {
  85. if (auto context = get_core_context()) {
  86. context->to_start();
  87. }
  88. }
  89. void PlaybackDock::move_end() {
  90. if (auto context = get_core_context()) {
  91. context->to_end();
  92. }
  93. }
  94. }