Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 835 | chris | 1 | package com.gebauz.bauzoid.menu; |
| 2 | |||
| 3 | import com.badlogic.gdx.files.FileHandle; |
||
| 4 | import com.gebauz.bauzoid.app.Consts; |
||
| 5 | import com.gebauz.bauzoid.game.Game; |
||
| 6 | import com.gebauz.bauzoid.math.Vector2; |
||
| 7 | import com.gebauz.bauzoid.math.Vector3; |
||
| 8 | import com.gebauz.bauzoid.math.Vector4; |
||
| 9 | import com.gebauz.bauzoid.menu.MenuItem.HorizontalAlign; |
||
| 10 | import com.gebauz.bauzoid.menu.MenuItem.VerticalAlign; |
||
| 11 | import com.gebauz.bauzoid.parser.Preprocessor; |
||
| 12 | import com.gebauz.bauzoid.parser.ScanException; |
||
| 13 | import com.gebauz.bauzoid.parser.Tokenizer; |
||
| 14 | |||
| 15 | /** Utility functions for Menu handling. */ |
||
| 16 | public class MenuUtil |
||
| 17 | { |
||
| 18 | // Constants======================================================================================== |
||
| 19 | public static final String LOG_TAG = Consts.LOG_TAG + ":MenuUtil"; |
||
| 20 | |||
| 21 | // Static variables================================================================================= |
||
| 22 | public static boolean verbose = true; |
||
| 23 | |||
| 24 | // Embedded Types=================================================================================== |
||
| 25 | |||
| 26 | // Methods========================================================================================== |
||
| 27 | |||
| 28 | public static Menu createMenuFromFile(Game game, FileHandle file) |
||
| 29 | { |
||
| 30 | String fileContents = Preprocessor.stripComments(file.readString()); |
||
| 31 | |||
| 32 | Menu menu = null; |
||
| 33 | |||
| 34 | try |
||
| 35 | { |
||
| 36 | Tokenizer tokenizer = new Tokenizer(fileContents); |
||
| 37 | tokenizer.setStringDelimiter(new char[] {'\'', '"'} ); |
||
| 38 | |||
| 39 | while (!tokenizer.checkNoMoreTokens()) |
||
| 40 | { |
||
| 41 | String identifier = tokenizer.readIdentifier(); |
||
| 42 | |||
| 43 | if (identifier.equalsIgnoreCase("Menu")) |
||
| 44 | { |
||
| 45 | String menuName = tokenizer.readString(); |
||
| 46 | |||
| 47 | menu = new Menu(game, menuName); |
||
| 48 | |||
| 49 | tokenizer.readToken("{"); |
||
| 50 | while (!tokenizer.checkToken("}")) |
||
| 51 | { |
||
| 52 | menu.parseLine(tokenizer); |
||
| 53 | } |
||
| 54 | tokenizer.readToken("}"); |
||
| 55 | } |
||
| 56 | else |
||
| 57 | { |
||
| 58 | throw new ScanException(LOG_TAG, "Syntax error at: " + tokenizer.getSurroundings()); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } |
||
| 62 | catch (ScanException e) |
||
| 63 | { |
||
| 64 | e.log(LOG_TAG); |
||
| 65 | if (menu != null) |
||
| 66 | { |
||
| 67 | menu.exit(); |
||
| 68 | menu = null; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | return menu; |
||
| 73 | } |
||
| 74 | |||
| 75 | // Parsing Tools====================================================================================== |
||
| 76 | |||
| 77 | |||
| 78 | public static HorizontalAlign stringToHorizontalAlignment(String align) |
||
| 79 | { |
||
| 80 | if (align.equalsIgnoreCase("right")) |
||
| 81 | return HorizontalAlign.RIGHT; |
||
| 82 | else if (align.equalsIgnoreCase("center")) |
||
| 83 | return HorizontalAlign.CENTER; |
||
| 84 | else //if (align.equalsIgnoreCase("left")) |
||
| 85 | return HorizontalAlign.LEFT; |
||
| 86 | } |
||
| 87 | |||
| 88 | public static VerticalAlign stringToVerticalAlignment(String align) |
||
| 89 | { |
||
| 90 | if (align.equalsIgnoreCase("bottom")) |
||
| 91 | return VerticalAlign.BOTTOM; |
||
| 92 | else if (align.equalsIgnoreCase("center")) |
||
| 93 | return VerticalAlign.CENTER; |
||
| 94 | else //if (align.equalsIgnoreCase("left")) |
||
| 95 | return VerticalAlign.TOP; |
||
| 96 | } |
||
| 97 | |||
| 98 | public static float parseNumber(Tokenizer t) throws ScanException |
||
| 99 | { |
||
| 100 | float result = t.readNumber(); |
||
| 101 | t.readToken(";"); |
||
| 102 | return result; |
||
| 103 | } |
||
| 104 | |||
| 105 | public static Vector2 parseVector2(Tokenizer t) throws ScanException |
||
| 106 | { |
||
| 107 | Vector2 result = new Vector2(); |
||
| 108 | result.x = t.readNumber(); |
||
| 109 | t.readToken(","); |
||
| 110 | result.y = t.readNumber(); |
||
| 111 | t.readToken(";"); |
||
| 112 | return result; |
||
| 113 | } |
||
| 114 | |||
| 115 | public static Vector3 parseVector3(Tokenizer t) throws ScanException |
||
| 116 | { |
||
| 117 | Vector3 result = new Vector3(); |
||
| 118 | result.x = t.readNumber(); |
||
| 119 | t.readToken(","); |
||
| 120 | result.y = t.readNumber(); |
||
| 121 | t.readToken(","); |
||
| 122 | result.z = t.readNumber(); |
||
| 123 | t.readToken(";"); |
||
| 124 | return result; |
||
| 125 | } |
||
| 126 | |||
| 127 | public static Vector4 parseVector4(Tokenizer t) throws ScanException |
||
| 128 | { |
||
| 129 | Vector4 result = new Vector4(); |
||
| 130 | result.x = t.readNumber(); |
||
| 131 | t.readToken(","); |
||
| 132 | result.y = t.readNumber(); |
||
| 133 | t.readToken(","); |
||
| 134 | result.z = t.readNumber(); |
||
| 135 | t.readToken(","); |
||
| 136 | result.w = t.readNumber(); |
||
| 137 | t.readToken(";"); |
||
| 138 | return result; |
||
| 139 | } |
||
| 140 | |||
| 141 | public static String parseString(Tokenizer t, String defaultValue) throws ScanException |
||
| 142 | { |
||
| 143 | String result = defaultValue; |
||
| 144 | result = t.readString(); |
||
| 145 | t.readToken(";"); |
||
| 146 | return result; |
||
| 147 | } |
||
| 148 | |||
| 149 | public static String parseString(Tokenizer t) throws ScanException |
||
| 150 | { |
||
| 151 | return parseString(t, ""); |
||
| 152 | } |
||
| 153 | |||
| 154 | public static boolean parseBool(Tokenizer t) throws ScanException |
||
| 155 | { |
||
| 156 | boolean result = false; |
||
| 157 | String boolValue = t.readIdentifier(); |
||
| 158 | |||
| 159 | if (boolValue.equalsIgnoreCase("true")) |
||
| 160 | result = true; |
||
| 161 | else if (boolValue.equalsIgnoreCase("false")) |
||
| 162 | result = false; |
||
| 163 | else |
||
| 164 | throw new ScanException(Tokenizer.UNEXPECTED_TOKEN + " Expected boolean value (true/false)", t.getSurroundings()); |
||
| 165 | |||
| 166 | t.readToken(";"); |
||
| 167 | |||
| 168 | return result; |
||
| 169 | } |
||
| 170 | |||
| 171 | } |
||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 |