@@ -11,7 +11,6 @@ public partial class CityLabelScene : Node2D {
1111
1212 private City city ;
1313 private Tile tile ;
14- private Vector2I tileCenter ;
1514
1615 private ImageTexture cityTexture ;
1716
@@ -37,6 +36,11 @@ public partial class CityLabelScene : Node2D {
3736 private static Theme popSizeTheme = new Theme ( ) ;
3837
3938 private int lastLabelWidth = 0 ;
39+ private int lastTurnsUntilGrowth ;
40+ private int lastTurnsUntilProductFinished ;
41+ private int lastCitySize ;
42+ private string lastProductionName ;
43+ private bool wasCapital ;
4044
4145 static CityLabelScene ( ) {
4246 smallFontTheme . DefaultFont = smallFont ;
@@ -54,94 +58,105 @@ static CityLabelScene() {
5458 nonEmbassyStar = PCXToGodot . getImageFromPCX ( cityIcons , 20 , 1 , 18 , 18 ) ;
5559 }
5660
57- public CityLabelScene ( City city , Tile tile , Vector2I tileCenter ) {
61+ public CityLabelScene ( City city , Tile tile ) {
5862 this . city = city ;
5963 this . tile = tile ;
60- this . tileCenter = tileCenter ;
61-
6264
6365 labelTextureRect . MouseFilter = Control . MouseFilterEnum . Ignore ;
6466 cityNameLabel . MouseFilter = Control . MouseFilterEnum . Ignore ;
6567 productionLabel . MouseFilter = Control . MouseFilterEnum . Ignore ;
6668 popSizeLabel . MouseFilter = Control . MouseFilterEnum . Ignore ;
6769
70+ cityNameLabel . Text = city . name ;
6871 AddChild ( labelTextureRect ) ;
6972 AddChild ( cityNameLabel ) ;
7073 AddChild ( productionLabel ) ;
7174 AddChild ( popSizeLabel ) ;
75+
76+ cityNameLabel . Theme = smallFontTheme ;
77+ cityNameLabel . OffsetTop = 24 ;
78+
79+ productionLabel . Theme = smallFontTheme ;
80+ productionLabel . OffsetTop = 36 ;
81+
82+ popSizeLabel . Theme = popSizeTheme ;
83+ popSizeLabel . OffsetTop = 28 ;
84+
85+ lastTurnsUntilGrowth = city . TurnsUntilGrowth ( ) ;
86+ lastTurnsUntilProductFinished = city . TurnsUntilProductionFinished ( ) ;
87+ lastCitySize = city . size ;
88+ lastProductionName = city . itemBeingProduced . name ;
89+ wasCapital = city . IsCapital ( ) ;
90+
91+ resize ( lastTurnsUntilGrowth , lastTurnsUntilProductFinished , lastCitySize , lastProductionName , wasCapital ) ;
7292 }
7393
74- public override void _Draw ( ) {
75- base . _Draw ( ) ;
94+ private string getCityNameAndGrowthString ( int turnsUntilGrowth ) {
95+ string turnsUntilGrowthText = turnsUntilGrowth == int . MaxValue || turnsUntilGrowth < 0 ? "- -" : turnsUntilGrowth . ToString ( ) ;
96+ return $ "{ city . name } : { turnsUntilGrowthText } ";
97+ }
7698
77- int turnsUntilGrowth = city . TurnsUntilGrowth ( ) ;
78- string turnsUntilGrowthText = turnsUntilGrowth == int . MaxValue || turnsUntilGrowth < 0 ? "- -" : "" + turnsUntilGrowth ;
79- string cityNameAndGrowth = $ "{ city . name } : { turnsUntilGrowthText } ";
80- string productionDescription = city . itemBeingProduced . name + " : " + city . TurnsUntilProductionFinished ( ) ;
99+ private void resize ( int turnsUntilGrowth , int turnsUntilProductionFinished , int citySize , string productionName , bool isCapital ) {
100+ string productionDescription = $ "{ productionName } : { turnsUntilProductionFinished } ";
101+ int productionDescriptionWidth = ( int ) smallFont . GetStringSize ( productionDescription ) . X ;
81102
103+ string cityNameAndGrowth = getCityNameAndGrowthString ( turnsUntilGrowth ) ;
82104 int cityNameAndGrowthWidth = ( int ) smallFont . GetStringSize ( cityNameAndGrowth ) . X ;
83- int productionDescriptionWidth = ( int ) smallFont . GetStringSize ( productionDescription ) . X ;
84105 int maxTextWidth = Math . Max ( cityNameAndGrowthWidth , productionDescriptionWidth ) ;
106+ int cityLabelWidth = maxTextWidth + ( isCapital ? 70 : 45 ) ; //TODO: Is 65 right? 70? Will depend on whether it's capital, too
85107
86- int cityLabelWidth = maxTextWidth + ( city . IsCapital ( ) ? 70 : 45 ) ; //TODO: Is 65 right? 70? Will depend on whether it's capital, too
87- int textAreaWidth = cityLabelWidth - ( city . IsCapital ( ) ? 50 : 25 ) ;
108+ int textAreaWidth = cityLabelWidth - ( isCapital ? 50 : 25 ) ;
88109 if ( log . IsEnabled ( LogEventLevel . Verbose ) ) {
89- log . Verbose ( "Width of city name = " + maxTextWidth ) ;
90110 log . Verbose ( "City label width: " + cityLabelWidth ) ;
91111 log . Verbose ( "Text area width: " + textAreaWidth ) ;
92112 }
93-
94113 if ( cityLabelWidth != lastLabelWidth ) {
95114 Image labelBackground = CreateLabelBackground ( cityLabelWidth , city , textAreaWidth ) ;
96- cityLabel = ImageTexture . CreateFromImage ( labelBackground ) ;
115+ cityLabel = ImageTexture . CreateFromImage ( labelBackground ) ;
97116 lastLabelWidth = cityLabelWidth ;
117+ labelTextureRect . Texture = cityLabel ;
118+ labelTextureRect . OffsetLeft = ( cityLabelWidth / - 2 ) ;
119+ labelTextureRect . OffsetTop = 24 ;
98120 }
99-
100- DrawLabelOnScreen ( tileCenter , cityLabelWidth , city , cityLabel ) ;
101- DrawTextOnLabel ( tileCenter , cityNameAndGrowthWidth , productionDescriptionWidth , city , cityNameAndGrowth , productionDescription , cityLabelWidth ) ;
102- }
103-
104- private void DrawLabelOnScreen ( Vector2I tileCenter , int cityLabelWidth , City city , ImageTexture cityLabel )
105- {
106- labelTextureRect . OffsetLeft = tileCenter . X + ( cityLabelWidth / - 2 ) ;
107- labelTextureRect . OffsetTop = tileCenter . Y + 24 ;
108- labelTextureRect . Texture = cityLabel ;
109- }
110-
111- private void DrawTextOnLabel ( Vector2I tileCenter , int cityNameAndGrowthWidth , int productionDescriptionWidth , City city , string cityNameAndGrowth , string productionDescription , int cityLabelWidth ) {
112-
113- //Destination for font is based on lower-left of baseline of font, not upper left as for blitted rectangles
114- int cityNameOffset = cityNameAndGrowthWidth / - 2 ;
115- int prodDescriptionOffset = productionDescriptionWidth / - 2 ;
116- if ( ! city . IsCapital ( ) ) {
121+ int cityNameOffset = cityNameAndGrowthWidth / - 2 - 8 ;
122+ int prodDescriptionOffset = productionDescriptionWidth / - 2 - 8 ;
123+ if ( ! isCapital ) {
117124 cityNameOffset += 12 ;
118125 prodDescriptionOffset += 12 ;
119126 }
120-
121- cityNameLabel . Theme = smallFontTheme ;
122127 cityNameLabel . Text = cityNameAndGrowth ;
123- cityNameLabel . OffsetLeft = tileCenter . X + cityNameOffset ;
124- cityNameLabel . OffsetTop = tileCenter . Y + 22 ;
128+ cityNameLabel . OffsetLeft = cityNameOffset ;
125129
126- productionLabel . Theme = smallFontTheme ;
127130 productionLabel . Text = productionDescription ;
128- productionLabel . OffsetLeft = tileCenter . X + prodDescriptionOffset ;
129- productionLabel . OffsetTop = tileCenter . Y + 32 ;
131+ productionLabel . OffsetLeft = prodDescriptionOffset ;
130132
131- //City pop size
132- string popSizeString = "" + city . size ;
133+ string popSizeString = citySize . ToString ( ) ;
133134 int popSizeWidth = ( int ) midSizedFont . GetStringSize ( popSizeString ) . X ;
134135 int popSizeOffset = LEFT_RIGHT_BOXES_WIDTH / 2 - popSizeWidth / 2 ;
135136
136- popSizeLabel . Theme = popSizeTheme ;
137-
138- if ( city . TurnsUntilGrowth ( ) < 0 ) {
137+ if ( turnsUntilGrowth < 0 ) {
139138 popSizeLabel . Theme = popThemeRed ;
140139 }
141140
142141 popSizeLabel . Text = popSizeString ;
143- popSizeLabel . OffsetLeft = tileCenter . X + cityLabelWidth / - 2 + popSizeOffset ;
144- popSizeLabel . OffsetTop = tileCenter . Y + 22 ;
142+ popSizeLabel . OffsetLeft = cityLabelWidth / - 2 + popSizeOffset ;
143+ }
144+
145+ public override void _Process ( double delta ) {
146+ int turnsUntilGrowth = city . TurnsUntilGrowth ( ) ;
147+ int turnsUntilProductionFinished = city . TurnsUntilProductionFinished ( ) ;
148+ int citySize = city . size ;
149+ string productionName = city . itemBeingProduced . name ;
150+ bool isCaptial = city . IsCapital ( ) ;
151+
152+ if ( turnsUntilGrowth != lastTurnsUntilGrowth || turnsUntilProductionFinished != lastTurnsUntilProductFinished || citySize != lastCitySize || productionName != lastProductionName || isCaptial != wasCapital ) {
153+ lastTurnsUntilGrowth = turnsUntilGrowth ;
154+ lastTurnsUntilProductFinished = turnsUntilProductionFinished ;
155+ lastCitySize = citySize ;
156+ lastProductionName = productionName ;
157+ wasCapital = isCaptial ;
158+ resize ( turnsUntilGrowth , turnsUntilProductionFinished , citySize , productionName , isCaptial ) ;
159+ }
145160 }
146161
147162 private Image CreateLabelBackground ( int cityLabelWidth , City city , int textAreaWidth )
0 commit comments