// TODO: Check if button already exists.
 // add a button to the config. the button must exist.
 public MJButtonTranslation addButton(MJButtonTranslation bt)
 {
     this.buttons.Add(bt);
     return(bt);
 }
Esempio n. 2
0
        // 0.6.9: create a button from the given values and return it.
        // was previously in the add-button function
        private MJButtonTranslation createButtonFromValues()
        {
            // empty button.
            MJButtonTranslation nobtn = new MJButtonTranslation(EMJBUTTON.None, EMJFUNCTION.SHOW_MENU);

            // Get the button.
            EMJBUTTON button = (EMJBUTTON)combo_Button.SelectedItem;
            // Get the action.
            EMJFUNCTION selaction = (EMJFUNCTION)combo_Action.SelectedItem;
            // keystroke, hitdelay and FN index.
            string mykeys   = txt_keystroke.Text;
            byte   fnidx    = 0;
            int    hitdelay = 0;

            // get the new hit delay.
            if (chk_repeat.Checked == true)
            {
                hitdelay = Int32.Parse(txt_repeattime.Text);
            }

            // get fn index.
            if (chk_FN.Checked == true)
            {
                fnidx = 1;
            }

            // get the desired button.
            // check if the button works.
            if (selaction == EMJFUNCTION.KEYBOARD_COMBINATION)
            {
                // it's a keyboard combination, it needs some keys.
                if (mykeys == "")
                {
                    MessageBox.Show("You need to set a key combination.", "Cannot create button:");
                    return(nobtn);
                }

                // check if the keyboard combination works.
                try
                {
                    // create the button for testing.
                    MJButtonTranslation testbtn = new MJButtonTranslation(button, EMJFUNCTION.KEYBOARD_COMBINATION, fnidx, mykeys);
                    // select the test box so nothing bad can happen.
                    txt_Test.Focus();
                    testbtn.hitKey();
                    btn_AddNew.Focus();
                }
                catch
                {
                    MessageBox.Show("The key combination is invalid! (break)", "Cannot create button.");
                    return(nobtn);
                }
            }

            // create the button
            MJButtonTranslation btn = new MJButtonTranslation(button, selaction, fnidx, mykeys);

            btn.hitDelay = (uint)hitdelay;

            // "external" settings.
            // set functions and stuff.
            switch (selaction)
            {
            case EMJFUNCTION.FN_MODIFICATOR:
            case EMJFUNCTION.SHOW_MENU:
            case EMJFUNCTION.SLOWER_MOVEMENT:
            case EMJFUNCTION.FASTER_MOVEMENT:
                Program.Input.Config.assignExternalFunction(btn);
                break;

            default:
                break;
            }

            // return the new button.
            return(btn);
        }
        // load a configuration.
        public bool LoadFrom(string filename)
        {
            try
            {
                Log.Line("Opening " + filename + " for loading the configuration..");
                FileStream   fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                StreamReader br = new StreamReader(fs);

                // read determinator and version
                string t  = br.ReadLine();
                byte   cd = byte.Parse(t);
                if (cd == configFileDeterminator)
                {
                    // determinator is ok, now read version.
                    t  = br.ReadLine();
                    cd = byte.Parse(t);
                    if (cd == configFileVersion)
                    {
                        // OK File and File Version are ok.
                        // clear the buttons..
                        this.clearButtons();

                        // load count of buttons.
                        t = br.ReadLine();
                        int count = int.Parse(t);
                        // .. and load the new ones.
                        Log.Line("Loading " + count + " buttons..");
                        for (int c = 0; c < count; c++)
                        {
                            // create a button and load it.
                            MJButtonTranslation b = new MJButtonTranslation(br);
                            assignExternalFunction(b);  // maybe assign an external function.
                            this.addButton(b);
                        }

                        // set the new config name.
                        this.configName = Path.GetFileName(filename);
                        Properties.Settings.Default.StartupConfig = filename;
                        Properties.Settings.Default.Save();

                        Log.Line("Loading success.");
                    }
                    else
                    {
                        Log.Line("Wrong file version: " + (int)cd + " [actual: " + (int)configFileVersion + "]");
                        MessageBox.Show("Wrong file version, sorry.", "I/O Error");
                    }
                }
                else
                {
                    Log.Line("Unknown filetype.");
                    MessageBox.Show("Invalid file.", "I/O Error");
                }

                Log.Line("DONE");
                br.Close();
                return(true);
            }
            catch
            {
                Log.Line("ERROR: Could not open file " + filename + "! Is it read protected?");
                MessageBox.Show("Could not open " + filename + ". Loading defaults.", "I/O Error");
                this.loadHardcodedDefaultConfig();
                return(false);
            }
        }