Browse Source

Enhanced `nk_tree_push_id` uniqueness #106

Previously I only used the `__FILE__` and a user `ID` to make
sure to get an unqiue identifier out of it. Like @ands pointed out
this is not enough. Since it is possible to have multipe instances
with the same `title` in the same `__FILE__`. So to fix that I had
the idea to string concat `__FILE__` and `__LINE__` into a string.
This allows to have the same sequence of `IDs` in different places
in one `__FILE__`.
vurtun 9 years ago
parent
commit
cc629f45ca
1 changed files with 5 additions and 2 deletions
  1. 5 2
      nuklear.h

+ 5 - 2
nuklear.h

@@ -281,6 +281,9 @@ typedef unsigned char nk_byte;
  * =========================================================================== */
 #define NK_UNDEFINED (-1.0f)
 #define NK_FLAG(x) (1 << (x))
+#define NK_STRINGIFY(x) #x
+#define NK_LINE_STR(x) NK_STRINGIFY(x)
+#define NK_FILE_LINE __FILE__":"NK_LINE_STR(__LINE__)
 
 struct nk_buffer;
 struct nk_allocator;
@@ -572,7 +575,7 @@ NK_API void                     nk_group_end(struct nk_context*);
 
 /* Layout: Tree */
 #define                         nk_tree_push(ctx, type, title, state) nk_tree_push_hashed(ctx, type, title, state, __FILE__,nk_strlen(__FILE__),__LINE__)
-#define                         nk_tree_push_id(ctx, type, tile, state, id) nk_tree_push_hashed(ctx, type, title, state, __FILE__,nk_strlen(__FILE__),id)
+#define                         nk_tree_push_id(ctx, type, title, state, id) nk_tree_push_hashed(ctx, type, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
 NK_API int                      nk_tree_push_hashed(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
 NK_API void                     nk_tree_pop(struct nk_context*);
 
@@ -16583,7 +16586,7 @@ nk_tree_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
 
     /* find or create tab persistent state (open/closed) */
     title_len = (int)nk_strlen(title);
-    title_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
+    title_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)type);
     if (hash) title_hash += nk_murmur_hash(hash, len, (nk_hash)line);
     state = nk_find_value(win, title_hash);
     if (!state) {