1 /*
2     Copyright © 2020, Inochi2D Project
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module creator.widgets.drag;
8 import creator.widgets;
9 import core.stdc.stdlib : malloc, free;
10 
11 private {
12     struct DragState {
13         float initialState;
14         bool isActive;
15         bool wasJustCreated;
16     }
17 }
18 
19 /**
20     A drag float that only returns true once you're done changing its value
21 */
22 bool incDragFloat(string id, float* value, float adjustSpeed, float minValue, float maxValue, string fmt, ImGuiSliderFlags flags = ImGuiSliderFlags.None) {
23     auto storage = igGetStateStorage();
24     auto igID = igGetID(id.ptr, id.ptr+id.length);
25 
26     // Store initial state if needed
27     float inState = *value;
28 
29     DragState* dragState = cast(DragState*)ImGuiStorage_GetVoidPtr(storage, igID);
30 
31     // initialize if need be
32     if (dragState is null) {
33         dragState = cast(DragState*)malloc(DragState.sizeof);
34 
35         dragState.initialState = inState;
36         dragState.isActive = true;
37         dragState.wasJustCreated = true;
38 
39         ImGuiStorage_SetVoidPtr(storage, igID, dragState);
40     }
41 
42     if (igDragFloat("###DRAG", value, adjustSpeed, minValue, maxValue, fmt.ptr, flags)) {
43         if (!dragState.isActive) {
44             dragState.initialState = inState;
45             dragState.isActive = true;
46             dragState.wasJustCreated = false;
47         }
48         return true;
49     } else {
50         if (dragState !is null && dragState.isActive) {
51             dragState.isActive = false;
52             return !dragState.wasJustCreated;
53         }
54     }
55     return false;
56 }
57 
58 /**
59     Gets whether specified DragFloat has state stored for it
60 */
61 bool incGetHasDragState(string id) {
62     auto storage = igGetStateStorage();
63     auto igID = igGetID(id.ptr, id.ptr+id.length);
64     return ImGuiStorage_GetVoidPtr(storage, igID) !is null;
65 }
66 
67 /**
68     Gets the initial value of the specified drag float
69 
70     Returns NaN if there's no drag state
71 */
72 float incGetDragFloatInitialValue(string id) {
73     auto storage = igGetStateStorage();
74     auto igID = igGetID(id.ptr, id.ptr+id.length);
75 
76     DragState* dragState = cast(DragState*)ImGuiStorage_GetVoidPtr(storage, igID);
77     if (dragState !is null) return dragState.initialState;
78     return float.nan;
79 }