doom3-bfg/neo/ui/RegExp.cpp

466 lines
9.2 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "precompiled.h"
2012-11-26 18:58:24 +00:00
#include "RegExp.h"
#include "DeviceContext.h"
#include "Window.h"
#include "UserInterfaceLocal.h"
int idRegister::REGCOUNT[NUMTYPES] = {4, 1, 1, 1, 0, 2, 3, 4};
/*
====================
idRegister::SetToRegs
====================
*/
void idRegister::SetToRegs( float* registers )
{
2012-11-26 18:58:24 +00:00
int i;
idVec4 v;
idVec2 v2;
idVec3 v3;
idRectangle rect;
if( !enabled || var == NULL || ( var && ( var->GetDict() || !var->GetEval() ) ) )
{
2012-11-26 18:58:24 +00:00
return;
}
switch( type )
{
case VEC4:
{
v = *static_cast<idWinVec4*>( var );
2012-11-26 18:58:24 +00:00
break;
}
case RECTANGLE:
{
rect = *static_cast<idWinRectangle*>( var );
2012-11-26 18:58:24 +00:00
v = rect.ToVec4();
break;
}
case VEC2:
{
v2 = *static_cast<idWinVec2*>( var );
2012-11-26 18:58:24 +00:00
v[0] = v2[0];
v[1] = v2[1];
break;
}
case VEC3:
{
v3 = *static_cast<idWinVec3*>( var );
2012-11-26 18:58:24 +00:00
v[0] = v3[0];
v[1] = v3[1];
v[2] = v3[2];
break;
}
case FLOAT:
{
v[0] = *static_cast<idWinFloat*>( var );
2012-11-26 18:58:24 +00:00
break;
}
case INT:
{
v[0] = *static_cast<idWinInt*>( var );
2012-11-26 18:58:24 +00:00
break;
}
case BOOL:
{
v[0] = *static_cast<idWinBool*>( var );
2012-11-26 18:58:24 +00:00
break;
}
default:
{
2012-11-26 18:58:24 +00:00
common->FatalError( "idRegister::SetToRegs: bad reg type" );
break;
}
}
for( i = 0; i < regCount; i++ )
{
2012-11-26 18:58:24 +00:00
registers[ regs[ i ] ] = v[i];
}
}
/*
=================
idRegister::GetFromRegs
=================
*/
void idRegister::GetFromRegs( float* registers )
{
2012-11-26 18:58:24 +00:00
idVec4 v;
idRectangle rect;
if( !enabled || var == NULL || ( var && ( var->GetDict() || !var->GetEval() ) ) )
{
2012-11-26 18:58:24 +00:00
return;
}
for( int i = 0; i < regCount; i++ )
{
2012-11-26 18:58:24 +00:00
v[i] = registers[regs[i]];
}
switch( type )
{
case VEC4:
{
*dynamic_cast<idWinVec4*>( var ) = v;
2012-11-26 18:58:24 +00:00
break;
}
case RECTANGLE:
{
2012-11-26 18:58:24 +00:00
rect.x = v.x;
rect.y = v.y;
rect.w = v.z;
rect.h = v.w;
*static_cast<idWinRectangle*>( var ) = rect;
2012-11-26 18:58:24 +00:00
break;
}
case VEC2:
{
*static_cast<idWinVec2*>( var ) = v.ToVec2();
2012-11-26 18:58:24 +00:00
break;
}
case VEC3:
{
*static_cast<idWinVec3*>( var ) = v.ToVec3();
2012-11-26 18:58:24 +00:00
break;
}
case FLOAT:
{
*static_cast<idWinFloat*>( var ) = v[0];
2012-11-26 18:58:24 +00:00
break;
}
case INT:
{
*static_cast<idWinInt*>( var ) = v[0];
2012-11-26 18:58:24 +00:00
break;
}
case BOOL:
{
*static_cast<idWinBool*>( var ) = ( v[0] != 0.0f );
2012-11-26 18:58:24 +00:00
break;
}
default:
{
2012-11-26 18:58:24 +00:00
common->FatalError( "idRegister::GetFromRegs: bad reg type" );
break;
}
}
}
/*
=================
idRegister::ReadFromDemoFile
=================
*/
void idRegister::ReadFromDemoFile( idDemoFile* f )
{
2012-11-26 18:58:24 +00:00
f->ReadBool( enabled );
f->ReadShort( type );
f->ReadInt( regCount );
for( int i = 0; i < 4; i++ )
2012-11-26 18:58:24 +00:00
f->ReadUnsignedShort( regs[i] );
name = f->ReadHashString();
}
/*
=================
idRegister::WriteToDemoFile
=================
*/
void idRegister::WriteToDemoFile( idDemoFile* f )
{
2012-11-26 18:58:24 +00:00
f->WriteBool( enabled );
f->WriteShort( type );
f->WriteInt( regCount );
for( int i = 0; i < 4; i++ )
2012-11-26 18:58:24 +00:00
f->WriteUnsignedShort( regs[i] );
f->WriteHashString( name );
}
/*
=================
idRegister::WriteToSaveGame
=================
*/
void idRegister::WriteToSaveGame( idFile* savefile )
{
2012-11-26 18:58:24 +00:00
int len;
2012-11-26 18:58:24 +00:00
savefile->Write( &enabled, sizeof( enabled ) );
savefile->Write( &type, sizeof( type ) );
savefile->Write( &regCount, sizeof( regCount ) );
savefile->Write( &regs[0], sizeof( regs ) );
len = name.Length();
savefile->Write( &len, sizeof( len ) );
savefile->Write( name.c_str(), len );
2012-11-26 18:58:24 +00:00
var->WriteToSaveGame( savefile );
}
/*
================
idRegister::ReadFromSaveGame
================
*/
void idRegister::ReadFromSaveGame( idFile* savefile )
{
2012-11-26 18:58:24 +00:00
int len;
2012-11-26 18:58:24 +00:00
savefile->Read( &enabled, sizeof( enabled ) );
savefile->Read( &type, sizeof( type ) );
savefile->Read( &regCount, sizeof( regCount ) );
savefile->Read( &regs[0], sizeof( regs ) );
2012-11-26 18:58:24 +00:00
savefile->Read( &len, sizeof( len ) );
name.Fill( ' ', len );
savefile->Read( &name[0], len );
2012-11-26 18:58:24 +00:00
var->ReadFromSaveGame( savefile );
}
/*
====================
idRegisterList::AddReg
====================
*/
void idRegisterList::AddReg( const char* name, int type, idVec4 data, idWindow* win, idWinVar* var )
{
if( FindReg( name ) == NULL )
{
2012-11-26 18:58:24 +00:00
assert( type >= 0 && type < idRegister::NUMTYPES );
int numRegs = idRegister::REGCOUNT[type];
idRegister* reg = new( TAG_OLD_UI ) idRegister( name, type );
2012-11-26 18:58:24 +00:00
reg->var = var;
for( int i = 0; i < numRegs; i++ )
{
reg->regs[i] = win->ExpressionConstant( data[i] );
2012-11-26 18:58:24 +00:00
}
int hash = regHash.GenerateKey( name, false );
regHash.Add( hash, regs.Append( reg ) );
}
}
/*
====================
idRegisterList::AddReg
====================
*/
void idRegisterList::AddReg( const char* name, int type, idTokenParser* src, idWindow* win, idWinVar* var )
{
2012-11-26 18:58:24 +00:00
idRegister* reg;
2012-11-26 18:58:24 +00:00
reg = FindReg( name );
if( reg == NULL )
{
assert( type >= 0 && type < idRegister::NUMTYPES );
2012-11-26 18:58:24 +00:00
int numRegs = idRegister::REGCOUNT[type];
reg = new( TAG_OLD_UI ) idRegister( name, type );
2012-11-26 18:58:24 +00:00
reg->var = var;
if( type == idRegister::STRING )
{
2012-11-26 18:58:24 +00:00
idToken tok;
if( src->ReadToken( &tok ) )
{
2012-11-26 18:58:24 +00:00
tok = idLocalization::GetString( tok );
var->Init( tok, win );
}
}
else
{
for( int i = 0; i < numRegs; i++ )
{
reg->regs[i] = win->ParseExpression( src, NULL );
if( i < numRegs - 1 )
{
src->ExpectTokenString( "," );
2012-11-26 18:58:24 +00:00
}
}
}
int hash = regHash.GenerateKey( name, false );
regHash.Add( hash, regs.Append( reg ) );
}
else
{
2012-11-26 18:58:24 +00:00
int numRegs = idRegister::REGCOUNT[type];
reg->var = var;
if( type == idRegister::STRING )
{
2012-11-26 18:58:24 +00:00
idToken tok;
if( src->ReadToken( &tok ) )
{
2012-11-26 18:58:24 +00:00
var->Init( tok, win );
}
}
else
{
for( int i = 0; i < numRegs; i++ )
{
2012-11-26 18:58:24 +00:00
reg->regs[i] = win->ParseExpression( src, NULL );
if( i < numRegs - 1 )
{
src->ExpectTokenString( "," );
2012-11-26 18:58:24 +00:00
}
}
}
}
}
/*
====================
idRegisterList::GetFromRegs
====================
*/
void idRegisterList::GetFromRegs( float* registers )
{
for( int i = 0; i < regs.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
regs[i]->GetFromRegs( registers );
}
}
/*
====================
idRegisterList::SetToRegs
====================
*/
void idRegisterList::SetToRegs( float* registers )
{
2012-11-26 18:58:24 +00:00
int i;
for( i = 0; i < regs.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
regs[i]->SetToRegs( registers );
}
}
/*
====================
idRegisterList::FindReg
====================
*/
idRegister* idRegisterList::FindReg( const char* name )
{
2012-11-26 18:58:24 +00:00
int hash = regHash.GenerateKey( name, false );
for( int i = regHash.First( hash ); i != -1; i = regHash.Next( i ) )
{
if( regs[i]->name.Icmp( name ) == 0 )
{
2012-11-26 18:58:24 +00:00
return regs[i];
}
}
return NULL;
}
/*
====================
idRegisterList::Reset
====================
*/
void idRegisterList::Reset()
{
2012-11-26 18:58:24 +00:00
regs.DeleteContents( true );
regHash.Clear();
}
/*
====================
idRegisterList::ReadFromSaveGame
====================
*/
void idRegisterList::ReadFromDemoFile( idDemoFile* f )
{
2012-11-26 18:58:24 +00:00
int c;
2012-11-26 18:58:24 +00:00
f->ReadInt( c );
regs.DeleteContents( true );
for( int i = 0; i < c; i++ )
{
idRegister* reg = new( TAG_OLD_UI ) idRegister;
2012-11-26 18:58:24 +00:00
reg->ReadFromDemoFile( f );
regs.Append( reg );
}
}
/*
====================
idRegisterList::ReadFromSaveGame
====================
*/
void idRegisterList::WriteToDemoFile( idDemoFile* f )
{
2012-11-26 18:58:24 +00:00
int c = regs.Num();
2012-11-26 18:58:24 +00:00
f->WriteInt( c );
for( int i = 0 ; i < c; i++ )
{
regs[i]->WriteToDemoFile( f );
2012-11-26 18:58:24 +00:00
}
}
/*
=====================
idRegisterList::WriteToSaveGame
=====================
*/
void idRegisterList::WriteToSaveGame( idFile* savefile )
{
2012-11-26 18:58:24 +00:00
int i, num;
2012-11-26 18:58:24 +00:00
num = regs.Num();
savefile->Write( &num, sizeof( num ) );
for( i = 0; i < num; i++ )
{
2012-11-26 18:58:24 +00:00
regs[i]->WriteToSaveGame( savefile );
}
}
/*
====================
idRegisterList::ReadFromSaveGame
====================
*/
void idRegisterList::ReadFromSaveGame( idFile* savefile )
{
2012-11-26 18:58:24 +00:00
int i, num;
2012-11-26 18:58:24 +00:00
savefile->Read( &num, sizeof( num ) );
for( i = 0; i < num; i++ )
{
2012-11-26 18:58:24 +00:00
regs[i]->ReadFromSaveGame( savefile );
}
}