-piccola modifica allo script

This commit is contained in:
Demetrio Cassarino
2025-02-26 14:55:09 +01:00
parent 2023a6217d
commit 7d1b2004d9
6 changed files with 103 additions and 7 deletions
+75 -2
View File
@@ -301,7 +301,8 @@ Public Class ScriptWindowVM
End If
End If
Dim bOk = Map.refSceneHostVM.SaveProj(sFileName)
File.WriteAllText(sFileName, m_sRichTextParagraph)
Dim sFileScript As String = RichTextBox(sFileName)
File.WriteAllText(sFileName, sFileScript)
' Imposto stato gestione mouse diretto della scena a nessuno
Map.refSceneHostVM.MainScene.SetStatusNull()
Return True
@@ -550,6 +551,78 @@ Public Class ScriptWindowVM
End Sub
Public Sub SyntaxHighlighting(rtb As RichTextBox)
Dim keywords As String() = {"local ", "function ", "end", " if ", " then ", " else ", " for ", "while", "do", "return"}
Dim colorsArray As String() = {"WHITE", "LGRAY", "GRAY", "BLACK", "RED", "MAROON", "YELLOW", "OLIVE", "LIME", "GREEN",
"AQUA", "TEAL", "BLUE", "NAVY", "FUCHSIA", "PURPLE", "ORANGE", "BROWN"}
Dim keywordColor As Color = Colors.Blue
Dim commentColor As Color = Colors.Green
Dim infoColor As New System.Windows.Media.Color With {
.A = 255,
.R = 206,
.G = 145,
.B = 120
}
' Evidenzia le parole chiave
For Each keyword In keywords
HighlightText(rtb, keyword, keywordColor)
Next
' Evidenzia le info
For Each color In colorsArray
HighlightText(rtb, "'" & color & "'", infoColor)
Next
' Evidenzia i commenti
HighlightText(rtb, "--", commentColor, True)
End Sub
Private Sub HighlightText(rtb As RichTextBox, word As String, color As Color, Optional isComment As Boolean = False)
Dim textRange As New TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd)
Dim text As String = textRange.Text
Dim startIndex As Integer = 0
While startIndex < text.Length
If text.Contains(word) Then
startIndex = text.IndexOf(word, startIndex)
If startIndex = -1 Then Exit While
Dim startPointer As TextPointer = GetTextPosition(rtb.Document.ContentStart, startIndex)
Dim endPointer As TextPointer = GetTextPosition(startPointer, word.Length)
Dim range As New TextRange(startPointer, endPointer)
range.ApplyPropertyValue(TextElement.ForegroundProperty, New SolidColorBrush(color))
If isComment Then
Dim commentEndIndex As Integer = text.IndexOf(Environment.NewLine, startIndex)
If commentEndIndex = -1 Then commentEndIndex = text.Length
endPointer = GetTextPosition(startPointer, commentEndIndex - startIndex)
range = New TextRange(startPointer, endPointer)
range.ApplyPropertyValue(TextElement.ForegroundProperty, New SolidColorBrush(color))
End If
End If
startIndex += word.Length
End While
End Sub
Private Function GetTextPosition(start As TextPointer, position As Integer) As TextPointer
Dim navigator As TextPointer = start
While navigator IsNot Nothing AndAlso position > 0
If navigator.GetPointerContext(LogicalDirection.Forward) = TextPointerContext.Text Then
Dim run As String = navigator.GetTextInRun(LogicalDirection.Forward)
Dim count As Integer = Math.Min(run.Length, position)
navigator = navigator.GetPositionAtOffset(count)
position -= count
Else
navigator = navigator.GetPositionAtOffset(1)
End If
End While
Return navigator
End Function
#End Region ' Methods
#Region "COMMANDS"
@@ -567,7 +640,7 @@ Public Class ScriptWindowVM
Public Sub Conferma()
' Recupero il file script
Dim sFileScript As String = RichTextBox(Map.refMainWindowVM.MainWindowM.sTempDir & "\" & "m_sNameFile" & ".lua")
Dim sFileScript As String = RichTextBox(Map.refMainWindowVM.MainWindowM.sTempDir & "\" & m_sNameFile & ".lua")
' Salvo il file
SaveScript(Map.refMainWindowVM.MainWindowM.sTempDir & "\" & m_sNameFile & ".lua", sFileScript)
' Eseguo il file