23
23
24
24
package com.github.netomi.sudoku.trainer.view
25
25
26
- import javafx.beans.property.IntegerProperty
27
- import javafx.beans.property.SimpleIntegerProperty
28
26
import javafx.collections.FXCollections
29
27
import javafx.collections.ObservableIntegerArray
30
28
import javafx.event.EventHandler
@@ -45,8 +43,7 @@ import com.github.netomi.sudoku.solver.LinkType.WEAK
45
43
import com.github.netomi.sudoku.trainer.Styles
46
44
import com.github.netomi.sudoku.trainer.model.DisplayOptions
47
45
import com.github.netomi.sudoku.trainer.pseudoClass
48
- import javafx.beans.property.BooleanProperty
49
- import javafx.beans.property.SimpleBooleanProperty
46
+ import javafx.beans.property.*
50
47
import javafx.css.PseudoClass
51
48
import javafx.geometry.Insets
52
49
import javafx.scene.layout.*
@@ -56,8 +53,11 @@ import kotlin.math.sign
56
53
/* *
57
54
* The view to display the state of an individual cell within a sudoku grid.
58
55
*/
59
- class CellFragment (private val cell : Cell ) : Fragment()
56
+ class CellFragment (cellArgument : Cell ) : Fragment()
60
57
{
58
+ private val cellProperty: ObjectProperty <Cell > = SimpleObjectProperty (cellArgument)
59
+ var cell: Cell by cellProperty
60
+
61
61
private val valueProperty: IntegerProperty = SimpleIntegerProperty (0 )
62
62
private var value by valueProperty
63
63
@@ -66,12 +66,77 @@ class CellFragment(private val cell: Cell) : Fragment()
66
66
val selectedProperty: BooleanProperty = SimpleBooleanProperty (false )
67
67
var selected by selectedProperty
68
68
69
- private val candidatesPane: GridPane
70
- private val assignedValueLabel: Label
69
+ private var candidatesPane by singleAssign< GridPane >()
70
+ private var assignedValueLabel by singleAssign< Label >()
71
71
// a simple pane to easily indicate the currently focused cell.
72
- private val selectPane: Pane
72
+ private var selectPane by singleAssign<Pane >()
73
+
74
+ override val root = stackpane {
75
+ addClass(Styles .sudokuCell)
76
+
77
+ selectPane = pane {
78
+ useMaxSize = true
79
+ id = Styles .cellSelectPane.name
80
+ }
81
+
82
+ setMinSize(30.0 , 30.0 )
83
+ useMaxSize = true
84
+
85
+ val rows = when (cell.owner.gridSize) {
86
+ 4 -> 2
87
+ 6 -> 2
88
+ 9 -> 3
89
+ else -> kotlin.error(" unexpected grid size ${cell.owner.gridSize} " )
90
+ }
91
+ val cols = cell.owner.gridSize / rows
92
+
93
+ val percentageRow = 100.0 / rows
94
+ val percentageCol = 100.0 / cols
95
+
96
+ candidatesPane = gridpane {
97
+ var possibleValue = 1
98
+ for (i in 0 until rows) {
99
+ for (j in 0 until cols) {
100
+ label {
101
+ useMaxHeight = true
102
+ maxWidth = 36.0
103
+
104
+ id = Styles .cellCandidate.name
105
+ text = possibleValue.toString()
106
+
107
+ gridpaneConstraints {
108
+ margin = Insets (3.0 )
109
+ columnRowIndex(j, i)
110
+ }
111
+ }
112
+ possibleValue++
113
+ }
114
+ }
73
115
74
- override val root = stackpane {}
116
+ for (i in 0 until rows) {
117
+ val row = RowConstraints (10.0 , 20.0 , Double .MAX_VALUE )
118
+ row.vgrow = Priority .ALWAYS
119
+ row.valignment = VPos .CENTER
120
+ row.percentHeight = percentageRow
121
+ rowConstraints.add(row)
122
+ }
123
+
124
+ for (i in 0 until cols) {
125
+ val col = ColumnConstraints (10.0 , 20.0 , Double .MAX_VALUE )
126
+ col.hgrow = Priority .ALWAYS
127
+ col.halignment = HPos .CENTER
128
+ col.percentWidth = percentageCol
129
+ columnConstraints.add(col)
130
+ }
131
+ }
132
+
133
+ assignedValueLabel = label {
134
+ id = Styles .cellValue.name
135
+ isVisible = false
136
+
137
+ textProperty().bind(valueProperty.asString())
138
+ }
139
+ }
75
140
76
141
fun refreshView (conflicts : Array <Conflict >, displayedHint : Hint ? ) {
77
142
assignedValueLabel.apply {
@@ -112,6 +177,12 @@ class CellFragment(private val cell: Cell) : Fragment()
112
177
}
113
178
}
114
179
180
+ fun resetView () {
181
+ selected = false
182
+ value = 0
183
+ possibleValuesProperty.clear()
184
+ }
185
+
115
186
private fun processHint (hint : Hint ? ): Array <CandidateState > {
116
187
val result = Array (cell.owner.gridSize + 1 ) { _ -> CandidateState .NONE }
117
188
@@ -314,74 +385,9 @@ class CellFragment(private val cell: Cell) : Fragment()
314
385
}
315
386
316
387
init {
317
- with (root) {
318
- addClass(Styles .sudokuCell)
319
-
320
- selectPane = pane {
321
- useMaxSize = true
322
- id = Styles .cellSelectPane.name
323
- }
324
-
325
- style + = getBorderStyle(cell)
326
-
327
- setMinSize(30.0 , 30.0 )
328
- useMaxSize = true
329
-
330
- val rows = when (cell.owner.gridSize) {
331
- 4 -> 2
332
- 6 -> 2
333
- 9 -> 3
334
- else -> kotlin.error(" unexpected grid size $cell .owner.gridSize" )
335
- }
336
- val cols = cell.owner.gridSize / rows
337
-
338
- val percentageRow = 100.0 / rows
339
- val percentageCol = 100.0 / cols
340
-
341
- candidatesPane = gridpane {
342
- var possibleValue = 1
343
- for (i in 0 until rows) {
344
- for (j in 0 until cols) {
345
- label {
346
- useMaxHeight = true
347
- maxWidth = 36.0
348
-
349
- id = Styles .cellCandidate.name
350
- text = possibleValue.toString()
351
-
352
- gridpaneConstraints {
353
- margin = Insets (3.0 )
354
- columnRowIndex(j, i)
355
- }
356
- }
357
- possibleValue++
358
- }
359
- }
360
-
361
- for (i in 0 until rows) {
362
- val row = RowConstraints (10.0 , 20.0 , Double .MAX_VALUE )
363
- row.vgrow = Priority .ALWAYS
364
- row.valignment = VPos .CENTER
365
- row.percentHeight = percentageRow
366
- rowConstraints.add(row)
367
- }
368
-
369
- for (i in 0 until cols) {
370
- val col = ColumnConstraints (10.0 , 20.0 , Double .MAX_VALUE )
371
- col.hgrow = Priority .ALWAYS
372
- col.halignment = HPos .CENTER
373
- col.percentWidth = percentageCol
374
- columnConstraints.add(col)
375
- }
376
- }
377
-
378
- assignedValueLabel = label {
379
- id = Styles .cellValue.name
380
- isVisible = false
381
- }
382
- }
383
-
384
- assignedValueLabel.textProperty().bind(valueProperty.asString())
388
+ val updateStyle: (Cell ) -> Unit = { root.style = getBorderStyle(it) }
389
+ cellProperty.onChange { it?.let (updateStyle) }
390
+ updateStyle.invoke(cell)
385
391
386
392
valueProperty.onChange { newValue ->
387
393
candidatesPane.isVisible = newValue == 0
0 commit comments