Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/gui/ObjectTypes/CList.cpp
Show All 32 Lines | |||||
CList::CList(CGUI& pGUI) | CList::CList(CGUI& pGUI) | ||||
: IGUIObject(pGUI), | : IGUIObject(pGUI), | ||||
IGUITextOwner(*static_cast<IGUIObject*>(this)), | IGUITextOwner(*static_cast<IGUIObject*>(this)), | ||||
IGUIScrollBarOwner(*static_cast<IGUIObject*>(this)), | IGUIScrollBarOwner(*static_cast<IGUIObject*>(this)), | ||||
m_Modified(false), | m_Modified(false), | ||||
m_PrevSelectedItem(-1), | m_PrevSelectedItem(-1), | ||||
m_LastItemClickTime(0), | m_LastItemClickTime(0), | ||||
m_BufferZone(), | m_BufferZone(this, "buffer_zone"), | ||||
m_Font(), | m_Font(this, "font"), | ||||
m_ScrollBar(), | m_ScrollBar(this, "scrollbar", false), | ||||
m_ScrollBarStyle(), | m_ScrollBarStyle(this, "scrollbar_style"), | ||||
m_ScrollBottom(false), | m_ScrollBottom(this, "scroll_bottom", false), | ||||
m_SoundDisabled(), | m_SoundDisabled(this, "sound_disabled"), | ||||
m_SoundSelected(), | m_SoundSelected(this, "sound_selected"), | ||||
m_Sprite(), | m_Sprite(this, "sprite"), | ||||
m_SpriteSelectArea(), | |||||
m_TextAlign(), | |||||
m_TextColor(), | |||||
m_TextColorSelected(), | |||||
m_Selected(), | |||||
m_AutoScroll(), | |||||
m_Hovered(), | |||||
m_List(), | |||||
m_ListData() | |||||
{ | |||||
RegisterSetting("buffer_zone", m_BufferZone); | |||||
RegisterSetting("font", m_Font); | |||||
RegisterSetting("scrollbar", m_ScrollBar); | |||||
RegisterSetting("scrollbar_style", m_ScrollBarStyle); | |||||
RegisterSetting("scroll_bottom", m_ScrollBottom); | |||||
RegisterSetting("sound_disabled", m_SoundDisabled); | |||||
RegisterSetting("sound_selected", m_SoundSelected); | |||||
RegisterSetting("sprite", m_Sprite); | |||||
// Add sprite_disabled! TODO | // Add sprite_disabled! TODO | ||||
RegisterSetting("sprite_selectarea", m_SpriteSelectArea); | m_SpriteSelectArea(this, "sprite_selectarea"), | ||||
RegisterSetting("text_align", m_TextAlign); | m_TextColor(this, "textcolor"), | ||||
RegisterSetting("textcolor", m_TextColor); | m_TextColorSelected(this, "textcolor_selected"), | ||||
RegisterSetting("textcolor_selected", m_TextColorSelected); | m_Selected(this, "selected", -1), // Index selected. -1 is none. | ||||
RegisterSetting("selected", m_Selected); // Index selected. -1 is none. | m_AutoScroll(this, "auto_scroll", false), | ||||
RegisterSetting("auto_scroll", m_AutoScroll); | m_Hovered(this, "hovered", -1), | ||||
RegisterSetting("hovered", m_Hovered); | |||||
// Each list item has both a name (in 'list') and an associated data string (in 'list_data') | // Each list item has both a name (in 'list') and an associated data string (in 'list_data') | ||||
RegisterSetting("list", m_List); | m_List(this, "list"), | ||||
RegisterSetting("list_data", m_ListData); | m_ListData(this, "list_data") | ||||
{ | |||||
SetSetting<bool>("scrollbar", false, true); | |||||
SetSetting<i32>("selected", -1, true); | |||||
SetSetting<i32>("hovered", -1, true); | |||||
SetSetting<bool>("auto_scroll", false, true); | |||||
// Add scroll-bar | // Add scroll-bar | ||||
CGUIScrollBarVertical* bar = new CGUIScrollBarVertical(pGUI); | CGUIScrollBarVertical* bar = new CGUIScrollBarVertical(pGUI); | ||||
bar->SetRightAligned(true); | bar->SetRightAligned(true); | ||||
AddScrollBar(bar); | AddScrollBar(bar); | ||||
} | } | ||||
CList::~CList() | CList::~CList() | ||||
{ | { | ||||
Show All 26 Lines | void CList::SetupText(bool append) | ||||
} | } | ||||
// Generate texts | // Generate texts | ||||
float buffered_y = 0.f; | float buffered_y = 0.f; | ||||
if (append && !m_ItemsYPositions.empty()) | if (append && !m_ItemsYPositions.empty()) | ||||
buffered_y = m_ItemsYPositions.back(); | buffered_y = m_ItemsYPositions.back(); | ||||
m_ItemsYPositions.resize(m_List.m_Items.size() + 1); | m_ItemsYPositions.resize(m_List->m_Items.size() + 1); | ||||
for (size_t i = append ? m_List.m_Items.size() - 1 : 0; i < m_List.m_Items.size(); ++i) | for (size_t i = append ? m_List->m_Items.size() - 1 : 0; i < m_List->m_Items.size(); ++i) | ||||
{ | { | ||||
CGUIText* text; | CGUIText* text; | ||||
if (!m_List.m_Items[i].GetOriginalString().empty()) | if (!m_List->m_Items[i].GetOriginalString().empty()) | ||||
text = &AddText(m_List.m_Items[i], m_Font, width, m_BufferZone); | text = &AddText(m_List->m_Items[i], m_Font, width, m_BufferZone); | ||||
else | else | ||||
{ | { | ||||
// Minimum height of a space character of the current font size | // Minimum height of a space character of the current font size | ||||
CGUIString align_string; | CGUIString align_string; | ||||
align_string.SetValue(L" "); | align_string.SetValue(L" "); | ||||
text = &AddText(align_string, m_Font, width, m_BufferZone); | text = &AddText(align_string, m_Font, width, m_BufferZone); | ||||
} | } | ||||
m_ItemsYPositions[i] = buffered_y; | m_ItemsYPositions[i] = buffered_y; | ||||
buffered_y += text->GetSize().Height; | buffered_y += text->GetSize().Height; | ||||
} | } | ||||
m_ItemsYPositions[m_List.m_Items.size()] = buffered_y; | m_ItemsYPositions[m_List->m_Items.size()] = buffered_y; | ||||
// Setup scrollbar | // Setup scrollbar | ||||
if (m_ScrollBar) | if (m_ScrollBar) | ||||
{ | { | ||||
GetScrollBar(0).SetScrollRange(m_ItemsYPositions.back()); | GetScrollBar(0).SetScrollRange(m_ItemsYPositions.back()); | ||||
GetScrollBar(0).SetScrollSpace(GetListRect().GetHeight()); | GetScrollBar(0).SetScrollSpace(GetListRect().GetHeight()); | ||||
CRect rect = GetListRect(); | CRect rect = GetListRect(); | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | case GUIM_MOUSE_PRESS_LEFT: | ||||
{ | { | ||||
PlaySound(m_SoundDisabled); | PlaySound(m_SoundDisabled); | ||||
break; | break; | ||||
} | } | ||||
int hovered = GetHoveredItem(); | int hovered = GetHoveredItem(); | ||||
if (hovered == -1) | if (hovered == -1) | ||||
break; | break; | ||||
SetSetting<i32>("selected", hovered, true); | m_Selected.Set(hovered, true); | ||||
UpdateAutoScroll(); | UpdateAutoScroll(); | ||||
PlaySound(m_SoundSelected); | PlaySound(m_SoundSelected); | ||||
if (timer_Time() - m_LastItemClickTime < SELECT_DBLCLICK_RATE && hovered == m_PrevSelectedItem) | if (timer_Time() - m_LastItemClickTime < SELECT_DBLCLICK_RATE && hovered == m_PrevSelectedItem) | ||||
this->SendMouseEvent(GUIM_MOUSE_DBLCLICK_LEFT_ITEM, EventNameMouseLeftDoubleClickItem); | this->SendMouseEvent(GUIM_MOUSE_DBLCLICK_LEFT_ITEM, EventNameMouseLeftDoubleClickItem); | ||||
else | else | ||||
this->SendMouseEvent(GUIM_MOUSE_PRESS_LEFT_ITEM, EventNameMouseLeftClickItem); | this->SendMouseEvent(GUIM_MOUSE_PRESS_LEFT_ITEM, EventNameMouseLeftClickItem); | ||||
m_LastItemClickTime = timer_Time(); | m_LastItemClickTime = timer_Time(); | ||||
m_PrevSelectedItem = hovered; | m_PrevSelectedItem = hovered; | ||||
break; | break; | ||||
} | } | ||||
case GUIM_MOUSE_LEAVE: | case GUIM_MOUSE_LEAVE: | ||||
{ | { | ||||
if (m_Hovered == -1) | if (m_Hovered == -1) | ||||
break; | break; | ||||
SetSetting<i32>("hovered", -1, true); | m_Hovered.Set(-1, true); | ||||
ScriptEvent(EventNameHoverChange); | ScriptEvent(EventNameHoverChange); | ||||
break; | break; | ||||
} | } | ||||
case GUIM_MOUSE_OVER: | case GUIM_MOUSE_OVER: | ||||
{ | { | ||||
int hovered = GetHoveredItem(); | int hovered = GetHoveredItem(); | ||||
if (hovered == m_Hovered) | if (hovered == m_Hovered) | ||||
break; | break; | ||||
SetSetting<i32>("hovered", hovered, true); | m_Hovered.Set(hovered, true); | ||||
ScriptEvent(EventNameHoverChange); | ScriptEvent(EventNameHoverChange); | ||||
break; | break; | ||||
} | } | ||||
case GUIM_LOAD: | case GUIM_LOAD: | ||||
{ | { | ||||
GetScrollBar(0).SetScrollBarStyle(m_ScrollBarStyle); | GetScrollBar(0).SetScrollBarStyle(m_ScrollBarStyle); | ||||
break; | break; | ||||
▲ Show 20 Lines • Show All 101 Lines • ▼ Show 20 Lines | if (selected >= 0 && selected+1 < (int)m_ItemsYPositions.size()) | ||||
rect_sel.left < GetScrollBar(0).GetOuterRect().right) | rect_sel.left < GetScrollBar(0).GetOuterRect().right) | ||||
rect_sel.left = GetScrollBar(0).GetOuterRect().right; | rect_sel.left = GetScrollBar(0).GetOuterRect().right; | ||||
} | } | ||||
m_pGUI.DrawSprite(sprite_selectarea, bz + 0.05f, rect_sel); | m_pGUI.DrawSprite(sprite_selectarea, bz + 0.05f, rect_sel); | ||||
} | } | ||||
} | } | ||||
for (size_t i = 0; i < m_List.m_Items.size(); ++i) | for (size_t i = 0; i < m_List->m_Items.size(); ++i) | ||||
{ | { | ||||
if (m_ItemsYPositions[i+1] - scroll < 0 || | if (m_ItemsYPositions[i+1] - scroll < 0 || | ||||
m_ItemsYPositions[i] - scroll > rect.GetHeight()) | m_ItemsYPositions[i] - scroll > rect.GetHeight()) | ||||
continue; | continue; | ||||
// Clipping area (we'll have to substract the scrollbar) | // Clipping area (we'll have to substract the scrollbar) | ||||
CRect cliparea = GetListRect(); | CRect cliparea = GetListRect(); | ||||
Show All 14 Lines | void CList::DrawList(const int& selected, const CGUISpriteInstance& sprite, const CGUISpriteInstance& sprite_selectarea, const CGUIColor& textcolor) | ||||
if (m_ScrollBar) | if (m_ScrollBar) | ||||
IGUIScrollBarOwner::Draw(); | IGUIScrollBarOwner::Draw(); | ||||
} | } | ||||
void CList::AddItem(const CGUIString& str, const CGUIString& data) | void CList::AddItem(const CGUIString& str, const CGUIString& data) | ||||
{ | { | ||||
// Do not send a settings-changed message | // Do not send a settings-changed message | ||||
m_List.m_Items.push_back(str); | m_List.GetMutable().m_Items.push_back(str); | ||||
m_ListData.m_Items.push_back(data); | m_ListData.GetMutable().m_Items.push_back(data); | ||||
SetupText(true); | SetupText(true); | ||||
} | } | ||||
void CList::AddItem(const CGUIString& strAndData) | void CList::AddItem(const CGUIString& strAndData) | ||||
{ | { | ||||
AddItem(strAndData, strAndData); | AddItem(strAndData, strAndData); | ||||
} | } | ||||
Show All 10 Lines | if (child.GetNodeName() == elmt_item) | ||||
return true; | return true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
void CList::SelectNextElement() | void CList::SelectNextElement() | ||||
{ | { | ||||
if (m_Selected != static_cast<int>(m_List.m_Items.size()) - 1) | if (m_Selected != static_cast<int>(m_List->m_Items.size()) - 1) | ||||
{ | { | ||||
SetSetting<i32>("selected", m_Selected + 1, true); | m_Selected.Set(m_Selected + 1, true); | ||||
PlaySound(m_SoundSelected); | PlaySound(m_SoundSelected); | ||||
} | } | ||||
} | } | ||||
void CList::SelectPrevElement() | void CList::SelectPrevElement() | ||||
{ | { | ||||
if (m_Selected > 0) | if (m_Selected > 0) | ||||
{ | { | ||||
SetSetting<i32>("selected", m_Selected - 1, true); | m_Selected.Set(m_Selected - 1, true); | ||||
PlaySound(m_SoundSelected); | PlaySound(m_SoundSelected); | ||||
} | } | ||||
} | } | ||||
void CList::SelectFirstElement() | void CList::SelectFirstElement() | ||||
{ | { | ||||
if (m_Selected >= 0) | if (m_Selected >= 0) | ||||
SetSetting<i32>("selected", 0, true); | m_Selected.Set(0, true); | ||||
} | } | ||||
void CList::SelectLastElement() | void CList::SelectLastElement() | ||||
{ | { | ||||
const int index = static_cast<int>(m_List.m_Items.size()) - 1; | const int index = static_cast<int>(m_List->m_Items.size()) - 1; | ||||
if (m_Selected != index) | if (m_Selected != index) | ||||
SetSetting<i32>("selected", index, true); | m_Selected.Set(index, true); | ||||
} | } | ||||
void CList::UpdateAutoScroll() | void CList::UpdateAutoScroll() | ||||
{ | { | ||||
// No scrollbar, no scrolling (at least it's not made to work properly). | // No scrollbar, no scrolling (at least it's not made to work properly). | ||||
if (!m_ScrollBar || m_Selected < 0 || static_cast<std::size_t>(m_Selected) >= m_ItemsYPositions.size()) | if (!m_ScrollBar || m_Selected < 0 || static_cast<std::size_t>(m_Selected) >= m_ItemsYPositions.size()) | ||||
return; | return; | ||||
Show All 22 Lines | int CList::GetHoveredItem() | ||||
mouse.Y += scroll; | mouse.Y += scroll; | ||||
// Mouse is over scrollbar | // Mouse is over scrollbar | ||||
if (m_ScrollBar && GetScrollBar(0).IsVisible() && | if (m_ScrollBar && GetScrollBar(0).IsVisible() && | ||||
mouse.X >= GetScrollBar(0).GetOuterRect().left && | mouse.X >= GetScrollBar(0).GetOuterRect().left && | ||||
mouse.X <= GetScrollBar(0).GetOuterRect().right) | mouse.X <= GetScrollBar(0).GetOuterRect().right) | ||||
return -1; | return -1; | ||||
for (size_t i = 0; i < m_List.m_Items.size(); ++i) | for (size_t i = 0; i < m_List->m_Items.size(); ++i) | ||||
if (mouse.Y >= rect.top + m_ItemsYPositions[i] && | if (mouse.Y >= rect.top + m_ItemsYPositions[i] && | ||||
mouse.Y < rect.top + m_ItemsYPositions[i + 1]) | mouse.Y < rect.top + m_ItemsYPositions[i + 1]) | ||||
return i; | return i; | ||||
return -1; | return -1; | ||||
} | } |
Wildfire Games · Phabricator