Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17ajf by
Menus/UI/UI.cpp
- Committer:
- el17ajf
- Date:
- 2019-04-05
- Revision:
- 27:2ed9e3c9f4e9
- Parent:
- 26:baa7077449e7
- Child:
- 30:11838cb6979f
File content as of revision 27:2ed9e3c9f4e9:
#include "UI.h"
#include "Graphics.h"
#include "Math.h"
namespace Menus {
UI::UI() {
reset();
}
void UI::reset() {
buttonCurrentlySelected = 0;
}
UI::~UI() {
}
void UI::clear() {
buttonPressedFrame = false;
nextYPos = PADDING_Y;
buttonCurrentlyDrawing = -1;
frame++;
}
void UI::drawTitle(const char * text) {
drawLabel(text);
}
void UI::drawLabel(const char * text) {
Graphics::UI::drawText(PADDING_X, nextY(), text);
}
void UI::drawFlashingLabel(const char * text) {
Graphics::UI::drawText(PADDING_X, nextY(), (frame % 16) > 8 ? text : "");
}
void UI::newLine() {
nextY();
}
int oldBorderX1,oldBorderY1, oldBorderX2, oldBorderY2;
bool UI::drawAndCheckButton(const char * text) {
buttonCurrentlyDrawing++;
int y = nextY();
Graphics::UI::drawText(PADDING_X, y, text);
bool selected = buttonCurrentlyDrawing == buttonCurrentlySelected;
int wordWidth = strlen(text) * (Graphics::UI::CHAR_WIDTH + Graphics::UI::CHAR_SPACE) - 1;
if (selected) {
int borderX1 = PADDING_X - 2;
int borderY1 = y - 2;
int borderX2 = PADDING_X + wordWidth + 2;
int borderY2 = y + Graphics::UI::CHAR_HEIGHT + 1;
Graphics::UI::drawBorder(
Math::lerp(oldBorderX1, borderX1),
Math::lerp(oldBorderY1, borderY1),
Math::lerp(oldBorderX2, borderX2),
Math::lerp(oldBorderY2, borderY2)
);
oldBorderX1 = Math::lerp(oldBorderX1, borderX1);
oldBorderY1 = Math::lerp(oldBorderY1, borderY1);
oldBorderX2 = Math::lerp(oldBorderX2, borderX2);
oldBorderY2 = Math::lerp(oldBorderY2, borderY2);
}
return buttonPressedFrame && selected;
}
void UI::selectNextButton() {
if (buttonCurrentlySelected < buttonCurrentlyDrawing) {
buttonCurrentlySelected++;
}
}
void UI::selectPreviousButton() {
if (buttonCurrentlySelected > 0) {
buttonCurrentlySelected--;
}
}
void UI::pressButton() {
buttonPressedFrame = true;
}
int UI::nextY() {
int currentYPos = nextYPos;
nextYPos += Graphics::UI::CHAR_HEIGHT + LINE_PADDING;
return currentYPos;
}
};
