20
20
import com .badlogic .gdx .graphics .Color ;
21
21
import com .badlogic .gdx .graphics .g2d .Batch ;
22
22
import com .badlogic .gdx .graphics .g2d .BitmapFont ;
23
+ import com .badlogic .gdx .graphics .g2d .GlyphLayout ;
23
24
import com .badlogic .gdx .scenes .scene2d .Actor ;
24
25
import com .badlogic .gdx .scenes .scene2d .InputEvent ;
25
26
import com .badlogic .gdx .scenes .scene2d .InputListener ;
26
27
import com .badlogic .gdx .scenes .scene2d .utils .Drawable ;
28
+ import com .badlogic .gdx .utils .Align ;
27
29
import com .bladecoder .engine .i18n .I18N ;
28
30
import com .bladecoder .engine .model .DialogOption ;
29
31
import com .bladecoder .engine .model .World ;
@@ -37,6 +39,8 @@ public class DialogUI extends Actor {
37
39
private Recorder recorder ;
38
40
39
41
private int selected = -1 ;
42
+
43
+ private final GlyphLayout layout = new GlyphLayout ();
40
44
41
45
public DialogUI (SceneScreen scr ) {
42
46
style = scr .getUI ().getSkin ().get (DialogUIStyle .class );
@@ -81,6 +85,11 @@ public void act(float delta) {
81
85
} else if ( !isVisible () && World .getInstance ().getCurrentDialog () != null && !World .getInstance ().inCutMode ()) {
82
86
setVisible (true );
83
87
}
88
+
89
+ if (isVisible ()) {
90
+ setWidth (getStage ().getViewport ().getScreenWidth ());
91
+ setHeight (calcHeight ());
92
+ }
84
93
}
85
94
86
95
@ Override
@@ -100,45 +109,80 @@ else if (options.size() == 1) { // If only has one option,
100
109
return ;
101
110
}
102
111
103
- float lineHeight = style .font .getLineHeight ();
104
- float y = lineHeight * options .size ();
105
- setWidth (getStage ().getViewport ().getScreenWidth ());
106
112
float margin = DPIUtils .getMarginSize ();
107
- setHeight ( margin + lineHeight * options . size ());
113
+ float y = margin ;
108
114
109
115
if (style .background != null ) {
110
116
style .background .draw (batch , getX (), getY (), getWidth (), getHeight ());
111
117
}
112
118
113
- for (int i = 0 ; i < options .size (); i ++ ) {
119
+ for (int i = options .size () - 1 ; i >= 0 ; i -- ) {
114
120
DialogOption o = options .get (i );
115
121
String str = o .getText ();
116
122
117
123
if (str .charAt (0 ) == '@' )
118
124
str = I18N .getString (str .substring (1 ));
119
125
120
126
if (i == selected ) {
121
- style .font .setColor (style .overFontColor );
122
- style .font .draw (batch , str , margin , y );
127
+ layout .setText (style .font , str , style .overFontColor , getWidth () - margin * 2 , Align .left , true );
123
128
} else {
124
- style .font .setColor (style .fontColor );
125
- style .font .draw (batch , str , margin , y );
129
+ layout .setText (style .font , str , style .fontColor , getWidth () - margin * 2 , Align .left , true );
126
130
}
127
131
128
- y -= lineHeight ;
132
+ y += layout .height - style .font .getDescent () + style .font .getAscent ();
133
+ style .font .draw (batch , layout , margin , y );
129
134
}
130
135
}
136
+
137
+ private float calcHeight () {
138
+ float height = 0 ;
139
+ float margin = DPIUtils .getMarginSize ();
140
+
141
+ ArrayList <DialogOption > options = World .getInstance ()
142
+ .getCurrentDialog ().getVisibleOptions ();
143
+
144
+ for (int i = 0 ; i < options .size (); i ++) {
145
+ DialogOption o = options .get (i );
146
+ String str = o .getText ();
147
+
148
+ if (str .charAt (0 ) == '@' )
149
+ str = I18N .getString (str .substring (1 ));
150
+ layout .setText (style .font , str , style .overFontColor , getStage ().getViewport ().getScreenWidth () - margin * 2 , Align .left , true );
151
+ height += layout .height - style .font .getDescent () + style .font .getAscent ();
152
+ }
153
+
154
+ return height + margin * 2 ;
155
+ }
131
156
132
157
private int getOption (float x , float y ) {
133
158
if (World .getInstance ().getCurrentDialog () == null )
134
159
return -1 ;
135
160
136
- float lineHeight = style .font .getLineHeight ();
161
+ ArrayList <DialogOption > options = World .getInstance ()
162
+ .getCurrentDialog ().getVisibleOptions ();
163
+
164
+ float margin = DPIUtils .getMarginSize ();
165
+ float oy = margin ;
166
+
167
+ int selectedLine = 0 ;
168
+
169
+ for (int i = options .size () - 1 ; i >= 0 ; i --) {
170
+ DialogOption o = options .get (i );
171
+ String str = o .getText ();
137
172
138
- int selectedLine = (int ) (y / lineHeight );
173
+ if (str .charAt (0 ) == '@' )
174
+ str = I18N .getString (str .substring (1 ));
175
+
176
+ layout .setText (style .font , str , style .overFontColor , getStage ().getViewport ().getScreenWidth () - margin * 2 , Align .left , true );
177
+ oy += layout .height - style .font .getDescent () + style .font .getAscent ();
178
+
179
+ if (oy > y ) {
180
+ selectedLine = i ;
181
+ break ;
182
+ }
183
+ }
139
184
140
- return World .getInstance ().getCurrentDialog ().getNumVisibleOptions ()
141
- - selectedLine - 1 ;
185
+ return selectedLine ;
142
186
}
143
187
144
188
private void select (int i ) {
0 commit comments