Imports EgtUILib Imports EgtWPFLib5 Imports System.IO Imports System.Text Public Class ScriptWindowVM Inherits VMBase #Region "FIELD & PROPERTIES" Friend m_bTextHasChanged As Boolean = False Private m_sNameFile As String = String.Empty Public Property sNameFile As String Get Return m_sNameFile End Get Set(value As String) m_sNameFile = value NotifyPropertyChanged(NameOf(sNameFile)) End Set End Property Private m_sRichTextParagraph As String = String.Empty Public Property sRichTextParagraph As String Get Return m_sRichTextParagraph End Get Set(value As String) m_sRichTextParagraph = value NotifyPropertyChanged(NameOf(sRichTextParagraph)) End Set End Property Private m_nFontSizeList As New List(Of Integer) Public Property FontSizeList As List(Of Integer) Get Return m_nFontSizeList End Get Set(value As List(Of Integer)) m_nFontSizeList = value NotifyPropertyChanged(NameOf(FontSizeList)) End Set End Property Private m_nSelFontSize As Integer Public Property SelFontSize As Integer Get Return m_nSelFontSize End Get Set(value As Integer) m_nSelFontSize = value If Not IsNothing(m_nSelFontSize) Then ChangeFontSize(m_nSelFontSize) NotifyPropertyChanged(NameOf(SelFontSize)) End Set End Property Friend Sub SetSelFontSize(value As Integer) m_nSelFontSize = value NotifyPropertyChanged(NameOf(SelFontSize)) End Sub Private m_sFontFamilyList As New List(Of FontFamily) Public Property FontFamilyList As List(Of FontFamily) Get Return m_sFontFamilyList End Get Set(value As List(Of FontFamily)) m_sFontFamilyList = value NotifyPropertyChanged(NameOf(FontFamilyList)) End Set End Property Private m_sSelFontFamily As FontFamily Public Property SelFontFamily As FontFamily Get Return m_sSelFontFamily End Get Set(value As FontFamily) m_sSelFontFamily = value If Not IsNothing(m_sSelFontFamily) Then ChangeFontFamily(m_sSelFontFamily) NotifyPropertyChanged(NameOf(SelFontFamily)) End Set End Property Friend Sub SetSelFontFamily(value As FontFamily) m_sSelFontFamily = value NotifyPropertyChanged(NameOf(SelFontFamily)) End Sub Private m_bBoldIsChecked As Boolean = False Public Property BoldIsChecked As Boolean Get Return m_bBoldIsChecked End Get Set(value As Boolean) m_bBoldIsChecked = value NotifyPropertyChanged(NameOf(BoldIsChecked)) End Set End Property Private m_bItalicIsChecked As Boolean = False Public Property ItalicIsChecked As Boolean Get Return m_bItalicIsChecked End Get Set(value As Boolean) m_bItalicIsChecked = value NotifyPropertyChanged(NameOf(ItalicIsChecked)) End Set End Property Private m_bUnderlineChecked As Boolean = False Public Property UnderlineChecked As Boolean Get Return m_bUnderlineChecked End Get Set(value As Boolean) m_bUnderlineChecked = value NotifyPropertyChanged(NameOf(UnderlineChecked)) End Set End Property Private m_bLeftIsChecked As Boolean = False Public Property LeftIsChecked As Boolean Get Return m_bLeftIsChecked End Get Set(value As Boolean) m_bLeftIsChecked = value SetCenterIsChecked(False) SetJustifyIsChecked(False) NotifyPropertyChanged(NameOf(LeftIsChecked)) End Set End Property Friend Sub SetLeftIsChecked(value As Boolean) m_bLeftIsChecked = value NotifyPropertyChanged(NameOf(LeftIsChecked)) End Sub Private m_bCenterIsChecked As Boolean = False Public Property CenterIsChecked As Boolean Get Return m_bCenterIsChecked End Get Set(value As Boolean) m_bCenterIsChecked = value SetLeftIsChecked(False) SetJustifyIsChecked(False) NotifyPropertyChanged(NameOf(CenterIsChecked)) End Set End Property Friend Sub SetCenterIsChecked(value As Boolean) m_bCenterIsChecked = value NotifyPropertyChanged(NameOf(CenterIsChecked)) End Sub Private m_bJustifyIsChecked As Boolean = False Public Property JustifyIsChecked As Boolean Get Return m_bJustifyIsChecked End Get Set(value As Boolean) m_bJustifyIsChecked = value SetLeftIsChecked(False) SetCenterIsChecked(False) NotifyPropertyChanged(NameOf(JustifyIsChecked)) End Set End Property Friend Sub SetJustifyIsChecked(value As Boolean) m_bJustifyIsChecked = value NotifyPropertyChanged(NameOf(JustifyIsChecked)) End Sub Private m_FileAlignLeftSVG As String = String.Empty Public Property FileAlignLeftSVG As String Get Return m_FileAlignLeftSVG End Get Set(value As String) m_FileAlignLeftSVG = value NotifyPropertyChanged(NameOf(FileAlignLeftSVG)) End Set End Property Friend Sub SetFileAlignLeftSVG(sFileAlignLeftSVG As String) m_FileAlignLeftSVG = sFileAlignLeftSVG NotifyPropertyChanged(NameOf(FileAlignLeftSVG)) End Sub Private m_FileAlignCenterSVG As String = String.Empty Public Property FileAlignCenterSVG As String Get Return m_FileAlignCenterSVG End Get Set(value As String) m_FileAlignCenterSVG = value NotifyPropertyChanged(NameOf(FileAlignCenterSVG)) End Set End Property Friend Sub SetFileAlignCenterSVG(sFileAlignCenterSVG As String) m_FileAlignCenterSVG = sFileAlignCenterSVG NotifyPropertyChanged(NameOf(FileAlignCenterSVG)) End Sub Private m_FileAlignJustifySVG As String = String.Empty Public Property FileAlignJustifySVG As String Get Return m_FileAlignJustifySVG End Get Set(value As String) m_FileAlignJustifySVG = value NotifyPropertyChanged(NameOf(FileAlignJustifySVG)) End Set End Property Friend Sub SetFileAlignJustifySVG(sFileAlignJustifySVG As String) m_FileAlignJustifySVG = sFileAlignJustifySVG NotifyPropertyChanged(NameOf(FileAlignJustifySVG)) End Sub #Region "Messages" Public ReadOnly Property Title As String Get Return "Script File" ' Opzioni End Get End Property Public ReadOnly Property Conferma_Msg As String Get Return EgtMsg(110003) ' Conferma End Get End Property Public ReadOnly Property Salva_Msg As String Get Return EgtMsg(110013) ' Salva End Get End Property Public ReadOnly Property Annulla_Msg As String Get Return EgtMsg(110004) ' Annulla End Get End Property #End Region ' Messages ' Definizione Comandi Private m_ConfermaCmd As ICommand Private m_cmdSaveAs As ICommand Private m_cmdAnnulla As ICommand Private m_cmdTextColor As ICommand #End Region ' Fields & Properties #Region "CONSTRUCTOR" Sub New() Map.SetRefScriptWindowVM(Me) If m_nFontSizeList.Count <= 0 Then CreateFontSizeList() If m_sFontFamilyList.Count <= 0 Then CreateFontFamilyList() SetToolbar() SetFileAlignLeftSVG(Map.refMainWindowVM.MainWindowM.sResourcesDir & "\alignleft.svg") SetFileAlignCenterSVG(Map.refMainWindowVM.MainWindowM.sResourcesDir & "\aligncenter.svg") SetFileAlignJustifySVG(Map.refMainWindowVM.MainWindowM.sResourcesDir & "\alignjustify.svg") End Sub #End Region ' Constructor #Region "METHODS" Public Function SaveProject() As Boolean Dim sFile As String = "" Dim sFileName As String = "" ' Se nome vuoto, assegno "New" If String.IsNullOrWhiteSpace(m_sNameFile) Then sFile = "New.lua" Else sFile = m_sNameFile & ".lua" End If ' Eventuale sistemazione estensione sFile = IO.Path.ChangeExtension(sFile, "lua") ' Assegnazione nome file con dialogo Dim SaveFileDialog As New EgtManageFileDialogV(Application.Current.MainWindow, New EgtManageFileDialogVM()) With { .Title = EgtMsg(110013), ' Salva .Filter = "lua files (*.lua)|*.lua", .FileName = sFile, .FilterIndex = 1, .InitialDirectory = Map.refMainWindowVM.MainWindowM.sTempDir, .ValidateNames = False, .OverwritePrompt = True, .Mode = 1 } If SaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then If Path.GetExtension(SaveFileDialog.SafeFileName).Equals(String.Empty) Then sFileName = SaveFileDialog.InitialDirectory & "\" & SaveFileDialog.SafeFileName & SaveFileDialog.SelFilter.sExstension.Trim("*"c) Else sFileName = SaveFileDialog.InitialDirectory & "\" & SaveFileDialog.SafeFileName End If End If Dim bOk = Map.refSceneHostVM.SaveProj(sFileName) File.WriteAllText(sFileName, m_sRichTextParagraph) ' Imposto stato gestione mouse diretto della scena a nessuno Map.refSceneHostVM.MainScene.SetStatusNull() Return True End Function Private Sub SaveScript(sPathFileScript As String, sRichTextParagraph As String) Map.refSceneHostVM.SaveProj(sPathFileScript) File.WriteAllText(sPathFileScript, sRichTextParagraph, Encoding.UTF8) End Sub Public Function RichTextBox(sPathFileScript As String) As String ' Recupero richtextbox Dim myRichTextBox As RichTextBox = Map.refScriptWindowV.sNameFile_RichTxBx ' Recupero flowdocument Dim myFlowDoc As FlowDocument = Map.refScriptWindowV.FDocumentFile ' Assegno al richtextbox il flowdocument myRichTextBox.Document = myFlowDoc ' Leggo il richtextbox dall'inizio alla fine Dim textRange As New TextRange(myRichTextBox.Document.ContentStart, myRichTextBox.Document.ContentEnd) Return textRange.Text End Function Private Sub CreateFontSizeList() For Size As Integer = 8 To 20 If Size Mod 2 = 0 Then m_nFontSizeList.Add(Size) End If Next End Sub ''' ''' Funzione che permette di cambiare la dimensione della font ''' ''' fontsize selezionata Private Sub ChangeFontSize(nSelFontSize As Integer) If nSelFontSize = GDB_ID.NULL Then Return Dim pixelSize As Double = Convert.ToDouble(nSelFontSize) * (96 / 72) Dim textRange = New TextRange(Map.refScriptWindowV.sNameFile_RichTxBx.Selection.Start, Map.refScriptWindowV.sNameFile_RichTxBx.Selection.[End]) textRange.ApplyPropertyValue(TextElement.FontSizeProperty, pixelSize) End Sub Private Sub CreateFontFamilyList() For Each FontFamily As FontFamily In Fonts.SystemFontFamilies m_sFontFamilyList.Add(FontFamily) Next End Sub Private Sub ChangeFontFamily(sSelFontFamily As FontFamily) If sSelFontFamily Is Nothing Then Return Dim fontFamily As String = sSelFontFamily.ToString() Dim textRange = New TextRange(Map.refScriptWindowV.sNameFile_RichTxBx.Selection.Start, Map.refScriptWindowV.sNameFile_RichTxBx.Selection.[End]) textRange.ApplyPropertyValue(TextElement.FontFamilyProperty, fontFamily) End Sub Friend Sub SetToolbar() Dim textRange = New TextRange(Map.refScriptWindowV.sNameFile_RichTxBx.Selection.Start, Map.refScriptWindowV.sNameFile_RichTxBx.Selection.[End]) Dim fontFamily As Object = textRange.GetPropertyValue(TextElement.FontFamilyProperty) SetSelFontFamily(fontFamily) Dim fontSize As Object = textRange.GetPropertyValue(TextElement.FontSizeProperty) SetSelFontSize(fontSize) If Not String.IsNullOrEmpty(textRange.Text) Then m_bBoldIsChecked = textRange.GetPropertyValue(TextElement.FontWeightProperty).Equals(FontWeights.Bold) m_bItalicIsChecked = textRange.GetPropertyValue(TextElement.FontStyleProperty).Equals(FontStyles.Italic) m_bUnderlineChecked = textRange.GetPropertyValue(Inline.TextDecorationsProperty).Equals(TextDecorations.Underline) End If m_bLeftIsChecked = textRange.GetPropertyValue(FlowDocument.TextAlignmentProperty).Equals(TextAlignment.Left) m_bCenterIsChecked = textRange.GetPropertyValue(FlowDocument.TextAlignmentProperty).Equals(TextAlignment.Center) m_bJustifyIsChecked = textRange.GetPropertyValue(FlowDocument.TextAlignmentProperty).Equals(TextAlignment.Justify) End Sub Public Sub InsertText(ByVal rtb As RichTextBox, ByVal content As String) If Not String.IsNullOrEmpty(content) Then rtb?.BeginChange() If Not String.IsNullOrEmpty(rtb.Selection.Text) Then rtb.Selection.Text = String.Empty End If Dim tp = rtb.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward) rtb.CaretPosition.InsertTextInRun(content) rtb.CaretPosition = tp rtb.EndChange() Keyboard.Focus(rtb) End If End Sub #End Region ' Methods #Region "COMMANDS" #Region "ConfermCmd" Public ReadOnly Property ConfermaCmd As ICommand Get If m_ConfermaCmd Is Nothing Then m_ConfermaCmd = New Command(AddressOf Conferma) End If Return m_ConfermaCmd End Get End Property Public Sub Conferma() ' Recupero il file script 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 EgtLuaExecFile(Map.refMainWindowVM.MainWindowM.sTempDir & "\" & m_sNameFile & ".lua") ' Imposto stato gestione mouse diretto della scena a nessuno Map.refSceneHostVM.MainScene.SetStatusNull() End Sub #End Region ' ConfermaCmd #Region "SaveAsCommand" Public ReadOnly Property SalvaCmd As ICommand Get If m_cmdSaveAs Is Nothing Then m_cmdSaveAs = New Command(AddressOf SaveAs) End If Return m_cmdSaveAs End Get End Property Public Sub SaveAs() SaveProject() End Sub #End Region ' SaveAsCommand #Region "AnnullaCmd" Public ReadOnly Property AnnullaCmd As ICommand Get If m_cmdAnnulla Is Nothing Then m_cmdAnnulla = New Command(AddressOf Annulla) End If Return m_cmdAnnulla End Get End Property Public Sub Annulla() Map.refScriptWindowV.Close() End Sub #End Region ' AnnullaCmd #Region "TextColorCmd" Public ReadOnly Property TextColorCmd As ICommand Get If m_cmdTextColor Is Nothing Then m_cmdTextColor = New Command(AddressOf TextColor) End If Return m_cmdTextColor End Get End Property Public Sub TextColor() ' Recupero colori custom Dim Col As New Color3d(10, 122, 150) Dim sCustomColors As String = "" GetMainPrivateProfileString(S_COLORS, K_CUSTOMCOLORS, "", sCustomColors) Dim CustomColors() As String = sCustomColors.Split(","c) Dim nCustomColors As New List(Of Integer) For Each Color In CustomColors Dim nColor As Integer If Integer.TryParse(Color, nColor) Then nCustomColors.Add(nColor) End If Next ' Creo dialogo colori Dim ColorDlg As New EgtColorPickerV(Application.Current.MainWindow, New EgtColorPickerVM()) With { .CustomColors = nCustomColors.ToArray(), .Color = Col.ToColor() } ' Visualizzo dialogo If ColorDlg.ShowDialog() <> Windows.Forms.DialogResult.OK Then Return 'Recupero colore scelto Dim selColor As New SolidColorBrush(System.Windows.Media.Color.FromArgb(CByte(ColorDlg.Color.A), CByte(ColorDlg.Color.R), CByte(ColorDlg.Color.G), CByte(ColorDlg.Color.B))) Dim textRange = New TextRange(Map.refScriptWindowV.sNameFile_RichTxBx.Selection.Start, Map.refScriptWindowV.sNameFile_RichTxBx.Selection.[End]) textRange.ApplyPropertyValue(TextElement.ForegroundProperty, selColor) End Sub #End Region ' TextColorCmd #End Region ' Commands End Class