Various minor UI/Net changes

This commit is contained in:
Anuken
2018-01-02 20:45:13 -05:00
parent 2796ab9801
commit c23743ee45
11 changed files with 240 additions and 36 deletions

View File

@@ -24,7 +24,6 @@ import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.resource.ItemStack;
import io.anuke.mindustry.resource.Weapon;
import io.anuke.mindustry.world.*;
import io.anuke.ucore.UCore;
import io.anuke.ucore.core.*;
import io.anuke.ucore.core.Inputs.Axis;
import io.anuke.ucore.core.Inputs.DeviceType;
@@ -33,7 +32,6 @@ import io.anuke.ucore.entities.EntityGroup;
import io.anuke.ucore.graphics.Atlas;
import io.anuke.ucore.modules.Module;
import io.anuke.ucore.scene.ui.layout.Unit;
import io.anuke.ucore.util.Bundles;
import io.anuke.ucore.util.Input;
import io.anuke.ucore.util.InputProxy;
import io.anuke.ucore.util.Mathf;
@@ -76,7 +74,6 @@ public class Control extends Module{
private InputProxy proxy;
private float controlx, controly;
private boolean controlling;
private Map map;
public Control(){
if(Mindustry.args.contains("-debug", false))
@@ -302,9 +299,7 @@ public class Control extends Module{
Timers.run(18, ()-> ui.hideLoading());
this.map = map;
Mindustry.platforms.onSceneChange(Bundles.get("text.playing", "Playing on map") + ": " + map.name, Bundles.get("text.wavenumber", "Wave") + " 0", "fight");
Mindustry.platforms.onSceneChange("Playing on map: " + map.name, "Wave 0", "fight");
}
public GameMode getMode(){
@@ -335,7 +330,7 @@ public class Control extends Module{
}
public void runWave(){
Mindustry.platforms.onSceneChange(Bundles.get("text.playing", "Playing on map") + ": " + map.name, Bundles.get("text.wavenumber", "Wave") + " " + wave, "fight");
if(Net.client() && Net.active()){
return;
}
@@ -390,20 +385,9 @@ public class Control extends Module{
wavetime = waveSpacing();
extrawavetime = maxwavespace;
}
void printEnemies(int wave){
int total = 0;
for(EnemySpawn spawn : spawns){
int spawnamount = spawn.evaluate(wave, 0);
total += spawnamount;
if(spawnamount > 0){
UCore.log(ClassReflection.getSimpleName(spawn.type) + " t" + spawn.tier(wave, 0) + " x" + spawnamount);
}
}
UCore.log("Total: " + total);
Mindustry.platforms.onSceneChange("Playing on map: "
+ Vars.world.getMap().name, "Wave " + wave, "fight");
}
public void enemyDeath(){

View File

@@ -260,6 +260,10 @@ public class NetClient extends Module {
Net.handle(Player.class, player -> {
player.add();
});
Net.handle(ChatPacket.class, packet -> {
//TODO
});
}
public void update(){
@@ -272,6 +276,12 @@ public class NetClient extends Module {
}
}
public void handleSendMessage(String message){
ChatPacket packet = new ChatPacket();
packet.text = message;
Net.send(packet, SendMode.tcp);
}
public void handleShoot(Weapon weapon, float x, float y, float angle){
ShootPacket packet = new ShootPacket();
packet.weaponid = (byte)weapon.ordinal();

View File

@@ -150,6 +150,18 @@ public class NetServer extends Module{
Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp);
});
Net.handleServer(ChatPacket.class, packet -> {
Player player = connections.get(Net.getLastConnection());
if(player == null)
return; //GHOSTS AAAA
//TODO add to chat fragment
packet.name = player.name;
Net.send(packet, SendMode.tcp);
});
}
//TODO decide whether to use effects

View File

@@ -312,7 +312,6 @@ public class UI extends SceneModule{
updateItems();
build.end();
}
public void showGameError(){

View File

@@ -56,8 +56,13 @@ public class MenuDialog extends FloatingDialog{
content().row();
content().addButton("$text.hostserver", () -> ui.showHostServer())
.disabled(b -> Net.active() || (Net.active() && !Net.server()));
content().addButton("$text.hostserver", () ->{
if(Vars.world.getMap().custom){
ui.showError("$text.nohost");
}else {
ui.showHostServer();
}
}).disabled(b -> Net.active() || (Net.active() && !Net.server()));
content().row();

View File

@@ -1,9 +1,167 @@
package io.anuke.mindustry.ui.fragments;
public class ChatFragment implements Fragment {
//TODO
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
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.ucore.core.Core;
import io.anuke.ucore.scene.Scene;
import io.anuke.ucore.scene.ui.Label;
import io.anuke.ucore.scene.ui.Label.LabelStyle;
import io.anuke.ucore.scene.ui.TextField;
import io.anuke.ucore.scene.ui.layout.Table;
import static io.anuke.ucore.core.Core.scene;
import static io.anuke.ucore.core.Core.skin;
public class ChatFragment extends Table implements Fragment{
private final static int messagesShown = 10;
private Array<ChatMessage> messages = new Array<>();
private float fadetime;
private float lastfadetime;
private boolean chatOpen = false;
private TextField chatfield;
private Label fieldlabel = new Label(">");
private BitmapFont font;
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 float textspacing = 10;
public ChatFragment(){
super();
setFillParent(true);
font = Core.skin.getFont("pixel-font");
setup();
}
@Override
public void build() {
scene.add(this);
}
private void setup(){
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.getStyle().fontColor = Color.WHITE;
chatfield.getStyle().font = skin.getFont("pixel-font-nomarkup");
bottom().left().marginBottom(offsety).marginLeft(offsetx*2).add(fieldlabel).padBottom(4f);
add(chatfield).padBottom(offsety).padLeft(offsetx).growX().padRight(offsetx).height(28);
}
@Override
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);
fieldlabel.setVisible(chatOpen);
batch.setColor(shadowColor);
float theight = offsety + spacing;
for(int i = 0; i < messagesShown && i < messages.size && i < fadetime; i ++){
layout.setText(font, messages.get(i).formattedMessage, Color.WHITE, textWidth, Align.bottomLeft, true);
theight += layout.height+textspacing;
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.draw(skin.getRegion("white"), offsetx, theight-layout.height+1-4, textWidth, layout.height+textspacing);
batch.setColor(shadowColor);
font.getCache().draw(batch);
}
batch.setColor(Color.WHITE);
if(fadetime > 0 && !chatOpen)
fadetime -= Gdx.graphics.getDeltaTime()*60f/120f;
}
private void sendMessage(){
String message = chatfield.getText();
chatfield.clearText();
if(message.replaceAll(" ", "").isEmpty()) return;
Vars.netClient.handleSendMessage(message);
}
public void toggle(){
Scene scene = getScene();
if(!chatOpen && (scene.getKeyboardFocus() == null || !scene.getKeyboardFocus().getParent().isVisible())){
scene.setKeyboardFocus(chatfield);
chatOpen = !chatOpen;
lastfadetime = fadetime;
fadetime = messagesShown+1;
}else if(chatOpen){
scene.setKeyboardFocus(null);
chatOpen = !chatOpen;
sendMessage();
fadetime = lastfadetime;
}
}
public boolean chatOpen(){
return chatOpen;
}
public void addMessage(String message, String sender){
messages.insert(0, new ChatMessage(message, sender));
fadetime += 1f;
fadetime = Math.min(fadetime, messagesShown)+2f;
}
private static class ChatMessage{
public final String sender;
public final String message;
public final String formattedMessage;
public ChatMessage(String message, String sender){
this.message = message;
this.sender = sender;
if(sender == null){ //no sender, this is a server message
formattedMessage = message;
}else{
formattedMessage = "[ROYAL]["+sender+"]: [YELLOW]"+message;
}
}
}
}