-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.pde
1387 lines (1140 loc) · 31.9 KB
/
Sudoku.pde
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
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import java.util.Comparator;
import java.util.ListIterator;
import org.chocosolver.solver.Model;
import org.chocosolver.solver.variables.IntVar;
import processing.sound.*;
SudokuWidget sudoku;
int[][] board = new int[9][9];
SoundFile backgroundMusic;
GridLayout gridLayout;
// input file picker uses seperate thread
boolean inputFileChosen = false;
// TODO:
// - bug: devision by zero exception when getting mouse events before initial draw
// - performance could be better
// - use board class with callbacks on change
// - solving should throw an exception if it fails to notify user
// - add notifications
String[] solveStrategies = SolveStrategy.getAllStrategies();
Solver solver = new Solver(solveStrategies[0], board);
void setup() {
size(800, 600);
frameRate(30);
surface.setResizable(true);
gridLayout = new GridLayout(this,12,12, 10);
Button resetButton = new Button("Reset");
resetButton.setOnClickPressedListener(new OnClickPressedListener() {
@Override
public void onClickPressed(int x, int y) {
reset();
}
});
gridLayout.addGridElement(resetButton, 0,1,3,1);
Button loadButton = new Button("Load...");
loadButton.setOnClickPressedListener(new OnClickPressedListener() {
@Override
public void onClickPressed(int x, int y) {
selectInput("Import Sudoku file", "importSudoku");
while(!inputFileChosen){
delay(100);
}
inputFileChosen = false;
sudoku.setBoard(board);
}
});
gridLayout.addGridElement(loadButton, 0,2,3,1);
Button saveButton = new Button("Save...");
saveButton.setOnClickPressedListener(new OnClickPressedListener() {
@Override
public void onClickPressed(int x, int y) {
selectOutput("Export Sudoku file", "exportSudoku");
}
});
gridLayout.addGridElement(saveButton, 0,3,3,1);
SelectBox selectBox = new SelectBox("Solver", solveStrategies);
selectBox.setOnOptionSelectedListener(new OnOptionSelectedListener() {
@Override
public void onOptionSelected(int index, String option) {
solver.setCurrentStrategy(option);
println(index, option);
}
} );
gridLayout.addGridElement(selectBox, 0,4,3,1);
final ImageButton playButton = new ImageButton("play-circle");
playButton.setOnClickPressedListener(new OnClickPressedListener() {
@Override
public void onClickPressed(int x, int y) {
if (playButton.getIconName().equals("play-circle")) {
playButton.setIcon("pause-circle");
backgroundMusic.play();
} else {
playButton.setIcon("play-circle");
backgroundMusic.pause();
}
}
});
gridLayout.addGridElement(playButton, 0, 10, 1, 1);
Slider volumeSlider = new Slider(300,100, 0,10,5);
volumeSlider.setOnSliderChangeListener(new OnSliderChangeListener() {
@Override
public void onSliderChange(int value) {
backgroundMusic.amp(value / 10.0f);
}
} );
gridLayout.addGridElement(volumeSlider, 1, 10, 2, 1);
sudoku = new SudokuWidget(board);
gridLayout.addGridElement(sudoku, 4,1,8,9);
Button backButton = new ImageButton("arrow-alt-circle-left");
backButton.setOnClickPressedListener(new OnClickPressedListener() {
@Override
public void onClickPressed(int x, int y) {
sudoku.revertLastMove();
}
});
gridLayout.addGridElement(backButton, 4, 10,1,1);
Button nextButton = new ImageButton("arrow-alt-circle-right");
nextButton.setOnClickPressedListener(new OnClickPressedListener() {
@Override
public void onClickPressed(int x, int y) {
sudoku.redoLastMove();
}
});
gridLayout.addGridElement(nextButton, 5, 10,1,1);
Button solveButton = new Button("Solve");
solveButton.setOnClickPressedListener(new OnClickPressedListener() {
@Override
public void onClickPressed(int x, int y) {
long startTime = System.nanoTime();
solver.solve();
//solve();
long endTime = System.nanoTime();
long duration = (endTime - startTime) / 1000000; //divide by 1000000 to get milliseconds.
println("took " + duration + "ms");
}
});
gridLayout.addGridElement(solveButton, 6,10,2,1);
Button hintButton = new Button("Hint");
hintButton.setOnClickPressedListener(new OnClickPressedListener() {
@Override
public void onClickPressed(int x, int y) {
Coordinate selected = sudoku.getSelectedCoordinate();
if(selected.equals(Coordinate.EmptyCoordinate)){
return;
}
int value = solver.solve(selected.getX(), selected.getY());
board[selected.getY()][selected.getX()] = value;
println(value);
//TODO
}
});
gridLayout.addGridElement(hintButton, 8,10,2,1);
backgroundMusic = new SoundFile(this, "sounds/gui/out2.wav");
backgroundMusic.amp(0.5f);
}
void reset() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
board[i][j] = 0;
}
}
}
void exportSudoku(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
return;
}
println("User selected " + selection.getAbsolutePath());
PrintWriter output = createWriter(selection.getAbsolutePath());
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
output.print(board[i][j] == 0 ? '.' : Character.forDigit(board[i][j],10));
}
}
output.println();
output.flush();
output.close();
}
void importSudoku(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
inputFileChosen = true;
return;
}
println("User selected " + selection.getAbsolutePath());
// import file
String[] lines = loadStrings(selection.getAbsolutePath());
for (String line : lines) {
if (line.length() == 81) {
for (int i = 0; i < 81; i++) {
int indexX = i % 9;
int indexY = floor(i / 9);
char c = line.charAt(i);
if (c == '.') {
board[indexY][indexX] = 0;
} else {
board[indexY][indexX] = Character.getNumericValue(c);
}
}
inputFileChosen = true;
return;
}
}
inputFileChosen = true;
}
void draw() {
background(200, 200, 255);
}
public boolean isValid(int[][] _board, int row, int col, int num) {
//check y
for (int c = 0; c < 9; c++) {
if (_board[row][c] == num) {
return false;
}
}
//check x
for (int r = 0; r < 9; r++) {
if (_board[r][col] == num) {
return false;
}
}
//check minigrid
int startCol = col - (col % 3);
int startRow = row - (row % 3);
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (_board[r + startRow][c + startCol] == num) {
return false;
}
}
}
return true;
}
public boolean validBoardConfiguration(int[][] _board) {
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
if (_board[y][x] != 0) {
int val = _board[y][x];
_board[y][x] = 0;
boolean valid = isValid(_board,y,x,val);
_board[y][x] = val;
if (!valid) {
return false;
}
}
}
}
return true;
}
public enum SolveStrategy
{
BACKTRACK("Backtrack"), CONSTRAINT("Constraint");
private String name;
SolveStrategy(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static SolveStrategy getByName(String name) {
for (SolveStrategy strategy : SolveStrategy.values()) {
if (strategy.getName().equals(name)) {
return strategy;
}
}
return null;
}
public static String[] getAllStrategies() {
SolveStrategy[] values = SolveStrategy.values();
String[] strategies = new String[values.length];
for (int i = 0; i < values.length; i++) {
strategies[i] = values[i].getName();
}
return strategies;
}
}
public class Solver {
SolveStrategy currentStrategy;
int[][] board;
public Solver(String strategy, int[][] board)
{
setCurrentStrategy(strategy);
this.board = board;
}
public void setCurrentStrategy(String solveStrategy) {
this.currentStrategy = SolveStrategy.getByName(solveStrategy);
}
public SolveStrategy getCurrentStrategy() {
return currentStrategy;
}
public boolean solve() {
return solve(this.board);
}
private boolean solve(int[][] _board){
if(!validBoardConfiguration(_board)){
return false;
}
switch(currentStrategy) {
case BACKTRACK:
println("solving via backtrack");
return solve_backtrack(_board);
case CONSTRAINT:
println("solving via constraint");
return solve_constraint(_board);
default:
break;
}
return false;
}
public int solve(int x, int y){
int[][] copy = cloneArray(board);
boolean success = solve(copy);
if(success){
return copy[y][x];
} else {
return 0;
}
}
private boolean solve_backtrack(int[][] _board) {
int row = - 1;
int col = - 1;
boolean isEmpty = true;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (_board[i][j] == 0)
{
row = i;
col = j;
// We still have some remaining
// missing values in Sudoku
isEmpty = false;
break;
}
}
if (!isEmpty) {
break;
}
}
// No empty space left
if (isEmpty)
{
return true;
}
// Else for each row backtrack
for (int num = 1; num <= 9; num++)
{
if (isValid(_board, row, col, num))
{
_board[row][col] = num;
if (solve_backtrack(_board))
{
return true;
}
else
{
// replace it
_board[row][col] = 0;
}
}
}
return false;
}
private boolean solve_constraint(int[][] _board) {
Model model = new Model("Sudoku solver");
int n = 9;
IntVar[][] rows = new IntVar[n][n];
IntVar[][] cols = new IntVar[n][n];
IntVar[][] carres = new IntVar[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (_board[i][j] > 0) {
rows[i][j] = model.intVar(_board[i][j]);
} else {
rows[i][j] = model.intVar("c_" + i + "_" + j, 1, n, false);
}
cols[j][i] = rows[i][j];
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
carres[j + k * 3][i] = rows[k * 3][i + j * 3];
carres[j + k * 3][i + 3] = rows[1 + k * 3][i + j * 3];
carres[j + k * 3][i + 6] = rows[2 + k * 3][i + j * 3];
}
}
}
for (int i = 0; i < n; i++) {
model.allDifferent(rows[i], "AC").post();
model.allDifferent(cols[i], "AC").post();
model.allDifferent(carres[i], "AC").post();
}
model.getSolver().solve();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
_board[i][j] = rows[i][j].getValue();
}
}
return true;
}
private int[][] cloneArray(int[][] src) {
int[][] dst = new int[src.length][src[0].length];
for (int i = 0; i < src.length; i++) {
for (int j = 0; j < src[i].length; j++) {
dst[i][j] = src[i][j];
}
}
return dst;
}
}
public interface OnClickPressedListener {
void onClickPressed(int x, int y);
}
public interface OnClickReleasedListener {
void onClickReleased(int x, int y);
}
public interface OnMouseEnterListener {
void onMouseEnter(int x, int y);
}
public interface OnMouseLeaveListener {
void onMouseLeave(int x, int y);
}
public interface OnMouseMoveListener {
void onMouseMove(int x, int y);
}
public interface OnKeyTypedListener {
void onKeyTyped(char key);
}
public interface OnSliderChangeListener {
void onSliderChange(int value);
}
public interface OnOptionSelectedListener {
void onOptionSelected(int index, String option);
}
public class Move {
Coordinate coordinate;
int valueBefore;
int valueAfter;
public Move(Coordinate coordinate, int valueBefore, int valueAfter) {
this.coordinate = coordinate;
this.valueBefore = valueBefore;
this.valueAfter = valueAfter;
}
public Coordinate getCoordinate() {
return coordinate;
}
public int getValueBefore() {
return valueBefore;
}
public int getValueAfter() {
return valueAfter;
}
}
public class SudokuWidget extends WidgetBase {
int[][] board;
int[][] boardOriginal;
List<Move> moves = new ArrayList<Move>();
int moveIndex = - 1;
int blinkTime;
boolean blinkOn;
Coordinate selectedCoordinate = new Coordinate( - 1, - 1);
Coordinate hoveredCoordinate = new Coordinate( - 1, - 1);
int lastX;
int lastY;
float cellSize;
public SudokuWidget(int[][] board) {
setBoard(board);
}
public void setBoard(int[][] board){
this.board = board;
this.boardOriginal = cloneArray(board);
}
public Coordinate getSelectedCoordinate(){
return selectedCoordinate;
}
public void revertLastMove() {
if (moveIndex >= 0) {
Move lastMove = moves.get(moveIndex);
Coordinate coordinate = lastMove.getCoordinate();
board[coordinate.getY()][coordinate.getX()] = lastMove.getValueBefore();
moveIndex--;
}
}
public void redoLastMove() {
if (moveIndex < moves.size() - 1) {
moveIndex++;
Move lastMove = moves.get(moveIndex);
Coordinate coordinate = lastMove.getCoordinate();
board[coordinate.getY()][coordinate.getX()] = lastMove.getValueAfter();
}
}
private int[][] cloneArray(int[][] src) {
int[][] dst = new int[src.length][src[0].length];
for (int i = 0; i < src.length; i++) {
for (int j = 0; j < src[i].length; j++) {
dst[i][j] = src[i][j];
}
}
return dst;
}
@Override
public void onMouseEnter(int x, int y) {}
@Override
public void onMouseLeave(int x, int y) {
hoveredCoordinate.setXY( - 1, - 1);
}
@Override
public void onMouseMove(int x, int y) {
int xIndex = floor((x - lastX) / cellSize);
int yIndex = floor((y - lastY) / cellSize);
if (xIndex < 0 || yIndex < 0 || xIndex > 8 || yIndex > 8 || boardOriginal[yIndex][xIndex] != 0) {
hoveredCoordinate.setXY( - 1, - 1);
return;
}
hoveredCoordinate.setXY(xIndex, yIndex);
}
@Override
public void onClickPressed(int x, int y) {
int xIndex = floor((x - lastX) / cellSize);
int yIndex = floor((y - lastY) / cellSize);
if (xIndex < 0 || yIndex < 0 || xIndex > 8 || yIndex > 8 || boardOriginal[yIndex][xIndex] != 0) {
selectedCoordinate.setXY( - 1, - 1);
return;
}
selectedCoordinate.setXY(xIndex, yIndex);
blinkTime = millis();
blinkOn = true;
}
@Override
public void onClickReleased(int x, int y) {}
@Override
public void onKeyTyped(char key) {
if (selectedCoordinate.getX() == - 1) {
return;
}
if (key == BACKSPACE) {
int oldVal = board[selectedCoordinate.getY()][selectedCoordinate.getX()];
if (oldVal == 0) {
return;
}
while(moveIndex < moves.size() - 1) {
moves.remove(moves.size() - 1);
}
moves.add(new Move(new Coordinate(selectedCoordinate.getX(), selectedCoordinate.getY()), oldVal, 0));
moveIndex++;
board[selectedCoordinate.getY()][selectedCoordinate.getX()] = 0;
selectedCoordinate.setXY( - 1, - 1);
return;
}
if (key < '1' || key > '9') {
return;
}
int val = Character.getNumericValue(key);
int oldVal = board[selectedCoordinate.getY()][selectedCoordinate.getX()];
if (val == oldVal) {
return;
}
while(moveIndex < moves.size() - 1) {
moves.remove(moves.size() - 1);
}
moves.add(new Move(new Coordinate(selectedCoordinate.getX(), selectedCoordinate.getY()), oldVal, val));
moveIndex++;
board[selectedCoordinate.getY()][selectedCoordinate.getX()] = val;
selectedCoordinate.setXY( - 1, - 1);
}
@Override
public void draw(int _x, int _y, int width, int height) {
// noFill();
// rect(_x,_y,width,height);
lastX = _x;
lastY = _y;
cellSize = min(width, height) / 9f;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
float x = _x + i * cellSize;
float y = _y + j * cellSize;
stroke(150);
int alpha = 180;
// hover gray
if (i == hoveredCoordinate.getX() && j == hoveredCoordinate.getY()) {
fill(160,160,160,alpha);
}
// normal white
else {
fill(255,255,255,alpha);
}
// draw cells
rect(x, y, cellSize, cellSize);
// selected cell - blinking cursor
if (boardOriginal[j][i] == 0 && i == selectedCoordinate.getX() && j == selectedCoordinate.getY()) {
if (blinkOn) {
fill(220,220,220, 150);
stroke(0);
line(x + cellSize * 0.2,y + cellSize * 0.95,x + cellSize * 0.8, y + cellSize * 0.95);
}
if (millis() - 500 > blinkTime) {
blinkTime = millis();
blinkOn = !blinkOn;
}
}
// draw number
if (board[j][i] != 0) {
textSize(cellSize * 0.8);
textAlign(CENTER,CENTER);
// user number is red
if (boardOriginal[j][i] == 0) {
fill(100,0,0);
} else {
fill(50);
}
text("" + board[j][i],x + cellSize / 2,y + cellSize / 2);
}
}
}
noFill();
stroke(0);
strokeWeight(1.2);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
float x = _x + i * cellSize * 3;
float y = _y + j * cellSize * 3;
rect(x , y , cellSize * 3, cellSize * 3);
}
}
strokeWeight(1);
}
}
public class SelectBox extends WidgetBase {
String text;
String[] options;
int selectedOption = 0;
private boolean[] hovered;
private boolean clicked;
int lastX;
int lastY;
int lastWidth;
int lastHeight;
OnOptionSelectedListener onOptionSelectedListener;
public SelectBox(String text, String[] options) {
super(3, 1);
this.hovered = new boolean[options.length + 1];
this.clicked = false;
this.text = text;
this.options = options;
}
public void setOnOptionSelectedListener(OnOptionSelectedListener onOptionSelectedListener) {
this.onOptionSelectedListener = onOptionSelectedListener;
}
@Override
public void onMouseEnter(int x, int y) {}
@Override
public void onMouseLeave(int x, int y) {
Arrays.fill(hovered, false);
clicked = false;
}
@Override
public void onMouseMove(int x, int y) {
Arrays.fill(hovered, false);
int index = (y - lastY) / lastHeight;
hovered[index] = true;
}
@Override
public void onClickPressed(int x, int y) {
hasOverlay = !hasOverlay;
int index = (y - lastY) / lastHeight;
if (index == 0) {
clicked = true;
} else {
int new_index = index - 1;
if (new_index != selectedOption) {
if (onOptionSelectedListener != null) {
selectedOption = new_index;
onOptionSelectedListener.onOptionSelected(selectedOption, options[selectedOption]);
}
}
selectedOption = index - 1;
}
}
@Override
public void onClickReleased(int x, int y) {
clicked = false;
}
@Override
public void onKeyTyped(char key) {}
@Override
public void draw(int x, int y, int width, int height) {
lastX = x;
lastY = y;
lastWidth = width;
lastHeight = height;
overlayHeight = height * options.length;
int heightBase = height;
int col = clicked ? 150 : hovered[0] ? 100 : 50;
//int alpha = 220;
stroke(col);
fill(col);
rect(x,y,width,heightBase);
fill(255);
textAlign(RIGHT, CENTER);
textSize(min(width, heightBase) * 0.3);
text(hasOverlay ? "▲" : "▼",x + width - 10,y + heightBase / 2);
textAlign(CENTER, CENTER);
text(text + " : " + options[selectedOption], x + width / 2, y + heightBase / 2);
textSize(14);
if (hasOverlay) {
int offset = width / this.width / 2;
for (int i = 0; i < options.length; i++) {
noStroke();
col = hovered[i + 1] ? 100 : 50;
fill(col);
rect(x + offset,y + heightBase * (i + 1),width - offset,heightBase);
fill(255);
textAlign(CENTER, CENTER);
textSize(min(width, heightBase) * 0.3);
text(options[i], x + width / 2, y + heightBase * (i + 1) + heightBase / 2);
textSize(14);
}
}
}
}
public class Slider extends WidgetBase {
private int min;
private int max;
private int value;
private boolean hovered = false;
private boolean clicked = false;
int lastWidth;
int lastX;
float lastHandleWidth;
OnSliderChangeListener onSliderChangeListener;
public Slider(int width, int height, int min, int max, int value) {
super(width,height);
this.min = min;
this.max = max;
this.value = value;
}
public void setOnSliderChangeListener(OnSliderChangeListener onSliderChangeListener) {
this.onSliderChangeListener = onSliderChangeListener;
}
@Override
public void onMouseEnter(int x, int y) {
}
@Override
public void onMouseLeave(int x, int y) {
clicked = false;
}
@Override
public void onMouseMove(int x, int y) {
if (clicked) {
int segmentWidth = round(lastWidth / (float)(max - min + 1));
int offset = x - lastX;
int index = floor(offset / segmentWidth);
int new_value = min(max,min + index);
if (value != new_value) {
value = new_value;
if (onSliderChangeListener != null) {
onSliderChangeListener.onSliderChange(value);
}
}
}
}
@Override
public void onClickPressed(int x, int y) {
clicked = true;
float segmentWidth = lastWidth / (float)(max - min + 1);
int offset = x - lastX;
int index = floor(offset / segmentWidth);
int new_value = min(max,min + index);
if (value != new_value) {
value = new_value;
if (onSliderChangeListener != null) {
onSliderChangeListener.onSliderChange(value);
}
}
}
@Override
public void onClickReleased(int x, int y) {
clicked = false;
}
@Override
public void onKeyTyped(char key) {}
@Override
public void draw(int x, int y, int width, int height) {
lastWidth = width;
lastX = x;
int lineHeight = round(height * 0.1);
float handleWidth = width / (float)(max - min + 1);
int handleHeight = lineHeight * 3;
lastHandleWidth = handleWidth;
stroke(50);
noFill();
fill(50);
rect(round(x + handleWidth * 0.5),round(y + height / 2 - lineHeight / 2),width - handleWidth,lineHeight, 10);
stroke(40);
fill(40);
rect(x + value * handleWidth,round(y + height / 2 - handleHeight / 2),handleWidth,handleHeight, 7);
textAlign(CENTER, CENTER);
textSize(min(width, height) * 0.3);
text("" + value, x + width / 2, y + height / 2 + handleHeight);
}
}
public class ImageButton extends Button {
PShape icon;
String iconName;
float ratio;
public ImageButton(String iconName) {
super("");
setIcon(iconName);
}
public void setIcon(String iconName) {
this.iconName = iconName;
this.icon = loadShape("icons/" + iconName + ".svg");
this.icon.disableStyle();
this.ratio = this.icon.getWidth() / (float)this.icon.getHeight();
}
public String getIconName() {
return this.iconName;
}
@Override
public void draw(int x, int y, int width, int height) {
int iconHeight = round(min(width,height) * 0.5);
int iconWidth = round(iconHeight * ratio);
int col = clicked ? 150 : hovered ? 100 : 50;
//int alpha = 220;
stroke(col);
fill(col);
rect(x,y,width,height, 10);
fill(255);
shape(icon, x + width / 2 - iconWidth / 2, y + height / 2 - iconHeight / 2, iconWidth, iconHeight);
}
}
public class Button extends WidgetBase {
String text;
boolean hovered = false;
boolean clicked = false;