1 #include "studiox_includes.h"
2 #include "import_sprite_frames_dlg.h"
3
4 enum import_sprite_frames_dlg_test_commands {
5 TEST_SAVE = 1,
6 TEST_CANCEL
7 };
8
IMPLEMENT_DYNAMIC(import_sprite_frames_dlg,express_dialog)9 IMPLEMENT_DYNAMIC(import_sprite_frames_dlg, express_dialog)
10
11 BEGIN_MESSAGE_MAP(import_sprite_frames_dlg, express_dialog)
12 ON_LBN_SELCHANGE(IDC_PIX_FOLDER_LIST, OnSelectPixFolder)
13 ON_MESSAGE(STUDIO_TEST, OnTestMessage)
14 END_MESSAGE_MAP()
15
16 ///////////////////////////////////////////////////////////////////////////////
17 import_sprite_frames_dlg::import_sprite_frames_dlg(CWnd *pParent)
18 : express_dialog(import_sprite_frames_dlg::IDD, pParent)
19 {
20 IconId = IDB_PIXELMAPS;
21 SetTitleText("Import Sprite Frames");
22 mPixGroupInfo = NULL;
23 }
24
25 ///////////////////////////////////////////////////////////////////////////////
~import_sprite_frames_dlg()26 import_sprite_frames_dlg::~import_sprite_frames_dlg()
27 {
28 }
29
30 ///////////////////////////////////////////////////////////////////////////////
OnInitDialog()31 BOOL import_sprite_frames_dlg::OnInitDialog()
32 {
33 express_dialog::OnInitDialog();
34
35 AddCancelButton();
36 AddSaveButton(_T("Import"));
37 SetSavedMsg(L"Imported");
38
39 InitPixFolderList();
40
41 return TRUE;
42 }
43
44 ///////////////////////////////////////////////////////////////////////////////
DoDataExchange(CDataExchange * pDX)45 void import_sprite_frames_dlg::DoDataExchange(CDataExchange* pDX)
46 {
47 DDX_Control(pDX, IDC_PIX_FOLDER_LIST, mPixFolderList);
48 DDX_Control(pDX, IDC_FRAME_LIST, mFrameList);
49
50 express_dialog::DoDataExchange(pDX);
51 }
52
53 ///////////////////////////////////////////////////////////////////////////////
OnOK()54 void import_sprite_frames_dlg::OnOK()
55 {
56 bool select_all = false;
57
58 if (mFrameList.GetSelCount() <= 0)
59 {
60 select_all = true;
61 }
62
63 CString name;
64
65 for (int index = 0; index < mFrameList.GetCount(); index++)
66 {
67 if ((mFrameList.GetSel(index) > 0) || select_all)
68 {
69 mFrameList.GetText(index, name);
70 mFrameNameList.Add(name);
71 }
72 }
73
74 express_dialog::OnOK();
75 }
76
77 ///////////////////////////////////////////////////////////////////////////////
OnSelectPixFolder()78 void import_sprite_frames_dlg::OnSelectPixFolder()
79 {
80 studiox_project* project = GetOpenProject();
81
82 if (!project)
83 {
84 return;
85 }
86
87 int sel = mPixFolderList.GetCurSel();
88 int folder_id = mPixFolderList.GetItemData(sel);
89 CString folder_name;
90 mPixFolderList.GetText(sel, folder_name);
91
92 res_info *info = project->FindResourceFolder(mPixGroupInfo, RES_TYPE_FOLDER, folder_id, folder_name);
93 UpdateFrameList(info);
94 }
95
96 ///////////////////////////////////////////////////////////////////////////////
UpdateFrameList(res_info * folder_info)97 void import_sprite_frames_dlg::UpdateFrameList(res_info *folder_info)
98 {
99 studiox_project* project = GetOpenProject();
100
101 if (!project)
102 {
103 return;
104 }
105
106 res_info* child = folder_info->child;
107
108 mFrameList.ResetContent();
109
110 while (child)
111 {
112 if (child->type == RES_TYPE_PIXELMAP)
113 {
114 mFrameList.AddString(child->name);
115 }
116 child = child->next;
117 }
118
119 UpdateListBoxHorizontalExtend(&mFrameList);
120 mFrameList.SelItemRange(true, 0, mFrameList.GetCount() - 1);
121 }
122
123 ///////////////////////////////////////////////////////////////////////////////
InitPixFolderList()124 void import_sprite_frames_dlg::InitPixFolderList()
125 {
126 studiox_project* project = GetOpenProject();
127
128 if (!project || !GetProjectView())
129 {
130 return;
131 }
132
133 int display = GetProjectView()->GetActiveDisplay();
134 int theme = project->mDisplays[display].active_theme;
135
136 mPixGroupInfo = project->FindResource(display, theme, RES_TYPE_GROUP, L"Pixelmaps");
137
138 INT sel;
139
140 res_info *info = mPixGroupInfo->child;
141
142 while(info)
143 {
144 if (info->type == RES_TYPE_FOLDER)
145 {
146 sel = mPixFolderList.AddString(info->name);
147 mPixFolderList.SetItemData(sel, info->folder_id);
148 }
149
150 info = info->next;
151 }
152 UpdateListBoxHorizontalExtend(&mPixFolderList);
153 mPixFolderList.SetCurSel(0);
154 OnSelectPixFolder();
155 }
156
157 ///////////////////////////////////////////////////////////////////////////////
UpdateListBoxHorizontalExtend(CListBox * pList)158 void import_sprite_frames_dlg::UpdateListBoxHorizontalExtend(CListBox* pList)
159 {
160 // Find the longest string in the list box.
161 CString str;
162 CSize sz;
163 int dx = 0;
164 TEXTMETRIC tm;
165 CDC* pDC = pList->GetDC();
166 CFont* pFont = pList->GetFont();
167
168 // Select the listbox font, save the old font
169 CFont* pOldFont = pDC->SelectObject(pFont);
170 // Get the text metrics for avg char width
171 pDC->GetTextMetrics(&tm);
172
173 for (int index = 0; index < pList->GetCount(); index++)
174 {
175 pList->GetText(index, str);
176 sz = pDC->GetTextExtent(str);
177
178 // Add the avg width to prevent clipping
179 sz.cx += tm.tmAveCharWidth;
180
181 if (sz.cx > dx)
182 dx = sz.cx;
183 }
184 // Select the old font back into the DC
185 pDC->SelectObject(pOldFont);
186 pList->ReleaseDC(pDC);
187
188 // Set the horizontal extent so every character of all strings
189 // can be scrolled to.
190 pList->SetHorizontalExtent(dx);
191 }
192
193 //////////////////////////////////////////////////////////////////////////////
OnTestMessage(WPARAM wParam,LPARAM lParam)194 LRESULT import_sprite_frames_dlg::OnTestMessage(WPARAM wParam, LPARAM lParam)
195 {
196
197 switch (wParam)
198 {
199 case TEST_SAVE:
200 OnOK();
201 break;
202
203 case TEST_CANCEL:
204 OnCancel();
205 break;
206 }
207
208 return 0;
209 }
210
211