VioletaBabel

30. 연출용 툴 본문

BCA/3. C# WPF
30. 연출용 툴
Beabletoet 2018. 4. 27. 16:04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--MainWindow.xaml-->
    <Window x:Class="GameTextBoxTool.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GameTextBoxTool"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox x:Name="Script" HorizontalAlignment="Left" Height="358" Margin="10,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="772" AcceptsReturn="True" VerticalScrollBarVisibility="Visible"/>
        <Button x:Name="LoadButton" Content="불러오기" HorizontalAlignment="Left" Margin="621,373,0,0" VerticalAlignment="Top" Width="75" Height="36" Click="LoadButton_Click"/>
        <Button x:Name="SaveButton" Content="저장" HorizontalAlignment="Left" Margin="707,373,0,0" VerticalAlignment="Top" Width="75" Height="36" Click="SaveButton_Click"/>
 
    </Grid>
</Window>
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//MainWindow.xaml.cs
using Microsoft.Win32;
//using System; // 추가로 Json에 저장되는 공백 삭제 코드 이용을 위한 것.
using System.IO;
using System.Net.Json;
using System.Windows;
 
namespace GameTextBoxTool
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            JsonObjectCollection root = new JsonObjectCollection();
            JsonArrayCollection arr = new JsonArrayCollection("Commands");
            int count = Script.LineCount;
            for (int i = 0; i < count; ++i)
            {
                string text = Script.GetLineText(i);
                //text = text.TrimEnd(Environment.NewLine.ToCharArray()); // 추가로 Json에 저장되는 공백 삭제 코드. 밑에 else if(조건)으로 대체함.
                //text = text.TrimEnd('\r','\n',' '); // 이것두.
                string[] commands = text.Split(null);
                JsonObjectCollection com = new JsonObjectCollection();
                JsonArrayCollection commList = new JsonArrayCollection("List");
                int c = 0;
                foreach (string t in commands)
                {//MessageBox.Show(t);
                    if (c.Equals(0))
                    {
                        com.Add(new JsonStringValue("Name", t));
                        ++c;
                    }
                    else if (!t.Equals(""))
                    {
                        JsonObjectCollection data = new JsonObjectCollection();
                        data.Add(new JsonStringValue(c++.ToString(),t));
                        commList.Add(data);
                    }
                }
                com.Add(commList);
                arr.Add(com);
            }
            root.Add(arr);
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Json File(.json) | *.json";
            if (dlg.ShowDialog().Equals(true))
            {
                string fName = dlg.FileName;
                string outs = root.ToString();
                File.WriteAllText(fName, outs);
            }
        }
 
        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Json File(.json) | *.json";
            if(dlg.ShowDialog().Equals(true))
            {
                string jsonText = File.ReadAllText(dlg.FileName);
                JsonTextParser textParser = new JsonTextParser();
                JsonObjectCollection root = textParser.Parse(jsonText) as JsonObjectCollection;
                JsonArrayCollection rootArray = root["Commands"as JsonArrayCollection;
                Script.Text = "";
                string texts = "";
                foreach(JsonObjectCollection com in rootArray)
                {
                    JsonStringValue Name = com["Name"as JsonStringValue;
                    JsonArrayCollection list = com["List"as JsonArrayCollection;
                    string name = Name.Value;
                    texts += (name + " ");
                    foreach(JsonObjectCollection l in list)
                    {
                        string param = l[0].GetValue() as string;
                        texts += (param + " ");
                    }
                    texts += "\r\n";
                }
                Script.Text = texts;
            }
 
        }
    }
}
 
cs



이를 이용해 유니티에서 사용한 예시 : http://violetababel.tistory.com/407

Comments