|
| 1 | + |
| 2 | +#include "UnrealEnginePythonPrivatePCH.h" |
| 3 | + |
| 4 | +#include "UEPySPythonComboBox.h" |
| 5 | + |
| 6 | + |
| 7 | +#define sw_python_combo_box StaticCastSharedRef<SPythonComboBox>(self->s_panel.s_widget.s_widget) |
| 8 | + |
| 9 | +static PyObject *py_ue_spython_combo_box_clear_selection(ue_PySPythonComboBox *self, PyObject * args) { |
| 10 | + sw_python_combo_box->ClearSelection(); |
| 11 | + Py_INCREF(self); |
| 12 | + return (PyObject *)self; |
| 13 | +} |
| 14 | + |
| 15 | +static PyObject *py_ue_spython_combo_box_get_selected_item(ue_PySPythonComboBox *self, PyObject * args) { |
| 16 | + |
| 17 | + TSharedPtr<FPythonItem> ptr_item = sw_python_combo_box->GetSelectedItem(); |
| 18 | + if (!ptr_item.IsValid()) |
| 19 | + return PyErr_Format(PyExc_Exception, "invalid shared pointer to python item"); |
| 20 | + |
| 21 | + Py_INCREF(ptr_item.Get()->py_object); |
| 22 | + return ptr_item.Get()->py_object; |
| 23 | +} |
| 24 | + |
| 25 | +static PyObject *py_ue_spython_combo_box_set_selected_item(ue_PySPythonComboBox *self, PyObject * args) { |
| 26 | + PyObject *py_item; |
| 27 | + if (!PyArg_ParseTuple(args, "O", &py_item)) { |
| 28 | + return nullptr; |
| 29 | + } |
| 30 | + |
| 31 | + for (TSharedPtr<FPythonItem> item : *(sw_python_combo_box->PythonOptionsSource)) { |
| 32 | + // just for being safe |
| 33 | + if (!item.IsValid()) |
| 34 | + continue; |
| 35 | + if (py_item == item.Get()->py_object) { |
| 36 | + sw_python_combo_box->SetSelectedItem(item); |
| 37 | + break; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + Py_INCREF(self); |
| 42 | + return (PyObject *)self; |
| 43 | +} |
| 44 | + |
| 45 | +static PyMethodDef ue_PySPythonComboBox_methods[] = { |
| 46 | + { "clear_selection", (PyCFunction)py_ue_spython_combo_box_clear_selection, METH_VARARGS, "" }, |
| 47 | + { "get_selected_item", (PyCFunction)py_ue_spython_combo_box_get_selected_item, METH_VARARGS, "" }, |
| 48 | + { "set_selected_item", (PyCFunction)py_ue_spython_combo_box_set_selected_item, METH_VARARGS, "" }, |
| 49 | + { NULL } /* Sentinel */ |
| 50 | +}; |
| 51 | + |
| 52 | +PyTypeObject ue_PySPythonComboBoxType = { |
| 53 | + PyVarObject_HEAD_INIT(NULL, 0) |
| 54 | + "unreal_engine.SPythonComboBox", /* tp_name */ |
| 55 | + sizeof(ue_PySPythonComboBox), /* tp_basicsize */ |
| 56 | + 0, /* tp_itemsize */ |
| 57 | + 0, /* tp_dealloc */ |
| 58 | + 0, /* tp_print */ |
| 59 | + 0, /* tp_getattr */ |
| 60 | + 0, /* tp_setattr */ |
| 61 | + 0, /* tp_reserved */ |
| 62 | + 0, /* tp_repr */ |
| 63 | + 0, /* tp_as_number */ |
| 64 | + 0, /* tp_as_sequence */ |
| 65 | + 0, /* tp_as_mapping */ |
| 66 | + 0, /* tp_hash */ |
| 67 | + 0, /* tp_call */ |
| 68 | + 0, /* tp_str */ |
| 69 | + 0, /* tp_getattro */ |
| 70 | + 0, /* tp_setattro */ |
| 71 | + 0, /* tp_as_buffer */ |
| 72 | + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 73 | + "Unreal Engine SPythonComboBox", /* tp_doc */ |
| 74 | + 0, /* tp_traverse */ |
| 75 | + 0, /* tp_clear */ |
| 76 | + 0, /* tp_richcompare */ |
| 77 | + 0, /* tp_weaklistoffset */ |
| 78 | + 0, /* tp_iter */ |
| 79 | + 0, /* tp_iternext */ |
| 80 | + ue_PySPythonComboBox_methods, /* tp_methods */ |
| 81 | +}; |
| 82 | + |
| 83 | +static int ue_py_spython_combo_box_init(ue_PySPythonComboBox *self, PyObject *args, PyObject *kwargs) { |
| 84 | + ue_py_slate_setup_farguments(SPythonComboBox); |
| 85 | + |
| 86 | + // first of all check for values |
| 87 | + PyObject *values = ue_py_dict_get_item(kwargs, "values"); |
| 88 | + if (!values) { |
| 89 | + PyErr_SetString(PyExc_Exception, "you must specify the combo box items"); |
| 90 | + return -1; |
| 91 | + } |
| 92 | + |
| 93 | + values = PyObject_GetIter(values); |
| 94 | + if (!values) { |
| 95 | + PyErr_SetString(PyExc_Exception, "values field is not an iterable"); |
| 96 | + return -1; |
| 97 | + } |
| 98 | + |
| 99 | + TArray<TSharedPtr<FPythonItem>> *items = new TArray<TSharedPtr<FPythonItem>>(); |
| 100 | + while (PyObject *item = PyIter_Next(values)) { |
| 101 | + Py_INCREF(item); |
| 102 | + // keep track of items |
| 103 | + self->s_panel.s_widget.py_refs.Add(item); |
| 104 | + items->Add(TSharedPtr<FPythonItem>(new FPythonItem(item))); |
| 105 | + } |
| 106 | + Py_DECREF(values); |
| 107 | + |
| 108 | + arguments.OptionsSource(items); |
| 109 | + |
| 110 | + ue_PySWidget *s_widget_content = nullptr; |
| 111 | + |
| 112 | + PyObject *content = ue_py_dict_get_item(kwargs, "content"); |
| 113 | + if (content) { |
| 114 | + s_widget_content = py_ue_is_swidget(content); |
| 115 | + if (!content) { |
| 116 | + PyErr_SetString(PyExc_Exception, "content is not a SWidget"); |
| 117 | + return -1; |
| 118 | + } |
| 119 | + Py_INCREF(s_widget_content); |
| 120 | + arguments.Content()[s_widget_content->s_widget]; |
| 121 | + } |
| 122 | + |
| 123 | + ue_py_slate_farguments_optional_struct_ptr("button_style", ButtonStyle, FButtonStyle); |
| 124 | + ue_py_slate_farguments_struct("content_padding", ContentPadding, FMargin); |
| 125 | + ue_py_slate_farguments_optional_bool("enable_gamepad_navigation_mode", EnableGamepadNavigationMode); |
| 126 | + ue_py_slate_farguments_struct("foreground_color", ForegroundColor, FSlateColor); |
| 127 | + ue_py_slate_farguments_optional_bool("has_down_arrow", HasDownArrow); |
| 128 | + ue_py_slate_farguments_optional_struct_ptr("item_style", ItemStyle, FTableRowStyle); |
| 129 | + ue_py_slate_farguments_optional_float("max_list_height", MaxListHeight); |
| 130 | + ue_py_slate_farguments_optional_enum("method", Method, EPopupMethod); |
| 131 | + ue_py_slate_farguments_optional_struct("pressed_sound_override", PressedSoundOverride, FSlateSound); |
| 132 | + ue_py_slate_farguments_optional_struct("pressed_sound_override", PressedSoundOverride, FSlateSound); |
| 133 | + ue_py_slate_farguments_event("on_generate_widget", OnGenerateWidget, TSlateDelegates<TSharedPtr<FPythonItem>>::FOnGenerateWidget, OnGenerateWidget); |
| 134 | + ue_py_slate_farguments_event("on_selection_changed", OnSelectionChanged, TSlateDelegates<TSharedPtr<FPythonItem>>::FOnSelectionChanged, OnSelectionChanged); |
| 135 | + |
| 136 | + |
| 137 | + ue_py_snew(SPythonComboBox, s_panel.s_widget); |
| 138 | + |
| 139 | + // keep track of the list, so we can delete on destruction |
| 140 | + sw_python_combo_box->PythonOptionsSource = items; |
| 141 | + |
| 142 | + // eventually track the content |
| 143 | + self->s_panel.s_widget.py_swidget_content = s_widget_content; |
| 144 | + |
| 145 | + return 0; |
| 146 | +} |
| 147 | + |
| 148 | +void ue_python_init_spython_combo_box(PyObject *ue_module) { |
| 149 | + |
| 150 | + ue_PySPythonComboBoxType.tp_base = &ue_PySPanelType; |
| 151 | + ue_PySPythonComboBoxType.tp_init = (initproc)ue_py_spython_combo_box_init; |
| 152 | + |
| 153 | + if (PyType_Ready(&ue_PySPythonComboBoxType) < 0) |
| 154 | + return; |
| 155 | + |
| 156 | + Py_INCREF(&ue_PySPythonComboBoxType); |
| 157 | + PyModule_AddObject(ue_module, "SPythonComboBox", (PyObject *)&ue_PySPythonComboBoxType); |
| 158 | +} |
0 commit comments