5#include "fifechan/widgets/checkbox.hpp"
10#include "fifechan/exception.hpp"
11#include "fifechan/font.hpp"
12#include "fifechan/graphics.hpp"
13#include "fifechan/image.hpp"
20 void drawMarkerBrush(
Graphics* graphics,
int x,
int y,
int thickness)
22 int const offset = (thickness - 1) / 2;
23 graphics->fillRectangle(x - offset, y - offset, thickness, thickness);
26 void drawMarkerStroke(
Graphics* graphics,
Point const & start,
Point const & end,
int thickness)
31 int const deltaX = std::abs(end.x - start.x);
32 int const stepX = (start.x < end.x) ? 1 : -1;
33 int const deltaY = -std::abs(end.y - start.y);
34 int const stepY = (start.y < end.y) ? 1 : -1;
35 int error = deltaX + deltaY;
38 drawMarkerBrush(graphics, x, y, thickness);
40 if ((x == end.x) && (y == end.y)) {
44 int const doubledError = 2 * error;
45 if (doubledError >= deltaY) {
49 if (doubledError <= deltaX) {
56 int scaleMarkerCoordinate(
int origin,
int size,
int numerator)
58 return origin + (((size - 1) * numerator) / 16);
67 CheckBox::CheckBox(std::string
const & caption,
bool selected) :
mSelected(selected)
82 if (
mMode == MarkerStyle::Rhombus) {
112 ((
getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
122 if (
mMode == MarkerStyle::Image) {
127 if (active && (
getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
139 if (active && (
getSelectionMode() & Widget::SelectionMode::Border) == Widget::SelectionMode::Border) {
147 ((
getSelectionMode() & Widget::SelectionMode::Background) == Widget::SelectionMode::Background)) {
153 Color highlightColor = faceColor + 0x303030;
154 highlightColor.
a = alpha;
155 Color shadowColor = faceColor - 0x303030;
156 shadowColor.
a = alpha;
158 int const recRight = rec.
x + rec.
width - 1;
159 int const recBottom = rec.
y + rec.
height - 1;
163 graphics->
drawLine(rec.
x, rec.
y, rec.
x, recBottom);
166 graphics->
drawLine(recRight, rec.
y, recRight, recBottom);
167 graphics->
drawLine(rec.
x, recBottom, recRight, recBottom);
173 case MarkerStyle::Checkmark:
176 case MarkerStyle::Cross:
179 case MarkerStyle::Dot:
183 throwException(
"Unknown marker.");
191 int const thickness = std::max(1, std::min(rec.
width, rec.
height) / 6);
193 Point const start(scaleMarkerCoordinate(rec.
x, rec.
width, 3), scaleMarkerCoordinate(rec.
y, rec.
height, 8));
194 Point const joint(scaleMarkerCoordinate(rec.
x, rec.
width, 7), scaleMarkerCoordinate(rec.
y, rec.
height, 12));
195 Point const end(scaleMarkerCoordinate(rec.
x, rec.
width, 13), scaleMarkerCoordinate(rec.
y, rec.
height, 4));
197 drawMarkerStroke(graphics, start, joint, thickness);
198 drawMarkerStroke(graphics, joint, end, thickness);
203 int const thickness = std::max(1, std::min(rec.
width, rec.
height) / 6);
205 Point const topLeft(scaleMarkerCoordinate(rec.
x, rec.
width, 3), scaleMarkerCoordinate(rec.
y, rec.
height, 3));
206 Point const bottomRight(
207 scaleMarkerCoordinate(rec.
x, rec.
width, 13), scaleMarkerCoordinate(rec.
y, rec.
height, 13));
208 Point const bottomLeft(
209 scaleMarkerCoordinate(rec.
x, rec.
width, 3), scaleMarkerCoordinate(rec.
y, rec.
height, 13));
210 Point const topRight(scaleMarkerCoordinate(rec.
x, rec.
width, 13), scaleMarkerCoordinate(rec.
y, rec.
height, 3));
212 drawMarkerStroke(graphics, topLeft, bottomRight, thickness);
213 drawMarkerStroke(graphics, bottomLeft, topRight, thickness);
220 int const radius = (rec.
width - 3) / 2;
228 Image const * img =
nullptr;
249 if (img !=
nullptr) {
269 Color highlightColor = faceColor + 0x303030;
270 highlightColor.
a = alpha;
271 Color shadowColor = faceColor - 0x303030;
272 shadowColor.
a = alpha;
277 int const hh = (h + 1) / 2;
279 for (i = 1; i <= hh; ++i) {
280 graphics->
drawLine(hh - i + 1, i, hh + i - 1, i);
283 for (i = 1; i < hh; ++i) {
284 graphics->
drawLine(hh - i + 1, h - i, hh + i - 1, h - i);
289 graphics->
drawLine(hh + 1, 1, h - 1, hh - 1);
292 graphics->
drawLine(1, hh + 1, hh, h);
293 graphics->
drawLine(hh + 1, h - 1, h, hh);
297 int const hhh = hh - 3;
300 for (i = 0; i < hhh; ++i) {
301 graphics->
drawLine(hh - i, 4 + i, hh + i, 4 + i);
303 for (i = 0; i < hhh; ++i) {
304 graphics->
drawLine(hh - i, h - 4 - i, hh + i, h - 4 - i);
318 int const fhh = (fh + 1) / 2;
321 graphics->
drawLine(0, fhh + 1, fhh + 1, 0);
322 graphics->
drawLine(fhh + 2, 1, fh + 2, fhh + 1);
323 graphics->
drawLine(fh + 1, fhh + 2, fhh + 1, fh + 2);
324 graphics->
drawLine(fhh + 1, fh + 2, 1, fhh + 2);
void drawMarkerImage(Graphics *graphics, Rectangle const &rec)
Draws the marker image.
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.
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 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.
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 keyboard key or character code.
int getValue() const
Gets the value of the key.
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.