1#include "RootDisplay.hpp"
3#include "Constraint.hpp"
4#include "Animation.hpp"
27 if (event->
touchIn(0, 0, 400, 240))
return ret;
33 ret |= onTouchDown(event);
34 ret |= onTouchDrag(event);
35 ret |= onTouchUp(event);
39 for (
int x = 0; x < this->
elements.size(); x++)
40 if (this->
elements.size() > x && this->elements[x])
41 ret |= this->
elements[x]->process(event);
52 if (RootDisplay::idleCursorPulsing) {
66 this->recalcPosition(
parent);
69 if (this->hasBackground) {
71 this->renderBackground(
true);
73 else if (RootDisplay::isDebug) {
74 backgroundColor = randomColor();
75 this->renderBackground(
false);
81 subelement->render(
this);
84 CST_Renderer* renderer = getRenderer();
89 CST_Rect d = { this->xAbs - 5, this->yAbs - 5, this->
width + 10, this->height + 10 };
90 CST_SetDrawBlend(renderer,
true);
91 CST_SetDrawColorRGBA(renderer, 0x10, 0xD9, 0xD9, 0x40);
92 CST_FillRect(renderer, &d);
97 CST_Rect d = { this->xAbs - 5, this->yAbs - 5, this->
width + 10, this->height + 10 };
100 int ticks = CST_GetTicks() / 100;
101 int pulseState = ticks % 20;
102 if (pulseState > 9) {
103 pulseState = 19 - pulseState;
106 if (!RootDisplay::idleCursorPulsing) {
112 for (
int x = -2; x <= 3; x++)
116 int g = 0xD9 - 0x01 * pulseState;
117 int b = 0xD9 - 0x01 * pulseState;
118 int edgeMod = x==1 ? 0 : abs(x);
119 int a = fmax(0x0, 0xFF - 0x10 * pulseState * edgeMod);
120 CST_rectangleRGBA(renderer, d.x + x, d.y + x, d.x + d.w - x, d.y + d.h - x, r, g, b, a);
124 CST_rectangleRGBA(renderer, d.x, d.y, d.x + d.w, d.y + d.h, 0x10, 0xD9, 0xD9, 0xFF);
126 CST_rectangleRGBA(renderer, d.x + 1, d.y + 1, d.x + d.w - 1, d.y + d.h - 1, 0x10, 0xD9, 0xD9, 0xFF);
131void Element::recalcPosition(
Element* parent) {
133 if (
parent && !isAbsolute)
135 this->xAbs =
parent->xAbs + this->x;
136 this->yAbs =
parent->yAbs + this->y;
138 this->xAbs = this->x;
139 this->yAbs = this->y;
145 constraint->apply(
this);
150 if (animations.size() > 0) {
151 std::vector<Animation*> toRemove;
157 bool finished = animation->step();
159 toRemove.push_back(animation);
163 animations.erase(std::remove(animations.begin(), animations.end(), animation), animations.end());
169CST_Rect Element::getBounds()
179void Element::renderBackground(
bool fill) {
180 CST_Renderer* renderer = getRenderer();
181 CST_Rect bounds = getBounds();
182 CST_SetDrawColorRGBA(renderer,
183 static_cast<Uint8
>(backgroundColor.r * 0xFF),
184 static_cast<Uint8
>(backgroundColor.g * 0xFF),
185 static_cast<Uint8
>(backgroundColor.b * 0xFF),
188 CST_SetDrawBlend(renderer,
false);
189 const auto RenderRect = fill ? CST_FillRect : CST_DrawRect;
190 RenderRect(renderer, &bounds);
201 if (!event->isTouchDown())
204 if (!event->
touchIn(this->xAbs, this->yAbs, this->width, this->height))
210 this->lastMouseX =
event->xPos;
223 if (!event->isTouchDrag())
228 if (event->
touchIn(this->xAbs, this->yAbs, this->width, this->height)) {
235 CST_LowRumble(event, 200);
238 CST_SetCursor(CST_CURSOR_HAND);
245 if (initialElasticCounter != NO_HIGHLIGHT) {
247 CST_SetCursor(CST_CURSOR_ARROW);
255 int TRESHOLD = 40 / SCALER / SCALER;
259 if (this->
dragging && (abs(event->yPos - this->lastMouseY) >= TRESHOLD || abs(event->xPos - this->lastMouseX) >= TRESHOLD))
264 if (prevElasticCounter != NO_HIGHLIGHT) {
266 CST_SetCursor(CST_CURSOR_ARROW);
275 if (!event->isTouchUp())
286 if (event->
touchIn(this->xAbs, this->yAbs, this->width, this->height))
296 if (actionWithEvents != NULL) {
297 this->actionWithEvents(event);
315void Element::append(
Element *element)
323void Element::remove(
Element *element)
330void Element::wipeAll(
bool delSelf)
334 child->wipeAll(
true);
338 if (delSelf && !isProtected) {
343void Element::removeAll(
bool moveToTrash)
348 RootDisplay::mainDisplay->trash.push_back(e);
358 this->elements.push_back(child);
360 child->recalcPosition(
this);
364Element* Element::setPosition(
int x,
int y)
370Element* Element::setAction(std::function<
void()> func)
384 this->y =
parent->height / 2 - this->height / 2;
390 return centerHorizontallyIn(
parent)->centerVerticallyIn(
parent);
393Element* Element::setAbsolute(
bool isAbs)
399CST_Renderer* Element::getRenderer() {
400 return RootDisplay::renderer;
403Element* Element::constrain(
int flags,
int padding)
405 constraints.push_back(
new Constraint(flags, padding));
411 std::function<
void(
float)> onStep,
412 std::function<
void()> onFinish
415 CST_GetTicks(), duration, onStep, onFinish)
422Element* Element::moveToFront() {
430Element* Element::setTouchable(
bool touchable)
438 CST_Texture* target = SDL_CreateTexture(getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
width, height);
441 SDL_SetRenderTarget(getRenderer(), target);
444 SDL_SetRenderDrawColor(getRenderer(), 255, 255, 255, 255);
445 SDL_RenderClear(getRenderer());
451 SDL_SetRenderTarget(getRenderer(), NULL);
454 CST_SavePNG(target, path.c_str());
virtual bool process(InputEvents *event)
process any input that is received for this element
bool hidden
whether this element should skip rendering or not
bool dragging
whether or not this element is currently being dragged
bool touchable
whether or not this element can be touched (highlights bounds)
void screenshot(std::string path)
Take a screenshot of this element and its children, and save it to the given path.
std::function< void()> action
the action to call (from binded callback) on touch or button selection https://stackoverflow....
int futureRedrawCounter
whether this element needs a redraw for the next X redraws (decreases each time) (0 is no redraws)
int elasticCounter
how much time is left in an elastic-type flick/scroll set by the last distance traveled in a scroll,...
virtual void render(Element *parent)
display the current state of the display
int lastMouseY
the last Y, X coordinate of the mouse (from a drag probably)
void position(int x, int y)
position the element
bool needsRedraw
whether or not this element needs the screen redrawn next time it's processed
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Element * parent
the parent element (can sometimes be null if it isn't set)
std::vector< Element * > elements
visible GUI child elements of this element