@ -1,9 +1,6 @@
package visualiser.Controllers ;
import javafx.application.Platform ;
import javafx.collections.ObservableList ;
import javafx.event.ActionEvent ;
import javafx.event.EventHandler ;
import javafx.fxml.FXML ;
import javafx.scene.control.Button ;
import javafx.scene.control.ListView ;
@ -11,25 +8,40 @@ import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane ;
import visualiser.gameController.Keys.ControlKey ;
import visualiser.gameController.Keys.KeyFactory ;
import java.util.HashMap ;
import java.util.Map ;
import static visualiser.app.App.keyFactory ;
/ * *
* Controller for the scene used to display and update current key bindings .
* /
public class KeyBindingsController {
@FXML Button btnSave ;
@FXML Button btnCancel ;
@FXML Button btnReset ;
@FXML ListView lstControl ;
@FXML ListView lstKey ;
@FXML ListView lstDescription ;
@FXML AnchorPane anchor ;
String currentButton = null ;
KeyFactory keyFactory = KeyFactory . getFactory ( ) ;
private @FXML Button btnSave ;
private @FXML Button btnCancel ;
private @FXML Button btnReset ;
private @FXML ListView lstControl ;
private @FXML ListView lstKey ;
private @FXML ListView lstDescription ;
private @FXML AnchorPane anchor ;
private Button currentButton = null ;
private KeyFactory newKeyFactory ;
public void initialize ( ) {
// create new key factory to modify, keeping the existing one safe
newKeyFactory = copyExistingFactory ( ) ;
initializeTable ( ) ;
populateTable ( ) ;
setKeyListener ( ) ;
}
// headings for each column
/ * *
* Sets up table before populating it .
* Set up includes headings , CSS styling and modifying default properties .
* /
public void initializeTable ( ) {
// set the headings for each column
lstKey . getItems ( ) . add ( "Key" ) ;
lstControl . getItems ( ) . add ( "Command" ) ;
lstDescription . getItems ( ) . add ( "Description" ) ;
@ -37,47 +49,6 @@ public class KeyBindingsController {
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 ) ;
button . setOnAction ( new EventHandler < ActionEvent > ( ) {
@Override public void handle ( ActionEvent e ) {
currentButton = button . getText ( ) ;
System . out . println ( "Button clicked" ) ;
}
} ) ;
lstKey . getItems ( ) . add ( button ) ;
lstControl . getItems ( ) . add ( entry . getValue ( ) ) ;
lstDescription . getItems ( ) . add ( entry . getValue ( ) . getProtocolCode ( ) ) ;
}
// stop the columns from being selectable, so only the buttons are
lstKey . getSelectionModel ( ) . selectedItemProperty ( )
. addListener ( ( observable , oldvalue , newValue ) - > {
Platform . runLater ( new Runnable ( ) {
public void run ( ) {
lstKey . getSelectionModel ( ) . select ( 0 ) ;
}
} ) ;
} ) ;
lstDescription . getSelectionModel ( ) . selectedItemProperty ( )
. addListener ( ( observable , oldvalue , newValue ) - > {
Platform . runLater ( new Runnable ( ) {
public void run ( ) {
lstDescription . getSelectionModel ( ) . select ( 0 ) ;
}
} ) ;
} ) ;
lstControl . getSelectionModel ( ) . selectedItemProperty ( )
. addListener ( ( observable , oldvalue , newValue ) - > {
Platform . runLater ( new Runnable ( ) {
public void run ( ) {
lstControl . getSelectionModel ( ) . select ( 0 ) ;
}
} ) ;
} ) ;
// add CSS stylesheet once the scene has been created
lstKey . sceneProperty ( ) . addListener ( ( obs , oldScene , newScene ) - > {
if ( newScene ! = null ) {
@ -85,7 +56,54 @@ public class KeyBindingsController {
}
} ) ;
setKeyListener ( ) ;
// stop the columns from being selectable, so only the buttons are
lstKey . getSelectionModel ( ) . selectedItemProperty ( )
. addListener ( ( observable , oldvalue , newValue ) - >
Platform . runLater ( ( ) - >
lstKey . getSelectionModel ( ) . select ( 0 ) ) ) ;
lstDescription . getSelectionModel ( ) . selectedItemProperty ( )
. addListener ( ( observable , oldvalue , newValue ) - >
Platform . runLater ( ( ) - >
lstDescription . getSelectionModel ( ) . select ( 0 ) ) ) ;
lstControl . getSelectionModel ( ) . selectedItemProperty ( )
. addListener ( ( observable , oldvalue , newValue ) - >
Platform . runLater ( ( ) - >
lstControl . getSelectionModel ( ) . select ( 0 ) ) ) ;
}
/ * *
* Populates the table with commands and their key binding details .
* /
public void populateTable ( ) {
// add each command to the table
for ( Map . Entry < String , ControlKey > entry : newKeyFactory . getKeyState ( ) . entrySet ( ) ) {
// create button for command
Button button = new Button ( entry . getKey ( ) ) ;
button . setMinWidth ( 120 ) ;
button . setId ( entry . getValue ( ) . toString ( ) ) ;
button . setOnAction ( e - > currentButton = button ) ;
// display details for command in table
lstControl . getItems ( ) . add ( entry . getValue ( ) ) ;
lstKey . getItems ( ) . add ( button ) ;
lstDescription . getItems ( ) . add ( entry . getValue ( ) . getProtocolCode ( ) ) ;
}
}
/ * *
* Makes a copy of the keyfactory that does not modify the original .
* @return new keyfactory to be modified
* /
public KeyFactory copyExistingFactory ( ) {
newKeyFactory = new KeyFactory ( ) ;
Map < String , ControlKey > newKeyState = newKeyFactory . getKeyState ( ) ;
Map < String , ControlKey > oldKeyState = keyFactory . getKeyState ( ) ;
newKeyState = new HashMap < > ( ) ; // clear default keys
// copy over commands and their keys
for ( Map . Entry < String , ControlKey > entry : oldKeyState . entrySet ( ) ) {
newKeyState . put ( entry . getKey ( ) , entry . getValue ( ) ) ;
}
return newKeyFactory ;
}
/ * *
@ -96,17 +114,12 @@ public class KeyBindingsController {
anchor . addEventFilter ( KeyEvent . KEY_PRESSED , event - > {
// if a button was clicked
if ( currentButton ! = null ) {
System . out . println ( "button is clicked" ) ;
// 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 ( event . getCode ( ) . toString
( ) ) ;
break ;
}
}
currentButton . setText ( event . getCode ( ) . toString ( ) ) ;
// update the control key
keyFactory . updateKey ( currentButton , event . getCode ( ) . toString ( ) ) ;
newKeyFactory . updateKey ( event . getCode ( ) . toString ( ) ,
currentButton . getId ( ) ) ;
// remove current button selection
currentButton = null ;
}