Skip to content

Commit 1f6fe7f

Browse files
hoffstadt-metecshoffstadt
authored andcommitted
feat: add combo imgui widget
1 parent 0056f1c commit 1f6fe7f

6 files changed

Lines changed: 249 additions & 57 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pilotlight/imgui_d.pyd
1717
pilotlight/*.pdb
1818
pilotlight/*.dll
1919
pilotlight/*.so
20+
pilotlight/*.rdi
2021
pilotlight/*.dSYM
2122
pilotlight/shaders/
2223

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"program": "${workspaceFolder}/out/pilotlight_python.exe",
88
"console": "integratedTerminal",
99
"request": "launch",
10-
"args": [],
10+
"args": ["../sandbox/demo.py"],
1111
"stopAtEntry": false,
1212
"cwd": "${workspaceFolder}/out/",
1313
"environment": []

extensions/pl_dearimgui_ext_m.cpp

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Index of this file:
3636
#include "pl_math.h"
3737

3838
#include "pl_graphics_ext.h"
39+
#include "pl_ds.h"
3940

4041
bool pl_parse(const char* formatstring, const char** keywords, PyObject* args, PyObject* kwargs, const char* message, ...);
4142
static ImVec2 pl__get_vec2_from_python(PyObject* ptValue);
@@ -315,19 +316,19 @@ ImGui_MenuItem(PyObject* self, PyObject* args, PyObject* kwargs)
315316
const char* pcLabel = nullptr;
316317
const char* pcShortcut = nullptr;
317318
int bEnabled = true;
318-
int bSelected = true;
319+
int bSelected = false;
319320
PyObject* ptPointer = Py_None;
320321
if (!pl_parse("s|spp$O", (const char**)apcKeywords, args, kwargs, __FUNCTION__,
321322
&pcLabel, &pcShortcut, &bSelected, &bEnabled, &ptPointer))
322323
return nullptr;
323324

324-
bool* pbSelected = nullptr;
325+
bool bbSelected = (bool)bSelected;
326+
bool* pbSelected = &bbSelected;
325327
if(!Py_IsNone(ptPointer))
326328
pbSelected = (bool*)PyCapsule_GetPointer(ptPointer, "plBoolPointer");
327329

328-
if(pbSelected)
329-
return PyBool_FromLong(ImGui::MenuItem(pcLabel, pcShortcut, pbSelected, bEnabled));
330-
return PyBool_FromLong(ImGui::MenuItem(pcLabel, pcShortcut, bSelected, bEnabled));
330+
bool bActivated = ImGui::MenuItem(pcLabel, pcShortcut, pbSelected, bEnabled);
331+
return Py_BuildValue("(pp)", bActivated, bbSelected);
331332
}
332333

333334
PyObject*
@@ -2992,6 +2993,42 @@ ImGui_EndCombo(PyObject* self)
29922993
Py_RETURN_NONE;
29932994
}
29942995

2996+
PyObject*
2997+
ImGui_Combo(PyObject* self, PyObject* args, PyObject* kwargs)
2998+
{
2999+
static const char* apcKeywords[] = {
3000+
"label",
3001+
"current_item",
3002+
"items",
3003+
"popup_max_height_in_items",
3004+
nullptr,
3005+
};
3006+
static const char** sbtEntries = nullptr;
3007+
3008+
const char* pcLabel = nullptr;
3009+
PyObject* ptCurrentItem = nullptr;
3010+
PyObject* ptItems = nullptr;
3011+
int popup_max_height_in_items = -1;
3012+
if (!pl_parse("sOO|i", (const char**)apcKeywords, args, kwargs, __FUNCTION__,
3013+
&pcLabel, &ptCurrentItem, &ptItems, &popup_max_height_in_items))
3014+
return nullptr;
3015+
3016+
int* ptCurrentItemInt = (int*)PyCapsule_GetPointer(ptCurrentItem, "plIntPointer");
3017+
3018+
Py_ssize_t pySize = PyList_Size(ptItems);
3019+
pl_sb_resize(sbtEntries, (uint32_t)pySize);
3020+
for(Py_ssize_t i = 0; i < pySize; i++)
3021+
{
3022+
PyObject* item = PyList_GetItem(ptItems, i);
3023+
sbtEntries[i] = PyUnicode_AsUTF8(item);
3024+
}
3025+
3026+
bool bActivated = ImGui::Combo(pcLabel, ptCurrentItemInt, sbtEntries, (int)pySize, popup_max_height_in_items);
3027+
pl_sb_reset(sbtEntries);
3028+
return Py_BuildValue("p", bActivated);
3029+
}
3030+
3031+
29953032
PyObject*
29963033
ImGui_BeginListBox(PyObject* self, PyObject* args, PyObject* kwargs)
29973034
{
@@ -3398,6 +3435,7 @@ static PyMethodDef gatCommands[] =
33983435
PL_PYTHON_COMMAND(ImGui_SetKeyboardFocusHere, METH_VARARGS | METH_KEYWORDS, NULL),
33993436

34003437
// imgui combo box/dropdown widgets
3438+
PL_PYTHON_COMMAND(ImGui_Combo, METH_VARARGS | METH_KEYWORDS, NULL),
34013439
PL_PYTHON_COMMAND(ImGui_BeginCombo, METH_VARARGS | METH_KEYWORDS, NULL),
34023440
PL_PYTHON_COMMAND(ImGui_EndCombo, METH_NOARGS, NULL),
34033441

pilotlight/imgui.pyi

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, NewType
1+
from typing import List, NewType, Tuple
22
from pilotlight.types import *
33
from pilotlight.enums import *
44

@@ -80,33 +80,12 @@ def ImGui_StyleColorsLight():
8080
def ImGui_StyleColorsClassic():
8181
...
8282

83-
def ImGui_Begin(name, bool_pointer: plBoolPointer = None, flags: ImGuiWindowFlags = 0) -> None:
83+
def ImGui_Begin(name: str, bool_pointer: plBoolPointer = None, flags: ImGuiWindowFlags = 0) -> bool:
8484
...
8585

8686
def ImGui_End():
8787
...
8888

89-
def ImGui_BeginMenuBar():
90-
...
91-
92-
def ImGui_BeginMainMenuBar():
93-
...
94-
95-
def ImGui_EndMenuBar():
96-
...
97-
98-
def ImGui_EndMainMenuBar():
99-
...
100-
101-
def ImGui_BeginMenu(label, enabled=True):
102-
...
103-
104-
def ImGui_EndMenu():
105-
...
106-
107-
def ImGui_MenuItem(label, shortcut="", selected=False, enabled=True, selected_pointer: plBoolPointer = None):
108-
...
109-
11089
########################################################################################################################
11190
# [SECTION] imgui window utilities
11291
########################################################################################################################
@@ -126,10 +105,10 @@ def ImGui_IsWindowHovered(flags: ImGuiHoveredFlags = 0) -> bool:
126105
def ImGui_GetWindowDpiScale() -> float:
127106
...
128107

129-
def ImGui_GetWindowPos() -> tuple[float, float]:
108+
def ImGui_GetWindowPos() -> Tuple[float, float]:
130109
...
131110

132-
def ImGui_GetWindowSize() -> tuple[float, float]:
111+
def ImGui_GetWindowSize() -> Tuple[float, float]:
133112
...
134113

135114
def ImGui_GetWindowWidth() -> float:
@@ -178,6 +157,41 @@ def ImGui_SetWindowCollapsed(collapsed: bool, cond: ImGuiCond = 0, name: str = N
178157
def ImGui_SetWindowFocus(name: str = None) -> None:
179158
...
180159

160+
########################################################################################################################
161+
# [SECTION] imgui menu stuff
162+
########################################################################################################################
163+
164+
def ImGui_BeginMenuBar() -> bool:
165+
...
166+
167+
def ImGui_BeginMainMenuBar() -> bool:
168+
...
169+
170+
def ImGui_EndMenuBar() -> None:
171+
...
172+
173+
def ImGui_EndMainMenuBar() -> None:
174+
...
175+
176+
def ImGui_BeginMenu(label:str, enabled:bool=True) -> bool:
177+
...
178+
179+
def ImGui_EndMenu() -> None:
180+
...
181+
182+
def ImGui_MenuItem(label:str, shortcut:str="", selected:bool=False, enabled:bool=True, selected_pointer: plBoolPointer = None) -> Tuple[bool, bool]:
183+
...
184+
185+
########################################################################################################################
186+
# [SECTION] imgui child windows
187+
########################################################################################################################
188+
189+
def ImGui_BeginChild(str_id: str, size:List[int, int]=None, child_flags: ImGuiChildFlags = 0, window_flags: ImGuiWindowFlags = 0) -> bool:
190+
...
191+
192+
def ImGui_EndChild() -> None:
193+
...
194+
181195
########################################################################################################################
182196
# [SECTION] imgui input utilities mouse
183197
########################################################################################################################
@@ -206,16 +220,16 @@ def ImGui_IsMousePosValid(mouse_pos=None) -> bool:
206220
def ImGui_IsAnyMouseDown() -> bool:
207221
...
208222

209-
def ImGui_GetMousePos() -> tuple[float, float]:
223+
def ImGui_GetMousePos() -> Tuple[float, float]:
210224
...
211225

212-
def ImGui_GetMousePosOnOpeningCurrentPopup() -> tuple[float, float]:
226+
def ImGui_GetMousePosOnOpeningCurrentPopup() -> Tuple[float, float]:
213227
...
214228

215229
def ImGui_IsMouseDragging(button: ImGuiMouseButton, lock_threshold: float = -1.0) -> bool:
216230
...
217231

218-
def ImGui_GetMouseDragDelta(button: ImGuiMouseButton = 0, lock_threshold: float = -1.0) -> tuple[float, float]:
232+
def ImGui_GetMouseDragDelta(button: ImGuiMouseButton = 0, lock_threshold: float = -1.0) -> Tuple[float, float]:
219233
...
220234

221235
def ImGui_ResetMouseDragDelta(button: ImGuiMouseButton = 0) -> None:
@@ -257,16 +271,6 @@ def ImGui_SaveIniSettingsToDisk(ini_filename: str) -> None:
257271
def ImGui_SaveIniSettingsToMemory() -> str | None:
258272
...
259273

260-
########################################################################################################################
261-
# [SECTION] imgui child windows
262-
########################################################################################################################
263-
264-
def ImGui_BeginChild(str_id: str, size=None, child_flags: ImGuiChildFlags = 0, window_flags: ImGuiWindowFlags = 0) -> bool:
265-
...
266-
267-
def ImGui_EndChild() -> None:
268-
...
269-
270274
########################################################################################################################
271275
# [SECTION] imgui id stack/scopes
272276
########################################################################################################################
@@ -284,10 +288,10 @@ def ImGui_GetID(id: str | int) -> int:
284288
# [SECTION] imgui main widgets
285289
########################################################################################################################
286290

287-
def ImGui_Button(label, size=None):
291+
def ImGui_Button(label:str, size=None):
288292
...
289293

290-
def ImGui_Checkbox(label, bool_pointer: plBoolPointer):
294+
def ImGui_Checkbox(label:str, bool_pointer: plBoolPointer):
291295
...
292296

293297
def ImGui_SmallButton(label: str) -> bool:
@@ -327,6 +331,9 @@ def ImGui_BeginCombo(label: str, preview_value: str, flags: ImGuiComboFlags = 0)
327331
def ImGui_EndCombo() -> None:
328332
...
329333

334+
def ImGui_Combo(label: str, current_item: plIntPointer, items: List[str], popup_max_height_in_items: int = -1) -> bool:
335+
...
336+
330337
########################################################################################################################
331338
# [SECTION] imgui list boxes
332339
########################################################################################################################
@@ -552,16 +559,16 @@ def ImGui_GetFrameHeightWithSpacing() -> float:
552559
# [SECTION] imgui layout cursor positioning
553560
########################################################################################################################
554561

555-
def ImGui_GetCursorScreenPos() -> tuple[float, float]:
562+
def ImGui_GetCursorScreenPos() -> Tuple[float, float]:
556563
...
557564

558565
def ImGui_SetCursorScreenPos(pos) -> None:
559566
...
560567

561-
def ImGui_GetContentRegionAvail() -> tuple[float, float]:
568+
def ImGui_GetContentRegionAvail() -> Tuple[float, float]:
562569
...
563570

564-
def ImGui_GetCursorPos() -> tuple[float, float]:
571+
def ImGui_GetCursorPos() -> Tuple[float, float]:
565572
...
566573

567574
def ImGui_GetCursorPosX() -> float:
@@ -579,7 +586,7 @@ def ImGui_SetCursorPosX(local_x: float) -> None:
579586
def ImGui_SetCursorPosY(local_y: float) -> None:
580587
...
581588

582-
def ImGui_GetCursorStartPos() -> tuple[float, float]:
589+
def ImGui_GetCursorStartPos() -> Tuple[float, float]:
583590
...
584591

585592
########################################################################################################################
@@ -793,13 +800,13 @@ def ImGui_IsAnyItemFocused() -> bool:
793800
def ImGui_GetItemID() -> int:
794801
...
795802

796-
def ImGui_GetItemRectMin() -> tuple[float, float]:
803+
def ImGui_GetItemRectMin() -> Tuple[float, float]:
797804
...
798805

799-
def ImGui_GetItemRectMax() -> tuple[float, float]:
806+
def ImGui_GetItemRectMax() -> Tuple[float, float]:
800807
...
801808

802-
def ImGui_GetItemRectSize() -> tuple[float, float]:
809+
def ImGui_GetItemRectSize() -> Tuple[float, float]:
803810
...
804811

805812
def ImGui_SetItemAllowOverlap() -> None:

0 commit comments

Comments
 (0)