Android Open Source - ubisoldiers Unit Customization Fragment






From Project

Back to project page ubisoldiers.

License

The source code is released under:

MIT License

If you think the Android project ubisoldiers listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.github.gobbisanches.ubisoldiers.app;
/*from  w  w w . j av a2s .  c  om*/
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.github.gobbisanches.ubisoldiers.mechanics.*;

import java.util.ArrayList;

import static com.github.gobbisanches.ubisoldiers.app.ItemListFragment.*;

/**
 * Created by Sanches on 12/07/2014.
 */
public class UnitCustomizationFragment extends Fragment implements ItemListFragmentListener {
    public interface UnitCustomizationFragmentListener {
        public void onChangeUnit(int unitCustomizationId, Unit updatedUnit);
    }

    private static final String UNIT_CUSTOMIZATION_ID = "com.github.gobbisanches.ubisoldier.app.UnitCustomizationFragment.UnitCustomizationId";
    private static final String UNIT_INDEX = "com.github.gobbisanches.ubisoldier.app.UnitCustomizationFragment.UnitIndex";
    private static final String GENERAL = "com.github.gobbisanches.ubisoldier.app.UnitCustomizationFragment.General";
    private static final int SOLDIER_LIST_ID = 0;
    private static final int WEAPON_LIST_ID = 1;
    private static final int ARMOR_LIST_ID = 2;

    private int unitCustomizationId;
    private General general;
    private Unit unit;
    private int unitIndex;
    private UnitFragment unitFragment;
    private ItemListFragment soldierListFragment;
    private ItemListFragment weaponListFragment;
    private ItemListFragment armorListFragment;
    private FragmentManager fragmentManager;
    private UnitCustomizationFragmentListener listener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        unitCustomizationId = getArguments().getInt(UNIT_CUSTOMIZATION_ID);
        general = (General) getArguments().getSerializable(GENERAL);

        unitIndex = getArguments().getInt(UNIT_INDEX);
        unit = general.getSquad().getUnit(unitIndex);
        fragmentManager = getFragmentManager();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View v = inflater.inflate(R.layout.unit_customization_layout, container, false);

        unitFragment = createUnitFragmentIfMissing(R.id.unitFragmentView, unit);
        ArrayList<Item> soldiers = new ArrayList<Item>(general.getAvailableSoldiers().values());
        soldiers.add(unit.getSoldier());
        soldierListFragment = createListFragmentIfMissing(R.id.soldierListView, SOLDIER_LIST_ID, soldiers);
        weaponListFragment = createListFragmentIfMissing(R.id.weaponListView, WEAPON_LIST_ID, new ArrayList<Item>(general.getAvailableWeapons().values()));
        armorListFragment = createListFragmentIfMissing(R.id.armorListView, ARMOR_LIST_ID, new ArrayList<Item>(general.getAvailableArmors().values()));

        return v;
    }

    private ItemListFragment createListFragmentIfMissing(int id, int listId, ArrayList<Item> items) {
        ItemListFragment fragment = (ItemListFragment) fragmentManager.findFragmentById(id);
        if (fragment == null) {
            fragment = ItemListFragment.newInstance(listId, items, this);
            fragmentManager.beginTransaction().add(id, fragment).commit();
        }

        return fragment;
    }

    private UnitFragment createUnitFragmentIfMissing(int id, Unit unit) {
        UnitFragment fragment = (UnitFragment) fragmentManager.findFragmentById(id);
        if (fragment == null) {
            fragment = UnitFragment.newInstance(unit);
            fragmentManager.beginTransaction().add(id, fragment).commit();
        }

        return fragment;
    }

    @Override
    public void onSelectItem(int id, Item item, int position, ArrayList<Item> allItems) {
        switch (id) {
            case SOLDIER_LIST_ID:
                unit.setSoldier((Soldier) item);
                break;
            case WEAPON_LIST_ID:
                unit.setWeapon((Weapon) item);
                break;
            case ARMOR_LIST_ID:
                unit.setArmor((Armor) item);
                break;
            default:
                throw new RuntimeException("Invalid list ID " + id);
        }

        unitFragment.updateUnitAndSetHpToFull(unit);
        listener.onChangeUnit(unitCustomizationId, unit);
    }

    private void setUnitCustomizationListener(UnitCustomizationFragmentListener listener) {
        this.listener = listener;
    }

    public static UnitCustomizationFragment newInstance(int unitCustomizationId,
                                                        General general,
                                                        int unitIndex,
                                                        UnitCustomizationFragmentListener listener) {
        if(general == null) {
            throw new RuntimeException("Invalid General object");
        }
        if((unitIndex < 0) || (unitIndex > 2)) {
            throw new RuntimeException("Invalid unitIndex " + unitIndex);
        }

        Bundle args = new Bundle();
        args.putInt(UNIT_CUSTOMIZATION_ID, unitCustomizationId);
        args.putSerializable(GENERAL, general);
        args.putInt(UNIT_INDEX, unitIndex);

        UnitCustomizationFragment fragment = new UnitCustomizationFragment();
        fragment.setArguments(args);
        fragment.setUnitCustomizationListener(listener);

        return fragment;
    }
}




Java Source Code List

com.github.gobbisanches.ubisoldiers.app.ArmyFragment.java
com.github.gobbisanches.ubisoldiers.app.BattleActivity.java
com.github.gobbisanches.ubisoldiers.app.BattleFragment.java
com.github.gobbisanches.ubisoldiers.app.ItemListFragment.java
com.github.gobbisanches.ubisoldiers.app.MainActivity.java
com.github.gobbisanches.ubisoldiers.app.ResourceManager.java
com.github.gobbisanches.ubisoldiers.app.SingleFragmentActivity.java
com.github.gobbisanches.ubisoldiers.app.UbisoldierUosApplication.java
com.github.gobbisanches.ubisoldiers.app.UbisoldiersDriver.java
com.github.gobbisanches.ubisoldiers.app.UnitCustomizationActivity.java
com.github.gobbisanches.ubisoldiers.app.UnitCustomizationFragment.java
com.github.gobbisanches.ubisoldiers.app.UnitFragment.java
com.github.gobbisanches.ubisoldiers.app.UosManager.java
com.github.gobbisanches.ubisoldiers.mechanics.Armor.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLogEntry.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLogParser.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLog.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleSquad.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleUnit.java
com.github.gobbisanches.ubisoldiers.mechanics.Battle.java
com.github.gobbisanches.ubisoldiers.mechanics.DefaultGameRules.java
com.github.gobbisanches.ubisoldiers.mechanics.GameRules.java
com.github.gobbisanches.ubisoldiers.mechanics.General.java
com.github.gobbisanches.ubisoldiers.mechanics.ItemCollection.java
com.github.gobbisanches.ubisoldiers.mechanics.Item.java
com.github.gobbisanches.ubisoldiers.mechanics.MechanicsEngine.java
com.github.gobbisanches.ubisoldiers.mechanics.PolicyManager.java
com.github.gobbisanches.ubisoldiers.mechanics.SearchEngine.java
com.github.gobbisanches.ubisoldiers.mechanics.Soldier.java
com.github.gobbisanches.ubisoldiers.mechanics.Squad.java
com.github.gobbisanches.ubisoldiers.mechanics.Unit.java
com.github.gobbisanches.ubisoldiers.mechanics.Weapon.java