forked from SolarLune/masterplan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.go
1944 lines (1503 loc) · 55.3 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"fmt"
"math"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/gen2brain/raylib-go/raymath"
"github.com/ncruces/zenity"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"github.com/chonla/roman-number-go"
"github.com/hako/durafmt"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
"github.com/faiface/beep/wav"
rl "github.com/gen2brain/raylib-go/raylib"
)
const (
TASK_TYPE_BOOLEAN = iota
TASK_TYPE_PROGRESSION
TASK_TYPE_NOTE
TASK_TYPE_IMAGE
TASK_TYPE_SOUND
TASK_TYPE_TIMER
TASK_TYPE_LINE
)
const (
TASK_NOT_DUE = iota
TASK_DUE_FUTURE
TASK_DUE_TODAY
TASK_DUE_LATE
)
type Task struct {
Rect rl.Rectangle
Board *Board
Position rl.Vector2
Open bool
Selected bool
MinSize rl.Vector2
TaskType *Spinner
Description *Textbox
CreationTime time.Time
CompletionTime time.Time
DeadlineCheckbox *Checkbox
DeadlineDaySpinner *NumberSpinner
DeadlineMonthSpinner *Spinner
DeadlineYearSpinner *NumberSpinner
TimerSecondSpinner *NumberSpinner
TimerMinuteSpinner *NumberSpinner
TimerValue float32
TimerRunning bool
TimerName *Textbox
CompletionCheckbox *Checkbox
CompletionProgressionCurrent *NumberSpinner
CompletionProgressionMax *NumberSpinner
Image rl.Texture2D
GifAnimation *GifAnimation
SoundControl *beep.Ctrl
SoundStream beep.StreamSeekCloser
SoundComplete bool
FilePathTextbox *Textbox
PrevFilePath string
ImageDisplaySize rl.Vector2
Resizeable bool
Resizing bool
Dragging bool
MouseDragStart rl.Vector2
TaskDragStart rl.Vector2
OriginalIndentation int
NumberingPrefix []int
ID int
PostOpenDelay int
PercentageComplete float32
Visible bool
LineEndings []*Task
LineBase *Task
LineBezier *Checkbox
// ArrowPointingToTask *Task
TaskAbove *Task
TaskBelow *Task
TaskRight *Task
TaskLeft *Task
RestOfStack []*Task
SubTasks []*Task
GridPositions []Position
Valid bool
}
func NewTask(board *Board) *Task {
months := []string{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
postX := float32(180)
task := &Task{
Rect: rl.Rectangle{0, 0, 16, 16},
Board: board,
TaskType: NewSpinner(postX, 32, 192, 24, "Check Box", "Progression", "Note", "Image", "Sound", "Timer", "Line"),
Description: NewTextbox(postX, 64, 512, 64),
CompletionCheckbox: NewCheckbox(postX, 96, 32, 32),
CompletionProgressionCurrent: NewNumberSpinner(postX, 96, 128, 40),
CompletionProgressionMax: NewNumberSpinner(postX+80, 96, 128, 40),
NumberingPrefix: []int{-1},
ID: board.Project.FirstFreeID(),
FilePathTextbox: NewTextbox(postX, 64, 512, 16),
DeadlineCheckbox: NewCheckbox(postX, 112, 32, 32),
DeadlineMonthSpinner: NewSpinner(postX+40, 128, 160, 40, months...),
DeadlineDaySpinner: NewNumberSpinner(postX+100, 80, 160, 40),
DeadlineYearSpinner: NewNumberSpinner(postX+240, 128, 160, 40),
TimerMinuteSpinner: NewNumberSpinner(postX, 0, 160, 40),
TimerSecondSpinner: NewNumberSpinner(postX, 0, 160, 40),
TimerName: NewTextbox(postX, 64, 512, 16),
LineEndings: []*Task{},
LineBezier: NewCheckbox(postX, 64, 32, 32),
GridPositions: []Position{},
Valid: true,
}
task.CreationTime = time.Now()
task.CompletionProgressionCurrent.Textbox.MaxCharactersPerLine = 19
task.CompletionProgressionCurrent.Textbox.AllowNewlines = false
task.CompletionProgressionCurrent.Minimum = 0
task.CompletionProgressionMax.Minimum = 0
task.CompletionProgressionMax.Textbox.MaxCharactersPerLine = task.CompletionProgressionCurrent.Textbox.MaxCharactersPerLine
task.CompletionProgressionMax.Textbox.AllowNewlines = false
task.MinSize = rl.Vector2{task.Rect.Width, task.Rect.Height}
task.Description.AllowNewlines = true
task.FilePathTextbox.AllowNewlines = false
task.DeadlineDaySpinner.Minimum = 1
task.DeadlineDaySpinner.Maximum = 31
task.DeadlineDaySpinner.Loop = true
task.DeadlineDaySpinner.Rect.X = task.DeadlineMonthSpinner.Rect.X + task.DeadlineMonthSpinner.Rect.Width + 8
task.DeadlineYearSpinner.Rect.X = task.DeadlineDaySpinner.Rect.X + task.DeadlineDaySpinner.Rect.Width + 8
task.TimerSecondSpinner.Minimum = 0
task.TimerSecondSpinner.Maximum = 59
task.TimerMinuteSpinner.Minimum = 0
return task
}
func (task *Task) Clone() *Task {
copyData := *task // By de-referencing and then making another reference, we should be essentially copying the struct
desc := *copyData.Description
copyData.Description = &desc
tt := *copyData.TaskType
copyData.TaskType = &tt
cc := *copyData.CompletionCheckbox
copyData.CompletionCheckbox = &cc
cpc := *copyData.CompletionProgressionCurrent
copyData.CompletionProgressionCurrent = &cpc
cpm := *copyData.CompletionProgressionMax
copyData.CompletionProgressionMax = &cpm
cPath := *copyData.FilePathTextbox
copyData.FilePathTextbox = &cPath
timerSec := *copyData.TimerSecondSpinner
copyData.TimerSecondSpinner = &timerSec
timerMinute := *copyData.TimerMinuteSpinner
copyData.TimerMinuteSpinner = &timerMinute
timerName := *copyData.TimerName
copyData.TimerName = &timerName
dlc := *copyData.DeadlineCheckbox
copyData.DeadlineCheckbox = &dlc
dds := *copyData.DeadlineDaySpinner
copyData.DeadlineDaySpinner = &dds
dms := *copyData.DeadlineMonthSpinner
copyData.DeadlineMonthSpinner = &dms
dys := *copyData.DeadlineYearSpinner
copyData.DeadlineYearSpinner = &dys
bl := *copyData.LineBezier
copyData.LineBezier = &bl
if task.LineBase != nil {
copyData.LineBase = task.LineBase
copyData.LineBase.LineEndings = append(copyData.LineBase.LineEndings, ©Data)
} else if len(task.ValidLineEndings()) > 0 {
copyData.LineEndings = []*Task{}
for _, end := range task.ValidLineEndings() {
newEnding := copyData.CreateLineEnding()
newEnding.Position = end.Position
newEnding.Board.ReorderTasks()
}
}
for _, ending := range copyData.LineEndings {
ending.Selected = true
ending.Position.Y += float32(ending.Board.Project.GridSize)
}
copyData.TimerRunning = false // We don't want to clone the timer running
copyData.TimerValue = 0
copyData.PrevFilePath = ""
copyData.GifAnimation = nil
copyData.SoundControl = nil
copyData.SoundStream = nil
copyData.ID = copyData.Board.Project.FirstFreeID()
copyData.ReceiveMessage(MessageTaskClose, nil) // We do this to recreate the resources for the Task, if necessary.
return ©Data
}
// Serialize returns the Task's changeable properties in the form of a complete JSON object in a string.
func (task *Task) Serialize() string {
jsonData := "{}"
jsonData, _ = sjson.Set(jsonData, `BoardIndex`, task.Board.Index())
jsonData, _ = sjson.Set(jsonData, `Position\.X`, task.Position.X)
jsonData, _ = sjson.Set(jsonData, `Position\.Y`, task.Position.Y)
jsonData, _ = sjson.Set(jsonData, `ImageDisplaySize\.X`, task.ImageDisplaySize.X)
jsonData, _ = sjson.Set(jsonData, `ImageDisplaySize\.Y`, task.ImageDisplaySize.Y)
jsonData, _ = sjson.Set(jsonData, `Checkbox\.Checked`, task.CompletionCheckbox.Checked)
jsonData, _ = sjson.Set(jsonData, `Progression\.Current`, task.CompletionProgressionCurrent.GetNumber())
jsonData, _ = sjson.Set(jsonData, `Progression\.Max`, task.CompletionProgressionMax.GetNumber())
jsonData, _ = sjson.Set(jsonData, `Description`, task.Description.Text())
jsonData, _ = sjson.Set(jsonData, `FilePath`, task.FilePathTextbox.Text())
jsonData, _ = sjson.Set(jsonData, `Selected`, task.Selected)
jsonData, _ = sjson.Set(jsonData, `TaskType\.CurrentChoice`, task.TaskType.CurrentChoice)
if task.Board.Project.SaveSoundsPlaying.Checked {
jsonData, _ = sjson.Set(jsonData, `SoundPaused`, task.SoundControl != nil && task.SoundControl.Paused)
}
if task.DeadlineCheckbox.Checked {
jsonData, _ = sjson.Set(jsonData, `DeadlineDaySpinner\.Number`, task.DeadlineDaySpinner.GetNumber())
jsonData, _ = sjson.Set(jsonData, `DeadlineMonthSpinner\.CurrentChoice`, task.DeadlineMonthSpinner.CurrentChoice)
jsonData, _ = sjson.Set(jsonData, `DeadlineYearSpinner\.Number`, task.DeadlineYearSpinner.GetNumber())
}
if task.TaskType.CurrentChoice == TASK_TYPE_TIMER {
jsonData, _ = sjson.Set(jsonData, `TimerSecondSpinner\.Number`, task.TimerSecondSpinner.GetNumber())
jsonData, _ = sjson.Set(jsonData, `TimerMinuteSpinner\.Number`, task.TimerMinuteSpinner.GetNumber())
jsonData, _ = sjson.Set(jsonData, `TimerName\.Text`, task.TimerName.Text())
}
jsonData, _ = sjson.Set(jsonData, `CreationTime`, task.CreationTime.Format(`Jan 2 2006 15:04:05`))
if !task.CompletionTime.IsZero() {
jsonData, _ = sjson.Set(jsonData, `CompletionTime`, task.CompletionTime.Format(`Jan 2 2006 15:04:05`))
}
if task.TaskType.CurrentChoice == TASK_TYPE_LINE {
// We want to set this in all cases, not just if it's a Line with valid line ending Task pointers;
// that way it serializes consistently regardless of how many line endings it has.
jsonData, _ = sjson.Set(jsonData, `BezierLines`, task.LineBezier.Checked)
if lineEndings := task.ValidLineEndings(); len(lineEndings) > 0 {
lineEndingPositions := []float32{}
for _, ending := range task.ValidLineEndings() {
if ending.Valid {
lineEndingPositions = append(lineEndingPositions, ending.Position.X, ending.Position.Y)
}
}
jsonData, _ = sjson.Set(jsonData, `LineEndings`, lineEndingPositions)
}
}
return jsonData
}
// Serializable returns if Tasks are able to be serialized properly. Only line endings aren't properly serializeable
func (task *Task) Serializable() bool {
return task.TaskType.CurrentChoice != TASK_TYPE_LINE || task.LineBase == nil
}
// Deserialize applies the JSON data provided to the Task, effectively "loading" it from that state. Previously,
// this was done via a map[string]interface{} which was loaded using a Golang JSON decoder, but it seems like it's
// significantly faster to use gjson and sjson to get and set JSON directly from a string, and for undo and redo,
// it seems to be easier to serialize and deserialize using a string (same as saving and loading) than altering
// the functions to work (as e.g. loading numbers from JSON gives float64s, but passing the map[string]interface{} directly from
// deserialization to serialization contains values that may be other discrete number types).
func (task *Task) Deserialize(jsonData string) {
// JSON encodes all numbers as 64-bit floats, so this saves us some visual ugliness.
getFloat := func(name string) float32 {
return float32(gjson.Get(jsonData, name).Float())
}
getInt := func(name string) int {
return int(gjson.Get(jsonData, name).Int())
}
getBool := func(name string) bool {
return gjson.Get(jsonData, name).Bool()
}
getString := func(name string) string {
return gjson.Get(jsonData, name).String()
}
hasData := func(name string) bool {
return gjson.Get(jsonData, name).Exists()
}
task.Position.X = getFloat(`Position\.X`)
task.Position.Y = getFloat(`Position\.Y`)
task.Rect.X = task.Position.X
task.Rect.Y = task.Position.Y
task.ImageDisplaySize.X = getFloat(`ImageDisplaySize\.X`)
task.ImageDisplaySize.Y = getFloat(`ImageDisplaySize\.Y`)
task.CompletionCheckbox.Checked = getBool(`Checkbox\.Checked`)
task.CompletionProgressionCurrent.SetNumber(getInt(`Progression\.Current`))
task.CompletionProgressionMax.SetNumber(getInt(`Progression\.Max`))
task.Description.SetText(getString(`Description`))
task.FilePathTextbox.SetText(getString(`FilePath`))
task.Selected = getBool(`Selected`)
task.TaskType.CurrentChoice = getInt(`TaskType\.CurrentChoice`)
if hasData(`DeadlineDaySpinner\.Number`) {
task.DeadlineCheckbox.Checked = true
task.DeadlineDaySpinner.SetNumber(getInt(`DeadlineDaySpinner\.Number`))
task.DeadlineMonthSpinner.CurrentChoice = getInt(`DeadlineMonthSpinner\.CurrentChoice`)
task.DeadlineYearSpinner.SetNumber(getInt(`DeadlineYearSpinner\.Number`))
}
if hasData(`TimerSecondSpinner\.Number`) {
task.TimerSecondSpinner.SetNumber(getInt(`TimerSecondSpinner\.Number`))
task.TimerMinuteSpinner.SetNumber(getInt(`TimerMinuteSpinner\.Number`))
task.TimerName.SetText(getString(`TimerName\.Text`))
}
creationTime, err := time.Parse(`Jan 2 2006 15:04:05`, getString(`CreationTime`))
if err == nil {
task.CreationTime = creationTime
}
if hasData(`CompletionTime`) {
// Wouldn't be strange to not have a completion for incomplete Tasks.
ctString := getString(`CompletionTime`)
completionTime, err := time.Parse(`Jan 2 2006 15:04:05`, ctString)
if err == nil {
task.CompletionTime = completionTime
}
}
if hasData(`BezierLines`) {
task.LineBezier.Checked = getBool(`BezierLines`)
}
if hasData(`LineEndings`) {
endPositions := gjson.Get(jsonData, `LineEndings`).Array()
for i := 0; i < len(endPositions); i += 2 {
ending := task.CreateLineEnding()
ending.Position.X = float32(endPositions[i].Float())
ending.Position.Y = float32(endPositions[i+1].Float())
ending.Rect.X = ending.Position.X
ending.Rect.Y = ending.Position.Y
}
}
// We do this to update the task after loading all of the information.
task.LoadResource(false)
if task.SoundControl != nil {
task.SoundControl.Paused = true
if gjson.Get(jsonData, `SoundPaused`).Exists() {
task.SoundControl.Paused = getBool(`SoundPaused`)
}
}
}
func (task *Task) Update() {
task.PostOpenDelay++
if task.SoundComplete {
// We want to lock and unlock the speaker as little as possible, and only when manipulating streams or controls.
speaker.Lock()
task.SoundComplete = false
task.SoundControl.Paused = true
task.SoundStream.Seek(0)
speaker.Unlock()
speaker.Play(beep.Seq(task.SoundControl, beep.Callback(task.OnSoundCompletion)))
speaker.Lock()
above := task.TaskAbove
if task.TaskBelow != nil && task.TaskBelow.TaskType.CurrentChoice == TASK_TYPE_SOUND && task.TaskBelow.SoundControl != nil {
task.SoundControl.Paused = true
task.TaskBelow.SoundControl.Paused = false
} else if above != nil {
for above.TaskAbove != nil && above.TaskAbove.SoundControl != nil && above.TaskType.CurrentChoice == TASK_TYPE_SOUND {
above = above.TaskAbove
}
if above != nil {
task.SoundControl.Paused = true
above.SoundControl.Paused = false
}
} else {
task.SoundControl.Paused = false
}
speaker.Unlock()
}
if task.Selected && task.Dragging && !task.Resizing {
delta := raymath.Vector2Subtract(GetWorldMousePosition(), task.MouseDragStart)
task.Position = raymath.Vector2Add(task.TaskDragStart, delta)
task.Rect.X = task.Position.X
task.Rect.Y = task.Position.Y
}
if task.Dragging && rl.IsMouseButtonReleased(rl.MouseLeftButton) {
task.Board.Project.SendMessage(MessageDropped, nil)
task.Board.Project.ReorderTasks()
}
if !task.Dragging || task.Resizing {
if math.Abs(float64(task.Rect.X-task.Position.X)) <= 1 {
task.Rect.X = task.Position.X
}
if math.Abs(float64(task.Rect.Y-task.Position.Y)) <= 1 {
task.Rect.Y = task.Position.Y
}
}
task.Rect.X += (task.Position.X - task.Rect.X) * 0.2
task.Rect.Y += (task.Position.Y - task.Rect.Y) * 0.2
task.Visible = true
scrW := float32(rl.GetScreenWidth()) / camera.Zoom
scrH := float32(rl.GetScreenHeight()) / camera.Zoom
// Slight optimization
cameraRect := rl.Rectangle{camera.Target.X - (scrW / 2), camera.Target.Y - (scrH / 2), scrW, scrH}
if task.Board.Project.FullyInitialized {
if task.Complete() && task.CompletionTime.IsZero() {
task.CompletionTime = time.Now()
} else if !task.Complete() {
task.CompletionTime = time.Time{}
}
if !rl.CheckCollisionRecs(task.Rect, cameraRect) {
task.Visible = false
}
}
if task.TaskType.CurrentChoice == TASK_TYPE_TIMER {
if task.TimerRunning {
countdownMax := float32(task.TimerSecondSpinner.GetNumber() + (task.TimerMinuteSpinner.GetNumber() * 60))
if countdownMax <= 0 {
task.TimerRunning = false
} else {
if task.TimerValue >= countdownMax {
task.TimerValue = countdownMax
task.TimerRunning = false
task.TimerValue = 0
task.Board.Project.Log("Timer [%s] elapsed.", task.TimerName.Text())
f, err := os.Open(filepath.Join("assets", "alarm.wav"))
if err == nil {
stream, _, _ := wav.Decode(f)
fn := func() {
stream.Close()
}
speaker.Play(beep.Seq(stream, beep.Callback(fn)))
}
if task.TaskBelow != nil && task.TaskBelow.TaskType.CurrentChoice == TASK_TYPE_TIMER {
task.TaskBelow.ToggleTimer()
}
} else {
task.TimerValue += rl.GetFrameTime()
}
}
}
}
}
func (task *Task) Draw() {
if task.Board.Project.BracketSubtasks.Checked && len(task.SubTasks) > 0 {
endingTask := task.SubTasks[len(task.SubTasks)-1]
for len(endingTask.SubTasks) != 0 {
endingTask = endingTask.SubTasks[len(endingTask.SubTasks)-1]
}
ep := endingTask.Position
ep.Y += endingTask.Rect.Height
gh := float32(task.Board.Project.GridSize / 2)
lines := []rl.Vector2{
{task.Position.X, task.Position.Y + gh},
{task.Position.X - gh, task.Position.Y + gh},
{task.Position.X - gh, ep.Y - gh},
{ep.X, ep.Y - gh},
}
lineColor := getThemeColor(GUI_INSIDE)
ts := []*Task{}
ts = append(ts, task.SubTasks...)
ts = append(ts, task)
for _, t := range ts {
if t.Selected {
lineColor = getThemeColor(GUI_OUTLINE_HIGHLIGHTED)
break
}
}
for i := range lines {
if i == len(lines)-1 {
break
}
rl.DrawLineEx(lines[i], lines[i+1], 1, lineColor)
}
// rl.DrawLineEx(task.Position, ep, 1, rl.White)
}
if !task.Visible {
return
}
name := task.Description.Text()
extendedText := false
if task.TaskType.CurrentChoice == TASK_TYPE_IMAGE {
_, filename := filepath.Split(task.FilePathTextbox.Text())
name = filename
task.Resizeable = true
} else if task.TaskType.CurrentChoice == TASK_TYPE_SOUND {
_, filename := filepath.Split(task.FilePathTextbox.Text())
name = filename
} else if task.TaskType.CurrentChoice == TASK_TYPE_BOOLEAN || task.TaskType.CurrentChoice == TASK_TYPE_PROGRESSION {
// Notes don't get just the first line written on the task in the overview.
cut := strings.Index(name, "\n")
if cut >= 0 {
if task.Board.Project.ShowIcons.Checked {
extendedText = true
}
name = name[:cut]
}
task.Resizeable = false
} else if task.TaskType.CurrentChoice == TASK_TYPE_TIMER {
minutes := int(task.TimerValue / 60)
seconds := int(task.TimerValue) % 60
timeString := fmt.Sprintf("%02d:%02d", minutes, seconds)
maxTimeString := fmt.Sprintf("%02d:%02d", task.TimerMinuteSpinner.GetNumber(), task.TimerSecondSpinner.GetNumber())
name = task.TimerName.Text() + " : " + timeString + " / " + maxTimeString
}
if len(task.SubTasks) > 0 && task.Completable() {
currentFinished := 0
for _, child := range task.SubTasks {
if child.Complete() {
currentFinished++
}
}
name = fmt.Sprintf("%s (%d / %d)", name, currentFinished, len(task.SubTasks))
} else if task.TaskType.CurrentChoice == TASK_TYPE_PROGRESSION {
name = fmt.Sprintf("%s (%d / %d)", name, task.CompletionProgressionCurrent.GetNumber(), task.CompletionProgressionMax.GetNumber())
}
sequenceType := task.Board.Project.NumberingSequence.CurrentChoice
if sequenceType != NUMBERING_SEQUENCE_OFF && task.NumberingPrefix[0] != -1 && task.Completable() {
n := ""
for i, value := range task.NumberingPrefix {
if !task.Board.Project.NumberTopLevel.Checked && i == 0 {
continue
}
romanNumber := roman.NewRoman().ToRoman(value)
switch sequenceType {
case NUMBERING_SEQUENCE_NUMBER:
n += fmt.Sprintf("%d.", value)
case NUMBERING_SEQUENCE_NUMBER_DASH:
if i == len(task.NumberingPrefix)-1 {
n += fmt.Sprintf("%d)", value)
} else {
n += fmt.Sprintf("%d-", value)
}
case NUMBERING_SEQUENCE_BULLET:
n += "o"
case NUMBERING_SEQUENCE_ROMAN:
n += fmt.Sprintf("%s.", romanNumber)
}
}
name = fmt.Sprintf("%s %s", n, name)
}
invalidImage := task.Image.ID == 0 && task.GifAnimation == nil
if !invalidImage && task.TaskType.CurrentChoice == TASK_TYPE_IMAGE {
name = ""
}
if !task.Complete() && task.DeadlineCheckbox.Checked {
// If there's a deadline, let's tell you how long you have
deadlineDuration := task.CalculateDeadlineDuration()
deadlineDuration += time.Hour * 24
if deadlineDuration.Hours() > 24 {
duration, _ := durafmt.ParseString(deadlineDuration.String())
duration.LimitFirstN(1)
name += " | Due in " + duration.String()
} else if deadlineDuration.Hours() >= 0 {
name += " | Due today!"
} else {
name += " | Overdue!"
}
}
taskDisplaySize := rl.MeasureTextEx(font, name, fontSize, spacing)
// Lock the sizes of the task to a grid
// All tasks except for images have an icon at the left
if task.Board.Project.ShowIcons.Checked && (task.TaskType.CurrentChoice != TASK_TYPE_IMAGE || invalidImage) {
taskDisplaySize.X += 16
}
if extendedText && task.Board.Project.ShowIcons.Checked {
taskDisplaySize.X += 16
}
if task.TaskType.CurrentChoice == TASK_TYPE_TIMER || task.TaskType.CurrentChoice == TASK_TYPE_SOUND {
taskDisplaySize.X += 32
}
taskDisplaySize.Y, _ = TextHeight(name, false) // Custom spacing to better deal with custom fonts
taskDisplaySize.X = float32((math.Ceil(float64((taskDisplaySize.X + 4) / float32(task.Board.Project.GridSize))))) * float32(task.Board.Project.GridSize)
taskDisplaySize.Y = float32((math.Ceil(float64((taskDisplaySize.Y) / float32(task.Board.Project.GridSize))))) * float32(task.Board.Project.GridSize)
if task.TaskType.CurrentChoice == TASK_TYPE_LINE {
taskDisplaySize.X = 16
}
if task.Rect.Width != taskDisplaySize.X || task.Rect.Height != taskDisplaySize.Y {
task.Rect.Width = taskDisplaySize.X
task.Rect.Height = taskDisplaySize.Y
// We need to update the Task's position list because it changes here
task.Board.RemoveTaskFromGrid(task, task.GridPositions)
task.GridPositions = task.Board.AddTaskToGrid(task)
}
if task.Image.ID != 0 && task.TaskType.CurrentChoice == TASK_TYPE_IMAGE {
if task.Rect.Width < task.ImageDisplaySize.X {
task.Rect.Width = task.ImageDisplaySize.X
}
if task.Rect.Height < task.ImageDisplaySize.Y {
task.Rect.Height = task.ImageDisplaySize.Y
}
}
if task.Rect.Width < task.MinSize.X {
task.Rect.Width = task.MinSize.X
}
if task.Rect.Height < task.MinSize.Y {
task.Rect.Height = task.MinSize.Y
}
color := getThemeColor(GUI_INSIDE)
if task.Complete() && task.TaskType.CurrentChoice != TASK_TYPE_PROGRESSION && len(task.SubTasks) == 0 {
color = getThemeColor(GUI_INSIDE_HIGHLIGHTED)
}
if task.TaskType.CurrentChoice == TASK_TYPE_NOTE {
color = getThemeColor(GUI_NOTE_COLOR)
}
outlineColor := getThemeColor(GUI_OUTLINE)
if task.Selected {
outlineColor = getThemeColor(GUI_OUTLINE_HIGHLIGHTED)
}
// Moved this to a function because it's used for the inside and outside, and the
// progress bar for progression-based Tasks.
applyGlow := func(color rl.Color) rl.Color {
glowVariance := float64(10)
if task.Selected {
glowVariance = 80
}
glow := uint8(math.Sin(float64((rl.GetTime()*math.Pi*2-(float32(task.ID)*0.1))))*(glowVariance/2) + (glowVariance / 2))
if color.R >= glow {
color.R -= glow
} else {
color.R = 0
}
if color.G >= glow {
color.G -= glow
} else {
color.G = 0
}
if color.B >= glow {
color.B -= glow
} else {
color.B = 0
}
return color
}
if task.Completable() || task.Selected {
color = applyGlow(color)
outlineColor = applyGlow(outlineColor)
}
perc := float32(0)
if len(task.SubTasks) > 0 && task.Completable() {
totalComplete := 0
for _, child := range task.SubTasks {
if child.Complete() {
totalComplete++
}
}
perc = float32(totalComplete) / float32(len(task.SubTasks))
} else if task.TaskType.CurrentChoice == TASK_TYPE_PROGRESSION {
cnum := task.CompletionProgressionCurrent.GetNumber()
mnum := task.CompletionProgressionMax.GetNumber()
if mnum < cnum {
task.CompletionProgressionMax.SetNumber(cnum)
mnum = cnum
}
if mnum != 0 {
perc = float32(cnum) / float32(mnum)
}
} else if task.TaskType.CurrentChoice == TASK_TYPE_SOUND && task.SoundStream != nil {
pos := task.SoundStream.Position()
len := task.SoundStream.Len()
perc = float32(pos) / float32(len)
} else if task.TaskType.CurrentChoice == TASK_TYPE_TIMER {
countdownMax := float32(task.TimerSecondSpinner.GetNumber() + (task.TimerMinuteSpinner.GetNumber() * 60))
// If countdownMax == 0, then task.TimerValue / countdownMax can equal a NaN, which breaks drawing the
// filling rectangle.
if countdownMax > 0 {
perc = task.TimerValue / countdownMax
}
}
if perc > 1 {
perc = 1
}
task.PercentageComplete += (perc - task.PercentageComplete) * 0.1
// Raising these "margins" because sounds can be longer, and so 3 seconds into a 5 minute song might would be 1%, or 0.01.
if task.PercentageComplete < 0.0001 {
task.PercentageComplete = 0
} else if task.PercentageComplete >= 0.9999 {
task.PercentageComplete = 1
}
rl.DrawRectangleRec(task.Rect, color)
if task.Due() == TASK_DUE_TODAY {
src := rl.Rectangle{208 + rl.GetTime()*30, 0, task.Rect.Width, task.Rect.Height}
dst := task.Rect
rl.DrawTexturePro(task.Board.Project.Patterns, src, dst, rl.Vector2{}, 0, getThemeColor(GUI_INSIDE_HIGHLIGHTED))
} else if task.Due() == TASK_DUE_LATE {
src := rl.Rectangle{208 + rl.GetTime()*120, 16, task.Rect.Width, task.Rect.Height}
dst := task.Rect
rl.DrawTexturePro(task.Board.Project.Patterns, src, dst, rl.Vector2{}, 0, getThemeColor(GUI_INSIDE_HIGHLIGHTED))
}
if task.PercentageComplete != 0 {
rect := task.Rect
rect.Width *= task.PercentageComplete
rl.DrawRectangleRec(rect, applyGlow(getThemeColor(GUI_INSIDE_HIGHLIGHTED)))
}
if task.TaskType.CurrentChoice == TASK_TYPE_IMAGE {
if task.GifAnimation != nil {
task.Image = task.GifAnimation.GetTexture()
task.GifAnimation.Update(task.Board.Project.GetFrameTime())
}
if task.Image.ID != 0 {
src := rl.Rectangle{0, 0, float32(task.Image.Width), float32(task.Image.Height)}
dst := task.Rect
dst.Width = task.ImageDisplaySize.X
dst.Height = task.ImageDisplaySize.Y
rl.SetTextureFilter(task.Image, rl.FilterAnisotropic4x)
rl.DrawTexturePro(task.Image, src, dst, rl.Vector2{}, 0, rl.White)
if task.Resizeable && task.Selected {
rec := task.Rect
rec.Width = 8
rec.Height = 8
if task.Board.Project.ZoomLevel <= 1 {
rec.Width *= 2
rec.Height *= 2
}
rec.X += task.Rect.Width - rec.Width
rec.Y += task.Rect.Height - rec.Height
rl.DrawRectangleRec(rec, getThemeColor(GUI_INSIDE))
rl.DrawRectangleLinesEx(rec, 1, getThemeColor(GUI_FONT_COLOR))
if rl.IsMouseButtonPressed(rl.MouseLeftButton) && rl.CheckCollisionPointRec(GetWorldMousePosition(), rec) {
task.Resizing = true
task.Board.Project.ResizingImage = true
task.Board.Project.SendMessage(MessageDropped, nil)
} else if rl.IsMouseButtonReleased(rl.MouseLeftButton) {
task.Resizing = false
task.Board.Project.ResizingImage = false
}
if task.Resizing {
endPoint := GetWorldMousePosition()
task.ImageDisplaySize.X = endPoint.X - task.Rect.X
task.ImageDisplaySize.Y = endPoint.Y - task.Rect.Y
if task.ImageDisplaySize.X < task.MinSize.X {
task.ImageDisplaySize.X = task.MinSize.X
}
if task.ImageDisplaySize.Y < task.MinSize.Y {
task.ImageDisplaySize.Y = task.MinSize.Y
}
if !rl.IsKeyDown(rl.KeyLeftAlt) && !rl.IsKeyDown(rl.KeyRightAlt) {
asr := float32(task.Image.Height) / float32(task.Image.Width)
task.ImageDisplaySize.Y = task.ImageDisplaySize.X * asr
}
if !rl.IsKeyDown(rl.KeyLeftShift) && !rl.IsKeyDown(rl.KeyRightShift) {
task.ImageDisplaySize = task.Board.Project.LockPositionToGrid(task.ImageDisplaySize)
}
}
rec.X = task.Rect.X
rec.Y = task.Rect.Y
rl.DrawRectangleRec(rec, getThemeColor(GUI_INSIDE))
rl.DrawRectangleLinesEx(rec, 1, getThemeColor(GUI_FONT_COLOR))
if rl.IsMouseButtonPressed(rl.MouseLeftButton) && rl.CheckCollisionPointRec(GetWorldMousePosition(), rec) {
task.ImageDisplaySize.X = float32(task.Image.Width)
task.ImageDisplaySize.Y = float32(task.Image.Height)
}
}
}
}
if task.Board.Project.OutlineTasks.Checked {
rl.DrawRectangleLinesEx(task.Rect, 1, outlineColor)
}
if (task.TaskType.CurrentChoice != TASK_TYPE_IMAGE || invalidImage) && task.TaskType.CurrentChoice != TASK_TYPE_LINE {
textPos := rl.Vector2{task.Rect.X + 2, task.Rect.Y + 2}
if task.Board.Project.ShowIcons.Checked {
textPos.X += 16
}
if task.TaskType.CurrentChoice == TASK_TYPE_TIMER || task.TaskType.CurrentChoice == TASK_TYPE_SOUND {
textPos.X += 32
}
DrawText(textPos, name)
}
controlPos := float32(0)
if task.Board.Project.ShowIcons.Checked {
controlPos = 16
iconColor := getThemeColor(GUI_FONT_COLOR)
iconSrc := rl.Rectangle{16, 0, 16, 16}
rotation := float32(0)
iconSrcIconPositions := map[int][]float32{
TASK_TYPE_BOOLEAN: {0, 0},
TASK_TYPE_PROGRESSION: {32, 0},
TASK_TYPE_NOTE: {64, 0},
TASK_TYPE_SOUND: {80, 0},
TASK_TYPE_IMAGE: {96, 0},
TASK_TYPE_TIMER: {0, 16},
TASK_TYPE_LINE: {64, 16},
}
if task.TaskType.CurrentChoice == TASK_TYPE_SOUND {
if task.SoundStream == nil || task.SoundControl.Paused {
iconColor = getThemeColor(GUI_OUTLINE)
}
}
iconSrc.X = iconSrcIconPositions[task.TaskType.CurrentChoice][0]
iconSrc.Y = iconSrcIconPositions[task.TaskType.CurrentChoice][1]
if len(task.SubTasks) > 0 && task.Completable() {
iconSrc.X = 128 // Hardcoding this because I'm an idiot
iconSrc.Y = 16
}
// task.ArrowPointingToTask = nil