Skip to content
This repository was archived by the owner on Jan 3, 2020. It is now read-only.

Commit af3068a

Browse files
committed
bug fix & rough sign support
1 parent b7e2e90 commit af3068a

File tree

7 files changed

+172
-63
lines changed

7 files changed

+172
-63
lines changed

src/main/java/org/devinprogress/YAIF/Bridges/CommonBridge.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ public ActionFeedback onUp(JTextField txt) {
4848
public ActionFeedback onDown(JTextField txt) {
4949
return null;
5050
}
51+
@Override
52+
public boolean sameAs(GuiScreen screen, GuiTextField txtField) {
53+
return txtField==txt;
54+
}
5155
}

src/main/java/org/devinprogress/YAIF/Bridges/DebugBridge.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ public ActionFeedback onUp(JTextField txt) {
4747
public ActionFeedback onDown(JTextField txt) {
4848
return null;
4949
}
50+
@Override
51+
public boolean sameAs(GuiScreen screen, GuiTextField txtField) {
52+
return false;
53+
}
5054
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.devinprogress.YAIF.Bridges;
2+
3+
import net.minecraft.client.gui.GuiScreen;
4+
import net.minecraft.client.gui.GuiTextField;
5+
import net.minecraft.client.gui.inventory.GuiEditSign;
6+
import net.minecraft.tileentity.TileEntitySign;
7+
import org.devinprogress.YAIF.InputFieldWrapper;
8+
9+
import javax.swing.*;
10+
import java.lang.reflect.Field;
11+
12+
/**
13+
* Created by recursiveg on 14-9-21.
14+
*/
15+
public class EditSignBridge implements IActionBridge {
16+
private GuiEditSign gui;
17+
private InputFieldWrapper wrapper;
18+
private int currentLine=0;
19+
private TileEntitySign sign;
20+
public EditSignBridge(GuiEditSign gui,InputFieldWrapper w){
21+
this.gui=gui;
22+
this.wrapper=w;
23+
currentLine=0;
24+
w.setTextNoEvent("");
25+
//TODO: USE AccessTransformer
26+
for(Field f:gui.getClass().getDeclaredFields())
27+
if(f.getType().equals(TileEntitySign.class)){
28+
try {
29+
f.setAccessible(true);
30+
sign = (TileEntitySign)f.get(gui);
31+
break;
32+
}catch(Exception e){
33+
e.printStackTrace();
34+
}
35+
}
36+
}
37+
@Override
38+
public ActionFeedback onEnter(JTextField txt) {
39+
currentLine=currentLine+1&3;
40+
wrapper.setTextNoEvent(sign.signText[currentLine]);
41+
return null;
42+
}
43+
44+
@Override
45+
public ActionFeedback onEsc(JTextField txt) {
46+
return ActionFeedback.Quit;
47+
}
48+
49+
@Override
50+
public ActionFeedback onChange(JTextField txt) {
51+
sign.signText[currentLine]=txt.getText();
52+
return null;
53+
}
54+
55+
@Override
56+
public ActionFeedback onTab(JTextField txt) {
57+
return null;
58+
}
59+
60+
@Override
61+
public ActionFeedback onUp(JTextField txt) {
62+
currentLine=currentLine-1&3;
63+
wrapper.setTextNoEvent(sign.signText[currentLine]);
64+
return null;
65+
}
66+
67+
@Override
68+
public ActionFeedback onDown(JTextField txt) {
69+
currentLine=currentLine+1&3;
70+
wrapper.setTextNoEvent(sign.signText[currentLine]);
71+
return null;
72+
}
73+
74+
@Override
75+
public boolean sameAs(GuiScreen screen, GuiTextField txtField) {
76+
return screen==gui;
77+
}
78+
}

src/main/java/org/devinprogress/YAIF/Bridges/GuiChatBridge.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import cpw.mods.fml.client.FMLClientHandler;
44
import net.minecraft.client.gui.GuiChat;
5+
import net.minecraft.client.gui.GuiScreen;
56
import net.minecraft.client.gui.GuiTextField;
67
import org.devinprogress.YAIF.InputFieldWrapper;
78
import org.devinprogress.YAIF.YetAnotherInputFix;
9+
import org.lwjgl.input.Keyboard;
810

911
import javax.swing.*;
1012
import java.lang.reflect.Field;
@@ -94,4 +96,9 @@ public ActionFeedback onDown(JTextField txt) {
9496
wrapper.setTextNoEvent(this.txt.getText());
9597
return null;
9698
}
99+
100+
@Override
101+
public boolean sameAs(GuiScreen screen, GuiTextField txtField) {
102+
return this.screen==screen && txtField==txt;
103+
}
97104
}

src/main/java/org/devinprogress/YAIF/Bridges/IActionBridge.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.devinprogress.YAIF.Bridges;
22

3+
import net.minecraft.client.gui.GuiScreen;
4+
import net.minecraft.client.gui.GuiTextField;
5+
36
import javax.swing.*;
47

58
/**
@@ -21,4 +24,5 @@ enum ActionFeedback{
2124
public ActionFeedback onTab(final JTextField txt);
2225
public ActionFeedback onUp(final JTextField txt);
2326
public ActionFeedback onDown(final JTextField txt);
27+
public boolean sameAs(GuiScreen screen,GuiTextField txtField);
2428
}

src/main/java/org/devinprogress/YAIF/InputFieldWrapper.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package org.devinprogress.YAIF;
22

3+
import com.sun.istack.internal.Nullable;
34
import cpw.mods.fml.client.FMLClientHandler;
45
import net.minecraft.client.gui.GuiChat;
5-
import org.apache.logging.log4j.Level;
6+
import net.minecraft.client.gui.inventory.GuiEditSign;
7+
import org.devinprogress.YAIF.Bridges.EditSignBridge;
68
import org.devinprogress.YAIF.Bridges.GuiChatBridge;
79
import org.devinprogress.YAIF.Bridges.IActionBridge;
10+
import org.lwjgl.opengl.AWTGLCanvas;
811
import org.lwjgl.opengl.Display;
912

1013
import javax.swing.*;
@@ -25,12 +28,12 @@ public class InputFieldWrapper {
2528

2629
private static boolean hasInitiated=false;
2730
private boolean enabled=true; //Reserved for Further Use
28-
private boolean Showed=false;
31+
private boolean shown =false;
2932
private boolean doTriggerOnChangeEvent=true;
3033
private IActionBridge bridge=null;
3134

32-
private Canvas canvas = null;
33-
private JFrame frame = null;
35+
private AWTGLCanvas canvas = null;
36+
private final JFrame frame=new JFrame("Minecraft");
3437
private JTextField txtField = null;
3538

3639
public InputFieldWrapper(int Width,int Height){ //Should be Called only once
@@ -40,8 +43,12 @@ public InputFieldWrapper(int Width,int Height){ //Should be Called only once
4043
}
4144
hasInitiated=true;
4245

43-
canvas =new Canvas();
44-
frame =new JFrame("Minecraft");
46+
try {
47+
canvas = new AWTGLCanvas();
48+
}catch(Exception e){
49+
e.printStackTrace();
50+
}
51+
canvas.setFocusable(true);
4552
txtField =new JTextField();
4653

4754
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
@@ -71,7 +78,7 @@ public void windowClosing(WindowEvent e) {
7178
frame.validate();
7279
}
7380

74-
private void bindKeys(){//TODO: add arrow up/down event for GuiChat
81+
private void bindKeys(){
7582
//Should be Called Only Once
7683
//Be careful about txtField.setText(). It will trigger here and further trigger the bridges.
7784
InputMap inputmap = txtField.getInputMap();
@@ -150,26 +157,28 @@ public void setEnabled(boolean flag){ //Reserved for further use
150157
}
151158

152159
public void show(){//called when GuiTextField: New/Re-click/change
153-
buildBridge();
154-
if(Showed)return;
155-
Showed=true;
156-
frame.setSize(new Dimension(frame.getWidth(), frame.getHeight() + TextFieldHeight));
157-
txtField.setVisible(true);
158-
FMLClientHandler.instance().getClient().setIngameNotInFocus();
159-
txtField.requestFocus();
160-
frame.validate();
160+
if(!enabled)return;
161+
bridge=getBridge();
162+
if((!shown)&&bridge!=null) {
163+
shown = true;
164+
frame.setSize(new Dimension(frame.getWidth(), frame.getHeight() + TextFieldHeight));
165+
txtField.setVisible(true);
166+
FMLClientHandler.instance().getClient().setIngameNotInFocus();
167+
txtField.requestFocus();
168+
frame.validate();
169+
}
161170
}
162171

163172
//TODO: Fix Bugs about focus
164173
public void hide(){
165174
bridge=null;
166-
if(!Showed)return;
167-
Showed=false;
175+
if(!shown)return;
176+
shown =false;
168177
txtField.setVisible(false);
169178
frame.setSize(new Dimension(frame.getWidth(), frame.getHeight() - TextFieldHeight));
170-
frame.validate();
171-
txtField.transferFocus();
179+
172180
canvas.requestFocusInWindow();
181+
frame.validate();
173182
FMLClientHandler.instance().getClient().setIngameFocus();
174183
}
175184

@@ -186,10 +195,16 @@ public void DoActions(IActionBridge.ActionFeedback action, Object obj){
186195
}
187196
}
188197

189-
private void buildBridge(){//Remember to add cases here if new Bridges added.
198+
@Nullable
199+
private IActionBridge getBridge(){//Remember to add cases here if new Bridges added.
200+
if(bridge!=null&&bridge.sameAs(YetAnotherInputFix.currentGuiScreen,YetAnotherInputFix.currentTextField))
201+
return bridge;
190202
if(YetAnotherInputFix.currentGuiScreen instanceof GuiChat)
191-
bridge=new GuiChatBridge(YetAnotherInputFix.currentTextField, (GuiChat)YetAnotherInputFix.currentGuiScreen,this);
192-
else hide();
203+
return new GuiChatBridge(YetAnotherInputFix.currentTextField, (GuiChat)YetAnotherInputFix.currentGuiScreen,this);
204+
else if(YetAnotherInputFix.currentGuiScreen instanceof GuiEditSign)
205+
return new EditSignBridge((GuiEditSign)YetAnotherInputFix.currentGuiScreen,this);
206+
else
207+
return null;
193208
//else bridge=new CommonBridge(YetAnotherInputFix.currentTextField, this);
194209
}
195210

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.devinprogress.YAIF;
22

33
import com.google.common.eventbus.EventBus;
4+
import cpw.mods.fml.client.FMLClientHandler;
45
import cpw.mods.fml.common.*;
56
import cpw.mods.fml.common.event.FMLInitializationEvent;
67
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
@@ -30,8 +31,8 @@
3031
public class YetAnotherInputFix{
3132

3233
public static boolean ObfuscatedEnv=true;
33-
private Set<Class<?>> InputableGui = new HashSet<Class<?>>();
34-
private Set<Class<?>> UnInputableGui = new HashSet<Class<?>>();
34+
private static Set<Class<?>> InputableGui = new HashSet<Class<?>>();
35+
private static Set<Class<?>> UnInputableGui = new HashSet<Class<?>>();
3536
public static GuiScreen currentGuiScreen = null;
3637
public static GuiTextField currentTextField = null;
3738
private static InputFieldWrapper wrapper =null;
@@ -49,57 +50,31 @@ public void load(FMLInitializationEvent event) {
4950
}
5051

5152
@SubscribeEvent
52-
public void onGuiChange(GuiOpenEvent e) {
53-
54-
boolean hasTextField;
55-
if (e.gui != null) {
56-
Class GuiClass = e.gui.getClass();
57-
58-
if (UnInputableGui.contains(GuiClass)) {
59-
hasTextField = false;
60-
} else if (InputableGui.contains(GuiClass)) {
61-
hasTextField = true;
62-
} else { //Use Reflection to Check all fields
63-
hasTextField = false;
64-
for (Field f : GuiClass.getDeclaredFields()) {
65-
if (f.getType() == GuiTextField.class) {
66-
hasTextField = true;
67-
break;
68-
}
69-
}
70-
/*TODO: Uncomment when bridges done
71-
//TODO: Find a better way to do this
72-
if( GuiClass.equals(GuiEditSign.class) ||
73-
GuiClass.equals(GuiScreenBook.class))
74-
hasTextField=true;*/
75-
76-
if (hasTextField)
77-
InputableGui.add(GuiClass);
78-
else
79-
UnInputableGui.add(GuiClass);
80-
}
81-
} else {
82-
hasTextField = false;
53+
public void onGuiChange(GuiScreenEvent.InitGuiEvent.Post e) {
54+
if(e.gui==null){
55+
logger.info("EventGui == null");
8356
}
84-
85-
if (hasTextField) {
86-
currentGuiScreen = e.gui;
57+
if(e.gui instanceof GuiEditSign){
58+
currentGuiScreen=e.gui;
8759
currentTextField=null;
88-
} else {
89-
currentGuiScreen = null;
90-
wrapper.hide();
60+
wrapper.show();
9161
}
92-
currentTextField = null;
9362
}
9463

9564
public static void TextFieldFocusChange(GuiTextField textField, boolean isFocused) {
9665
if (isFocused) {
97-
if (currentGuiScreen != null) {
66+
/*if (currentGuiScreen != null) {
9867
currentTextField = textField;
9968
wrapper.show();
10069
}else {
10170
currentTextField = null;
10271
wrapper.hide();
72+
}*/
73+
GuiScreen sc= FMLClientHandler.instance().getClient().currentScreen;
74+
if(GuiCanInput(sc)){
75+
currentGuiScreen=sc;
76+
currentTextField=textField;
77+
wrapper.show();
10378
}
10479
} else {
10580
if (currentTextField == textField) {
@@ -108,4 +83,26 @@ public static void TextFieldFocusChange(GuiTextField textField, boolean isFocuse
10883
}
10984
}
11085
}
86+
87+
private static boolean GuiCanInput(GuiScreen gui){
88+
if(gui==null)return false;
89+
Class GuiClass=gui.getClass();
90+
if(UnInputableGui.contains(GuiClass))return false;
91+
if(InputableGui.contains(GuiClass))return true;
92+
boolean hasTextField = false;
93+
94+
for (Field f : GuiClass.getDeclaredFields()) {
95+
if (f.getType() == GuiTextField.class) {
96+
hasTextField = true;
97+
break;
98+
}
99+
}
100+
101+
if (hasTextField)
102+
InputableGui.add(GuiClass);
103+
else
104+
UnInputableGui.add(GuiClass);
105+
106+
return hasTextField;
107+
}
111108
}

0 commit comments

Comments
 (0)