Created
August 18, 2020 13:17
-
-
Save Mr00Anderson/986341d4229e2fa06879c57ff2a64805 to your computer and use it in GitHub Desktop.
Editable label
editable label
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.virtual_hex.gdx.engine.scene2d; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.Input; | |
import com.badlogic.gdx.scenes.scene2d.Event; | |
import com.badlogic.gdx.scenes.scene2d.InputEvent; | |
import com.badlogic.gdx.scenes.scene2d.ui.Label; | |
import com.badlogic.gdx.scenes.scene2d.ui.Table; | |
import com.badlogic.gdx.scenes.scene2d.ui.TextField; | |
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; | |
import com.badlogic.gdx.scenes.scene2d.utils.FocusListener; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class EditableLabel { | |
public String label; | |
public Table rootTable; | |
public Label labelWidget; | |
public TextField textFieldWidget; | |
public transient List<ChangeNotifier> changeNotifierList = new ArrayList<>(); | |
public EditableLabel() { | |
} | |
public EditableLabel(String label, Table rootTable, Label labelWidget, TextField textFieldWidget) { | |
this.label = label; | |
this.rootTable = rootTable; | |
this.labelWidget = labelWidget; | |
this.textFieldWidget = textFieldWidget; | |
this.rootTable.add(labelWidget); | |
setText(label); | |
} | |
public void init(){ | |
textFieldWidget.addListener(new FocusListener() { | |
@Override | |
public boolean handle(Event event) { | |
if(event instanceof InputEvent){ | |
InputEvent inputEvent = (InputEvent) event; | |
switch (inputEvent.getType()){ | |
case keyDown:{ | |
if(Gdx.input.isKeyJustPressed(Input.Keys.ENTER)){ | |
label = textFieldWidget.getText(); | |
labelWidget.setText(label); | |
rootTable.clearChildren(); | |
rootTable.add(labelWidget); | |
System.out.println(event); | |
return true; | |
} | |
} | |
} | |
} | |
return super.handle(event); | |
} | |
}); | |
this.labelWidget.addListener(new ClickListener(){ | |
@Override | |
public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttonInt) { | |
rootTable.clearChildren(); | |
rootTable.add(textFieldWidget); | |
System.out.println(event); | |
return true; | |
} | |
}); | |
} | |
public List<ChangeNotifier> getChangeNotifierList() { | |
return changeNotifierList; | |
} | |
public String getLabel() { | |
return label; | |
} | |
public Table getRootTable() { | |
return rootTable; | |
} | |
public Label getLabelWidget() { | |
return labelWidget; | |
} | |
public TextField getTextFieldWidget() { | |
return textFieldWidget; | |
} | |
public String getText() { | |
return label; | |
} | |
public void setText(String jointText) { | |
this.label = jointText; | |
this.labelWidget.setText(jointText); | |
this.textFieldWidget.setText(jointText); | |
} | |
public interface ChangeNotifier { | |
void changed(String newText); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment