Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
13 KB
Referenced Files
None
Subscribers
None
diff --git a/util/gui/scroll-list.cpp b/util/gui/scroll-list.cpp
index 7118992a..cd82a70c 100644
--- a/util/gui/scroll-list.cpp
+++ b/util/gui/scroll-list.cpp
@@ -1,208 +1,287 @@
#include "../bitmap.h"
#include "../trans-bitmap.h"
+#include "../funcs.h"
#include "scroll-list.h"
#include "../font.h"
#include <math.h>
#include "../debug.h"
namespace Gui{
static const double FONT_SPACER = 1.3;
// static const int GradientMax = 50;
const double EPSILON = 0.01;
/*
static int selectedGradientStart(){
static int color = Graphics::makeColor(19, 167, 168);
return color;
}
static int selectedGradientEnd(){
static int color = Graphics::makeColor(27, 237, 239);
return color;
}
*/
+
+int justify(Justify justification, int left, int right, int size){
+ switch (justification){
+ case LeftJustify: return left;
+ case RightJustify: return right - size;
+ case CenterJustify: return (left + right) / 2 - size / 2;
+ }
+ return 0;
+}
+
+ScrollListInterface::~ScrollListInterface(){
+}
ScrollItem::ScrollItem(){
}
ScrollItem::~ScrollItem(){
}
ScrollList::ScrollList():
currentIndex(0),
fontSpacingX(0),
fontSpacingY(0),
currentPosition(0),
scrollWait(0),
scrollWaitTime(4),
scrollMotion(1.2),
// selectedGradient(GradientMax, selectedGradientStart(), selectedGradientEnd()),
// useGradient(false),
useHighlight(false),
allowWrap(true),
scroll(0),
justification(CenterJustify){}
ScrollList::ScrollList(const ScrollList & copy):
currentIndex(copy.currentIndex),
fontSpacingX(copy.fontSpacingX),
fontSpacingY(copy.fontSpacingY),
currentPosition(copy.currentPosition),
scrollWait(copy.scrollWait),
// selectedGradient(GradientMax, selectedGradientStart(), selectedGradientEnd()),
// useGradient(copy.useGradient),
useHighlight(copy.useHighlight),
allowWrap(true),
scroll(0){}
ScrollList::~ScrollList(){}
ScrollList & ScrollList::operator=(const ScrollList & copy){
return *this;
}
void ScrollList::act(){
if (scrollWait == 0){
if (scroll > EPSILON){
// scroll -= SCROLL_MOTION;
scroll /= scrollMotion;
} else if (scroll < -EPSILON){
// scroll += SCROLL_MOTION;
scroll /= scrollMotion;
} else {
scroll = 0;
currentPosition = currentIndex;
}
} else {
scrollWait -= 1;
}
/*
if (scrollWait > 0){
scrollWait -= 1;
} else {
currentPosition = currentIndex;
}
*/
}
-int ScrollList::justify(int left, int right, int size) const {
- switch (justification){
- case LeftJustify: return left;
- case RightJustify: return right - size;
- case CenterJustify: return (left + right) / 2 - size / 2;
- }
- return 0;
-}
/* this is the smooth scroll stuff from context-box */
void ScrollList::doDraw(int x, int y, int min_y, int max_y, const Font & font, int current, int selected, const Graphics::Bitmap & area, int direction) const {
/* sanity check */
if (text.size() == 0){
return;
}
while (y < max_y && y > min_y){
/* circuluar */
int pick = current;
while (pick < 0){
pick += text.size();
}
pick = pick % text.size();
Util::ReferenceCount<ScrollItem> option = text[pick];
/* center justification */
- const int startx = justify(1, area.getWidth() - 1, option->size(font));
+ const int startx = justify(justification, 1, area.getWidth() - 1, option->size(font));
/* the selected option will have a distance of 0 */
int distance = current - selected;
option->draw(x + startx, y, area, font, distance);
if (text.size() == 1){
return;
}
current += direction;
y += direction * font.getHeight() / FONT_SPACER;
}
}
void ScrollList::render(const Graphics::Bitmap & where, const Font & font) const {
/* middle of the bitmap offset by the scroll amount. */
int y = where.getHeight() / 2 + scroll * font.getHeight() / 2 - font.getHeight() / 2;
/* allow options to be drawn a little off the bitmap */
int min_y = 0 - font.getHeight() / FONT_SPACER;
int max_y = where.getHeight();
/* draw down starting from the current selection */
doDraw(0, y, min_y, max_y, font, currentIndex, currentIndex, where, 1);
/* then draw up, skipping the current selection */
doDraw(0, y - font.getHeight() / FONT_SPACER, min_y, max_y, font, currentIndex - 1, currentIndex, where, -1);
}
void ScrollList::addItem(const Util::ReferenceCount<ScrollItem> & text){
this->text.push_back(text);
}
void ScrollList::addItems(const std::vector<Util::ReferenceCount<ScrollItem> > & texts){
this->text.insert(text.end(), texts.begin(), texts.end());
}
void ScrollList::clearItems(){
this->text.clear();
}
void ScrollList::setPosition(const Gui::Coordinate & location){
this->position = location;
}
bool ScrollList::next(){
/* FIXME: probably if the current index goes past the boundary we shouldn't scroll */
currentIndex++;
scroll = 1;
if (scrollWait == 0){
scrollWait = scrollWaitTime;
}
if (currentIndex >= text.size()){
if (allowWrap){
currentIndex = 0;
return true;
} else {
currentIndex = text.size()-1;
return false;
}
}
return true;
}
bool ScrollList::previous(){
scroll = -1;
if (scrollWait == 0){
scrollWait = scrollWaitTime;
}
if (currentIndex > 0){
currentIndex--;
} else if (currentIndex == 0){
if (allowWrap){
currentIndex = text.size()-1;
return true;
} else {
return false;
}
}
return true;
}
bool ScrollList::setCurrentIndex(unsigned int index){
if (index >= text.size()){
return false;
}
currentIndex = index;
return true;
}
+NormalList::NormalList():
+position(0),
+first(-1),
+last(-1),
+justification(CenterJustify){
+}
+
+NormalList::~NormalList(){
+}
+
+void NormalList::act(){
+}
+
+void NormalList::render(const Graphics::Bitmap & work, const Font & font) const {
+ int maxItems = work.getHeight() / (font.getHeight() + FONT_SPACER);
+ if (first == -1){
+ first = 0;
+ last = Util::min(maxItems, text.size());
+ }
+ if (position < first){
+ int difference = first - position;
+ first = 0;
+ last -= difference;
+ }
+ if (position > last){
+ int difference = position - last;
+ last = position;
+ first += difference;
+ }
+ first = 0;
+ last = text.size();
+
+ double y = 0;
+ for (int index = first; index < last; index += 1){
+ int distance = position - index;
+ Util::ReferenceCount<ScrollItem> option = text[index];
+ const int x = justify(justification, 1, work.getWidth() - 1, option->size(font));
+ option->draw(x, y, work, font, distance);
+ y += font.getHeight() / FONT_SPACER;
+ }
+}
+
+void NormalList::addItem(const Util::ReferenceCount<ScrollItem> & item){
+ text.push_back(item);
+}
+
+void NormalList::addItems(const std::vector<Util::ReferenceCount<ScrollItem> > & texts){
+ this->text.insert(text.end(), texts.begin(), texts.end());
+}
+
+void NormalList::clearItems(){
+ this->text.clear();
+}
+
+unsigned int NormalList::getCurrentIndex() const {
+ return position;
+}
+
+bool NormalList::next(){
+ if (position < text.size() - 1){
+ position += 1;
+ return true;
+ }
+ return false;
+}
+
+bool NormalList::previous(){
+ if (position > 0){
+ position -= 1;
+ return true;
+ }
+ return false;
+}
+
}
diff --git a/util/gui/scroll-list.h b/util/gui/scroll-list.h
index 1893a7c8..0b8cd903 100644
--- a/util/gui/scroll-list.h
+++ b/util/gui/scroll-list.h
@@ -1,197 +1,258 @@
#ifndef _gui_scroll_list_h
#define _gui_scroll_list_h
#include <string>
#include <vector>
#include "coordinate.h"
#include "../pointer.h"
class Font;
namespace Gui{
+enum Justify{
+ LeftJustify,
+ RightJustify,
+ CenterJustify
+};
+
/* pure virtual class */
class ScrollItem{
public:
ScrollItem();
/* distance is how far from the selected item we are. 0 means we are the selected item.
* distance can be positive or negative.
*/
virtual void draw(int x, int y, const Graphics::Bitmap & where, const Font & font, int distance) const = 0;
/* size in pixels, used for justification */
virtual int size(const Font & font) const = 0;
virtual ~ScrollItem();
};
-class ScrollList {
+/* pure virtual */
+class ScrollListInterface{
+public:
+ virtual ~ScrollListInterface();
+
+ //! Logic
+ virtual void act() = 0;
+
+ //! Render
+ virtual void render(const Graphics::Bitmap &, const Font & font) const = 0;
+
+ //! Add item
+ virtual void addItem(const Util::ReferenceCount<ScrollItem> & item) = 0;
+
+ //! Add vector of text
+ virtual void addItems(const std::vector<Util::ReferenceCount<ScrollItem> > & items) = 0;
+
+ virtual void clearItems() = 0;
+
+ virtual unsigned int getCurrentIndex() const = 0;
+
+ virtual bool next() = 0;
+
+ //! Previous
+ virtual bool previous() = 0;
+
+};
+
+class ScrollList: public ScrollListInterface {
public:
ScrollList();
ScrollList(const ScrollList &);
virtual ~ScrollList();
- enum Justify{
- LeftJustify,
- RightJustify,
- CenterJustify
- };
-
//! copy
ScrollList & operator=(const ScrollList &);
//! Logic
virtual void act();
//! Render
virtual void render(const Graphics::Bitmap &, const Font & font) const;
//! Add item
virtual void addItem(const Util::ReferenceCount<ScrollItem> & item);
//! Add vector of text
virtual void addItems(const std::vector<Util::ReferenceCount<ScrollItem> > & items);
virtual void clearItems();
//! Set Position
virtual void setPosition(const Gui::Coordinate &);
//! Set font spacing
inline virtual void setFontSpacing(int x, int y){
this->fontSpacingX = x;
this->fontSpacingY = y;
}
//! Set font spacing X
inline virtual void setFontSpacingX(int x){
this->fontSpacingX = x;
}
//! Set font spacing X
inline virtual void setFontSpacingY(int y){
this->fontSpacingY = y;
}
//! Next
virtual bool next();
//! Previous
virtual bool previous();
//! Set current index
virtual bool setCurrentIndex(unsigned int index);
//! Get current index
virtual inline unsigned int getCurrentIndex() const {
return this->currentIndex;
}
/*
//! Set gradient
virtual inline void setGradient(bool use){
this->useGradient = use;
}
//! Get gradient
virtual inline bool gradientActive() const {
return this->useGradient;
}
*/
virtual inline double getScrollMotion() const {
return scrollMotion;
}
virtual inline void setScrollMotion(double what){
if (what < 1.01){
what = 1.01;
}
scrollMotion = what;
}
virtual inline void setScrollWaitTime(int what){
scrollWaitTime = what;
}
virtual inline int getScrollWaitTime() const {
return scrollWaitTime;
}
//! Set highlight
virtual inline void setHighlight(bool use){
this->useHighlight = use;
}
//! Get highlight
virtual inline bool highlightActive() const {
return this->useHighlight;
}
//! Set wrap
virtual inline void setWrap(bool wrap){
this->allowWrap = wrap;
}
//! Get wrap
virtual inline bool getWrap() const {
return this->allowWrap;
}
virtual inline void setJustification(Justify what){
this->justification = what;
}
private:
- int justify(int left, int right, int size) const;
-
/* smooth drawing */
void doDraw(int x, int y, int min_y, int max_y, const Font & font, int current, int selected, const Graphics::Bitmap & area, int direction) const;
//! Text
std::vector<Util::ReferenceCount<ScrollItem> > text;
//! Current index
unsigned int currentIndex;
//! Coordinates (size of)
Gui::Coordinate position;
//! Font Spacing
int fontSpacingX, fontSpacingY;
//! Current position for smooth scrolling
int currentPosition;
//! Scroll wait
int scrollWait;
int scrollWaitTime;
/* speed at which the menu scrolls */
double scrollMotion;
//! Gradient for selected cursor
// Effects::Gradient selectedGradient;
//! Use gradient
// bool useGradient;
//! Use highlight
bool useHighlight;
//! Is wrappable
bool allowWrap;
/* how much to scroll by */
double scroll;
Justify justification;
};
+class NormalList: public ScrollListInterface {
+public:
+ NormalList();
+ virtual ~NormalList();
+
+ //! Logic
+ virtual void act();
+
+ //! Render
+ virtual void render(const Graphics::Bitmap &, const Font & font) const;
+
+ //! Add item
+ virtual void addItem(const Util::ReferenceCount<ScrollItem> & item);
+
+ //! Add vector of text
+ virtual void addItems(const std::vector<Util::ReferenceCount<ScrollItem> > & items);
+
+ virtual void clearItems();
+
+ virtual unsigned int getCurrentIndex() const;
+
+ virtual bool next();
+ virtual bool previous();
+
+ virtual inline void setJustification(Justify what){
+ this->justification = what;
+ }
+
+protected:
+ std::vector<Util::ReferenceCount<ScrollItem> > text;
+ int position;
+ mutable int first, last;
+ Justify justification;
+};
+
}
#endif

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 10:42 AM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68495
Default Alt Text
(13 KB)

Event Timeline