doom3-bfg/neo/ui/EditWindow.cpp

796 lines
15 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 "../idlib/precompiled.h"
#include "DeviceContext.h"
#include "Window.h"
#include "UserInterfaceLocal.h"
#include "SliderWindow.h"
#include "EditWindow.h"
bool idEditWindow::ParseInternalVar( const char* _name, idTokenParser* src )
{
if( idStr::Icmp( _name, "maxchars" ) == 0 )
{
2012-11-26 18:58:24 +00:00
maxChars = src->ParseInt();
return true;
}
if( idStr::Icmp( _name, "numeric" ) == 0 )
{
2012-11-26 18:58:24 +00:00
numeric = src->ParseBool();
return true;
}
if( idStr::Icmp( _name, "wrap" ) == 0 )
{
2012-11-26 18:58:24 +00:00
wrap = src->ParseBool();
return true;
}
if( idStr::Icmp( _name, "readonly" ) == 0 )
{
2012-11-26 18:58:24 +00:00
readonly = src->ParseBool();
return true;
}
if( idStr::Icmp( _name, "forceScroll" ) == 0 )
{
2012-11-26 18:58:24 +00:00
forceScroll = src->ParseBool();
return true;
}
if( idStr::Icmp( _name, "source" ) == 0 )
{
2012-11-26 18:58:24 +00:00
ParseString( src, sourceFile );
return true;
}
if( idStr::Icmp( _name, "password" ) == 0 )
{
2012-11-26 18:58:24 +00:00
password = src->ParseBool();
return true;
}
if( idStr::Icmp( _name, "cvarMax" ) == 0 )
{
2012-11-26 18:58:24 +00:00
cvarMax = src->ParseInt();
return true;
}
2012-11-26 18:58:24 +00:00
return idWindow::ParseInternalVar( _name, src );
}
idWinVar* idEditWindow::GetWinVarByName( const char* _name, bool fixup, drawWin_t** owner )
{
if( idStr::Icmp( _name, "cvar" ) == 0 )
{
2012-11-26 18:58:24 +00:00
return &cvarStr;
}
if( idStr::Icmp( _name, "password" ) == 0 )
{
2012-11-26 18:58:24 +00:00
return &password;
}
if( idStr::Icmp( _name, "liveUpdate" ) == 0 )
{
2012-11-26 18:58:24 +00:00
return &liveUpdate;
}
if( idStr::Icmp( _name, "cvarGroup" ) == 0 )
{
2012-11-26 18:58:24 +00:00
return &cvarGroup;
}
return idWindow::GetWinVarByName( _name, fixup, owner );
}
void idEditWindow::CommonInit()
{
2012-11-26 18:58:24 +00:00
maxChars = 128;
numeric = false;
paintOffset = 0;
cursorPos = 0;
cursorLine = 0;
cvarMax = 0;
wrap = false;
sourceFile = "";
scroller = NULL;
sizeBias = 0;
lastTextLength = 0;
forceScroll = false;
password = NULL;
cvar = NULL;
liveUpdate = true;
readonly = false;
scroller = new( TAG_OLD_UI ) idSliderWindow( gui );
2012-11-26 18:58:24 +00:00
}
idEditWindow::idEditWindow( idUserInterfaceLocal* g ) : idWindow( g )
{
2012-11-26 18:58:24 +00:00
gui = g;
CommonInit();
}
idEditWindow::~idEditWindow()
{
2012-11-26 18:58:24 +00:00
}
void idEditWindow::GainFocus()
{
2012-11-26 18:58:24 +00:00
cursorPos = text.Length();
EnsureCursorVisible();
}
void idEditWindow::Draw( int time, float x, float y )
{
2012-11-26 18:58:24 +00:00
idVec4 color = foreColor;
2012-11-26 18:58:24 +00:00
UpdateCvar( true );
2012-11-26 18:58:24 +00:00
int len = text.Length();
if( len != lastTextLength )
{
2012-11-26 18:58:24 +00:00
scroller->SetValue( 0.0f );
EnsureCursorVisible();
lastTextLength = len;
}
float scale = textScale;
2012-11-26 18:58:24 +00:00
idStr pass;
const char* buffer;
if( password )
{
2012-11-26 18:58:24 +00:00
const char* temp = text;
for( ; *temp; temp++ )
{
2012-11-26 18:58:24 +00:00
pass += "*";
}
buffer = pass;
}
else
{
2012-11-26 18:58:24 +00:00
buffer = text;
}
if( cursorPos > len )
{
2012-11-26 18:58:24 +00:00
cursorPos = len;
}
2012-11-26 18:58:24 +00:00
idRectangle rect = textRect;
2012-11-26 18:58:24 +00:00
rect.x -= paintOffset;
rect.w += paintOffset;
if( wrap && scroller->GetHigh() > 0.0f )
{
2012-11-26 18:58:24 +00:00
float lineHeight = GetMaxCharHeight( ) + 5;
rect.y -= scroller->GetValue() * lineHeight;
rect.w -= sizeBias;
rect.h = ( breaks.Num() + 1 ) * lineHeight;
}
if( hover && !noEvents && Contains( gui->CursorX(), gui->CursorY() ) )
{
2012-11-26 18:58:24 +00:00
color = hoverColor;
}
else
{
2012-11-26 18:58:24 +00:00
hover = false;
}
if( flags & WIN_FOCUS )
{
2012-11-26 18:58:24 +00:00
color = hoverColor;
}
dc->DrawText( buffer, scale, 0, color, rect, wrap, ( flags & WIN_FOCUS ) ? cursorPos : -1 );
2012-11-26 18:58:24 +00:00
}
/*
=============
idEditWindow::HandleEvent
=============
*/
const char* idEditWindow::HandleEvent( const sysEvent_t* event, bool* updateVisuals )
{
2012-11-26 18:58:24 +00:00
static char buffer[ MAX_EDITFIELD ];
if( wrap )
{
2012-11-26 18:58:24 +00:00
// need to call this to allow proper focus and capturing on embedded children
const char* ret = idWindow::HandleEvent( event, updateVisuals );
if( ret != NULL && *ret != NULL )
{
2012-11-26 18:58:24 +00:00
return ret;
}
}
if( ( event->evType != SE_CHAR && event->evType != SE_KEY ) )
{
2012-11-26 18:58:24 +00:00
return "";
}
2012-11-26 18:58:24 +00:00
idStr::Copynz( buffer, text.c_str(), sizeof( buffer ) );
int key = event->evValue;
int len = text.Length();
if( event->evType == SE_CHAR )
{
if( key == '`' )
{
2012-11-26 18:58:24 +00:00
return "";
}
if( updateVisuals )
{
2012-11-26 18:58:24 +00:00
*updateVisuals = true;
}
if( maxChars && len > maxChars )
{
2012-11-26 18:58:24 +00:00
len = maxChars;
}
if( readonly )
{
2012-11-26 18:58:24 +00:00
return "";
}
//
// ignore any non printable chars (except enter when wrap is enabled)
//
if( wrap && ( key == K_ENTER || key == K_KP_ENTER ) )
{
}
else if( !idStr::CharIsPrintable( key ) )
{
2012-11-26 18:58:24 +00:00
return "";
}
if( numeric )
{
if( ( key < '0' || key > '9' ) && key != '.' )
{
return "";
2012-11-26 18:58:24 +00:00
}
}
if( dc->GetOverStrike() )
{
if( maxChars && cursorPos >= maxChars )
{
return "";
2012-11-26 18:58:24 +00:00
}
}
else
{
if( ( len == MAX_EDITFIELD - 1 ) || ( maxChars && len >= maxChars ) )
{
return "";
2012-11-26 18:58:24 +00:00
}
memmove( &buffer[ cursorPos + 1 ], &buffer[ cursorPos ], len + 1 - cursorPos );
}
2012-11-26 18:58:24 +00:00
buffer[ cursorPos ] = key;
2012-11-26 18:58:24 +00:00
text = buffer;
UpdateCvar( false );
RunScript( ON_ACTION );
if( cursorPos < len + 1 )
{
2012-11-26 18:58:24 +00:00
cursorPos++;
}
EnsureCursorVisible();
}
else if( event->evType == SE_KEY && event->evValue2 )
{
if( updateVisuals )
{
2012-11-26 18:58:24 +00:00
*updateVisuals = true;
}
if( key == K_DEL )
{
if( readonly )
{
2012-11-26 18:58:24 +00:00
return "";
}
if( cursorPos < len )
{
memmove( &buffer[cursorPos], &buffer[cursorPos + 1], len - cursorPos );
2012-11-26 18:58:24 +00:00
text = buffer;
UpdateCvar( false );
RunScript( ON_ACTION );
}
return "";
}
if( key == K_BACKSPACE ) // ctrl-h is backspace
{
if( readonly )
{
2012-11-26 18:58:24 +00:00
return "";
}
if( cursorPos > 0 )
{
if( cursorPos >= len )
{
2012-11-26 18:58:24 +00:00
buffer[len - 1] = 0;
cursorPos = len - 1;
}
else
{
memmove( &buffer[ cursorPos - 1 ], &buffer[ cursorPos ], len + 1 - cursorPos );
2012-11-26 18:58:24 +00:00
cursorPos--;
}
2012-11-26 18:58:24 +00:00
text = buffer;
UpdateCvar( false );
RunScript( ON_ACTION );
}
2012-11-26 18:58:24 +00:00
return "";
}
if( key == K_RIGHTARROW )
{
if( cursorPos < len )
{
if( ( idKeyInput::IsDown( K_LCTRL ) || idKeyInput::IsDown( K_RCTRL ) ) )
{
2012-11-26 18:58:24 +00:00
// skip to next word
while( ( cursorPos < len ) && ( buffer[ cursorPos ] != ' ' ) )
{
2012-11-26 18:58:24 +00:00
cursorPos++;
}
while( ( cursorPos < len ) && ( buffer[ cursorPos ] == ' ' ) )
{
2012-11-26 18:58:24 +00:00
cursorPos++;
}
}
else
{
if( cursorPos < len )
{
2012-11-26 18:58:24 +00:00
cursorPos++;
}
}
}
2012-11-26 18:58:24 +00:00
EnsureCursorVisible();
2012-11-26 18:58:24 +00:00
return "";
}
if( key == K_LEFTARROW )
{
if( ( idKeyInput::IsDown( K_LCTRL ) || idKeyInput::IsDown( K_RCTRL ) ) )
{
2012-11-26 18:58:24 +00:00
// skip to previous word
while( ( cursorPos > 0 ) && ( buffer[ cursorPos - 1 ] == ' ' ) )
{
2012-11-26 18:58:24 +00:00
cursorPos--;
}
while( ( cursorPos > 0 ) && ( buffer[ cursorPos - 1 ] != ' ' ) )
{
2012-11-26 18:58:24 +00:00
cursorPos--;
}
}
else
{
if( cursorPos > 0 )
{
2012-11-26 18:58:24 +00:00
cursorPos--;
}
}
2012-11-26 18:58:24 +00:00
EnsureCursorVisible();
2012-11-26 18:58:24 +00:00
return "";
}
if( key == K_HOME )
{
if( ( idKeyInput::IsDown( K_LCTRL ) || idKeyInput::IsDown( K_RCTRL ) ) || cursorLine <= 0 || ( cursorLine >= breaks.Num() ) )
{
cursorPos = 0;
}
else
{
2012-11-26 18:58:24 +00:00
cursorPos = breaks[cursorLine];
}
EnsureCursorVisible();
return "";
}
if( key == K_END )
{
if( ( idKeyInput::IsDown( K_LCTRL ) || idKeyInput::IsDown( K_RCTRL ) ) || ( cursorLine < -1 ) || ( cursorLine >= breaks.Num() - 1 ) )
{
2012-11-26 18:58:24 +00:00
cursorPos = len;
}
else
{
2012-11-26 18:58:24 +00:00
cursorPos = breaks[cursorLine + 1] - 1;
}
EnsureCursorVisible();
return "";
}
if( key == K_INS )
{
if( !readonly )
{
2012-11-26 18:58:24 +00:00
dc->SetOverStrike( !dc->GetOverStrike() );
}
return "";
}
if( key == K_DOWNARROW )
{
if( ( idKeyInput::IsDown( K_LCTRL ) || idKeyInput::IsDown( K_RCTRL ) ) )
{
2012-11-26 18:58:24 +00:00
scroller->SetValue( scroller->GetValue() + 1.0f );
}
else
{
if( cursorLine < breaks.Num() - 1 )
{
2012-11-26 18:58:24 +00:00
int offset = cursorPos - breaks[cursorLine];
cursorPos = breaks[cursorLine + 1] + offset;
EnsureCursorVisible();
}
}
}
if( key == K_UPARROW )
{
if( ( idKeyInput::IsDown( K_LCTRL ) || idKeyInput::IsDown( K_RCTRL ) ) )
{
2012-11-26 18:58:24 +00:00
scroller->SetValue( scroller->GetValue() - 1.0f );
}
else
{
if( cursorLine > 0 )
{
2012-11-26 18:58:24 +00:00
int offset = cursorPos - breaks[cursorLine];
cursorPos = breaks[cursorLine - 1] + offset;
EnsureCursorVisible();
}
}
}
if( key == K_ENTER || key == K_KP_ENTER )
{
2012-11-26 18:58:24 +00:00
RunScript( ON_ACTION );
RunScript( ON_ENTER );
return cmd;
}
if( key == K_ESCAPE )
{
2012-11-26 18:58:24 +00:00
RunScript( ON_ESC );
return cmd;
}
}
else if( event->evType == SE_KEY && !event->evValue2 )
{
if( key == K_ENTER || key == K_KP_ENTER )
{
2012-11-26 18:58:24 +00:00
RunScript( ON_ENTERRELEASE );
return cmd;
}
else
{
2012-11-26 18:58:24 +00:00
RunScript( ON_ACTIONRELEASE );
}
}
2012-11-26 18:58:24 +00:00
return "";
}
void idEditWindow::PostParse()
{
2012-11-26 18:58:24 +00:00
idWindow::PostParse();
if( maxChars == 0 )
{
2012-11-26 18:58:24 +00:00
maxChars = 10;
}
if( sourceFile.Length() )
{
void* buffer;
2012-11-26 18:58:24 +00:00
fileSystem->ReadFile( sourceFile, &buffer );
text = ( char* ) buffer;
2012-11-26 18:58:24 +00:00
fileSystem->FreeFile( buffer );
}
2012-11-26 18:58:24 +00:00
InitCvar();
InitScroller( false );
2012-11-26 18:58:24 +00:00
EnsureCursorVisible();
2012-11-26 18:58:24 +00:00
flags |= WIN_CANFOCUS;
}
/*
================
idEditWindow::InitScroller
This is the same as in idListWindow
================
*/
void idEditWindow::InitScroller( bool horizontal )
{
const char* thumbImage = "guis/assets/scrollbar_thumb.tga";
const char* barImage = "guis/assets/scrollbarv.tga";
const char* scrollerName = "_scrollerWinV";
if( horizontal )
{
2012-11-26 18:58:24 +00:00
barImage = "guis/assets/scrollbarh.tga";
scrollerName = "_scrollerWinH";
}
const idMaterial* mat = declManager->FindMaterial( barImage );
2012-11-26 18:58:24 +00:00
mat->SetSort( SS_GUI );
sizeBias = mat->GetImageWidth();
2012-11-26 18:58:24 +00:00
idRectangle scrollRect;
if( horizontal )
{
2012-11-26 18:58:24 +00:00
sizeBias = mat->GetImageHeight();
scrollRect.x = 0;
scrollRect.y = ( clientRect.h - sizeBias );
2012-11-26 18:58:24 +00:00
scrollRect.w = clientRect.w;
scrollRect.h = sizeBias;
}
else
{
scrollRect.x = ( clientRect.w - sizeBias );
2012-11-26 18:58:24 +00:00
scrollRect.y = 0;
scrollRect.w = sizeBias;
scrollRect.h = clientRect.h;
}
scroller->InitWithDefaults( scrollerName, scrollRect, foreColor, matColor, mat->GetName(), thumbImage, !horizontal, true );
InsertChild( scroller, NULL );
scroller->SetBuddy( this );
2012-11-26 18:58:24 +00:00
}
void idEditWindow::HandleBuddyUpdate( idWindow* buddy )
{
2012-11-26 18:58:24 +00:00
}
void idEditWindow::EnsureCursorVisible()
{
if( readonly )
{
2012-11-26 18:58:24 +00:00
cursorPos = -1;
}
else if( maxChars == 1 )
{
2012-11-26 18:58:24 +00:00
cursorPos = 0;
}
if( !dc )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
SetFont();
if( !wrap )
{
2012-11-26 18:58:24 +00:00
int cursorX = 0;
if( password )
{
2012-11-26 18:58:24 +00:00
cursorX = cursorPos * dc->CharWidth( '*', textScale );
}
else
{
2012-11-26 18:58:24 +00:00
int i = 0;
while( i < text.Length() && i < cursorPos )
{
if( idStr::IsColor( &text[i] ) )
{
2012-11-26 18:58:24 +00:00
i += 2;
}
else
{
2012-11-26 18:58:24 +00:00
cursorX += dc->CharWidth( text[i], textScale );
i++;
}
}
}
int maxWidth = GetMaxCharWidth( );
int left = cursorX - maxWidth;
int right = ( cursorX - textRect.w ) + maxWidth;
if( paintOffset > left )
{
2012-11-26 18:58:24 +00:00
// When we go past the left side, we want the text to jump 6 characters
paintOffset = left - maxWidth * 6;
}
if( paintOffset < right )
{
2012-11-26 18:58:24 +00:00
paintOffset = right;
}
if( paintOffset < 0 )
{
2012-11-26 18:58:24 +00:00
paintOffset = 0;
}
scroller->SetRange( 0.0f, 0.0f, 1.0f );
}
else
{
2012-11-26 18:58:24 +00:00
// Word wrap
2012-11-26 18:58:24 +00:00
breaks.Clear();
idRectangle rect = textRect;
rect.w -= sizeBias;
dc->DrawText( text, textScale, textAlign, colorWhite, rect, true, ( flags & WIN_FOCUS ) ? cursorPos : -1, true, &breaks );
int fit = textRect.h / ( GetMaxCharHeight() + 5 );
if( fit < breaks.Num() + 1 )
{
scroller->SetRange( 0, breaks.Num() + 1 - fit, 1 );
}
else
{
2012-11-26 18:58:24 +00:00
// The text fits completely in the box
scroller->SetRange( 0.0f, 0.0f, 1.0f );
2012-11-26 18:58:24 +00:00
}
if( forceScroll )
{
2012-11-26 18:58:24 +00:00
scroller->SetValue( breaks.Num() - fit );
}
else if( readonly )
{
}
else
{
2012-11-26 18:58:24 +00:00
cursorLine = 0;
for( int i = 1; i < breaks.Num(); i++ )
{
if( cursorPos >= breaks[i] )
{
2012-11-26 18:58:24 +00:00
cursorLine = i;
}
else
{
2012-11-26 18:58:24 +00:00
break;
}
}
int topLine = idMath::Ftoi( scroller->GetValue() );
if( cursorLine < topLine )
{
2012-11-26 18:58:24 +00:00
scroller->SetValue( cursorLine );
}
else if( cursorLine >= topLine + fit )
{
2012-11-26 18:58:24 +00:00
scroller->SetValue( ( cursorLine - fit ) + 1 );
}
}
}
}
void idEditWindow::Activate( bool activate, idStr& act )
{
idWindow::Activate( activate, act );
if( activate )
{
2012-11-26 18:58:24 +00:00
UpdateCvar( true, true );
EnsureCursorVisible();
}
}
/*
============
idEditWindow::InitCvar
============
*/
void idEditWindow::InitCvar( )
{
if( cvarStr[0] == '\0' )
{
if( text.GetName() == NULL )
{
2012-11-26 18:58:24 +00:00
common->Warning( "idEditWindow::InitCvar: gui '%s' window '%s' has an empty cvar string", gui->GetSourceFile(), name.c_str() );
}
cvar = NULL;
return;
}
2012-11-26 18:58:24 +00:00
cvar = cvarSystem->Find( cvarStr );
if( !cvar )
{
2012-11-26 18:58:24 +00:00
common->Warning( "idEditWindow::InitCvar: gui '%s' window '%s' references undefined cvar '%s'", gui->GetSourceFile(), name.c_str(), cvarStr.c_str() );
return;
}
}
/*
============
idEditWindow::UpdateCvar
============
*/
void idEditWindow::UpdateCvar( bool read, bool force )
{
if( force || liveUpdate )
{
if( cvar )
{
if( read )
{
2012-11-26 18:58:24 +00:00
text = cvar->GetString();
}
else
{
2012-11-26 18:58:24 +00:00
cvar->SetString( text );
if( cvarMax && ( cvar->GetInteger() > cvarMax ) )
{
2012-11-26 18:58:24 +00:00
cvar->SetInteger( cvarMax );
}
}
}
}
}
/*
============
idEditWindow::RunNamedEvent
============
*/
void idEditWindow::RunNamedEvent( const char* eventName )
{
2012-11-26 18:58:24 +00:00
idStr event, group;
if( !idStr::Cmpn( eventName, "cvar read ", 10 ) )
{
2012-11-26 18:58:24 +00:00
event = eventName;
group = event.Mid( 10, event.Length() - 10 );
if( !group.Cmp( cvarGroup ) )
{
2012-11-26 18:58:24 +00:00
UpdateCvar( true, true );
}
}
else if( !idStr::Cmpn( eventName, "cvar write ", 11 ) )
{
2012-11-26 18:58:24 +00:00
event = eventName;
group = event.Mid( 11, event.Length() - 11 );
if( !group.Cmp( cvarGroup ) )
{
2012-11-26 18:58:24 +00:00
UpdateCvar( false, true );
}
}
}