本篇是游戏中物件的定义与使用(3)的续篇。
下载源码和工程
创建MIL编辑器
主窗口的设计:
修改物件属性对话框的设计:
resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by MILEdit.rc
//
#define IDD_EDIT 101
#define IDD_MODIFY 102
#define IDR_241 105
#define IDC_LOAD 1000
#define IDC_SAVE 1001
#define IDC_ITEMS 1002
#define IDC_NEW 1003
#define IDC_EDIT 1004
#define IDC_OK 1004
#define IDC_CLEAR 1005
#define IDC_NUM 1005
#define IDC_WEAPON 1006
#define IDC_ARMOR 1007
#define IDC_SHIELD 1008
#define IDC_HEALING 1009
#define IDC_CONTAINER 1010
#define IDC_MONEY 1011
#define IDC_ACCESSORY 1012
#define IDC_EDIBLE 1013
#define IDC_COLLECTION 1014
#define IDC_TRANSPORTATION 1015
#define IDC_SELLABLE 1016
#define IDC_CANDROP 1017
#define IDC_USEONCE 1018
#define IDC_UNKNOWN 1019
#define IDC_USAGE 1020
#define IDC_NAME 1021
#define IDC_DESCRIPTION 1022
#define IDC_SETALL 1023
#define IDC_WEIGHT 1024
#define IDC_ITEMSIZE 1025
#define IDC_VALUE 1026
#define IDC_PRICE 1027
#define IDC_CLEARALL 1028
#define IDC_SCRIPT 1029
#define IDC_MESH 1030
#define IDC_IMAGE 1031
#define IDC_CANCEL 1032
#define IDC_OTHER 1033
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 106
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1022
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
MILREIT.RC
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_EDIT DIALOGEX 0, 0, 267, 130
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Master Item List Editor"
CLASS "MILEDIT"
FONT 11, "Segoe UI", 400, 0, 0x0
BEGIN
PUSHBUTTON "Load",IDC_LOAD,210,90,50,14
PUSHBUTTON "Save",IDC_SAVE,210,110,50,14
LISTBOX IDC_ITEMS,5,15,195,109,LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
LTEXT "Item List",IDC_STATIC,5,5,50,8
PUSHBUTTON "New",IDC_NEW,210,70,50,14
DEFPUSHBUTTON "Edit",IDC_EDIT,210,15,50,14
PUSHBUTTON "Clear",IDC_CLEAR,210,35,50,14
END
IDD_MODIFY DIALOGEX 0, 0, 282, 196
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION
CAPTION "Modify Item"
FONT 11, "Segoe UI", 400, 0, 0x0
BEGIN
EDITTEXT IDC_NAME,55,20,80,12,ES_AUTOHSCROLL
EDITTEXT IDC_DESCRIPTION,55,35,80,12,ES_AUTOHSCROLL
EDITTEXT IDC_WEIGHT,55,50,80,12,ES_AUTOHSCROLL
EDITTEXT IDC_ITEMSIZE,55,65,80,12,ES_AUTOHSCROLL
EDITTEXT IDC_VALUE,55,80,80,12,ES_AUTOHSCROLL
EDITTEXT IDC_PRICE,55,95,80,12,ES_AUTOHSCROLL
EDITTEXT IDC_SCRIPT,55,120,80,12,ES_AUTOHSCROLL
EDITTEXT IDC_MESH,55,135,80,12,ES_AUTOHSCROLL
EDITTEXT IDC_IMAGE,55,150,80,12,ES_AUTOHSCROLL
CONTROL "Money",IDC_MONEY,"Button",BS_AUTORADIOBUTTON | WS_GROUP,150,20,60,10
CONTROL "Weapon",IDC_WEAPON,"Button",BS_AUTORADIOBUTTON,150,30,60,10
CONTROL "Armor",IDC_ARMOR,"Button",BS_AUTORADIOBUTTON,150,40,60,10
CONTROL "Shield",IDC_SHIELD,"Button",BS_AUTORADIOBUTTON,150,50,60,10
CONTROL "Accessory",IDC_ACCESSORY,"Button",BS_AUTORADIOBUTTON,150,60,60,10
CONTROL "Edible",IDC_EDIBLE,"Button",BS_AUTORADIOBUTTON,150,70,60,10
CONTROL "Healing",IDC_HEALING,"Button",BS_AUTORADIOBUTTON,150,80,60,10
CONTROL "Collection",IDC_COLLECTION,"Button",BS_AUTORADIOBUTTON,150,90,60,10
CONTROL "Transportation",IDC_TRANSPORTATION,"Button",BS_AUTORADIOBUTTON,150,100,60,10
CONTROL "Container",IDC_CONTAINER,"Button",BS_AUTORADIOBUTTON,150,110,60,10
CONTROL "Other",IDC_OTHER,"Button",BS_AUTORADIOBUTTON,150,120,60,10
CONTROL "Sellable",IDC_SELLABLE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,150,140,50,10
CONTROL "Can Drop",IDC_CANDROP,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,150,151,50,10
CONTROL "Use Once",IDC_USEONCE,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,215,140,50,10
CONTROL "Unknown",IDC_UNKNOWN,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,215,151,50,10
LISTBOX IDC_USAGE,215,30,60,40,LBS_SORT | LBS_MULTIPLESEL | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
PUSHBUTTON "Set All",IDC_SETALL,220,75,50,14
PUSHBUTTON "Clear All",IDC_CLEARALL,220,95,50,14
DEFPUSHBUTTON "OK",IDC_OK,69,175,50,14
PUSHBUTTON "Cancel",IDC_CANCEL,163,175,50,14
LTEXT "Item #",IDC_STATIC,5,5,25,10
LTEXT "#",IDC_NUM,35,5,50,10
RTEXT "Name",IDC_STATIC,25,20,25,10
RTEXT "Description",IDC_STATIC,11,35,40,10
RTEXT "Weight",IDC_STATIC,21,50,30,10
RTEXT "Size",IDC_STATIC,25,65,25,10
RTEXT "Modifier Value",IDC_STATIC,5,80,45,10
RTEXT "Price",IDC_STATIC,31,95,20,10
LTEXT "Usage:",IDC_STATIC,215,20,45,10
RTEXT "Script",IDC_STATIC,25,120,25,10
RTEXT "Mesh",IDC_STATIC,25,135,25,10
RTEXT "Image",IDC_STATIC,25,150,25,10
END
/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_EDIT, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 260
TOPMARGIN, 7
BOTTOMMARGIN, 123
END
IDD_MODIFY, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 275
TOPMARGIN, 7
BOTTOMMARGIN, 189
END
END
#endif // APSTUDIO_INVOKED
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
#endif // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
主程序:
/*************************************************************************
PURPOSE:
Master Item List edit.
*************************************************************************/
#include <windows.h>
#include <stdio.h>
#include <commctrl.h>
#include "resource.h"
#include "mil.h"
#include "tool.h"
#pragma warning(disable : 4312 4313 4996)
#define MAX_ITEMS 1024
#define array_num(arr) (sizeof(arr) / sizeof((arr)[0]))
//////////////////////////////////// global variable ////////////////////////////////////
HWND g_hwnd;
// The OPENFILENAME structure contains information that the GetOpenFileName and GetSaveFileName
// functions use to initialize an Open or Save As dialog box. After the user closes the dialog box,
// the system returns information about the user's selection in this structure.
OPENFILENAME g_ofn;
char g_mil_file[MAX_PATH]; // filename for item files
sItem g_items[MAX_ITEMS]; // item list
long g_edit_item_index; // item index to modify
HWND g_items_wnd; // handle to items listbox
const WORD g_categories[] = {
IDC_MONEY, IDC_WEAPON, IDC_ARMOR, IDC_SHIELD,
IDC_ACCESSORY, IDC_EDIBLE, IDC_HEALING,
IDC_COLLECTION, IDC_TRANSPORTATION, IDC_CONTAINER,
IDC_OTHER
};
const WORD g_flags[] = {
IDC_SELLABLE, IDC_CANDROP, IDC_USEONCE, IDC_UNKNOWN
};
//////////////////////////////////// function ////////////////////////////////////
//-----------------------------------------------------------------------------------
// Load all items information from file.
//-----------------------------------------------------------------------------------
BOOL load_items(const char* filename)
{
ZeroMemory(g_items, sizeof(g_items));
// read in all items from file
FILE* fp;
if((fp = fopen(filename, "rb")) == NULL)
return FALSE;
fread(&g_items, 1, sizeof(g_items), fp);
fclose(fp);
// update items listbox
reset_listbox(g_items_wnd);
for(long i = 0; i < MAX_ITEMS; i++)
{
char text[256];
sprintf(text, "%5lu: %s", i, g_items[i].name);
add_string_to_listbox(g_items_wnd, text);
}
return TRUE;
}
//-----------------------------------------------------------------------------------
// New a mater item list.
//-----------------------------------------------------------------------------------
BOOL new_mil()
{
if(MessageBox(g_hwnd, "Are you sure? (Looses any unsaved MIL information)", "New MIL", MB_YESNO) != IDYES)
return FALSE;
reset_listbox(g_items_wnd);
ZeroMemory(g_items, sizeof(g_items));
for(long i = 0; i < MAX_ITEMS; i++)
{
char buf[16];
sprintf(buf, "%5lu:", i);
insert_string_to_listbox(g_items_wnd, i, buf);
}
return TRUE;
}
//-----------------------------------------------------------------------------------
// Load all items from file.
//-----------------------------------------------------------------------------------
BOOL load_mil()
{
// setup the open dialog informaotion
g_ofn.hwndOwner = g_hwnd;
g_ofn.lpstrFile = g_mil_file;
g_ofn.lpstrTitle = "Load MIL File";
g_ofn.lpstrFilter = "MIL Item Files (*.mil)\0*.mil\0All Files (*.*)\0*.*\0\0";
g_ofn.lpstrDefExt = "mil";
// ask for filename
if(! GetOpenFileName(&g_ofn))
return FALSE;
if(! load_items(g_mil_file))
{
MessageBox(g_hwnd, g_mil_file, "Unable to open file.", MB_OK);
return FALSE;
}
return TRUE;
}
//-----------------------------------------------------------------------------------
// Save all items information to file.
//-----------------------------------------------------------------------------------
BOOL save_mil()
{
// setup the open dialog information
g_ofn.hwndOwner = g_hwnd;
g_ofn.lpstrFile = g_mil_file;
g_ofn.lpstrTitle = "Load MIL File";
g_ofn.lpstrFilter = "MIL Item Files (*.mil)\0*.mil\0All Files (*.*)\0*.*\0\0";
g_ofn.lpstrDefExt = "mil";
// Ask for filename
if(! GetSaveFileName(&g_ofn))
return FALSE;
FILE* fp;
if((fp = fopen(g_mil_file, "wb")) == NULL)
return FALSE;
fwrite(&g_items, 1, sizeof(g_items), fp);
fclose(fp);
return TRUE;
}
//-----------------------------------------------------------------------------------
// Update entry.
//-----------------------------------------------------------------------------------
void update_entry(HWND dlg, long item_index)
{
sItem& item = g_items[item_index];
// update category
for(long i = 0; i < array_num(g_items); i++)
{
if(get_listbox_cur_sel(GetDlgItem(dlg, g_categories[i])) == TRUE)
{
item.category = i;
break;
}
}
// update flags
item.flags = 0;
for(long i = 0; i < array_num(g_flags); i++)
{
if(get_listbox_cur_sel(GetDlgItem(dlg, g_flags[i])) == TRUE)
set_bit(item.flags, i);
}
// update usage
item.usage = 0;
HWND usage_wnd = GetDlgItem(dlg, IDC_USAGE);
for(long i = 0; i < 32; i++)
{
if(get_listbox_sel(usage_wnd, i) == TRUE)
set_bit(item.usage, i);
}
char text[32];
// update entries
GetWindowText(GetDlgItem(dlg, IDC_NAME), item.name, sizeof(item.name));
GetWindowText(GetDlgItem(dlg, IDC_DESCRIPTION), item.desc, sizeof(item.desc));
GetWindowText(GetDlgItem(dlg, IDC_WEIGHT), text, sizeof(text));
item.weight = (float)atof(text);
GetWindowText(GetDlgItem(dlg, IDC_ITEMSIZE), text, sizeof(text));
item.size = (float)atof(text);
GetWindowText(GetDlgItem(dlg, IDC_VALUE), text, sizeof(text));
item.value = atol(text);
GetWindowText(GetDlgItem(dlg, IDC_PRICE), text, sizeof(text));
item.price = atol(text);
GetWindowText(GetDlgItem(dlg, IDC_SCRIPT), item.script_filename, sizeof(item.script_filename));
GetWindowText(GetDlgItem(dlg, IDC_MESH), item.mesh_filename, sizeof(item.mesh_filename));
GetWindowText(GetDlgItem(dlg, IDC_IMAGE), item.image_filename, sizeof(item.image_filename));
}
//-----------------------------------------------------------------------------------
// Modify dialog procedure.
//-----------------------------------------------------------------------------------
BOOL CALLBACK modify_dialog_proc(HWND dlg, UINT msg_id, WPARAM word_param, LPARAM long_param)
{
char text[16];
static HWND usage_wnd;
switch(msg_id)
{
case WM_INITDIALOG:
{
// return an error if item index is not valid
if(g_edit_item_index > MAX_ITEMS)
{
EndDialog(dlg, FALSE);
return FALSE;
}
// set up current item attributes
sItem& item = g_items[g_edit_item_index];
// set for category button
long cate_index = item.category;
set_button_state(GetDlgItem(dlg, g_categories[cate_index]), BST_CHECKED);
// set for flag button
for(long i = 0; i < array_num(g_flags); i++)
{
long flag_state = check_bit(item.flags, i);
set_button_state(GetDlgItem(dlg, g_flags[i]), flag_state);
}
// set for usage listbox
usage_wnd = GetDlgItem(dlg, IDC_USAGE);
reset_listbox(usage_wnd);
for(long i = 0; i < 32; i++)
{
sprintf(text, "%5lu", i);
insert_string_to_listbox(usage_wnd, i, text);
if(check_bit(item.usage, i))
set_listbox_sel(usage_wnd, TRUE, i);
}
// set item number
sprintf(text, "%lu", g_edit_item_index);
SetWindowText(GetDlgItem(dlg, IDC_NUM), text);
// set for other entries control
SetWindowText(GetDlgItem(dlg, IDC_NAME), item.name);
SetWindowText(GetDlgItem(dlg, IDC_DESCRIPTION), item.desc);
sprintf(text, "%lf", item.weight);
SetWindowText(GetDlgItem(dlg, IDC_WEIGHT), text);
sprintf(text, "%lf", item.size);
SetWindowText(GetDlgItem(dlg, IDC_ITEMSIZE), text);
sprintf(text, "%lf", item.value);
SetWindowText(GetDlgItem(dlg, IDC_VALUE), text);
sprintf(text, "%lf", item.price);
SetWindowText(GetDlgItem(dlg, IDC_PRICE), text);
SetWindowText(GetDlgItem(dlg, IDC_SCRIPT), item.script_filename);
SetWindowText(GetDlgItem(dlg, IDC_MESH), item.mesh_filename);
SetWindowText(GetDlgItem(dlg, IDC_IMAGE), item.image_filename);
return TRUE;
}
case WM_COMMAND:
switch(LOWORD(word_param))
{
case IDC_OK:
update_entry(dlg, g_edit_item_index);
EndDialog(dlg, TRUE);
return TRUE;
case IDC_CANCEL:
EndDialog(dlg, FALSE);
return TRUE;
case IDC_SETALL:
for(long i = 0; i < 32; i++)
set_listbox_sel(usage_wnd, TRUE, i);
break;
case IDC_CLEARALL:
for(long i = 0; i < 32; i++)
set_listbox_sel(usage_wnd, FALSE, i);
break;
}
break;
}
return FALSE;
}
//------------------------------------------------------------------------------------------------
// Main window procedure.
//------------------------------------------------------------------------------------------------
LRESULT CALLBACK window_proc(HWND hwnd, UINT msg_id, WPARAM word_param, LPARAM long_param)
{
int sel;
char text[256];
switch(msg_id)
{
case WM_COMMAND:
switch(LOWORD(word_param))
{
case IDC_NEW: // new MIL file
new_mil();
break;
case IDC_LOAD: // load a mil file
load_mil();
break;
case IDC_SAVE: // sae a mil file
save_mil();
break;
case IDC_CLEAR: // clear an items
if((sel = (int)get_listbox_cur_sel(g_items_wnd)) == LB_ERR)
break;
ZeroMemory(&g_items[sel], sizeof(sItem));
delete_listbox_string(g_items_wnd, sel);
sprintf(text, "%5lu:", sel);
insert_string_to_listbox(g_items_wnd, sel, text);
break;
case IDC_ITEMS: // edit an entry
if(HIWORD(word_param) != LBN_DBLCLK)
break;
case IDC_EDIT:
if((sel = (int)get_listbox_cur_sel(g_items_wnd)) == LB_ERR)
break;
g_edit_item_index = sel;
DialogBox(NULL, MAKEINTRESOURCE(IDD_MODIFY), hwnd, modify_dialog_proc);
delete_listbox_string(g_items_wnd, sel);
sprintf(text, "%5lu: %s", sel, g_items[sel].name);
insert_string_to_listbox(g_items_wnd, sel, text);
break;
}
break;
case WM_CREATE:
// initialize the save/load dialog box information
ZeroMemory(&g_ofn, sizeof(OPENFILENAME));
g_ofn.lStructSize = sizeof(OPENFILENAME);
g_ofn.nMaxFile = MAX_PATH;
g_ofn.nMaxFileTitle = MAX_PATH;
g_ofn.Flags = OFN_HIDEREADONLY;
// set default MIL filename
strcpy(g_mil_file, "..\\data\\default.mil");
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg_id, word_param, long_param);
}
return 0;
}
//-----------------------------------------------------------------------------------
// Routine entry.
//-----------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
InitCommonControls();
const char* class_name = "MILEDIT";
WNDCLASS win_class;
// create window class and register it
win_class.style = CS_HREDRAW | CS_VREDRAW;
win_class.lpfnWndProc = window_proc;
win_class.cbClsExtra = 0;
win_class.cbWndExtra = DLGWINDOWEXTRA;
win_class.hInstance = inst;
win_class.hIcon = LoadIcon(inst, IDI_APPLICATION);
win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
win_class.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
win_class.lpszMenuName = NULL;
win_class.lpszClassName = class_name;
if(! RegisterClass(&win_class))
return FALSE;
// The CreateDialog macro creates a modeless dialog box from a dialog box template resource.
// The CreateDialog macro uses the CreateDialogParam function.
//
// HWND CreateDialog(
// HINSTANCE hInstance,
// LPCTSTR lpTemplate,
// HWND hWndParent,
// DLGPROC lpDialogFunc);
//
// hInstance:
// [in] Handle to the module whose executable file contains the dialog box template.
//
// lpTemplate
// [in] Specifies the dialog box template. This parameter is either the pointer to a null-terminated
// character string that specifies the name of the dialog box template or an integer value that
// specifies the resource identifier of the dialog box template. If the parameter specifies a resource
// identifier, its high-order word must be zero and its low-order word must contain the identifier.
// You can use the MAKEINTRESOURCE macro to create this value.
//
// hWndParent:
// [in] Handle to the window that owns the dialog box.
//
// lpDialogFunc:
// [in] Pointer to the dialog box procedure. For more information about the dialog box procedure,
// see DialogProc.
//
// Return Value:
// If the function succeeds, the return value is the handle to the dialog box.
// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
// Create the dialog box window and show it
g_hwnd = CreateDialog(inst, MAKEINTRESOURCE(IDD_EDIT), 0, NULL);
ShowWindow(g_hwnd, cmd_show);
UpdateWindow(g_hwnd);
g_items_wnd = GetDlgItem(g_hwnd, IDC_ITEMS);
// force a load of action in default.mla
load_items("..\\Data\\Default.mil");
MSG msg;
// message loop
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterClass(class_name, inst);
return 0;
}