Browse code

add Select arrows

devnewton authored on 26/05/2014 at 13:35:12
Showing 2 changed files
... ...
@@ -26,7 +26,6 @@ package im.bci.jnuit.widgets;
26 26
 import java.util.List;
27 27
 
28 28
 import im.bci.jnuit.NuitToolkit;
29
-import im.bci.jnuit.text.TextColor;
30 29
 import im.bci.jnuit.visitors.WidgetVisitor;
31 30
 
32 31
 /**
... ...
@@ -42,6 +41,8 @@ public class Select<T> extends Widget {
42 42
     private int selected;
43 43
     private int oldSelected;
44 44
     private boolean suckFocus;
45
+    private String valueSuffix = " >";
46
+    private String valuePrefix = "< ";
45 47
 
46 48
     public Select(NuitToolkit toolkit, List<T> possibleValues) {
47 49
         this.toolkit = toolkit;
... ...
@@ -52,7 +53,7 @@ public class Select<T> extends Widget {
52 52
     public float getMinWidth() {
53 53
         float minWidth = 0.0f;
54 54
         for (T value : possibleValues) {
55
-            minWidth = Math.max(toolkit.getFont().getWidth(value.toString()), minWidth);
55
+            minWidth = Math.max(toolkit.getFont().getWidth(valuePrefix + value.toString() + valueSuffix), minWidth);
56 56
         }
57 57
         return minWidth;
58 58
     }
... ...
@@ -61,7 +62,7 @@ public class Select<T> extends Widget {
61 61
     public float getMinHeight() {
62 62
         float minHeight = 0.0f;
63 63
         for (T value : possibleValues) {
64
-            minHeight = Math.max(toolkit.getFont().getHeight(value.toString()), minHeight);
64
+            minHeight = Math.max(toolkit.getFont().getHeight(valuePrefix + value.toString() + valueSuffix), minHeight);
65 65
         }
66 66
         return minHeight;
67 67
     }
... ...
@@ -120,7 +121,12 @@ public class Select<T> extends Widget {
120 120
 
121 121
     @Override
122 122
     public void onMouseClick(float mouseX, float mouseY) {
123
-        onRight();
123
+        float centerX = getX() + this.getWidth() / 2.0f;
124
+        if (mouseX < centerX) {
125
+            onLeft();
126
+        } else {
127
+            onRight();
128
+        }
124 129
     }
125 130
 
126 131
     @Override
... ...
@@ -133,4 +139,21 @@ public class Select<T> extends Widget {
133 133
     public void accept(WidgetVisitor visitor) {
134 134
         visitor.visit(this);
135 135
     }
136
+
137
+    public String getValueSuffix() {
138
+        return valueSuffix;
139
+    }
140
+
141
+    public void setValueSuffix(String valueSuffix) {
142
+        this.valueSuffix = valueSuffix;
143
+    }
144
+
145
+    public String getValuePrefix() {
146
+        return valuePrefix;
147
+    }
148
+
149
+    public void setValuePrefix(String valuePrefix) {
150
+        this.valuePrefix = valuePrefix;
151
+    }
152
+
136 153
 }
... ...
@@ -205,7 +205,7 @@ public class LwjglNuitRenderer implements WidgetVisitor, BackgroundVisitor, Nuit
205 205
     public void visit(Button widget) {
206 206
         GL11.glPushMatrix();
207 207
         String translatedText = translator.getMessage(widget.getText());
208
-        GL11.glTranslatef(widget.getX() + widget.getWidth() / 2.0f /*- font.getWidth(translatedText)/2.0f*/, widget.getY() + widget.getHeight() / 2.0f + font.getHeight(translatedText) / 2.0f, 0.0f);
208
+        GL11.glTranslatef(widget.getX() + widget.getWidth() / 2.0f, widget.getY() + widget.getHeight() / 2.0f + font.getHeight(translatedText) / 2.0f, 0.0f);
209 209
         GL11.glScalef(1, -1, 1);
210 210
         drawString(widget, translatedText, LwjglNuitFont.Align.CENTER);
211 211
         GL11.glPopMatrix();
... ...
@@ -331,9 +331,9 @@ public class LwjglNuitRenderer implements WidgetVisitor, BackgroundVisitor, Nuit
331 331
 
332 332
         if (null != text) {
333 333
             GL11.glPushMatrix();
334
-            GL11.glTranslatef(widget.getX() + widget.getWidth() / 2.0f - font.getWidth(text) / 2.0f, widget.getY() + widget.getHeight() / 2.0f + font.getHeight(text) / 2.0f, 0.0f);
334
+            GL11.glTranslatef(widget.getX() + widget.getWidth() / 2.0f, widget.getY() + widget.getHeight() / 2.0f + font.getHeight(text) / 2.0f, 0.0f);
335 335
             GL11.glScalef(1, -1, 1);
336
-            drawString(widget, text, LwjglNuitFont.Align.LEFT);
336
+            drawString(widget, text, LwjglNuitFont.Align.CENTER);
337 337
             GL11.glPopMatrix();
338 338
         }
339 339
     }
... ...
@@ -354,9 +354,9 @@ public class LwjglNuitRenderer implements WidgetVisitor, BackgroundVisitor, Nuit
354 354
     public void visit(Label widget) {
355 355
         GL11.glPushMatrix();
356 356
         String translatedText = translator.getMessage(widget.getText());
357
-        GL11.glTranslatef(widget.getX() + widget.getWidth() / 2.0f - font.getWidth(translatedText) / 2.0f, widget.getY() + widget.getHeight() / 2.0f + font.getHeight(translatedText) / 2.0f, 0.0f);
357
+        GL11.glTranslatef(widget.getX() + widget.getWidth() / 2.0f, widget.getY() + widget.getHeight() / 2.0f + font.getHeight(translatedText) / 2.0f, 0.0f);
358 358
         GL11.glScalef(1, -1, 1);
359
-        drawString(widget, translatedText, LwjglNuitFont.Align.LEFT);
359
+        drawString(widget, translatedText, LwjglNuitFont.Align.CENTER);
360 360
         GL11.glPopMatrix();
361 361
     }
362 362
 
... ...
@@ -371,10 +371,10 @@ public class LwjglNuitRenderer implements WidgetVisitor, BackgroundVisitor, Nuit
371 371
     @Override
372 372
     public void visit(Select widget) {
373 373
         GL11.glPushMatrix();
374
-        String text = String.valueOf(widget.getSelected());
375
-        GL11.glTranslatef(widget.getX() + widget.getWidth() / 2.0f - font.getWidth(text) / 2.0f, widget.getY() + widget.getHeight() / 2.0f + font.getHeight(text) / 2.0f, 0.0f);
374
+        String text = widget.getValuePrefix() + String.valueOf(widget.getSelected()) + widget.getValueSuffix();
375
+        GL11.glTranslatef(widget.getX() + widget.getWidth() / 2.0f, widget.getY() + widget.getHeight() / 2.0f + font.getHeight(text) / 2.0f, 0.0f);
376 376
         GL11.glScalef(1, -1, 1);
377
-        drawString(widget, text, LwjglNuitFont.Align.LEFT);
377
+        drawString(widget, text, LwjglNuitFont.Align.CENTER);
378 378
         GL11.glPopMatrix();
379 379
     }
380 380
 
... ...
@@ -395,9 +395,9 @@ public class LwjglNuitRenderer implements WidgetVisitor, BackgroundVisitor, Nuit
395 395
     public void visit(Toggle widget) {
396 396
         GL11.glPushMatrix();
397 397
         String text = widget.getText();
398
-        GL11.glTranslatef(widget.getX() + widget.getWidth() / 2.0f - font.getWidth(text) / 2.0f, widget.getY() + widget.getHeight() / 2.0f + font.getHeight(text) / 2.0f, 0.0f);
398
+        GL11.glTranslatef(widget.getX() + widget.getWidth() / 2.0f, widget.getY() + widget.getHeight() / 2.0f + font.getHeight(text) / 2.0f, 0.0f);
399 399
         GL11.glScalef(1, -1, 1);
400
-        drawString(widget, text, LwjglNuitFont.Align.LEFT);
400
+        drawString(widget, text, LwjglNuitFont.Align.CENTER);
401 401
         GL11.glPopMatrix();
402 402
     }
403 403