Unit control mode

This commit is contained in:
Anuken
2022-02-10 17:36:34 -05:00
parent 1b8da1ff55
commit 907d2a46b2
3 changed files with 210 additions and 116 deletions
+29 -5
View File
@@ -115,6 +115,8 @@ public class DesktopInput extends InputHandler{
drawSelection(schemX, schemY, cursorX, cursorY, Vars.maxSchematicSize);
}
if(commandMode){
//draw command overlay UI
for(Unit unit : selectedUnits){
@@ -158,6 +160,8 @@ public class DesktopInput extends InputHandler{
Draw.color(Pal.accent, 0.3f);
Fill.crect(commandRectX, commandRectY, x2 - commandRectX, y2 - commandRectY);
}
}
Draw.reset();
}
@@ -271,12 +275,19 @@ public class DesktopInput extends InputHandler{
}
}
commandMode = input.keyDown(Binding.commandMode) && !locked && state.rules.unitCommand && block == null;
shouldShoot = !scene.hasMouse() && !locked;
if(!locked && state.rules.unitCommand && block == null){
if(input.keyTap(Binding.commandMode)){
commandMode = !commandMode;
}
}else{
commandMode = false;
}
//TODO should selected units be cleared out of command mode?
if(!commandMode){
selectedUnits.clear();
//selectedUnits.clear();
}
//validate commanding units
@@ -549,7 +560,8 @@ public class DesktopInput extends InputHandler{
if(multiSelect()){
//tiny brain method of unique addition
selectedUnits.removeAll(units);
}else if(units.size > 0){
}else{
//nothing selected, clear units
selectedUnits.clear();
}
selectedUnits.addAll(units);
@@ -673,7 +685,7 @@ public class DesktopInput extends InputHandler{
}
}
//TODO
//TODO when shift is held? ctrl?
public boolean multiSelect(){
return false;
}
@@ -684,6 +696,8 @@ public class DesktopInput extends InputHandler{
tappedOne = true;
//click: select a single unit
if(button == KeyCode.mouseLeft){
Unit unit = selectedCommandUnit(input.mouseWorldX(), input.mouseWorldY());
if(unit != null){
if(!multiSelect()){
@@ -696,7 +710,14 @@ public class DesktopInput extends InputHandler{
selectedUnits.add(unit);
}
}
}else if(selectedUnits.size > 0){
}else{
//deselect
selectedUnits.clear();
}
}else if(button == KeyCode.mouseRight){
//right click: move to position
if(selectedUnits.size > 0){
//move to location - TODO right click instead?
Vec2 target = input.mouseWorld().cpy();
@@ -714,6 +735,9 @@ public class DesktopInput extends InputHandler{
Call.commandUnits(player, ids, attack instanceof Building b ? b : null, attack instanceof Unit u ? u : null, target);
}
}
return super.tap(x, y, count, button);
}
+1
View File
@@ -17,6 +17,7 @@ public class ItemImage extends Stack{
add(new Table(t -> {
t.left().bottom();
//TODO outline? .style(Styles.outlineLabel)
t.add(amount > 1000 ? UI.formatAmount(amount) : amount + "");
t.pack();
}));
@@ -42,9 +42,10 @@ public class PlacementFragment extends Fragment{
Object lastDisplayState;
Team lastTeam;
boolean wasHovered;
Table blockTable, toggler, topTable;
Table blockTable, toggler, topTable, blockCatTable, commandTable;
Stack mainStack;
ScrollPane blockPane;
boolean blockSelectEnd;
boolean blockSelectEnd, wasCommandMode;
int blockSelectSeq;
long blockSelectSeqMillis;
Binding[] blockSelect = {
@@ -366,11 +367,76 @@ public class PlacementFragment extends Fragment{
hovered.display(topTable);
}
});
}).colspan(3).fillX().visible(this::hasInfoBox).touchable(Touchable.enabled);
frame.row();
frame.image().color(Pal.gray).colspan(3).height(4).growX();
frame.row();
frame.table(Tex.pane2, blocksSelect -> {
}).colspan(3).fillX().visible(this::hasInfoBox).touchable(Touchable.enabled).row();
frame.image().color(Pal.gray).colspan(3).height(4).growX().row();
blockCatTable = new Table();
commandTable = new Table(Tex.pane2);
mainStack = new Stack();
mainStack.update(() -> {
if(control.input.commandMode != wasCommandMode){
mainStack.clearChildren();
mainStack.addChild(control.input.commandMode ? commandTable : blockCatTable);
wasCommandMode = control.input.commandMode;
//hacky, but forces command table to be same width as blocks
if(wasCommandMode){
commandTable.getCells().peek().width(blockCatTable.getWidth());
}
}
});
frame.add(mainStack).colspan(3).fill();
//commandTable: commanded units
{
commandTable.touchable = Touchable.enabled;
commandTable.add("[accent]Command Mode").fill().center().labelAlign(Align.center).row();
commandTable.image().color(Pal.accent).growX().pad(20f).padTop(0f).padBottom(4f).row();
commandTable.table(u -> {
u.left();
int[] curCount = {0};
Runnable rebuildCommand = () -> {
u.clearChildren();
var units = control.input.selectedUnits;
if(units.size > 0){
int[] counts = new int[content.units().size];
for(var unit : units){
counts[unit.type.id] ++;
}
int col = 0;
for(int i = 0; i < counts.length; i++){
if(counts[i] > 0){
var type = content.unit(i);
u.add(new ItemImage(type.uiIcon, counts[i])).tooltip(type.localizedName).pad(4);
if(++col % 7 == 0){
u.row();
}
}
}
}else{
u.add("[no units]").color(Color.lightGray).growX().center().labelAlign(Align.center).pad(6);
}
};
u.update(() -> {
int size = control.input.selectedUnits.size;
if(curCount[0] != size){
curCount[0] = size;
rebuildCommand.run();
}
});
rebuildCommand.run();
}).grow();
}
//blockCatTable: all blocks | all categories
{
blockCatTable.table(Tex.pane2, blocksSelect -> {
blocksSelect.margin(4).marginTop(0);
blockPane = blocksSelect.pane(blocks -> blockTable = blocks).height(194f).update(pane -> {
if(pane.hasScroll()){
@@ -384,7 +450,7 @@ public class PlacementFragment extends Fragment{
blocksSelect.row();
blocksSelect.table(control.input::buildPlacementUI).name("inputTable").growX();
}).fillY().bottom().touchable(Touchable.enabled);
frame.table(categories -> {
blockCatTable.table(categories -> {
categories.bottom();
categories.add(new Image(Styles.black6){
@Override
@@ -428,6 +494,9 @@ public class PlacementFragment extends Fragment{
}).group(group).update(i -> i.setChecked(currentCategory == cat)).name("category-" + cat.name());
}
}).fillY().bottom().touchable(Touchable.enabled);
}
mainStack.add(blockCatTable);
rebuildCategory.run();
frame.update(() -> {