i'm trying to make a void with a Void as an argument in libgdx,
how do i make it work?
this is what i have:
public class ReleaseDetector{
boolean Touched = false;
//
// --- Simple void for touch release detection, when looped. ---
// --- and the argument void, or how i imagine it..
//
public void ReleaseListener(void MyArgumentVoid)//<---The argument void
{
if (Gdx.input.isTouched()){
Touched = true;
}
if (!Gdx.input.isTouched() && Touched){
MyArgumentVoid();<----------// Call for a void from the Argument.
Touched = false;
}
}
}
Usage in MyGdxGame class, or how i imagine it:
public class MyGdxGame extends ApplicationAdapter {
int Counter = 0;
ReleaseDetector RD = new ReleaseDetector();
public void AddCounter(){// just a simple void.
Counter++;
}
#Override
public void render() { // Render Loop void.
RD.ReleaseListener(AddCounter);// how i imagine it's usage.
}
}
now, how am i making it real? i hope there is a simple way..
It seems that you need a callback method which takes no argument and returns void.
public class ReleaseDetector {
boolean touched = false;
public void releaseListener(MyGdxGame game) { // Argument
if (Gdx.input.isTouched()) {
touched = true;
}
if (!Gdx.input.isTouched() && touched){
game.addCounter(); // Callback
touched = false;
}
}
}
public class MyGdxGame extends ApplicationAdapter {
int counter = 0;
ReleaseDetector detector = new ReleaseDetector();
public void addCounter() {
counter++;
}
#Override
public void render() { // Render Loop.
detector.releaseListener(this);
}
}
Hope this helps.
Related
So I have a loop class that is basically as follows:
public class Loop extends Thread {
private boolean running;
public Loop() {
running = false;
}
public void run() {
while(running) {
//Do stuff
}
}
public void setRunning(boolean b) {
running = b;
}
}
What I'd like to know is whether or not it is possible to store methods. For example the class could look like this instead.
public class Loop extends Thread {
private boolean running;
private Method method;
public Loop() {
running = false;
}
public void run() {
while(running) {
if(method != null)
method.callMethod();
}
}
public void setRunning(boolean b) {
running = b;
}
public void setMethod(Method m) {
method = m;
}
}
Is anything like this possible?
I assume you want this functionality in Java 6, so you can use interface and anonymous class.
Interface code:
public interface Listener {
void callMethod();
}
Your Thread:
public class Loop extends Thread {
private boolean running;
private Listener listener;
public Loop() {
running = false;
}
public void run() {
while(running) {
if(listener != null)
listener.callMethod();
}
}
public void setRunning(boolean b) {
running = b;
}
public void setListener(Listener listener) {
this.listener = listener;
}
}
Set Listener:
Loop loop = new Loop();
loop.setListener(new Listener() {
#Override
public void callMethod() {
// Do stuff
}
});
This will work for your usecase. If you want to save methods and pass methods as data, you have to either use Java 8 (not supported on all Android API levels) or Kotlin.
I want to create efficienty timer in LibGDX framework, that will count the time left for my character. The doSmth() method should be called as many times as some flag is set to true. I know that the third parametr of Timer is that, how many times should it trigger. For now one Im calling the method recursive, but I dont think it is the efficient way.
public void updateTimer(){
new Timer().scheduleTask(new Timer.Task() {
#Override
public void run() {
doSmth();
updateTimer();
}
},1);
}
It would be more accurate to use a repeat count. Your method will introduce a bit of error each time the task is run, because the task is run on the GL thread, so it will occur just slightly after one second, but you are repeating it one second after that. So with each repeat you are slightly further behind.
private Timer.Task myTimerTask = new Timer.Task() {
#Override
public void run() {
doSmth();
}
};
public void startTimer(){
Timer.schedule(myTimerTask, 1f, 1f);
}
And when you need to stop it:
myTimerTask.cancel();
The com.badlogic.gdx.utils.Timer executes tasks in the future on the main loop thread,even if your game is in a pause screen, a menu or in another state, you can simply control time in the render method by adding delta time.
private float timeSeconds = 0f;
private float period = 1f;
public void render() {
//Execute handleEvent each 1 second
timeSeconds +=Gdx.graphics.getRawDeltaTime();
if(timeSeconds > period){
timeSeconds-=period;
handleEvent();
}
[...]
}
public void handleEvent() {
[...]
}
To keep organized i personally have an array on my main game class that holds all my timed events and process everything on the render cycle. In your case you can put some control variables as you wish.
my implementation example:
// MainGame.java
private ObjectMap<TimedEventEnum, TimedEvent> hshTimedEvent;
public void render(){
executeTimedEvents();
}
private void executeTimedEvents() {
for (ObjectMap.Entry<TimedEventEnum, TimedEvent> entry : hshTimedEvent) {
TimedEvent event = entry.value;
event.process();
}
}
public void killEvent(TimedEventEnum event) {
hshTimedEvent.remove(event);
}
// TimedEventEnum.java
public enum TimedEventEnum {
COUNT_MONEY,
CHECK_FOR_ACHIEVS,
ANOTHER_EVENT_EXAMPLE
}
//CountMoneyTimedEvent.java
public class CountMoneyTimedEvent extends Timer implements TimedEvent {
public CountMoneyTimedEvent() {
super();
init(this, 4f, false);
}
#Override
public void execute() {
//execute logic here
}
#Override
public void reset() {
this.timesFired = 0L;
}
}
//Timer.java
public abstract class Timer {
private Float deltaCount;
private Float timeToEvent;
private Boolean isRepeatable;
protected Long timesFired;
private TimedEvent event;
Timer() {
}
public void init(TimedEvent event, Float eventTime, Boolean isRepeatable) {
this.deltaCount = 0f;
this.timeToEvent = eventTime;
this.isRepeatable = isRepeatable;
this.timesFired = 0L;
this.event = event;
}
public void process() {
if (isEventTime()) {
event.execute();
}
}
private Boolean isEventTime() {
if (event != null && (isRepeatable || timesFired == 0)) {
deltaCount += Gdx.graphics.getRawDeltaTime();
if (deltaCount > timeToEvent) {
deltaCount -= timeToEvent;
timesFired++;
return true;
}
}
return false;
}
protected void executeNextEvent() {
deltaCount = timeToEvent;
}
}
// TimedEvent.java
public interface TimedEvent {
void execute();
void reset();
void process();
}
im currently trying to create a program that dynamically changes the text on JTextArea's and JButtons when they are pressed, or display a JOptionPane. At the moment nothing is happening when i press the buttons, they are not getting updated nor are they making a dialogue box appear.
Help is appreciated
private MillionaireGui view;
private Millionaire model;
private String question;
private int gameState;
private boolean isRunning;
public MillionaireController(MillionaireGui view, Millionaire model) {
this.view = view;
this.model = model;
this.question = null;
this.gameState = 0;
this.isRunning = true;
}
public void setModel(Millionaire model) {
this.model = model;
}
public void setView(MillionaireGui gui) {
this.view = gui;
}
public void getQuestion() {
question = model.getDeck().generateQuestion();
view.setQuestion(question);
}
public void update(){
while(isRunning){
if(gameState == 0){
getQuestion();
ArrayList<String> ans = model.getDeck().getAnswers();
view.setButtonA(ans.get(0));
view.setButtonB(ans.get(1));
view.setButtonC(ans.get(2));
view.setButtonD(ans.get(3));
gameState = 1;
}
if(gameState == 1){
if(view.getAnswer() != 0){
if(model.getDeck().isCorrect(view.getAnswer())){
view.dispCorrectAnswer();
view.setAnswer(0);
gameState = 0;
}
else {
gameState = 3;
}
}
}
if(gameState == 3){
isRunning = false;
view.displayErrorMsg();
}
}
}
#Override
public void run() {
update();
}
GUI:
public void setButtonB(String str){
buttonB.setText(str);
}
public void setButtonC(String str){
buttonC.setText(str);
}
public void setButtonD(String str){
buttonD.setText(str);
}
public void setAnswer(int num){
answer = num;
}
public String getQuestion(){
return question;
}
public void setQuestion(String str){
question = str;
questionField.setText(str);
}
MAIN:
public class Millionaire_main {
public Millionaire_main(){
}
public static void main(String[] args) {
MillionaireGui gui = new MillionaireGui();
QuestionDeck deck = new QuestionDeck();
Millionaire model = new Millionaire(deck);
MillionaireController control = new MillionaireController(gui, model);
gui.setVisible(true);
Thread thread = new Thread(control);
thread.start();
}
}
The code within the update() method seems to be running with a thread. What I think is happening is that you have 2 threads, one of which is doing some background task which causes the update. The background thread is not the EDT, thus any UI updates will not be visible.
Trying the approach below would fix the problem (most likely at least)
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run() {
view.setButtonA(ans.get(0));
view.setButtonB(ans.get(1));
view.setButtonC(ans.get(2));
view.setButtonD(ans.get(3));
}
});
The above should place your button settings events on the EDT, which should trigger the change.
Looks like maybe you just need to revalidate the container.
After you set all the button text fields, call gui.revalidate() to mark everything as invalid & validate. Here's more on the differences between those 3 methods
Also (as mentioned by #npinti) - I'm not sure exactly what you're doing with the extra thread, but be aware that modifying GUI components outside of the AWT thread is NOT a good idea
I am using the Document method createPosition to add Positions to an array. These Positions change depending on the user inserting/removing lines.
How do I hook changes in my array? I need to know the new value and the value which it has changed from.
Interface Position has no method to add listener to monitor position change. So you need do it manually using wrapper for Position object.
public class PositionHolder {
private int oldPosition;
private Position position;
private PropertyChangeSupport propertySupport = new PropertyChangeSupport(this);
public PositionHolder(Position aPos) {
position = aPos;
oldPosition = aPos.getOffset();
}
public void addPropertyChangeListener(PropertyChangeListener aListener) {
propertySupport.addPropertyChangeListener(aListener);
}
// same for remove listener
public void verifyPosition() {
// no custom if statement required - event is fiered only when old != new
propertySupport.firePropertyChangeEvent("position", oldPosition, position.getOffset());
oldPosition = position.getOffset();
}
public Position getPosition() {
return position;
}
}
public class PositionUpdater implements DocumentListener {
private List<PositionHolder> holders;
public PositionUpdater(List<PositionHolder> someHolders) {
holders = someHolders;
}
public void insertUpdate(DocumentEvent e) {
for (PositionHolder h : holders) {
h.verifyPosition();
}
}
public void removeUpdate(DocumentEvent e) {
insertUpdate(e);
}
public void changeUpdate(DocumentEvent e) {
insertUpdate(e);
}
}
JTextComponent textComp = // your text component
List<PositionHolder> holderList = new ArrayList<>(); // your list of positions
textComp.getDocument().addDocumentListener(new PositionUpdater(holderList));
Now you will get notification when position is changed. You only need to fill the holderList with wrappers for your positions and register your event listener on these wrappers.
I have a textual editor that extends AbstractTextEditor and I also have an Outline that needs to be saved when its content is modified by the user. I am currently using a Saveable which is added to the editor.
If the editor was marked as 'dirty' and it is saved, the Saveableis saved as well. However, if the Saveable's state changes to 'dirty', the * next to the file name does not appear. The save button in the top menu bar does show, but when I click it, nothing happens.
This is my implementation:
public class MyTextEditor extends AbstractTextEditor {
...
public void setOutlineSaveable(Saveable saveable) {
this.outlineSaveable = saveable;
ISaveablesLifecycleListener lifecycleListener = (ISaveablesLifecycleListener)getSite().getService(ISaveablesLifecycleListener.class);
lifecycleListener.handleLifecycleEvent( new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_OPEN, new Saveable[] {saveable}, false));
}
#Override
public Saveable[] getSaveables() {
if(outlineSaveable != null) {
// copy Saveables from super.getSaveables() to a new array
Saveable[] superSaveables = super.getSaveables();
Saveable[] res = new Saveable[superSaveables.length + 1];
int i = 0;
for(; i < superSaveables.length; i++) {
res[i] = superSaveables[i];
}
res[i] = outlineSaveable;
return res;
}
else
return super.getSaveables();
}
public void saveableDirty() {
firePropertyChange(PROP_DIRTY);
}
}
My ContentOutlinePage:
public class GraphicalOutlinePage extends ContentOutlinePage {
...
private GraphicalOutlineSaveable saveable;
public Saveable getSaveable() {
return saveable;
}
class GraphicalOutlineSaveable extends Saveable {
private boolean dirty = false;
private IEditorPart editor;
public GraphicalOutlineSaveable(IEditorPart editor) {
this.editor = editor;
}
#Override
public void doSave(IProgressMonitor monitor) throws CoreException {
viewer.doSave(monitor);
dirty = false;
}
#Override
public boolean equals(Object obj) {
System.err.println("GraphicalOutline.GraphicalOutlineSaveable.equals");
return obj instanceof GraphicalOutlineSaveable && ((Saveable)obj).getName() == getName();
}
#Override
public ImageDescriptor getImageDescriptor() {
return editor.getEditorInput().getImageDescriptor();
}
#Override
public String getName() {
return "Graphical Outline: " + editor.getEditorInput().getName();
}
#Override
public String getToolTipText() {
return "";
}
#Override
public boolean isDirty() {
System.err.println("GraphicalOutlinePage.GraphicalOutlineSaveable.isDirty: " + dirty);
return dirty;
}
public void setDirty() {
System.err.println("GraphicalOutlinePage.GraphicalOutlineSaveable.setDirty");
dirty = true;
// notify text editor about property change
if(editor instanceof AbstractTextEditor) {
((MyTextEditor)editor).saveableDirty();
}
}
#Override
public int hashCode() {
return viewer.hashCode();
}
}
}
vieweris a GraphicalViewerdisplayed in the ContentOutlinePage.
Somewhere in another class, I then call:
textEditor.setSaveable(grOutlinePage.getSaveable());
You may need to override the main editor isDirty() method and test each of the Saveable objects dirty flags.
It seems that the handling of multiple Saveables is not done as cleanly is it might have been.