Ejemplo n.º 1
0
PyObject *pyrna_callback_add(BPy_StructRNA *self, PyObject *args)
{
	void *handle;

	PyObject *cb_func, *cb_args;
	char *cb_event_str = NULL;
	int cb_event;

	if (!PyArg_ParseTuple(args, "OO!|s:bpy_struct.callback_add", &cb_func, &PyTuple_Type, &cb_args, &cb_event_str))
		return NULL;
	
	if (!PyCallable_Check(cb_func)) {
		PyErr_SetString(PyExc_TypeError, "callback_add(): first argument isn't callable");
		return NULL;
	}

	if (RNA_struct_is_a(self->ptr.type, &RNA_Region)) {
		if (cb_event_str) {
			static EnumPropertyItem region_draw_mode_items[] = {
				{REGION_DRAW_POST_PIXEL, "POST_PIXEL", 0, "Post Pixel", ""},
				{REGION_DRAW_POST_VIEW, "POST_VIEW", 0, "Post View", ""},
				{REGION_DRAW_PRE_VIEW, "PRE_VIEW", 0, "Pre View", ""},
				{0, NULL, 0, NULL, NULL}};
	
			if (pyrna_enum_value_from_id(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") < 0) {
				return NULL;
			}
		}
		else {
			cb_event = REGION_DRAW_POST_PIXEL;
		}

		handle = ED_region_draw_cb_activate(((ARegion *)self->ptr.data)->type, cb_region_draw, (void *)args, cb_event);
		Py_INCREF(args);
	}
	else {
		PyErr_SetString(PyExc_TypeError, "callback_add(): type does not suppport callbacks");
		return NULL;
	}

	return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL);
}
Ejemplo n.º 2
0
PyObject *pyrna_callback_classmethod_remove(PyObject *UNUSED(self), PyObject *args)
{
	PyObject *cls;
	PyObject *py_handle;
	void *handle;
	void *customdata;
	StructRNA *srna;
	char *cb_regiontype_str;
	int cb_regiontype;

	if (PyTuple_GET_SIZE(args) < 2) {
		PyErr_SetString(PyExc_ValueError, "callback_remove(handle): expected at least 2 args");
		return NULL;
	}

	cls = PyTuple_GET_ITEM(args, 0);
	if (!(srna = pyrna_struct_as_srna(cls, false, "callback_remove"))) {
		return NULL;
	}
	py_handle = PyTuple_GET_ITEM(args, 1);
	handle = PyCapsule_GetPointer(py_handle, RNA_CAPSULE_ID);
	if (handle == NULL) {
		PyErr_SetString(PyExc_ValueError, "callback_remove(handle): NULL handle given, invalid or already removed");
		return NULL;
	}

	if (RNA_struct_is_a(srna, &RNA_Space)) {
		if (!PyArg_ParseTuple(args, "OO!s:Space.draw_handler_remove",
		                      &cls, &PyCapsule_Type, &py_handle,  /* already assigned, no matter */
		                      &cb_regiontype_str))
		{
			return NULL;
		}

		customdata = ED_region_draw_cb_customdata(handle);
		Py_DECREF((PyObject *)customdata);

		if (pyrna_enum_value_from_id(region_type_items, cb_regiontype_str, &cb_regiontype, "bpy_struct.callback_remove()") == -1) {
			return NULL;
		}
		else {
			const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
			if (spaceid == -1) {
				PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
				return NULL;
			}
			else {
				SpaceType *st = BKE_spacetype_from_id(spaceid);
				ARegionType *art = BKE_regiontype_from_id(st, cb_regiontype);

				ED_region_draw_cb_exit(art, handle);
			}
		}
	}
	else {
		PyErr_SetString(PyExc_TypeError, "callback_remove(): type does not support callbacks");
		return NULL;
	}

	/* don't allow reuse */
	PyCapsule_SetName(py_handle, RNA_CAPSULE_ID_INVALID);

	Py_RETURN_NONE;
}
Ejemplo n.º 3
0
PyObject *pyrna_callback_classmethod_add(PyObject *UNUSED(self), PyObject *args)
{
	void *handle;
	PyObject *cls;
	PyObject *cb_func, *cb_args;
	char *cb_regiontype_str;
	char *cb_event_str;
	int cb_event;
	int cb_regiontype;
	StructRNA *srna;

	if (PyTuple_GET_SIZE(args) < 2) {
		PyErr_SetString(PyExc_ValueError, "handler_add(handle): expected at least 2 args");
		return NULL;
	}

	cls = PyTuple_GET_ITEM(args, 0);
	if (!(srna = pyrna_struct_as_srna(cls, false, "handler_add"))) {
		return NULL;
	}
	cb_func = PyTuple_GET_ITEM(args, 1);
	if (!PyCallable_Check(cb_func)) {
		PyErr_SetString(PyExc_TypeError, "first argument isn't callable");
		return NULL;
	}

	/* class specific callbacks */
	if (RNA_struct_is_a(srna, &RNA_Space)) {
		if (!PyArg_ParseTuple(args, "OOO!ss:Space.draw_handler_add",
		                      &cls, &cb_func,  /* already assigned, no matter */
		                      &PyTuple_Type, &cb_args, &cb_regiontype_str, &cb_event_str))
		{
			return NULL;
		}

		if (pyrna_enum_value_from_id(region_draw_mode_items, cb_event_str, &cb_event, "bpy_struct.callback_add()") == -1) {
			return NULL;
		}
		else if (pyrna_enum_value_from_id(region_type_items, cb_regiontype_str, &cb_regiontype, "bpy_struct.callback_add()") == -1) {
			return NULL;
		}
		else {
			const eSpace_Type spaceid = rna_Space_refine_reverse(srna);
			if (spaceid == -1) {
				PyErr_Format(PyExc_TypeError, "unknown space type '%.200s'", RNA_struct_identifier(srna));
				return NULL;
			}
			else {
				SpaceType *st = BKE_spacetype_from_id(spaceid);
				ARegionType *art = BKE_regiontype_from_id(st, cb_regiontype);

				handle = ED_region_draw_cb_activate(art, cb_region_draw, (void *)args, cb_event);
				Py_INCREF(args);
			}
		}
	}
	else {
		PyErr_SetString(PyExc_TypeError, "callback_add(): type does not support callbacks");
		return NULL;
	}

	return PyCapsule_New((void *)handle, RNA_CAPSULE_ID, NULL);
}