View Javadoc
1 package org.cyberiantiger.console; 2 3 import java.util.Map; 4 import java.util.HashMap; 5 import java.util.regex.*; 6 import java.io.*; 7 8 public class ElephantMUDConsoleAction extends AbstractConsoleAction { 9 10 private static Pattern mudMode = Pattern.compile("//A//(/(0|\".*\"),(0|\".*\"),(0|\".*\"),(-?//d+),(-?//d+),(-?//d+),///)(.*)//z$",Pattern.DOTALL); 11 12 private static Map pinkFish = new HashMap(); 13 14 public final static int NO_WRAP = 1; 15 public final static int NO_BLOCK = 2; 16 public final static int NO_BUFFER = 4; 17 public final static int IS_INVIS = 8; 18 public final static int IS_DEAD = 16; 19 public final static int IS_CHANNEL = 32; 20 public final static int NO_NEWLINE = 64; 21 22 static { 23 ConsoleAction tmp; 24 pinkFish.put("RESET",new ResetConsoleAction()); 25 pinkFish.put("BOLD",new BoldConsoleAction(true)); 26 pinkFish.put("FLASH",new FlashConsoleAction(true)); 27 pinkFish.put("RED",new ForegroundConsoleAction(Console.RED)); 28 pinkFish.put("YELLOW",tmp=new ForegroundConsoleAction(Console.YELLOW)); 29 pinkFish.put("ORANGE",tmp); 30 pinkFish.put("GREEN",new ForegroundConsoleAction(Console.GREEN)); 31 pinkFish.put("CYAN",new ForegroundConsoleAction(Console.CYAN)); 32 pinkFish.put("BLUE",new ForegroundConsoleAction(Console.BLUE)); 33 pinkFish.put("MAGENTA",new ForegroundConsoleAction(Console.MAGENTA)); 34 pinkFish.put("BLACK",new ForegroundConsoleAction(Console.BLACK)); 35 pinkFish.put("WHITE",new ForegroundConsoleAction(Console.WHITE)); 36 pinkFish.put("B_RED",new BackgroundConsoleAction(Console.RED)); 37 pinkFish.put("B_YELLOW",tmp=new BackgroundConsoleAction(Console.YELLOW)); 38 pinkFish.put("B_ORANGE",tmp); 39 pinkFish.put("B_GREEN",new BackgroundConsoleAction(Console.GREEN)); 40 pinkFish.put("B_CYAN",new BackgroundConsoleAction(Console.CYAN)); 41 pinkFish.put("B_BLUE",new BackgroundConsoleAction(Console.BLUE)); 42 pinkFish.put("B_MAGENTA",new BackgroundConsoleAction(Console.MAGENTA)); 43 pinkFish.put("B_BLACK",new BackgroundConsoleAction(Console.BLACK)); 44 pinkFish.put("B_WHITE",new BackgroundConsoleAction(Console.WHITE)); 45 tmp = new NullConsoleAction(); 46 pinkFish.put("ENDTERM",tmp); 47 pinkFish.put("TERMSIZE",tmp); 48 pinkFish.put("TOINPUT",tmp); 49 pinkFish.put("TOMAIN",tmp); 50 pinkFish.put("TOSTATUS",tmp); 51 pinkFish.put("SC",tmp); 52 pinkFish.put("RC",tmp); 53 pinkFish.put("CLS",new ClearScreenConsoleAction()); 54 pinkFish.put("CLEOL",tmp); 55 pinkFish.put("BELL",new BeepConsoleAction()); 56 57 // The following action(s) are not used by Elephant, but included for 58 // completeness 59 pinkFish.put("REVERSE",new ReverseConsoleAction(true)); 60 } 61 62 private String pClass; 63 private String sClass; 64 private String orig; 65 private int flags; 66 private int indent; 67 private int wrapAt; 68 69 private String data; 70 71 public ElephantMUDConsoleAction(String data) { 72 Matcher m = mudMode.matcher(data); 73 if(m.matches()) { 74 String tmp; 75 tmp = m.group(1); 76 if(tmp.length()>1) pClass = tmp.substring(1,tmp.length()-1); 77 tmp = m.group(2); 78 if(tmp.length()>1) sClass = tmp.substring(1,tmp.length()-1); 79 tmp = m.group(3); 80 if(tmp.length()>1) orig = tmp.substring(1,tmp.length()-1); 81 flags = Integer.parseInt(m.group(4)); 82 indent = Integer.parseInt(m.group(5)); 83 wrapAt = Integer.parseInt(m.group(6)); 84 this.data = m.group(7); 85 } else { 86 this.data = data; 87 } 88 } 89 90 public ElephantMUDConsoleAction( 91 String pClass,String sClass,String orig, 92 int flags, int indent, int wrapAt, String data 93 ) 94 { 95 this.pClass = pClass; 96 this.sClass = sClass; 97 this.orig = orig; 98 this.flags = flags; 99 this.indent = indent; 100 this.wrapAt = wrapAt; 101 this.data = data; 102 } 103 104 public String getPrimaryClass() { 105 return pClass; 106 } 107 108 public String getSecondaryClass() { 109 return sClass; 110 } 111 112 public String getOrigin() { 113 return orig; 114 } 115 116 public int getFlags() { 117 return flags; 118 } 119 120 public int getIndent() { 121 return indent; 122 } 123 124 public int getWrapAt() { 125 return wrapAt; 126 } 127 128 public String getData() { 129 return data; 130 } 131 132 public void apply(Console con) { 133 int w = con.getWidth(); 134 int h = con.getHeight(); 135 String[] lines = data.split("\r?\n"); 136 137 // Check cursor position: If not at 0, newline 138 if(con.getCursorX() != 0) { 139 con.setCursorX(0); 140 con.moveCursorY(1); 141 } 142 143 for(int i=0;i<lines.length;i++) { 144 String[] tokens = lines[i].split("%//^"); 145 for(int j=0;j<tokens.length;j++) { 146 String token = tokens[j]; 147 ConsoleAction action = (ConsoleAction) pinkFish.get(token); 148 if(action != null) { 149 action.apply(con); 150 } else { 151 con.drawString(token); 152 } 153 } 154 // After all but last line 155 if(i != lines.length-1) { 156 con.setCursorX(0); 157 con.moveCursorY(1); 158 } 159 } 160 // Hack, prompt and status msg have invalid flags ! 161 if( 162 !( 163 "prompt".equals(pClass) || 164 "status".equals(pClass) || 165 (flags & NO_WRAP) != 0 166 ) 167 ) 168 { 169 con.setCursorX(0); 170 con.moveCursorY(1); 171 } 172 con.setForeground(con.WHITE); 173 con.setBackground(con.BLACK); 174 con.setBold(false); 175 con.setFlash(false); 176 } 177 178 public void appendToBuffer(StringBuffer buffer) { 179 } 180 181 }

This page was automatically generated by Maven