From db519a7036f18b026cde23dc8c588776c0d35f1f Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Tue, 14 Aug 2012 14:22:41 +0100 Subject: [PATCH] Varying borders for button, exit dropdown menu with Esc or outside click, fixes #100 --- src/interface/Appearance.cpp | 1 + src/interface/Appearance.h | 2 ++ src/interface/Border.h | 7 +++++- src/interface/Button.cpp | 14 ++++++++++- src/interface/DropDown.cpp | 45 ++++++++++++++++++++++++++---------- src/interface/DropDown.h | 2 ++ 6 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/interface/Appearance.cpp b/src/interface/Appearance.cpp index 45c3592a0..f2f562c62 100644 --- a/src/interface/Appearance.cpp +++ b/src/interface/Appearance.cpp @@ -30,6 +30,7 @@ namespace ui BorderDisabled(100, 100, 100), Margin(1, 4), + Border(1), icon(NoIcon), diff --git a/src/interface/Appearance.h b/src/interface/Appearance.h index e6f0a93be..b87626930 100644 --- a/src/interface/Appearance.h +++ b/src/interface/Appearance.h @@ -48,6 +48,8 @@ namespace ui ui::Colour BorderDisabled; ui::Border Margin; + + ui::Border Border; Icon icon; diff --git a/src/interface/Border.h b/src/interface/Border.h index b5ae5058c..a1ceb81e1 100644 --- a/src/interface/Border.h +++ b/src/interface/Border.h @@ -40,10 +40,15 @@ namespace ui Left(left) { } + + inline bool operator == (const int& v) const + { + return (Top == v && Right == v && Bottom == v && Left == v); + } inline bool operator == (const Border& v) const { - return (Top == v.Top || Right == v.Right || Bottom == v.Bottom || Left == v.Left); + return (Top == v.Top && Right == v.Right && Bottom == v.Bottom && Left == v.Left); } inline bool operator != (const Border& v) const diff --git a/src/interface/Button.cpp b/src/interface/Button.cpp index c6ebd79d1..9d7a6fcfe 100644 --- a/src/interface/Button.cpp +++ b/src/interface/Button.cpp @@ -121,7 +121,19 @@ void Button::Draw(const Point& screenPos) bgColour = Appearance.BackgroundInactive; g->fillrect(Position.X+1, Position.Y+1, Size.X-2, Size.Y-2, backgroundColour.Red, backgroundColour.Green, backgroundColour.Blue, backgroundColour.Alpha); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + if(Appearance.Border == 1) + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + else + { + if(Appearance.Border.Top) + g->draw_line(Position.X, Position.Y, Position.X+Size.X-1, Position.Y, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + if(Appearance.Border.Bottom) + g->draw_line(Position.X, Position.Y+Size.Y-1, Position.X+Size.X-1, Position.Y+Size.Y-1, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + if(Appearance.Border.Left) + g->draw_line(Position.X, Position.Y, Position.X, Position.Y+Size.Y-1, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + if(Appearance.Border.Right) + g->draw_line(Position.X+Size.X-1, Position.Y, Position.X+Size.X-1, Position.Y+Size.Y-1, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + } g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, buttonDisplayText, textColour.Red, textColour.Green, textColour.Blue, textColour.Alpha); bool iconInvert = (backgroundColour.Blue + (3*backgroundColour.Green) + (2*backgroundColour.Red))>544?true:false; diff --git a/src/interface/DropDown.cpp b/src/interface/DropDown.cpp index d8bb87ca5..580d7bbee 100644 --- a/src/interface/DropDown.cpp +++ b/src/interface/DropDown.cpp @@ -43,16 +43,17 @@ public: { Button * tempButton = new Button(Point(1, currentY), Point(Size.X-2, 16), dropDown->options[i].first); tempButton->Appearance = appearance; + if(i) + tempButton->Appearance.Border = ui::Border(0, 1, 1, 1); tempButton->SetActionCallback(new ItemSelectedAction(this, dropDown->options[i].first)); AddComponent(tempButton); - currentY += 15; + currentY += 16; } } virtual void OnDraw() { Graphics * g = ui::Engine::Ref().g; - g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 100, 100, 100, 255); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, appearance.BackgroundInactive.Red, appearance.BackgroundInactive.Green, appearance.BackgroundInactive.Blue, appearance.BackgroundInactive.Alpha); + g->clearrect(Position.X, Position.Y, Size.X, Size.Y); } void setOption(std::string option) { @@ -68,6 +69,10 @@ public: dropDown->callback->OptionChanged(dropDown, dropDown->options[optionIndex]); } } + virtual void OnTryExit(ExitMethod method) + { + SelfDestruct(); + } virtual ~DropDownWindow() {} }; @@ -95,23 +100,39 @@ void DropDown::Draw(const Point& screenPos) } Graphics * g = ui::Engine::Ref().g; Point Position = screenPos; - if(isMouseInside) + + ui::Colour textColour = Appearance.TextInactive; + ui::Colour borderColour = Appearance.BorderInactive; + ui::Colour backgroundColour = Appearance.BackgroundInactive; + + if (isMouseInside) { - g->fillrect(Position.X-1, Position.Y-1, Size.X+2, Size.Y+2, Appearance.BackgroundActive.Red, Appearance.BackgroundActive.Green, Appearance.BackgroundActive.Blue, 255); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BorderActive.Red, Appearance.BorderActive.Green, Appearance.BorderActive.Blue, 255); - if(optionIndex!=-1) - g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, Appearance.TextActive.Red, Appearance.TextActive.Green, Appearance.TextActive.Blue, 255); + textColour = Appearance.TextHover; + borderColour = Appearance.BorderHover; + backgroundColour = Appearance.BackgroundHover; } else { - g->fillrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BackgroundInactive.Red, Appearance.BackgroundInactive.Green, Appearance.BackgroundInactive.Blue, 255); - g->drawrect(Position.X, Position.Y, Size.X, Size.Y, Appearance.BorderInactive.Red, Appearance.BorderInactive.Green, Appearance.BorderInactive.Blue, 255); - if(optionIndex!=-1) - g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, Appearance.TextInactive.Red, Appearance.TextInactive.Green, Appearance.TextInactive.Blue, 255); + textColour = Appearance.TextInactive; + borderColour = Appearance.BorderInactive; + backgroundColour = Appearance.BackgroundInactive; } + g->fillrect(Position.X-1, Position.Y-1, Size.X+2, Size.Y+2, backgroundColour.Red, backgroundColour.Green, backgroundColour.Blue, backgroundColour.Alpha); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, borderColour.Red, borderColour.Green, borderColour.Blue, borderColour.Alpha); + if(optionIndex!=-1) + g->drawtext(Position.X+textPosition.X, Position.Y+textPosition.Y, options[optionIndex].first, textColour.Red, textColour.Green, textColour.Blue, textColour.Alpha); } +void DropDown::OnMouseEnter(int x, int y) +{ + isMouseInside = true; +} + +void DropDown::OnMouseLeave(int x, int y) +{ + isMouseInside = false; +} std::pair DropDown::GetOption() { if(optionIndex!=-1) diff --git a/src/interface/DropDown.h b/src/interface/DropDown.h index 813c035e3..3aba97156 100644 --- a/src/interface/DropDown.h +++ b/src/interface/DropDown.h @@ -39,6 +39,8 @@ public: void SetActionCallback(DropDownAction * action) { callback = action;} virtual void Draw(const Point& screenPos); virtual void OnMouseClick(int x, int y, unsigned int button); + virtual void OnMouseEnter(int x, int y); + virtual void OnMouseLeave(int x, int y); virtual ~DropDown(); };