@ -6,13 +6,16 @@ import javafx.event.ActionEvent;
import javafx.event.EventHandler ;
import javafx.fxml.FXML ;
import javafx.scene.control.Button ;
import javafx.scene.control.Label ;
import javafx.scene.control.ListView ;
import javafx.scene.input.KeyEvent ;
import javafx.scene.layout.AnchorPane ;
import visualiser.gameController.Keys.ControlKey ;
import visualiser.gameController.Keys.KeyFactory ;
import java.util.Map ;
/ * *
* Controller for the scene used to display and update current key bindings .
* /
public class KeyBindingsController {
@FXML Button btnSave ;
@FXML Button btnCancel ;
@ -20,18 +23,21 @@ public class KeyBindingsController {
@FXML ListView lstControl ;
@FXML ListView lstKey ;
@FXML ListView lstDescription ;
@FXML Label lblTitle ;
@FXML AnchorPane anchor ;
String currentButton = null ;
KeyFactory keyFactory = KeyFactory . getFactory ( ) ;
public void initialize ( ) {
// headings for each column
lstKey . getItems ( ) . add ( "Key" ) ;
lstControl . getItems ( ) . add ( "Command" ) ;
lstDescription . getItems ( ) . add ( "Description" ) ;
lstKey . getSelectionModel ( ) . select ( 0 ) ;
lstControl . getSelectionModel ( ) . select ( 0 ) ;
lstDescription . getSelectionModel ( ) . select ( 0 ) ;
// populate columns with current key bindings and buttons to update
for ( Map . Entry < String , ControlKey > entry : keyFactory . getKeyState ( ) . entrySet ( ) ) {
Button button = new Button ( entry . getKey ( ) ) ;
button . setMinWidth ( 120 ) ;
@ -46,10 +52,7 @@ public class KeyBindingsController {
lstDescription . getItems ( ) . add ( entry . getValue ( ) . getProtocolCode ( ) ) ;
}
lstKey . getSelectionModel ( ) . select ( 0 ) ;
lstControl . getSelectionModel ( ) . select ( 0 ) ;
lstDescription . getSelectionModel ( ) . select ( 0 ) ;
// stop the columns from being selectable, so only the buttons are
lstKey . getSelectionModel ( ) . selectedItemProperty ( )
. addListener ( ( observable , oldvalue , newValue ) - > {
Platform . runLater ( new Runnable ( ) {
@ -75,42 +78,52 @@ public class KeyBindingsController {
} ) ;
} ) ;
// add CSS stylesheet once the scene has been created
lstKey . sceneProperty ( ) . addListener ( ( obs , oldScene , newScene ) - > {
if ( newScene ! = null ) {
newScene . getStylesheets ( ) . add ( "/css/keyBindings.css" ) ;
}
} ) ;
}
public void cancel ( ) {
System . out . println ( "cancel clicked" ) ;
setKeyListener ( ) ;
}
public void reset ( ) {
System . out . println ( "reset clicked" ) ;
}
public void save ( ) {
System . out . println ( "save clicked" ) ;
}
public void onKey ( KeyEvent e ) {
System . out . println ( "key pressed" ) ;
/ * *
* Creates a listener for the base anchorpane for key presses .
* It updates the current key bindings if required .
* /
public void setKeyListener ( ) {
anchor . addEventFilter ( KeyEvent . KEY_PRESSED , event - > {
// if a button was clicked
if ( currentButton ! = null ) {
if ( currentButton ! = null ) {
// update text on the button
ObservableList buttons = lstKey . getItems ( ) ;
for ( int i = 1 ; i < buttons . size ( ) ; i + + ) {
if ( currentButton = = ( ( Button ) buttons . get ( i ) ) . getText ( ) ) {
( ( Button ) buttons . get ( i ) ) . setText ( e . getCode ( ) . toString ( ) ) ;
( ( Button ) buttons . get ( i ) ) . setText ( event . getCode ( ) . toString
( ) ) ;
break ;
}
}
// update the control key
keyFactory . updateKey ( currentButton , e . getCode ( ) . toString ( ) ) ;
keyFactory . updateKey ( currentButton , e vent . getCode ( ) . toString ( ) ) ;
// remove current button selection
currentButton = null ;
}
event . consume ( ) ;
} ) ;
}
public void cancel ( ) {
System . out . println ( "cancel clicked" ) ;
}
public void reset ( ) {
System . out . println ( "reset clicked" ) ;
}
public void save ( ) {
System . out . println ( "save clicked" ) ;
}
}