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

Commit edb9fd8

Browse files
committed
better JFrame
1 parent 6c7de16 commit edb9fd8

File tree

2 files changed

+58
-46
lines changed

2 files changed

+58
-46
lines changed

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

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import cpw.mods.fml.client.FMLClientHandler;
44
import net.minecraft.client.gui.GuiChat;
55
import net.minecraft.client.gui.inventory.GuiEditSign;
6-
import org.devinprogress.YAIF.Bridges.CommonBridge;
76
import org.devinprogress.YAIF.Bridges.EditSignBridge;
87
import org.devinprogress.YAIF.Bridges.GuiChatBridge;
98
import org.devinprogress.YAIF.Bridges.IActionBridge;
@@ -24,7 +23,7 @@
2423
* Created by recursiveg on 14-9-11.
2524
*/
2625
public class InputFieldWrapper {
27-
private static final int TextFieldHeight=25;
26+
private static final int fontSize=20;
2827

2928
private static boolean hasInitiated=false;
3029
private boolean enabled=true; //Reserved for Further Use
@@ -33,8 +32,9 @@ public class InputFieldWrapper {
3332
private IActionBridge bridge=null;
3433

3534
private AWTGLCanvas canvas = null;
36-
private final JFrame frame=new JFrame("Minecraft");
37-
private JTextField txtField = null;
35+
private JFrame frame=null;
36+
private JTextField textField = null;
37+
private JPanel panel=null;
3838

3939
public InputFieldWrapper(int Width,int Height){ //Should be Called only once
4040
if(hasInitiated){
@@ -43,54 +43,66 @@ public InputFieldWrapper(int Width,int Height){ //Should be Called only once
4343
}
4444
hasInitiated=true;
4545

46+
// Create Instances
4647
try {
4748
canvas = new AWTGLCanvas();
4849
}catch(Exception e){
4950
e.printStackTrace();
5051
}
51-
canvas.setFocusable(true);
52-
txtField =new JTextField();
52+
frame=new JFrame("Minecraft 1.7.10");
53+
textField=new JTextField();
54+
panel=new JPanel();
5355

56+
// Setup Canvas
57+
canvas.setFocusable(true);
58+
try {
59+
Display.setParent(canvas);
60+
}catch(Exception e){
61+
e.printStackTrace();
62+
}
63+
canvas.setPreferredSize(new Dimension(Width, Height));
64+
65+
// Setup TextField
66+
//textField.setVisible(false);
67+
textField.setVisible(true);
68+
textField.setFont(new Font("Times New Roman",Font.PLAIN, fontSize));
69+
70+
// Setup Panel
71+
panel.setLayout(new BorderLayout());
72+
panel.add(canvas, BorderLayout.CENTER);
73+
panel.add(textField, BorderLayout.PAGE_END);
74+
panel.setVisible(true);
75+
panel.validate();
76+
77+
// Setup frame
78+
frame.setUndecorated(false);
5479
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
80+
frame.setContentPane(panel);
5581
frame.addWindowListener(new WindowAdapter() {
5682
@Override
5783
public void windowClosing(WindowEvent e) {
5884
FMLClientHandler.instance().getClient().shutdown();
5985
}
6086
});
61-
frame.setLayout(new BorderLayout());
62-
frame.setVisible(true);
63-
frame.add(canvas,BorderLayout.CENTER);
64-
try {
65-
Display.setParent(canvas);
66-
}catch(Exception e){
67-
e.printStackTrace();
68-
}
69-
frame.setPreferredSize(new Dimension(Width, Height));
70-
frame.pack();
71-
72-
txtField.setVisible(false);
73-
txtField.setPreferredSize(new Dimension(Width, TextFieldHeight));
74-
bindKeys();
75-
frame.add(txtField, BorderLayout.PAGE_END);
76-
7787
frame.pack();
7888
frame.validate();
89+
frame.setLocationRelativeTo(null);
90+
frame.setVisible(true);
7991
}
8092

8193
public void onTabComplete(){
8294
if(bridge!=null)
83-
bridge.onTabComplete(txtField);
95+
bridge.onTabComplete(textField);
8496
}
8597

8698
private void bindKeys(){
8799
//Should be Called Only Once
88-
//Be careful about txtField.setText(). It will trigger here and further trigger the bridges.
89-
InputMap inputmap = txtField.getInputMap();
90-
ActionMap actionmap = txtField.getActionMap();
100+
//Be careful about textField.setText(). It will trigger here and further trigger the bridges.
101+
InputMap inputmap = textField.getInputMap();
102+
ActionMap actionmap = textField.getActionMap();
91103

92-
txtField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
93-
txtField.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
104+
textField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
105+
textField.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
94106
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
95107
KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
96108
KeyStroke tab=KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0);
@@ -102,51 +114,51 @@ private void bindKeys(){
102114
Action enterAction = new AbstractAction() {
103115
@Override
104116
public void actionPerformed(ActionEvent event) {
105-
if(bridge!=null)DoActions(bridge.onEnter(txtField),null);
117+
if(bridge!=null)DoActions(bridge.onEnter(textField),null);
106118
}
107119
};
108120
Action escAction = new AbstractAction() {
109121
@Override
110122
public void actionPerformed(ActionEvent event) {
111-
if(bridge!=null)DoActions(bridge.onEsc(txtField),null);
123+
if(bridge!=null)DoActions(bridge.onEsc(textField),null);
112124
}
113125
};
114126
Action tabAction = new AbstractAction() {
115127
@Override
116128
public void actionPerformed(ActionEvent event) {
117-
if(bridge!=null)DoActions(bridge.onTab(txtField),null);
129+
if(bridge!=null)DoActions(bridge.onTab(textField),null);
118130
}
119131
};
120132
Action upAction = new AbstractAction() {
121133
@Override
122134
public void actionPerformed(ActionEvent event) {
123-
if(bridge!=null)DoActions(bridge.onUp(txtField),null);
135+
if(bridge!=null)DoActions(bridge.onUp(textField),null);
124136
}
125137
};
126138
Action downAction = new AbstractAction() {
127139
@Override
128140
public void actionPerformed(ActionEvent event) {
129-
if(bridge!=null)DoActions(bridge.onDown(txtField),null);
141+
if(bridge!=null)DoActions(bridge.onDown(textField),null);
130142
}
131143
};
132144
Action backspAction=new AbstractAction() {
133145
@Override
134146
public void actionPerformed(ActionEvent e) {
135-
if(bridge!=null)DoActions(bridge.onBackspace(txtField),null);
147+
if(bridge!=null)DoActions(bridge.onBackspace(textField),null);
136148
}
137149
};
138-
txtField.getDocument().addDocumentListener(new DocumentListener() {
150+
textField.getDocument().addDocumentListener(new DocumentListener() {
139151
@Override
140152
public void insertUpdate(DocumentEvent e) {
141153
if(bridge!=null&&doTriggerOnChangeEvent)
142-
DoActions(bridge.onChange(txtField), null);
154+
DoActions(bridge.onChange(textField), null);
143155
doTriggerOnChangeEvent = true;
144156
}
145157

146158
@Override
147159
public void removeUpdate(DocumentEvent e) {
148160
if(bridge!=null&&doTriggerOnChangeEvent)
149-
DoActions(bridge.onChange(txtField), null);
161+
DoActions(bridge.onChange(textField), null);
150162
doTriggerOnChangeEvent = true;
151163
}
152164

@@ -177,10 +189,10 @@ public void show(){//called when GuiTextField: New/Re-click/change
177189
bridge=getBridge();
178190
if((!shown)&&bridge!=null) {
179191
shown = true;
180-
frame.setSize(new Dimension(frame.getWidth(), frame.getHeight() + TextFieldHeight));
181-
txtField.setVisible(true);
192+
frame.setSize(new Dimension(frame.getWidth(), frame.getHeight() + fontSize));
193+
textField.setVisible(true);
182194
FMLClientHandler.instance().getClient().setIngameNotInFocus();
183-
txtField.requestFocus();
195+
textField.requestFocus();
184196
frame.validate();
185197
}
186198
}
@@ -189,8 +201,8 @@ public void hide(){
189201
bridge=null;
190202
if(!shown)return;
191203
shown =false;
192-
txtField.setVisible(false);
193-
frame.setSize(new Dimension(frame.getWidth(), frame.getHeight() - TextFieldHeight));
204+
textField.setVisible(false);
205+
frame.setSize(new Dimension(frame.getWidth(), frame.getHeight() - fontSize));
194206

195207
canvas.requestFocusInWindow();
196208
frame.validate();
@@ -231,8 +243,8 @@ public void setTextNoEvent(final String str){
231243
SwingUtilities.invokeLater(new Runnable() {
232244
@Override
233245
public void run() {
234-
txtField.setText(str);
235-
txtField.setCaretPosition(txtField.getText().length());
246+
textField.setText(str);
247+
textField.setCaretPosition(textField.getText().length());
236248
}
237249
});
238250
}

src/main/java/org/devinprogress/YAIF/Transformer/ASMTransformer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static void insertWrapperStartup(MethodNode mn){
5757
mn.instructions.insertBefore(n,new VarInsnNode(Opcodes.ALOAD,0));
5858
mn.instructions.insertBefore(n,new FieldInsnNode(Opcodes.GETFIELD,obfedClassName.replace('.','/'),HeightName,"I"));
5959
mn.instructions.insertBefore(n,new MethodInsnNode(Opcodes.INVOKESTATIC,
60-
"org/devinprogress/YAIF/YetAnotherInputFix","SetupTextFieldWrapper","(II)V"));
60+
"org/devinprogress/YAIF/YetAnotherInputFix","SetupTextFieldWrapper","(II)V",false));
6161
// Codes are braced by existed TryCatchBlock
6262
mn.maxStack+=1;
6363
}
@@ -78,7 +78,7 @@ public static void hookGuiTextFocusChange(MethodNode mn){
7878
mn.instructions.insertBefore(n,new VarInsnNode(Opcodes.ILOAD,1));
7979
mn.instructions.insertBefore(n,new MethodInsnNode(Opcodes.INVOKESTATIC,
8080
"org/devinprogress/YAIF/YetAnotherInputFix","TextFieldFocusChange",
81-
"(L"+obfedClassName.replace('.','/')+";Z)V"));
81+
"(L"+obfedClassName.replace('.','/')+";Z)V",false));
8282
}
8383

8484
public static void hookPostTabComplete(MethodNode mn){

0 commit comments

Comments
 (0)