1 module creator.core.tasks;
2 import core.thread.fiber;
3 import bindbc.imgui;
4 
5 private {
6 __gshared:
7     Task[] tasks;
8     string status_ = "No pending tasks...";
9     float progress_ = -1;
10 
11     string status;
12     float statusTime = 0;
13     enum STATUS_TIME_SET = 5.0;
14 }
15 
16 public:
17 
18 /**
19     A task
20 */
21 struct Task {
22     /**
23         The name of the task
24     */
25     string name;
26 
27     /**
28         The task's worker
29     */
30     Fiber worker;
31 }
32 
33 /**
34     Adds task to the list
35 */
36 void incTaskAdd(string name, void delegate() worker) {
37     tasks ~= Task(name, new Fiber(worker));
38 }
39 
40 /**
41     Sets the status of task
42 */
43 void incTaskStatus(string status) {
44     status_ = status;
45 }
46 
47 /**
48     Gets the curently posted status
49 */
50 string incTaskGetStatus() {
51     return status_;
52 }
53 
54 /**
55     Gets the current progress of the current task
56 */
57 float incTaskGetProgress() {
58     return progress_;
59 }
60 
61 /**
62     Sets the progress of the current task
63 */
64 void incTaskProgress(float progress) {
65     progress_ = progress;
66 }
67 
68 /**
69     Gets count of pending tasks
70 */
71 size_t incTaskLength() {
72     return tasks.length;
73 }
74 
75 /**
76     Yields a task/fiber
77 */
78 void incTaskYield() {
79     Fiber.yield();
80 }
81 
82 /**
83     Updates tasks
84 */
85 void incTaskUpdate() {
86     if (tasks.length > 0) {
87         if (tasks[0].worker.state != Fiber.State.TERM) {
88             tasks[0].worker.call();
89         } else {
90             tasks = tasks[1..$];
91             progress_ = -1;
92         }
93 
94         if (tasks.length == 0) {
95             incTaskStatus("No pending tasks...");
96         }
97     }
98 }
99 
100 /**
101     Sets status in status area if previous item is hovered
102 */
103 void incHoverStatus(string status_) {
104     if (igIsItemHovered()) incSetStatus(status_);
105 }
106 
107 /**
108     Sets status in status area
109 */
110 void incSetStatus(string status_) {
111     status = status_;
112     statusTime = STATUS_TIME_SET;
113 }
114 
115 /**
116     Gets status in status area
117 */
118 string incGetStatus() {
119     return status;
120 }
121 
122 /**
123     Updates the status system
124 */
125 void incStatusUpdate() {
126     if (statusTime > 0) {
127         statusTime -= igGetIO().DeltaTime;
128 
129         if (statusTime <= 0) {
130             status = null;
131         }
132     }
133 }