📄 sdltextbox.cpp
字号:
// ============================================================================
// Data Structures For Game Programmers
// Ron Penton
// SDLTextBox.cpp
// This is a basic SDLGUI library that I've set up for use with the game demos
// I've used in the book.
// ============================================================================
#include "SDLTextBox.h"
#include <string.h>
// -------------------------------------------------------
// Name: SDLTextBox::SDLTextBox
// Description: This constructs the text box
// Arguments: - p_x: x coordinate
// - p_y: y coordinate
// - p_w, p_h: the width and height.
// - p_string: a pointer to the string of
// the text box
// - p_size: the max size of the string.
// - p_tforecol: the foreground color of the
// text
// - p_tbackcol: the background color of the
// text
// - p_font: the font of the text.
// - p_func: the function this calls when
// enter is pressed.
// -------------------------------------------------------
SDLTextBox::SDLTextBox( int p_x, int p_y,
int p_w, int p_h,
char* p_string,
int p_size,
SDL_Color p_tforecol,
SDL_Color p_tbackcol,
TTF_Font* p_font,
bool p_enabled,
void (*p_func)(void) )
{
m_x = p_x;
m_y = p_y;
m_w = p_w;
m_h = p_h;
m_string = p_string;
m_size = p_size;
m_tforecol = p_tforecol;
m_tbackcol = p_tbackcol;
m_font = p_font;
m_hasFocus = false;
m_enabled = p_enabled;
m_func = p_func;
m_blink = false;
m_timer = SDL_GetTicks();
}
// ----------------------------------------------------------------
// Name: Draw
// Description: draws the item on a surface
// Arguments: p_dest: the destination surface
// Return Value: None
// ----------------------------------------------------------------
void SDLTextBox::Draw( SDL_Surface* p_dest )
{
// render text
int blinkx = m_x + 1;
// if the string has some letters in it
if( m_string[0] != '\0' )
{
// render the text
SDL_Surface* text = TTF_RenderText_Shaded( m_font, m_string,
m_tforecol, m_tbackcol );
// find out if the text is longer than the width of the box
if( text->w > m_w )
{
// the text is larger than the box,
// so render only the right part of the text.
SDL_Rect sourcerect;
SDL_Rect destrect;
sourcerect.w = m_w;
sourcerect.h = text->h;
sourcerect.x = text->w - m_w;
sourcerect.y = 0;
destrect.w = m_w;
destrect.h = text->h;
destrect.x = m_x;
destrect.y = m_y + ((m_h - text->h) / 2);
SDL_BlitSurface( text, &sourcerect, p_dest, &destrect );
blinkx = m_x + m_w - 1;
}
else
{
// render the whole string
SDLBlit( text, p_dest, m_x, m_y + ((m_h - text->h) / 2));
blinkx = m_x + text->w + 1;
}
SDL_FreeSurface( text );
}
// draw the box.
if( m_hasFocus == true )
{
// red if the box has the focus
SDLLine( p_dest, m_x, m_y, m_x + m_w, m_y, RED );
SDLLine( p_dest, m_x, m_y + m_h, m_x + m_w, m_y + m_h, RED );
SDLLine( p_dest, m_x, m_y, m_x, m_y + m_h, RED );
SDLLine( p_dest, m_x + m_w, m_y, m_x + m_w, m_y + m_h, RED );
}
else
{
// grey if the box doesn't have the focus
SDLLine( p_dest, m_x, m_y, m_x + m_w, m_y, GREY );
SDLLine( p_dest, m_x, m_y + m_h, m_x + m_w, m_y + m_h, GREY );
SDLLine( p_dest, m_x, m_y, m_x, m_y + m_h, GREY );
SDLLine( p_dest, m_x + m_w, m_y, m_x + m_w, m_y + m_h, GREY );
}
// draw blinking cursor.
if( m_blink == false && m_hasFocus == true )
{
SDLLine( p_dest, blinkx, m_y + 2,
blinkx, m_y + m_h - 2, BLACK );
}
if( SDL_GetTicks() - m_timer > 500 )
{
m_timer = SDL_GetTicks();
m_blink = !m_blink;
}
}
// ----------------------------------------------------------------
// Name: ClickDown
// Description: Tells the item that it was clicked on.
// Arguments: None
// Return Value: None
// ----------------------------------------------------------------
void SDLTextBox::ClickDown()
{
// do nothing
}
// ----------------------------------------------------------------
// Name: ClickUp
// Description: Tells the item that it a mouse was released
// on this item
// Arguments: None
// Return Value: None
// ----------------------------------------------------------------
void SDLTextBox::ClickUp()
{
// do nothing
}
// ----------------------------------------------------------------
// Name: IsOver
// Description: determines if a set of coordinates is over this
// item
// Arguments: p_x, p_y: the coordinates
// Return Value: true if the coordinates are over this item
// ----------------------------------------------------------------
bool SDLTextBox::IsOver( int p_x, int p_y )
{
if( p_x >= m_x && p_x < (m_x + m_w) )
{
if( p_y >= m_y && p_y < (m_y + m_h) )
{
return true;
}
}
return false;
}
// ----------------------------------------------------------------
// Name: ResetOnUp
// Description: This tells the item to reset itself because
// the user unclicked the mouse (only valid
// for some items)
// Arguments: None
// Return Value: None
// ----------------------------------------------------------------
void SDLTextBox::ResetOnUp()
{
// do nothing
return;
}
// ----------------------------------------------------------------
// Name: CanGetFocus
// Description: determines if the item can get the focus
// Arguments: None
// Return Value: true if the item can get the focus
// ----------------------------------------------------------------
bool SDLTextBox::CanGetFocus()
{
if( m_enabled == true )
return true;
return false;
}
// ----------------------------------------------------------------
// Name: GetFocus
// Description: tells the item to get the focus or not.
// Arguments: p_focus: true if the item gets the focus
// Return Value: None
// ----------------------------------------------------------------
void SDLTextBox::GetFocus( bool p_focus )
{
m_hasFocus = p_focus;
}
// ----------------------------------------------------------------
// Name: KeyPress
// Description: tells the GUI that a key was pressed when this
// item was in focus
// Arguments: p_key: the that was pressed
// p_mod: the keyboard modifier flag (shift, alt,
// control).
// p_char: the UNICODE representation of the key
// Return Value: None
// ----------------------------------------------------------------
void SDLTextBox::KeyPress( SDLKey p_key, SDLMod p_mod, Uint16 p_char )
{
int current = strlen( m_string );
char k = (char)p_char;
if( p_key == SDLK_DELETE || p_key == SDLK_BACKSPACE )
{
m_string[current - 1] = 0;
}
else if( p_key == SDLK_RETURN )
{
if( m_func != 0 )
{
m_func();
}
}
else
{
// only accept 'character' keys
if( p_key >= SDLK_SPACE && p_key <= SDLK_z )
{
if( current < m_size )
{
m_string[current] = k;
m_string[current+1] = 0;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -