|
| 1 | +package org.devinprogress.inputfix; |
| 2 | + |
| 3 | +import cpw.mods.fml.client.FMLClientHandler; |
| 4 | +import org.lwjgl.opengl.Display; |
| 5 | + |
| 6 | +import javax.swing.*; |
| 7 | +import javax.swing.event.DocumentEvent; |
| 8 | +import javax.swing.event.DocumentListener; |
| 9 | +import java.awt.*; |
| 10 | +import java.awt.event.ActionEvent; |
| 11 | +import java.awt.event.KeyEvent; |
| 12 | +import java.util.Collections; |
| 13 | + |
| 14 | +/** |
| 15 | + * Created by recursiveg on 14-9-11. |
| 16 | + */ |
| 17 | +public class InputFieldWrapper { |
| 18 | + private static final int TextFieldHeight=25; |
| 19 | + private Canvas canvas = null; |
| 20 | + private JFrame frame = null; |
| 21 | + private JTextField txtField = null; |
| 22 | + private boolean Showed=false; |
| 23 | + private IActionBridge bridge=null; |
| 24 | + |
| 25 | + public InputFieldWrapper(int Width,int Height){ |
| 26 | + canvas =new Canvas(); |
| 27 | + frame =new JFrame("Minecraft"); |
| 28 | + txtField =new JTextField(); |
| 29 | + |
| 30 | + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| 31 | + frame.setLayout(new BorderLayout()); |
| 32 | + frame.setVisible(true); |
| 33 | + frame.add(canvas,BorderLayout.CENTER); |
| 34 | + try { |
| 35 | + Display.setParent(canvas); |
| 36 | + }catch(Exception e){ |
| 37 | + e.printStackTrace(); |
| 38 | + } |
| 39 | + frame.setPreferredSize(new Dimension(Width, Height)); |
| 40 | + frame.pack(); |
| 41 | + |
| 42 | + txtField.setVisible(false); |
| 43 | + txtField.setPreferredSize(new Dimension(Width, TextFieldHeight)); |
| 44 | + bindKeys(); |
| 45 | + frame.add(txtField, BorderLayout.PAGE_END); |
| 46 | + |
| 47 | + frame.pack(); |
| 48 | + frame.validate(); |
| 49 | + } |
| 50 | + |
| 51 | + public void show(){ |
| 52 | + buildBridge(); |
| 53 | + if(Showed)return; |
| 54 | + Showed=true; |
| 55 | + frame.setSize(new Dimension(frame.getWidth(), frame.getHeight() + TextFieldHeight)); |
| 56 | + txtField.setVisible(true); |
| 57 | + txtField.setCaretPosition(txtField.getText().length()); |
| 58 | + //txtField.setText(""); |
| 59 | + FMLClientHandler.instance().getClient().setIngameNotInFocus(); |
| 60 | + txtField.requestFocus(); |
| 61 | + frame.validate(); |
| 62 | + } |
| 63 | + public void hide(){ |
| 64 | + bridge=null; |
| 65 | + if(!Showed)return; |
| 66 | + Showed=false; |
| 67 | + txtField.setVisible(false); |
| 68 | + txtField.setText(""); |
| 69 | + frame.setSize(new Dimension(frame.getWidth(), frame.getHeight() - TextFieldHeight)); |
| 70 | + canvas.requestFocus(); |
| 71 | + FMLClientHandler.instance().getClient().setIngameFocus(); |
| 72 | + frame.validate(); |
| 73 | + } |
| 74 | + public void DoActions(IActionBridge.ActionFeedback action, Object obj){ |
| 75 | + if(action== IActionBridge.ActionFeedback.Quit){ |
| 76 | + hide(); |
| 77 | + }else if(action== IActionBridge.ActionFeedback.Nothing){ |
| 78 | + return; |
| 79 | + }else if(action== IActionBridge.ActionFeedback.SetText){ |
| 80 | + if (obj instanceof String) |
| 81 | + txtField.setText((String)obj); |
| 82 | + }else if(action== IActionBridge.ActionFeedback.Clean){ |
| 83 | + txtField.setText(""); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void bindKeys(){ |
| 88 | + InputMap inputmap = txtField.getInputMap(); |
| 89 | + ActionMap actionmap = txtField.getActionMap(); |
| 90 | + |
| 91 | + txtField.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET); |
| 92 | + txtField.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET); |
| 93 | + KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); |
| 94 | + KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
| 95 | + KeyStroke tab=KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0); |
| 96 | + Action enterAction = new AbstractAction() { |
| 97 | + @Override |
| 98 | + public void actionPerformed(ActionEvent event) { |
| 99 | + if(bridge!=null)DoActions(bridge.onEnter(txtField),null); |
| 100 | + } |
| 101 | + }; |
| 102 | + Action escAction = new AbstractAction() { |
| 103 | + @Override |
| 104 | + public void actionPerformed(ActionEvent event) { |
| 105 | + if(bridge!=null)DoActions(bridge.onEsc(txtField),null); |
| 106 | + } |
| 107 | + }; |
| 108 | + Action tabAction = new AbstractAction() { |
| 109 | + @Override |
| 110 | + public void actionPerformed(ActionEvent event) { |
| 111 | + if(bridge!=null)DoActions(bridge.onTab(txtField),null); |
| 112 | + } |
| 113 | + }; |
| 114 | + inputmap.put(enter, "enter");actionmap.put("enter", enterAction); |
| 115 | + inputmap.put(esc, "esc");actionmap.put("esc", escAction); |
| 116 | + inputmap.put(tab,"tab");actionmap.put("tab",tabAction); |
| 117 | + txtField.getDocument().addDocumentListener(new DocumentListener() { |
| 118 | + @Override |
| 119 | + public void insertUpdate(DocumentEvent e) { |
| 120 | + if(bridge!=null)DoActions(bridge.onChanged(txtField),null); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void removeUpdate(DocumentEvent e) { |
| 125 | + if(bridge!=null)DoActions(bridge.onChanged(txtField),null); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void changedUpdate(DocumentEvent e) { |
| 130 | + |
| 131 | + } |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + private void buildBridge(){//TODO |
| 136 | + //bridge=new DebugBridge(Main.currentTextField,Main.currentGuiScreen,this); |
| 137 | + bridge=new CommonBridge(Main.currentTextField,Main.currentGuiScreen,this); |
| 138 | + } |
| 139 | +} |
0 commit comments