Changeset View
Standalone View
source/gui/CList.cpp
Show All 26 Lines | |||||
#include "soundmanager/ISoundManager.h" | #include "soundmanager/ISoundManager.h" | ||||
CList::CList() | CList::CList() | ||||
: m_Modified(false), m_PrevSelectedItem(-1), m_LastItemClickTime(0) | : m_Modified(false), m_PrevSelectedItem(-1), m_LastItemClickTime(0) | ||||
{ | { | ||||
// Add sprite_disabled! TODO | // Add sprite_disabled! TODO | ||||
AddSetting(GUIST_float, "buffer_zone"); | AddSetting(GUIST_float, "buffer_zone"); | ||||
elexis: Either not change all these lines, or replace the tabs with exactly one space.
We don't want to… | |||||
AddSetting(GUIST_int, "cell_id"); | |||||
AddSetting(GUIST_CStrW, "font"); | AddSetting(GUIST_CStrW, "font"); | ||||
AddSetting(GUIST_int, "hovered"); // Index hovered. -1 is none. | |||||
Not Done Inline ActionsThis must be a separate commit, so that an error when changing the orderwouldnt have to be a bug in the hover patch elexis: This must be a separate commit, so that an error when changing the orderwouldnt have to be a… | |||||
// Each list item has both a name (in 'list') and an associated data string (in 'list_data') | |||||
AddSetting(GUIST_CGUIList, "list"); | |||||
AddSetting(GUIST_CGUIList, "list_data"); // TODO: this should be a list of raw strings, not of CGUIStrings | |||||
Done Inline ActionsTabs should only occur before the first non-whitespace character, I suggest to keep those 2 as, add L53 with tab and L57 without tab elexis: Tabs should only occur before the first non-whitespace character, I suggest to keep those 2 as… | |||||
AddSetting(GUIST_bool, "scrollbar"); | AddSetting(GUIST_bool, "scrollbar"); | ||||
AddSetting(GUIST_CStr, "scrollbar_style"); | AddSetting(GUIST_CStr, "scrollbar_style"); | ||||
AddSetting(GUIST_int, "selected"); // Index selected. -1 is none. | |||||
AddSetting(GUIST_CStrW, "sound_disabled"); | AddSetting(GUIST_CStrW, "sound_disabled"); | ||||
AddSetting(GUIST_CStrW, "sound_selected"); | AddSetting(GUIST_CStrW, "sound_selected"); | ||||
AddSetting(GUIST_CGUISpriteInstance, "sprite"); | AddSetting(GUIST_CGUISpriteInstance, "sprite"); | ||||
AddSetting(GUIST_CGUISpriteInstance, "sprite_selectarea"); | AddSetting(GUIST_CGUISpriteInstance, "sprite_selectarea"); | ||||
AddSetting(GUIST_int, "cell_id"); | |||||
AddSetting(GUIST_EAlign, "text_align"); | AddSetting(GUIST_EAlign, "text_align"); | ||||
AddSetting(GUIST_CColor, "textcolor"); | AddSetting(GUIST_CColor, "textcolor"); | ||||
AddSetting(GUIST_CColor, "textcolor_selected"); | AddSetting(GUIST_CColor, "textcolor_selected"); | ||||
AddSetting(GUIST_int, "selected"); // Index selected. -1 is none. | |||||
AddSetting(GUIST_CStrW, "tooltip"); | AddSetting(GUIST_CStrW, "tooltip"); | ||||
AddSetting(GUIST_CStr, "tooltip_style"); | AddSetting(GUIST_CStr, "tooltip_style"); | ||||
// Each list item has both a name (in 'list') and an associated data string (in 'list_data') | |||||
AddSetting(GUIST_CGUIList, "list"); | |||||
AddSetting(GUIST_CGUIList, "list_data"); // TODO: this should be a list of raw strings, not of CGUIStrings | |||||
GUI<int>::SetSetting(this, "hovered", -1); | |||||
GUI<bool>::SetSetting(this, "scrollbar", false); | GUI<bool>::SetSetting(this, "scrollbar", false); | ||||
// Nothing is selected as default. | |||||
GUI<int>::SetSetting(this, "selected", -1); | GUI<int>::SetSetting(this, "selected", -1); | ||||
// Add scroll-bar | // Add scroll-bar | ||||
CGUIScrollBarVertical* bar = new CGUIScrollBarVertical(); | CGUIScrollBarVertical* bar = new CGUIScrollBarVertical(); | ||||
bar->SetRightAligned(true); | bar->SetRightAligned(true); | ||||
AddScrollBar(bar); | AddScrollBar(bar); | ||||
} | } | ||||
CList::~CList() | CList::~CList() | ||||
▲ Show 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | case GUIM_MOUSE_PRESS_LEFT: | ||||
if (!enabled) | if (!enabled) | ||||
{ | { | ||||
CStrW soundPath; | CStrW soundPath; | ||||
if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_disabled", soundPath) == PSRETURN_OK && !soundPath.empty()) | if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_disabled", soundPath) == PSRETURN_OK && !soundPath.empty()) | ||||
g_SoundManager->PlayAsUI(soundPath.c_str(), false); | g_SoundManager->PlayAsUI(soundPath.c_str(), false); | ||||
break; | break; | ||||
} | } | ||||
bool scrollbar; | int hovered = GetHoveredItem(); | ||||
CGUIList* pList; | if (hovered == -1) | ||||
GUI<bool>::GetSetting(this, "scrollbar", scrollbar); | break; | ||||
GUI<CGUIList>::GetSettingPointer(this, "list", pList); | GUI<int>::SetSetting(this, "selected", hovered); | ||||
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)pList->m_Items.size(); ++i) | |||||
{ | |||||
if (mouse.y >= rect.top + m_ItemsYPositions[i] && | |||||
mouse.y < rect.top + m_ItemsYPositions[i+1] && | |||||
// mouse is not over scroll-bar | |||||
(!scrollbar || !GetScrollBar(0).IsVisible() || | |||||
mouse.x < GetScrollBar(0).GetOuterRect().left || | |||||
mouse.x > GetScrollBar(0).GetOuterRect().right)) | |||||
{ | |||||
set = i; | |||||
} | |||||
} | |||||
if (set != -1) | |||||
{ | |||||
GUI<int>::SetSetting(this, "selected", set); | |||||
UpdateAutoScroll(); | UpdateAutoScroll(); | ||||
CStrW soundPath; | CStrW soundPath; | ||||
if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_selected", soundPath) == PSRETURN_OK && !soundPath.empty()) | if (g_SoundManager && GUI<CStrW>::GetSetting(this, "sound_selected", soundPath) == PSRETURN_OK && !soundPath.empty()) | ||||
g_SoundManager->PlayAsUI(soundPath.c_str(), false); | g_SoundManager->PlayAsUI(soundPath.c_str(), false); | ||||
if (timer_Time() - m_LastItemClickTime < SELECT_DBLCLICK_RATE && set == m_PrevSelectedItem) | if (timer_Time() - m_LastItemClickTime < SELECT_DBLCLICK_RATE && hovered == m_PrevSelectedItem) | ||||
this->SendEvent(GUIM_MOUSE_DBLCLICK_LEFT_ITEM, "mouseleftdoubleclickitem"); | this->SendEvent(GUIM_MOUSE_DBLCLICK_LEFT_ITEM, "mouseleftdoubleclickitem"); | ||||
m_LastItemClickTime = timer_Time(); | m_LastItemClickTime = timer_Time(); | ||||
m_PrevSelectedItem = set; | m_PrevSelectedItem = hovered; | ||||
break; | |||||
Not Done Inline ActionsThis code has the exact same code flow, so the split and early break is correct elexis: This code has the exact same code flow, so the split and early break is correct | |||||
} | } | ||||
case GUIM_MOUSE_LEAVE: | |||||
{ | |||||
GUI<int>::SetSetting(this, "hovered", -1); | |||||
ScriptEvent("hoverchange"); | |||||
break; | |||||
} | |||||
case GUIM_MOUSE_OVER: | |||||
{ | |||||
GUI<int>::SetSetting(this, "hovered", GetHoveredItem()); | |||||
Not Done Inline ActionsThis sends a GUIM_SETTINGS_UPDATED message upon each mouse move. But we only need one if the setting changed actually. I'm adding early returns so that the setting is changed only upon actual change. The message still arrives twice, but it's not our code doing so. elexis: This sends a `GUIM_SETTINGS_UPDATED` message upon each mouse move. But we only need one if the… | |||||
ScriptEvent("hoverchange"); | |||||
break; | break; | ||||
} | } | ||||
case GUIM_LOAD: | case GUIM_LOAD: | ||||
{ | { | ||||
CStr scrollbar_style; | CStr scrollbar_style; | ||||
GUI<CStr>::GetSetting(this, "scrollbar_style", scrollbar_style); | GUI<CStr>::GetSetting(this, "scrollbar_style", scrollbar_style); | ||||
GetScrollBar(0).SetScrollBarStyle(scrollbar_style); | GetScrollBar(0).SetScrollBarStyle(scrollbar_style); | ||||
Show All 24 Lines | case SDLK_HOME: | ||||
break; | break; | ||||
case SDLK_END: | case SDLK_END: | ||||
SelectLastElement(); | SelectLastElement(); | ||||
UpdateAutoScroll(); | UpdateAutoScroll(); | ||||
result = IN_HANDLED; | result = IN_HANDLED; | ||||
break; | break; | ||||
case SDLK_UP: | case SDLK_UP: | ||||
Done Inline ActionsAlmost the entire block was copy & pasted from GUIM_MOUSE_PRESS_LEFT, better make a private helper member that returns the currently hovered item (moving stuff above the switch might work too but had the disadvantage of exposing variables to the other cases that are not used by those cases) elexis: Almost the entire block was copy & pasted from `GUIM_MOUSE_PRESS_LEFT`, better make a private… | |||||
SelectPrevElement(); | SelectPrevElement(); | ||||
UpdateAutoScroll(); | UpdateAutoScroll(); | ||||
result = IN_HANDLED; | result = IN_HANDLED; | ||||
break; | break; | ||||
case SDLK_DOWN: | case SDLK_DOWN: | ||||
SelectNextElement(); | SelectNextElement(); | ||||
UpdateAutoScroll(); | UpdateAutoScroll(); | ||||
▲ Show 20 Lines • Show All 225 Lines • ▼ Show 20 Lines | return; // this means, if it wants to align both up and down at the same time | ||||
// this will have precedence. | // this will have precedence. | ||||
} | } | ||||
// Check lower boundary | // Check lower boundary | ||||
CRect rect = GetListRect(); | CRect rect = GetListRect(); | ||||
if (m_ItemsYPositions[selected+1]-rect.GetHeight() > scroll) | if (m_ItemsYPositions[selected+1]-rect.GetHeight() > scroll) | ||||
GetScrollBar(0).SetPos(m_ItemsYPositions[selected+1]-rect.GetHeight()); | GetScrollBar(0).SetPos(m_ItemsYPositions[selected+1]-rect.GetHeight()); | ||||
} | } | ||||
int CList::GetHoveredItem() | |||||
elexisAuthorUnsubmitted Done Inline ActionsThanks, looks much better without the duplication! elexis: Thanks, looks much better without the duplication! | |||||
{ | |||||
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; | |||||
for (int i = 0; i < (int)pList->m_Items.size(); ++i) | |||||
elexisAuthorUnsubmitted Done Inline Actionsis the conversion needed? elexis: is the conversion needed? | |||||
if (mouse.y >= rect.top + m_ItemsYPositions[i] && | |||||
Not Done Inline Actionsscroll-bar -> scrollbar elexis: scroll-bar -> scrollbar | |||||
mouse.y < rect.top + m_ItemsYPositions[i + 1] && | |||||
elexisAuthorUnsubmitted Done Inline Actionssame amount of tabs as the if ( then spaces. elexis: same amount of tabs as the `if (` then spaces. | |||||
elexisAuthorUnsubmitted Not Done Inline Actions(The way the for-loop does it, not the way this if statement does it, according to other people at least) elexis: (The way the for-loop does it, not the way this if statement does it, according to other people… | |||||
// mouse is not over scroll-bar | |||||
(!scrollbar || !GetScrollBar(0).IsVisible() || | |||||
mouse.x < GetScrollBar(0).GetOuterRect().left || | |||||
mouse.x > GetScrollBar(0).GetOuterRect().right)) | |||||
{ | |||||
return i; | |||||
} | |||||
return -1; | |||||
} |
Either not change all these lines, or replace the tabs with exactly one space.
We don't want to align things because of the same issue you run into:
When adding one more item that is longer than the rest, every line has to be aligned differently again.