55#include " UEPySCompoundWidget.h"
66#include " UEPyFGeometry.h"
77#include " UEPyFPaintContext.h"
8+ #include " UEPyFCharacterEvent.h"
9+ #include " UEPyFKeyEvent.h"
810
911extern PyTypeObject ue_PySPythonWidgetType;
1012
@@ -13,14 +15,80 @@ class SPythonWidget : public SCompoundWidget
1315public:
1416
1517 SLATE_BEGIN_ARGS (SPythonWidget)
16- { }
18+ {
19+ }
1720 SLATE_END_ARGS ();
1821
1922 void Construct (const FArguments& Args)
2023 {
2124
2225 }
2326
27+ virtual bool SupportsKeyboardFocus () const override
28+ {
29+ return true ;
30+ }
31+
32+ virtual FReply OnKeyChar (const FGeometry & MyGeometry, const FCharacterEvent & InCharacterEvent) override
33+ {
34+ FScopePythonGIL gil;
35+
36+ if (!PyObject_HasAttrString (self, (char *)" on_key_char" ))
37+ return FReply::Unhandled ();
38+
39+ PyObject *py_callable_on_key_char = PyObject_GetAttrString (self, (char *)" on_key_char" );
40+ if (!PyCallable_Check (py_callable_on_key_char))
41+ {
42+ UE_LOG (LogPython, Error, TEXT (" on_key_char is not a callable" ));
43+ return FReply::Unhandled ();
44+ }
45+
46+ PyObject *ret = PyObject_CallFunction (py_callable_on_key_char, (char *)" OO" , py_ue_new_fgeometry (MyGeometry), py_ue_new_fcharacter_event (InCharacterEvent));
47+ if (!ret)
48+ {
49+ unreal_engine_py_log_error ();
50+ return FReply::Unhandled ();
51+ }
52+
53+ if (ret == Py_False)
54+ {
55+ Py_DECREF (ret);
56+ return FReply::Unhandled ();
57+ }
58+ Py_DECREF (ret);
59+ return FReply::Handled ();
60+ }
61+
62+ virtual FReply OnKeyDown (const FGeometry & MyGeometry, const FKeyEvent & InKeyEvent) override
63+ {
64+ FScopePythonGIL gil;
65+
66+ if (!PyObject_HasAttrString (self, (char *)" on_key_down" ))
67+ return FReply::Unhandled ();
68+
69+ PyObject *py_callable_on_key_down = PyObject_GetAttrString (self, (char *)" on_key_down" );
70+ if (!PyCallable_Check (py_callable_on_key_down))
71+ {
72+ UE_LOG (LogPython, Error, TEXT (" on_key_down is not a callable" ));
73+ return FReply::Unhandled ();
74+ }
75+
76+ PyObject *ret = PyObject_CallFunction (py_callable_on_key_down, (char *)" OO" , py_ue_new_fgeometry (MyGeometry), py_ue_new_fkey_event (InKeyEvent));
77+ if (!ret)
78+ {
79+ unreal_engine_py_log_error ();
80+ return FReply::Unhandled ();
81+ }
82+
83+ if (ret == Py_False)
84+ {
85+ Py_DECREF (ret);
86+ return FReply::Unhandled ();
87+ }
88+ Py_DECREF (ret);
89+ return FReply::Handled ();
90+ }
91+
2492 virtual int32 OnPaint (const FPaintArgs & Args,
2593 const FGeometry & AllottedGeometry,
2694 const FSlateRect & MyClippingRect,
@@ -30,23 +98,29 @@ class SPythonWidget : public SCompoundWidget
3098 bool bParentEnabled) const override
3199 {
32100
33- if (py_callable_paint)
34- {
35- FPaintContext context (AllottedGeometry, MyClippingRect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled);
101+ FScopePythonGIL gil;
36102
37- FScopePythonGIL gil;
103+ if (!PyObject_HasAttrString (self, (char *)" paint" ))
104+ return LayerId + 1 ;
38105
39- PyObject *ret = PyObject_CallFunction (py_callable_paint , (char *)" O " , py_ue_new_fpaint_context (context) );
40- if (!ret )
41- {
42- unreal_engine_py_log_error ( );
43- return LayerId + 1 ;
44- }
106+ PyObject *py_callable_paint = PyObject_GetAttrString (self , (char *)" paint " );
107+ if (!PyCallable_Check (py_callable_paint) )
108+ {
109+ UE_LOG (LogPython, Error, TEXT ( " paint is not a callable " ) );
110+ return LayerId + 1 ;
111+ }
45112
46- Py_DECREF (ret );
113+ FPaintContext context (AllottedGeometry, MyClippingRect, OutDrawElements, LayerId, InWidgetStyle, bParentEnabled );
47114
115+ PyObject *ret = PyObject_CallFunction (py_callable_paint, (char *)" O" , py_ue_new_fpaint_context (context));
116+ if (!ret)
117+ {
118+ unreal_engine_py_log_error ();
119+ return LayerId + 1 ;
48120 }
49121
122+ Py_DECREF (ret);
123+
50124 return LayerId + 1 ;
51125
52126 }
@@ -55,11 +129,17 @@ class SPythonWidget : public SCompoundWidget
55129 const double InCurrentTime,
56130 const float InDeltaTime) override
57131 {
132+ FScopePythonGIL gil;
58133
59- if (!py_callable_tick )
134+ if (!PyObject_HasAttrString (self, ( char *) " tick " ) )
60135 return ;
61136
62- FScopePythonGIL gil;
137+ PyObject *py_callable_tick = PyObject_GetAttrString (self, (char *)" tick" );
138+ if (!PyCallable_Check (py_callable_tick))
139+ {
140+ UE_LOG (LogPython, Error, TEXT (" tick is not a callable" ));
141+ return ;
142+ }
63143
64144 PyObject *ret = PyObject_CallFunction (py_callable_tick, (char *)" Off" , py_ue_new_fgeometry (AllottedGeometry), InCurrentTime, InDeltaTime);
65145 if (!ret)
@@ -71,19 +151,13 @@ class SPythonWidget : public SCompoundWidget
71151 Py_DECREF (ret);
72152 }
73153
74- void SetPythonPaint (PyObject *py_callable)
75- {
76- py_callable_paint = py_callable;
77- }
78-
79- void SetPythonTick (PyObject *py_callable)
154+ void SetPyObject (PyObject *py_obj)
80155 {
81- py_callable_tick = py_callable ;
156+ self = py_obj ;
82157 }
83158
84159protected:
85- PyObject *py_callable_tick;
86- PyObject *py_callable_paint;
160+ PyObject *self;
87161
88162};
89163
0 commit comments