playback_dock.cpp 3.0 KB

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