1 Star 0 Fork 0

yoyojacky/CSMoE

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
BuildModeDialog.cpp 37.47 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
//========= Copyright ?1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include <ctype.h>
#include <stdio.h>
#include <tier1/utlvector.h>
#include <tier1/KeyValues.h>
#include <vgui/IInputInternal.h>
#include <vgui/ILocalize.h>
#include <vgui/ISurface.h>
#include <vgui/ISystem.h>
#include <vgui/KeyCode.h>
#include <vgui/MouseCode.h>
#include "BuildModeDialog.h"
#include "Label.h"
#include "TextEntry.h"
#include "Button.h"
#include "CheckButton.h"
#include "RadioButton.h"
#include "MenuButton.h"
#include "ComboBox.h"
#include "BuildGroup.h"
#include "MessageBox.h"
#include "Menu.h"
#include "Divider.h"
#include "PanelListPanel.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
using namespace vgui2;
struct PanelItem_t
{
PanelItem_t() : m_EditLabel(NULL) {}
Panel *m_EditLabel;
TextEntry *m_EditPanel;
ComboBox *m_pCombo;
Button *m_EditButton;
char m_szName[64];
int m_iType;
};
class CSmallTextEntry : public TextEntry
{
DECLARE_CLASS_SIMPLE( CSmallTextEntry, TextEntry );
public:
CSmallTextEntry( Panel *parent, char const *panelName ) :
BaseClass( parent, panelName )
{
}
virtual void ApplySchemeSettings( IScheme *scheme )
{
BaseClass::ApplySchemeSettings( scheme );
SetFont( scheme->GetFont( "DefaultVerySmall" ) );
}
};
//-----------------------------------------------------------------------------
// Purpose: Holds a list of all the edit fields for the currently selected panel
//-----------------------------------------------------------------------------
class BuildModeDialog::PanelList
{
public:
CUtlVector<PanelItem_t> m_PanelList;
void AddItem( Panel *label, TextEntry *edit, ComboBox *combo, Button *button, const char *name, int type )
{
PanelItem_t item;
item.m_EditLabel = label;
item.m_EditPanel = edit;
Q_strncpy(item.m_szName, name, sizeof(item.m_szName));
item.m_iType = type;
item.m_pCombo = combo;
item.m_EditButton = button;
m_PanelList.AddToTail( item );
}
void RemoveAll( void )
{
for ( int i = 0; i < m_PanelList.Size(); i++ )
{
PanelItem_t *item = &m_PanelList[i];
delete item->m_EditLabel;
delete item->m_EditPanel;
delete item->m_EditButton;
}
m_PanelList.RemoveAll();
m_pControls->RemoveAll();
}
KeyValues *m_pResourceData;
PanelListPanel *m_pControls;
};
//-----------------------------------------------------------------------------
// Purpose: Dialog for adding localized strings
//-----------------------------------------------------------------------------
class BuildModeLocalizedStringEditDialog : public Frame
{
DECLARE_CLASS_SIMPLE(BuildModeLocalizedStringEditDialog, Frame);
public:
#pragma warning( disable : 4355 )
BuildModeLocalizedStringEditDialog() : Frame(this, NULL)
{
m_pTokenEntry = new TextEntry(this, NULL);
m_pValueEntry = new TextEntry(this, NULL);
m_pFileCombo = new ComboBox(this, NULL, 12, false);
m_pOKButton = new Button(this, NULL, "OK");
m_pCancelButton = new Button(this, NULL, "Cancel");
m_pCancelButton->SetCommand("Close");
m_pOKButton->SetCommand("OK");
// add the files to the combo
for (int i = 0; i < localize()->GetLocalizationFileCount(); i++)
{
m_pFileCombo->AddItem(localize()->GetLocalizationFileName(i), NULL);
}
}
#pragma warning( default : 4355 )
virtual void DoModal(const char *token)
{
input()->SetAppModalSurface(GetVPanel());
// setup data
m_pTokenEntry->SetText(token);
// lookup the value
StringIndex_t val = localize()->FindIndex(token);
if (val != INVALID_STRING_INDEX)
{
m_pValueEntry->SetText(localize()->GetValueByIndex(val));
// set the place in the file combo
m_pFileCombo->SetText(localize()->GetFileNameByIndex(val));
}
else
{
m_pValueEntry->SetText("");
}
}
private:
virtual void PerformLayout()
{
}
virtual void OnClose()
{
input()->SetAppModalSurface(NULL);
BaseClass::OnClose();
//PostActionSignal(new KeyValues("Command"
}
virtual void OnCommand(const char *command)
{
if (!stricmp(command, "OK"))
{
//!! apply changes
}
else
{
BaseClass::OnCommand(command);
}
}
vgui2::TextEntry *m_pTokenEntry;
vgui2::TextEntry *m_pValueEntry;
vgui2::ComboBox *m_pFileCombo;
vgui2::Button *m_pOKButton;
vgui2::Button *m_pCancelButton;
};
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
BuildModeDialog::BuildModeDialog(BuildGroup *buildGroup) : Frame(buildGroup->GetContextPanel(), "BuildModeDialog")
{
SetMinimumSize(300, 256);
SetSize(300, 420);
m_pCurrentPanel = NULL;
m_pEditableParents = NULL;
m_pEditableChildren = NULL;
m_pNextChild = NULL;
m_pPrevChild = NULL;
m_pBuildGroup = buildGroup;
_undoSettings = NULL;
_copySettings = NULL;
_autoUpdate = false;
MakePopup();
SetTitle("VGUI Build Mode Editor", true);
CreateControls();
LoadUserConfig("BuildModeDialog");
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
BuildModeDialog::~BuildModeDialog()
{
m_pPanelList->m_pResourceData->deleteThis();
m_pPanelList->m_pControls->DeleteAllItems();
if (_undoSettings)
_undoSettings->deleteThis();
if (_copySettings)
_copySettings->deleteThis();
}
//-----------------------------------------------------------------------------
// Purpose: makes sure build mode has been shut down properly
//-----------------------------------------------------------------------------
void BuildModeDialog::OnClose()
{
if (m_pBuildGroup->IsEnabled())
{
m_pBuildGroup->SetEnabled(false);
}
else
{
BaseClass::OnClose();
MarkForDeletion();
}
}
class CBuildModeNavCombo : public ComboBox
{
DECLARE_CLASS_SIMPLE( CBuildModeNavCombo, ComboBox );
public:
CBuildModeNavCombo(Panel *parent, const char *panelName, int numLines, bool allowEdit, bool getParents, Panel *context ) :
BaseClass( parent, panelName, numLines, allowEdit ),
m_bParents( getParents )
{
m_hContext = context;
}
virtual void OnShowMenu(Menu *menu)
{
menu->DeleteAllItems();
if ( !m_hContext.Get() )
return;
if ( m_bParents )
{
Panel *p = m_hContext->GetParent();
while ( p )
{
EditablePanel *ep = dynamic_cast < EditablePanel * >( p );
if ( ep && ep->GetBuildGroup() )
{
KeyValues *kv = new KeyValues( "Panel" );
kv->SetPtr( "ptr", p );
char const *text = ep->GetName() ? ep->GetName() : "unnamed";
menu->AddMenuItem( text, new KeyValues("SetText", "text", text), GetParent(), kv );
}
p = p->GetParent();
}
}
else
{
int i;
int c = m_hContext->GetChildCount();
for ( i = 0; i < c; ++i )
{
EditablePanel *ep = dynamic_cast < EditablePanel * >( m_hContext->GetChild( i ) );
if ( ep && ep->IsVisible() && ep->GetBuildGroup() )
{
KeyValues *kv = new KeyValues( "Panel" );
kv->SetPtr( "ptr", ep );
char const *text = ep->GetName() ? ep->GetName() : "unnamed";
menu->AddMenuItem( text, new KeyValues("SetText", "text", text), GetParent(), kv );
}
}
}
}
private:
bool m_bParents;
vgui2::PHandle m_hContext;
};
//-----------------------------------------------------------------------------
// Purpose: Creates the build mode editing controls
//-----------------------------------------------------------------------------
void BuildModeDialog::CreateControls()
{
int i;
m_pPanelList = new PanelList;
m_pPanelList->m_pResourceData = new KeyValues( "BuildDialog" );
m_pPanelList->m_pControls = new PanelListPanel(this, "BuildModeControls");
// file to edit combo box is first
m_pFileSelectionCombo = new ComboBox(this, "FileSelectionCombo", 10, false);
for ( i = 0; i < m_pBuildGroup->GetRegisteredControlSettingsFileCount(); i++)
{
m_pFileSelectionCombo->AddItem(m_pBuildGroup->GetRegisteredControlSettingsFileByIndex(i), NULL);
}
if (m_pFileSelectionCombo->GetItemCount() < 2)
{
m_pFileSelectionCombo->SetEnabled(false);
}
int buttonH = 18;
// status info at top of dialog
m_pStatusLabel = new Label(this, "StatusLabel", "[nothing currently selected]");
m_pStatusLabel->SetTextColorState(Label::CS_DULL);
m_pStatusLabel->SetTall( buttonH );
m_pDivider = new Divider(this, "Divider");
// drop-down combo box for adding new controls
m_pAddNewControlCombo = new ComboBox(this, NULL, 30, false);
m_pAddNewControlCombo->SetSize(116, buttonH);
m_pAddNewControlCombo->SetOpenDirection(Menu::DOWN);
m_pEditableParents = new CBuildModeNavCombo( this, NULL, 15, false, true, m_pBuildGroup->GetContextPanel() );
m_pEditableParents->SetSize(116, buttonH);
m_pEditableParents->SetOpenDirection(Menu::DOWN);
m_pEditableChildren = new CBuildModeNavCombo( this, NULL, 15, false, false, m_pBuildGroup->GetContextPanel() );
m_pEditableChildren->SetSize(116, buttonH);
m_pEditableChildren->SetOpenDirection(Menu::DOWN);
m_pNextChild = new Button( this, "NextChild", "Next", this );
m_pNextChild->SetCommand( new KeyValues( "OnChangeChild", "direction", 1 ) );
m_pPrevChild = new Button( this, "PrevChild", "Prev", this );
m_pPrevChild->SetCommand( new KeyValues( "OnChangeChild", "direction", -1 ) );
// controls that can be added
// this list comes from controls EditablePanel can create by name.
int defaultItem = m_pAddNewControlCombo->AddItem("None", NULL);
CUtlVector< char const * > names;
CBuildFactoryHelper::GetFactoryNames( names );
// Sort the names
CUtlRBTree< char const *, int > sorted( 0, 0, StringLessThan );
for ( i = 0; i < names.Count(); ++i )
{
sorted.Insert( names[ i ] );
}
for ( i = sorted.FirstInorder(); i != sorted.InvalidIndex(); i = sorted.NextInorder( i ) )
{
m_pAddNewControlCombo->AddItem( sorted[ i ], NULL );
}
m_pAddNewControlCombo->ActivateItem(defaultItem);
m_pExitButton = new Button(this, "ExitButton", "&Exit");
m_pExitButton->SetSize(64, buttonH);
m_pSaveButton = new Button(this, "SaveButton", "&Save");
m_pSaveButton->SetSize(64, buttonH);
m_pApplyButton = new Button(this, "ApplyButton", "&Apply");
m_pApplyButton->SetSize(64, buttonH);
m_pReloadLocalization = new Button( this, "Localization", "&Reload Localization" );
m_pReloadLocalization->SetSize( 100, buttonH );
m_pExitButton->SetCommand("Exit");
m_pSaveButton->SetCommand("Save");
m_pApplyButton->SetCommand("Apply");
m_pReloadLocalization->SetCommand( new KeyValues( "ReloadLocalization" ) );
m_pDeleteButton = new Button(this, "DeletePanelButton", "Delete");
m_pDeleteButton->SetSize(64, buttonH);
m_pDeleteButton->SetCommand("DeletePanel");
m_pVarsButton = new MenuButton(this, "VarsButton", "Variables");
m_pVarsButton->SetSize(72, buttonH);
m_pVarsButton->SetOpenDirection(Menu::UP);
// iterate the vars
KeyValues *vars = m_pBuildGroup->GetDialogVariables();
if (vars && vars->GetFirstSubKey())
{
// create the menu
m_pVarsButton->SetEnabled(true);
Menu *menu = new Menu(m_pVarsButton, "VarsMenu");
// set all the variables to be copied to the clipboard when selected
for (KeyValues *kv = vars->GetFirstSubKey(); kv != NULL; kv = kv->GetNextKey())
{
char buf[32];
_snprintf(buf, sizeof(buf), "%%%s%%", kv->GetName());
menu->AddMenuItem(kv->GetName(), new KeyValues("SetClipboardText", "text", buf), this);
}
m_pVarsButton->SetMenu(menu);
}
else
{
// no variables
m_pVarsButton->SetEnabled(false);
}
m_pApplyButton->SetTabPosition(1);
m_pPanelList->m_pControls->SetTabPosition(2);
m_pVarsButton->SetTabPosition(3);
m_pDeleteButton->SetTabPosition(4);
m_pAddNewControlCombo->SetTabPosition(5);
m_pSaveButton->SetTabPosition(6);
m_pExitButton->SetTabPosition(7);
m_pEditableParents->SetTabPosition( 8 );
m_pEditableChildren->SetTabPosition( 9 );
m_pPrevChild->SetTabPosition( 10 );
m_pNextChild->SetTabPosition( 11 );
m_pReloadLocalization->SetTabPosition( 12 );
}
void BuildModeDialog::ApplySchemeSettings( IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
HFont font = pScheme->GetFont( "DefaultVerySmall" );
m_pStatusLabel->SetFont( font );
m_pReloadLocalization->SetFont( font );
m_pExitButton->SetFont( font );
m_pSaveButton->SetFont( font );
m_pApplyButton->SetFont( font );
m_pAddNewControlCombo->SetFont( font );
m_pEditableParents->SetFont( font );
m_pEditableChildren->SetFont( font );
m_pDeleteButton->SetFont( font );
m_pVarsButton->SetFont( font );
m_pPrevChild->SetFont( font );
m_pNextChild->SetFont( font );
}
//-----------------------------------------------------------------------------
// Purpose: lays out controls
//-----------------------------------------------------------------------------
void BuildModeDialog::PerformLayout()
{
BaseClass::PerformLayout();
// layout parameters
const int BORDER_GAP = 16, YGAP_SMALL = 4, YGAP_LARGE = 8, TITLE_HEIGHT = 24, BOTTOM_CONTROLS_HEIGHT = 145, XGAP = 6;
int wide, tall;
GetSize(wide, tall);
int xpos = BORDER_GAP;
int ypos = BORDER_GAP + TITLE_HEIGHT;
// controls from top down
// selection combo
m_pFileSelectionCombo->SetBounds(xpos, ypos, wide - (BORDER_GAP * 2), m_pStatusLabel->GetTall());
ypos += (m_pStatusLabel->GetTall() + YGAP_SMALL);
// status
m_pStatusLabel->SetBounds(xpos, ypos, wide - (BORDER_GAP * 2), m_pStatusLabel->GetTall());
ypos += (m_pStatusLabel->GetTall() + YGAP_SMALL);
// center control
m_pPanelList->m_pControls->SetPos(xpos, ypos);
m_pPanelList->m_pControls->SetSize(wide - (BORDER_GAP * 2), tall - (ypos + BOTTOM_CONTROLS_HEIGHT));
// controls from bottom-right
ypos = tall - BORDER_GAP;
xpos = BORDER_GAP + m_pVarsButton->GetWide() + m_pDeleteButton->GetWide() + m_pAddNewControlCombo->GetWide() + (XGAP * 2);
// bottom row of buttons
ypos -= m_pApplyButton->GetTall();
xpos -= m_pApplyButton->GetWide();
m_pApplyButton->SetPos(xpos, ypos);
xpos -= m_pExitButton->GetWide();
xpos -= XGAP;
m_pExitButton->SetPos(xpos, ypos);
xpos -= m_pSaveButton->GetWide();
xpos -= XGAP;
m_pSaveButton->SetPos(xpos, ypos);
// divider
xpos = BORDER_GAP;
ypos -= (YGAP_LARGE + m_pDivider->GetTall());
m_pDivider->SetBounds(xpos, ypos, wide - (xpos + BORDER_GAP), 2);
ypos -= (YGAP_LARGE + m_pVarsButton->GetTall());
xpos = BORDER_GAP;
m_pEditableParents->SetPos( xpos, ypos );
m_pEditableChildren->SetPos( xpos + 150, ypos );
ypos -= (YGAP_LARGE + 18 );
xpos = BORDER_GAP;
m_pReloadLocalization->SetPos( xpos, ypos );
xpos += ( XGAP ) + m_pReloadLocalization->GetWide();
m_pPrevChild->SetPos( xpos, ypos );
m_pPrevChild->SetSize( 64, m_pReloadLocalization->GetTall() );
xpos += ( XGAP ) + m_pPrevChild->GetWide();
m_pNextChild->SetPos( xpos, ypos );
m_pNextChild->SetSize( 64, m_pReloadLocalization->GetTall() );
ypos -= (YGAP_LARGE + m_pVarsButton->GetTall());
xpos = BORDER_GAP;
// edit buttons
m_pVarsButton->SetPos(xpos, ypos);
xpos += (XGAP + m_pVarsButton->GetWide());
m_pDeleteButton->SetPos(xpos, ypos);
xpos += (XGAP + m_pDeleteButton->GetWide());
m_pAddNewControlCombo->SetPos(xpos, ypos);
}
//-----------------------------------------------------------------------------
// Purpose: Deletes all the controls from the panel
//-----------------------------------------------------------------------------
void BuildModeDialog::RemoveAllControls( void )
{
// free the array
m_pPanelList->RemoveAll();
}
//-----------------------------------------------------------------------------
// Purpose: simple helper function to get a token from a string
// Input : char **string - pointer to the string pointer, which will be incremented
// Output : const char * - pointer to the token
//-----------------------------------------------------------------------------
const char *ParseTokenFromString( const char **string )
{
static char buf[128];
buf[0] = 0;
// find the first alnum character
const char *tok = *string;
while ( !isalnum(*tok) && *tok != 0 )
{
tok++;
}
// read in all the alnum characters
int pos = 0;
while ( isalnum(tok[pos]) )
{
buf[pos] = tok[pos];
pos++;
}
// null terminate the token
buf[pos] = 0;
// update the main string pointer
*string = &(tok[pos]);
// return a pointer to the static buffer
return buf;
}
void BuildModeDialog::OnTextKillFocus()
{
if ( !m_pCurrentPanel )
return;
ApplyDataToControls();
}
//-----------------------------------------------------------------------------
// Purpose: sets up the current control to edit
//-----------------------------------------------------------------------------
void BuildModeDialog::SetActiveControl(Panel *controlToEdit)
{
if (m_pCurrentPanel == controlToEdit)
{
// it's already set, so just update the property data and quit
if (m_pCurrentPanel)
{
UpdateControlData(m_pCurrentPanel);
}
return;
}
// reset the data
m_pCurrentPanel = controlToEdit;
RemoveAllControls();
m_pPanelList->m_pControls->MoveScrollBarToTop();
if (!m_pCurrentPanel)
{
m_pStatusLabel->SetText("[nothing currently selected]");
m_pStatusLabel->SetTextColorState(Label::CS_DULL);
RemoveAllControls();
return;
}
// get the control description string
const char *controlDesc = m_pCurrentPanel->GetDescription();
// parse out the control description
int tabPosition = 1;
while (1)
{
const char *dataType = ParseTokenFromString(&controlDesc);
// finish when we have no more tokens
if (*dataType == 0)
break;
// default the data type to a string
int datat = TYPE_STRING;
if (!stricmp(dataType, "int"))
{
datat = TYPE_STRING; //!! just for now
}
else if (!stricmp(dataType, "alignment"))
{
datat = TYPE_ALIGNMENT;
}
else if (!stricmp(dataType, "autoresize"))
{
datat = TYPE_AUTORESIZE;
}
else if (!stricmp(dataType, "corner"))
{
datat = TYPE_CORNER;
}
else if (!stricmp(dataType, "localize"))
{
datat = TYPE_LOCALIZEDSTRING;
}
// get the field name
const char *fieldName = ParseTokenFromString(&controlDesc);
int itemHeight = 18;
// build a control & label
Label *label = new Label(this, NULL, fieldName);
label->SetSize(96, itemHeight);
label->SetContentAlignment(Label::a_east);
TextEntry *edit = NULL;
ComboBox *editCombo = NULL;
Button *editButton = NULL;
if (datat == TYPE_ALIGNMENT)
{
// drop-down combo box
editCombo = new ComboBox(this, NULL, 9, false);
editCombo->AddItem("north-west", NULL);
editCombo->AddItem("north", NULL);
editCombo->AddItem("north-east", NULL);
editCombo->AddItem("west", NULL);
editCombo->AddItem("center", NULL);
editCombo->AddItem("east", NULL);
editCombo->AddItem("south-west", NULL);
editCombo->AddItem("south", NULL);
editCombo->AddItem("south-east", NULL);
edit = editCombo;
}
else if (datat == TYPE_AUTORESIZE)
{
// drop-down combo box
editCombo = new ComboBox(this, NULL, 4, false);
editCombo->AddItem( "0 - no auto-resize", NULL);
editCombo->AddItem( "1 - resize right", NULL);
editCombo->AddItem( "2 - resize down", NULL);
editCombo->AddItem( "3 - down & right", NULL);
edit = editCombo;
}
else if (datat == TYPE_CORNER)
{
// drop-down combo box
editCombo = new ComboBox(this, NULL, 4, false);
editCombo->AddItem("0 - top-left", NULL);
editCombo->AddItem("1 - top-right", NULL);
editCombo->AddItem("2 - bottom-left", NULL);
editCombo->AddItem("3 - bottom-right", NULL);
edit = editCombo;
}
else if (datat == TYPE_LOCALIZEDSTRING)
{
editButton = new Button(this, NULL, "...");
editButton->SetParent(this);
editButton->AddActionSignalTarget(this);
editButton->SetTabPosition(tabPosition++);
editButton->SetTall( itemHeight );
label->SetAssociatedControl(editButton);
}
else
{
// normal string edit
edit = new CSmallTextEntry(this, NULL);
}
if (edit)
{
edit->SetTall( itemHeight );
edit->SetParent(this);
edit->AddActionSignalTarget(this);
edit->SetTabPosition(tabPosition++);
label->SetAssociatedControl(edit);
}
HFont smallFont = scheme()->GetIScheme( GetScheme() )->GetFont( "DefaultVerySmall" );
if ( label )
{
label->SetFont( smallFont );
}
if ( edit )
{
edit->SetFont( smallFont );
}
if ( editCombo )
{
editCombo->SetFont( smallFont );
}
if ( editButton )
{
editButton->SetFont( smallFont );
}
// add to our control list
m_pPanelList->AddItem(label, edit, editCombo, editButton, fieldName, datat);
if ( edit )
{
m_pPanelList->m_pControls->AddItem(label, edit);
}
else
{
m_pPanelList->m_pControls->AddItem(label, editButton);
}
}
// check and see if the current panel is a Label
// iterate through the class hierarchy
if ( controlToEdit->IsBuildModeDeletable() )
{
m_pDeleteButton->SetEnabled(true);
}
else
{
m_pDeleteButton->SetEnabled(false);
}
// update the property data in the dialog
UpdateControlData(m_pCurrentPanel);
// set our title
if ( m_pBuildGroup->GetResourceName() )
{
m_pFileSelectionCombo->SetText(m_pBuildGroup->GetResourceName());
}
else
{
m_pFileSelectionCombo->SetText("[ no resource file associated with dialog ]");
}
m_pApplyButton->SetEnabled(false);
InvalidateLayout();
Repaint();
}
//-----------------------------------------------------------------------------
// Purpose: Updates the edit fields with information about the control
//-----------------------------------------------------------------------------
void BuildModeDialog::UpdateControlData(Panel *control)
{
KeyValues *dat = m_pPanelList->m_pResourceData->FindKey( control->GetName(), true );
control->GetSettings( dat );
// apply the settings to the edit panels
for ( int i = 0; i < m_pPanelList->m_PanelList.Size(); i++ )
{
const char *name = m_pPanelList->m_PanelList[i].m_szName;
const char *datstring = dat->GetString( name, "" );
UpdateEditControl(m_pPanelList->m_PanelList[i], datstring);
}
char statusText[512];
Q_snprintf(statusText, sizeof(statusText), "%s: \'%s\'", control->GetClassName(), control->GetName());
m_pStatusLabel->SetText(statusText);
m_pStatusLabel->SetTextColorState(Label::CS_NORMAL);
}
//-----------------------------------------------------------------------------
// Purpose: Updates the data in a single edit control
//-----------------------------------------------------------------------------
void BuildModeDialog::UpdateEditControl(PanelItem_t &panelItem, const char *datstring)
{
switch (panelItem.m_iType)
{
case TYPE_AUTORESIZE:
case TYPE_CORNER:
{
int dat = atoi(datstring);
panelItem.m_pCombo->ActivateItemByRow(dat);
}
break;
case TYPE_LOCALIZEDSTRING:
{
panelItem.m_EditButton->SetText(datstring);
}
break;
default:
{
wchar_t unicode[512];
localize()->ConvertANSIToUnicode(datstring, unicode, sizeof(unicode));
panelItem.m_EditPanel->SetText(unicode);
}
break;
}
}
//-----------------------------------------------------------------------------
// Purpose: Called when one of the buttons is pressed
//-----------------------------------------------------------------------------
void BuildModeDialog::OnCommand(const char *command)
{
if (!stricmp(command, "Save"))
{
// apply the current data and save it to disk
ApplyDataToControls();
if (m_pBuildGroup->SaveControlSettings())
{
// disable save button until another change has been made
m_pSaveButton->SetEnabled(false);
}
}
else if (!stricmp(command, "Exit"))
{
// exit build mode
ExitBuildMode();
}
else if (!stricmp(command, "Apply"))
{
// apply data to controls
ApplyDataToControls();
}
else if (!stricmp(command, "DeletePanel"))
{
OnDeletePanel();
}
else if (!stricmp(command, "RevertToSaved"))
{
RevertToSaved();
}
else if (!stricmp(command, "ShowHelp"))
{
ShowHelp();
}
else
{
BaseClass::OnCommand(command);
}
}
//-----------------------------------------------------------------------------
// Purpose: Deletes a panel from the buildgroup
//-----------------------------------------------------------------------------
void BuildModeDialog::OnDeletePanel()
{
if (!m_pCurrentPanel->IsBuildModeEditable())
{
return;
}
m_pBuildGroup->RemoveSettings();
SetActiveControl(m_pBuildGroup->GetCurrentPanel());
_undoSettings->deleteThis();
_undoSettings = NULL;
m_pSaveButton->SetEnabled(true);
}
//-----------------------------------------------------------------------------
// Purpose: Applies the current settings to the build controls
//-----------------------------------------------------------------------------
void BuildModeDialog::ApplyDataToControls()
{
// don't apply if the panel is not editable
if ( !m_pCurrentPanel->IsBuildModeEditable())
{
UpdateControlData( m_pCurrentPanel );
return; // return success, since we are behaving as expected.
}
char fieldName[512];
if (m_pPanelList->m_PanelList[0].m_EditPanel)
{
m_pPanelList->m_PanelList[0].m_EditPanel->GetText(fieldName, sizeof(fieldName));
}
else
{
m_pPanelList->m_PanelList[0].m_EditButton->GetText(fieldName, sizeof(fieldName));
}
// check to see if any buildgroup panels have this name
Panel *panel = m_pBuildGroup->FieldNameTaken(fieldName);
if (panel)
{
if (panel != m_pCurrentPanel)// make sure name is taken by some other panel not this one
{
char messageString[255];
Q_snprintf(messageString, sizeof( messageString ), "Fieldname is not unique: %s\nRename it and try again.", fieldName);
MessageBox *errorBox = new MessageBox("Cannot Apply", messageString , false);
errorBox->DoModal();
UpdateControlData(m_pCurrentPanel);
m_pApplyButton->SetEnabled(false);
return;
}
}
// create a section to store settings
// m_pPanelList->m_pResourceData->getSection( m_pCurrentPanel->GetName(), true );
KeyValues *dat = new KeyValues( m_pCurrentPanel->GetName() );
// loop through the textedit filling in settings
for ( int i = 0; i < m_pPanelList->m_PanelList.Size(); i++ )
{
const char *name = m_pPanelList->m_PanelList[i].m_szName;
char buf[512];
if (m_pPanelList->m_PanelList[i].m_EditPanel)
{
m_pPanelList->m_PanelList[i].m_EditPanel->GetText(buf, sizeof(buf));
}
else
{
m_pPanelList->m_PanelList[i].m_EditButton->GetText(buf, sizeof(buf));
}
switch (m_pPanelList->m_PanelList[i].m_iType)
{
case TYPE_CORNER:
case TYPE_AUTORESIZE:
// the integer value is assumed to be the first part of the string for these items
dat->SetInt(name, atoi(buf));
break;
default:
dat->SetString(name, buf);
break;
}
}
// dat is built, hand it back to the control
m_pCurrentPanel->ApplySettings( dat );
if ( m_pBuildGroup->GetContextPanel() )
{
m_pBuildGroup->GetContextPanel()->Repaint();
}
m_pApplyButton->SetEnabled(false);
m_pSaveButton->SetEnabled(true);
}
//-----------------------------------------------------------------------------
// Purpose: Store the settings of the current panel in a KeyValues
//-----------------------------------------------------------------------------
void BuildModeDialog::StoreUndoSettings()
{
// don't save if the planel is not editable
if ( !m_pCurrentPanel->IsBuildModeEditable())
{
if (_undoSettings)
_undoSettings->deleteThis();
_undoSettings = NULL;
return;
}
if (_undoSettings)
{
_undoSettings->deleteThis();
_undoSettings = NULL;
}
_undoSettings = StoreSettings();
}
//-----------------------------------------------------------------------------
// Purpose: Revert to the stored the settings of the current panel in a keyValues
//-----------------------------------------------------------------------------
void BuildModeDialog::DoUndo()
{
if ( _undoSettings )
{
m_pCurrentPanel->ApplySettings( _undoSettings );
UpdateControlData(m_pCurrentPanel);
_undoSettings->deleteThis();
_undoSettings = NULL;
}
m_pSaveButton->SetEnabled(true);
}
//-----------------------------------------------------------------------------
// Purpose: Copy the settings of the current panel into a keyValues
//-----------------------------------------------------------------------------
void BuildModeDialog::DoCopy()
{
if (_copySettings)
{
_copySettings->deleteThis();
_copySettings = NULL;
}
_copySettings = StoreSettings();
Q_strncpy (_copyClassName, m_pCurrentPanel->GetClassName(), sizeof( _copyClassName ) );
}
//-----------------------------------------------------------------------------
// Purpose: Create a new Panel with the _copySettings applied
//-----------------------------------------------------------------------------
void BuildModeDialog::DoPaste()
{
// Make a new control located where you had the mouse
int x, y;
input()->GetCursorPos(x, y);
m_pBuildGroup->GetContextPanel()->ScreenToLocal(x,y);
Panel *newPanel = OnNewControl(_copyClassName, x, y);
if (newPanel)
{
newPanel->ApplySettings(_copySettings);
newPanel->SetPos(x, y);
char name[255];
m_pBuildGroup->GetNewFieldName(name, sizeof(name), newPanel);
newPanel->SetName(name);
}
}
//-----------------------------------------------------------------------------
// Purpose: Store the settings of the current panel in a keyValues
//-----------------------------------------------------------------------------
KeyValues *BuildModeDialog::StoreSettings()
{
KeyValues *storedSettings;
storedSettings = new KeyValues( m_pCurrentPanel->GetName() );
// loop through the textedit filling in settings
for ( int i = 0; i < m_pPanelList->m_PanelList.Size(); i++ )
{
const char *name = m_pPanelList->m_PanelList[i].m_szName;
char buf[512];
if (m_pPanelList->m_PanelList[i].m_EditPanel)
{
m_pPanelList->m_PanelList[i].m_EditPanel->GetText(buf, sizeof(buf));
}
else
{
m_pPanelList->m_PanelList[i].m_EditButton->GetText(buf, sizeof(buf));
}
switch (m_pPanelList->m_PanelList[i].m_iType)
{
case TYPE_CORNER:
case TYPE_AUTORESIZE:
// the integer value is assumed to be the first part of the string for these items
storedSettings->SetInt(name, atoi(buf));
break;
default:
storedSettings->SetString(name, buf);
break;
}
}
return storedSettings;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void BuildModeDialog::OnKeyCodeTyped(KeyCode code)
{
if (code == KEY_ENTER) // if someone hits return apply the changes
{
ApplyDataToControls();
}
else
{
Frame::OnKeyCodeTyped(code);
}
}
//-----------------------------------------------------------------------------
// Purpose: Checks to see if any text has changed
//-----------------------------------------------------------------------------
void BuildModeDialog::OnTextChanged( Panel *panel )
{
if (panel == m_pFileSelectionCombo)
{
// reload file if it's changed
char newFile[512];
m_pFileSelectionCombo->GetText(newFile, sizeof(newFile));
if (stricmp(newFile, m_pBuildGroup->GetResourceName()) != 0)
{
// file has changed, reload
SetActiveControl(NULL);
m_pBuildGroup->ChangeControlSettingsFile(newFile);
}
return;
}
if (panel == m_pAddNewControlCombo)
{
char buf[40];
m_pAddNewControlCombo->GetText(buf, 40);
if (stricmp(buf, "None") != 0)
{
OnNewControl(buf);
// reset box back to None
m_pAddNewControlCombo->ActivateItemByRow( 0 );
}
}
if ( panel == m_pEditableChildren )
{
KeyValues *kv = m_pEditableChildren->GetActiveItemUserData();
if ( kv )
{
EditablePanel *ep = reinterpret_cast< EditablePanel * >( kv->GetPtr( "ptr" ) );
if ( ep )
{
ep->ActivateBuildMode();
}
}
}
if ( panel == m_pEditableParents )
{
KeyValues *kv = m_pEditableParents->GetActiveItemUserData();
if ( kv )
{
EditablePanel *ep = reinterpret_cast< EditablePanel * >( kv->GetPtr( "ptr" ) );
if ( ep )
{
ep->ActivateBuildMode();
}
}
}
if (m_pCurrentPanel && m_pCurrentPanel->IsBuildModeEditable())
{
m_pApplyButton->SetEnabled(true);
}
if (_autoUpdate)
{
ApplyDataToControls();
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void BuildModeDialog::ExitBuildMode( void )
{
// make sure rulers are off
if (m_pBuildGroup->HasRulersOn())
{
m_pBuildGroup->ToggleRulerDisplay();
}
m_pBuildGroup->SetEnabled(false);
}
//-----------------------------------------------------------------------------
// Purpose: Create a new control in the context panel
//-----------------------------------------------------------------------------
Panel *BuildModeDialog::OnNewControl( const char *name, int x, int y)
{
// returns NULL on failure
Panel *newPanel = m_pBuildGroup->NewControl(name, x, y);
if (newPanel)
{
// call mouse commands to simulate selecting the new
// panel. This will set everything up correctly in the buildGroup.
m_pBuildGroup->MousePressed(MOUSE_LEFT, newPanel);
m_pBuildGroup->MouseReleased(MOUSE_LEFT, newPanel);
}
m_pSaveButton->SetEnabled(true);
return newPanel;
}
//-----------------------------------------------------------------------------
// Purpose: enable the save button, useful when buildgroup needs to Activate it.
//-----------------------------------------------------------------------------
void BuildModeDialog::EnableSaveButton()
{
m_pSaveButton->SetEnabled(true);
}
//-----------------------------------------------------------------------------
// Purpose: Revert to the saved settings in the .res file
//-----------------------------------------------------------------------------
void BuildModeDialog::RevertToSaved()
{
// hide the dialog as reloading will destroy it
surface()->SetPanelVisible(this->GetVPanel(), false);
m_pBuildGroup->ReloadControlSettings();
}
//-----------------------------------------------------------------------------
// Purpose: Display some information about the editor
//-----------------------------------------------------------------------------
void BuildModeDialog::ShowHelp()
{
char helpText[]= "In the Build Mode Dialog Window:\n"
"Delete button - deletes the currently selected panel if it is deletable.\n"
"Apply button - applies changes to the Context Panel.\n"
"Save button - saves all settings to file. \n"
"Revert to saved- reloads the last saved file.\n"
"Auto Update - any changes apply instantly.\n"
"Typing Enter in any text field applies changes.\n"
"New Control menu - creates a new panel in the upper left corner.\n\n"
"In the Context Panel:\n"
"After selecting and moving a panel Ctrl-z will undo the move.\n"
"Shift clicking panels allows multiple panels to be selected into a group.\n"
"Ctrl-c copies the settings of the last selected panel.\n"
"Ctrl-v creates a new panel with the copied settings at the location of the mouse pointer.\n"
"Arrow keys slowly move panels, holding shift + arrow will slowly resize it.\n"
"Holding right mouse button down opens a dropdown panel creation menu.\n"
" Panel will be created where the menu was opened.\n"
"Delete key deletes the currently selected panel if it is deletable.\n"
" Does nothing to multiple selections.";
MessageBox *helpDlg = new MessageBox ("Build Mode Help", helpText, this);
helpDlg->AddActionSignalTarget(this);
helpDlg->DoModal();
}
void BuildModeDialog::ShutdownBuildMode()
{
m_pBuildGroup->SetEnabled(false);
}
void BuildModeDialog::OnPanelMoved()
{
m_pApplyButton->SetEnabled(true);
}
//-----------------------------------------------------------------------------
// Purpose: message handles thats sets the text in the clipboard
//-----------------------------------------------------------------------------
void BuildModeDialog::OnSetClipboardText(const char *text)
{
system()->SetClipboardText(text, strlen(text));
}
void BuildModeDialog::OnCreateNewControl( char const *text )
{
if ( !Q_stricmp( text, "None" ) )
return;
OnNewControl( text, m_nClick[ 0 ], m_nClick[ 1 ] );
}
void BuildModeDialog::OnShowNewControlMenu()
{
if ( !m_pBuildGroup )
return;
int i;
input()->GetCursorPos( m_nClick[ 0 ], m_nClick[ 1 ] );
m_pBuildGroup->GetContextPanel()->ScreenToLocal( m_nClick[ 0 ], m_nClick[ 1 ] );
if ( m_hContextMenu )
delete m_hContextMenu.Get();
m_hContextMenu = new Menu( this, "NewControls" );
// Show popup menu
m_hContextMenu->AddMenuItem( "None", "None", new KeyValues( "CreateNewControl", "text", "None" ), this );
CUtlVector< char const * > names;
CBuildFactoryHelper::GetFactoryNames( names );
// Sort the names
CUtlRBTree< char const *, int > sorted( 0, 0, StringLessThan );
for ( i = 0; i < names.Count(); ++i )
{
sorted.Insert( names[ i ] );
}
for ( i = sorted.FirstInorder(); i != sorted.InvalidIndex(); i = sorted.NextInorder( i ) )
{
m_hContextMenu->AddMenuItem( sorted[ i ], sorted[ i ], new KeyValues( "CreateNewControl", "text", sorted[ i ] ), this );
}
Menu::PlaceContextMenu( this, m_hContextMenu );
}
void BuildModeDialog::OnReloadLocalization()
{
// reload localization files
localize()->ReloadLocalizationFiles( );
}
bool BuildModeDialog::IsBuildGroupEnabled()
{
// Don't ever edit the actual build dialog!!!
return false;
}
void BuildModeDialog::OnChangeChild( int direction )
{
Assert( direction == 1 || direction == -1 );
if ( !m_pBuildGroup )
return;
Panel *current = m_pCurrentPanel;
Panel *context = m_pBuildGroup->GetContextPanel();
if ( !current || current == context )
{
current = NULL;
if ( context->GetChildCount() > 0 )
{
current = context->GetChild( 0 );
}
}
else
{
int i;
// Move in direction requested
int children = context->GetChildCount();
for ( i = 0; i < children; ++i )
{
Panel *child = context->GetChild( i );
if ( child == current )
{
break;
}
}
if ( i < children )
{
for ( int offset = 1; offset < children; ++offset )
{
int test = ( i + ( direction * offset ) ) % children;
if ( test < 0 )
test += children;
if ( test == i )
continue;
Panel *check = context->GetChild( test );
BuildModeDialog *bm = dynamic_cast< BuildModeDialog * >( check );
if ( bm )
continue;
current = check;
break;
}
}
}
if ( !current )
{
return;
}
SetActiveControl( current );
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yoyojacky/CSMoE.git
git@gitee.com:yoyojacky/CSMoE.git
yoyojacky
CSMoE
CSMoE
master

搜索帮助