Guest User

Untitled

a guest
Apr 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. @Override
  2. public void handle(Event event) {
  3. if (event.getSource() instanceof Tile) {
  4. Tile tile = (Tile) event.getSource();
  5. if (event.getEventType().equals(MouseEvent.MOUSE_CLICKED)) {
  6. if (((MouseEvent) event).getButton().equals(MouseButton.SECONDARY))
  7. tile.toggleFlag();
  8. else if (((MouseEvent) event).getClickCount() == 2)
  9. mineField.massClick(tile);
  10. }
  11. if (event.getEventType().equals(MouseEvent.DRAG_DETECTED))
  12. if (!((MouseEvent) event).getButton().equals(MouseButton.SECONDARY))
  13. tile.startFullDrag();
  14. if (event.getEventType().equals(MouseDragEvent.MOUSE_DRAG_ENTERED))
  15. tile.arm();
  16. if (event.getEventType().equals(MouseDragEvent.MOUSE_DRAG_EXITED))
  17. tile.disarm();
  18. if (event.getEventType().equals(MouseDragEvent.MOUSE_DRAG_RELEASED))
  19. mineField.clickedTile(tile);
  20. if (event.getEventType().equals(ActionEvent.ANY))
  21. mineField.clickedTile(tile);
  22. }
  23. }
  24.  
  25. public class Main extends Application {
  26.  
  27. @Override
  28. public void start(Stage primaryStage) {
  29. try {
  30.  
  31. StackPane root = new StackPane();
  32.  
  33. root.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
  34.  
  35. if( e.isPrimaryButtonDown() && e.isSecondaryButtonDown()) {
  36. System.out.println( "Both down");
  37. } else if( e.isPrimaryButtonDown()) {
  38. System.out.println( "Primary down");
  39. } else if( e.isSecondaryButtonDown()) {
  40. System.out.println( "Secondary down");
  41. }
  42.  
  43. });
  44.  
  45. Scene scene = new Scene(root,400,400);
  46. primaryStage.setScene(scene);
  47. primaryStage.show();
  48.  
  49. } catch(Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54. public static void main(String[] args) {
  55. launch(args);
  56. }
  57. }
  58.  
  59. public class Main extends Application {
  60.  
  61. BooleanProperty primaryMouseButtonDown = new SimpleBooleanProperty();
  62. BooleanProperty secondaryMouseButtonDown = new SimpleBooleanProperty();
  63.  
  64. @Override
  65. public void start(Stage primaryStage) {
  66. try {
  67.  
  68. StackPane root = new StackPane();
  69.  
  70. root.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
  71. primaryMouseButtonDown.setValue( e.isPrimaryButtonDown());
  72. secondaryMouseButtonDown.setValue( e.isSecondaryButtonDown());
  73. });
  74.  
  75. root.addEventFilter(MouseEvent.MOUSE_RELEASED, e -> {
  76. primaryMouseButtonDown.setValue( e.isPrimaryButtonDown());
  77. secondaryMouseButtonDown.setValue( e.isSecondaryButtonDown());
  78. });
  79.  
  80. BooleanBinding binding = Bindings.and(primaryMouseButtonDown, secondaryMouseButtonDown);
  81. binding.addListener( new ChangeListener<Boolean>() {
  82.  
  83. @Override
  84. public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
  85. System.out.println( "Mouse Button Event: " + oldValue + " -> " + newValue);
  86. }
  87. });
  88.  
  89.  
  90. Scene scene = new Scene(root,400,400);
  91. primaryStage.setScene(scene);
  92. primaryStage.show();
  93.  
  94. } catch(Exception e) {
  95. e.printStackTrace();
  96. }
  97. }
  98.  
  99. public static void main(String[] args) {
  100. launch(args);
  101. }
  102. }
  103.  
  104. boolean mouse_1, mouse_2 = false;
  105.  
  106. public void mousePressed(MouseEvent e){
  107. //The numbers are just made up I don't remember the actual codes for the buttons but it's simple enough to figure out.
  108. if(e.getButton()==1){
  109. mouse_1 = true;
  110. }
  111. if(e.getButton()==2){
  112. mouse_2 = true;
  113. }
  114.  
  115. if(mouse_1&&mouse_2){
  116. //Your code here
  117. }
  118. }
  119.  
  120. public void mouseReleased(MouseEvent e){
  121. if(e.getButton() == 1){
  122. mouse_1 = false;
  123. }
  124. if(e.getButton() == 2){
  125. mouse_2 = false;
  126. }
  127. }
  128.  
  129. //flag to track both buttons being pressed
  130. private boolean wereBothButtonsPressed = false;
  131.  
  132. private void onMousePressed(MouseEvent e) {
  133. //single button press sets flag to false
  134. wereBothButtonsPressed = e.isPrimaryButtonDown() && e.isSecondaryButtonDown();
  135. }
  136.  
  137. private void onMouseReleased(MouseEvent e) {
  138. if (e.isPrimaryButtonDown() || e.isSecondaryButtonDown()) {
  139. //do nothing if user is still holding the button
  140. return;
  141. }
  142. if (wereBothButtonsPressed) {
  143. System.out.prinln("Both buttons");
  144. } else if (e.getButton() == MouseButton.PRIMARY) {
  145. System.out.prinln("Only primary");
  146. } else if (e.getButton() == MouseButton.SECONDARY) {
  147. System.out.prinln("Only secondary");
  148. }
  149. }
  150.  
  151. private boolean wereBothButtonsPressed = false;
  152.  
  153. @Override
  154. public void handle(Event event) {
  155. ...
  156.  
  157. if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
  158. MouseEvent me = (MouseEvent) event;
  159. wereBothButtonsPressed = me.isPrimaryButtonDown() && me.isSecondaryButtonDown();
  160. } else if (event.getEventType().equals(MouseEvent.MOUSE_RELEASED)) {
  161. MouseEvent me = (MouseEvent) event;
  162. if (!me.isPrimaryButtonDown() && !me.isSecondaryButtonDown()) {
  163. if(wereBothButtonsPressed) {
  164. mineField.massClick(tile);
  165. } else if(me.getButton() == MouseButton.PRIMARY) {
  166. mineField.clickedTile(tile);
  167. } else if(me.getButton() == MouseButton.SECONDARY) {
  168. tile.toggleFlag();
  169. }
  170. }
  171. ...
Add Comment
Please, Sign In to add comment