Added new choose dialog (creature types and etc):

* added incremental search;
 * added keyboard hotkeys (up/down for select, enter for choose);
 * added choose by mouse double click;
 * added source card name;
 * fixed cancel button on required choice (#4230);
 * fixed text sizes form messages (now it's auto size);
This commit is contained in:
Oleg Agafonov 2017-12-25 08:12:02 +04:00
parent 1efb51a22c
commit 749ca59ad6
7 changed files with 597 additions and 247 deletions

View file

@ -33,6 +33,7 @@ import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.effects.OneShotEffect;
import mage.choices.Choice;
import mage.choices.ChoiceCreatureType;
import mage.choices.ChoiceImpl;
import mage.constants.Outcome;
import mage.constants.SubType;
@ -63,9 +64,7 @@ public class ChooseCreatureTypeEffect extends OneShotEffect {
mageObject = game.getObject(source.getSourceId());
}
if (controller != null && mageObject != null) {
Choice typeChoice = new ChoiceImpl(true);
typeChoice.setMessage("Choose creature type");
typeChoice.setChoices(SubType.getCreatureTypes(false).stream().map(SubType::toString).collect(Collectors.toCollection(LinkedHashSet::new)));
Choice typeChoice = new ChoiceCreatureType(mageObject);
while (!controller.choose(outcome, typeChoice, game)) {
if (!controller.canRespond()) {
return false;

View file

@ -37,19 +37,34 @@ import java.util.Set;
*/
public interface Choice {
boolean isChosen();
boolean isRequired();
void clearChoice();
String getMessage();
void setMessage(String message);
void setChoice(String choice);
void setChoiceByKey(String choiceKey);
Set<String> getChoices();
Map<String,String> getKeyChoices();
void setChoices(Set<String> choices);
void setKeyChoices(Map<String, String> choices);
String getChoice();
String getChoiceKey();
boolean isKeyChoice();
String getSubMessage();
void setSubMessage(String subMessage);
void clearChoice();
boolean isChosen();
boolean isRequired();
Choice copy();
// string choice
void setChoices(Set<String> choices);
Set<String> getChoices();
void setChoice(String choice);
String getChoice();
// key-value choice
boolean isKeyChoice();
void setKeyChoices(Map<String, String> choices);
Map<String,String> getKeyChoices();
void setChoiceByKey(String choiceKey);
String getChoiceKey();
// search
boolean isSearchEnabled();
void setSearchEnabled(boolean isEnabled);
void setSearchText(String searchText);
String getSearchText();
}

View file

@ -1,5 +1,6 @@
package mage.choices;
import mage.MageObject;
import mage.constants.SubType;
import java.util.LinkedHashSet;
@ -7,10 +8,28 @@ import java.util.stream.Collectors;
public class ChoiceCreatureType extends ChoiceImpl {
private static String DEFAULT_MESSAGE = "Choose a creature type";
public ChoiceCreatureType() {
super(true);
this(true, DEFAULT_MESSAGE, null);
}
public ChoiceCreatureType(MageObject source) {
this(true, DEFAULT_MESSAGE, source);
}
public ChoiceCreatureType(String chooseMessage, MageObject source) {
this(true, chooseMessage, source);
}
public ChoiceCreatureType(boolean required, String chooseMessage, MageObject source){
super(required);
this.setChoices(SubType.getCreatureTypes(false).stream().map(SubType::toString).collect(Collectors.toCollection(LinkedHashSet::new)));
this.message = "Choose a creature type:";
this.setMessage(chooseMessage);
if(source != null) {
this.setSubMessage(source.getIdName());
}
this.setSearchEnabled(true);
}
public ChoiceCreatureType(final ChoiceCreatureType choice) {

View file

@ -47,6 +47,9 @@ public class ChoiceImpl implements Choice, Serializable {
protected Set<String> choices = new LinkedHashSet<>();
protected Map<String, String> keyChoices = new LinkedHashMap<>();
protected String message;
protected String subMessage;
protected boolean searchEnabled;
protected String searchText;
public ChoiceImpl() {
this(false);
@ -61,6 +64,7 @@ public class ChoiceImpl implements Choice, Serializable {
this.chosen = choice.chosen;
this.required = choice.required;
this.message = choice.message;
this.message = choice.subMessage;
this.choices.addAll(choice.choices);
this.choiceKey = choice.choiceKey;
this.keyChoices = choice.keyChoices; // list should never change for the same object so copy by reference
@ -88,6 +92,12 @@ public class ChoiceImpl implements Choice, Serializable {
this.message = message;
}
@Override
public String getSubMessage(){ return subMessage; }
@Override
public void setSubMessage(String subMessage){ this.subMessage = subMessage; }
@Override
public Set<String> getChoices() {
return choices;
@ -150,4 +160,24 @@ public class ChoiceImpl implements Choice, Serializable {
return !keyChoices.isEmpty();
}
}
@Override
public boolean isSearchEnabled(){
return this.searchEnabled;
};
@Override
public void setSearchEnabled(boolean isEnabled){
this.searchEnabled = isEnabled;
};
@Override
public void setSearchText(String searchText){
this.searchText = searchText;
};
@Override
public String getSearchText(){
return this.searchText;
};
}