|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 https://www.thecoderscorner.com (Nutricherry LTD). |
| 3 | + * This product is licensed under an Apache license, see the LICENSE file in the top-level directory. |
| 4 | + */ |
| 5 | + |
| 6 | +#include "DrawableTouchCalibrator.h" |
| 7 | +#include <BaseDialog.h> |
| 8 | + |
| 9 | +using namespace tcextras; |
| 10 | +using namespace tcgfx; |
| 11 | + |
| 12 | +#define TOUCH_BLACK RGB(0,0,0) |
| 13 | +#define TOUCH_ORANGE RGB(255, 69, 0) |
| 14 | +#define TOUCH_YELLOW RGB(255, 255, 0) |
| 15 | + |
| 16 | +void TouchScreenCalibrator::started(BaseMenuRenderer *currentRenderer) { |
| 17 | + if (currentRenderer->getRendererType() != RENDER_TYPE_CONFIGURABLE) { |
| 18 | + giveItBack(); |
| 19 | + } |
| 20 | + drawable = renderer->getDeviceDrawable(); |
| 21 | + drawable->startDraw(); |
| 22 | + drawable->setDrawColor(TOUCH_BLACK); |
| 23 | + drawable->drawBox(Coord(0, 0), drawable->getDisplayDimensions(), true); |
| 24 | + drawable->endDraw(); |
| 25 | + takeOverCount = 0; |
| 26 | +} |
| 27 | + |
| 28 | +void TouchScreenCalibrator::renderLoop(unsigned int currentValue, RenderPressMode userClick) { |
| 29 | + |
| 30 | + // If the dialog is in use then we leave this loop immediately to give it priority. |
| 31 | + // As of 2.1 onwards this behaviour is up to you, you can choose to have higher priority than dialogs. |
| 32 | + if (renderer->getDialog() != nullptr && renderer->getDialog()->isInUse()) { |
| 33 | + giveItBack(); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + drawable->startDraw(); |
| 38 | + drawable->setDrawColor(TOUCH_BLACK); |
| 39 | + drawable->drawCircle(Coord(oldX, oldY), 10, true); |
| 40 | + |
| 41 | + drawable->drawBox(Coord(0, 0), Coord(drawable->getDisplayDimensions().x, 45), true); |
| 42 | + drawable->setColors(TOUCH_ORANGE, TOUCH_BLACK); |
| 43 | + drawable->drawText(Coord(0, 2), nullptr, 0, "Calibrate screen"); |
| 44 | + |
| 45 | + char sz[40]; |
| 46 | + strcpy(sz, "x: "); |
| 47 | + fastftoa(sz, touchScreen->getLastX(), 3, sizeof sz); |
| 48 | + strcat(sz, ", y: "); |
| 49 | + fastftoa(sz, touchScreen->getLastY(), 3, sizeof sz); |
| 50 | + strcat(sz, ", z: "); |
| 51 | + fastltoa(sz, touchScreen->getLastTouchState(), 1, NOT_PADDED, sizeof sz); |
| 52 | + |
| 53 | + drawable->setColors(TOUCH_YELLOW, TOUCH_BLACK); |
| 54 | + drawable->drawText(Coord(0, 22), nullptr, 0, sz); |
| 55 | + |
| 56 | + oldX = int(touchScreen->getLastX() * (float) drawable->getDisplayDimensions().x); |
| 57 | + oldY = int(touchScreen->getLastY() * (float) drawable->getDisplayDimensions().y); |
| 58 | + drawable->setDrawColor(TOUCH_YELLOW); |
| 59 | + drawable->drawCircle(Coord(oldX, oldY), 10, true); |
| 60 | + |
| 61 | + if (oldX < 40 && oldY < 40 && touchScreen->getLastTouchState() == iotouch::HELD) { |
| 62 | + giveItBack(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void TouchScreenCalibrator::giveItBack() { |
| 67 | + renderer->giveBackDisplay(); |
| 68 | + if(calibrationCompletedHandler) calibrationCompletedHandler(); |
| 69 | +} |
0 commit comments