Make chat scrollable, other chat fixes

This commit is contained in:
iczero
2018-03-26 00:12:06 -07:00
parent e0c1bb8da1
commit 30fc96e4da
2 changed files with 17 additions and 16 deletions

View File

@ -26,8 +26,9 @@ public class DefaultKeybinds {
"block_info", Input.CONTROL_LEFT,
"player_list", Input.TAB,
"chat", Input.ENTER,
"chat_scroll_up", Input.UP,
"chat_scroll_down", Input.DOWN,
"chat_history_prev", Input.UP,
"chat_history_next", Input.DOWN,
"chat_scroll", new Axis(Input.SCROLL),
"console", Input.GRAVE,
"weapon_1", Input.NUM_1,
"weapon_2", Input.NUM_2,
@ -55,8 +56,9 @@ public class DefaultKeybinds {
"rotate", new Axis(Input.CONTROLLER_A, Input.CONTROLLER_B),
"player_list", Input.CONTROLLER_START,
"chat", Input.ENTER,
"chat_scroll_up", Input.UP,
"chat_scroll_down", Input.DOWN,
"chat_history_prev", Input.UP,
"chat_history_next", Input.DOWN,
"chat_scroll", new Axis(Input.SCROLL),
"console", Input.GRAVE,
"weapon_1", Input.NUM_1,
"weapon_2", Input.NUM_2,

View File

@ -19,8 +19,7 @@ import io.anuke.ucore.scene.ui.Label.LabelStyle;
import io.anuke.ucore.scene.ui.TextField;
import io.anuke.ucore.scene.ui.layout.Table;
import io.anuke.ucore.scene.ui.layout.Unit;
import io.anuke.ucore.util.Log;
import java.util.Arrays;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.state;
import static io.anuke.ucore.core.Core.scene;
@ -30,7 +29,7 @@ public class ChatFragment extends Table implements Fragment{
private final static int messagesShown = 10;
private final static int maxLength = 150;
private Array<ChatMessage> messages = new Array<>();
private float fadetime, lastfade;
private float fadetime;
private boolean chatOpen = false;
private TextField chatfield;
private Label fieldlabel = new Label(">");
@ -42,6 +41,7 @@ public class ChatFragment extends Table implements Fragment{
private float textspacing = Unit.dp.scl(10);
private Array<String> history = new Array<String>();
private int historyPos = 0;
private int scrollPos = 0;
public ChatFragment(){
super();
@ -62,15 +62,16 @@ public class ChatFragment extends Table implements Fragment{
}
if (chatOpen) {
if (Inputs.keyTap("chat_scroll_up") && historyPos < history.size - 1) {
if (Inputs.keyTap("chat_history_prev") && historyPos < history.size - 1) {
if (historyPos == 0) history.set(0, chatfield.getText());
historyPos++;
updateChat();
}
if (Inputs.keyTap("chat_scroll_down") && historyPos > 0) {
if (Inputs.keyTap("chat_history_next") && historyPos > 0) {
historyPos--;
updateChat();
}
scrollPos = (int)Mathf.clamp(scrollPos + Inputs.getAxis("chat_scroll"), 0, Math.max(0, messages.size - messagesShown));
}
});
@ -134,16 +135,16 @@ public class ChatFragment extends Table implements Fragment{
batch.setColor(shadowColor);
float theight = offsety + spacing + getMarginBottom();
for(int i = 0; i < messagesShown && i < messages.size && i < fadetime; i ++){
for(int i = scrollPos; i < messages.size && i < messagesShown + scrollPos && (i < fadetime || chatOpen); i++){
layout.setText(font, messages.get(i).formattedMessage, Color.WHITE, textWidth, Align.bottomLeft, true);
theight += layout.height+textspacing;
if(i == 0) theight -= textspacing+1;
if(i - scrollPos == 0) theight -= textspacing+1;
font.getCache().clear();
font.getCache().addText(messages.get(i).formattedMessage, fontoffsetx + offsetx, offsety + theight, textWidth, Align.bottomLeft, true);
if(fadetime-i < 1f && fadetime-i >= 0f){
if(!chatOpen && fadetime-i < 1f && fadetime-i >= 0f){
font.getCache().setAlphas(fadetime-i);
batch.setColor(0, 0, 0, shadowColor.a*(fadetime-i));
}
@ -163,10 +164,10 @@ public class ChatFragment extends Table implements Fragment{
private void sendMessage(){
String message = chatfield.getText();
clearChatInput();
history.insert(1, message);
if(message.replaceAll(" ", "").isEmpty()) return;
history.insert(1, message);
NetEvents.handleSendMessage(message);
}
@ -176,12 +177,10 @@ public class ChatFragment extends Table implements Fragment{
scene.setKeyboardFocus(chatfield);
chatfield.fireClick();
chatOpen = !chatOpen;
lastfade = fadetime;
fadetime = messagesShown + 1;
}else{
scene.setKeyboardFocus(null);
chatOpen = !chatOpen;
fadetime = lastfade;
scrollPos = 0;
sendMessage();
}
}