📄 sdlgui.cpp
字号:
// draw all visible items.
for( index = 0; index < m_itemcount; index++ )
{
if( m_items[index] != 0 )
{
if( m_items[index]->Visible() == true )
m_items[index]->Draw( m_screen );
}
}
}
// ----------------------------------------------------------------
// Name: Update
// Description: updates the GUI surface onto the screen.
// Arguments: None.
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::Update()
{
SDL_UpdateRect( m_screen, 0, 0, 0, 0 );
}
// ----------------------------------------------------------------
// Name: MouseDown
// Description: tells the GUI that a mouse was clicked.
// Arguments: p_x, p_y: coordinates of the click
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::MouseDown( int p_x, int p_y )
{
int index;
// loop through every item, and check if the mouse is over the item.
// if so, and the item is visible, tell it that it has been clicked.
for( index = 0; index < m_itemcount; index++ )
{
if( m_items[index]->IsOver( p_x, p_y ) && m_items[index]->Visible() )
{
m_items[index]->ClickDown();
}
}
}
// ----------------------------------------------------------------
// Name: MouseUp
// Description: tells the GUI that a mouse button was released
// Arguments: p_x, p_y: coordinates of the mouse
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::MouseUp( int p_x, int p_y )
{
int index;
// loop through every item, and check if the mouse is over the item.
// if so, and the item is visible, tell it that the mouse has been
// released over it.
for( index = 0; index < m_itemcount; index++ )
{
if( m_items[index]->IsOver( p_x, p_y ) && m_items[index]->Visible() )
{
m_items[index]->ClickUp();
// if the current item can get the focus, set the focus.
if( m_items[index]->CanGetFocus() )
{
if( m_currentitem != -1 )
m_items[m_currentitem]->GetFocus( false );
m_currentitem = index;
m_items[m_currentitem]->GetFocus( true );
}
}
m_items[index]->ResetOnUp();
}
}
// ----------------------------------------------------------------
// Name: KeyDown
// Description: Tells the GUI that a key was pressed
// 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 SDLGUI::KeyDown( SDLKey p_key, SDLMod p_mod, Uint16 p_char )
{
// if tab was pressed, cycle through until the
// next item that can get the focus is found.
if( p_key == SDLK_TAB && m_currentitem != -1 )
{
// if shift is being held, cycle backwards
// else cycle forwards.
if( p_mod & KMOD_LSHIFT || p_mod & KMOD_RSHIFT )
{
// loop through backwards until you find an item
// that can get the focus.
m_items[m_currentitem]->GetFocus( false );
while( 1 )
{
m_currentitem--;
if( m_currentitem < 0 )
m_currentitem = (m_itemcount - 1);
if( m_items[m_currentitem]->CanGetFocus() )
break;
}
m_items[m_currentitem]->GetFocus( true );
}
else
{
// loop through forwards until you find an item
// that can get the focus.
m_items[m_currentitem]->GetFocus( false );
while( 1 )
{
m_currentitem++;
if( m_currentitem >= m_itemcount )
m_currentitem = 0;
if( m_items[m_currentitem]->CanGetFocus() )
break;
}
m_items[m_currentitem]->GetFocus( true );
}
}
if( m_currentitem != -1 )
{
m_items[m_currentitem]->KeyPress( p_key, p_mod, p_char );
}
}
// ----------------------------------------------------------------
// Name: Point
// Description: draws a point on the GUI
// Arguments: x, y: coordinates
// p_color: the color of the point
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::Point( int x, int y, SDL_Color p_color )
{
SDLPoint( m_screen, x, y, p_color );
}
// ----------------------------------------------------------------
// Name: Line
// Description: draws a line on the GUI
// Arguments: x1, y1: first point
// x2, y2: second point
// p_color: color of the line
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::Line( int x1, int y1,
int x2, int y2, SDL_Color p_color )
{
SDLLine( m_screen, x1, y1, x2, y2, p_color );
}
// ----------------------------------------------------------------
// Name: ArrowLine
// Description: draws a line, with optional arrowheads
// Arguments: x1, y1: first point
// x2, y2: second point
// r1, r2: the radius of the points (see Appendx
// C in the book)
// arrow1, arrow2: determines if an arrow is drawn
// p_color: color of the line
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::ArrowLine( int x1, int y1, int x2, int y2,
int r1, int r2, bool arrow1, bool arrow2, SDL_Color p_color )
{
SDLArrowLine( m_screen, x1, y1, x2, y2, r1, r2, arrow1, arrow2, p_color );
}
// ----------------------------------------------------------------
// Name: Blit
// Description: draws a surface on the GUI
// Arguments: p_source: surface to draw from
// x, y: coordinates
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::Blit( SDL_Surface* p_source, int x, int y )
{
SDLBlit( p_source, m_screen, x, y );
}
// ----------------------------------------------------------------
// Name: Box
// Description: draws a solid box on the GUI
// Arguments: x, y: coordinates
// width, height: width and height of the box
// p_color: color of the box
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::Box( int x, int y, int width, int height, SDL_Color p_color )
{
SDLBox( m_screen, x, y, width, height, p_color );
}
// ----------------------------------------------------------------
// Name: GetFont
// Description: gets a pointer to a given font in the GUI
// Arguments: p_index: the index of the font
// Return Value: a pointer to the font
// ----------------------------------------------------------------
TTF_Font* SDLGUI::GetFont( int p_index )
{
return m_fonts[p_index];
}
// ----------------------------------------------------------------
// Name: GetItem
// Description: Gets a pointer to the given item
// Arguments: p_item: the index of the item
// Return Value: a pointer to the item
// ----------------------------------------------------------------
SDLGUIItem* SDLGUI::GetItem( int p_item )
{
return m_items[ p_item ];
}
// ----------------------------------------------------------------
// Name: GetScreen
// Description: Gets a pointer to the screen surface
// Arguments: None
// Return Value: a pointer to the screen surface
// ----------------------------------------------------------------
SDL_Surface* SDLGUI::GetScreen()
{
return m_screen;
}
// ----------------------------------------------------------------
// Name: SetFocus
// Description: This sets the focus of the GUI to a particular
// item.
// Arguments: p_item: index of the item
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::SetFocus( int p_item )
{
if( p_item < m_itemcount && p_item >= 0 )
{
if( m_items[p_item] != 0 )
{
m_items[p_item]->GetFocus( true );
if( m_currentitem != -1 )
{
m_items[m_currentitem]->GetFocus( false );
}
m_currentitem = p_item;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -