|
27 | 27 | import com.badlogic.gdx.math.Vector2; |
28 | 28 | import org.destinationsol.common.SolMath; |
29 | 29 | import org.destinationsol.files.FileManager; |
| 30 | +import org.destinationsol.TextAlignment; |
| 31 | + |
| 32 | +import javax.swing.text.TabExpander; |
| 33 | +import javax.xml.soap.Text; |
30 | 34 |
|
31 | 35 | public class CommonDrawer { |
32 | | - public final float w; |
33 | | - public final float h; |
34 | | - public final float r; |
35 | | - |
36 | | - private final SpriteBatch mySpriteBatch; |
37 | | - private final BitmapFont myFont; |
38 | | - private final float myOrigFontHeight; |
39 | | - private final TextureChecker myTextureChecker; |
40 | | - private final GlyphLayout layout; |
41 | | - |
42 | | - public CommonDrawer() { |
43 | | - myTextureChecker = new TextureChecker(); |
44 | | - w = Gdx.graphics.getWidth(); |
45 | | - h = Gdx.graphics.getHeight(); |
46 | | - r = w / h; |
47 | | - mySpriteBatch = new SpriteBatch(); |
48 | | - |
49 | | - final FileHandle fontFile = FileManager.getInstance().getFontsDirectory().child("main.fnt"); |
50 | | - myFont = new BitmapFont(fontFile, true); |
51 | | - myFont.setUseIntegerPositions(false); |
52 | | - |
53 | | - myOrigFontHeight = myFont.getXHeight(); |
54 | | - |
55 | | - layout = new GlyphLayout(); |
56 | | - } |
57 | | - |
58 | | - public void setMtx(Matrix4 mtx) { |
59 | | - mySpriteBatch.setProjectionMatrix(mtx); |
60 | | - } |
61 | | - |
62 | | - public void begin() { |
63 | | - mySpriteBatch.begin(); |
64 | | - } |
65 | | - |
66 | | - public void end() { |
67 | | - myTextureChecker.onEnd(); |
68 | | - mySpriteBatch.end(); |
69 | | - } |
70 | | - |
71 | | - public void drawString(String s, float x, float y, float fontSize, boolean centered, Color col) { |
72 | | - if (s == null) return; |
73 | | - myTextureChecker.onString(myFont.getRegion().getTexture()); |
74 | | - myFont.setColor(col); |
75 | | - myFont.getData().setScale(fontSize / myOrigFontHeight); |
76 | | - if (!centered) { |
77 | | - myFont.draw(mySpriteBatch, s, x, y); |
78 | | - return; |
| 36 | + public final float w; |
| 37 | + public final float h; |
| 38 | + public final float r; |
| 39 | + |
| 40 | + private final SpriteBatch mySpriteBatch; |
| 41 | + private final BitmapFont myFont; |
| 42 | + private final float myOrigFontHeight; |
| 43 | + private final TextureChecker myTextureChecker; |
| 44 | + private final GlyphLayout layout; |
| 45 | + |
| 46 | + public CommonDrawer() { |
| 47 | + myTextureChecker = new TextureChecker(); |
| 48 | + w = Gdx.graphics.getWidth(); |
| 49 | + h = Gdx.graphics.getHeight(); |
| 50 | + r = w / h; |
| 51 | + mySpriteBatch = new SpriteBatch(); |
| 52 | + |
| 53 | + final FileHandle fontFile = FileManager.getInstance().getFontsDirectory().child("main.fnt"); |
| 54 | + myFont = new BitmapFont(fontFile, true); |
| 55 | + myFont.setUseIntegerPositions(false); |
| 56 | + |
| 57 | + myOrigFontHeight = myFont.getXHeight(); |
| 58 | + |
| 59 | + layout = new GlyphLayout(); |
| 60 | + } |
| 61 | + |
| 62 | + public void setMtx(Matrix4 mtx) { |
| 63 | + mySpriteBatch.setProjectionMatrix(mtx); |
| 64 | + } |
| 65 | + |
| 66 | + public void begin() { |
| 67 | + mySpriteBatch.begin(); |
| 68 | + } |
| 69 | + |
| 70 | + public void end() { |
| 71 | + myTextureChecker.onEnd(); |
| 72 | + mySpriteBatch.end(); |
| 73 | + } |
| 74 | + |
| 75 | + public void drawString(String s, float x, float y, float fontSize, boolean centered, Color col) { |
| 76 | + drawString(s, x, y, fontSize, TextAlignment.CENTER, centered, col); |
| 77 | + } |
| 78 | + |
| 79 | + public void drawString(String s, float x, float y, float fontSize, TextAlignment align, boolean verticalCentering, Color col) { |
| 80 | + if (s == null) return; |
| 81 | + |
| 82 | + myTextureChecker.onString(myFont.getRegion().getTexture()); |
| 83 | + myFont.setColor(col); |
| 84 | + myFont.getData().setScale(fontSize / myOrigFontHeight); |
| 85 | + // http://www.badlogicgames.com/wordpress/?p=3658 |
| 86 | + layout.reset(); |
| 87 | + layout.setText(myFont, s); |
| 88 | + |
| 89 | + switch(align){ |
| 90 | + case LEFT: |
| 91 | + break; |
| 92 | + case CENTER: |
| 93 | + x -= layout.width / 2; |
| 94 | + break; |
| 95 | + case RIGHT: |
| 96 | + x -= layout.width; |
| 97 | + break; |
| 98 | + } |
| 99 | + |
| 100 | + if(verticalCentering) { |
| 101 | + y -= layout.height / 2; |
| 102 | + } |
| 103 | + |
| 104 | + myFont.draw(mySpriteBatch, layout, x, y); |
| 105 | + } |
| 106 | + |
| 107 | + public void draw(TextureRegion tr, float width, float height, float origX, float origY, float x, float y, |
| 108 | + float rot, Color tint) { |
| 109 | + setTint(tint); |
| 110 | + if (tr instanceof TextureAtlas.AtlasRegion) { |
| 111 | + myTextureChecker.onReg((TextureAtlas.AtlasRegion) tr); |
| 112 | + } else { |
| 113 | + throw new AssertionError("Unexpected texture class"); |
| 114 | + } |
| 115 | + mySpriteBatch.draw(tr, x - origX, y - origY, origX, origY, width, height, 1, 1, rot); |
79 | 116 | } |
80 | 117 |
|
81 | | - // http://www.badlogicgames.com/wordpress/?p=3658 |
82 | | - layout.reset(); |
83 | | - layout.setText(myFont, s); |
84 | | - x -= layout.width / 2; |
85 | | - y -= layout.height / 2; |
86 | | - myFont.draw(mySpriteBatch, layout, x, y); |
87 | | - } |
88 | | - |
89 | | - |
90 | | - public void draw(TextureRegion tr, float width, float height, float origX, float origY, float x, float y, |
91 | | - float rot, Color tint) |
92 | | - { |
93 | | - setTint(tint); |
94 | | - if (tr instanceof TextureAtlas.AtlasRegion) { |
95 | | - myTextureChecker.onReg((TextureAtlas.AtlasRegion)tr); |
96 | | - } else { |
97 | | - throw new AssertionError("Unexpected texture class"); |
| 118 | + private void setTint(Color tint) { |
| 119 | + mySpriteBatch.setColor(tint); |
98 | 120 | } |
99 | | - mySpriteBatch.draw(tr, x - origX, y - origY, origX, origY, width, height, 1, 1, rot); |
100 | | - } |
101 | | - |
102 | | - private void setTint(Color tint) { |
103 | | - mySpriteBatch.setColor(tint); |
104 | | - } |
105 | | - |
106 | | - public void draw(TextureRegion tex, Rectangle rect, Color tint) { |
107 | | - draw(tex, rect.width, rect.height, (float) 0, (float) 0, rect.x, rect.y, (float) 0, tint); |
108 | | - } |
109 | | - |
110 | | - public void drawCircle(TextureRegion tex, Vector2 center, float radius, Color col, float width, float vh) { |
111 | | - float relRad = radius / vh; |
112 | | - int pointCount = (int) (160 * relRad); |
113 | | - Vector2 pos = SolMath.getVec(); |
114 | | - if (pointCount < 8) pointCount = 8; |
115 | | - float lineLen = radius * SolMath.PI * 2 / pointCount; |
116 | | - float angleStep = 360f / pointCount; |
117 | | - float angleStepH = angleStep / 2; |
118 | | - for (int i = 0; i < pointCount; i++) { |
119 | | - float angle = angleStep * i; |
120 | | - SolMath.fromAl(pos, angle, radius); |
121 | | - pos.add(center); |
122 | | - draw(tex, width, lineLen, (float) 0, (float) 0, pos.x, pos.y, angle + angleStepH, col); |
| 121 | + |
| 122 | + public void draw(TextureRegion tex, Rectangle rect, Color tint) { |
| 123 | + draw(tex, rect.width, rect.height, (float) 0, (float) 0, rect.x, rect.y, (float) 0, tint); |
| 124 | + } |
| 125 | + |
| 126 | + public void drawCircle(TextureRegion tex, Vector2 center, float radius, Color col, float width, float vh) { |
| 127 | + float relRad = radius / vh; |
| 128 | + int pointCount = (int) (160 * relRad); |
| 129 | + Vector2 pos = SolMath.getVec(); |
| 130 | + if (pointCount < 8) pointCount = 8; |
| 131 | + float lineLen = radius * SolMath.PI * 2 / pointCount; |
| 132 | + float angleStep = 360f / pointCount; |
| 133 | + float angleStepH = angleStep / 2; |
| 134 | + for (int i = 0; i < pointCount; i++) { |
| 135 | + float angle = angleStep * i; |
| 136 | + SolMath.fromAl(pos, angle, radius); |
| 137 | + pos.add(center); |
| 138 | + draw(tex, width, lineLen, (float) 0, (float) 0, pos.x, pos.y, angle + angleStepH, col); |
| 139 | + } |
| 140 | + SolMath.free(pos); |
| 141 | + } |
| 142 | + |
| 143 | + public void drawLine(TextureRegion tex, float x, float y, float angle, float len, Color col, float width) { |
| 144 | + draw(tex, len, width, 0, width / 2, x, y, angle, col); |
| 145 | + } |
| 146 | + |
| 147 | + public void drawLine(TextureRegion tex, Vector2 p1, Vector2 p2, Color col, float width, boolean precise) { |
| 148 | + Vector2 v = SolMath.getVec(p2); |
| 149 | + v.sub(p1); |
| 150 | + drawLine(tex, p1.x, p1.y, SolMath.angle(v, precise), v.len(), col, width); |
| 151 | + SolMath.free(v); |
| 152 | + } |
| 153 | + |
| 154 | + public void dispose() { |
| 155 | + mySpriteBatch.dispose(); |
| 156 | + myFont.dispose(); |
| 157 | + } |
| 158 | + |
| 159 | + public SpriteBatch getBatch(Texture texture, TextureAtlas.AtlasRegion tex) { |
| 160 | + myTextureChecker.onSprite(texture, tex); |
| 161 | + return mySpriteBatch; |
| 162 | + } |
| 163 | + |
| 164 | + public void setAdditive(boolean additive) { |
| 165 | + int dstFunc = additive ? GL20.GL_ONE : GL20.GL_ONE_MINUS_SRC_ALPHA; |
| 166 | + mySpriteBatch.setBlendFunction(GL20.GL_SRC_ALPHA, dstFunc); |
123 | 167 | } |
124 | | - SolMath.free(pos); |
125 | | - } |
126 | | - |
127 | | - public void drawLine(TextureRegion tex, float x, float y, float angle, float len, Color col, float width) { |
128 | | - draw(tex, len, width, 0, width/2, x, y, angle, col); |
129 | | - } |
130 | | - |
131 | | - public void drawLine(TextureRegion tex, Vector2 p1, Vector2 p2, Color col, float width, boolean precise) { |
132 | | - Vector2 v = SolMath.getVec(p2); |
133 | | - v.sub(p1); |
134 | | - drawLine(tex, p1.x, p1.y, SolMath.angle(v, precise), v.len(), col, width); |
135 | | - SolMath.free(v); |
136 | | - } |
137 | | - |
138 | | - public void dispose() { |
139 | | - mySpriteBatch.dispose(); |
140 | | - myFont.dispose(); |
141 | | - } |
142 | | - |
143 | | - public SpriteBatch getBatch(Texture texture, TextureAtlas.AtlasRegion tex) { |
144 | | - myTextureChecker.onSprite(texture, tex); |
145 | | - return mySpriteBatch; |
146 | | - } |
147 | | - |
148 | | - public void setAdditive(boolean additive) { |
149 | | - int dstFunc = additive ? GL20.GL_ONE : GL20.GL_ONE_MINUS_SRC_ALPHA; |
150 | | - mySpriteBatch.setBlendFunction(GL20.GL_SRC_ALPHA, dstFunc); |
151 | | - } |
152 | 168 | } |
0 commit comments