/* * 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.adapters; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import com.arconus.dicecommander.R; import com.arconus.dicecommander.model.entities.character.power.ActionType; import com.arconus.dicecommander.model.entities.character.power.AttackType; import com.arconus.dicecommander.model.entities.character.power.CharPower; import com.arconus.dicecommander.model.entities.character.power.PowerUsage; import java.util.List; public class CharPowerAdapter extends ArrayAdapter<CharPower> { private final Context context; public CharPowerAdapter(Context context, List<CharPower> objects) { super(context, R.layout.power_card_layout, objects); this.context = context; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.power_card_layout, parent, false); CharPower cpr = this.getItem(position); TextView name = (TextView) rowView.findViewById(R.id.power_card_name); name.setText(cpr.getName()); setBannerColor(rowView, cpr.getPowerUsage()); setActionType(rowView, cpr.getActionType()); setAttackType(rowView, cpr.getAttackType()); setHitString(rowView, cpr); setDamageString(rowView, cpr); return rowView; } private void setBannerColor(View v, PowerUsage p) { View powerBanner = v.findViewById(R.id.power_card_banner); switch (p) { case AT_WILL: powerBanner.setBackgroundColor(context.getResources().getColor(R.color.at_will_green)); break; case ENCOUNTER: powerBanner.setBackgroundColor(context.getResources().getColor(R.color.encounter_red)); break; case DAILY: powerBanner.setBackgroundColor(context.getResources().getColor(R.color.daily_gray)); break; } } private void setHitString(View v, CharPower cpr) { if (cpr.hasAttackRoll()) { String s = ""; TextView attackRollString = (TextView) v.findViewById(R.id.power_card_to_hit_string); s += (cpr.getAttackRollMod() >= 0) ? "+" : ""; s += cpr.getAttackRollMod(); s += " vs "; s += context.getResources().getString(cpr.getTargetDefense().getResourceID()); attackRollString.setText(s); } } private void setDamageString(View v, CharPower cpr) { if (cpr.hasDamageRoll()) { String s = ""; TextView damageRollString = (TextView) v.findViewById(R.id.power_card_damage_roll_string); s += cpr.getDamageRollString(); damageRollString.setText(s); } } private void setActionType(View v, ActionType type) { TextView actionType = (TextView) v.findViewById(R.id.power_card_action_type); actionType.setText(context.getResources().getString(type.getResourceID())); } private void setAttackType(View v, AttackType type) { TextView attackType = (TextView) v.findViewById(R.id.power_card_attack_type); attackType.setText(context.getResources().getString(type.getResourceID())); } }