@@ -21,6 +21,7 @@ public class Icon implements IIcon, IJsonSerializable {
2121
2222 private final IDrawable drawable ;
2323 private int width = 0 , height = 0 ;
24+ private float aspectRatio = 0 ;
2425 private Alignment alignment = Alignment .Center ;
2526 private final Box margin = new Box ();
2627
@@ -55,13 +56,37 @@ public void draw(GuiContext context, int x, int y, int width, int height, Widget
5556 y += this .margin .getTop ();
5657 width -= this .margin .horizontal ();
5758 height -= this .margin .vertical ();
58- if (this .width > 0 ) {
59- x += (int ) (width * this .alignment .x - this .width * this .alignment .x );
60- width = this .width ;
59+ int frameWidth = width ;
60+ int frameHeight = height ;
61+ if (this .width > 0 ) width = this .width ;
62+ if (this .height > 0 ) height = this .height ;
63+ if (this .aspectRatio > 0 ) {
64+ if (this .width <= 0 ) {
65+ if (this .height <= 0 ) {
66+ // width and height is unset, so adjust width or height so that one of them takes the full space
67+ float w = width , h = height ;
68+ float properW = this .aspectRatio * h ;
69+ if (w > properW ) {
70+ width = (int ) properW ;
71+ } else if (w < properW ) {
72+ height = (int ) (w / this .aspectRatio );
73+ }
74+ } else {
75+ // height is set, so adjust width to height
76+ float properW = this .aspectRatio * height ;
77+ width = (int ) properW ;
78+ }
79+ } else if (this .height <= 0 ) {
80+ // width is set, so adjust height to width
81+ height = (int ) (width / this .aspectRatio );
82+ }
6183 }
62- if (this .height > 0 ) {
63- y += (int ) (height * this .alignment .y - this .height * this .alignment .y );
64- height = this .height ;
84+ // apply alignment
85+ if (width != frameWidth ) {
86+ x += (int ) (frameWidth * this .alignment .x - width * this .alignment .x );
87+ }
88+ if (height != frameHeight ) {
89+ y += (int ) (frameHeight * this .alignment .y - height * this .alignment .y );
6590 }
6691 this .drawable .draw (context , x , y , width , height , widgetTheme );
6792 }
@@ -96,6 +121,11 @@ public Icon size(int size) {
96121 return width (size ).height (size );
97122 }
98123
124+ public Icon aspectRatio (float aspectRatio ) {
125+ this .aspectRatio = aspectRatio ;
126+ return this ;
127+ }
128+
99129 public Icon alignment (Alignment alignment ) {
100130 this .alignment = alignment ;
101131 return this ;
@@ -148,6 +178,7 @@ public void loadFromJson(JsonObject json) {
148178 this .height = (json .has ("autoHeight" ) || json .has ("autoSize" )) &&
149179 JsonHelper .getBoolean (json , true , "autoHeight" , "autoSize" ) ? 0 :
150180 JsonHelper .getInt (json , 0 , "height" , "h" , "size" );
181+ this .aspectRatio = JsonHelper .getFloat (json , 0 , "aspectRatio" );
151182 this .alignment = JsonHelper .deserialize (json , Alignment .class , Alignment .Center , "alignment" , "align" );
152183 this .margin .fromJson (json );
153184 }
@@ -161,6 +192,7 @@ public boolean saveToJson(JsonObject json) {
161192 json .add ("drawable" , JsonHelper .serialize (this .drawable ));
162193 json .addProperty ("width" , this .width );
163194 json .addProperty ("height" , this .height );
195+ json .addProperty ("aspectRatio" , this .aspectRatio );
164196 json .add ("alignment" , JsonHelper .serialize (this .alignment ));
165197 this .margin .toJson (json );
166198 return true ;
0 commit comments