goat3d
diff goatview/src/goatview.cc @ 95:da100bf13f7f
[goat3d] implemented animation loading
[goatview] working on the animation controls
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 19 May 2014 06:42:40 +0300 |
parents | 21319e71117f |
children | 32dccb16678f b43d33f3ba69 |
line diff
1.1 --- a/goatview/src/goatview.cc Sun May 18 19:58:47 2014 +0300 1.2 +++ b/goatview/src/goatview.cc Mon May 19 06:42:40 2014 +0300 1.3 @@ -1,4 +1,5 @@ 1.4 #include <stdio.h> 1.5 +#include <limits.h> 1.6 #include <map> 1.7 #include "opengl.h" 1.8 #include <QtOpenGL/QtOpenGL> 1.9 @@ -78,6 +79,7 @@ 1.10 goat3d_free(scene); 1.11 } 1.12 if(!(scene = goat3d_create()) || goat3d_load(scene, fname) == -1) { 1.13 + QMessageBox::critical(this, "Error", "Failed to load scene file: " + QString(fname)); 1.14 return false; 1.15 } 1.16 1.17 @@ -97,6 +99,52 @@ 1.18 return true; 1.19 } 1.20 1.21 +bool GoatView::load_anim(const char *fname) 1.22 +{ 1.23 + if(!scene) { 1.24 + QMessageBox::critical(this, "Error", "You must load a scene before loading any animations!"); 1.25 + return false; 1.26 + } 1.27 + 1.28 + if(goat3d_load_anim(scene, fname) == -1) { 1.29 + QMessageBox::critical(this, "Error", QString("Failed to load animation: ") + QString(fname)); 1.30 + return false; 1.31 + } 1.32 + 1.33 + 1.34 + long tstart = LONG_MAX, tend = LONG_MIN; 1.35 + int num_nodes = goat3d_get_node_count(scene); 1.36 + for(int i=0; i<num_nodes; i++) { 1.37 + goat3d_node *node = goat3d_get_node(scene, i); 1.38 + if(goat3d_get_node_parent(node)) { 1.39 + continue; 1.40 + } 1.41 + 1.42 + long t0, t1; 1.43 + if(goat3d_get_anim_timeline(node, &t0, &t1) != -1) { 1.44 + if(t0 < tstart) tstart = t0; 1.45 + if(t1 > tend) tend = t1; 1.46 + } 1.47 + } 1.48 + 1.49 + if(tstart != LONG_MAX) { 1.50 + act_play->setDisabled(false); 1.51 + act_rewind->setDisabled(false); 1.52 + chk_loop->setDisabled(false); 1.53 + 1.54 + slider_time->setDisabled(false); 1.55 + slider_time->setMinimum(tstart); 1.56 + slider_time->setMaximum(tend); 1.57 + 1.58 + spin_time->setDisabled(false); 1.59 + spin_time->setMinimum(tstart); 1.60 + spin_time->setMaximum(tend); 1.61 + } 1.62 + 1.63 + post_redisplay(); 1.64 + return true; 1.65 +} 1.66 + 1.67 bool GoatView::make_menu() 1.68 { 1.69 // file menu 1.70 @@ -177,34 +225,56 @@ 1.71 dock_cont->setLayout(dock_hbox); 1.72 1.73 // animation control box 1.74 - QGridLayout *anim_ctl_box = new QGridLayout; 1.75 - dock_hbox->addLayout(anim_ctl_box); 1.76 + QGroupBox *grp_anim_ctl = new QGroupBox("Animation controls"); 1.77 + // TODO figure out how these fucking stretching policies work... 1.78 + //grp_anim_ctl->sizePolicy().setHorizontalPolicy(QSizePolicy::Maximum); 1.79 + grp_anim_ctl->sizePolicy().setHorizontalStretch(1); 1.80 + dock_hbox->addWidget(grp_anim_ctl); 1.81 1.82 - anim_ctl_box->addWidget(new QLabel("Animation"), 0, 0); 1.83 - cbox_anims = new QComboBox; 1.84 - cbox_anims->setDisabled(true); 1.85 - anim_ctl_box->addWidget(cbox_anims, 0, 1); 1.86 + QVBoxLayout *anim_ctl_box = new QVBoxLayout; 1.87 + grp_anim_ctl->setLayout(anim_ctl_box); 1.88 1.89 chk_loop = new QCheckBox("loop"); 1.90 + chk_loop->setDisabled(true); 1.91 chk_loop->setChecked(false); 1.92 - anim_ctl_box->addWidget(chk_loop, 1, 0); 1.93 + anim_ctl_box->addWidget(chk_loop); 1.94 1.95 QToolBar *toolbar_ctl = new QToolBar; 1.96 - anim_ctl_box->addWidget(toolbar_ctl, 1, 1); 1.97 + anim_ctl_box->addWidget(toolbar_ctl); 1.98 1.99 act_rewind = new QAction(style()->standardIcon(QStyle::SP_MediaSkipBackward), "Rewind", this); 1.100 act_rewind->setDisabled(true); 1.101 + connect(act_rewind, &QAction::triggered, [this](){ slider_time->setValue(slider_time->minimum()); }); 1.102 toolbar_ctl->addAction(act_rewind); 1.103 + 1.104 act_play = new QAction(style()->standardIcon(QStyle::SP_MediaPlay), "Play", this); 1.105 act_play->setDisabled(true); 1.106 toolbar_ctl->addAction(act_play); 1.107 1.108 - // timeline slider 1.109 + // slider and spinbox 1.110 + QWidget *ssgroup = new QWidget; 1.111 + ssgroup->sizePolicy().setHorizontalStretch(4); 1.112 + dock_hbox->addWidget(ssgroup); 1.113 + 1.114 + QGridLayout *ssgrid = new QGridLayout; 1.115 + //dock_hbox->addLayout(ssgrid); 1.116 + ssgroup->setLayout(ssgrid); 1.117 + 1.118 + ssgrid->addWidget(new QLabel("msec"), 0, 0); 1.119 + spin_time = new QSpinBox; 1.120 + spin_time->setDisabled(true); 1.121 + ssgrid->addWidget(spin_time, 0, 1); 1.122 + 1.123 slider_time = new QSlider(Qt::Orientation::Horizontal); 1.124 slider_time->setDisabled(true); 1.125 + ssgrid->addWidget(slider_time, 1, 0, 1, 3); 1.126 + 1.127 connect(slider_time, &QSlider::valueChanged, 1.128 - [&](){ anim_time = slider_time->value(); post_redisplay(); }); 1.129 - dock_hbox->addWidget(slider_time); 1.130 + [&](){ anim_time = slider_time->value(); spin_time->setValue(anim_time); post_redisplay(); }); 1.131 + 1.132 + typedef void (QSpinBox::*ValueChangedIntFunc)(int); 1.133 + connect(spin_time, (ValueChangedIntFunc)&QSpinBox::valueChanged, 1.134 + [&](){ anim_time = spin_time->value(); slider_time->setValue(anim_time); post_redisplay(); }); 1.135 1.136 dock = new QDockWidget(this); 1.137 dock->setAllowedAreas(Qt::BottomDockWidgetArea); 1.138 @@ -235,7 +305,7 @@ 1.139 statusBar()->showMessage("failed to load scene file"); 1.140 return; 1.141 } 1.142 - statusBar()->showMessage("Successfully loaded scene: " + QString(fname)); 1.143 + statusBar()->showMessage("Successfully loaded scene: " + QString(fname.c_str())); 1.144 } 1.145 1.146 void GoatView::open_anim() 1.147 @@ -252,7 +322,7 @@ 1.148 statusBar()->showMessage("failed to load animation file"); 1.149 return; 1.150 } 1.151 - statusBar()->showMessage("Successfully loaded animation: " + QString(fname)); 1.152 + statusBar()->showMessage("Successfully loaded animation: " + QString(fname.c_str())); 1.153 } 1.154 1.155