Verified Commit dfa8f1aa authored by RobLoach's avatar RobLoach
Browse files

Add method chaining

parent d679863c
......@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed
- Updated C++ namespace from `Modules` to `love`
- [Method chaining](https://en.wikipedia.org/wiki/Method_chaining) for all `set*` functions
```
graphics.love.point(100,100).point(50, 200)
```
## 0.11.1 - 2017-12-10
### Fixed
......
......@@ -33,13 +33,14 @@ float SoundData::getVolume() {
return m_volume;
}
void SoundData::setVolume(float volume) {
SoundData& SoundData::setVolume(float volume) {
if (volume > 1.0f) {
volume = 1.0f;
} else if (volume < 0.0f) {
volume = 0.0f;
}
m_volume = volume;
return *this;
}
void SoundData::unload() {
......@@ -94,8 +95,9 @@ bool SoundData::isLooping() {
return loop;
}
void SoundData::setLooping(bool looping) {
SoundData& SoundData::setLooping(bool looping) {
loop = looping;
return *this;
}
} // namespace Audio
......
......@@ -64,7 +64,7 @@ class SoundData {
/**
* @brief Sets the current volume of the Source.
*/
void setVolume(float volume);
SoundData& setVolume(float volume);
/**
* @brief Returns whether the Source is playing.
......@@ -91,7 +91,7 @@ class SoundData {
/**
* @brief Set whether the Source should loop.
*/
void setLooping(bool loop);
SoundData& setLooping(bool loop);
};
} // namespace Audio
......
......@@ -30,10 +30,12 @@ float audio::getVolume() {
return m_volume;
}
void audio::setVolume(float volume) {
audio& audio::setVolume(float volume) {
if (volume >= 0.0f && volume <= 1.0f) {
m_volume = volume;
}
return *this;
}
void audio::mixer_render(int16_t *buffer) {
......
......@@ -39,7 +39,7 @@ class audio {
*
* @param volume 1.0f is max and 0.0f is off.
*/
void setVolume(float volume);
audio& setVolume(float volume);
};
} // namespace love
......
......@@ -37,24 +37,28 @@ bool graphics::load() {
return true;
}
void graphics::clear() {
clear(backR, backG, backB, backA);
graphics& graphics::clear() {
return clear(backR, backG, backB, backA);
}
void graphics::clear(int r, int g, int b, int a) {
graphics& graphics::clear(int r, int g, int b, int a) {
SDL_Surface* screen = getScreen();
Uint32 color = SDL_MapRGBA(screen->format, r, g, b, a);
SDL_FillRect(screen, NULL, color);
return *this;
}
void graphics::clear(int r, int g, int b) {
clear(r, g, b, 255);
graphics& graphics::clear(int r, int g, int b) {
return clear(r, g, b, 255);
}
void graphics::point(int x, int y) {
graphics& graphics::point(int x, int y) {
pixelRGBA(getScreen(), x, y, r, g, b, a);
return *this;
}
void graphics::point(Point* p) {
point(p->x, p->y);
graphics& graphics::point(Point* p) {
return point(p->x, p->y);
}
/*
TODO: Fix graphics.points(Vector<Points>)
......@@ -71,27 +75,33 @@ void graphics::points(std::vector<Point> points) {
}
*/
void graphics::rectangle(const std::string& drawmode, int x, int y, int width, int height) {
graphics& graphics::rectangle(const std::string& drawmode, int x, int y, int width, int height) {
if (drawmode == "line") {
rectangleRGBA(getScreen(), x, y, x + width, y + height, r, g, b, a);
} else {
boxRGBA(getScreen(), x, y, x + width, y + height, r, g, b, a);
}
return *this;
}
void graphics::line(int x1, int y1, int x2, int y2) {
graphics& graphics::line(int x1, int y1, int x2, int y2) {
lineRGBA(getScreen(), x1, y1, x2, y2, r, g, b, a);
return *this;
}
void graphics::draw(Image* image, int x, int y) {
graphics& graphics::draw(Image* image, int x, int y) {
if (image && image->loaded()) {
SDL_Rect dstrect;
dstrect.x = x;
dstrect.y = y;
SDL_BlitSurface(image->surface, NULL, getScreen(), &dstrect);
}
return *this;
}
void graphics::draw(Image* image, Quad quad, int x, int y) {
graphics& graphics::draw(Image* image, Quad quad, int x, int y) {
if (image && image->loaded()) {
SDL_Rect dest;
dest.x = x;
......@@ -101,9 +111,11 @@ void graphics::draw(Image* image, Quad quad, int x, int y) {
SDL_Rect src = quad.toRect();
SDL_BlitSurface(image->surface, &src, getScreen(), &dest);
}
return *this;
}
void graphics::draw(Image* image, int x, int y, float r, float sx, float sy, float ox, float oy) {
graphics& graphics::draw(Image* image, int x, int y, float r, float sx, float sy, float ox, float oy) {
if (image && image->loaded()) {
ChaiLove* app = ChaiLove::getInstance();
float angle = app->math.degrees(r);
......@@ -118,25 +130,27 @@ void graphics::draw(Image* image, int x, int y, float r, float sx, float sy, flo
SDL_FreeSurface(tempSurface);
}
}
return *this;
}
void graphics::draw(Image* image, int x, int y, float r, float sx, float sy, float ox) {
draw(image, x, y, r, sx, sy, ox, 0.0f);
graphics& graphics::draw(Image* image, int x, int y, float r, float sx, float sy, float ox) {
return draw(image, x, y, r, sx, sy, ox, 0.0f);
}
void graphics::draw(Image* image, int x, int y, float r, float sx, float sy) {
draw(image, x, y, r, sx, sy, 0.0f, 0.0f);
graphics& graphics::draw(Image* image, int x, int y, float r, float sx, float sy) {
return draw(image, x, y, r, sx, sy, 0.0f, 0.0f);
}
void graphics::draw(Image* image, int x, int y, float r, float sx) {
draw(image, x, y, r, sx, sx, 0.0f, 0.0f);
graphics& graphics::draw(Image* image, int x, int y, float r, float sx) {
return draw(image, x, y, r, sx, sx, 0.0f, 0.0f);
}
void graphics::draw(Image* image, int x, int y, float r) {
graphics& graphics::draw(Image* image, int x, int y, float r) {
if (r == 0.0f) {
draw(image, x, y);
return draw(image, x, y);
} else {
draw(image, x, y, r, 1.0f, 1.0f, 0.0f, 0.0f);
return draw(image, x, y, r, 1.0f, 1.0f, 0.0f, 0.0f);
}
}
......@@ -152,18 +166,22 @@ Quad graphics::newQuad(int x, int y, int width, int height, int sw, int sh) {
return Quad(x, y, width, height, sw, sh);
}
void graphics::print(const std::string& text, int x, int y) {
graphics& graphics::print(const std::string& text, int x, int y) {
activeFont->print(text, x, y, r, g, b, a);
return *this;
}
void graphics::setColor(int red, int green, int blue, int alpha) {
graphics& graphics::setColor(int red, int green, int blue, int alpha) {
r = red;
g = green;
b = blue;
a = alpha;
return *this;
}
void graphics::setColor(int red, int green, int blue) {
setColor(red, green, blue, 255);
graphics& graphics::setColor(int red, int green, int blue) {
return setColor(red, green, blue, 255);
}
Color graphics::getBackgroundColor() {
......@@ -174,25 +192,29 @@ Color graphics::getBackgroundColor() {
c.a = backA;
return c;
}
void graphics::setBackgroundColor(int red, int green, int blue) {
setBackgroundColor(red, green, blue, 255);
graphics& graphics::setBackgroundColor(int red, int green, int blue) {
return setBackgroundColor(red, green, blue, 255);
}
void graphics::setBackgroundColor(int red, int green, int blue, int alpha) {
graphics& graphics::setBackgroundColor(int red, int green, int blue, int alpha) {
backR = red;
backG = green;
backB = blue;
backA = alpha;
return *this;
}
/**
* @brief Sets the default scaling filters used with images, and fonts.
*/
void graphics::setDefaultFilter(const std::string& filter) {
graphics& graphics::setDefaultFilter(const std::string& filter) {
if (filter == "linear") {
m_smooth = 1;
} else if (filter == "nearest") {
m_smooth = 0;
}
return *this;
}
/**
......@@ -215,28 +237,34 @@ int graphics::getHeight() {
return getScreen()->h;
}
void graphics::circle(const std::string& drawmode, int x, int y, int radius) {
graphics& graphics::circle(const std::string& drawmode, int x, int y, int radius) {
if (drawmode == "line") {
circleRGBA(getScreen(), x, y, radius, r, g, b, a);
} else {
filledCircleRGBA(getScreen(), x, y, radius, r, g, b, a);
}
return *this;
}
void graphics::arc(const std::string& drawmode, int x, int y, int radius, int angle1, int angle2) {
graphics& graphics::arc(const std::string& drawmode, int x, int y, int radius, int angle1, int angle2) {
if (drawmode == "line") {
arcRGBA(getScreen(), x, y, radius, angle1, angle2, r, g, b, a);
} else {
filledPieRGBA(getScreen(), x, y, radius, angle1, angle2, r, g, b, a);
}
return *this;
}
void graphics::ellipse(const std::string& drawmode, int x, int y, int radiusx, int radiusy) {
graphics& graphics::ellipse(const std::string& drawmode, int x, int y, int radiusx, int radiusy) {
if (drawmode == "line") {
ellipseRGBA(getScreen(), x, y, radiusx, radiusy, r, g, b, a);
} else {
filledEllipseRGBA(getScreen(), x, y, radiusx, radiusy, r, g, b, a);
}
return *this;
}
Font* graphics::newFont(const std::string& filename, int glyphWidth, int glyphHeight, const std::string& letters) {
......@@ -255,12 +283,16 @@ Font* graphics::newFont(const std::string& filename) {
return newFont(filename, 12);
}
void graphics::setFont(Font* font) {
graphics& graphics::setFont(Font* font) {
activeFont = font;
return *this;
}
void graphics::setFont() {
graphics& graphics::setFont() {
activeFont = &defaultFont;
return *this;
}
Font* graphics::getFont() {
......
......@@ -40,32 +40,32 @@ class graphics {
* love.graphics.rectangle("fill", 100, 100, 50, 50)
* @endcode
*/
void rectangle(const std::string& drawmode, int x, int y, int width, int height);
graphics& rectangle(const std::string& drawmode, int x, int y, int width, int height);
/**
* @brief Clears the screen to the set background color.
*/
void clear();
graphics& clear();
/**
* @brief Clears the screen to the given background color.
*/
void clear(int r, int g, int b, int a);
graphics& clear(int r, int g, int b, int a);
/**
* @brief Clears the screen to the given background color.
*/
void clear(int r, int g, int b);
graphics& clear(int r, int g, int b);
/**
* @brief Draws a point.
*/
void point(int x, int y);
graphics& point(int x, int y);
/**
* @brief Draws a point.
*/
void point(Point* p);
graphics& point(Point* p);
// void points(std::vector<Point*>);
// void points(std::vector<Point>);
......@@ -73,7 +73,7 @@ class graphics {
/**
* @brief Draws a line.
*/
void line(int x1, int y1, int x2, int y2);
graphics& line(int x1, int y1, int x2, int y2);
/**
......@@ -83,12 +83,12 @@ class graphics {
* @param x The position to draw the object (x-axis).
* @param y The position to draw the object (y-axis).
*/
void draw(Image* image, int x, int y);
graphics& draw(Image* image, int x, int y);
/**
* @brief Draws an image on screen, using the given Quad as a source.
*/
void draw(Image* image, Quad quad, int x, int y);
graphics& draw(Image* image, Quad quad, int x, int y);
/**
* @brief Creates a new Quad.
......@@ -142,9 +142,9 @@ class graphics {
*
* @param The font to set as the active font.
*/
void setFont(Font* font);
graphics& setFont(Font* font);
void setFont();
graphics& setFont();
/**
* @brief Retrieve th currently active font.
......@@ -158,17 +158,17 @@ class graphics {
* @param x The position to draw the object (x-axis).
* @param y The position to draw the object (y-axis).
*/
void print(const std::string& text, int x, int y);
graphics& print(const std::string& text, int x, int y);
/**
* @brief Sets the active drawing color to the given color.
*/
void setColor(int red, int green, int blue, int alpha);
graphics& setColor(int red, int green, int blue, int alpha);
/**
* @brief Sets the active drawing color to the given color.
*/
void setColor(int red, int green, int blue);
graphics& setColor(int red, int green, int blue);
/**
* @brief Retrieves the active background color.
......@@ -178,17 +178,17 @@ class graphics {
/**
* @brief Sets the background color to the given color.
*/
void setBackgroundColor(int red, int green, int blue, int alpha);
graphics& setBackgroundColor(int red, int green, int blue, int alpha);
/**
* @brief Sets the background color to the given color.
*/
void setBackgroundColor(int red, int green, int blue);
graphics& setBackgroundColor(int red, int green, int blue);
/**
* @brief Sets the default scaling filters used with images, and fonts.
*/
void setDefaultFilter(const std::string& filter);
graphics& setDefaultFilter(const std::string& filter);
/**
* @brief Returns the default scaling filters used with images and fonts.
......@@ -208,12 +208,12 @@ class graphics {
/**
* @brief Draws a circle.
*/
void circle(const std::string& drawmode, int x, int y, int radius);
graphics& circle(const std::string& drawmode, int x, int y, int radius);
/**
* @brief Draws an ellipse.
*/
void ellipse(const std::string& drawmode, int x, int y, int radiusx, int radiusy);
graphics& ellipse(const std::string& drawmode, int x, int y, int radiusx, int radiusy);
/**
* @brief Draws an image with the given angle, zoom, and origin.
......@@ -227,16 +227,16 @@ class graphics {
* @param ox (0) Origin offset (x-axis).
* @param oy (0) Origin offset (y-axis).
*/
void draw(Image* image, int x, int y, float r, float sx, float sy, float ox, float oy);
void draw(Image* image, int x, int y, float r, float sx, float sy, float ox);
void draw(Image* image, int x, int y, float r, float sx, float sy);
void draw(Image* image, int x, int y, float r, float sx);
void draw(Image* image, int x, int y, float r);
graphics& draw(Image* image, int x, int y, float r, float sx, float sy, float ox, float oy);
graphics& draw(Image* image, int x, int y, float r, float sx, float sy, float ox);
graphics& draw(Image* image, int x, int y, float r, float sx, float sy);
graphics& draw(Image* image, int x, int y, float r, float sx);
graphics& draw(Image* image, int x, int y, float r);
/**
* @brief Draws an arc.
*/
void arc(const std::string& drawmode, int x, int y, int radius, int angle1, int angle2);
graphics& arc(const std::string& drawmode, int x, int y, int radius, int angle1, int angle2);
Uint8 r = 255, g = 255, b = 255, a = 255;
Uint8 backR = 0, backG = 0, backB = 0, backA = 255;
......
......@@ -28,13 +28,14 @@ float math::random(float min, float max) {
return Random::get(min, max);
}
void math::setRandomSeed(int min, int max) {
setRandomSeed(random(min, max));
math& math::setRandomSeed(int min, int max) {
return setRandomSeed(random(min, max));
}
void math::setRandomSeed(int seed) {
math& math::setRandomSeed(int seed) {
Random::seed(seed);
m_seed = seed;
return *this;
}
int math::getRandomSeed() {
......
......@@ -25,12 +25,12 @@ class math {
/**
* @brief Sets the random seed to a random seed between the given min and max values.
*/
void setRandomSeed(int min, int max);
math& setRandomSeed(int min, int max);
/**
* @brief Sets the seed of the random number generator.
*/
void setRandomSeed(int seed);
math& setRandomSeed(int seed);
/**
* @brief Gets the seed of the random number generator.
......
......@@ -20,18 +20,21 @@ bool mouse::isVisible() {
return SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE;
}
void mouse::setVisible(bool enable) {
mouse& mouse::setVisible(bool enable) {
SDL_ShowCursor(enable ? SDL_ENABLE : SDL_DISABLE);
return *this;
}
void mouse::setX(int x) {
mouse& mouse::setX(int x) {
m_x = x;
SDL_WarpMouse(m_x, m_y);
return *this;
}
void mouse::setY(int y) {
mouse& mouse::setY(int y) {
m_y = y;
SDL_WarpMouse(m_x, m_y);
return *this;
}
int mouse::getX() {
......
......@@ -16,7 +16,8 @@ class mouse {
/**
* @brief Sets the current visibility of the cursor.
*/
void setVisible(bool enable);
mouse& setVisible(bool enable);
/**
* @brief Checks if the cursor is visible.
*/
......@@ -25,11 +26,11 @@ class mouse {
/**
* @brief Sets the current X position of the mouse.
*/
void setX(int x);
mouse& setX(int x);
/**
* @brief Sets the current Y position of the mouse.
*/
void setY(int y);
mouse& setY(int y);
/**
* @brief Returns the current x-position of the mouse.
*/
......
......@@ -18,6 +18,7 @@ using Types::Input::Joystick;
using Types::System::windowConfig;
using Types::System::moduleConfig;
using Types::Audio::SoundData;
using love::graphics;
namespace love {
......@@ -167,8 +168,8 @@ script::script(const std::string& file) {
chai.add(fun(&graphics::rectangle), "rectangle");
chai.add(fun(&graphics::newImage), "newImage");
chai.add(fun(&graphics::print), "print");
chai.add(fun<void, graphics, int, int>(&graphics::point), "point");
chai.add(fun<void, graphics, Point*>(&graphics::point), "point");
chai.add(fun<love::graphics&, graphics, int, int>(&graphics::point), "point");
chai.add(fun<love::graphics&, graphics, Point*>(&graphics::point), "point");
// chai.add(bootstrap::standard_library::vector_type<std::vector<Point*>>("VectorPointPointer"));
// chai.add(bootstrap::standard_library::vector_type<std::vector<Point>>("VectorPoint"));
// chai.add(fun<void, graphics, std::vector<Point*>>(&graphics::points), "points");
......@@ -186,25 +187,25 @@ script::script(const std::string& file) {
chai.add(fun<Font*, graphics, const std::string&>(&graphics::newFont), "newFont");
chai.add(fun<Font*, graphics, const std::string&, int, int, const std::string&>(&graphics::newFont), "newFont");
chai.add(fun<Font*, graphics>(&graphics::newFont), "newFont");
chai.add(fun<void, graphics, Font*>(&graphics::setFont), "setFont");
chai.add(fun<void, graphics>(&graphics::setFont), "setFont");
chai.add(fun<love::graphics&, graphics, Font*>(&graphics::setFont), "setFont");
chai.add(fun<love::graphics&, graphics>(&graphics::setFont), "setFont");
chai.add(fun<Font*, graphics>(&graphics::getFont), "getFont");
chai.add(fun<void, graphics, int, int, int, int>(&graphics::setColor), "setColor");
chai.add(fun<void, graphics, int, int, int>(&graphics::setColor), "setColor");
chai.add(fun<void, graphics, int, int, int, int>(&graphics::setBackgroundColor), "setBackgroundColor");
chai.add(fun<void, graphics, int, int, int>(&graphics::setBackgroundColor), "setBackgroundColor");
chai.add(fun<void, graphics, Image*, int, int>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, int, int, float, float, float, float, float>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, int, int, float, float, float, float>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, int, int, float, float, float>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, int, int, float, float>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, int, int, float>(&graphics::draw), "draw");
chai.add(fun<void, graphics, Image*, Quad, int, int>(&graphics::draw), "draw");
chai.add(fun&