com.arconus.dicecommander.ui.dialogs.PowerRollResultDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.arconus.dicecommander.ui.dialogs.PowerRollResultDialog.java

Source

/*
 *   Copyright (C) 2012 Joe AmRhein
 *
 *   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 com.arconus.dicecommander.ui.dialogs;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.arconus.dicecommander.R;
import com.arconus.dicecommander.model.entities.character.power.CharPowerRollResult;
import com.arconus.dicecommander.model.entities.dice.CriticalStatus;

public class PowerRollResultDialog extends DialogFragment {
    CharPowerRollResult roll;

    public static PowerRollResultDialog newInstance(CharPowerRollResult roll) {
        PowerRollResultDialog f = new PowerRollResultDialog();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putParcelable("roll", roll);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        roll = getArguments().getParcelable("roll");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.power_roll_result_dialog, container, false);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        prepareRollResultDialog(v);

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return v;
    }

    private void prepareRollResultDialog(View view) {
        if (roll.getHasAttackRoll()) {
            TextView attkRollResult = (TextView) view.findViewById(R.id.attack_roll_result);
            attkRollResult.setText(String.valueOf(roll.getAttackRollResult()));

            TextView targetDefense = (TextView) view.findViewById(R.id.attack_roll_target_defense);
            targetDefense.setText(roll.getTargetDefenseResID());

            showCritStatus(view);
        } else {
            TextView attackRollTitle = (TextView) view.findViewById(R.id.attack_roll_title);
            attackRollTitle.setVisibility(View.GONE);

            LinearLayout attackRollLayout = (LinearLayout) view.findViewById(R.id.attack_roll_result_layout);
            attackRollLayout.setVisibility(View.GONE);
        }

        TextView dmgRollResult = (TextView) view.findViewById(R.id.damage_roll_result);
        if (roll.getHasDamageRoll()) {
            dmgRollResult.setText(String.valueOf(roll.getDamageRollResult()));
        } else {
            TextView damageRollTitle = (TextView) view.findViewById(R.id.damage_roll_title);
            damageRollTitle.setVisibility(View.GONE);

            dmgRollResult.setVisibility(View.GONE);
        }

    }

    private void showCritStatus(View view) {
        if (roll != null) {
            CriticalStatus status = roll.getCritStatus();

            if (status == CriticalStatus.SUCCESS || status == CriticalStatus.FAILURE) {
                TextView critStatus = (TextView) view.findViewById(R.id.critical_status);
                if (critStatus != null) {
                    critStatus.setVisibility(View.VISIBLE);

                    int stringID = (status == CriticalStatus.SUCCESS) ? R.string.critical_hit
                            : R.string.critical_failure;
                    int colorID = (status == CriticalStatus.SUCCESS) ? R.color.critical_hit
                            : R.color.critical_failure;

                    critStatus.setText(stringID);
                    critStatus.setTextColor(getResources().getColor(colorID));
                }
            }
        }
    }
}