1- using Rampastring . XNAUI ;
1+ using Microsoft . Xna . Framework ;
2+ using Rampastring . XNAUI ;
23using Rampastring . XNAUI . XNAControls ;
34using System ;
45using System . Collections . Generic ;
1718
1819namespace TSMapEditor . UI . Windows
1920{
21+ public class ScriptListBoxItemTag
22+ {
23+ public ScriptListBoxItemTag ( ScriptActionEntry scriptActionEntry , string text , string description )
24+ {
25+ ScriptActionEntry = scriptActionEntry ;
26+ Text = text ;
27+ Description = description ;
28+ }
29+
30+ public ScriptActionEntry ScriptActionEntry { get ; set ; }
31+
32+ public string Text { get ; set ; }
33+ public string Description { get ; set ; }
34+
35+ public void Process ( int maxWidth , int fontIndex )
36+ {
37+ DescriptionLines . Clear ( ) ;
38+
39+ if ( Description == null )
40+ return ;
41+
42+ int textWidth = ( int ) Renderer . MeasureString ( Text + " " , fontIndex ) . X ;
43+ int remainingWidth = maxWidth - textWidth ;
44+
45+ int descriptionWidth = ( int ) Renderer . MeasureString ( Description , fontIndex ) . X ;
46+ if ( descriptionWidth > remainingWidth )
47+ {
48+ var lines = Renderer . GetFixedTextLines ( Description , fontIndex , remainingWidth ) ;
49+ DescriptionLines . Add ( lines [ 0 ] ) ;
50+ lines . RemoveAt ( 0 ) ;
51+
52+ string remainingText = string . Join ( " " , lines ) ;
53+ DescriptionLines . AddRange ( Renderer . GetFixedTextLines ( remainingText , fontIndex , maxWidth ) ) ;
54+ }
55+ else
56+ {
57+ DescriptionLines . Add ( Description ) ;
58+ }
59+ }
60+
61+ public List < string > DescriptionLines { get ; } = new List < string > ( ) ;
62+ }
63+
64+ public class ScriptActionListBox : EditorListBox
65+ {
66+ public ScriptActionListBox ( WindowManager windowManager ) : base ( windowManager )
67+ {
68+ AllowMultiLineItems = false ;
69+ }
70+
71+ public ScriptListBoxItemTag TagFromItem ( int index ) => ( ScriptListBoxItemTag ) Items [ index ] . Tag ;
72+
73+ public void AddTaggedItem ( ScriptListBoxItemTag tag )
74+ {
75+ var item = new XNAListBoxItem ( ) { Tag = tag } ;
76+ tag . Process ( Width - GetScrollBarWidth ( ) , FontIndex ) ;
77+ AddItem ( item ) ;
78+ }
79+
80+ public void UpdateItemTag ( int index , string text , string description )
81+ {
82+ var tag = TagFromItem ( index ) ;
83+ tag . Text = text ;
84+ tag . Description = description ;
85+ tag . Process ( Width - GetScrollBarWidth ( ) , FontIndex ) ;
86+ }
87+
88+ protected override int GetListBoxItemLineCount ( XNAListBoxItem listBoxItem )
89+ {
90+ ScriptListBoxItemTag tag = ( ScriptListBoxItemTag ) listBoxItem . Tag ;
91+ if ( tag . DescriptionLines . Count <= 1 )
92+ return 1 ;
93+
94+ return tag . DescriptionLines . Count ;
95+ }
96+
97+ protected override void DrawListBoxItem ( int index , int y )
98+ {
99+ const int ITEM_TEXT_TEXTURE_MARGIN = 2 ;
100+
101+ XNAListBoxItem lbItem = Items [ index ] ;
102+
103+ int x = TextBorderDistance ;
104+
105+ if ( index == SelectedIndex )
106+ {
107+ int drawnWidth ;
108+
109+ if ( DrawSelectionUnderScrollbar || ! ScrollBar . IsDrawn ( ) || ! EnableScrollbar )
110+ {
111+ drawnWidth = Width - 2 ;
112+ }
113+ else
114+ {
115+ drawnWidth = Width - 2 - ScrollBar . Width ;
116+ }
117+
118+ FillRectangle ( new Rectangle ( 1 , y , drawnWidth ,
119+ GetListBoxItemLineCount ( lbItem ) * LineHeight ) ,
120+ FocusColor ) ;
121+ }
122+
123+ if ( lbItem . Texture != null )
124+ {
125+ int textureHeight = lbItem . Texture . Height ;
126+ int textureWidth = lbItem . Texture . Width ;
127+ int textureYPosition = 0 ;
128+
129+ if ( lbItem . Texture . Height > LineHeight )
130+ {
131+ double scaleRatio = textureHeight / ( double ) LineHeight ;
132+ textureHeight = LineHeight ;
133+ textureWidth = ( int ) ( textureWidth / scaleRatio ) ;
134+ }
135+ else
136+ {
137+ textureYPosition = ( LineHeight - textureHeight ) / 2 ;
138+ }
139+
140+ DrawTexture ( lbItem . Texture ,
141+ new Rectangle ( x , y + textureYPosition ,
142+ textureWidth , textureHeight ) , Color . White ) ;
143+
144+ x += textureWidth + ITEM_TEXT_TEXTURE_MARGIN ;
145+ }
146+
147+ x += lbItem . TextXPadding ;
148+
149+ var tag = TagFromItem ( index ) ;
150+ if ( tag . Description == null )
151+ {
152+ DrawStringWithShadow ( tag . Text , FontIndex , new Vector2 ( x , y ) , UISettings . ActiveSettings . AltColor ) ;
153+ }
154+ else
155+ {
156+ string text = tag . Text + " " ;
157+ int width = ( int ) Renderer . MeasureString ( text , FontIndex ) . X ;
158+ DrawStringWithShadow ( text , FontIndex , new Vector2 ( x , y ) , UISettings . ActiveSettings . AltColor ) ;
159+ DrawStringWithShadow ( tag . DescriptionLines [ 0 ] , FontIndex , new Vector2 ( x + width , y ) , UISettings . ActiveSettings . SubtleTextColor ) ;
160+ for ( int i = 1 ; i < tag . DescriptionLines . Count ; i ++ )
161+ {
162+ DrawStringWithShadow ( tag . DescriptionLines [ i ] , FontIndex , new Vector2 ( x , y + ( i * LineHeight ) ) , UISettings . ActiveSettings . SubtleTextColor ) ;
163+ }
164+ }
165+ }
166+ }
167+
20168 public enum ScriptSortMode
21169 {
22170 ID ,
@@ -47,7 +195,7 @@ public ScriptsWindow(WindowManager windowManager, Map map, EditorState editorSta
47195 private EditorListBox lbScriptTypes ;
48196 private EditorSuggestionTextBox tbFilter ;
49197 private EditorTextBox tbName ;
50- private EditorListBox lbActions ;
198+ private ScriptActionListBox lbActions ;
51199 private EditorPopUpSelector selTypeOfAction ;
52200 private XNALabel lblParameterDescription ;
53201 private EditorNumberTextBox tbParameterValue ;
@@ -90,7 +238,7 @@ public override void Initialize()
90238 lbScriptTypes = FindChild < EditorListBox > ( nameof ( lbScriptTypes ) ) ;
91239 tbFilter = FindChild < EditorSuggestionTextBox > ( nameof ( tbFilter ) ) ;
92240 tbName = FindChild < EditorTextBox > ( nameof ( tbName ) ) ;
93- lbActions = FindChild < EditorListBox > ( nameof ( lbActions ) ) ;
241+ lbActions = FindChild < ScriptActionListBox > ( nameof ( lbActions ) ) ;
94242 selTypeOfAction = FindChild < EditorPopUpSelector > ( nameof ( selTypeOfAction ) ) ;
95243 lblParameterDescription = FindChild < XNALabel > ( nameof ( lblParameterDescription ) ) ;
96244 tbParameterValue = FindChild < EditorNumberTextBox > ( nameof ( tbParameterValue ) ) ;
@@ -482,7 +630,8 @@ private void TbParameterValue_TextChanged(object sender, EventArgs e)
482630
483631 ScriptActionEntry entry = editedScript . Actions [ lbActions . SelectedIndex ] ;
484632 entry . Argument = tbParameterValue . Value ;
485- lbActions . SelectedItem . Text = GetActionEntryText ( lbActions . SelectedIndex , entry ) ;
633+ ( string text , string description ) = GetActionEntryText ( lbActions . SelectedIndex , entry ) ;
634+ lbActions . UpdateItemTag ( lbActions . SelectedIndex , text , description ) ;
486635 }
487636
488637 private void TbParameterValue_MouseScrolled ( object sender , InputEventArgs e )
@@ -494,7 +643,8 @@ private void TbParameterValue_MouseScrolled(object sender, InputEventArgs e)
494643
495644 ScriptActionEntry entry = editedScript . Actions [ lbActions . SelectedIndex ] ;
496645 entry . Argument = Cursor . ScrollWheelValue > 0 ? entry . Argument - 1 : entry . Argument + 1 ;
497- lbActions . SelectedItem . Text = GetActionEntryText ( lbActions . SelectedIndex , entry ) ;
646+ ( string text , string description ) = GetActionEntryText ( lbActions . SelectedIndex , entry ) ;
647+ lbActions . UpdateItemTag ( lbActions . SelectedIndex , text , description ) ;
498648
499649 tbParameterValue . TextChanged -= TbParameterValue_TextChanged ;
500650 ScriptAction action = map . EditorConfig . ScriptActions . GetValueOrDefault ( entry . Action ) ;
@@ -623,7 +773,8 @@ private void SelectScriptActionDarkeningPanel_Hidden(object sender, EventArgs e)
623773 {
624774 ScriptActionEntry entry = editedScript . Actions [ lbActions . SelectedIndex ] ;
625775 entry . Action = selectScriptActionWindow . SelectedObject . ID ;
626- lbActions . Items [ lbActions . SelectedIndex ] . Text = GetActionEntryText ( lbActions . SelectedIndex , entry ) ;
776+ ( string text , string description ) = GetActionEntryText ( lbActions . SelectedIndex , entry ) ;
777+ lbActions . UpdateItemTag ( lbActions . SelectedIndex , text , description ) ;
627778 }
628779 }
629780
@@ -710,8 +861,6 @@ private void SetParameterEntryText(ScriptActionEntry scriptActionEntry, ScriptAc
710861 return ;
711862 }
712863
713- lbActions . SelectedItem . Text = GetActionEntryText ( scriptActionEntry . Action , scriptActionEntry ) ;
714-
715864 if ( action . ParamType == TriggerParamType . BuildingWithProperty )
716865 {
717866 tbParameterValue . Text = GetBuildingWithPropertyText ( scriptActionEntry . Argument ) ;
@@ -928,11 +1077,8 @@ private void EditScript(Script script)
9281077 for ( int i = 0 ; i < editedScript . Actions . Count ; i ++ )
9291078 {
9301079 var actionEntry = editedScript . Actions [ i ] ;
931- lbActions . AddItem ( new XNAListBoxItem ( )
932- {
933- Text = GetActionEntryText ( i , actionEntry ) ,
934- Tag = actionEntry
935- } ) ;
1080+ ( string text , string description ) = GetActionEntryText ( i , actionEntry ) ;
1081+ lbActions . AddTaggedItem ( new ScriptListBoxItemTag ( actionEntry , text , description ) ) ;
9361082 }
9371083
9381084 ddScriptColor . SelectedIndex = ddScriptColor . Items . FindIndex ( item => item . Text == editedScript . EditorColor ) ;
@@ -944,16 +1090,16 @@ private void EditScript(Script script)
9441090 lbActions . ScrollToSelectedElement ( ) ;
9451091 }
9461092
947- private string GetActionEntryText ( int index , ScriptActionEntry entry )
1093+ private ( string text , string description ) GetActionEntryText ( int index , ScriptActionEntry entry )
9481094 {
9491095 string actionEntryText ;
9501096 string textDetails = null ;
9511097
9521098 ScriptAction action = GetScriptAction ( entry . Action ) ;
9531099 if ( action == null )
954- {
1100+ {
9551101 actionEntryText = $ "#{ index } - Unknown";
956- textDetails = $ "( { entry . Argument . ToString ( CultureInfo . InvariantCulture ) } ) ";
1102+ textDetails = $ "{ entry . Argument . ToString ( CultureInfo . InvariantCulture ) } ";
9571103 }
9581104 else
9591105 {
@@ -969,49 +1115,46 @@ private string GetActionEntryText(int index, ScriptActionEntry entry)
9691115 BuildingType buildingType = map . Rules . BuildingTypes . GetElementIfInRange ( buildingTypeIndex ) ;
9701116
9711117 if ( buildingType != null )
972- textDetails = $ "( { buildingType . Index } - { buildingType . GetEditorDisplayName ( ) } - { propertyDescription } ) ";
1118+ textDetails = $ "{ entry . Argument } - { buildingType . GetEditorDisplayName ( ) } - { propertyDescription } ";
9731119 break ;
9741120
9751121 case TriggerParamType . LocalVariable :
9761122 var localVar = map . LocalVariables . GetElementIfInRange ( entry . Argument ) ;
9771123 if ( localVar != null )
978- textDetails = $ "( { localVar . Index } - { localVar . Name } ) ";
1124+ textDetails = $ "{ localVar . Index } - { localVar . Name } ";
9791125 break ;
9801126
9811127 case TriggerParamType . HouseType :
9821128 var houseType = map . Houses . GetElementIfInRange ( entry . Argument ) ;
9831129 if ( houseType != null )
984- textDetails = $ "( { houseType . ID } - { houseType . ININame } ) ";
1130+ textDetails = $ "{ houseType . ID } - { houseType . ININame } ";
9851131 break ;
9861132
9871133 case TriggerParamType . Animation :
9881134 var animation = map . Rules . AnimTypes . GetElementIfInRange ( entry . Argument ) ;
9891135 if ( animation != null )
990- textDetails = $ "( { animation . Index } - { animation . ININame } ) ";
1136+ textDetails = $ "{ animation . Index } - { animation . ININame } ";
9911137 break ;
9921138
9931139 case TriggerParamType . Unknown :
9941140 // special handling: script actions that have no type but still have presets should be handled by showing the preset value
9951141 // otherwise we'll show no text after the name of the action
996- if ( hasValidPresetOption )
1142+ if ( hasValidPresetOption )
9971143 goto default ;
9981144
9991145 textDetails = null ;
10001146 break ;
10011147
10021148 default :
10031149 if ( hasValidPresetOption )
1004- textDetails = $ "( { presetOptionIndex } - { action . PresetOptions [ presetOptionIndex ] . Text } ) ";
1150+ textDetails = $ "{ presetOptionIndex } - { action . PresetOptions [ presetOptionIndex ] . Text } ";
10051151 else
1006- textDetails = $ "( { entry . Argument . ToString ( CultureInfo . InvariantCulture ) } ) ";
1152+ textDetails = $ "{ entry . Argument . ToString ( CultureInfo . InvariantCulture ) } ";
10071153 break ;
10081154 }
10091155 }
10101156
1011- if ( string . IsNullOrEmpty ( textDetails ) )
1012- return actionEntryText ;
1013-
1014- return $ "{ actionEntryText } { textDetails } ";
1157+ return ( actionEntryText , textDetails ) ;
10151158 }
10161159
10171160 private string GetActionNameFromIndex ( int index )
0 commit comments