1
0
forked from 0ad/0ad

Updated CList to fit JS interface, also added CDropDown in GUI.h

This was SVN commit r2163.
This commit is contained in:
Gee 2005-04-23 23:20:50 +00:00
parent 4be2ac7839
commit 15759ff4d7
9 changed files with 162 additions and 180 deletions

View File

@ -22,6 +22,7 @@ gee@pyro.nu
#include "CRadioButton.h"
#include "CInput.h"
#include "CList.h"
#include "CDropDown.h"
#include "CProgressBar.h"
#include "CTooltip.h"
#include "MiniMap.h"
@ -156,6 +157,12 @@ int CGUI::HandleEvent(const SDL_Event* ev)
ret = EV_HANDLED;
}
else if (m_FocusedObject)
{
m_FocusedObject->HandleMessage(SGUIMessage(GUIM_LOST_FOCUS));
//if (m_FocusedObject-> TODO SelfishFocus?
m_FocusedObject = 0;
}
break;
case SDL_BUTTON_WHEELDOWN: // wheel down
@ -331,6 +338,7 @@ void CGUI::Initialize()
AddObjectType("minimap", &CMiniMap::ConstructObject);
AddObjectType("input", &CInput::ConstructObject);
// AddObjectType("list", &CList::ConstructObject);
AddObjectType("dropdown", &CDropDown::ConstructObject);
}
void CGUI::Process()

View File

@ -96,10 +96,6 @@ void CGUIScrollBarVertical::Draw()
);
}
LOG(ERROR, LOG_CATEGORY, "GetBarRect(%f,%f,%f,%f)", GetBarRect().left, GetBarRect().top, GetBarRect().right, GetBarRect().bottom);
LOG(ERROR, LOG_CATEGORY, "m_SpriteBarVertical = %s", GetStyle()->m_SpriteBarVertical.GetName().c_str());
LOG(ERROR, LOG_CATEGORY, "m_SpriteBackVertical = %s", GetStyle()->m_SpriteBackVertical.GetName().c_str());
// Draw bar
/*if (m_BarPressed)
GetGUI()->DrawSprite(GUI<>::FallBackSprite(GetStyle()->m_SpriteBarVerticalPressed, GetStyle()->m_SpriteBarVertical),

View File

@ -8,6 +8,8 @@ gee@pyro.nu
#include "GUI.h"
#include "CList.h"
#include "ps/CLogger.h"
using namespace std;
//-------------------------------------------------------------------
@ -28,6 +30,7 @@ CList::CList()
AddSetting(GUIST_int, "selected"); // Index selected. -1 is none.
//AddSetting(GUIST_CStr, "tooltip");
//AddSetting(GUIST_CStr, "tooltip_style");
AddSetting(GUIST_CGUIList, "list");
GUI<bool>::SetSetting(this, "scrollbar", false);
@ -49,9 +52,14 @@ void CList::SetupText()
if (!GetGUI())
return;
CGUIList *pList;
GUI<CGUIList>::GetSettingPointer(this, "list", pList);
//LOG(ERROR, LOG_CATEGORY, "SetupText() %s", GetPresentableName().c_str());
//assert(m_GeneratedTexts.size()>=1);
m_ItemsYPositions.resize( m_Items.size()+1 );
m_ItemsYPositions.resize( pList->m_Items.size()+1 );
// Delete all generated texts. Some could probably be saved,
// but this is easier, and this function will never be called
@ -75,7 +83,7 @@ void CList::SetupText()
//GUI<CGUIString>::GetSetting(this, "caption", caption);
GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
float width = m_CachedActualSize.GetWidth();
float width = GetListRect().GetWidth();
// remove scrollbar if applicable
if (scrollbar && GetScrollBar(0).GetStyle())
width -= GetScrollBar(0).GetStyle()->m_Width;
@ -86,12 +94,12 @@ void CList::SetupText()
// Generate texts
float buffered_y = 0.f;
for (size_t i=0; i<m_Items.size(); ++i)
for (int i=0; i<(int)pList->m_Items.size(); ++i)
{
// Create a new SGUIText. Later on, input it using AddText()
SGUIText *text = new SGUIText();
*text = GetGUI()->GenerateText(m_Items[i], font, width, buffer_zone, this);
*text = GetGUI()->GenerateText(pList->m_Items[i], font, width, buffer_zone, this);
m_ItemsYPositions[i] = buffered_y;
buffered_y += text->m_Size.cy;
@ -108,7 +116,7 @@ void CList::SetupText()
if (scrollbar)
{
GetScrollBar(0).SetScrollRange( m_ItemsYPositions.back() );
GetScrollBar(0).SetScrollSpace( m_CachedActualSize.GetHeight() );
GetScrollBar(0).SetScrollSpace( GetListRect().GetHeight() );
}
}
@ -130,10 +138,16 @@ void CList::HandleMessage(const SGUIMessage &Message)
Message.value == CStr("z") ||
Message.value == CStr("absolute")))
{
GetScrollBar(0).SetX( m_CachedActualSize.right );
GetScrollBar(0).SetY( m_CachedActualSize.top );
CRect rect = GetListRect();
GetScrollBar(0).SetX( rect.right );
GetScrollBar(0).SetY( rect.top );
GetScrollBar(0).SetZ( GetBufferedZ() );
GetScrollBar(0).SetLength( m_CachedActualSize.bottom - m_CachedActualSize.top );
GetScrollBar(0).SetLength( rect.bottom - rect.top );
}
if (Message.value == "list")
{
SetupText();
}
if (Message.value == CStr("scrollbar"))
@ -157,20 +171,23 @@ void CList::HandleMessage(const SGUIMessage &Message)
case GUIM_MOUSE_PRESS_LEFT:
{
bool scrollbar;
CGUIList *pList;
GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
GUI<CGUIList>::GetSettingPointer(this, "list", pList);
float scroll=0.f;
if (scrollbar)
{
scroll = GetScrollBar(0).GetPos();
}
CRect rect = GetListRect();
CPos mouse = GetMousePos();
mouse.y += scroll;
int set=-1;
for (int i=0; i<(int)m_Items.size(); ++i)
for (int i=0; i<(int)pList->m_Items.size(); ++i)
{
if (mouse.y >= m_CachedActualSize.top + m_ItemsYPositions[i] &&
mouse.y < m_CachedActualSize.top + m_ItemsYPositions[i+1] &&
if (mouse.y >= rect.top + m_ItemsYPositions[i] &&
mouse.y < rect.top + m_ItemsYPositions[i+1] &&
// mouse is not over scroll-bar
!(mouse.x >= GetScrollBar(0).GetOuterRect().left &&
mouse.x <= GetScrollBar(0).GetOuterRect().right))
@ -202,10 +219,11 @@ void CList::HandleMessage(const SGUIMessage &Message)
case GUIM_LOAD:
{
GetScrollBar(0).SetX( m_CachedActualSize.right );
GetScrollBar(0).SetY( m_CachedActualSize.top );
CRect rect = GetListRect();
GetScrollBar(0).SetX( rect.right );
GetScrollBar(0).SetY( rect.top );
GetScrollBar(0).SetZ( GetBufferedZ() );
GetScrollBar(0).SetLength( m_CachedActualSize.bottom - m_CachedActualSize.top );
GetScrollBar(0).SetLength( rect.bottom - rect.top );
CStr scrollbar_style;
GUI<CStr>::GetSetting(this, "scrollbar_style", scrollbar_style);
@ -226,42 +244,53 @@ int CList::ManuallyHandleEvent(const SDL_Event* ev)
switch (szChar)
{
case SDLK_HOME:
SelectFirstElement();
UpdateAutoScroll();
break;
case SDLK_HOME:
SelectFirstElement();
UpdateAutoScroll();
break;
case SDLK_END:
SelectLastElement();
UpdateAutoScroll();
break;
case SDLK_END:
SelectLastElement();
UpdateAutoScroll();
break;
case SDLK_UP:
SelectPrevElement();
UpdateAutoScroll();
break;
case SDLK_UP:
SelectPrevElement();
UpdateAutoScroll();
break;
case SDLK_DOWN:
SelectNextElement();
UpdateAutoScroll();
break;
case SDLK_DOWN:
SelectNextElement();
UpdateAutoScroll();
break;
case SDLK_PAGEUP:
GetScrollBar(0).ScrollMinusPlenty();
break;
case SDLK_PAGEUP:
GetScrollBar(0).ScrollMinusPlenty();
break;
case SDLK_PAGEDOWN:
GetScrollBar(0).ScrollPlusPlenty();
break;
case SDLK_PAGEDOWN:
GetScrollBar(0).ScrollPlusPlenty();
break;
default: // Do nothing
break;
default: // Do nothing
break;
}
return EV_HANDLED;
}
void CList::Draw()
{
int selected;
GUI<int>::GetSetting(this, "selected", selected);
DrawList(selected, "sprite", "sprite_selectarea", "textcolor");
}
void CList::DrawList(const int &selected,
const CStr &_sprite,
const CStr &_sprite_selected,
const CStr &_textcolor)
{
float bz = GetBufferedZ();
@ -277,14 +306,18 @@ void CList::Draw()
if (GetGUI())
{
CGUISpriteInstance *sprite=NULL, *sprite_selectarea=NULL;
int cell_id, selected;
GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite", sprite);
GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite_selectarea", sprite_selectarea);
GUI<int>::GetSetting(this, "cell_id", cell_id);
GUI<int>::GetSetting(this, "selected", selected);
CRect rect = GetListRect();
GetGUI()->DrawSprite(*sprite, cell_id, bz, m_CachedActualSize);
CGUISpriteInstance *sprite=NULL, *sprite_selectarea=NULL;
int cell_id;
GUI<CGUISpriteInstance>::GetSettingPointer(this, _sprite, sprite);
GUI<CGUISpriteInstance>::GetSettingPointer(this, _sprite_selected, sprite_selectarea);
GUI<int>::GetSetting(this, "cell_id", cell_id);
CGUIList *pList;
GUI<CGUIList>::GetSettingPointer(this, "list", pList);
GetGUI()->DrawSprite(*sprite, cell_id, bz, rect);
float scroll=0.f;
if (scrollbar)
@ -297,16 +330,16 @@ void CList::Draw()
assert(selected >= 0 && selected+1 < (int)m_ItemsYPositions.size());
// Get rectangle of selection:
CRect rect(m_CachedActualSize.left, m_CachedActualSize.top + m_ItemsYPositions[selected] - scroll,
m_CachedActualSize.right, m_CachedActualSize.top + m_ItemsYPositions[selected+1] - scroll);
CRect rect(rect.left, rect.top + m_ItemsYPositions[selected] - scroll,
rect.right, rect.top + m_ItemsYPositions[selected+1] - scroll);
if (rect.top <= m_CachedActualSize.bottom &&
rect.bottom >= m_CachedActualSize.top)
if (rect.top <= rect.bottom &&
rect.bottom >= rect.top)
{
if (rect.bottom > m_CachedActualSize.bottom)
rect.bottom = m_CachedActualSize.bottom;
if (rect.top < m_CachedActualSize.top)
rect.top = m_CachedActualSize.top;
if (rect.bottom > rect.bottom)
rect.bottom = rect.bottom;
if (rect.top < rect.top)
rect.top = rect.top;
if (scrollbar)
{
@ -326,24 +359,32 @@ void CList::Draw()
}
CColor color;
GUI<CColor>::GetSetting(this, "textcolor", color);
GUI<CColor>::GetSetting(this, _textcolor, color);
for (int i=0; i<(int)m_Items.size(); ++i)
for (int i=0; i<(int)pList->m_Items.size(); ++i)
{
if (m_ItemsYPositions[i+1] - scroll < 0 ||
m_ItemsYPositions[i] - scroll > m_CachedActualSize.GetHeight())
m_ItemsYPositions[i] - scroll > rect.GetHeight())
continue;
IGUITextOwner::Draw(i, color, m_CachedActualSize.TopLeft() - CPos(0.f, scroll - m_ItemsYPositions[i]), bz+0.1f);
IGUITextOwner::Draw(i, color, rect.TopLeft() - CPos(0.f, scroll - m_ItemsYPositions[i]), bz+0.1f);
}
}
}
void CList::AddItem(const CStr& str)
{
LOG(ERROR, "gui", "Hej: %s", str.c_str());
CGUIList *pList;
GUI<CGUIList>::GetSettingPointer(this, "list", pList);
CGUIString gui_string;
gui_string.SetValue(str);
m_Items.push_back( gui_string );
pList->m_Items.push_back( gui_string );
// TODO Temp
SetupText();
}
bool CList::HandleAdditionalChildren(const XMBElement& child, CXeromyces* pFile)
@ -367,7 +408,10 @@ void CList::SelectNextElement()
int selected;
GUI<int>::GetSetting(this, "selected", selected);
if (selected != m_Items.size()-1)
CGUIList *pList;
GUI<CGUIList>::GetSettingPointer(this, "list", pList);
if (selected != pList->m_Items.size()-1)
{
++selected;
GUI<int>::SetSetting(this, "selected", selected);
@ -402,9 +446,12 @@ void CList::SelectLastElement()
int selected;
GUI<int>::GetSetting(this, "selected", selected);
if (selected != m_Items.size()-1)
CGUIList *pList;
GUI<CGUIList>::GetSettingPointer(this, "list", pList);
if (selected != pList->m_Items.size()-1)
{
GUI<int>::SetSetting(this, "selected", (int)m_Items.size()-1);
GUI<int>::SetSetting(this, "selected", (int)pList->m_Items.size()-1);
}
}
@ -416,6 +463,8 @@ void CList::UpdateAutoScroll()
GUI<int>::GetSetting(this, "selected", selected);
GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
CRect rect = GetListRect();
// No scrollbar, no scrolling (at least it's not made to work properly).
if (!scrollbar)
return;
@ -431,101 +480,8 @@ void CList::UpdateAutoScroll()
}
// Check lower boundary
if (m_ItemsYPositions[selected+1]-m_CachedActualSize.GetHeight() > scroll)
if (m_ItemsYPositions[selected+1]-rect.GetHeight() > scroll)
{
GetScrollBar(0).SetPos(m_ItemsYPositions[selected+1]-m_CachedActualSize.GetHeight());
GetScrollBar(0).SetPos(m_ItemsYPositions[selected+1]-rect.GetHeight());
}
/* float buffer_zone;
bool multiline;
GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);
GUI<bool>::GetSetting(this, "multiline", multiline);
// Autoscrolling up and down
if (multiline)
{
CStr font_name;
bool scrollbar;
GUI<CStr>::GetSetting(this, "font", font_name);
GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
float scroll=0.f;
if (!scrollbar)
return;
scroll = GetScrollBar(0).GetPos();
// Now get the height of the font.
// TODO: Get the real font
CFont font(font_name);
float spacing = (float)font.GetLineSpacing();
//float height = font.GetHeight();
// TODO Gee (2004-11-21): Okay, I need a 'list' for some reasons, but I would really like to
// be able to get the specific element here. This is hopefully a temporary hack.
list<SRow>::iterator current = m_CharacterPositions.begin();
int row=0;
while (current != m_CharacterPositions.end())
{
if (m_iBufferPos >= current->m_ListStart &&
m_iBufferPos <= current->m_ListStart+(int)current->m_ListOfX.size())
break;
++current;
++row;
}
// If scrolling down
if (-scroll + (float)(row+1) * spacing + buffer_zone*2.f > m_CachedActualSize.GetHeight())
{
// Scroll so the selected row is shown completely, also with buffer_zone length to the edge.
GetScrollBar(0).SetPos((float)(row+1) * spacing - m_CachedActualSize.GetHeight() + buffer_zone*2.f);
}
else
// If scrolling up
if (-scroll + (float)row * spacing < 0.f)
{
// Scroll so the selected row is shown completely, also with buffer_zone length to the edge.
GetScrollBar(0).SetPos((float)row * spacing);
}
}
else // autoscrolling left and right
{
// Get X position of position:
if (m_CharacterPositions.empty())
return;
float x_position = 0.f;
float x_total = 0.f;
if (!m_CharacterPositions.begin()->m_ListOfX.empty())
{
// Get position of m_iBufferPos
if ((int)m_CharacterPositions.begin()->m_ListOfX.size() >= m_iBufferPos &&
m_iBufferPos != 0)
x_position = m_CharacterPositions.begin()->m_ListOfX[m_iBufferPos-1];
// Get complete length:
x_total = m_CharacterPositions.begin()->m_ListOfX[ m_CharacterPositions.begin()->m_ListOfX.size()-1 ];
}
// Check if outside to the right
if (x_position - m_HorizontalScroll + buffer_zone*2.f > m_CachedActualSize.GetWidth())
m_HorizontalScroll = x_position - m_CachedActualSize.GetWidth() + buffer_zone*2.f;
// Check if outside to the left
if (x_position - m_HorizontalScroll < 0.f)
m_HorizontalScroll = x_position;
// Check if the text doesn't even fill up to the right edge even though scrolling is done.
if (m_HorizontalScroll != 0.f &&
x_total - m_HorizontalScroll + buffer_zone*2.f < m_CachedActualSize.GetWidth())
m_HorizontalScroll = x_total - m_CachedActualSize.GetWidth() + buffer_zone*2.f;
// Now this is the fail-safe, if x_total isn't even the length of the control,
// remove all scrolling
if (x_total + buffer_zone*2.f < m_CachedActualSize.GetWidth())
m_HorizontalScroll = 0.f;
}*/
}

View File

@ -38,6 +38,7 @@ gee@pyro.nu
// Declarations
//--------------------------------------------------------
/**
* @author Gustav Larsson
*
@ -61,6 +62,11 @@ public:
virtual void ResetStates() { IGUIScrollBarOwner::ResetStates(); }
/**
* Adds an item last to the list.
*/
virtual void AddItem(const CStr& str);
protected:
/**
* Sets up text, should be called every time changes has been
@ -85,11 +91,6 @@ protected:
*/
virtual void Draw();
/**
* Adds an item last to the list.
*/
virtual void AddItem(const CStr& str);
/**
* Easy select elements functions
*/
@ -106,11 +107,17 @@ protected:
// Called every time the auto-scrolling should be checked.
void UpdateAutoScroll();
/**
* List of items (as text), the post-processed result is stored in
* the IGUITextOwner structure of this class.
*/
std::vector<CGUIString> m_Items;
// Extended drawing interface, this is so that classes built on the this one
// can use other sprite names.
void DrawList(const int &selected, const CStr &_sprite,
const CStr &_sprite_selected, const CStr &_textcolor);
// Get the area of the list. This is so that i can easily be changed, like in CDropDown
// where the area is not equal to m_CachedActualSize.
virtual CRect GetListRect() const { return m_CachedActualSize; }
// List of items.
//CGUIList m_List;
/**
* List of each element's relative y position. Will be

View File

@ -33,6 +33,7 @@ gee@pyro.nu
#include "GUIbase.h"
#include "GUIutil.h"
#include "GUItext.h"
#include "CGUIList.h"
#include "IGUIObject.h"
#include "IGUIButtonBehavior.h"
#include "IGUIScrollBarOwner.h"

View File

@ -34,6 +34,7 @@ my @types = qw(
EAlign
EVAlign
CPos
CGUIList
);
@ -99,7 +100,10 @@ TYPE(EVAlign)
#ifndef GUITYPE_IGNORE_CPos
TYPE(CPos)
#endif
#ifndef GUITYPE_IGNORE_CGUIList
TYPE(CGUIList)
#endif
#ifdef PLEASE_DO_NOT_DEFINE_THIS
// See IGUIObject.h for 'enum EGUISettingType'
enum {GUIST_bool,GUIST_int,GUIST_float,GUIST_CColor,GUIST_CClientArea,GUIST_CGUIString,GUIST_CGUISpriteInstance,GUIST_CStr,GUIST_CStrW,GUIST_EAlign,GUIST_EVAlign,GUIST_CPos};
enum {GUIST_bool,GUIST_int,GUIST_float,GUIST_CColor,GUIST_CClientArea,GUIST_CGUIString,GUIST_CGUISpriteInstance,GUIST_CStr,GUIST_CStrW,GUIST_EAlign,GUIST_EVAlign,GUIST_CPos,GUIST_CGUIList};
#endif

View File

@ -238,6 +238,14 @@ bool __ParseString<CGUISpriteInstance>(const CStr& Value, CGUISpriteInstance &Ou
return true;
}
template <>
bool __ParseString<CGUIList>(const CStr& Value, CGUIList &Output)
{
//LOG(WARNING, LOG_CATEGORY, "Cannot set a 'list' from a string.");
return false;
}
//--------------------------------------------------------
void guiLoadIdentity()

View File

@ -60,28 +60,32 @@ void IGUIButtonBehavior::HandleMessage(const SGUIMessage &Message)
CColor IGUIButtonBehavior::ChooseColor()
{
CColor color, color_over, color_pressed, color_disabled;
CColor color, color2;
// Yes, the object must possess these settings. They are standard
GUI<CColor>::GetSetting(this, "textcolor", color);
GUI<CColor>::GetSetting(this, "textcolor_over", color_over);
GUI<CColor>::GetSetting(this, "textcolor_pressed", color_pressed);
GUI<CColor>::GetSetting(this, "textcolor_disabled", color_disabled);
bool enabled;
GUI<bool>::GetSetting(this, "enabled", enabled);
if (!enabled)
{
return GUI<>::FallBackColor(color_disabled, color);
GUI<CColor>::GetSetting(this, "textcolor_disabled", color2);
return GUI<>::FallBackColor(color2, color);
}
else
if (m_MouseHovering)
{
if (m_Pressed)
return GUI<>::FallBackColor(color_pressed, color);
{
GUI<CColor>::GetSetting(this, "textcolor_pressed", color2);
return GUI<>::FallBackColor(color2, color);
}
else
return GUI<>::FallBackColor(color_over, color);
{
GUI<CColor>::GetSetting(this, "textcolor_over", color2);
return GUI<>::FallBackColor(color2, color);
}
}
else return color;
}

View File

@ -41,8 +41,6 @@ void IGUITextOwner::HandleMessage(const SGUIMessage &Message)
// these. Although that is not certain, but one will have to manually
// change it and disregard this function.
// TODO Gee: (2004-09-07) Make sure this is all options that can affect the text.
// Also TODO: If several things are changing (e.g. when loading the file),
// only call SetupText once (probably just before it's drawn)
if (Message.value == "size" || Message.value == "z" ||
Message.value == "absolute" || Message.value == "caption" ||
Message.value == "font" || Message.value == "textcolor" ||