Skip to contentMethod: isResizable()
      1: /*
2:  * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3:  *
4:  * This file is part of ipspiel24-VierConnects-gui.
5:  *
6:  * ipspiel24-VierConnects-gui is free software: you can redistribute it and/or modify it under
7:  * the terms of the GNU General Public License as published by the Free Software
8:  * Foundation, either version 3 of the License, or (at your option) any later
9:  * version.
10:  *
11:  * ipspiel24-VierConnects-gui is distributed in the hope that it will be useful, but WITHOUT
12:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13:  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14:  * details.
15:  *
16:  * You should have received a copy of the GNU General Public License along with
17:  * ipspiel24-VierConnects-gui. If not, see <http://www.gnu.org/licenses/>.
18:  */
19: package de.fhdw.gaming.ipspiel24.VierConnects.gui.impl;
20: 
21: import de.fhdw.gaming.ipspiel24.VierConnects.core.domain.VierConnectsFieldState;
22: import javafx.beans.property.ObjectProperty;
23: import javafx.beans.property.SimpleObjectProperty;
24: import javafx.beans.value.ObservableValue;
25: import javafx.scene.canvas.Canvas;
26: import javafx.scene.canvas.GraphicsContext;
27: import javafx.scene.layout.Region;
28: import javafx.scene.paint.Color;
29: import javafx.util.Duration;
30: 
31: /**
32:  * Displays a Vier Connects field.
33:  */
34: final class VierConnectsFieldView extends Region {
35: 
36:     /**
37:      * The field state.
38:      */
39:     private VierConnectsFieldState fieldState;
40: 
41:     /**
42:      * The canvas.
43:      */
44:     private final Canvas canvas;
45: 
46:     /**
47:      * The duration of a move.
48:      */
49:     private final ObjectProperty<Duration> moveDuration;
50:     
51:     /**
52:      * {@code true} if the field is highlighted, else {@code false}.
53:      */
54:     private boolean highlighted;
55: 
56:     /**
57:      * Creates an {@link VierConnectsFieldView}.
58:      *
59:      * @param fieldState The field state.
60:      */
61:     VierConnectsFieldView(final VierConnectsFieldState fieldState) {
62:         this.fieldState = fieldState;
63:         this.moveDuration = new SimpleObjectProperty<>(Duration.millis(500.0));
64:         this.highlighted = false;
65: 
66:         this.canvas = new Canvas() {
67: 
68:             @Override
69:             public boolean isResizable() {
70:                 return true;
71:             }
72: 
73:             @Override
74:             public double prefWidth(final double height) {
75:                 return 0.0;
76:             }
77: 
78:             @Override
79:             public double prefHeight(final double width) {
80:                 return 0.0;
81:             }
82: 
83:             @Override
84:             public double maxWidth(final double height) {
85:                 return Double.POSITIVE_INFINITY;
86:             }
87: 
88:             @Override
89:             public double maxHeight(final double width) {
90:                 return Double.POSITIVE_INFINITY;
91:             }
92:         };
93: 
94:         this.canvas.widthProperty().bind(this.widthProperty());
95:         this.canvas.heightProperty().bind(this.heightProperty());
96:         this.getChildren().add(this.canvas);
97: 
98:         this.widthProperty().addListener(
99:                 (final ObservableValue<? extends Number> observable, final Number oldValue, final Number newValue) -> {
100:                     this.draw();
101:                 });
102:         this.heightProperty().addListener(
103:                 (final ObservableValue<? extends Number> observable, final Number oldValue, final Number newValue) -> {
104:                     this.draw();
105:                 });
106: 
107:         this.setMinSize(50.0, 50.0);
108:         this.setMaxSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
109:     }
110: 
111:     /**
112:      * Returns the move duration property.
113:      */
114:     ObjectProperty<Duration> moveDurationProperty() {
115:         return this.moveDuration;
116:     }
117: 
118:     /**
119:      * Updates the state of the field control.
120:      *
121:      * @param fieldState The field state.
122:      */
123:     void setFieldState(final VierConnectsFieldState fieldState) {
124:         this.fieldState = fieldState;
125:         this.draw();
126:     }
127: 
128:     /**
129:      * Updates the highlight state of the field control.
130:      *
131:      * @param highlighted {@code true} if the field is highlighted, else {@code false}.
132:      */
133:     void setHighlighted(final boolean highlighted) {
134:         this.highlighted = highlighted;
135:         this.draw();
136:     }
137: 
138:     /**
139:      * Draws the field.
140:      */
141:     private void draw() {
142:         final double height = this.getHeight();
143:         final double width = this.getWidth();
144: 
145:         final GraphicsContext gc = this.canvas.getGraphicsContext2D();
146:         gc.setFill(this.highlighted ? Color.BLUE : Color.DARKBLUE);
147:         gc.fillRect(0.0, 0.0, width, height);
148:         
149:         final double marginheight = height * 0.1;
150:         final double marginwidth = width * 0.1;
151:         
152:     
153:         
154: 
155:         switch (this.fieldState) {
156:         case EMPTY:
157:             gc.setFill(Color.WHITE);
158:             gc.fillOval(marginwidth, marginheight, width - 2 * marginwidth, height - 2 * marginheight);
159:             gc.stroke();
160:             break;
161:         case CROSS:
162:             gc.setFill(Color.RED);
163:             gc.fillOval(marginwidth, marginheight, width - 2 * marginwidth, height - 2 * marginheight);
164:             break;
165:         case NOUGHT:
166:             gc.setFill(Color.YELLOW);
167:             gc.fillOval(marginwidth, marginheight, width - 2 * marginwidth, height - 2 * marginheight);
168:             break;
169:         default:
170:             throw new UnsupportedOperationException(
171:                     String.format("Unknown Vier Connects field state '%s'.", this.fieldState));
172:         }
173:     }
174: }