diff --git a/checkbox.cpp b/checkbox.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c6f282a7e02d52a5c0d8548d0e8ad3343c2b030 --- /dev/null +++ b/checkbox.cpp @@ -0,0 +1,74 @@ +#include "checkbox.hpp" +#include <cmath> + +using namespace genv; + +CheckBox::CheckBox(Application *parent, int x, int y, int sx, int sy) + : Widget(parent, x, y, sx, sy), _checked(false), _value(0) {} + +void CheckBox::draw() +{ + gout << move_to(_x, _y) << color(0, 255, 255) << box(_size_x, _size_y); + gout << move_to(_x + 2, _y + 2) << color(0, 0, 0) << box(_size_x - 4, _size_y - 4); + + if (_value % 2 == 0 && _value != 0) + { + drawCross(_x + 4, _y + 4, _size_x - 8, _size_y - 8, 255, 0, 0); + } + + if (_value % 2 == 1 && _value != 0) + { + drawCircle(_x + _size_x / 2, _y + _size_y / 2, _size_x / 2 - 3, 0, 255, 0); + } +} + + +void CheckBox::drawCross(int x, int y, int width, int height, int r, int g, int b) +{ + gout << color(r, g, b); + gout << move_to(x, y) << line(width, height); + gout << move_to(x + 1, y) << line(width, height); + gout << move_to(x + width, y) << line(-width, height); + gout << move_to(x + width - 1, y) << line(-width, height); +} + +void CheckBox::drawCircle(int x, int y, int radius, int r, int g, int b) +{ + gout << color(r, g, b); + for (int i = -radius; i < radius; i++) + { + for (int j = -radius; j < radius; j++) + { + if (i * i + j * j < pow(radius, 2)) + { + gout << move_to(x + i, y + j) << dot; + } + } + } +} + +void CheckBox::handle(event ev) +{ + if (ev.type == ev_mouse && is_selected(ev.pos_x, ev.pos_y) && ev.button == btn_left && !_checked) + { + _checked = true; + } +} + +bool CheckBox::is_checked() +{ + return _checked; +} + +void CheckBox::value(int sz) +{ + _value = sz; +} + +int CheckBox::value_sz() +{ + return _value; +} +int CheckBox::getPlayerValue() const { + return playerValue; +}