1+ using System ;
2+ using UnityEditor ;
3+ using UnityEditor . Localization . UI ;
4+ using UnityEngine ;
5+
6+ namespace RedGame . Framework . EditorTools
7+ {
8+ public partial class LocalizeGptWindow
9+ {
10+ enum OutputType
11+ {
12+ None , Prompt , Error ,
13+ }
14+
15+ private SimpleEditorTableView < TranslateRec > _tableView ;
16+ private Vector2 _scrollPosition ;
17+ private string _outputStr ;
18+ private OutputType _outputType ;
19+ private bool _gptFoldout = true ;
20+ private bool _collectionFoldout = true ;
21+
22+ private void OnGUI ( )
23+ {
24+ if ( IsBusy ( ) )
25+ {
26+ OnBusyGUI ( ) ;
27+ return ;
28+ }
29+
30+ OnGptModelGUI ( ) ;
31+ EditorGUILayout . Space ( ) ;
32+ OnSelectCollectionGUI ( ) ;
33+ EditorGUILayout . Space ( ) ;
34+ if ( ! _curCollection )
35+ {
36+ return ;
37+ }
38+
39+ OnEntryListGUI ( ) ;
40+ OnOutputGUI ( ) ;
41+ }
42+
43+ private SimpleEditorTableView < TranslateRec > CreateTable ( )
44+ {
45+ SimpleEditorTableView < TranslateRec > tableView = new SimpleEditorTableView < TranslateRec > ( ) ;
46+
47+ GUIStyle labelGUIStyle = new GUIStyle ( GUI . skin . label )
48+ {
49+ padding = new RectOffset ( left : 10 , right : 10 , top : 2 , bottom : 2 )
50+ } ;
51+
52+ tableView . AddColumn ( "" , 30 , ( rect , rec ) =>
53+ {
54+ rec . selected = EditorGUI . Toggle (
55+ position : rect ,
56+ value : rec . selected
57+ ) ;
58+ } ) . SetMaxWidth ( 40 ) . SetSorting ( ( a , b ) => a . selected . CompareTo ( b . selected ) ) ;
59+
60+ tableView . AddColumn ( "Key" , 80 , ( rect , rec ) =>
61+ {
62+ EditorGUI . LabelField (
63+ position : rect ,
64+ label : rec . key ,
65+ style : labelGUIStyle
66+ ) ;
67+ } ) . SetAutoResize ( true ) . SetSorting ( ( a , b ) => String . Compare ( a . key , b . key , StringComparison . Ordinal ) ) ;
68+
69+ tableView . AddColumn ( "Src Locales" , 100 , ( rect , rec ) =>
70+ {
71+ EditorGUI . LabelField (
72+ position : rect ,
73+ label : rec . srcLangNames ,
74+ style : labelGUIStyle
75+ ) ;
76+ } ) . SetAllowToggleVisibility ( true ) ;
77+
78+ tableView . AddColumn ( "Dst Locales" , 100 , ( rect , rec ) =>
79+ {
80+ EditorGUI . LabelField (
81+ position : rect ,
82+ label : string . Join ( ',' , rec . dstLangNames ) ,
83+ style : labelGUIStyle
84+ ) ;
85+ } ) . SetAllowToggleVisibility ( true ) ;
86+
87+ tableView . AddColumn ( "Operation" , 180 , ( rect , rec ) =>
88+ {
89+ Rect rt1 = new Rect ( rect . x , rect . y , rect . width / 2 , rect . height ) ;
90+
91+ if ( GUI . Button ( rt1 , "Show Prompt" ) )
92+ {
93+ RefreshRecord ( rec . key ) ;
94+ Output ( "System Prompt: \n " + rec . systemPrompt + "\n User Prompt:\n " + rec . prompt , OutputType . Prompt ) ;
95+ }
96+
97+ Rect rt2 = new Rect ( rect . x + rect . width / 2 , rect . y , rect . width / 2 , rect . height ) ;
98+
99+ if ( GUI . Button ( rt2 , "Translate" ) )
100+ {
101+ TranslateSingleRec ( rec ) ;
102+ }
103+ } ) ;
104+ return tableView ;
105+ }
106+
107+ private void OnSelectCollectionGUI ( )
108+ {
109+ if ( _collections == null || _recs == null )
110+ {
111+ RefreshStringTableCollection ( ) ;
112+ }
113+
114+ if ( _collections == null || _collections . Length == 0 )
115+ {
116+ EditorGUILayout . BeginHorizontal ( ) ;
117+ EditorGUILayout . HelpBox ( "No StringTableCollection found.\n " +
118+ "Please create at least one collection with Unity Localization" , MessageType . Error ) ;
119+ EditorGUILayout . EndHorizontal ( ) ;
120+
121+ if ( GUILayout . Button ( "Create Collection" ) )
122+ {
123+ LocalizationTablesWindow . ShowWindow ( ) ;
124+ }
125+
126+ return ;
127+ }
128+
129+ _collectionFoldout = EditorGUILayout . BeginFoldoutHeaderGroup ( _collectionFoldout , "String Table Collection" ) ;
130+
131+ if ( _collectionFoldout )
132+ {
133+ EditorGUI . indentLevel ++ ;
134+ EditorGUILayout . BeginVertical ( "Box" ) ;
135+ EditorGUILayout . BeginHorizontal ( ) ;
136+ int index = Array . IndexOf ( _collections , _curCollection ) ;
137+ index = EditorGUILayout . Popup ( "Select Collection" , index , _collectionNames ) ;
138+ if ( index >= 0 && index < _collections . Length )
139+ {
140+ _curCollection = _collections [ index ] ;
141+ }
142+
143+ EditorGUILayout . EndHorizontal ( ) ;
144+
145+ EditorGUILayout . EndVertical ( ) ;
146+ EditorGUI . indentLevel -- ;
147+ }
148+ EditorGUILayout . EndFoldoutHeaderGroup ( ) ;
149+ }
150+
151+ private void OnGptModelGUI ( )
152+ {
153+ _gptFoldout = EditorGUILayout . BeginFoldoutHeaderGroup ( _gptFoldout , "GPT Model Parameters" ) ;
154+
155+ if ( _gptFoldout )
156+ {
157+ EditorGUI . indentLevel ++ ;
158+ EditorGUI . BeginChangeCheck ( ) ;
159+ EditorGUILayout . BeginVertical ( "Box" ) ;
160+ _baseUrl = EditorGUILayout . TextField ( "Base URL" , _baseUrl ) ;
161+ if ( string . IsNullOrEmpty ( _baseUrl ) )
162+ _baseUrl = DEFAULT_BASE_URL ;
163+
164+ _apiKey = EditorGUILayout . PasswordField ( "API Key" , _apiKey ) ;
165+
166+ int index = Array . IndexOf ( _validModels , _model ) ;
167+ index = EditorGUILayout . Popup ( "Model" , index , _validModels ) ;
168+ if ( index >= 0 && index < _validModels . Length )
169+ {
170+ _model = _validModels [ index ] ;
171+ }
172+
173+ _temperature = EditorGUILayout . Slider ( "Temperature" , _temperature , 0 , 1 ) ;
174+ EditorGUILayout . EndVertical ( ) ;
175+
176+ if ( EditorGUI . EndChangeCheck ( ) )
177+ {
178+ SaveSettings ( ) ;
179+ }
180+
181+ EditorGUI . indentLevel -- ;
182+ }
183+
184+ EditorGUILayout . EndFoldoutHeaderGroup ( ) ;
185+
186+ }
187+
188+ private void OnBusyGUI ( )
189+ {
190+ TranslateRec rec = _pendingRecs [ _currentPendingRecIndex ] ;
191+ EditorGUILayout . Space ( ) ;
192+
193+ EditorGUILayout . LabelField ( "Translating " , EditorStyles . boldLabel ) ;
194+
195+ EditorGUILayout . Space ( ) ;
196+
197+ EditorGUILayout . BeginHorizontal ( ) ;
198+ Rect progressRect = GUILayoutUtility . GetRect ( 100 , EditorGUIUtility . singleLineHeight , GUILayout . ExpandWidth ( true ) ) ;
199+ EditorGUI . ProgressBar ( progressRect , GetProgress ( ) ,
200+ $ "{ rec . key } ({ _currentPendingRecIndex + 1 } /{ _pendingRecs . Length } )") ;
201+ if ( GUILayout . Button ( "Cancel" , GUILayout . Width ( 100 ) ) )
202+ {
203+ CancelTask ( ) ;
204+ }
205+ EditorGUILayout . EndHorizontal ( ) ;
206+
207+ EditorGUILayout . BeginVertical ( "Box" ) ;
208+ EditorGUILayout . LabelField ( "System Prompt: " , EditorStyles . boldLabel ) ;
209+ EditorGUI . indentLevel ++ ;
210+ EditorGUILayout . LabelField ( rec . systemPrompt , EditorStyles . wordWrappedLabel ) ;
211+ EditorGUI . indentLevel -- ;
212+ EditorGUILayout . Space ( ) ;
213+
214+ EditorGUILayout . LabelField ( "User Prompt: " , EditorStyles . boldLabel ) ;
215+ EditorGUI . indentLevel ++ ;
216+ EditorGUILayout . LabelField ( rec . prompt , EditorStyles . wordWrappedLabel ) ;
217+ EditorGUI . indentLevel -- ;
218+ EditorGUILayout . Space ( ) ;
219+ EditorGUILayout . EndVertical ( ) ;
220+ }
221+
222+ private void OnOutputGUI ( )
223+ {
224+ if ( ! string . IsNullOrEmpty ( _outputStr ) && _outputType != OutputType . None )
225+ {
226+ EditorGUILayout . Space ( ) ;
227+ EditorGUILayout . LabelField ( _outputType . ToString ( ) , EditorStyles . boldLabel ) ;
228+ EditorGUILayout . Space ( ) ;
229+ if ( _outputType == OutputType . Error )
230+ {
231+ EditorGUILayout . HelpBox ( _outputStr , MessageType . Error ) ;
232+ } else
233+ {
234+ _scrollPosition = EditorGUILayout . BeginScrollView ( _scrollPosition , GUILayout . Height ( 100 ) ) ;
235+ EditorGUILayout . TextArea ( _outputStr , GUILayout . ExpandHeight ( true ) ) ;
236+ EditorGUILayout . EndScrollView ( ) ;
237+ }
238+ }
239+ }
240+
241+ private void OnEntryListGUI ( )
242+ {
243+ EditorGUILayout . LabelField ( "Entries to be localized" , EditorStyles . boldLabel ) ;
244+ if ( GUILayout . Button ( "Open Table Editor" ) )
245+ {
246+ LocalizationTablesWindow . ShowWindow ( _curCollection ) ;
247+ }
248+ if ( _recs == null )
249+ RefreshRecords ( ) ;
250+
251+ if ( _recs == null || _recs . Length == 0 )
252+ {
253+ EditorGUILayout . HelpBox ( "No Entry to translate.\n " +
254+ "Only entries with partial language translations are displayed here.\n " +
255+ "Edit entries in Localization Table Editor and press [Refresh List] button" ,
256+ MessageType . Warning ) ;
257+ return ;
258+ }
259+
260+ EditorGUILayout . BeginHorizontal ( ) ;
261+
262+ if ( GUILayout . Button ( "Select All" , GUILayout . Width ( 100 ) ) )
263+ {
264+ foreach ( var rec in _recs )
265+ {
266+ rec . selected = true ;
267+ }
268+ }
269+
270+ if ( GUILayout . Button ( "Deselect All" , GUILayout . Width ( 100 ) ) )
271+ {
272+ foreach ( var rec in _recs )
273+ {
274+ rec . selected = false ;
275+ }
276+ }
277+
278+ if ( GUILayout . Button ( "Translate Selected" , GUILayout . ExpandWidth ( true ) ) )
279+ {
280+ TranslateSelectedRecs ( ) ;
281+ }
282+
283+ EditorGUILayout . EndHorizontal ( ) ;
284+
285+ EditorGUILayout . Space ( ) ;
286+ _tableView ??= CreateTable ( ) ;
287+ _tableView . DrawTableGUI ( _recs , ( _recs . Length + 2 ) * EditorGUIUtility . singleLineHeight ) ;
288+ }
289+
290+ }
291+ }
0 commit comments