|
| 1 | +package imgui; |
| 2 | + |
| 3 | +/** |
| 4 | + * Helper: Key-Value storage |
| 5 | + * Typically you don't have to worry about this since a storage is held within each Window. |
| 6 | + * We use it to e.g. store collapse state for a tree (Int 0/1) |
| 7 | + * This is optimized for efficient lookup (dichotomy into a contiguous buffer) and rare insertion (typically tied to user interactions aka max once a frame) |
| 8 | + * You can use it as custom user storage for temporary values. Declare your own storage if, for example: |
| 9 | + * - You want to manipulate the open/close state of a particular sub-tree in your interface (tree node uses Int 0/1 to store their state). |
| 10 | + * - You want to store custom debug data easily without adding or editing structures in your code (probably not efficient, but convenient) |
| 11 | + * Types are NOT stored, so it is up to you to make sure your Key don't collide with different types. |
| 12 | + */ |
| 13 | +public final class ImGuiStorage implements ImGuiDestroyableStruct { |
| 14 | + long ptr; |
| 15 | + |
| 16 | + /** |
| 17 | + * This class will create a native structure. |
| 18 | + * Call {@link #destroy()} method to manually free used memory. |
| 19 | + */ |
| 20 | + public ImGuiStorage() { |
| 21 | + ImGui.touch(); |
| 22 | + ptr = nCreate(); |
| 23 | + } |
| 24 | + |
| 25 | + ImGuiStorage(final long ptr) { |
| 26 | + this.ptr = ptr; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void destroy() { |
| 31 | + nDestroy(ptr); |
| 32 | + } |
| 33 | + |
| 34 | + /*JNI |
| 35 | + #include <stdint.h> |
| 36 | + #include <imgui.h> |
| 37 | +
|
| 38 | + jfieldID imGuiStoragePtrID; |
| 39 | +
|
| 40 | + #define IMGUI_STORAGE ((ImGuiStorage*)env->GetLongField(object, imGuiStoragePtrID)) |
| 41 | + */ |
| 42 | + |
| 43 | + static native void nInit(); /* |
| 44 | + jclass jImGuiStorageClass = env->FindClass("imgui/ImGuiStorage"); |
| 45 | + imGuiStoragePtrID = env->GetFieldID(jImGuiStorageClass, "ptr", "J"); |
| 46 | + */ |
| 47 | + |
| 48 | + private native long nCreate(); /* |
| 49 | + ImGuiStorage* imGuiStorage = new ImGuiStorage(); |
| 50 | + return (intptr_t)imGuiStorage; |
| 51 | + */ |
| 52 | + |
| 53 | + private native void nDestroy(long ptr); /* |
| 54 | + delete (ImGuiStorage*)ptr; |
| 55 | + */ |
| 56 | + |
| 57 | + // - Get***() functions find pair, never add/allocate. Pairs are sorted so a query is O(log N) |
| 58 | + // - Set***() functions find pair, insertion on demand if missing. |
| 59 | + // - Sorted insertion is costly, paid once. A typical frame shouldn't need to insert any new pair. |
| 60 | + |
| 61 | + public native void clear(); /* |
| 62 | + IMGUI_STORAGE->Clear(); |
| 63 | + */ |
| 64 | + |
| 65 | + public native int getInt(int imGuiID); /* |
| 66 | + return IMGUI_STORAGE->GetInt(imGuiID); |
| 67 | + */ |
| 68 | + |
| 69 | + public native int getInt(int imGuiID, int defaultVal); /* |
| 70 | + return IMGUI_STORAGE->GetInt(imGuiID, defaultVal); |
| 71 | + */ |
| 72 | + |
| 73 | + public native void setInt(int imGuiID, int val); /* |
| 74 | + IMGUI_STORAGE->SetInt(imGuiID, val); |
| 75 | + */ |
| 76 | + |
| 77 | + public native boolean getBool(int imGuiID); /* |
| 78 | + return IMGUI_STORAGE->GetBool(imGuiID); |
| 79 | + */ |
| 80 | + |
| 81 | + public native boolean getBool(int imGuiID, boolean defaultVal); /* |
| 82 | + return IMGUI_STORAGE->GetBool(imGuiID, defaultVal); |
| 83 | + */ |
| 84 | + |
| 85 | + public native void setBool(int imGuiID, boolean val); /* |
| 86 | + IMGUI_STORAGE->SetBool(imGuiID, val); |
| 87 | + */ |
| 88 | + |
| 89 | + public native float getFloat(int imGuiID); /* |
| 90 | + return IMGUI_STORAGE->GetFloat(imGuiID); |
| 91 | + */ |
| 92 | + |
| 93 | + public native float getFloat(int imGuiID, float defaultVal); /* |
| 94 | + return IMGUI_STORAGE->GetFloat(imGuiID, defaultVal); |
| 95 | + */ |
| 96 | + |
| 97 | + public native void setFloat(int imGuiID, float val); /* |
| 98 | + IMGUI_STORAGE->SetFloat(imGuiID, val); |
| 99 | + */ |
| 100 | + |
| 101 | + /** |
| 102 | + * Use on your own storage if you know only integer are being stored (open/close all tree nodes) |
| 103 | + */ |
| 104 | + public native void setAllInt(int val); /* |
| 105 | + IMGUI_STORAGE->SetAllInt(val); |
| 106 | + */ |
| 107 | + |
| 108 | + /** |
| 109 | + * For quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once. |
| 110 | + */ |
| 111 | + public native void buildSortByKey(); /* |
| 112 | + IMGUI_STORAGE->BuildSortByKey(); |
| 113 | + */ |
| 114 | +} |
0 commit comments