-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.jai
2830 lines (2468 loc) · 81.2 KB
/
ui.jai
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
main :: () {
modify_string(*subwindow_fenstring, "startpos");
init_xiangqi_tables();
init_textures :: () #expand {
success := simp.texture_load_from_file(*sprites, "data/xiangqi.png");
assert(success);
success = simp.texture_load_from_file(*setting_sprite, "data/settings.png");
assert(success);
}
xiangqi_board_click :: (x: float, y: float, mouse_event: bool, key_pressed: int, left_or_right: bool) {
if subwindow_newgame.open {
// outside of the board.
xiangqi_mouse = XiangqiMouse.Idle;
xiangqi_highlight = .Idle;
xiangqi_highlight_select = -1;
from_index = 0xFF;
return; // do not process board inputs when subwindows are open.
}
center_x: float = 0;
center_y: float = 0;
board_margin: float = 0;
if off_center == true && right_panel_display != 2 {
center_x = height / 2.0;
center_y = center_x;
board_margin = center_x * .925;
} else {
center_x = width / 2.0;
center_y = height / 2.0;
board_margin = min(center_x, center_y) * .925;
}
sq := board_margin / 5.0;
hsq := sq / 2.0; //hsq = half square
board_x := center_x - sq * 4.5;
board_y := center_y - sq * 5.0;
end_x := board_x + sq * 9.0;
end_y := board_y + sq * 10.0;
if mouse_event && x >= board_x && x <= end_x && y >= board_y && y <= end_y then {
row := cast(u8) ((x - board_x) / sq);
col := cast(u8) ((y - board_y) / sq);
if orient then {
row = 8 - row;
col = 9 - col;
}
if left_or_right == true {
xiangqi_highlight = .Idle;
clear_arrows();
if xiangqi_mouse == {
case XiangqiMouse.Idle;
if key_pressed == 1 then {
sq := row_col_to_index(row, col);
piece := xiangqi.board[sq];
if piece != 0 then {
xiangqi_mouse = XiangqiMouse.Drag;
from_index = cast(u8) row_col_to_index(row, col);
}
}
case XiangqiMouse.Drag;
to_index := row_col_to_index(row, col);
if from_index != to_index then {
piece := xiangqi.board[from_index];
capture := xiangqi.board[row + col * 9];
move := Move32.{cast(u8)from_index, cast(u8)to_index, capture, piece};
gui_make_move(move, false);
xiangqi_mouse = XiangqiMouse.Idle;
from_index = 0xFF;
} else {
xiangqi_mouse = XiangqiMouse.Select;
}
case XiangqiMouse.Select;
to_index := row_col_to_index(row, col);
piece := xiangqi.board[from_index];
capture := xiangqi.board[row + col * 9];
move := Move32.{cast(u8)from_index, cast(u8)to_index, capture, piece};
gui_make_move(move, true);
xiangqi_mouse = XiangqiMouse.Idle;
from_index = 0xFF;
}
} else {
position := row_col_to_index(row, col);
if xiangqi_highlight == {
case .Idle;
if key_pressed == 1 {
xiangqi_highlight_select = position;
xiangqi_highlight = .Select;
}
case .Select;
if key_pressed == 0 {
if xiangqi_highlight_select != position {
// add an arrow...
arrow: Arrow;
arrow.from = cast(u8) xiangqi_highlight_select;
arrow.to = cast(u8) position;
add_arrow(arrow);
}
xiangqi_highlight = .Idle;
}
}
}
} else {
// outside of the board.
xiangqi_mouse = XiangqiMouse.Idle;
xiangqi_highlight = .Idle;
xiangqi_highlight_select = -1;
from_index = 0xFF;
}
}
render_ui :: () #expand {
text_theme := *default_overall_theme.text_input_theme;
label_theme := *default_overall_theme.label_theme;
k := clamp(height * 0.05, 15.0, 45.0);
r := get_rect(5.0, height - 2.5 - k, k, k);
if button(r, "", texture=*setting_sprite) then {
if subwindow_newgame.open == false {
display_settings = !display_settings;
}
}
if display_settings {
r.x += 20.0;
r.y -= 5.0;
r.w = 8.5 * k;
color := Vector4.{.05, .05, .05, .9};
simp.set_shader_for_color(true);
num_buttons :: 15.0;
simp.immediate_quad(17.0, r.y, r.x + r.w + 7.0, r.y - num_buttons*k - 5.0*num_buttons - 3.0, color);
r.y -= k + 5.0;
if button(r, get_ui_str().new_game) then {
subwindow_newgame.open = true;
}
r.y -= k + 5.0;
dropdown(r, get_ui_str().right_panel, *right_panel_display);
r.y -= k + 5.0;
if button(r, get_ui_str().undo) then {
gui_undo_move();
}
r.y -= k + 5.0;
if button(r, get_ui_str().redo) then {
gui_redo_move();
}
r.y -= k + 5.0;
dropdown(r, get_ui_str().piece_style, cast(*s32)*piece_style);
r.y -= k + 5.0;
dropdown(r, get_ui_str().board_style, cast(*s32)*board_style);
r.y -= k + 5.0;
if base_checkbox(r, get_ui_str().orientation, !orient) then {
orient = !orient;
}
r.y -= k + 5.0;
if base_checkbox(r, get_ui_str().blindfold, blindfold) then {
blindfold = !blindfold;
}
r.y -= k + 5.0;
if base_checkbox(r, get_ui_str().show_moves, show_moves) then {
show_moves = !show_moves;
}
r.y -= k + 5.0;
if base_checkbox(r, get_ui_str().audio, sound) then {
sound = !sound;
}
r.y -= k + 5.0;
if base_checkbox(r, get_ui_str().highlight_move, highlight_move) then {
highlight_move = !highlight_move;
}
r.y -= k + 5.0;
if base_checkbox(r, get_ui_str().animations, mov_anim_state != MovAnim.Off) then {
if mov_anim_state == MovAnim.Off
mov_anim_state = MovAnim.Idle;
else
mov_anim_state = MovAnim.Off;
}
slider_theme := *default_overall_theme.slider_theme;
r.y -= k + 5.0;
slider(r, *mov_anim_complete, 0.00, 1.0, 0.01, slider_theme, prefix=get_ui_str().animation_speed);
r.y -= k + 5.0;
if base_checkbox(r, get_ui_str().logging, engine_logging) {
engine_logging = !engine_logging;
}
r.y -= k + 5.0;
dropdown(r, string.["English", "简体中文"], cast(*s32)*language);
r.y -= k + 5.0;
}
if status_string {
r = get_rect(5.0, 5.0, 15*k, k);
label(r, status_string, label_theme);
}
}
render_xiangqiboard :: (mouse_x: float, mouse_y: float, mouse_event: bool) #expand {
simp.set_shader_for_color(true);
center_x: float = 0;
center_y: float = 0;
board_margin: float = 0;
if off_center == true && right_panel_display != 2 {
center_x = height / 2.0;
center_y = center_x;
board_margin = center_x * .925;
} else {
center_x = width / 2.0;
center_y = height / 2.0;
board_margin = min(center_x, center_y) * .925;
}
sq := board_margin / 5.0;
hsq := sq / 2.0;
board_x := center_x - sq * 4.5;
board_y := center_y - sq * 5.0;
color: Vector4 = ---;
if board_style == {
case .White;
color = Vector4.{.999, .958, .859, 1.};
case .Mocha;
color = Vector4.{.707, .605, .484, 1.};
case .Bamboo;
color = Vector4.{.824, .6875, .00703, 1.};
case .Rust;
color = Vector4.{.7456, .35764, .0, 1.};
}
simp.immediate_quad(board_x, board_y, board_x + 9.0 * sq, board_y + 10.0 * sq, color);
board_x += hsq;
board_y += hsq;
color = Vector4.{0,0,0,1};
// draw the board grid.
simp.set_shader_for_color(true);
gridx := board_x;
gridy := board_y;
for 0..9 {
line_width := 1;
if it == 0 || it == 9 {
line_width = 2;
}
simp.immediate_quad(gridx, gridy, gridx + 8*sq, gridy + line_width, color);
gridy += sq;
}
gridx = board_x;
gridy = board_y;
for 0..8 {
if it == 0 || it == 8 then {
simp.immediate_quad(gridx, gridy, gridx + 2, gridy + 9*sq, color);
} else {
simp.immediate_quad(gridx, gridy, gridx + 1, gridy + 4*sq, color);
simp.immediate_quad(gridx, gridy + 5*sq, gridx + 1, gridy + 9*sq, color);
}
gridx += sq;
}
right_crosses :: (x: float, y: float, gridx: float, gridy: float, sq: float, color: Vector4) {
v0, v1, v2, v3: Vector2;
v0 = Vector2.{gridx + (x + 0.250)*sq, gridy + (y - 0.050)*sq - 1};
v1 = Vector2.{gridx + (x + 0.050)*sq, gridy + (y - 0.050)*sq - 1};
v2 = Vector2.{gridx + (x + 0.050)*sq, gridy + (y - 0.050)*sq};
v3 = Vector2.{gridx + (x + 0.250)*sq, gridy + (y - 0.050)*sq};
simp.immediate_quad(v0, v1, v2, v3, color);
v0 = Vector2.{gridx + (x + 0.050)*sq+1, gridy + (y - 0.050)*sq};
v1 = Vector2.{gridx + (x + 0.050)*sq+1, gridy + (y - 0.250)*sq};
v2 = Vector2.{gridx + (x + 0.050)*sq, gridy + (y - 0.250)*sq};
v3 = Vector2.{gridx + (x + 0.050)*sq, gridy + (y - 0.050)*sq};
simp.immediate_quad(v0, v1, v2, v3, color);
v0 = Vector2.{gridx + (x + 0.250)*sq, gridy + (y + 0.050)*sq + 1};
v1 = Vector2.{gridx + (x + 0.250)*sq, gridy + (y + 0.050)*sq};
v2 = Vector2.{gridx + (x + 0.050)*sq, gridy + (y + 0.050)*sq};
v3 = Vector2.{gridx + (x + 0.050)*sq, gridy + (y + 0.050)*sq + 1};
simp.immediate_quad(v0, v1, v2, v3, color);
v0 = Vector2.{gridx + (x + 0.050)*sq + 1, gridy + (y + 0.050)*sq};
v1 = Vector2.{gridx + (x + 0.050)*sq, gridy + (y + 0.050)*sq};
v2 = Vector2.{gridx + (x + 0.050)*sq, gridy + (y + 0.250)*sq};
v3 = Vector2.{gridx + (x + 0.050)*sq + 1, gridy + (y + 0.250)*sq};
simp.immediate_quad(v0, v1, v2, v3, color);
}
left_crosses :: (x: float, y: float, gridx: float, gridy: float, sq: float, color: Vector4) {
v0, v1, v2, v3: Vector2;
v0 = Vector2.{gridx + (x - 0.050)*sq+1, gridy + (y - 0.050)*sq};
v1 = Vector2.{gridx + (x - 0.050)*sq+1, gridy + (y - 0.250)*sq};
v2 = Vector2.{gridx + (x - 0.050)*sq, gridy + (y - 0.250)*sq};
v3 = Vector2.{gridx + (x - 0.050)*sq, gridy + (y - 0.050)*sq};
simp.immediate_quad(v0, v1, v2, v3, color);
v0 = Vector2.{gridx + (x - 0.250)*sq, gridy + (y - 0.050)*sq - 1};
v1 = Vector2.{gridx + (x - 0.250)*sq, gridy + (y - 0.050)*sq};
v2 = Vector2.{gridx + (x - 0.050)*sq, gridy + (y - 0.050)*sq};
v3 = Vector2.{gridx + (x - 0.050)*sq, gridy + (y - 0.050)*sq - 1};
simp.immediate_quad(v0, v1, v2, v3, color);
// other one....
v0 = Vector2.{gridx + (x - 0.250)*sq, gridy + (y + 0.050)*sq + 1};
v1 = Vector2.{gridx + (x - 0.250)*sq, gridy + (y + 0.050)*sq};
v2 = Vector2.{gridx + (x - 0.050)*sq, gridy + (y + 0.050)*sq};
v3 = Vector2.{gridx + (x - 0.050)*sq, gridy + (y + 0.050)*sq + 1};
simp.immediate_quad(v0, v1, v2, v3, color);
v0 = Vector2.{gridx + (x - 0.050)*sq + 1, gridy + (y + 0.050)*sq};
v1 = Vector2.{gridx + (x - 0.050)*sq, gridy + (y + 0.050)*sq};
v2 = Vector2.{gridx + (x - 0.050)*sq, gridy + (y + 0.250)*sq};
v3 = Vector2.{gridx + (x - 0.050)*sq + 1, gridy + (y + 0.250)*sq};
simp.immediate_quad(v0, v1, v2, v3, color);
}
crosses :: (x: float, y: float, gridx: float, gridy: float, sq: float, color: Vector4) {
left_crosses(x, y, gridx, gridy, sq, color);
right_crosses(x, y, gridx, gridy, sq, color);
}
pawn_crosses :: (y: float, gridx: float, gridy: float, sq: float, color: Vector4) {
right_crosses(0,y, gridx, gridy, sq, color);
crosses (2,y, gridx, gridy, sq, color);
crosses (4,y, gridx, gridy, sq, color);
crosses (6,y, gridx, gridy, sq, color);
left_crosses (8,y, gridx, gridy, sq, color);
}
gridx = board_x;
// draw the palace diagonals.
v0 := Vector2.{gridx + 3*sq, gridy};
v1 := Vector2.{gridx + 5*sq, gridy + 2*sq};
v2 := Vector2.{gridx + 5*sq - 1, gridy + 2*sq};
v3 := Vector2.{gridx + 3*sq, gridy + 1};
simp.immediate_quad(v0, v1, v2, v3, color);
v0 = Vector2.{gridx + 3*sq, gridy + 2*sq};
v1 = Vector2.{gridx + 5*sq, gridy};
v2 = Vector2.{gridx + 5*sq + 1, gridy};
v3 = Vector2.{gridx + 3*sq, gridy + 2*sq + 1};
simp.immediate_quad(v0, v1, v2, v3, color);
v0 = Vector2.{gridx + 3*sq, gridy + 7*sq};
v1 = Vector2.{gridx + 5*sq, gridy + 9*sq};
v2 = Vector2.{gridx + 5*sq - 1, gridy + 9*sq};
v3 = Vector2.{gridx + 3*sq, gridy + 7*sq + 1};
simp.immediate_quad(v0, v1, v2, v3, color);
v0 = Vector2.{gridx + 3*sq, gridy + 9*sq};
v1 = Vector2.{gridx + 5*sq, gridy + 7*sq};
v2 = Vector2.{gridx + 5*sq + 1, gridy + 7*sq};
v3 = Vector2.{gridx + 3*sq, gridy + 9*sq + 1};
simp.immediate_quad(v0, v1, v2, v3, color);
crosses(1,2, gridx, gridy, sq, color);
crosses(7,2, gridx, gridy, sq, color);
crosses(1,7, gridx, gridy, sq, color);
crosses(7,7, gridx, gridy, sq, color);
pawn_crosses(3, gridx, gridy, sq, color);
pawn_crosses(6, gridx, gridy, sq, color);
simp.set_shader_for_images(*sprites);
color = Vector4.{1., 1., 1., 1.};
board_x -= hsq;
board_y -= hsq;
if show_moves == true then {
for move: moves {
if move.from != from_index then {
continue;
}
to := move.to;
render_possible_move(board_x, board_y, sq, to, arrow_color);
}
}
if highlight_move == true then {
if prev_from != 0xFF then {
render_possible_move(board_x, board_y, sq, prev_from, arrow_color);
}
if prev_to != 0xFF then {
render_possible_move(board_x, board_y, sq, prev_to, arrow_color);
}
}
// draw all the pieces.
if blindfold == false {
for piece_array, turn: xiangqi.pieces {
gen := piece_array.king.array[0];
gen = orient_piece(gen);
render_piece(board_x, board_y, sq, gen, 0.0, turn, color);
for adv: piece_array.adv {
adv = orient_piece(adv);
render_piece(board_x, board_y, sq, adv, 1.0, turn, color);
}
for ele: piece_array.ele {
ele = orient_piece(ele);
render_piece(board_x, board_y, sq, ele, 3.0, turn, color);
}
for kni: piece_array.kni {
kni = orient_piece(kni);
render_piece(board_x, board_y, sq, kni, 2.0, turn, color);
}
for rook: piece_array.rook {
rook = orient_piece(rook);
render_piece(board_x, board_y, sq, rook, 4.0, turn, color);
}
for can: piece_array.cannon {
can = orient_piece(can);
render_piece(board_x, board_y, sq, can, 5.0, turn, color);
}
for sol: piece_array.pawn {
sol = orient_piece(sol);
render_piece(board_x, board_y, sq, sol, 6.0, turn, color);
}
}
// dragged piece.
if xiangqi_mouse == XiangqiMouse.Drag then {
ren_idx :: float.[0.0,1.0,3.0,2.0,4.0,5.0,6.0];
row, col := row_col_of(from_index);
index := xiangqi.board[row + col * 9];
turn := (index & 2) >> 1;
index >>= 2;
sprite_index := ren_idx[index];
render_piece_drag(mouse_x, mouse_y, hsq, sq, sprite_index, turn, color);
}
if mov_anim_state == MovAnim.Play && moves_made_count then {
ren_idx :: float.[0.0,1.0,3.0,2.0,4.0,5.0,6.0];
mov := xiangqi.hashes.info[moves_made_count-1].move;
from, to := orient_piece(mov.from), orient_piece(mov.to);
piece := mov.piece >> 2;
from_x, from_y := row_col_of(from);
to_x, to_y := row_col_of(to);
from_vector: Vector2;
from_vector.x = board_x + sq * from_x;
from_vector.y = board_y + sq * from_y;
to_vector: Vector2;
to_vector.x = board_x + sq * to_x;
to_vector.y = board_y + sq * to_y;
ratio := mov_anim_time / mov_anim_complete;
mov_anim_time += dt;
if mov_anim_time >= mov_anim_complete {
mov_anim_time = 0;
mov_anim_state = MovAnim.Idle;
}
p0 := lerp(from_vector, to_vector, ratio);
sprite_index := ren_idx[piece];
render_piece_drag(p0.x + hsq, p0.y + hsq, hsq, sq, sprite_index, xiangqi.turn ^ 1, color);
}
}
simp.set_shader_for_color(true);
for index: 0..num_arrows-1 {
arrow: Arrow = arrows[index];
render_arrow(arrow, board_x, board_y, sq);
}
if highlight_move == true then {
arrow: Arrow;
arrow.from = cast(u8) prev_from;
arrow.to = cast(u8) prev_to;
render_arrow(arrow, board_x, board_y, sq);
}
render_pv_lines :: (pv_info: [..] PV_Info, board_x: float, board_y: float, sq: float) {
for pv_line, identifier: pv_info {
if pv_line.show == false then continue;
for arrow: pv_line.pv {
render_arrow(arrow, board_x, board_y, sq);
}
}
}
render_pv_lines(red_pv_lines, board_x, board_y, sq);
render_pv_lines(black_pv_lines, board_x, board_y, sq);
}
display_moves :: (height: float, board_margin: float, width: float, k: float) #expand {
r := get_rect(height + 3.0, height - board_margin - 3.0 - k, width - board_margin*2.5 - height - 6.0, k);
label_r := r;
label_r.x += k;
hsq := k / 2.0;
get_player_info :: (index: int, time_manager: *TimeManager, time_ms: float) -> string {
name := "";
if index == -1 {
name = "";
} else {
name = engines_array[index].name;
}
if time_manager.time_control == {
case .None;
return name;
case .Clock;
time_ms /= 1000.0;
mins := cast(int) (time_ms / 60.0);
secs := cast(int) fmod_cycling(time_ms, 60.0);
if mins == 0 && secs <= 3 then {
return tprint("%1 -- 0:0%2", name, formatFloat(fmod_cycling(time_ms, 60.0) , trailing_width=3));
} else {
return tprint("%1 -- %2:%3", name, mins, formatInt(secs, minimum_digits=2));
}
case .Time_per_move;
if index == -1 then {
return name;
}
secs_per_move := time_manager.seconds_per_move;
return tprint("%1 -- %2 secs per move", name, formatFloat(secs_per_move, trailing_width=2));
case .Depth;
if index == -1 then {
return name;
}
return tprint("%1 -- Depth %2", name, time_manager.depth);
}
return "";
}
red_display_string := get_player_info(red_ai, *r_time_manager, r_time_ms);
black_display_string := get_player_info(black_ai, *b_time_manager, b_time_ms);
bottom_string: string;
top_string: string;
if orient == true {
// red on bottom, black on top.
bottom_string = red_display_string;
top_string = black_display_string;
} else {
// black on bottom, red on top.
bottom_string = black_display_string;
top_string = red_display_string;
}
color := Vector4.{1, 1, 1, 1.};
label_theme := *default_overall_theme.label_theme;
label(label_r, bottom_string, label_theme);
label_r.y -= k + 5.0;
label(label_r, top_string, label_theme);
label_r.y -= k + 5.0;
label_r.x -= k;
label_r.w *= .5;
if base_checkbox(label_r, get_ui_str().red_pv, show_r_eval) {
if red_ai == -1 then
show_r_eval = false;
else
show_r_eval = !show_r_eval;
}
label_r.x += label_r.w + 5.0;
if base_checkbox(label_r, get_ui_str().black_pv, show_b_eval) {
if black_ai == -1 then
show_b_eval = false;
else
show_b_eval = !show_b_eval;
}
label_r.x -= label_r.w / 2.0;
if xiangqi.turn == xx (orient) {
color.w = .15;
}
simp.set_shader_for_images(*sprites);
render_piece_drag(r.x + hsq, r.y + hsq, hsq, k, 0.0, xx !orient, color);
color.w = 1;
if xiangqi.turn == xx (!orient) {
color.w = .15;
}
r.y -= k + 5.0;
render_piece_drag(r.x + hsq, r.y + hsq, hsq, k, 0.0, xx orient, color);
color.w = 1;
region: *Scrollable_Region_State;
inside: Rect;
r.y -= k + 5.0;
if red_ai != -1 && show_r_eval == true {
r.y -= k * 6;
r.h = k * 6;
region, inside = begin_scrollable_region(r);
s := inside;
s.y = s.y + s.h - k;
s.h = k;
s.y += scroll_value_red;
for *pv_line, identifier: red_pv_lines {
if base_checkbox(s, show_pv_line(pv_line), pv_line.show, identifier=identifier) {
pv_line.show = !pv_line.show;
}
s.y -= floor(k*1.1 + 0.5);
}
end_scrollable_region(region, s.x + s.w, s.y, *scroll_value_red);
r.y -= k * .5;
}
if black_ai != -1 && show_b_eval == true {
r.y -= k * 6;
r.h = k * 6;
region, inside = begin_scrollable_region(r);
s := inside;
s.y = s.y + s.h - k;
s.h = k;
s.y += scroll_value_black;
for *pv_line, identifier: black_pv_lines {
if base_checkbox(s, show_pv_line(pv_line), pv_line.show, identifier=identifier) {
pv_line.show = !pv_line.show;
}
s.y -= floor(k*1.1 + 0.5);
}
end_scrollable_region(region, s.x + s.w, s.y, *scroll_value_black);
r.y -= k * .5;
}
val := r.y;
r.y = board_margin + 3.0;
r.h = val - board_margin;
if r.h < k {
return; // do not render if too small...
}
region, inside = begin_scrollable_region(r);
s := inside;
s.y = s.y + s.h - k;
s.h = k;
s.y += move_list_scroll_value;
// TODO: this code assumes that red goes first, which is not always the case.
// what if we were in a custom position loaded from a fen, and black goes first?
num_rect := s;
num_rect.w = k;
red_move_rect := num_rect;
red_move_rect.x += k * 1.25;
red_move_rect.w = s.w / 2.0;
black_move_rect := num_rect;
black_move_rect.x += k;
black_move_rect.x += (s.w-2*k) / 2.0;
black_move_rect.w = s.w / 2.0;
// TODO: (DANIEL, Feb 24, 2023) this is not xiangqi standard notation. It's just UCCI moves.
// maybe support for standard notation later?
to_notation :: (move: Move32) -> string {
PIECE_VALUE :: u8.[#char "K", #char "A", #char "E", #char "N", #char "R", #char "C", #char "P"];
buf := talloc_string(5);
row_from, col_from := row_col_of(move.from);
row_to, col_to := row_col_of(move.to);
buf[0] = PIECE_VALUE[move.piece >> 2];
buf[1] = cast(u8)(row_from + #char "a");
buf[2] = xx (col_from + #char "0");
buf[3] = xx (row_to + #char "a");
buf[4] = xx (col_to + #char "0");
return buf;
// general, advisor, elephant, horse, rook, cannon, soldier
}
// NOTE: (Daniel) Feb 25, 2023: label(r, "label", do_scissors=true) causes a compiler crash.
index := 0;
numbering := 0;
while index < moves_made_count {
numbering += 1;
label(num_rect, tprint("%.", numbering), label_theme);
mov_red := xiangqi.hashes.info[index].move;
label(red_move_rect, to_notation(mov_red), label_theme);
index += 1;
if index >= moves_made_count then
break;
mov_black := xiangqi.hashes.info[index].move;
index += 1;
label(black_move_rect, to_notation(mov_black), label_theme);
num_rect.y -= floor(k*1.1 + 0.5);
red_move_rect.y -= floor(k*1.1 + 0.5);
black_move_rect.y -= floor(k*1.1 + 0.5);
s.y -= floor(k*1.1 + 0.5);
}
end_scrollable_region(region, s.x + s.w, s.y, *move_list_scroll_value);
}
render_right_panel :: () {
if off_center == false || right_panel_display == 2 {
return;
}
simp.set_shader_for_color(true);
color := Vector4.{.1, .1, .1, .85};
board_margin := height * ((1 - .925) *.5);
k := clamp(height * 0.05, 35.0, 95.0);
simp.immediate_quad(height, board_margin, width - board_margin*2.5, height - board_margin, color);
if right_panel_display == 0 {
display_moves(height, board_margin, width, k);
} else if right_panel_display == 1 {
engines_panel(height, board_margin, width, k);
}
}
// init_zobrist_tables(); todo: add back in later...
random_seed(current_time_monotonic().low);
path = copy_string(path_strip_filename(get_path_of_running_executable()));
set_working_directory(path);
set_default_theme(default_theme_procs[Default_Themes.Grayscale]());
overall_theme_modify();
win := create_window(cast(int) width, cast(int) height, "Xiangqi");
simp.set_render_target(win);
font := simp.get_font_at_size("data", "SourceHanSansHWSC-Regular.otf", 16);
ui_set_font(font);
xiangqi_startpos(*xiangqi);
reset(*moves);
array_reserve(*red_pv_lines, 200);
array_reserve(*black_pv_lines, 200);
legal_moves(*xiangqi, *moves);
init_sound();
init_textures();
ui_init();
ctrl_pressed := 0;
load_engines_at_startup();
subwindow_newgame.rect = get_rect(50,50,580,580);
subwindow_newgame.draw = draw_subwindow_newgame;
subwindow_newgame.title_text = get_ui_str().new_game;
subwindow_newgame.open = false;
defer {
for *engine: engines_array {
close_engine(engine);
}
}
while render_loop := true {
dt = looptime_end - looptime_begin;
looptime_begin = looptime_end;
update_window_events();
for get_window_resizes() {
if it.window == win {
width = cast(float) it.width;
height = cast(float) it.height;
// a bit of a supersition, use a golden section to determine
// where to place the widgets.
if cast(float)width > cast(float) height * SQRT2 {
off_center = true;
} else {
off_center = false;
}
simp.update_window(win);
}
}
mouse_x, mouse_y := get_mouse_pointer_position(win, true);
mouse_event := false;
key_pressed := 0;
for e: events_this_frame {
if e.type == .QUIT then {
break render_loop;
}
getrect_handle_event(e);
if e.type == .KEYBOARD then {
if e.key_code == .MOUSE_BUTTON_LEFT {
key_pressed = e.key_pressed;
mouse_event = true;
xiangqi_board_click(cast(float) mouse_x, cast(float) mouse_y, mouse_event, key_pressed, true);
} else if e.key_code == .MOUSE_BUTTON_RIGHT {
key_pressed = e.key_pressed;
mouse_event = true;
xiangqi_board_click(cast(float) mouse_x, cast(float) mouse_y, mouse_event, key_pressed, false);
} else if e.key_code == .CTRL {
ctrl_pressed = e.key_pressed;
} else if e.key_pressed == 1 && e.key_code == #char "Z" && ctrl_pressed {
gui_undo_move();
} else if e.key_pressed == 1 && e.key_code == #char "Y" && ctrl_pressed {
gui_redo_move();
}
}
}
// loop through engines and do engine logic.
if moves.count then {
for *engine, index: engines_array {
protocol := ifx engine.protocol == .uci then "uci" else "ucci";
update_ucci(engine, index, protocol);
}
}
simp.clear_render_target(.15, .15, .15, 1.0);
render_xiangqiboard(cast(float)mouse_x, cast(float)mouse_y, mouse_event);
time := seconds_since_init();
ui_per_frame_update(win, cast(s32) width, cast(s32) height, time);
// The Subwindow starts open, but pressing the Close button can close it.
if subwindow_newgame.open {
add_subwindow(*subwindow_newgame);
}
render_right_panel();
render_ui();
// sound effects.
Sound.update();
draw_popups(); // This draws Subwindows as well.
clock_update();
sleep_milliseconds(20);
simp.swap_buffers(win);
reset_temporary_storage();
looptime_end = cast(float)seconds_since_init();
}
}
#scope_file
render_arrow :: (arrow: Arrow, corner_x: float, corner_y: float, square: float) {
arrow_point :: (index: int, corner_x: float, corner_y: float, square: float, half_square: float) -> Vector2 {
index = orient_piece(cast(u8)index);
row, col := row_col_of(cast(u8)index);
return Vector2.{corner_x + square * row + half_square, corner_y + square * col + half_square};
}
hsquare := square * .5;
from_pt := arrow_point(arrow.from, corner_x, corner_y, square, hsquare);
to\ _pt := arrow_point(arrow.to\, corner_x, corner_y, square, hsquare);
dx := to_pt.x - from_pt.x;
dy := to_pt.y - from_pt.y;
hypotenuse := sqrt(dx*dx + dy*dy);
// ... I suck at trigonometry...haven't done it in a while... so this...
ct := (dx / hypotenuse);
st := (dy / hypotenuse);
size := square * .12;
w := Vector2.{ct, st} * size;
h := Vector2.{st, -ct} * size;
qsquare := square * .70;
from_pt.x += ct * square * .425;
from_pt.y += st * square * .425;
to_pt.x -= ct * qsquare;
to_pt.y -= st * qsquare;
p0 := from_pt - w - h;
p1 := to_pt + w - h;
p2 := to_pt + w + h;
p3 := from_pt - w + h;
simp.immediate_quad(p0, p1, p2, p3, arrow_color);
from_pt = to_pt;
to_pt.x += ct * qsquare * .6;
to_pt.y += st * qsquare * .6;
w = Vector2.{ct, st} * size;
h = Vector2.{st, -ct} * size;
p0 = from_pt + w - h * 2.25;
p1 = to_pt + w;
p2 = from_pt + w + h * 2.25;
simp.immediate_quad(p0, p1, p1, p2, arrow_color);
}
engines_panel :: (height: float, board_margin: float, width: float, k: float) #expand {
r := get_rect(height + 3.0, height - board_margin - 3.0 - k, width - board_margin*2.5 - height - 6.0, k);
engine_names: [..] string;
engine_names.allocator = temp;
for *engine: engines_array {
array_add(*engine_names, engine.name);
}
array_add(*engine_names, get_ui_str().add_engine);
dropdown(r, engine_names, *engine_selection);
slider_theme := *default_overall_theme.slider_theme;
label_theme := *default_overall_theme.label_theme;
text_theme: *Text_Input_Theme = *default_overall_theme.text_input_theme;
XVAL :: 2.5;
r.y -= k + 10.0;
if engine_selection == engine_names.count - 1 {
label(r, get_ui_str().name, label_theme);
r.x += k * XVAL;
r.w -= k * XVAL;
act1, _, name_state := text_input(r, engine_name_input, text_theme);
if act1 & .TEXT_MODIFIED {
engine_name_input = get_result(name_state);
}
r.x -= k * XVAL;
r.w += k * XVAL;
r.y -= k + 10.0;
label(r, get_ui_str().path, label_theme);
r.x += k * XVAL;
r.w -= k * XVAL;
act2, __, engine_path_state := text_input(r, engine_path_input, text_theme);
if act2 & .TEXT_MODIFIED {
engine_path_input = get_result(engine_path_state);
}
r.x -= k * XVAL;
r.w += k * XVAL;
r.y -= k + 10.0;
label(r, get_ui_str().protocol, label_theme);
r.x += k * XVAL;
r.w -= k * XVAL;
dropdown(r, .["uci", "ucci"], *protocol_choice);
r.y -= k + 10.0;
if button(r, get_ui_str().load_engine) {
path := get_result(engine_path_state);
engine, success := load_engine(path, cast(Protocol)protocol_choice);
if success == true {
name := get_result(name_state);
if name {
// (Daniel - Mar 6, 2023.) Memory leak. String not freed.
// will address issue if we are repeatedly opening and closing engines 100 times.
// constantly, but I don't think this happens often, so won't bother with this
engine.name = copy_string(name);
} else {
engine.name = copy_string(path);
}
array_add(*engines_array, engine);
reset(name_state);
reset(engine_path_state);
}
}
} else {
y := r.y;
engine := *engines_array[engine_selection];
if engine.options.count == 0 {
label(r, get_ui_str().no_options_available, label_theme);
return;
}
r.y = board_margin + 3.0;
if button(r, get_ui_str().cancel) {
for *option: engine.options {
using option;
check_value = check_value_old;
spin_value = spin_value_old;
combo_value = combo_value_old;
string_value = string_value_old;
}
}
protocol_prefix := "";
if engine.protocol == Protocol.uci then {
protocol_prefix = "setoption name";
} else if engine.protocol == Protocol.ucci then {
protocol_prefix = "setoption";
}
protocol_suffix := "";
if engine.protocol == Protocol.uci then {
protocol_suffix = "value ";
} else if engine.protocol == Protocol.ucci then {
protocol_suffix = "";
}
r.y += k * 1.1;
if button(r, get_ui_str().set_option) {
process := *engine.process;
for *option: engine.options {
using option;
if type == {
case .check;