private void initClickModeSelectionUI(IGame game) { DockPanel clickModePanel = new DockPanel(); add(clickModePanel); clickModePanel.setWidth( Integer.toString(FieldCanvas.SIZE * game.getBoard().getDimension().x) + "px"); clickModePanel.setHorizontalAlignment(DockPanel.ALIGN_CENTER); clickOpenButton = new RadioButton("ClickOpens", "click opens"); clickOpenButton.setValue(true); clickFlagButton = new RadioButton("ClickFlags", "click flags"); clickFlagButton.setValue(false); ValueChangeHandler<Boolean> checkBoxToggler = new ValueChangeHandler<Boolean>() { @Override public void onValueChange(ValueChangeEvent<Boolean> event) { CheckBox checkBox = (CheckBox) event.getSource(); if (checkBox == clickOpenButton) clickFlagButton.setValue(!event.getValue()); else clickOpenButton.setValue(!event.getValue()); } }; clickOpenButton.addValueChangeHandler(checkBoxToggler); clickFlagButton.addValueChangeHandler(checkBoxToggler); clickModePanel.add(clickFlagButton, DockPanel.WEST); clickModePanel.add(clickOpenButton, DockPanel.EAST); clickModePanel.setCellHorizontalAlignment(clickOpenButton, DockPanel.ALIGN_LEFT); clickModePanel.setCellHorizontalAlignment(clickFlagButton, DockPanel.ALIGN_RIGHT); }
private void initGameListener() { gameListener = new IMinesGameListener() { @Override public void onBusted() { messageBox.setText("Exploded!"); autoPlayTimer.cancel(); setGameFinished(true); } @Override public void onChange(int flags, int mines) { int bombsToFind = mines - flags; messageBox.setText(Integer.toString(bombsToFind) + " mines left to find."); minesCounter.setValue(bombsToFind); } @Override public void onDisarmed() { messageBox.setText("Mines cleared!"); autoPlayTimer.cancel(); setGameFinished(true); } @Override public void onStart() { startWatch(); messageBox.setText(game.getBombCount() + " mines left to find."); if (clickOpenButton.getValue()) // switch to Flag Mode clickFlagButton.setValue(true, true); } }; game.addListener(gameListener); }
private void initMessageUI() { messageBox = new TextBox(); messageBox.setReadOnly(true); messageBox.setWidth(FieldCanvas.SIZE * game.getBoard().getDimension().x + "px"); messageBox.setText("Click field to start game."); add(messageBox); }
private void initCountersUI() { DockPanel counterPanel = new DockPanel(); add(counterPanel); minesCounter = new Counter(3); minesCounter.setValue(game.getBombCount()); counterPanel.add(minesCounter, DockPanel.WEST); final Counter timeCounter = new Counter(3); counterPanel.setHorizontalAlignment(DockPanel.ALIGN_RIGHT); counterPanel.add(timeCounter, DockPanel.EAST); counterPanel.setWidth( Integer.toString(FieldCanvas.SIZE * game.getBoard().getDimension().x) + "px"); game.getWatch() .addListener( new IStopWatchListener() { @Override public void onTimeChange(int currentTime) { timeCounter.setValue(currentTime); } }); }
private void initBoardUI() { final ClickHandler clickListener = new ClickHandler() { @Override public void onClick(ClickEvent event) { boolean shiftKey = event.isShiftKeyDown(); FieldCanvas canvas = (FieldCanvas) event.getSource(); IField field = canvas.getField(); if (clickOpenButton.getValue() != shiftKey) // logical XOR game.onRequestOpen(field); else game.onRequestFlag(field); } }; gameCanvas = new BoardCanvas(game.getBoard(), clickListener); add(gameCanvas); }
public GamePanel(final IGame game) { this.game = game; initClickModeSelectionUI(game); initCountersUI(); initBoardUI(); initMessageUI(); initAutoPlay(); initGameListener(); timeTicker = new Timer() { @Override public void run() { GamePanel.this.game.tickWatch(); } }; if (game.isRunning()) startWatch(); }
public void dispose() { timeTicker.cancel(); autoPlayTimer.cancel(); game.removeListener(gameListener); gameCanvas.dispose(); }