Java tutorial
/* * Copyright 2016 Edward Eisenhart * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package eisene.riskspeedtools; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import java.security.InvalidParameterException; /** * Created by Edward Eisenhart on Sep 2016. */ public class BattleFrag extends Fragment implements BattleView { private BattlePresenter presenter; boolean isDiceCountIncreasing = true; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_battle, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); presenter = new BattlePresenterImpl(this); setMode(false); setupOnClickListeners(); } /** * Called when the 'Roll' button is clicked. */ public void rollOnClick() { if (isBlitzMode()) { presenter.blitzOnClick(getNumAttackBlitz(), getNumDefendBlitz()); } else { presenter.rollOnClick(getNumAttackDice(), getNumDefendDice()); } } /** * Increments or decrements the number of attack dice to use on click. */ public void attackDiceOnClick() { ImageView aDie2 = (ImageView) findViewById(R.id.attack_die_2); ImageView aDie3 = (ImageView) findViewById(R.id.attack_die_3); if (aDie3.getVisibility() == View.VISIBLE) { aDie3.setVisibility(View.INVISIBLE); isDiceCountIncreasing = false; } else if (aDie2.getVisibility() == View.VISIBLE) { if (isDiceCountIncreasing) { aDie3.setVisibility(View.VISIBLE); } else { aDie2.setVisibility(View.INVISIBLE); } } else { aDie2.setVisibility(View.VISIBLE); isDiceCountIncreasing = true; } } /** * Increments or decrements the number of defense dice to use on click. */ public void defendDiceOnClick() { ImageView dDie2 = (ImageView) findViewById(R.id.defend_die_2); if (dDie2.getVisibility() == View.VISIBLE) { dDie2.setVisibility(View.INVISIBLE); } else { dDie2.setVisibility(View.VISIBLE); } } /** * Changes the mode (dice or blitz) on click. */ public void modeOnClick() { LinearLayout blitzFrame = (LinearLayout) findViewById(R.id.blitz_frame); setMode(blitzFrame.getVisibility() == View.GONE); } /** * Sets the mode (dice or blitz). * @param toBlitzMode True to set to blitz mode, false to set to dice mode. */ public void setMode(boolean toBlitzMode) { LinearLayout blitzFrame = (LinearLayout) findViewById(R.id.blitz_frame); LinearLayout diceFrame = (LinearLayout) findViewById(R.id.dice_frame); if (toBlitzMode) { blitzFrame.setVisibility(View.VISIBLE); diceFrame.setVisibility(View.GONE); } else { diceFrame.setVisibility(View.VISIBLE); blitzFrame.setVisibility(View.GONE); } } /** * Gets the number of attack dice. * @return The number of attack dice. */ public int getNumAttackDice() { ImageView aDie2 = (ImageView) findViewById(R.id.attack_die_2); ImageView aDie3 = (ImageView) findViewById(R.id.attack_die_3); if (aDie3.getVisibility() == View.VISIBLE) { return 3; } else if (aDie2.getVisibility() == View.VISIBLE) { return 2; } else { return 1; } } /** * Gets the number of defend dice. * @return The number of defend dice. */ public int getNumDefendDice() { ImageView dDie2 = (ImageView) findViewById(R.id.defend_die_2); if (dDie2.getVisibility() == View.VISIBLE) { return 2; } else { return 1; } } /** * Gets the number of blitz attackers. * @return The number of blitz attackers. */ public int getNumAttackBlitz() { EditText attack = (EditText) findViewById(R.id.blitz_attack_start); Integer numAttack = Converters.getInteger(attack); if (numAttack != null) { return numAttack; } else { attack.setText("0"); return 0; } } /** * Gets the number of blitz defenders. * @return The number of blitz defenders. */ public int getNumDefendBlitz() { EditText defend = (EditText) findViewById(R.id.blitz_defense_start); Integer numDefend = Converters.getInteger(defend); if (numDefend != null) { return numDefend; } else { defend.setText("0"); return 0; } } /** * Sets the number of attack losses to display. * @param numLosses The number of attack losses. */ public void setNumAttackLosses(int numLosses) { TextView attackLosses = (TextView) findViewById(R.id.blitz_attack_losses); TextView attackTotal = (TextView) findViewById(R.id.blitz_attack_final); TextView attackStart = (TextView) findViewById(R.id.blitz_attack_start); int attackStartNum = Integer.parseInt(attackStart.getText().toString()); int attackEndNum = attackStartNum - numLosses; attackLosses.setText("-" + Integer.toString(numLosses)); attackTotal.setText(Integer.toString(attackEndNum)); } /** * Sets the number of defense losses to display. * @param numLosses The number of defense losses. */ public void setNumDefenseLosses(int numLosses) { TextView defendLosses = (TextView) findViewById(R.id.blitz_defense_losses); TextView defendTotal = (TextView) findViewById(R.id.blitz_defense_final); TextView defendStart = (TextView) findViewById(R.id.blitz_defense_start); int defendStartNum = Integer.parseInt(defendStart.getText().toString()); int defendEndNum = defendStartNum - numLosses; defendLosses.setText("-" + Integer.toString(numLosses)); defendTotal.setText(Integer.toString(defendEndNum)); } /** * Checks if we are displaying blitz mode or dice mode. * @return True if we are in blitz mode, false if in dice mode. */ public boolean isBlitzMode() { LinearLayout blitzFrame = (LinearLayout) findViewById(R.id.blitz_frame); return (blitzFrame.getVisibility() == View.VISIBLE); } /** * Sets and display the dice values. * @param a1 Value of attack dice #1 * @param isA1Red True if attack dice #1 lost, false if it won. * @param a2 Value of attack dice #2. 0 if the dice is not used. * @param isA2Red True if attack dice #2 lost, false if it won. * @param a3 Value of attack dice #3. 0 if the dice is not used. * @param d1 Value of defend dice #1 * @param d2 Value of defend dice #2. 0 if the dice is not used. */ public void setDiceValues(int a1, boolean isA1Red, int a2, boolean isA2Red, int a3, int d1, int d2) { ImageView aDie1 = (ImageView) findViewById(R.id.attack_die_1); ImageView aDie2 = (ImageView) findViewById(R.id.attack_die_2); ImageView aDie3 = (ImageView) findViewById(R.id.attack_die_3); ImageView dDie1 = (ImageView) findViewById(R.id.defend_die_1); ImageView dDie2 = (ImageView) findViewById(R.id.defend_die_2); setDiceValue(aDie1, a1, isA1Red); setDiceValue(aDie2, a2, isA2Red); setDiceValue(aDie3, a3, false); setDiceValue(dDie1, d1, !isA1Red); setDiceValue(dDie2, d2, !isA2Red); } /** * Look for a child view with the given id. If this view has the given * id, return this view. * * @param id The id to search for. * @return The view that has the given id in the hierarchy or null */ private View findViewById(int id) { return getView().findViewById(id); } /** * Sets the correct dice picture to the ImageView. * @param view The dice imageview. * @param value The dice value. * @param isRed Whether the dice lost or won. */ private void setDiceValue(ImageView view, int value, boolean isRed) { int toDraw; switch (value) { case 0: toDraw = R.drawable.blank_die; break; case 1: toDraw = (isRed) ? R.drawable.red_die1 : R.drawable.die1; break; case 2: toDraw = (isRed) ? R.drawable.red_die2 : R.drawable.die2; break; case 3: toDraw = (isRed) ? R.drawable.red_die3 : R.drawable.die3; break; case 4: toDraw = (isRed) ? R.drawable.red_die4 : R.drawable.die4; break; case 5: toDraw = (isRed) ? R.drawable.red_die5 : R.drawable.die5; break; case 6: toDraw = (isRed) ? R.drawable.red_die6 : R.drawable.die6; break; default: throw new InvalidParameterException("Invalid dice value"); } view.setImageResource(toDraw); } private void setupOnClickListeners() { (findViewById(R.id.btn_blitz)).setOnClickListener(mBlitzButtonOnClick); (findViewById(R.id.attack_dice)).setOnClickListener(mAttackDiceOnClick); (findViewById(R.id.defend_dice)).setOnClickListener(mDefendDiceOnClick); (findViewById(R.id.btn_roll)).setOnClickListener(mRollOnClick); } private View.OnClickListener mBlitzButtonOnClick = new View.OnClickListener() { public void onClick(View v) { modeOnClick(); } }; private View.OnClickListener mAttackDiceOnClick = new View.OnClickListener() { public void onClick(View v) { attackDiceOnClick(); } }; private View.OnClickListener mDefendDiceOnClick = new View.OnClickListener() { public void onClick(View v) { defendDiceOnClick(); } }; private View.OnClickListener mRollOnClick = new View.OnClickListener() { public void onClick(View v) { rollOnClick(); } }; }