Skip to content

Commit

Permalink
various HUD improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto De Ioris committed Jul 8, 2017
1 parent c6938bd commit 03c2f96
Show file tree
Hide file tree
Showing 14 changed files with 281 additions and 24 deletions.
24 changes: 21 additions & 3 deletions Source/UnrealEnginePython/Private/PyActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ APyActor::APyActor()

}

void APyActor::PostInitializeComponents()
void APyActor::PreInitializeComponents()
{
Super::PostInitializeComponents();

Super::PreInitializeComponents();

if (PythonModule.IsEmpty())
return;
Expand Down Expand Up @@ -73,6 +72,25 @@ void APyActor::PostInitializeComponents()

ue_bind_events_for_py_class_by_attribute(this, py_actor_instance);

if (!PyObject_HasAttrString(py_actor_instance, (char *)"pre_initialize_components"))
return;

PyObject *pic_ret = PyObject_CallMethod(py_actor_instance, (char *)"pre_initialize_components", NULL);
if (!pic_ret) {
unreal_engine_py_log_error();
return;
}
Py_DECREF(pic_ret);
}


void APyActor::PostInitializeComponents()
{
Super::PostInitializeComponents();

if (!py_actor_instance)
return;

if (!PyObject_HasAttrString(py_actor_instance, (char *)"post_initialize_components"))
return;

Expand Down
39 changes: 27 additions & 12 deletions Source/UnrealEnginePython/Private/UEPyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ static PyMethodDef unreal_engine_methods[] = {
{ "compress_image_array", py_unreal_engine_compress_image_array, METH_VARARGS, "" },
{ "create_checkerboard_texture", py_unreal_engine_create_checkerboard_texture, METH_VARARGS, "" },
{ "create_transient_texture", py_unreal_engine_create_transient_texture, METH_VARARGS, "" },
{ "create_transient_texture_render_target2d", py_unreal_engine_create_transient_texture_render_target2d, METH_VARARGS, "" },

{ "create_world", py_unreal_engine_create_world, METH_VARARGS, "" },

Expand Down Expand Up @@ -290,7 +291,7 @@ static PyMethodDef unreal_engine_methods[] = {

{ "get_game_viewport_client", py_unreal_engine_get_game_viewport_client, METH_VARARGS, "" },
#pragma warning(suppress: 4191)
{ "open_color_picker", (PyCFunction)py_unreal_engine_open_color_picker, METH_VARARGS| METH_KEYWORDS, "" },
{ "open_color_picker", (PyCFunction)py_unreal_engine_open_color_picker, METH_VARARGS | METH_KEYWORDS, "" },
{ "destroy_color_picker", py_unreal_engine_destroy_color_picker, METH_VARARGS, "" },
{ "play_sound", py_unreal_engine_play_sound, METH_VARARGS, "" },
#if WITH_EDITOR
Expand Down Expand Up @@ -406,7 +407,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
{ "set_actor_label", (PyCFunction)py_ue_set_actor_label, METH_VARARGS, "" },

{ "get_editor_world_counterpart_actor", (PyCFunction)py_ue_get_editor_world_counterpart_actor, METH_VARARGS, "" },

{ "find_actor_by_label", (PyCFunction)py_ue_find_actor_by_label, METH_VARARGS, "" },
{ "save_package", (PyCFunction)py_ue_save_package, METH_VARARGS, "" },
{ "asset_can_reimport", (PyCFunction)py_ue_asset_can_reimport, METH_VARARGS, "" },
Expand Down Expand Up @@ -437,6 +438,7 @@ static PyMethodDef ue_PyUObject_methods[] = {

{ "is_rooted", (PyCFunction)py_ue_is_rooted, METH_VARARGS, "" },
{ "add_to_root", (PyCFunction)py_ue_add_to_root, METH_VARARGS, "" },
{ "auto_root", (PyCFunction)py_ue_auto_root, METH_VARARGS, "" },
{ "remove_from_root", (PyCFunction)py_ue_remove_from_root, METH_VARARGS, "" },

{ "find_function", (PyCFunction)py_ue_find_function, METH_VARARGS, "" },
Expand Down Expand Up @@ -498,6 +500,8 @@ static PyMethodDef ue_PyUObject_methods[] = {
{ "hud_draw_2d_line", (PyCFunction)py_ue_hud_draw_2d_line, METH_VARARGS, "" },
{ "hud_draw_line", (PyCFunction)py_ue_hud_draw_line, METH_VARARGS, "" },
{ "hud_draw_texture", (PyCFunction)py_ue_hud_draw_texture, METH_VARARGS, "" },
{ "hud_draw_rect", (PyCFunction)py_ue_hud_draw_rect, METH_VARARGS, "" },
{ "hud_draw_text", (PyCFunction)py_ue_hud_draw_text, METH_VARARGS, "" },


// Movements
Expand Down Expand Up @@ -527,6 +531,8 @@ static PyMethodDef ue_PyUObject_methods[] = {

{ "component_is_registered", (PyCFunction)py_ue_component_is_registered, METH_VARARGS, "" },
{ "register_component", (PyCFunction)py_ue_register_component, METH_VARARGS, "" },
{ "unregister_component", (PyCFunction)py_ue_unregister_component, METH_VARARGS, "" },
{ "destroy_component", (PyCFunction)py_ue_destroy_component, METH_VARARGS, "" },

{ "actor_destroy_component", (PyCFunction)py_ue_actor_destroy_component, METH_VARARGS, "" },
{ "destroy_actor_component", (PyCFunction)py_ue_actor_destroy_component, METH_VARARGS, "" },
Expand Down Expand Up @@ -554,7 +560,8 @@ static PyMethodDef ue_PyUObject_methods[] = {
{ "actor_has_component_of_type", (PyCFunction)py_ue_actor_has_component_of_type, METH_VARARGS, "" },

{ "actor_destroy", (PyCFunction)py_ue_actor_destroy, METH_VARARGS, "" },
{ "actor_spawn", (PyCFunction)py_ue_actor_spawn, METH_VARARGS, "" },
#pragma warning(suppress: 4191)
{ "actor_spawn", (PyCFunction)py_ue_actor_spawn, METH_VARARGS | METH_KEYWORDS, "" },
{ "actor_has_tag", (PyCFunction)py_ue_actor_has_tag, METH_VARARGS, "" },
{ "get_actor_bounds", (PyCFunction)py_ue_get_actor_bounds, METH_VARARGS, "" },

Expand All @@ -579,6 +586,8 @@ static PyMethodDef ue_PyUObject_methods[] = {
{ "get_actor_components_by_type", (PyCFunction)py_ue_get_actor_components_by_type, METH_VARARGS, "" },
{ "get_components_by_type", (PyCFunction)py_ue_get_actor_components_by_type, METH_VARARGS, "" },

{ "add_python_component", (PyCFunction)py_ue_add_python_component, METH_VARARGS, "" },

{ "set_simulate_physics", (PyCFunction)py_ue_set_simulate_physics, METH_VARARGS, "" },
{ "add_impulse", (PyCFunction)py_ue_add_impulse, METH_VARARGS, "" },
{ "add_angular_impulse", (PyCFunction)py_ue_add_angular_impulse, METH_VARARGS, "" },
Expand All @@ -588,7 +597,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
{ "get_physics_linear_velocity", (PyCFunction)py_ue_get_physics_linear_velocity, METH_VARARGS, "" },
{ "set_physics_angular_velocity", (PyCFunction)py_ue_set_physics_angular_velocity, METH_VARARGS, "" },
{ "get_physics_angular_velocity", (PyCFunction)py_ue_get_physics_angular_velocity, METH_VARARGS, "" },
{ "find_object", (PyCFunction)py_ue_find_object, METH_VARARGS, "" },
{ "find_object", (PyCFunction)py_ue_find_object, METH_VARARGS, "" },
{ "get_world", (PyCFunction)py_ue_get_world, METH_VARARGS, "" },
{ "has_world", (PyCFunction)py_ue_has_world, METH_VARARGS, "" },

Expand All @@ -597,8 +606,8 @@ static PyMethodDef ue_PyUObject_methods[] = {
{ "get_world_location_at_distance_along_spline", (PyCFunction)py_ue_get_world_location_at_distance_along_spline, METH_VARARGS, "" },
{ "get_spline_length", (PyCFunction)py_ue_get_spline_length, METH_VARARGS, "" },

// Widget
{ "take_widget", (PyCFunction)py_ue_take_widget, METH_VARARGS, "" },
// Widget
{ "take_widget", (PyCFunction)py_ue_take_widget, METH_VARARGS, "" },

// WidgetComponent
{ "set_slate_widget", (PyCFunction)py_ue_set_slate_widget, METH_VARARGS, "" },
Expand All @@ -622,6 +631,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
{ "get_num_spectators", (PyCFunction)py_ue_get_num_spectators, METH_VARARGS, "" },
{ "get_player_controller", (PyCFunction)py_ue_get_player_controller, METH_VARARGS, "" },
{ "get_player_hud", (PyCFunction)py_ue_get_player_hud, METH_VARARGS, "" },
{ "set_player_hud", (PyCFunction)py_ue_set_player_hud, METH_VARARGS, "" },
{ "get_player_camera_manager", (PyCFunction)py_ue_get_player_camera_manager, METH_VARARGS, "" },
{ "get_player_pawn", (PyCFunction)py_ue_get_player_pawn, METH_VARARGS, "" },

Expand Down Expand Up @@ -671,6 +681,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
{ "texture_get_width", (PyCFunction)py_ue_texture_get_width, METH_VARARGS, "" },
{ "texture_get_height", (PyCFunction)py_ue_texture_get_height, METH_VARARGS, "" },
{ "render_target_get_data", (PyCFunction)py_ue_render_target_get_data, METH_VARARGS, "" },
{ "texture_update_resource", (PyCFunction)py_ue_texture_update_resource, METH_VARARGS, "" },

// Sequencer
{ "sequencer_master_tracks", (PyCFunction)py_ue_sequencer_master_tracks, METH_VARARGS, "" },
Expand Down Expand Up @@ -749,12 +760,16 @@ void ue_pydelegates_cleanup(ue_PyUObject *self) {
UE_LOG(LogPython, Warning, TEXT("Removing UPythonDelegate %p from ue_PyUObject %p mapped to UObject %p"), py_delegate, self, self->ue_object);
#endif
py_delegate->RemoveFromRoot();
}
}
}
self->python_delegates_gc->clear();
delete self->python_delegates_gc;
self->python_delegates_gc = nullptr;

if (self->auto_rooted && self->ue_object->IsRooted()) {
self->ue_object->RemoveFromRoot();
}

Py_XDECREF(self->py_dict);
}

Expand Down Expand Up @@ -826,8 +841,8 @@ static PyObject *ue_PyUObject_getattro(ue_PyUObject *self, PyObject *attr_name)
return PyLong_FromLong(u_enum->FindEnumIndex(item.Key));
#endif
}
}
}
}
#endif
if (self->ue_object->IsA<UEnum>()) {
UEnum *u_enum = (UEnum *)self->ue_object;
Expand All @@ -837,15 +852,15 @@ static PyObject *ue_PyUObject_getattro(ue_PyUObject *self, PyObject *attr_name)
#else
return PyLong_FromLong(u_enum->FindEnumIndex(FName(UTF8_TO_TCHAR(attr))));
#endif
}
}
}
}

if (function) {
// swallow previous exception
PyErr_Clear();
return py_ue_new_callable(function, self->ue_object);
}
}
}
}
return ret;
}
Expand Down Expand Up @@ -1460,7 +1475,7 @@ void unreal_engine_py_log_error() {
PyObject *zero = PyUnicode_AsUTF8String(PyObject_Str(value));
if (zero) {
msg = PyBytes_AsString(zero);
}
}
#else
msg = PyString_AsString(PyObject_Str(value));
#endif
Expand Down
6 changes: 4 additions & 2 deletions Source/UnrealEnginePython/Private/UEPyModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

typedef struct {
PyObject_HEAD
/* Type-specific fields go here. */
UObject *ue_object;
/* Type-specific fields go here. */
UObject *ue_object;
// reference to proxy class (can be null)
PyObject *py_proxy;
// list of exposed delegates
std::list<UPythonDelegate*> *python_delegates_gc;
// the __dict__
PyObject *py_dict;
// if true RemoveFromRoot will be called at object destruction time
int auto_rooted;
} ue_PyUObject;


Expand Down
95 changes: 92 additions & 3 deletions Source/UnrealEnginePython/Private/UObject/UEPyActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "Runtime/LevelSequence/Public/LevelSequenceActor.h"
#include "Runtime/LevelSequence/Public/LevelSequence.h"
#include "PythonComponent.h"
#include "UEPyObject.h"

PyObject *py_ue_actor_has_tag(ue_PyUObject * self, PyObject * args) {

Expand Down Expand Up @@ -309,6 +311,30 @@ PyObject *py_ue_setup_attachment(ue_PyUObject *self, PyObject * args) {
Py_RETURN_NONE;
}

PyObject *py_ue_unregister_component(ue_PyUObject * self, PyObject * args) {
ue_py_check(self);

UActorComponent *component = ue_py_check_type<UActorComponent>(self);
if (!component)
return PyErr_Format(PyExc_Exception, "uobject is not an UActorComponent");

component->UnregisterComponent();

Py_RETURN_NONE;
}

PyObject *py_ue_destroy_component(ue_PyUObject * self, PyObject * args) {
ue_py_check(self);

UActorComponent *component = ue_py_check_type<UActorComponent>(self);
if (!component)
return PyErr_Format(PyExc_Exception, "uobject is not an UActorComponent");

component->DestroyComponent();

Py_RETURN_NONE;
}

PyObject *py_ue_add_actor_component(ue_PyUObject * self, PyObject * args) {

ue_py_check(self);
Expand Down Expand Up @@ -369,7 +395,40 @@ PyObject *py_ue_add_actor_component(ue_PyUObject * self, PyObject * args) {
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
Py_INCREF(ret);
return ret;
}

PyObject *py_ue_add_python_component(ue_PyUObject * self, PyObject * args) {

ue_py_check(self);

char *name;
char *module_name;
char *class_name;
if (!PyArg_ParseTuple(args, "sss:add_python_component", &name, &module_name, &class_name)) {
return NULL;
}

AActor *actor = ue_py_check_type<AActor >(self);
if (!actor) {
return PyErr_Format(PyExc_Exception, "uobject is not an AActor");
}

UPythonComponent *component = NewObject<UPythonComponent>(actor, FName(UTF8_TO_TCHAR(name)));
if (!component)
return PyErr_Format(PyExc_Exception, "unable to create component");

component->PythonModule = FString(UTF8_TO_TCHAR(module_name));
component->PythonClass = FString(UTF8_TO_TCHAR(class_name));

if (actor->GetWorld()) {
component->RegisterComponent();
}

PyObject *ret = (PyObject *)ue_get_python_wrapper(component);
if (!ret)
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
Py_INCREF(ret);
return ret;
}

PyObject *py_ue_actor_create_default_subobject(ue_PyUObject * self, PyObject * args) {
Expand Down Expand Up @@ -580,7 +639,7 @@ PyObject *py_ue_get_actor_components_by_type(ue_PyUObject * self, PyObject * arg
}


PyObject *py_ue_actor_spawn(ue_PyUObject * self, PyObject * args) {
PyObject *py_ue_actor_spawn(ue_PyUObject * self, PyObject * args, PyObject *kwargs) {

ue_py_check(self);

Expand Down Expand Up @@ -629,9 +688,39 @@ PyObject *py_ue_actor_spawn(ue_PyUObject * self, PyObject * args) {
rotation = py_rotation->rot;
}

AActor *actor = world->SpawnActor((UClass *)py_obj->ue_object, &location, &rotation);
AActor *actor = nullptr;
PyObject *ret = nullptr;

if (kwargs && PyDict_Size(kwargs) > 0) {
FTransform transform;
transform.SetTranslation(location);
transform.SetRotation(rotation.Quaternion());
actor = world->SpawnActorDeferred<AActor>((UClass *)py_obj->ue_object, transform);
if (!actor)
return PyErr_Format(PyExc_Exception, "unable to spawn a new Actor");
ue_PyUObject *py_u_obj = ue_get_python_wrapper(actor);
if (!py_u_obj)
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");

PyObject *py_iter = PyObject_GetIter(kwargs);

while (PyObject *py_key = PyIter_Next(py_iter)) {
PyObject *void_ret = py_ue_set_property(py_u_obj, Py_BuildValue("OO", py_key, PyDict_GetItem(kwargs, py_key)));
if (!void_ret) {
return PyErr_Format(PyExc_Exception, "unable to set property for new Actor");
}
}
Py_DECREF(py_iter);
UGameplayStatics::FinishSpawningActor(actor, transform);
ret = (PyObject *)py_u_obj;
}
else {
actor = world->SpawnActor((UClass *)py_obj->ue_object, &location, &rotation);
if (!actor)
return PyErr_Format(PyExc_Exception, "unable to spawn a new Actor");
ret = (PyObject *)ue_get_python_wrapper(actor);
}

PyObject *ret = (PyObject *)ue_get_python_wrapper(actor);
if (!ret)
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
Py_INCREF(ret);
Expand Down
5 changes: 4 additions & 1 deletion Source/UnrealEnginePython/Private/UObject/UEPyActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ PyObject *py_ue_get_editor_world_counterpart_actor(ue_PyUObject *, PyObject *);
#endif
PyObject *py_ue_get_owner(ue_PyUObject *, PyObject *);
PyObject *py_ue_add_actor_component(ue_PyUObject *, PyObject *);
PyObject *py_ue_add_python_component(ue_PyUObject *, PyObject *);
PyObject *py_ue_add_actor_root_component(ue_PyUObject *, PyObject *);
PyObject *py_ue_actor_has_component_of_type(ue_PyUObject *, PyObject *);
PyObject *py_ue_get_actor_component_by_type(ue_PyUObject *, PyObject *);
PyObject *py_ue_get_actor_components_by_type(ue_PyUObject *, PyObject *);
PyObject *py_ue_actor_spawn(ue_PyUObject * self, PyObject *);
PyObject *py_ue_actor_spawn(ue_PyUObject * self, PyObject *, PyObject *);
PyObject *py_ue_get_overlapping_actors(ue_PyUObject *, PyObject *);

PyObject *py_ue_actor_destroy_component(ue_PyUObject *, PyObject *);

PyObject *py_ue_register_component(ue_PyUObject * self, PyObject *);
PyObject *py_ue_unregister_component(ue_PyUObject * self, PyObject *);
PyObject *py_ue_destroy_component(ue_PyUObject * self, PyObject *);
PyObject *py_ue_component_is_registered(ue_PyUObject * self, PyObject *);
PyObject *py_ue_actor_create_default_subobject(ue_PyUObject * self, PyObject *);
Loading

0 comments on commit 03c2f96

Please sign in to comment.