From c9c51776ee08827f7d6c7b8b4d671a4660c32b64 Mon Sep 17 00:00:00 2001 From: Mitchell Stokes Date: Sat, 16 Jul 2011 05:25:15 +0000 Subject: BGE Animations: Some updates to the Python api: * Adding methods KX_GameObject.stopAction() and KX_GameObject.isPlayingAction(). * Made all layer arguments optional. This means I had to change setActionFrame(layer, frame) to setActionFrame(frame, layer=0). This seems a little backwards to me, but I guess that's what you get with optional arguments. Also, this will break existing scripts. * Made sure to check user supplied layer values on all action methods. Previously this was only done for playAction(). * Fixed a few newline issues. --- source/gameengine/Ketsji/KX_GameObject.cpp | 70 ++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 14 deletions(-) (limited to 'source/gameengine/Ketsji/KX_GameObject.cpp') diff --git a/source/gameengine/Ketsji/KX_GameObject.cpp b/source/gameengine/Ketsji/KX_GameObject.cpp index cc275bad200..4d841e78d70 100644 --- a/source/gameengine/Ketsji/KX_GameObject.cpp +++ b/source/gameengine/Ketsji/KX_GameObject.cpp @@ -1561,8 +1561,10 @@ PyMethodDef KX_GameObject::Methods[] = { KX_PYMETHODTABLE(KX_GameObject, sendMessage), KX_PYMETHODTABLE_KEYWORDS(KX_GameObject, playAction), + KX_PYMETHODTABLE(KX_GameObject, stopAction), KX_PYMETHODTABLE(KX_GameObject, getActionFrame), KX_PYMETHODTABLE(KX_GameObject, setActionFrame), + KX_PYMETHODTABLE(KX_GameObject, isPlayingAction), // dict style access for props {"get",(PyCFunction) KX_GameObject::sPyget, METH_VARARGS}, @@ -3041,9 +3043,18 @@ KX_PYMETHODDEF_DOC_VARARGS(KX_GameObject, sendMessage, Py_RETURN_NONE; } +static void layer_check(short &layer, const char *method_name) +{ + if (layer < 0 || layer > MAX_ACTION_LAYERS) + { + printf("KX_GameObject.%s(): given layer (%d) is out of range (0 - %d), setting to 0.\n", method_name, layer, MAX_ACTION_LAYERS-1); + layer = 0; + } +} + KX_PYMETHODDEF_DOC(KX_GameObject, playAction, "playAction(name, start_frame, end_frame, layer=0, priority=0 blendin=0, play_mode=ACT_MODE_PLAY, layer_weight=0.0, ipo_flags=0, speed=1.0)\n" - "plays an action\n") + "Plays an action\n") { const char* name; float start, end, blendin=0.f, speed=1.f, layer_weight=0.f; @@ -3057,11 +3068,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, playAction, &name, &start, &end, &layer, &priority, &blendin, &play_mode, &layer_weight, &ipo_flags, &speed)) return NULL; - if (layer < 0 || layer > MAX_ACTION_LAYERS) - { - printf("KX_GameObject.playAction(): given layer (%d) is out of range (0 - %d), setting to 0", layer, MAX_ACTION_LAYERS-1); - layer = 0; - } + layer_check(layer, "playAction"); if (play_mode < 0 || play_mode > BL_Action::ACT_MODE_MAX) { @@ -3080,33 +3087,68 @@ KX_PYMETHODDEF_DOC(KX_GameObject, playAction, Py_RETURN_NONE; } +KX_PYMETHODDEF_DOC(KX_GameObject, stopAction, + "stopAction(layer=0)\n" + "Stop playing the action on the given layer\n") +{ + short layer=0; + + if (!PyArg_ParseTuple(args, "|h:stopAction", &layer)) + return NULL; + + layer_check(layer, "stopAction"); + + StopAction(layer); + + Py_RETURN_NONE; +} + KX_PYMETHODDEF_DOC(KX_GameObject, getActionFrame, - "getActionFrame(layer)\n" - "Gets the current frame of the action playing in the supplied layer") + "getActionFrame(layer=0)\n" + "Gets the current frame of the action playing in the supplied layer\n") { - short layer; + short layer=0; - if (!PyArg_ParseTuple(args, "h:getActionFrame", &layer)) + if (!PyArg_ParseTuple(args, "|h:getActionFrame", &layer)) return NULL; + layer_check(layer, "getActionFrame"); + return PyLong_FromLong(GetActionFrame(layer)); } KX_PYMETHODDEF_DOC(KX_GameObject, setActionFrame, - "setActionFrame(layer, frame)\n" - "Set the current frame of the action playing in the supplied layer") + "setActionFrame(frame, layer=0)\n" + "Set the current frame of the action playing in the supplied layer\n") { - short layer; + short layer=0; float frame; - if (!PyArg_ParseTuple(args, "hf:setActionFrame", &layer, &frame)) + if (!PyArg_ParseTuple(args, "f|h:setActionFrame", &frame, &layer)) return NULL; + layer_check(layer, "setActionFrame"); + SetActionFrame(layer, frame); Py_RETURN_NONE; } +KX_PYMETHODDEF_DOC(KX_GameObject, isPlayingAction, + "isPlayingAction(layer=0)\n" + "Checks to see if there is an action playing in the given layer\n") +{ + short layer=0; + + if (!PyArg_ParseTuple(args, "|h:isPlayingAction", &layer)) + return NULL; + + layer_check(layer, "isPlayingAction"); + + return PyBool_FromLong(!IsActionDone(layer)); +} + + /* dict style access */ -- cgit v1.2.3