-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
+1
View File
@@ -115,6 +115,7 @@
Name="sNameFile_RichTxBx"
TextChanged="sNameFile_RichTxBx_TextChanged"
PreviewKeyDown="sNameFile_RichTxBx_PreviewKeyDown"
PreviewMouseWheel="sNameFile_RichTxBx_PreviewMouseWheel"
Style="{StaticResource Script_RichTxBx}">
<FlowDocument Name="FDocumentFile">
<Paragraph>
+20 -1
View File
@@ -22,7 +22,8 @@
End Sub
Private Sub sNameFile_RichTxBx_TextChanged(sender As Object, e As TextChangedEventArgs)
If Not IsNothing(Map.refScriptWindowVM) Then Map.refScriptWindowVM.ChangeTextColor()
'If Not IsNothing(Map.refScriptWindowVM) Then Map.refScriptWindowVM.ChangeTextColor()
If Not IsNothing(Map.refScriptWindowVM) Then Map.refScriptWindowVM.SyntaxHighlighting(sNameFile_RichTxBx)
End Sub
Private Sub sNameFile_RichTxBx_SelectionChanged(sender As Object, e As RoutedEventArgs)
@@ -33,9 +34,27 @@
If e.Key = Key.Enter Then
Map.refScriptWindowVM.InsertText(sNameFile_RichTxBx, vbCrLf)
e.Handled = True
ElseIf e.Key = Key.Tab Then
e.Handled = True
' Ottieni la posizione corrente del cursore
Dim caretPosition As TextPointer = sNameFile_RichTxBx.CaretPosition
' Inserisci un carattere di tabulazione (3 spazi)
Dim tabString As String = " "
Dim run As New Run(tabString)
Dim textRange As New TextRange(caretPosition, caretPosition)
textRange.Text = tabString
' Aggiorna la posizione del cursore
sNameFile_RichTxBx.CaretPosition = textRange.End
End If
End Sub
Private Sub sNameFile_RichTxBx_PreviewMouseWheel(sender As Object, e As MouseWheelEventArgs)
' Ottieni il numero di righe da scorrere
Dim linesToScroll As Integer = e.Delta / 5
' Scorri il contenuto della RichTextBox
sNameFile_RichTxBx.ScrollToVerticalOffset(sNameFile_RichTxBx.VerticalOffset - linesToScroll)
End Sub
#End Region ' Events
End Class
+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