From dc1700f0ff66102c16a0c9eeb6f8bcc326d36b60 Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 3 Jan 2018 23:12:46 -0500 Subject: [PATCH] Chat implementation, currently buggy --- core/assets/ui/uiskin.json | 6 +++ core/src/io/anuke/mindustry/core/Control.java | 6 ++- .../io/anuke/mindustry/core/NetClient.java | 6 ++- .../io/anuke/mindustry/core/NetServer.java | 2 - core/src/io/anuke/mindustry/core/UI.java | 12 +++++- .../mindustry/ui/fragments/ChatFragment.java | 42 ++++++++++++------- .../ui/fragments/PlacementFragment.java | 3 ++ .../ui/fragments/PlayerListFragment.java | 3 ++ 8 files changed, 60 insertions(+), 20 deletions(-) diff --git a/core/assets/ui/uiskin.json b/core/assets/ui/uiskin.json index ab6dec7b92..57191fc3f0 100644 --- a/core/assets/ui/uiskin.json +++ b/core/assets/ui/uiskin.json @@ -5,6 +5,11 @@ com.badlogic.gdx.graphics.g2d.BitmapFont: { markupEnabled: true, scale: 0.5 }, + default-font-chat: { + file: square.fnt, + markupEnabled: false, + scale: 0.5 + }, title: { file: title.fnt, markupEnabled: true, @@ -30,6 +35,7 @@ io.anuke.ucore.scene.Skin$TintedDrawable: { dialogDim: {name: white, color: {r: 0, g: 0, b: 0, a: 0.9} }, invis: {name: white, color: {r: 0, g: 0, b: 0, a: 0} } loadDim: {name: white, color: {r: 0, g: 0, b: 0, a: 0.8} }, + chatfield: {name: white, color: {r: 0, g: 0, b: 0, a: 0.2}}, clear: {name: white, color: {r: 0.1, g: 0.1, b: 0.1, a: 0.75}}, clear-over: {name: white, color: {r: 1, g: 1, b: 1, a: 0.2} }, clear-down: {name: white, color: {r: 1, g: 1, b: 1, a: 0.4} } diff --git a/core/src/io/anuke/mindustry/core/Control.java b/core/src/io/anuke/mindustry/core/Control.java index cf06fe73b3..e969fe4de4 100644 --- a/core/src/io/anuke/mindustry/core/Control.java +++ b/core/src/io/anuke/mindustry/core/Control.java @@ -148,6 +148,8 @@ public class Control extends Module{ "dash", Input.SHIFT_LEFT, "rotate_alt", new Axis(Input.R, Input.E), "rotate", new Axis(Input.SCROLL), + "player_list", Input.TAB, + "chat", Input.ENTER, "weapon_1", Input.NUM_1, "weapon_2", Input.NUM_2, "weapon_3", Input.NUM_3, @@ -172,6 +174,7 @@ public class Control extends Module{ "dash", Input.CONTROLLER_Y, "rotate_alt", new Axis(Input.UNSET), "rotate", new Axis(Input.CONTROLLER_A, Input.CONTROLLER_B), + "player_list", Input.CONTROLLER_START, "weapon_1", Input.NUM_1, "weapon_2", Input.NUM_2, "weapon_3", Input.NUM_3, @@ -256,7 +259,8 @@ public class Control extends Module{ //multiplying by 2 so you start with more time in the beginning wavetime = waveSpacing()*2; - + + //hacky, but I doubt anyone will use this many resources if(mode.infiniteResources){ Arrays.fill(items, 999999999); } diff --git a/core/src/io/anuke/mindustry/core/NetClient.java b/core/src/io/anuke/mindustry/core/NetClient.java index 2806746d89..d66998ca4c 100644 --- a/core/src/io/anuke/mindustry/core/NetClient.java +++ b/core/src/io/anuke/mindustry/core/NetClient.java @@ -261,7 +261,7 @@ public class NetClient extends Module { }); Net.handle(ChatPacket.class, packet -> { - //TODO + Gdx.app.postRunnable(() -> Vars.ui.addChatMessage(packet.name, packet.text)); }); Net.handle(KickPacket.class, packet -> { @@ -285,6 +285,10 @@ public class NetClient extends Module { ChatPacket packet = new ChatPacket(); packet.text = message; Net.send(packet, SendMode.tcp); + + if(Net.server()){ + Vars.ui.addChatMessage(Vars.player.name, packet.text); + } } public void handleShoot(Weapon weapon, float x, float y, float angle){ diff --git a/core/src/io/anuke/mindustry/core/NetServer.java b/core/src/io/anuke/mindustry/core/NetServer.java index daf67f53ce..40c448657e 100644 --- a/core/src/io/anuke/mindustry/core/NetServer.java +++ b/core/src/io/anuke/mindustry/core/NetServer.java @@ -157,8 +157,6 @@ public class NetServer extends Module{ if(player == null) return; //GHOSTS AAAA - //TODO add to chat fragment - packet.name = player.name; Net.send(packet, SendMode.tcp); }); diff --git a/core/src/io/anuke/mindustry/core/UI.java b/core/src/io/anuke/mindustry/core/UI.java index 1a1a9c3656..f541f32045 100644 --- a/core/src/io/anuke/mindustry/core/UI.java +++ b/core/src/io/anuke/mindustry/core/UI.java @@ -59,7 +59,9 @@ public class UI extends SceneModule{ toolfrag = new ToolFragment(), hudfrag = new HudFragment(), placefrag = new PlacementFragment(), - weaponfrag = new WeaponFragment(); + weaponfrag = new WeaponFragment(), + chatfrag = new ChatFragment(), + listfrag = new PlayerListFragment(); public UI() { Dialog.setShowAction(()-> sequence( @@ -309,10 +311,18 @@ public class UI extends SceneModule{ toolfrag.build(); + chatfrag.build(); + + listfrag.build(); + updateItems(); build.end(); } + + public void addChatMessage(String sender, String message){ + ((ChatFragment)chatfrag).addMessage(message, sender); + } public void showGameError(){ gameerror.show(); diff --git a/core/src/io/anuke/mindustry/ui/fragments/ChatFragment.java b/core/src/io/anuke/mindustry/ui/fragments/ChatFragment.java index 3cafff47ac..f55bc4e3c9 100644 --- a/core/src/io/anuke/mindustry/ui/fragments/ChatFragment.java +++ b/core/src/io/anuke/mindustry/ui/fragments/ChatFragment.java @@ -8,7 +8,10 @@ import com.badlogic.gdx.graphics.g2d.GlyphLayout; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Array; import io.anuke.mindustry.Vars; +import io.anuke.mindustry.net.Net; import io.anuke.ucore.core.Core; +import io.anuke.ucore.core.Inputs; +import io.anuke.ucore.core.Timers; import io.anuke.ucore.scene.Scene; import io.anuke.ucore.scene.ui.Label; import io.anuke.ucore.scene.ui.Label.LabelStyle; @@ -18,8 +21,10 @@ import io.anuke.ucore.scene.ui.layout.Table; import static io.anuke.ucore.core.Core.scene; import static io.anuke.ucore.core.Core.skin; +//TODO show chat even when not toggled public class ChatFragment extends Table implements Fragment{ private final static int messagesShown = 10; + private final static int maxLength = 150; private Array messages = new Array<>(); private float fadetime; private float lastfadetime; @@ -30,14 +35,21 @@ public class ChatFragment extends Table implements Fragment{ private GlyphLayout layout = new GlyphLayout(); private float offsetx = 4, offsety = 4, fontoffsetx = 2, chatspace = 50; private float textWidth = 600; - private Color shadowColor = new Color(0,0,0,0.4f); + private Color shadowColor = new Color(0, 0, 0, 0.4f); private float textspacing = 10; public ChatFragment(){ super(); setFillParent(true); - font = Core.skin.getFont("pixel-font"); + font = Core.skin.getFont("default-font"); + + //TODO put it input + update(() -> { + if(Net.active() && Inputs.keyTap("chat")){ + toggle(); + } + }); setup(); } @@ -51,12 +63,12 @@ public class ChatFragment extends Table implements Fragment{ fieldlabel.setStyle(new LabelStyle(fieldlabel.getStyle())); fieldlabel.getStyle().font = font; fieldlabel.setStyle(fieldlabel.getStyle()); - fieldlabel.setFontScale(2); - chatfield = new TextField("", new TextField.TextFieldStyle(skin.get(TextField.TextFieldStyle.class))); - chatfield.getStyle().background = skin.getDrawable("blank"); + chatfield = new TextField("", new TextField.TextFieldStyle(skin.get(TextField.TextFieldStyle.class))); + chatfield.setTextFieldFilter((field, c) -> field.getText().length() < maxLength); + chatfield.getStyle().background = skin.getDrawable("chatfield"); chatfield.getStyle().fontColor = Color.WHITE; - chatfield.getStyle().font = skin.getFont("pixel-font-nomarkup"); + chatfield.getStyle().font = skin.getFont("default-font-chat"); bottom().left().marginBottom(offsety).marginLeft(offsetx*2).add(fieldlabel).padBottom(4f); @@ -67,16 +79,15 @@ public class ChatFragment extends Table implements Fragment{ public void draw(Batch batch, float alpha){ batch.setColor(shadowColor); + if(chatOpen) batch.draw(skin.getRegion("white"), offsetx, chatfield.getY(), Gdx.graphics.getWidth()-offsetx*2, chatfield.getHeight()-1); - font.getData().setScale(2f); font.getData().down = -21.5f; font.getData().lineHeight = 22f; super.draw(batch, alpha); - float spacing = chatspace; chatfield.setVisible(chatOpen); @@ -89,15 +100,14 @@ public class ChatFragment extends Table implements Fragment{ 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 == 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){ font.getCache().setAlphas(fadetime-i); - batch.setColor(0,0,0,shadowColor.a*(fadetime-i)); - + batch.setColor(0, 0, 0, shadowColor.a*(fadetime-i)); } batch.draw(skin.getRegion("white"), offsetx, theight-layout.height+1-4, textWidth, layout.height+textspacing); @@ -109,7 +119,7 @@ public class ChatFragment extends Table implements Fragment{ batch.setColor(Color.WHITE); if(fadetime > 0 && !chatOpen) - fadetime -= Gdx.graphics.getDeltaTime()*60f/120f; + fadetime -= Timers.delta()/120f; } private void sendMessage(){ @@ -118,6 +128,8 @@ public class ChatFragment extends Table implements Fragment{ if(message.replaceAll(" ", "").isEmpty()) return; + + Vars.netClient.handleSendMessage(message); } @@ -128,12 +140,12 @@ public class ChatFragment extends Table implements Fragment{ scene.setKeyboardFocus(chatfield); chatOpen = !chatOpen; lastfadetime = fadetime; - fadetime = messagesShown+1; + fadetime = messagesShown + 1; }else if(chatOpen){ scene.setKeyboardFocus(null); chatOpen = !chatOpen; sendMessage(); - fadetime = lastfadetime; + fadetime = messagesShown + 1; //TODO? } } @@ -145,7 +157,7 @@ public class ChatFragment extends Table implements Fragment{ messages.insert(0, new ChatMessage(message, sender)); fadetime += 1f; - fadetime = Math.min(fadetime, messagesShown)+2f; + fadetime = Math.min(fadetime, messagesShown) + 2f; } private static class ChatMessage{ diff --git a/core/src/io/anuke/mindustry/ui/fragments/PlacementFragment.java b/core/src/io/anuke/mindustry/ui/fragments/PlacementFragment.java index 81c4611f6c..5de804e180 100644 --- a/core/src/io/anuke/mindustry/ui/fragments/PlacementFragment.java +++ b/core/src/io/anuke/mindustry/ui/fragments/PlacementFragment.java @@ -3,6 +3,7 @@ package io.anuke.mindustry.ui.fragments; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.math.Interpolation; import com.badlogic.gdx.utils.Align; +import io.anuke.mindustry.Vars; import io.anuke.mindustry.core.GameState; import io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.input.InputHandler; @@ -24,6 +25,8 @@ public class PlacementFragment implements Fragment{ Table breaktable, next; public void build(){ + if(!Vars.android) return; + InputHandler input = control.getInput(); float s = 50f; diff --git a/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java b/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java index 6e3fe1a9a7..40ee3cc2ff 100644 --- a/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java +++ b/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java @@ -5,6 +5,7 @@ import io.anuke.mindustry.entities.Player; import io.anuke.mindustry.net.Net; import io.anuke.mindustry.ui.BorderImage; import io.anuke.ucore.core.Draw; +import io.anuke.ucore.core.Inputs; import io.anuke.ucore.scene.builders.label; import io.anuke.ucore.scene.builders.table; import io.anuke.ucore.scene.ui.layout.Table; @@ -21,6 +22,8 @@ public class PlayerListFragment implements Fragment{ row(); add(content).grow(); }}.end(); + + visible(() -> Inputs.keyDown("player_list")); }}.end(); rebuild();