Exemple #1
0
        /// <summary>
        /// Render NumberBox control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="r.MasterScale">Master Scale Factor</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderNumberBox(RenderInfo r, UICommand uiCmd)
        {
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_NumberBox));
            UIInfo_NumberBox info = uiCmd.Info as UIInfo_NumberBox;

            SpinnerControl spinner = new SpinnerControl()
            {
                Value                    = info.Value,
                FontSize                 = CalcFontPointScale(),
                Minimum                  = info.Min,
                Maximum                  = info.Max,
                DecimalPlaces            = 0,
                Change                   = info.Interval,
                VerticalContentAlignment = VerticalAlignment.Center,
            };

            spinner.ValueChanged += (object sender, RoutedPropertyChangedEventArgs <decimal> e) =>
            {
                info.Value = (int)e.NewValue;
                uiCmd.Update();
            };

            SetToolTip(spinner, info.ToolTip);
            DrawToCanvas(r, spinner, uiCmd.Rect);
        }
        public static List <LogInfo> Visible(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>(1);

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_Visible));
            CodeInfo_Visible info = cmd.Info as CodeInfo_Visible;

            string visibilityStr = StringEscaper.Preprocess(s, info.Visibility);
            bool   visibility    = false;

            if (visibilityStr.Equals("True", StringComparison.OrdinalIgnoreCase))
            {
                visibility = true;
            }
            else if (visibilityStr.Equals("False", StringComparison.OrdinalIgnoreCase) == false)
            {
                logs.Add(new LogInfo(LogState.Error, $"Invalid boolean value [{visibilityStr}]"));
                return(logs);
            }

            Plugin        p     = cmd.Addr.Plugin;
            PluginSection iface = p.GetInterface(out string ifaceSecName);

            if (iface == null)
            {
                logs.Add(new LogInfo(LogState.Error, $"Plugin [{cmd.Addr.Plugin.ShortPath}] does not have section [{ifaceSecName}]"));
                return(logs);
            }

            List <UICommand> uiCodes = iface.GetUICodes(true);
            UICommand        uiCmd   = uiCodes.Find(x => x.Key.Equals(info.InterfaceKey, StringComparison.OrdinalIgnoreCase));

            if (uiCmd == null)
            {
                logs.Add(new LogInfo(LogState.Error, $"Cannot find interface control [{info.InterfaceKey}] from section [{ifaceSecName}]"));
                return(logs);
            }

            if (uiCmd.Visibility != visibility)
            {
                uiCmd.Visibility = visibility;
                uiCmd.Update();

                // Re-render Plugin
                Application.Current.Dispatcher.Invoke(() =>
                {
                    MainWindow w = (Application.Current.MainWindow as MainWindow);
                    if (w.CurMainTree.Plugin == cmd.Addr.Plugin)
                    {
                        w.DrawPlugin(cmd.Addr.Plugin);
                    }
                });
            }

            logs.Add(new LogInfo(LogState.Success, $"Interface control [{info.InterfaceKey}]'s visibility set to [{visibility}]"));

            return(logs);
        }
Exemple #3
0
        /// <summary>
        /// Render RadioGroup control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderRadioButton(RenderInfo r, UICommand uiCmd, UICommand[] radioButtons)
        {
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_RadioButton));
            UIInfo_RadioButton info = uiCmd.Info as UIInfo_RadioButton;

            double fontSize = CalcFontPointScale();

            RadioButton radio = new RadioButton()
            {
                GroupName = r.Plugin.FullPath,
                Content   = uiCmd.Text,
                FontSize  = fontSize,
                IsChecked = info.Selected,
                VerticalContentAlignment = VerticalAlignment.Center,
            };

            if (info.SectionName != null)
            {
                radio.Click += (object sender, RoutedEventArgs e) =>
                {
                    if (r.Plugin.Sections.ContainsKey(info.SectionName)) // Only if section exists
                    {
                        SectionAddress addr = new SectionAddress(r.Plugin, r.Plugin.Sections[info.SectionName]);
                        UIRenderer.RunOneSection(addr, $"{r.Plugin.Title} - RadioButton [{uiCmd.Key}]", info.HideProgress);
                    }
                    else
                    {
                        Application.Current.Dispatcher.Invoke((Action)(() =>
                        {
                            MainWindow w = Application.Current.MainWindow as MainWindow;
                            w.Logger.System_Write(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exists"));
                        }));
                    }
                };
            }

            radio.Checked += (object sender, RoutedEventArgs e) =>
            {
                RadioButton btn = sender as RadioButton;
                info.Selected = true;

                // Uncheck the other RadioButtons
                List <UICommand> updateList = radioButtons.Where(x => !x.Key.Equals(uiCmd.Key, StringComparison.Ordinal)).ToList();
                foreach (UICommand uncheck in updateList)
                {
                    Debug.Assert(uncheck.Info.GetType() == typeof(UIInfo_RadioButton));
                    UIInfo_RadioButton unInfo = uncheck.Info as UIInfo_RadioButton;

                    unInfo.Selected = false;
                }

                updateList.Add(uiCmd);
                UICommand.Update(updateList);
            };

            SetToolTip(radio, info.ToolTip);
            DrawToCanvas(r, radio, uiCmd.Rect);
        }
Exemple #4
0
        /// <summary>
        /// Render CheckBox control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderCheckBox(RenderInfo r, UICommand uiCmd)
        {
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_CheckBox));
            UIInfo_CheckBox info = uiCmd.Info as UIInfo_CheckBox;

            CheckBox checkBox = new CheckBox()
            {
                Content   = uiCmd.Text,
                IsChecked = info.Value,
                FontSize  = CalcFontPointScale(),
                VerticalContentAlignment = VerticalAlignment.Center,
            };

            if (info.SectionName != null)
            {
                checkBox.Click += (object sender, RoutedEventArgs e) =>
                {
                    if (r.Plugin.Sections.ContainsKey(info.SectionName)) // Only if section exists
                    {
                        SectionAddress addr = new SectionAddress(r.Plugin, r.Plugin.Sections[info.SectionName]);
                        UIRenderer.RunOneSection(addr, $"{r.Plugin.Title} - CheckBox [{uiCmd.Key}]", info.HideProgress);
                    }
                    else
                    {
                        r.Logger.System_Write(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exists"));
                    }
                };
            }

            checkBox.Checked += (object sender, RoutedEventArgs e) =>
            {
                CheckBox box = sender as CheckBox;
                info.Value = true;
                uiCmd.Update();
            };
            checkBox.Unchecked += (object sender, RoutedEventArgs e) =>
            {
                CheckBox box = sender as CheckBox;
                info.Value = false;
                uiCmd.Update();
            };

            SetToolTip(checkBox, info.ToolTip);
            DrawToCanvas(r, checkBox, uiCmd.Rect);
        }
Exemple #5
0
        /// <summary>
        /// Render ComboBox control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderComboBox(RenderInfo r, UICommand uiCmd)
        {
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_ComboBox));
            UIInfo_ComboBox info = uiCmd.Info as UIInfo_ComboBox;

            ComboBox comboBox = new ComboBox()
            {
                FontSize                 = CalcFontPointScale(),
                ItemsSource              = info.Items,
                SelectedIndex            = info.Index,
                VerticalContentAlignment = VerticalAlignment.Center,
            };

            comboBox.LostFocus += (object sender, RoutedEventArgs e) =>
            {
                ComboBox box = sender as ComboBox;
                if (info.Index != box.SelectedIndex)
                {
                    info.Index = box.SelectedIndex;
                    uiCmd.Text = info.Items[box.SelectedIndex];
                    uiCmd.Update();
                }
            };

            if (info.SectionName != null)
            {
                comboBox.SelectionChanged += (object sender, SelectionChangedEventArgs e) =>
                {
                    if (r.Plugin.Sections.ContainsKey(info.SectionName)) // Only if section exists
                    {
                        SectionAddress addr = new SectionAddress(r.Plugin, r.Plugin.Sections[info.SectionName]);
                        UIRenderer.RunOneSection(addr, $"{r.Plugin.Title} - CheckBox [{uiCmd.Key}]", info.HideProgress);
                    }
                    else
                    {
                        r.Logger.System_Write(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exists"));
                    }
                };
            }

            SetToolTip(comboBox, info.ToolTip);
            DrawToCanvas(r, comboBox, uiCmd.Rect);
        }
Exemple #6
0
        /// <summary>
        /// Render TextBox control.
        /// Return true if failed.
        /// </summary>
        /// <param name="canvas">Parent canvas</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderTextBox(RenderInfo r, UICommand uiCmd)
        {
            // WB082 textbox control's y coord is of textbox's, not textlabel's.
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_TextBox));
            UIInfo_TextBox info = uiCmd.Info as UIInfo_TextBox;

            TextBox box = new TextBox()
            {
                Text     = info.Value,
                FontSize = CalcFontPointScale(),
                VerticalContentAlignment = VerticalAlignment.Center,
            };

            box.LostFocus += (object sender, RoutedEventArgs e) =>
            {
                TextBox tBox = sender as TextBox;
                info.Value = tBox.Text;
                uiCmd.Update();
            };
            SetToolTip(box, info.ToolTip);
            DrawToCanvas(r, box, uiCmd.Rect);

            if (uiCmd.Text.Equals(string.Empty, StringComparison.Ordinal) == false)
            {
                TextBlock block = new TextBlock()
                {
                    Text = uiCmd.Text,
                    LineStackingStrategy = LineStackingStrategy.BlockLineHeight,
                    LineHeight           = CalcFontPointScale(),
                    FontSize             = CalcFontPointScale(),
                };
                SetToolTip(block, info.ToolTip);
                double margin    = PointToDeviceIndependentPixel * DefaultFontPoint * 1.2;
                Rect   blockRect = new Rect(uiCmd.Rect.Left, uiCmd.Rect.Top - margin, uiCmd.Rect.Width, uiCmd.Rect.Height);
                DrawToCanvas(r, block, blockRect);
            }
        }
Exemple #7
0
        public static List <LogInfo> Set(EngineState s, CodeCommand cmd)
        {
            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_Set));
            CodeInfo_Set info = cmd.Info as CodeInfo_Set;

            Variables.VarKeyType varType = Variables.DetermineType(info.VarKey);
            if (varType == Variables.VarKeyType.None)
            {
                // Check Macro
                if (Regex.Match(info.VarKey, Macro.MacroNameRegex, RegexOptions.Compiled).Success) // Macro Name Validation
                {
                    string macroCommand = StringEscaper.Preprocess(s, info.VarValue);

                    if (macroCommand.Equals("NIL", StringComparison.OrdinalIgnoreCase))
                    {
                        macroCommand = null;
                    }

                    LogInfo log = s.Macro.SetMacro(info.VarKey, macroCommand, cmd.Addr, info.Permanent);
                    return(new List <LogInfo>(1)
                    {
                        log
                    });
                }
            }

            // [WB082 Behavior]
            // If PERMANENT was used but the key exists in interface command, the value will not be written to script.project but in interface.
            // Need to investigate where the logs are saved in this case.
            switch (info.Permanent)
            {
            case true:
            {         // Check if interface contains VarKey
                List <LogInfo> logs = new List <LogInfo>();

                if (Variables.DetermineType(info.VarKey) != Variables.VarKeyType.Variable)
                {
                    goto case false;
                }

                string varKey     = Variables.TrimPercentMark(info.VarKey);
                string finalValue = StringEscaper.Preprocess(s, info.VarValue);

                #region Set UI
                Plugin        p     = cmd.Addr.Plugin;
                PluginSection iface = p.GetInterface(out string sectionName);
                if (iface == null)
                {
                    goto case false;
                }

                List <UICommand> uiCmds = iface.GetUICodes(true);
                UICommand        uiCmd  = uiCmds.Find(x => x.Key.Equals(varKey));
                if (uiCmd == null)
                {
                    goto case false;
                }

                bool match = false;
                switch (uiCmd.Type)
                {
                case UIType.TextBox:
                {
                    Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_TextBox));
                    UIInfo_TextBox uiInfo = uiCmd.Info as UIInfo_TextBox;

                    uiInfo.Value = finalValue;

                    logs.Add(new LogInfo(LogState.Success, $"Interface [{varKey}] set to [{finalValue}]"));
                    match = true;
                }
                break;

                case UIType.NumberBox:
                {
                    Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_NumberBox));
                    UIInfo_NumberBox uiInfo = uiCmd.Info as UIInfo_NumberBox;

                    // WB082 just write string value in case of error, but PEBakery will throw warning
                    if (!NumberHelper.ParseInt32(finalValue, out int intVal))
                    {
                        logs.Add(new LogInfo(LogState.Warning, $"[{finalValue}] is not valid integer"));
                        return(logs);
                    }

                    uiInfo.Value = intVal;

                    logs.Add(new LogInfo(LogState.Success, $"Interface [{varKey}] set to [{finalValue}]"));
                    match = true;
                }
                break;

                case UIType.CheckBox:
                {
                    Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_CheckBox));
                    UIInfo_CheckBox uiInfo = uiCmd.Info as UIInfo_CheckBox;

                    if (finalValue.Equals("True", StringComparison.OrdinalIgnoreCase))
                    {
                        uiInfo.Value = true;

                        logs.Add(new LogInfo(LogState.Success, $"Interface [{varKey}] set to [True]"));
                        match = true;
                    }
                    else if (finalValue.Equals("False", StringComparison.OrdinalIgnoreCase))
                    {
                        uiInfo.Value = false;

                        logs.Add(new LogInfo(LogState.Success, $"Interface [{varKey}] set to [False]"));
                        match = true;
                    }
                    else
                    {                 // WB082 just write string value in case of error, but PEBakery will throw warning
                        logs.Add(new LogInfo(LogState.Warning, $"[{finalValue}] is not valid boolean"));
                        return(logs);
                    }
                }
                break;

                case UIType.ComboBox:
                {
                    Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_ComboBox));
                    UIInfo_ComboBox uiInfo = uiCmd.Info as UIInfo_ComboBox;

                    int idx = uiInfo.Items.FindIndex(x => x.Equals(finalValue, StringComparison.OrdinalIgnoreCase));
                    if (idx == -1)
                    {                 // Invalid Index
                        logs.Add(new LogInfo(LogState.Warning, $"[{finalValue}] not found in item list"));
                        return(logs);
                    }

                    uiInfo.Index = idx;
                    uiCmd.Text   = uiInfo.Items[idx];

                    logs.Add(new LogInfo(LogState.Success, $"Interface [{varKey}] set to [{uiCmd.Text}]"));
                    match = true;
                }
                break;

                case UIType.RadioButton:
                {
                    Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_RadioButton));
                    UIInfo_RadioButton uiInfo = uiCmd.Info as UIInfo_RadioButton;

                    if (finalValue.Equals("True", StringComparison.OrdinalIgnoreCase))
                    {
                        uiInfo.Selected = true;

                        logs.Add(new LogInfo(LogState.Success, $"Interface [{varKey}] set to true]"));
                        match = true;
                    }
                    else if (finalValue.Equals("False", StringComparison.OrdinalIgnoreCase))
                    {
                        uiInfo.Selected = false;

                        logs.Add(new LogInfo(LogState.Success, $"Interface [{varKey}] set to [False]"));
                        match = true;
                    }
                    else
                    {                 // WB082 just write string value, but PEBakery will ignore and throw and warning
                        logs.Add(new LogInfo(LogState.Warning, $"[{finalValue}] is not valid boolean"));
                        return(logs);
                    }
                }
                break;

                case UIType.FileBox:
                {
                    Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_FileBox));
                    UIInfo_FileBox uiInfo = uiCmd.Info as UIInfo_FileBox;

                    uiCmd.Text = finalValue;

                    logs.Add(new LogInfo(LogState.Success, $"Interface [{varKey}] set to [{finalValue}]"));
                    match = true;
                }
                break;

                case UIType.RadioGroup:
                {
                    Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_RadioGroup));
                    UIInfo_RadioGroup uiInfo = uiCmd.Info as UIInfo_RadioGroup;

                    int idx = uiInfo.Items.FindIndex(x => x.Equals(finalValue, StringComparison.OrdinalIgnoreCase));
                    if (idx == -1)
                    {                 // Invalid Index
                        logs.Add(new LogInfo(LogState.Warning, $"[{finalValue}] not found in item list"));
                        return(logs);
                    }

                    uiInfo.Selected = idx;

                    logs.Add(new LogInfo(LogState.Success, $"Interface [{varKey}] set to [{finalValue}]"));
                    match = true;
                }
                break;
                }

                if (match)
                {
                    uiCmd.Update();

                    logs.AddRange(Variables.SetVariable(s, info.VarKey, info.VarValue, false, false));
                    return(logs);
                }
                else
                {
                    goto case false;
                }
                #endregion
            }

            case false:
            default:
                return(Variables.SetVariable(s, info.VarKey, info.VarValue, info.Global, info.Permanent));
            }
        }
        public static List <LogInfo> VisibleOp(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>(8);

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_VisibleOp));
            CodeInfo_VisibleOp infoOp = cmd.Info as CodeInfo_VisibleOp;

            Plugin        p     = cmd.Addr.Plugin;
            PluginSection iface = p.GetInterface(out string ifaceSecName);

            if (iface == null)
            {
                logs.Add(new LogInfo(LogState.Error, $"Plugin [{cmd.Addr.Plugin.ShortPath}] does not have section [{ifaceSecName}]"));
                return(logs);
            }

            List <UICommand> uiCodes = iface.GetUICodes(true);

            List <Tuple <string, bool> > prepArgs = new List <Tuple <string, bool> >();

            foreach (CodeInfo_Visible info in infoOp.InfoList)
            {
                string visibilityStr = StringEscaper.Preprocess(s, info.Visibility);
                bool   visibility    = false;
                if (visibilityStr.Equals("True", StringComparison.OrdinalIgnoreCase))
                {
                    visibility = true;
                }
                else if (visibilityStr.Equals("False", StringComparison.OrdinalIgnoreCase) == false)
                {
                    throw new ExecuteException($"Invalid boolean value [{visibilityStr}]");
                }

                prepArgs.Add(new Tuple <string, bool>(info.InterfaceKey, visibility));
            }

            List <UICommand> uiCmdList = new List <UICommand>();

            foreach (Tuple <string, bool> args in prepArgs)
            {
                UICommand uiCmd = uiCodes.Find(x => x.Key.Equals(args.Item1, StringComparison.OrdinalIgnoreCase));
                if (uiCmd == null)
                {
                    logs.Add(new LogInfo(LogState.Error, $"Cannot find interface control [{args.Item1}] from section [{ifaceSecName}]"));
                    continue;
                }

                uiCmd.Visibility = args.Item2;
                uiCmdList.Add(uiCmd);
            }

            UICommand.Update(uiCmdList);

            foreach (Tuple <string, bool> args in prepArgs)
            {
                logs.Add(new LogInfo(LogState.Success, $"Interface control [{args.Item1}]'s visibility set to [{args.Item2}]"));
            }

            // Re-render Plugin
            Application.Current.Dispatcher.Invoke(() =>
            {
                MainWindow w = (Application.Current.MainWindow as MainWindow);
                if (w.CurMainTree.Plugin == cmd.Addr.Plugin)
                {
                    w.DrawPlugin(cmd.Addr.Plugin);
                }
            });

            return(logs);
        }
        public static List <LogInfo> WriteInterface(EngineState s, CodeCommand cmd)
        { // WriteInterface,<Element>,<PluginFile>,<Section>,<Key>,<Value>
            List <LogInfo> logs = new List <LogInfo>(2);

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_WriteInterface));
            CodeInfo_WriteInterface info = cmd.Info as CodeInfo_WriteInterface;

            string pluginFile = StringEscaper.Preprocess(s, info.PluginFile);
            string section    = StringEscaper.Preprocess(s, info.Section);
            string key        = StringEscaper.Preprocess(s, info.Key);
            string finalValue = StringEscaper.Preprocess(s, info.Value);

            Plugin p = Engine.GetPluginInstance(s, cmd, s.CurrentPlugin.FullPath, pluginFile, out bool inCurrentPlugin);

            if (!p.Sections.ContainsKey(section))
            {
                logs.Add(new LogInfo(LogState.Error, $"Plugin [{pluginFile}] does not have section [{section}]"));
                return(logs);
            }

            PluginSection    iface  = p.Sections[section];
            List <UICommand> uiCmds = iface.GetUICodes(true);
            UICommand        uiCmd  = uiCmds.Find(x => x.Key.Equals(key, StringComparison.OrdinalIgnoreCase));

            if (uiCmd == null)
            {
                logs.Add(new LogInfo(LogState.Error, $"Interface [{key}] does not exist"));
                return(logs);
            }

            switch (info.Element)
            {
            case InterfaceElement.Text:
                uiCmd.Text = finalValue;
                break;

            case InterfaceElement.Visible:
            {
                bool visibility = false;
                if (finalValue.Equals("True", StringComparison.OrdinalIgnoreCase))
                {
                    visibility = true;
                }
                else if (!finalValue.Equals("False", StringComparison.OrdinalIgnoreCase))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{finalValue}] is not a valid boolean value"));
                    return(logs);
                }

                uiCmd.Visibility = visibility;
            }
            break;

            case InterfaceElement.PosX:
            {
                if (!NumberHelper.ParseInt32(finalValue, out int x))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{finalValue}] is not a valid integer"));
                    return(logs);
                }

                uiCmd.Rect.X = x;
            }
            break;

            case InterfaceElement.PosY:
            {
                if (!NumberHelper.ParseInt32(finalValue, out int y))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{finalValue}] is not a valid integer"));
                    return(logs);
                }

                uiCmd.Rect.Y = y;
            }
            break;

            case InterfaceElement.Width:
            {
                if (!NumberHelper.ParseInt32(finalValue, out int width))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{finalValue}] is not a valid integer"));
                    return(logs);
                }

                uiCmd.Rect.Width = width;
            }
            break;

            case InterfaceElement.Height:
            {
                if (!NumberHelper.ParseInt32(finalValue, out int height))
                {
                    logs.Add(new LogInfo(LogState.Error, $"[{finalValue}] is not a valid integer"));
                    return(logs);
                }

                uiCmd.Rect.Height = height;
            }
            break;

            case InterfaceElement.Value:
            {
                bool success = uiCmd.SetValue(finalValue, false, out List <LogInfo> varLogs);
                logs.AddRange(varLogs);

                if (success == false && varLogs.Count == 0)
                {
                    logs.Add(new LogInfo(LogState.Error, $"Wring [Value] to [{uiCmd.Type}] is not supported"));
                    return(logs);
                }
            }
            break;

            default:
                throw new InternalException($"Internal Logic Error at WriteInterface");
            }

            // Update uiCmd into file
            uiCmd.Update();

            // Rerender Plugin
            Application.Current?.Dispatcher.Invoke(() =>
            { // Application.Current is null in unit test
                MainWindow w = (Application.Current.MainWindow as MainWindow);
                if (w.CurMainTree.Plugin == cmd.Addr.Plugin)
                {
                    w.DrawPlugin(cmd.Addr.Plugin);
                }
            });

            return(logs);
        }
Exemple #10
0
        /// <summary>
        /// Render RadioGroup control.
        /// Return true if failed.
        /// </summary>
        /// <param name="r.Canvas">Parent r.Canvas</param>
        /// <param name="uiCmd">UICommand</param>
        /// <returns>Success = false, Failure = true</returns>
        public static void RenderRadioGroup(RenderInfo r, UICommand uiCmd)
        {
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_RadioGroup));
            UIInfo_RadioGroup info = uiCmd.Info as UIInfo_RadioGroup;

            double fontSize = CalcFontPointScale();

            GroupBox box = new GroupBox()
            {
                Header      = uiCmd.Text,
                FontSize    = fontSize,
                BorderBrush = Brushes.LightGray,
            };

            SetToolTip(box, info.ToolTip);

            Grid grid = new Grid();

            box.Content = grid;

            for (int i = 0; i < info.Items.Count; i++)
            {
                RadioButton radio = new RadioButton()
                {
                    GroupName = r.Plugin.FullPath + uiCmd.Key,
                    Content   = info.Items[i],
                    Tag       = i,
                    FontSize  = fontSize,
                    VerticalContentAlignment = VerticalAlignment.Center,
                };

                radio.IsChecked = (i == info.Selected);
                radio.Checked  += (object sender, RoutedEventArgs e) =>
                {
                    RadioButton btn = sender as RadioButton;
                    info.Selected = (int)btn.Tag;
                    uiCmd.Update();
                };

                if (info.SectionName != null)
                {
                    radio.Click += (object sender, RoutedEventArgs e) =>
                    {
                        if (r.Plugin.Sections.ContainsKey(info.SectionName)) // Only if section exists
                        {
                            SectionAddress addr = new SectionAddress(r.Plugin, r.Plugin.Sections[info.SectionName]);
                            UIRenderer.RunOneSection(addr, $"{r.Plugin.Title} - RadioGroup [{uiCmd.Key}]", info.HideProgress);
                        }
                        else
                        {
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                MainWindow w = Application.Current.MainWindow as MainWindow;
                                w.Logger.System_Write(new LogInfo(LogState.Error, $"Section [{info.SectionName}] does not exists"));
                            });
                        }
                    };
                }

                SetToolTip(radio, info.ToolTip);

                Grid.SetRow(radio, i);
                grid.RowDefinitions.Add(new RowDefinition());
                grid.Children.Add(radio);
            }

            Rect rect = new Rect(uiCmd.Rect.Left, uiCmd.Rect.Top, uiCmd.Rect.Width, uiCmd.Rect.Height);

            DrawToCanvas(r, box, rect);
        }
Exemple #11
0
        /// <summary>
        /// Render FileBox control.
        /// Return true if failed.
        /// </summary>
        /// <param name="canvas">Parent canvas</param>
        /// <param name="uiCmd">UICommand</param>
        public static void RenderFileBox(RenderInfo r, UICommand uiCmd, Variables variables)
        {
            // It took time to find WB082 textbox control's y coord is of textbox's, not textlabel's.
            Debug.Assert(uiCmd.Info.GetType() == typeof(UIInfo_FileBox));
            UIInfo_FileBox info = uiCmd.Info as UIInfo_FileBox;

            TextBox box = new TextBox()
            {
                Text     = uiCmd.Text,
                FontSize = CalcFontPointScale(),
                VerticalContentAlignment = VerticalAlignment.Center,
            };

            box.TextChanged += (object sender, TextChangedEventArgs e) =>
            {
                TextBox tBox = sender as TextBox;
                uiCmd.Text = tBox.Text;
                uiCmd.Update();
            };
            SetToolTip(box, info.ToolTip);

            Button button = new Button()
            {
                FontSize = CalcFontPointScale(),
                Content  = ImageHelper.GetMaterialIcon(PackIconMaterialKind.FolderUpload, 0),
            };

            SetToolTip(button, info.ToolTip);

            button.Click += (object sender, RoutedEventArgs e) =>
            {
                Button bt = sender as Button;

                if (info.IsFile)
                { // File
                    string currentPath = StringEscaper.Preprocess(variables, uiCmd.Text);
                    if (File.Exists(currentPath))
                    {
                        currentPath = Path.GetDirectoryName(currentPath);
                    }
                    else
                    {
                        currentPath = string.Empty;
                    }

                    Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog()
                    {
                        Filter           = "All Files|*.*",
                        InitialDirectory = currentPath,
                    };
                    if (dialog.ShowDialog() == true)
                    {
                        box.Text = dialog.FileName;
                    }
                }
                else
                { // Directory
                    string currentPath = StringEscaper.Preprocess(variables, uiCmd.Text);
                    if (Directory.Exists(currentPath) == false)
                    {
                        currentPath = string.Empty;
                    }

                    System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog()
                    {
                        ShowNewFolderButton = true,
                        SelectedPath        = currentPath,
                    };
                    System.Windows.Forms.DialogResult result = dialog.ShowDialog();
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        box.Text = dialog.SelectedPath;
                        if (Directory.Exists(dialog.SelectedPath) && !dialog.SelectedPath.EndsWith("\\", StringComparison.Ordinal))
                        {
                            box.Text += "\\";
                        }
                    }
                }
            };

            double margin  = 5;
            Rect   boxRect = new Rect(uiCmd.Rect.Left, uiCmd.Rect.Top, uiCmd.Rect.Width - (uiCmd.Rect.Height + margin), uiCmd.Rect.Height);
            Rect   btnRect = new Rect(boxRect.Right + margin, uiCmd.Rect.Top, uiCmd.Rect.Height, uiCmd.Rect.Height);

            DrawToCanvas(r, box, boxRect);
            DrawToCanvas(r, button, btnRect);
        }
Exemple #12
0
        public static List <LogInfo> Set(EngineState s, CodeCommand cmd)
        {
            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_Set));
            CodeInfo_Set info = cmd.Info as CodeInfo_Set;

            Variables.VarKeyType varType = Variables.DetermineType(info.VarKey);
            if (varType == Variables.VarKeyType.None)
            {
                // Check Macro
                if (Regex.Match(info.VarKey, Macro.MacroNameRegex, RegexOptions.Compiled).Success) // Macro Name Validation
                {
                    string macroCommand = StringEscaper.Preprocess(s, info.VarValue);

                    if (macroCommand.Equals("NIL", StringComparison.OrdinalIgnoreCase))
                    {
                        macroCommand = null;
                    }

                    LogInfo log = s.Macro.SetMacro(info.VarKey, macroCommand, cmd.Addr, info.Permanent, false);
                    return(new List <LogInfo>(1)
                    {
                        log
                    });
                }
            }

            // [WB082 Behavior]
            // If PERMANENT was used but the key exists in interface command, the value will not be written to script.project but in interface.
            // Need to investigate where the logs are saved in this case.
            switch (info.Permanent)
            {
            case true:
            {         // Check if interface contains VarKey
                List <LogInfo> logs = new List <LogInfo>();

                if (Variables.DetermineType(info.VarKey) != Variables.VarKeyType.Variable)
                {
                    goto case false;
                }

                string varKey     = Variables.TrimPercentMark(info.VarKey);
                string finalValue = StringEscaper.Preprocess(s, info.VarValue);

                #region Set UI
                Plugin        p     = cmd.Addr.Plugin;
                PluginSection iface = p.GetInterface(out string sectionName);
                if (iface == null)
                {
                    goto case false;
                }

                List <UICommand> uiCmds = iface.GetUICodes(true);
                UICommand        uiCmd  = uiCmds.Find(x => x.Key.Equals(varKey, StringComparison.OrdinalIgnoreCase));
                if (uiCmd == null)
                {
                    goto case false;
                }

                bool match = uiCmd.SetValue(finalValue, false, out List <LogInfo> varLogs);
                logs.AddRange(varLogs);

                if (match)
                {
                    uiCmd.Update();

                    logs.AddRange(Variables.SetVariable(s, info.VarKey, info.VarValue, false, false));
                    return(logs);
                }
                else
                {
                    goto case false;
                }
                #endregion
            }

            case false:
            default:
                return(Variables.SetVariable(s, info.VarKey, info.VarValue, info.Global, info.Permanent));
            }
        }