Adds screen to edit ability scores.
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package com.majinnaibu.monstercards.ui.editmonster;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.navigation.NavBackStackEntry;
|
||||
import androidx.navigation.NavController;
|
||||
import androidx.navigation.Navigation;
|
||||
|
||||
import com.majinnaibu.monstercards.R;
|
||||
|
||||
public class EditAbilityScoresFragment extends Fragment {
|
||||
private EditMonsterViewModel mViewModel;
|
||||
private ViewHolder mHolder;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment);
|
||||
NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.edit_monster_navigation);
|
||||
mViewModel = new ViewModelProvider(backStackEntry).get(EditMonsterViewModel.class);
|
||||
|
||||
// Inflate the layout for this fragment
|
||||
View root = inflater.inflate(R.layout.fragment_edit_ability_scores, container, false);
|
||||
|
||||
mHolder = new ViewHolder(root);
|
||||
|
||||
mViewModel.getStrength().observe(getViewLifecycleOwner(), value -> mHolder.strength.setText(String.valueOf(value)));
|
||||
mHolder.increaseStrength.setOnClickListener(v -> mViewModel.incrementStrength());
|
||||
mHolder.decreaseStrength.setOnClickListener(v -> mViewModel.decrementStrength());
|
||||
|
||||
mViewModel.getDexterity().observe(getViewLifecycleOwner(), value -> mHolder.dexterity.setText(String.valueOf(value)));
|
||||
mHolder.increaseDexterity.setOnClickListener(v -> mViewModel.incrementDexterity());
|
||||
mHolder.decreaseDexterity.setOnClickListener(v -> mViewModel.decrementDexterity());
|
||||
|
||||
mViewModel.getConstitution().observe(getViewLifecycleOwner(), value -> mHolder.constitution.setText(String.valueOf(value)));
|
||||
mHolder.increaseConstitution.setOnClickListener(v -> mViewModel.incrementConstitution());
|
||||
mHolder.decreaseConstitution.setOnClickListener(v -> mViewModel.decrementConstitution());
|
||||
|
||||
mViewModel.getIntelligence().observe(getViewLifecycleOwner(), value -> mHolder.intelligence.setText(String.valueOf(value)));
|
||||
mHolder.increaseIntelligence.setOnClickListener(v -> mViewModel.incrementIntelligence());
|
||||
mHolder.decreaseIntelligence.setOnClickListener(v -> mViewModel.decrementIntelligence());
|
||||
|
||||
mViewModel.getWisdom().observe(getViewLifecycleOwner(), value -> mHolder.wisdom.setText(String.valueOf(value)));
|
||||
mHolder.increaseWisdom.setOnClickListener(v -> mViewModel.incrementWisdom());
|
||||
mHolder.decreaseWisdom.setOnClickListener(v -> mViewModel.decrementWisdom());
|
||||
|
||||
mViewModel.getCharisma().observe(getViewLifecycleOwner(), value -> mHolder.charisma.setText(String.valueOf(value)));
|
||||
mHolder.increaseCharisma.setOnClickListener(v -> mViewModel.incrementCharisma());
|
||||
mHolder.decreaseCharisma.setOnClickListener(v -> mViewModel.decrementCharisma());
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private static class ViewHolder {
|
||||
TextView strength;
|
||||
Button decreaseStrength;
|
||||
Button increaseStrength;
|
||||
TextView dexterity;
|
||||
Button increaseDexterity;
|
||||
Button decreaseDexterity;
|
||||
TextView constitution;
|
||||
Button increaseConstitution;
|
||||
Button decreaseConstitution;
|
||||
TextView intelligence;
|
||||
Button increaseIntelligence;
|
||||
Button decreaseIntelligence;
|
||||
TextView wisdom;
|
||||
Button increaseWisdom;
|
||||
Button decreaseWisdom;
|
||||
TextView charisma;
|
||||
Button increaseCharisma;
|
||||
Button decreaseCharisma;
|
||||
|
||||
ViewHolder(View root) {
|
||||
strength = root.findViewById(R.id.strength);
|
||||
increaseStrength = root.findViewById(R.id.strength_increment);
|
||||
decreaseStrength = root.findViewById(R.id.strength_decrement);
|
||||
dexterity = root.findViewById(R.id.dexterity);
|
||||
increaseDexterity = root.findViewById(R.id.dexterity_increment);
|
||||
decreaseDexterity = root.findViewById(R.id.dexterity_decrement);
|
||||
constitution = root.findViewById(R.id.constitution);
|
||||
increaseConstitution = root.findViewById(R.id.constitution_increment);
|
||||
decreaseConstitution = root.findViewById(R.id.constitution_decrement);
|
||||
intelligence = root.findViewById(R.id.intelligence);
|
||||
increaseIntelligence = root.findViewById(R.id.intelligence_increment);
|
||||
decreaseIntelligence = root.findViewById(R.id.intelligence_decrement);
|
||||
wisdom = root.findViewById(R.id.wisdom);
|
||||
increaseWisdom = root.findViewById(R.id.wisdom_increment);
|
||||
decreaseWisdom = root.findViewById(R.id.wisdom_decrement);
|
||||
charisma = root.findViewById(R.id.charisma);
|
||||
increaseCharisma = root.findViewById(R.id.charisma_increment);
|
||||
decreaseCharisma = root.findViewById(R.id.charisma_decrement);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,6 +102,13 @@ public class EditMonsterFragment extends MCFragment {
|
||||
Navigation.findNavController(view).navigate(action);
|
||||
});
|
||||
|
||||
mHolder.abilityScoresButton.setOnClickListener(v -> {
|
||||
NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditAbilityScoresFragment();
|
||||
View view = getView();
|
||||
assert view != null;
|
||||
Navigation.findNavController(view).navigate(action);
|
||||
});
|
||||
|
||||
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
|
||||
@Override
|
||||
public void handleOnBackPressed() {
|
||||
@@ -154,11 +161,13 @@ public class EditMonsterFragment extends MCFragment {
|
||||
TextView basicInfoButton;
|
||||
TextView armorButton;
|
||||
TextView speedButton;
|
||||
TextView abilityScoresButton;
|
||||
|
||||
ViewHolder(View root) {
|
||||
basicInfoButton = root.findViewById(R.id.basicInfo);
|
||||
armorButton = root.findViewById(R.id.armor);
|
||||
speedButton = root.findViewById(R.id.speed);
|
||||
abilityScoresButton = root.findViewById(R.id.abilityScores);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,12 @@ public class EditMonsterViewModel extends ViewModel {
|
||||
private final MutableLiveData<Integer> mClimbSpeed;
|
||||
private final MutableLiveData<Integer> mFlySpeed;
|
||||
private final MutableLiveData<Integer> mSwimSpeed;
|
||||
private final MutableLiveData<Integer> mStrength;
|
||||
private final MutableLiveData<Integer> mDexterity;
|
||||
private final MutableLiveData<Integer> mConstitution;
|
||||
private final MutableLiveData<Integer> mIntelligence;
|
||||
private final MutableLiveData<Integer> mWisdom;
|
||||
private final MutableLiveData<Integer> mCharisma;
|
||||
private final MutableLiveData<String> mName;
|
||||
private final MutableLiveData<String> mErrorMessage;
|
||||
private final MutableLiveData<String> mSize;
|
||||
@@ -69,6 +75,13 @@ public class EditMonsterViewModel extends ViewModel {
|
||||
mCanHover = new MutableLiveData<>(false);
|
||||
mHasCustomSpeed = new MutableLiveData<>(false);
|
||||
mCustomSpeed = new MutableLiveData<>("");
|
||||
mStrength = new MutableLiveData<>(10);
|
||||
mDexterity = new MutableLiveData<>(10);
|
||||
mConstitution = new MutableLiveData<>(10);
|
||||
mIntelligence = new MutableLiveData<>(10);
|
||||
mWisdom = new MutableLiveData<>(10);
|
||||
mCharisma = new MutableLiveData<>(10);
|
||||
|
||||
// TODO: consider initializing this to true so all new monsters need saving
|
||||
mHasChanges = new MutableLiveData<>(false);
|
||||
}
|
||||
@@ -97,6 +110,12 @@ public class EditMonsterViewModel extends ViewModel {
|
||||
mCanHover.setValue(monster.canHover);
|
||||
mHasCustomSpeed.setValue(monster.hasCustomSpeed);
|
||||
mCustomSpeed.setValue(monster.customSpeedDescription);
|
||||
mStrength.setValue(monster.strengthScore);
|
||||
mDexterity.setValue(monster.dexterityScore);
|
||||
mConstitution.setValue(monster.constitutionScore);
|
||||
mIntelligence.setValue(monster.intelligenceScore);
|
||||
mWisdom.setValue(monster.wisdomScore);
|
||||
mCharisma.setValue(monster.charismaScore);
|
||||
|
||||
mHasChanges.setValue(false);
|
||||
}
|
||||
@@ -336,7 +355,6 @@ public class EditMonsterViewModel extends ViewModel {
|
||||
if (!Objects.equals(mWalkSpeed.getValue(), walkSpeed)) {
|
||||
mWalkSpeed.setValue(walkSpeed);
|
||||
mHasChanges.setValue(true);
|
||||
Logger.logDebug(String.format("Setting walk speed to %d ft.", walkSpeed));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -457,6 +475,119 @@ public class EditMonsterViewModel extends ViewModel {
|
||||
setSwimSpeed(mSwimSpeed.getValue() - 5);
|
||||
}
|
||||
|
||||
public LiveData<Integer> getStrength() {
|
||||
return mStrength;
|
||||
}
|
||||
|
||||
public void setStrength(int strength) {
|
||||
if (!Objects.equals(mStrength.getValue(), strength)) {
|
||||
mStrength.setValue(strength);
|
||||
mHasChanges.setValue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void incrementStrength() {
|
||||
setStrength(mStrength.getValue() + 1);
|
||||
}
|
||||
|
||||
public void decrementStrength() {
|
||||
setStrength(mStrength.getValue() - 1);
|
||||
}
|
||||
|
||||
public LiveData<Integer> getDexterity() {
|
||||
return mDexterity;
|
||||
}
|
||||
|
||||
public void setDexterity(int dexterity) {
|
||||
if (!Objects.equals(mDexterity.getValue(), dexterity)) {
|
||||
mDexterity.setValue(dexterity);
|
||||
mHasChanges.setValue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void incrementDexterity() {
|
||||
setDexterity(mDexterity.getValue() + 1);
|
||||
}
|
||||
|
||||
public void decrementDexterity() {
|
||||
setDexterity(mDexterity.getValue() - 1);
|
||||
}
|
||||
|
||||
public LiveData<Integer> getConstitution() {
|
||||
return mConstitution;
|
||||
}
|
||||
|
||||
public void setConstitution(int constitution) {
|
||||
if (!Objects.equals(mConstitution.getValue(), constitution)) {
|
||||
mConstitution.setValue(constitution);
|
||||
mHasChanges.setValue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void incrementConstitution() {
|
||||
setConstitution(mConstitution.getValue() + 1);
|
||||
}
|
||||
|
||||
public void decrementConstitution() {
|
||||
setConstitution(mConstitution.getValue() - 1);
|
||||
}
|
||||
|
||||
public LiveData<Integer> getIntelligence() {
|
||||
return mIntelligence;
|
||||
}
|
||||
|
||||
public void setIntelligence(int intelligence) {
|
||||
if (!Objects.equals(mIntelligence.getValue(), intelligence)) {
|
||||
mIntelligence.setValue(intelligence);
|
||||
mHasChanges.setValue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void incrementIntelligence() {
|
||||
setIntelligence(mIntelligence.getValue() + 1);
|
||||
}
|
||||
|
||||
public void decrementIntelligence() {
|
||||
setIntelligence(mIntelligence.getValue() - 1);
|
||||
}
|
||||
|
||||
public LiveData<Integer> getWisdom() {
|
||||
return mWisdom;
|
||||
}
|
||||
|
||||
public void setWisdom(int wisdom) {
|
||||
if (!Objects.equals(mWisdom.getValue(), wisdom)) {
|
||||
mWisdom.setValue(wisdom);
|
||||
mHasChanges.setValue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void incrementWisdom() {
|
||||
setWisdom(mWisdom.getValue() + 1);
|
||||
}
|
||||
|
||||
public void decrementWisdom() {
|
||||
setWisdom(mWisdom.getValue() - 1);
|
||||
}
|
||||
|
||||
public LiveData<Integer> getCharisma() {
|
||||
return mCharisma;
|
||||
}
|
||||
|
||||
public void setCharisma(int charisma) {
|
||||
if (!Objects.equals(mCharisma.getValue(), charisma)) {
|
||||
mCharisma.setValue(charisma);
|
||||
mHasChanges.setValue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void incrementCharisma() {
|
||||
setCharisma(mCharisma.getValue() + 1);
|
||||
}
|
||||
|
||||
public void decrementCharisma() {
|
||||
setCharisma(mCharisma.getValue() - 1);
|
||||
}
|
||||
|
||||
public Monster buildMonster() {
|
||||
Monster monster = new Monster();
|
||||
@@ -482,6 +613,12 @@ public class EditMonsterViewModel extends ViewModel {
|
||||
monster.canHover = mCanHover.getValue();
|
||||
monster.hasCustomSpeed = mHasCustomSpeed.getValue();
|
||||
monster.customSpeedDescription = mCustomSpeed.getValue();
|
||||
monster.strengthScore = mStrength.getValue();
|
||||
monster.dexterityScore = mDexterity.getValue();
|
||||
monster.constitutionScore = mConstitution.getValue();
|
||||
monster.intelligenceScore = mIntelligence.getValue();
|
||||
monster.wisdomScore = mWisdom.getValue();
|
||||
monster.charismaScore = mCharisma.getValue();
|
||||
|
||||
return monster;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user