Пример #1
0
 private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
 {
     //teachingTarget 변경점이 있는 경우, 저장할거냐고 물어봄.
     if (string.IsNullOrEmpty(JsonCharacter.GetInstance().GetOriginalJsonString()))
     {
         DialogResult dialogResult = MessageBox.Show("새로 저장하시겠습니까?", "Question", MessageBoxButtons.YesNoCancel);
         if (dialogResult == DialogResult.Cancel)
         {
             e.Cancel = true;
             return;
         }
         else if (dialogResult == DialogResult.Yes)
         {
             buttonSaveAs_Click(null, null);
         }
     }
     else if (JsonCharacter.GetInstance().GetOriginalJsonString() != JsonCharacter.GetInstance().ConvertJsonString())
     {
         DialogResult dialogResult = MessageBox.Show("변경사항을 저장하시겠습니까?", "Question", MessageBoxButtons.YesNoCancel);
         if (dialogResult == DialogResult.Cancel)
         {
             e.Cancel = true;
             return;
         }
         else if (dialogResult == DialogResult.Yes)
         {
             buttonSave_Click(null, null);
         }
     }
 }
Пример #2
0
 public Character(bool enable, Image image, string imageFileName, string name, string caption, JsonCharacter parentJsonCharacter)
 {
     this.enable              = enable;
     this.image               = image;
     this.imageFileName       = imageFileName;
     this.name                = name;
     this.caption             = caption;
     this.parentJsonCharacter = parentJsonCharacter;
 }
Пример #3
0
        public static JsonCharacter GetInstance()
        {
            if (instance == null)
            {
                instance = new JsonCharacter();
            }

            return(instance);
        }
Пример #4
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(jsonFilePath) == true)
            {
                buttonSaveAs_Click(null, null);
            }

            JsonCharacter.GetInstance().SaveJsonFile(KeywordPathManager.GetOriginalPath(jsonFilePath), true);
            AppConfiguration.SetAppConfig("Json_file_path", KeywordPathManager.GetKeywordPath(jsonFilePath));
        }
Пример #5
0
        public static bool LoadJsonFile(string filePath, out JsonCharacter jsonCharacter)
        {
            if (File.Exists(filePath) == false)
            {
                jsonCharacter = new JsonCharacter();
                return(false);
            }

            //파일을 읽어서 Json string 생성
            string jsonString = System.IO.File.ReadAllText(filePath);

            try
            {
                //Json object로 생성
                Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(jsonString);
                //Json object를 JsonCharacter 객체로 형변환 후 Instance로 설정
                jsonCharacter = jObject.ToObject(typeof(JsonCharacter)) as JsonCharacter;
            }
            catch (Exception exception)
            {
                throw exception;
            }

            jsonCharacter.rootPath = KeywordPathManager.GetOriginalPath(jsonCharacter.rootPath);

            for (int i = 0; i < jsonCharacter.characterList.Count(); i++)
            {
                //부모-자식관계 설정
                jsonCharacter.characterList[i].SetParentJsonCharacter(jsonCharacter);

                string imageFilePath = $@"{jsonCharacter.rootPath}\{jsonCharacter.characterList[i].imageFileName}";
                if (File.Exists(imageFilePath))
                {
                    //File lock이 되므로 코드 변경함.
                    //jsonCharacter.characterList[i].image = Image.FromFile(imageFilePath);
                    using (FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read))
                    {
                        Image loadImage = Image.FromStream(fileStream);
                        jsonCharacter.characterList[i].image = loadImage.Clone() as Image;
                        loadImage.Dispose();
                        fileStream.Close();
                    }
                }
                else
                {
                    jsonCharacter.characterList[i].image = Properties.Resources.DefaultImageBitmap.Clone() as Image;
                }
            }

            //창 닫기 전에 변경사항 확인용 원본 저장
            jsonCharacter.originalJsonString = jsonCharacter.ConvertJsonString();

            return(true);
        }
Пример #6
0
        private void numericUpDownRandomSelectCount_ValueChanged(object sender, EventArgs e)
        {
            //Linq 쿼리문으로 enable갯수 카운트
            int enableCount = (from character in JsonCharacter.GetInstance().characterList
                               where character.enable == true
                               select character).Count();

            if ((sender as NumericUpDown).Value > enableCount)
            {
                (sender as NumericUpDown).Value = enableCount;
            }
        }
Пример #7
0
        private void buttonSaveAs_Click(object sender, EventArgs e)
        {
            Directory.CreateDirectory(saveDirectoryPath);

            saveJsonFileDialog.InitialDirectory = saveDirectoryPath;
            saveJsonFileDialog.FileName         = string.Empty;
            if (DialogResult.OK != saveJsonFileDialog.ShowDialog())
            {
                return;
            }

            JsonCharacter.GetInstance().SaveJsonFile(saveJsonFileDialog.FileName);
            jsonFilePath = $@"{JsonCharacter.GetInstance().rootPath}\{Path.GetFileName(saveJsonFileDialog.FileName)}";
            AppConfiguration.SetAppConfig("Json_file_path", KeywordPathManager.GetKeywordPath($@"{JsonCharacter.GetInstance().rootPath}\{Path.GetFileName(saveJsonFileDialog.FileName)}"));
        }
Пример #8
0
        private void buttonAddCharacter_Click(object sender, EventArgs e)
        {
            JsonCharacter.GetInstance().characterList.Add(new Character(true,
                                                                        Properties.Resources.DefaultImageBitmap.Clone() as Image,
                                                                        "DefaultImageBitmap.bmp",   //gif를 저장하면, 추후 pictureBox에서 런타임 fileStream 예외 발생함. 그래서 bitmap을 사용
                                                                        string.Empty,
                                                                        string.Empty,
                                                                        JsonCharacter.GetInstance()));

            //데이터 리바인딩(데이터 갱신)
            dataGridViewMain.DataSource = new BindingList <Character>(JsonCharacter.GetInstance().characterList);
            //자동 스크롤
            dataGridViewMain.Rows[JsonCharacter.GetInstance().characterList.Count() - 1].Selected = true;
            dataGridViewMain.FirstDisplayedScrollingRowIndex = JsonCharacter.GetInstance().characterList.Count() - 1;
        }
Пример #9
0
        private void buttonEnableAll_Click(object sender, EventArgs e)
        {
            foreach (Character character in JsonCharacter.GetInstance().characterList)
            {
                character.enable = true;
            }

            //자동 스크롤을 위한 변수 저장
            int scrollIndex   = dataGridViewMain.FirstDisplayedScrollingRowIndex;
            int selectedIndex = dataGridViewMain.SelectedRows[0].Index;

            //데이터 리바인딩(데이터 갱신)
            dataGridViewMain.DataSource = new BindingList <Character>(JsonCharacter.GetInstance().characterList);
            //자동 스크롤
            dataGridViewMain.Rows[selectedIndex].Selected    = true;
            dataGridViewMain.FirstDisplayedScrollingRowIndex = scrollIndex;
        }
Пример #10
0
        private void Initialize()
        {
            dataGridViewMain.AutoGenerateColumns   = false;
            dataGridViewWinner.AutoGenerateColumns = false;
            pictureBoxSelectedImage.Image          = Properties.Resources.DefaultImage;

            string[] seedStringArray       = null;
            int      randomNumberSeedCount = 0;

            if (int.TryParse(AppConfiguration.GetAppConfig("Random_number_seed_count"), out randomNumberSeedCount))
            {
                seedStringArray = new string[randomNumberSeedCount];
                for (int i = 0; i < randomNumberSeedCount; i++)
                {
                    seedStringArray[i] = AppConfiguration.GetAppConfig($@"Seed_{(i+1).ToString("00")}");
                }
            }
            randomNumberGenerator = new RandomNumberGenerator(seedStringArray);

            saveDirectoryPath = AppConfiguration.GetAppConfig("Save_directory_path");
            saveDirectoryPath = KeywordPathManager.GetOriginalPath(saveDirectoryPath); //null, empty인 경우 바탕화면으로 설정됨.

            jsonFilePath = AppConfiguration.GetAppConfig("Json_file_path");
            if (string.IsNullOrEmpty(jsonFilePath) == false)
            {
                jsonFilePath = KeywordPathManager.GetOriginalPath(jsonFilePath);
                JsonCharacter jsonCharacter = new JsonCharacter();
                if (JsonCharacter.LoadJsonFile(jsonFilePath, out jsonCharacter) == false)
                {
                    MessageBox.Show($@"Json 파일 읽어오기 실패.{Environment.NewLine}{AppConfiguration.GetAppConfig("Json_file_path")}");
                    jsonFilePath = string.Empty;
                }
                else
                {
                    JsonCharacter.SetInstance(jsonCharacter);
                }
            }

            dataGridViewMain.DataSource = new BindingList <Character>(JsonCharacter.GetInstance().characterList);
        }
Пример #11
0
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewMain.SelectedRows.Count < 1)
            {
                return;
            }

            //자동 스크롤을 위한 변수 저장
            int scrollIndex   = dataGridViewMain.FirstDisplayedScrollingRowIndex;
            int selectedIndex = dataGridViewMain.SelectedRows[0].Index;

            //선택 데이터 삭제
            JsonCharacter.GetInstance().characterList.Remove(dataGridViewMain.SelectedRows[0].DataBoundItem as Character);
            //데이터 리바인딩(데이터 갱신)
            dataGridViewMain.DataSource = new BindingList <Character>(JsonCharacter.GetInstance().characterList);
            //자동 스크롤
            if (JsonCharacter.GetInstance().characterList.Count > 0)
            {
                dataGridViewMain.Rows[selectedIndex == 0 ? 0 : selectedIndex - 1].Selected = true;
                dataGridViewMain.FirstDisplayedScrollingRowIndex = scrollIndex;
            }
        }
Пример #12
0
        private void buttonRandomSelect_Click(object sender, EventArgs e)
        {
            List <Character> enableCharacterList = (from character in JsonCharacter.GetInstance().characterList
                                                    where character.enable == true
                                                    select character).ToList();

            if (numericUpDownRandomSelectCount.Value == enableCharacterList.Count)
            {
                //전부 당첨자
                foreach (Character character in enableCharacterList)
                {
                    character.SetWinning(true);
                }
            }
            else
            {
                foreach (Character character in enableCharacterList)
                {
                    character.SetWinning(false);
                }

                var winningCount = numericUpDownRandomSelectCount.Value;
                while (winningCount > 0)
                {
                    var randomNumber = randomNumberGenerator.Generate(0, enableCharacterList.Count - 1);
                    if (enableCharacterList[randomNumber].GetWinning() == false)
                    {
                        enableCharacterList[randomNumber].SetWinning(true);
                        winningCount--;
                    }
                }
            }

            List <Character> winnerCharacterList = (from character in enableCharacterList
                                                    where character.GetWinning() == true
                                                    select character).ToList();

            dataGridViewWinner.DataSource = new BindingList <Character>(winnerCharacterList);
        }
Пример #13
0
        private void buttonLoad_Click(object sender, EventArgs e)
        {
            openJsonFileDialog.InitialDirectory = saveDirectoryPath;
            openJsonFileDialog.FileName         = string.Empty;
            if (DialogResult.OK != openJsonFileDialog.ShowDialog())
            {
                return;
            }

            jsonFilePath = openJsonFileDialog.FileName;
            JsonCharacter jsonCharacter = new JsonCharacter();

            if (JsonCharacter.LoadJsonFile(jsonFilePath, out jsonCharacter) == false)
            {
                MessageBox.Show($@"Json 파일 오류. {AppConfiguration.GetAppConfig("Json_file_path")}");
            }
            else
            {
                JsonCharacter.SetInstance(jsonCharacter);
            }

            dataGridViewMain.DataSource = new BindingList <Character>(JsonCharacter.GetInstance().characterList);
            AppConfiguration.SetAppConfig("Json_file_path", KeywordPathManager.GetKeywordPath(jsonFilePath));
        }
Пример #14
0
 public static void SetInstance(JsonCharacter jsonCharacter)
 {
     instance?.Dispose();
     instance = jsonCharacter;
 }
Пример #15
0
 public void SetParentJsonCharacter(JsonCharacter parentJsonCharacter)
 {
     this.parentJsonCharacter = parentJsonCharacter;
 }
Пример #16
0
        private void dataGridViewMain_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((sender as DataGridView).SelectedRows.Count < 1)
            {
                return;
            }

            if ((sender as DataGridView).Columns[e.ColumnIndex].Name == ColumnEnable.Name)
            {
                //값 변경 후 EndEdit를 해줘야 클릭 이벤트마다 즉시 값을 적용함. (더블클릭 이벤트에도 넣어줘야 함.)
                ((sender as DataGridView).Rows[e.RowIndex].DataBoundItem as Character).enable = !((sender as DataGridView).Rows[e.RowIndex].DataBoundItem as Character).enable;

                if (((sender as DataGridView).Rows[e.RowIndex].DataBoundItem as Character).enable == false)
                {
                    //numericUpDownRandomSelectCount 최대값 확인
                    numericUpDownRandomSelectCount_ValueChanged(numericUpDownRandomSelectCount, null);
                }

                //자동 스크롤을 위한 변수 저장
                int scrollIndex   = (sender as DataGridView).FirstDisplayedScrollingRowIndex;
                int selectedIndex = (sender as DataGridView).SelectedRows[0].Index;
                //데이터 리바인딩(데이터 갱신)
                (sender as DataGridView).DataSource = new BindingList <Character>(JsonCharacter.GetInstance().characterList);
                //자동 스크롤
                (sender as DataGridView).Rows[selectedIndex].Selected    = true;
                (sender as DataGridView).FirstDisplayedScrollingRowIndex = scrollIndex;
            }
            else if ((sender as DataGridView).Columns[e.ColumnIndex].Name == ColumnImage.Name)
            {
                openImageFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                openImageFileDialog.FileName         = string.Empty;
                if (DialogResult.OK != openImageFileDialog.ShowDialog())
                {
                    return;
                }

                Character selectedCharacter = ((sender as DataGridView).Rows[e.RowIndex].DataBoundItem as Character);

                try
                {
                    //File lock이 되므로 코드 변경함.
                    //((sender as DataGridView).Rows[e.RowIndex].DataBoundItem as Character).image = Image.FromFile(openImageFileDialog.FileName);
                    using (FileStream fileStream = new FileStream(openImageFileDialog.FileName, FileMode.Open, FileAccess.Read))
                    {
                        selectedCharacter.image?.Dispose();
                        Image loadImage = Image.FromStream(fileStream);
                        selectedCharacter.image = loadImage?.Clone() as Image;
                        loadImage?.Dispose();
                        fileStream.Close();
                    }
                    selectedCharacter.imageFileName = openImageFileDialog.SafeFileName;
                    selectedCharacter.name          = Path.GetFileNameWithoutExtension(openImageFileDialog.FileName);
                }
                catch (Exception)
                {
                    MessageBox.Show("이미지 파일만 열 수 있습니다.");
                    return;
                }

                //자동 스크롤을 위한 변수 저장
                int scrollIndex   = (sender as DataGridView).FirstDisplayedScrollingRowIndex;
                int selectedIndex = (sender as DataGridView).SelectedRows[0].Index;
                //데이터 리바인딩(데이터 갱신)
                (sender as DataGridView).DataSource = new BindingList <Character>(JsonCharacter.GetInstance().characterList);
                //자동 스크롤
                (sender as DataGridView).Rows[selectedIndex].Selected    = true;
                (sender as DataGridView).FirstDisplayedScrollingRowIndex = scrollIndex;
            }
        }
Пример #17
0
 //Class constructor chaining
 public Character(Image image, string imageFileName, JsonCharacter parentJsonCharacter) : this(true, image, imageFileName, string.Empty, string.Empty, parentJsonCharacter)
 {
 }