6#include "fifechan/widgets/checkbox.hpp"
13#include "fifechan/exception.hpp"
14#include "fifechan/font.hpp"
15#include "fifechan/graphics.hpp"
16#include "fifechan/image.hpp"
23 void drawMarkerBrush(
Graphics* graphics,
int x,
int y,
int thickness)
25 int const offset = (thickness - 1) / 2;
26 graphics->fillRectangle(x - offset, y - offset, thickness, thickness);
29 void drawMarkerStroke(
Graphics* graphics,
Point const & start,
Point const & end,
int thickness)
34 int const deltaX = std::abs(end.x - start.x);
35 int const stepX = (start.x < end.x) ? 1 : -1;
36 int const deltaY = -std::abs(end.y - start.y);
37 int const stepY = (start.y < end.y) ? 1 : -1;
38 int error = deltaX + deltaY;
41 drawMarkerBrush(graphics, x, y, thickness);
43 if ((x == end.x) && (y == end.y)) {
47 int const doubledError = 2 * error;
48 if (doubledError >= deltaY) {
52 if (doubledError <= deltaX) {
59 int scaleMarkerCoordinate(
int origin,
int size,
int numerator)
61 return origin + (((size - 1) * numerator) / 16);
70 CheckBox::CheckBox(std::string
const & caption,
bool selected) :
mSelected(selected)
86 if (
mMode == MarkerStyle::Rhombus) {
116 ((
getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
126 if (
mMode == MarkerStyle::Image) {
131 if (active && (
getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
143 if (active && (
getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
151 ((
getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
157 Color highlightColor = faceColor + 0x303030;
158 highlightColor.
a = alpha;
159 Color shadowColor = faceColor - 0x303030;
160 shadowColor.
a = alpha;
162 int const recRight = rec.
x + rec.
width - 1;
163 int const recBottom = rec.
y + rec.
height - 1;
167 graphics->
drawLine(rec.
x, rec.
y, rec.
x, recBottom);
170 graphics->
drawLine(recRight, rec.
y, recRight, recBottom);
171 graphics->
drawLine(rec.
x, recBottom, recRight, recBottom);
177 case MarkerStyle::Checkmark:
180 case MarkerStyle::Cross:
183 case MarkerStyle::Dot:
195 int const thickness = std::max(1, std::min(rec.
width, rec.
height) / 6);
197 Point const start(scaleMarkerCoordinate(rec.
x, rec.
width, 3), scaleMarkerCoordinate(rec.
y, rec.
height, 8));
198 Point const joint(scaleMarkerCoordinate(rec.
x, rec.
width, 7), scaleMarkerCoordinate(rec.
y, rec.
height, 12));
199 Point const end(scaleMarkerCoordinate(rec.
x, rec.
width, 13), scaleMarkerCoordinate(rec.
y, rec.
height, 4));
201 drawMarkerStroke(graphics, start, joint, thickness);
202 drawMarkerStroke(graphics, joint, end, thickness);
207 int const thickness = std::max(1, std::min(rec.
width, rec.
height) / 6);
209 Point const topLeft(scaleMarkerCoordinate(rec.
x, rec.
width, 3), scaleMarkerCoordinate(rec.
y, rec.
height, 3));
210 Point const bottomRight(
211 scaleMarkerCoordinate(rec.
x, rec.
width, 13), scaleMarkerCoordinate(rec.
y, rec.
height, 13));
212 Point const bottomLeft(
213 scaleMarkerCoordinate(rec.
x, rec.
width, 3), scaleMarkerCoordinate(rec.
y, rec.
height, 13));
214 Point const topRight(scaleMarkerCoordinate(rec.
x, rec.
width, 13), scaleMarkerCoordinate(rec.
y, rec.
height, 3));
216 drawMarkerStroke(graphics, topLeft, bottomRight, thickness);
217 drawMarkerStroke(graphics, bottomLeft, topRight, thickness);
224 int const radius = (rec.
width - 3) / 2;
232 Image const * img =
nullptr;
253 if (img !=
nullptr) {
273 Color highlightColor = faceColor + 0x303030;
274 highlightColor.
a = alpha;
275 Color shadowColor = faceColor - 0x303030;
276 shadowColor.
a = alpha;
281 int const hh = (h + 1) / 2;
283 for (i = 1; i <= hh; ++i) {
284 graphics->
drawLine(hh - i + 1, i, hh + i - 1, i);
287 for (i = 1; i < hh; ++i) {
288 graphics->
drawLine(hh - i + 1, h - i, hh + i - 1, h - i);
293 graphics->
drawLine(hh + 1, 1, h - 1, hh - 1);
296 graphics->
drawLine(1, hh + 1, hh, h);
297 graphics->
drawLine(hh + 1, h - 1, h, hh);
301 int const hhh = hh - 3;
304 for (i = 0; i < hhh; ++i) {
305 graphics->
drawLine(hh - i, 4 + i, hh + i, 4 + i);
307 for (i = 0; i < hhh; ++i) {
308 graphics->
drawLine(hh - i, h - 4 - i, hh + i, h - 4 - i);
322 int const fhh = (fh + 1) / 2;
325 graphics->
drawLine(0, fhh + 1, fhh + 1, 0);
326 graphics->
drawLine(fhh + 2, 1, fh + 2, fhh + 1);
327 graphics->
drawLine(fh + 1, fhh + 2, fhh + 1, fh + 2);
328 graphics->
drawLine(fhh + 1, fh + 2, 1, fhh + 2);
384 Key
const key = keyEvent.
getKey();
386 if (key.getValue() == fcn::Key::KEY_RETURN || key.getValue() == fcn::Key::SPACE) {
395 Key
const key = keyEvent.
getKey();
397 if (key.getValue() == fcn::Key::KEY_RETURN || key.getValue() == fcn::Key::SPACE) {
422 if (mouseEvent.
getButton() == MouseEvent::Button::Left) {
void drawMarkerImage(Graphics *graphics, Rectangle const &rec)
Draws the marker image.
static void drawCheckmark(Graphics *graphics, Rectangle const &rec)
Draws the checkmark.
void setBackgroundImage(std::string const &filename)
Sets the background image to display, that includes the caption region.
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Image const * getBackgroundImage() const
Gets background image.
MarkerStyle getMarkerStyle() const
Gets the marker mode of the check box.
virtual void setSelected(bool selected)
Sets the check box to be selected or not.
void keyReleased(KeyEvent &keyEvent) override
Called if a key is released when the widget has keyboard focus.
MarkerStyle mMode
Holds the marker style of the check box.
void mouseClicked(MouseEvent &mouseEvent) override
Called when a mouse button is pressed and released (clicked) on the widget area.
void draw(Graphics *graphics) override
Draws the widget.
Image const * mBackgroundImage
Holds the background image, that includes the caption region.
bool mSelected
True if the check box is selected, false otherwise.
virtual void drawBox(Graphics *graphics)
Draws the box of the check box.
virtual void toggleSelected()
Toggles the check box between being selected and not being selected.
void drawRhombus(Graphics *graphics)
Draws the rhombus.
bool mInternalBackgroundImage
True if the background image was loaded internally, false otherwise.
static void drawCross(Graphics *graphics, Rectangle const &rec)
Draws the cross.
void adjustSize() override
Resizes the widget's size to fit the content exactly.
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed down on the widget area.
void adjustSizeImpl() override
Adjusts the size of the checkbox to fit the caption.
void mouseReleased(MouseEvent &mouseEvent) override
Called when a mouse button has been released on the widget area.
void setMarkerStyle(MarkerStyle mode)
Set the marker style of the check box.
virtual bool isSelected() const
Checks if the check box is selected.
static void drawDot(Graphics *graphics, Rectangle const &rec)
Draws the dot.
uint8_t a
Alpha color component (0-255).
virtual int getWidth(std::string const &text) const =0
Gets the width of a string.
virtual int getHeight() const =0
Gets the height of the glyphs in the font.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
virtual void popClipArea()
Removes the top most clip area from the stack.
virtual void drawImage(Image const *image, int srcX, int srcY, int dstX, int dstY, int width, int height)=0
Draws a part of an image.
void drawText(std::string const &text, int x, int y)
Draws text with a default left alignment.
virtual bool pushClipArea(Rectangle area)
Pushes a clip area onto the stack.
virtual void setFont(Font *font)
Sets the font to use when drawing text.
virtual void drawLine(int x1, int y1, int x2, int y2)=0
Draws a line.
virtual void drawFillCircle(Point const &p, unsigned int radius)=0
Draws a filled circle.
virtual void setColor(Color const &color)=0
Sets the color to use when drawing.
virtual void fillRectangle(Rectangle const &rectangle)=0
Draws a filled rectangle.
Abstract holder for image data.
static Image * load(std::string const &filename, bool convertToDisplayFormat=true)
Loads an image by using the class' image loader.
virtual int getWidth() const =0
Gets the width of the image.
Key const & getKey() const
Gets the key of the event.
Represents a mouse event.
MouseEvent::Button getButton() const
Gets the button of the mouse event.
Represents a 2D coordinate (X, Y).
Represents a rectangular area (X, Y, Width, Height).
int width
Holds the width of the rectangle.
int y
Holds the x coordinate of the rectangle.
int x
Holds the x coordinate of the rectangle.
int height
Holds the height of the rectangle.
Used replacement tokens by configure_file():
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.