Commit 34442605 authored by Romain Bignon's avatar Romain Bignon

better support of scrollbars

parent 81f0dd4a
......@@ -307,13 +307,13 @@ int LSPmsCommand::Exec(PlayerList players, EC_Client *me, ParvList parv)
ListServerForm->ServerList->AddItem(false,
StringF("%3d %-23s %2s %3s/%-3s %3s/%-3s %-3s", SDL_GetTicks()-t0, parv[2].substr(0,23).c_str(), parv[9].c_str(),
parv[4].c_str(), parv[5].c_str(), parv[6].c_str(), parv[7].c_str(), parv[8].c_str()),
parv[1], fgreen_color, true, *Font::GetInstance(Font::Normal), parv[2]);
parv[1], fgreen_color, true, parv[2]);
else
ListServerForm->ServerList->AddItem(false,
StringF("%4d %-27s %2s %3s/%-3s %3s/%-3s %-3s", SDL_GetTicks()-t0, parv[2].c_str(), parv[9].c_str(),
parv[4].c_str(), parv[5].c_str(), parv[6].c_str(), parv[7].c_str(), parv[8].c_str()),
parv[1], (parv[3][0] == '+' && parv[9] == APP_PVERSION) ? white_color : red_color,
(parv[3][0] == '+' && parv[9] == APP_PVERSION), *Font::GetInstance(Font::Small), parv[2]);
(parv[3][0] == '+' && parv[9] == APP_PVERSION), parv[2]);
ListServerForm->nb_chans += StrToTyp<uint>(parv[6]);
ListServerForm->nb_wchans += StrToTyp<uint>(parv[8]);
......@@ -1070,7 +1070,7 @@ int SCOREmsCommand::Exec(PlayerList players, EC_Client *me, ParvList parv)
if(GlobalScoresForm)
{
if(GlobalScoresForm->ListBox->Empty())
GlobalScoresForm->BestPlayer->SetCaption(StringF(_("And the better player is.... %s with %s points!"),
GlobalScoresForm->BestPlayer->SetCaption(StringF(_("And the best player is.... %s with %s points!"),
parv[1].c_str(), parv[5].c_str()));
int defeats = StrToTyp<int>(parv[7]) - StrToTyp<int>(parv[8]);
......@@ -1082,7 +1082,7 @@ int SCOREmsCommand::Exec(PlayerList players, EC_Client *me, ParvList parv)
if(GlobalScoresForm->best_incomes < StrToTyp<int>(parv[6]))
{
GlobalScoresForm->best_incomes = StrToTyp<int>(parv[6]);
GlobalScoresForm->BestIncomes->SetCaption(StringF(_("The one who incomes the must of money is %s with $%s per turn!"),
GlobalScoresForm->BestIncomes->SetCaption(StringF(_("The one who won the most money is %s with $%s per turn!"),
parv[1].c_str(), parv[6].c_str()));
}
if(GlobalScoresForm->best_kills < StrToTyp<int>(parv[3]))
......
......@@ -26,8 +26,8 @@
#include <assert.h>
TComboBox::TComboBox(Font* f, int _x, int _y, uint _width)
: TListBox(Rectanglei(_x, _y, _width, f->GetHeight())), real_y(_y), opened(false), visible_len(0),
chaine(_x+5, _y, "", white_color, f), COMBOBOX_HEIGHT(f->GetHeight()), font(f)
: TListBox(Rectanglei(_x, _y, _width, f->GetHeight()), true, f), real_y(_y), opened(false), visible_len(0),
chaine(_x+5, _y, "", white_color, f), COMBOBOX_HEIGHT(f->GetHeight())
{
}
......@@ -91,10 +91,9 @@ TListBoxItem* TComboBox::AddItem (bool selected,
const std::string &value,
const Color& color,
bool enabled,
Font& font,
const std::string& name)
{
TListBoxItem* item = TListBox::AddItem(selected, label, value, color, enabled, font, name);
TListBoxItem* item = TListBox::AddItem(selected, label, value, color, enabled, name);
item->SetGrayDisable();
if(opened)
size.y = CalculateHeight();
......@@ -130,16 +129,24 @@ bool TComboBox::Clic (const Point2i& mouse, int button)
{
if(!Enabled()) return false;
bool r = false;
bool r = false, want_close = false;
if(opened && MouseIsOnWhichItem(mouse) != -1)
{
want_close = true;
r = true;
}
if(opened)
r = TListBox::Clic(mouse, button);
else if(Mouse(mouse)) // We can clic everywhere on the label and the button to open
SetOpened(!opened), r = true;
if(opened && MouseIsOnWhichItem(mouse) != -1)
SetOpened(false), r = true;
else if(Mouse(mouse))
{
// We can clic everywhere on the label and the button to open
SetOpened(!opened);
r = true;
}
if(!r && opened && !Mouse(mouse))
if(opened && (want_close || (!r && !Mouse(mouse))))
SetOpened(false), r = true;
return r;
......
......@@ -53,7 +53,6 @@ public:
const std::string &value,
const Color& _color = white_color,
bool _enabled = true,
Font& font = *Font::GetInstance(Font::Small),
const std::string& name = "");
void ClearItems();
virtual void Deselect (uint index);
......@@ -78,7 +77,6 @@ protected:
void SetOpened(bool _o);
TLabel chaine;
uint COMBOBOX_HEIGHT;
Font* font;
int CalculateHeight();
};
......
......@@ -45,8 +45,8 @@
#define SCROLLBAR
TListBoxItem::TListBoxItem(const std::string& _name, const std::string& _label, Font& _font, const std::string& _value, const Color& color)
: TLabel(0,0, _label, color, &_font), value(_value), name(_name)
TListBoxItem::TListBoxItem(const std::string& _name, const std::string& _label, Font* _font, const std::string& _value, const Color& color)
: TLabel(0,0, _label, color, _font), value(_value), name(_name)
{
}
......@@ -66,20 +66,21 @@ const std::string& TListBoxItem::Name() const
return name;
}
// struct CompareItems
// {
// bool operator()(const ListBoxItem& a, const ListBoxItem& b)
// {
// return a.GetLabel() < b.GetLabel();
// }
// };
TListBox::TListBox (const Rectanglei &rect, bool always_one_selected_b)
: TComponent(rect), no_item_hint(false), scrolling(false), box_color(BoxColor), on_change(0), gray_disable(true)
struct CompareItems
{
bool operator()(const TListBoxItem* a, const TListBoxItem* b)
{
return a->Label() < b->Label();
}
};
TListBox::TListBox (const Rectanglei &rect, bool always_one_selected_b, Font* _font)
: TComponent(rect), no_item_hint(false), scrolling_delta(-1), box_color(BoxColor), on_change(0), gray_disable(true), font(_font)
{
first_visible_item = 0;
selected_item = -1;
always_one_selected = always_one_selected_b;
nb_visible_items_max = Height() / font->GetHeight();
}
void TListBox::Init()
......@@ -120,7 +121,7 @@ int TListBox::MouseIsOnWhichItem(const Point2i &mousePosition) const
void TListBox::ClicUp (const Point2i& pos, int button)
{
scrolling = false;
scrolling_delta = -1;
}
bool TListBox::Clic(const Point2i &mousePosition, int button)
......@@ -131,7 +132,7 @@ bool TListBox::Clic(const Point2i &mousePosition, int button)
return false;
if(button == SDL_BUTTON_LEFT && ScrollBarPos().Contains(mousePosition))
scrolling = true;
scrolling_delta = mousePosition.y - ScrollBarPos().Y();
// buttons for listbox with more items than visible (first or last item not visible)
if(m_down.Visible())
......@@ -140,7 +141,7 @@ bool TListBox::Clic(const Point2i &mousePosition, int button)
if(button == SDL_BUTTON_WHEELDOWN || (button == SDL_BUTTON_LEFT && m_down.Mouse(mousePosition)))
{
// bottom button
if( first_visible_item+1 < m_items.size() /*m_items.back()->Y() + m_items.back()->Height() > Y() + Height()*/ )
if( first_visible_item < (m_items.size() - nb_visible_items_max) /*m_items.back()->Y() + m_items.back()->Height() > Y() + Height()*/ )
first_visible_item++ ;
return true;
......@@ -180,18 +181,20 @@ void TListBox::Draw(const Point2i &mousePosition)
{
int item = MouseIsOnWhichItem(mousePosition);
Rectanglei rect (X(), Y(), Width()-12, Height());
Rectanglei scrollbar = ScrollBarPos();
// Draw border and bg color
Window()->BoxColor(rect, box_color);
if(scrolling)
if(scrolling_delta >= 0)
{
if(mousePosition.y > Y() + Height() - 12)
first_visible_item = m_items.size()-1;
else if(mousePosition.y < Y() + 12)
int y = mousePosition.y - scrolling_delta;
if(y + scrollbar.Height() > Y() + Height() - 12)
first_visible_item = m_items.size() - nb_visible_items_max;
else if(y < Y() + 12)
first_visible_item = 0;
else
first_visible_item = (mousePosition.y - Y() - 10) * m_items.size() / (Height()-20);
first_visible_item = (y - Y() - 10) * m_items.size() / (Height()-20);
}
// Draw items
......@@ -243,10 +246,8 @@ void TListBox::Draw(const Point2i &mousePosition)
m_down.Show();
m_up.Draw(mousePosition);
m_down.Draw(mousePosition);
#ifdef SCROLLBAR
Rectanglei scrollbar = ScrollBarPos();
Window()->BoxColor(scrollbar, (scrolling || scrollbar.Contains(mousePosition)) ? white_color : gray_color);
#endif
Window()->BoxColor(scrollbar, (scrolling_delta >= 0 || scrollbar.Contains(mousePosition)) ? white_color : gray_color);
Window()->RectangleColor(scrollbar, /*(scrolling_delta >= 0 || scrollbar.Contains(mousePosition)) ? gray_color : */white_color);
}
else
m_down.Hide();
......@@ -255,12 +256,15 @@ void TListBox::Draw(const Point2i &mousePosition)
Rectanglei TListBox::ScrollBarPos() const
{
if(m_items.empty())
return Rectanglei(0,0,0,0);
uint tmp_y, tmp_h;
tmp_y = Y()+10+ first_visible_item* (Height()-20) / m_items.size();
tmp_h = /*nb_visible_items_max * */(Height()-20) / m_items.size();
tmp_h = nb_visible_items_max * (Height()-20) / m_items.size();
if (tmp_h < 5) tmp_h =5;
return Rectanglei(X()+Width()-11, tmp_y, 9, /*tmp_y+*/tmp_h);
return Rectanglei(X()+Width()-11, tmp_y, 9, tmp_h);
}
TListBoxItem* TListBox::AddItem (bool selected,
......@@ -268,7 +272,7 @@ TListBoxItem* TListBox::AddItem (bool selected,
const std::string &value,
const Color& color,
bool enabled,
Font& font, const std::string& name)
const std::string& name)
{
uint pos = m_items.size();
......@@ -290,44 +294,31 @@ TListBoxItem* TListBox::AddItem (bool selected,
void TListBox::Sort()
{
//std::sort( m_items.begin(), m_items.end(), CompareItems() );
std::sort( m_items.begin(), m_items.end(), CompareItems() );
SetWantRedraw();
}
void TListBox::ScrollTo(TListBoxItem* item)
{
uint i = 0;
for(std::vector<TListBoxItem*>::const_iterator it = m_items.begin(); it != m_items.end() && *it != item; ++it) i++;
for(std::vector<TListBoxItem*>::const_iterator it = m_items.begin(); it != m_items.end() && *it != item; ++it)
i++;
ScrollTo(i);
}
/* TODO améliorer ce code moche */
void TListBox::ScrollTo(uint id)
{
if(id == first_visible_item || id >= m_items.size())
return;
first_visible_item = id;
/*else if(id > first_visible_item)
while(1)
{
if(m_items.back()->Y() + m_items.back()->Height() > Y() + Height())
first_visible_item++;
else
break;
if(first_visible_item == id)
break;
}
if (nb_visible_items_max > m_items.size())
first_visible_item = 0;
else if (id > (m_items.size() - nb_visible_items_max/2))
first_visible_item = m_items.size() - nb_visible_items_max;
else if (id < nb_visible_items_max/2)
first_visible_item = 0;
else
while(1)
{
if(first_visible_item > 0)
first_visible_item--;
else
break;
if(first_visible_item == id)
break;
}*/
first_visible_item = id - nb_visible_items_max/2;
SetWantRedraw();
}
......
......@@ -58,7 +58,7 @@ private:
std::string name;
public:
TListBoxItem(const std::string& _name, const std::string& _label, Font& _font, const std::string& value,
TListBoxItem(const std::string& _name, const std::string& _label, Font* _font, const std::string& value,
const Color& color = white_color);
const std::string& Label() const;
......@@ -70,7 +70,7 @@ class TListBox : public TComponent
{
/* Constructeurs */
public:
TListBox (const Rectanglei &rect, bool always_one_selected_b = true);
TListBox (const Rectanglei &rect, bool always_one_selected_b = true, Font* font = Font::GetInstance(Font::Small));
~TListBox();
typedef void (*OnChangeFunc) (TListBox*);
......@@ -89,7 +89,7 @@ public:
TListBoxItem* AddItem(bool selected, const std::string &label,
const std::string &value,
const Color& color = white_color, bool enabled = true,
Font& font = *Font::GetInstance(Font::Small), const std::string& name = "");
const std::string& name = "");
void Sort();
......@@ -142,9 +142,13 @@ public:
/* Variables privées */
private:
void Recalculate();
uint nb_visible_items_max;
uint total_height;
bool always_one_selected;
bool no_item_hint;
bool scrolling;
int scrolling_delta;
Point2i scroll_point;
protected:
......@@ -161,6 +165,7 @@ protected:
OnChangeFunc on_change;
bool gray_disable;
Font* font;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment