[QoL] Chat modes (#3869)

Credits: Remint32 (idea); sk7725 (korean i18n); Volas171 (spanish i18n).
This commit is contained in:
Antsiferov Andrew
2020-12-25 00:05:41 +03:00
committed by GitHub
parent 1e8621d9d4
commit 43757a247e
6 changed files with 67 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package mindustry.ui.fragments;
import arc.*;
import arc.Input.*;
import arc.func.*;
import arc.graphics.*;
import arc.graphics.g2d.*;
import arc.math.*;
@@ -14,6 +15,7 @@ import arc.util.*;
import mindustry.*;
import mindustry.gen.*;
import mindustry.input.*;
import mindustry.type.*;
import mindustry.ui.*;
import static arc.Core.*;
@@ -27,6 +29,7 @@ public class ChatFragment extends Table{
private boolean shown = false;
private TextField chatfield;
private Label fieldlabel = new Label(">");
private ChatMode mode = ChatMode.normal;
private Font font;
private GlyphLayout layout = new GlyphLayout();
private float offsetx = Scl.scl(4), offsety = Scl.scl(4), fontoffsetx = Scl.scl(2), chatspace = Scl.scl(50);
@@ -76,6 +79,9 @@ public class ChatFragment extends Table{
historyPos--;
updateChat();
}
if(input.keyTap(Binding.chat_mode)){
nextMode();
}
scrollPos = (int)Mathf.clamp(scrollPos + input.axis(Binding.chat_scroll), 0, Math.max(0, messages.size - messagesShown));
}
});
@@ -216,14 +222,35 @@ public class ChatFragment extends Table{
}
public void updateChat(){
chatfield.setText(history.get(historyPos));
chatfield.setCursorPosition(chatfield.getText().length());
chatfield.setText(mode.normalizedPrefix() + history.get(historyPos));
updateCursor();
}
public void nextMode(){
ChatMode prev = mode;
do{
mode = mode.next();
}while(!mode.isValid());
if(chatfield.getText().startsWith(prev.normalizedPrefix())){
chatfield.setText(mode.normalizedPrefix() + chatfield.getText().substring(prev.normalizedPrefix().length()));
}else{
chatfield.setText(mode.normalizedPrefix());
}
updateCursor();
}
public void clearChatInput(){
historyPos = 0;
history.set(0, "");
chatfield.setText("");
chatfield.setText(mode.normalizedPrefix());
updateCursor();
}
public void updateCursor(){
chatfield.setCursorPosition(chatfield.getText().length());
}
public boolean shown(){
@@ -256,4 +283,36 @@ public class ChatFragment extends Table{
}
}
private enum ChatMode{
normal(""),
team("/t"),
admin("/a", player::admin)
;
public String prefix;
public Boolp valid;
public static final ChatMode[] all = values();
ChatMode(String prefix){
this.prefix = prefix;
this.valid = () -> true;
}
ChatMode(String prefix, Boolp valid){
this.prefix = prefix;
this.valid = valid;
}
public ChatMode next(){
return all[(ordinal() + 1) % all.length];
}
public String normalizedPrefix(){
return prefix.isEmpty() ? "" : prefix + " ";
}
public boolean isValid(){
return valid.get();
}
}
}