OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * ***** BEGIN GPL LICENSE BLOCK ***** |
| 3 * |
| 4 * This program is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU General Public License |
| 6 * as published by the Free Software Foundation; either version 2 |
| 7 * of the License, or (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, write to the Free Software Foundation, |
| 16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 * |
| 18 * Contributor(s): Mitchell Stokes |
| 19 * |
| 20 * ***** END GPL LICENSE BLOCK ***** |
| 21 */ |
| 22 |
| 23 /** \file KX_LibLoadStatus.cpp |
| 24 * \ingroup bgeconv |
| 25 */ |
| 26 |
| 27 #include "KX_LibLoadStatus.h" |
| 28 #include "PIL_time.h" |
| 29 |
| 30 KX_LibLoadStatus::KX_LibLoadStatus(class KX_BlenderSceneConverter* kx_converter, |
| 31 class KX_KetsjiEngine* kx_engine, |
| 32 class KX_Scene* merge_scene, |
| 33 const char *path) : |
| 34 m_converter(kx_converter), |
| 35 m_engine(kx_engine), |
| 36 m_mergescene(merge_scene), |
| 37 m_data(NULL), |
| 38 m_libname(path), |
| 39 m_refcount(1), |
| 40 m_progress(0.f), |
| 41 #ifdef WITH_PYTHON |
| 42 m_finish_cb(NULL), |
| 43 m_progress_cb(NULL) |
| 44 #endif |
| 45 { |
| 46 m_endtime = m_starttime = PIL_check_seconds_timer(); |
| 47 } |
| 48 |
| 49 void KX_LibLoadStatus::Finish() |
| 50 { |
| 51 m_progress = 1.f; |
| 52 m_endtime = PIL_check_seconds_timer(); |
| 53 |
| 54 RunFinishCallback(); |
| 55 RunProgressCallback(); |
| 56 } |
| 57 |
| 58 void KX_LibLoadStatus::RunFinishCallback() |
| 59 { |
| 60 #ifdef WITH_PYTHON |
| 61 PyObject* args = Py_BuildValue("(O)", GetProxy()); |
| 62 PyObject* ret = NULL; |
| 63 |
| 64 if (m_finish_cb) { |
| 65 if (!PyObject_Call(m_finish_cb, args, NULL)) { |
| 66 PyErr_Print(); |
| 67 PyErr_Clear(); |
| 68 } |
| 69 } |
| 70 |
| 71 Py_DECREF(args); |
| 72 #endif |
| 73 } |
| 74 |
| 75 void KX_LibLoadStatus::RunProgressCallback() |
| 76 { |
| 77 #ifdef WITH_PYTHON |
| 78 PyObject* args = Py_BuildValue("(O)", GetProxy()); |
| 79 PyObject* ret = NULL; |
| 80 |
| 81 if (m_progress_cb) { |
| 82 if (!PyObject_Call(m_progress_cb, args, NULL)) { |
| 83 PyErr_Print(); |
| 84 PyErr_Clear(); |
| 85 } |
| 86 } |
| 87 |
| 88 Py_DECREF(args); |
| 89 #endif |
| 90 } |
| 91 |
| 92 class KX_BlenderSceneConverter *KX_LibLoadStatus::GetConverter() |
| 93 { |
| 94 return m_converter; |
| 95 } |
| 96 |
| 97 class KX_KetsjiEngine *KX_LibLoadStatus::GetEngine() |
| 98 { |
| 99 return m_engine; |
| 100 } |
| 101 |
| 102 class KX_Scene *KX_LibLoadStatus::GetMergeScene() |
| 103 { |
| 104 return m_mergescene; |
| 105 } |
| 106 |
| 107 void KX_LibLoadStatus::SetLibName(const char *name) |
| 108 { |
| 109 m_libname = name; |
| 110 } |
| 111 |
| 112 const char *KX_LibLoadStatus::GetLibName() |
| 113 { |
| 114 return m_libname; |
| 115 } |
| 116 |
| 117 void KX_LibLoadStatus::SetData(void *data) |
| 118 { |
| 119 m_data = data; |
| 120 } |
| 121 |
| 122 void *KX_LibLoadStatus::GetData() |
| 123 { |
| 124 return m_data; |
| 125 } |
| 126 |
| 127 void KX_LibLoadStatus::SetProgress(float progress) |
| 128 { |
| 129 m_progress = progress; |
| 130 RunProgressCallback(); |
| 131 } |
| 132 |
| 133 float KX_LibLoadStatus::GetProgress() |
| 134 { |
| 135 return m_progress; |
| 136 } |
| 137 |
| 138 void KX_LibLoadStatus::AddProgress(float progress) |
| 139 { |
| 140 m_progress += progress; |
| 141 RunProgressCallback(); |
| 142 } |
| 143 |
| 144 #ifdef WITH_PYTHON |
| 145 |
| 146 PyMethodDef KX_LibLoadStatus::Methods[] =· |
| 147 { |
| 148 {NULL} //Sentinel |
| 149 }; |
| 150 |
| 151 PyAttributeDef KX_LibLoadStatus::Attributes[] = { |
| 152 KX_PYATTRIBUTE_RW_FUNCTION("onFinish", KX_LibLoadStatus, pyattr_get_onfi
nish, pyattr_set_onfinish), |
| 153 KX_PYATTRIBUTE_RW_FUNCTION("onProgress", KX_LibLoadStatus, pyattr_get_on
progress, pyattr_set_onprogress), |
| 154 KX_PYATTRIBUTE_FLOAT_RO("progress", KX_LibLoadStatus, m_progress), |
| 155 KX_PYATTRIBUTE_STRING_RO("libraryName", KX_LibLoadStatus, m_libname), |
| 156 KX_PYATTRIBUTE_RO_FUNCTION("timeTaken", KX_LibLoadStatus, pyattr_get_tim
etaken), |
| 157 { NULL } //Sentinel |
| 158 }; |
| 159 |
| 160 PyTypeObject KX_LibLoadStatus::Type = { |
| 161 PyVarObject_HEAD_INIT(NULL, 0) |
| 162 "KX_LibLoadStatus", |
| 163 sizeof(PyObjectPlus_Proxy), |
| 164 0, |
| 165 py_base_dealloc, |
| 166 0, |
| 167 0, |
| 168 0, |
| 169 0, |
| 170 py_base_repr, |
| 171 0,0,0,0,0,0,0,0,0, |
| 172 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 173 0,0,0,0,0,0,0, |
| 174 Methods, |
| 175 0, |
| 176 0, |
| 177 &PyObjectPlus::Type, |
| 178 0,0,0,0,0,0, |
| 179 py_base_new |
| 180 }; |
| 181 |
| 182 |
| 183 PyObject* KX_LibLoadStatus::pyattr_get_onfinish(void *self_v, const KX_PYATTRIBU
TE_DEF *attrdef) |
| 184 { |
| 185 KX_LibLoadStatus* self = static_cast<KX_LibLoadStatus*>(self_v); |
| 186 ········ |
| 187 if (self->m_finish_cb) { |
| 188 Py_INCREF(self->m_finish_cb); |
| 189 return self->m_finish_cb; |
| 190 } |
| 191 |
| 192 Py_RETURN_NONE; |
| 193 } |
| 194 |
| 195 int KX_LibLoadStatus::pyattr_set_onfinish(void *self_v, const KX_PYATTRIBUTE_DEF
*attrdef, PyObject *value) |
| 196 { |
| 197 KX_LibLoadStatus* self = static_cast<KX_LibLoadStatus*>(self_v); |
| 198 |
| 199 if (!PyCallable_Check(value)) { |
| 200 PyErr_SetString(PyExc_TypeError, "KX_LibLoadStatus.onFinished re
quires a callable object"); |
| 201 return PY_SET_ATTR_FAIL; |
| 202 } |
| 203 |
| 204 if (self->m_finish_cb) |
| 205 Py_DECREF(self->m_finish_cb); |
| 206 |
| 207 Py_INCREF(value); |
| 208 self->m_finish_cb = value; |
| 209 |
| 210 return PY_SET_ATTR_SUCCESS; |
| 211 } |
| 212 |
| 213 PyObject* KX_LibLoadStatus::pyattr_get_onprogress(void *self_v, const KX_PYATTRI
BUTE_DEF *attrdef) |
| 214 { |
| 215 KX_LibLoadStatus* self = static_cast<KX_LibLoadStatus*>(self_v); |
| 216 ········ |
| 217 if (self->m_progress_cb) { |
| 218 Py_INCREF(self->m_progress_cb); |
| 219 return self->m_progress_cb; |
| 220 } |
| 221 |
| 222 Py_RETURN_NONE; |
| 223 } |
| 224 |
| 225 int KX_LibLoadStatus::pyattr_set_onprogress(void *self_v, const KX_PYATTRIBUTE_D
EF *attrdef, PyObject *value) |
| 226 { |
| 227 KX_LibLoadStatus* self = static_cast<KX_LibLoadStatus*>(self_v); |
| 228 |
| 229 if (!PyCallable_Check(value)) { |
| 230 PyErr_SetString(PyExc_TypeError, "KX_LibLoadStatus.onProgress re
quires a callable object"); |
| 231 return PY_SET_ATTR_FAIL; |
| 232 } |
| 233 |
| 234 if (self->m_progress_cb) |
| 235 Py_DECREF(self->m_progress_cb); |
| 236 |
| 237 Py_INCREF(value); |
| 238 self->m_progress_cb = value; |
| 239 |
| 240 return PY_SET_ATTR_SUCCESS; |
| 241 } |
| 242 |
| 243 PyObject* KX_LibLoadStatus::pyattr_get_timetaken(void *self_v, const KX_PYATTRIB
UTE_DEF *attrdef) |
| 244 { |
| 245 KX_LibLoadStatus* self = static_cast<KX_LibLoadStatus*>(self_v); |
| 246 |
| 247 return PyFloat_FromDouble(self->m_endtime - self->m_starttime); |
| 248 } |
| 249 #endif // WITH_PYTHON |
OLD | NEW |