Files
ESP8266lib/ext/lcd/ui/UIButton.h
kazu ccd7f119d3 worked on SPI, fixed some bugs
adjusted LCD code
added code for INA3221
worked on UI
2020-06-24 21:28:44 +02:00

105 lines
2.3 KiB
C++

#ifndef UI_BUTTON_H
#define UI_BUTTON_H
#include "UIElement.h"
class UIButton : public UIElement {
public:
class Listener {
public:
virtual void onClick(UIButton*) = 0;
};
private:
bool enabled = true;
const char* txt;
uint8_t txtH;
uint16_t txtW;
bool down = false;
const Color fillNormal = Color::fromRGB(180,180,180);
const Color fillDown = Color::fromRGB(120,120,120);
const Color fillFocus = Color::fromRGB(120,120,140);
const Color frameBright = Color::fromRGB(230,230,230);
const Color frameDark = Color::fromRGB(50,50,50);
const Color cText = Color::fromRGB(0,0,0);
const Color cTextDisabled = Color::fromRGB(140,140,140);
Listener* listener = nullptr;
static constexpr const char* TAG = "UIButton";
public:
UIButton(const char* txt) : txt(txt) {
txtW = fnt_f1.getWidth(txt);
txtH = fnt_f1.getHeight();
}
void setText(const char* txt) {
this->txt = txt;
}
const char* getText() const {
return this->txt;
}
void setListener(Listener* l) {
this->listener = l;
}
void setEnabled(const bool en) {
this->enabled = en;
setNeedsRedraw();
}
void draw(UIPainter& p) override {
if (focus) {
p.setFG( fillFocus );
} else {
p.setFG( down ? fillDown : fillNormal );
}
p.fillRect(rect);
p.setFG( down ? frameDark : frameBright );
// p.drawLine(rect.x, rect.y, rect.x+rect.w, rect.y); // top
// p.drawLine(rect.x, rect.y, rect.x, rect.y+rect.h); // left
p.drawLineHor(rect.x, rect.x+rect.w, rect.y); // top
p.drawLineVer(rect.y, rect.y+rect.h, rect.x); // left;
p.setFG( down ? frameBright : frameDark );
// p.drawLine(rect.x, rect.y+rect.h, rect.x+rect.w, rect.y+rect.h); // bottom
// p.drawLine(rect.x+rect.w, rect.y, rect.x+rect.w, rect.y+rect.h); // right
p.drawLineHor(rect.x, rect.x+rect.w, rect.y+rect.h); // bottom
p.drawLineVer(rect.y, rect.y+rect.h, rect.x+rect.w); // right;
p.setFG( enabled ? cText : cTextDisabled );
uint16_t o = down ? 1 : 0;
p.drawText(rect.x+rect.w/2-txtW/2+o, rect.y+rect.h/2-txtH/2+o, txt);
}
void onTouchDown(const uint16_t, const uint16_t) override {
ESP_LOGI(TAG, "onTouchDown()");
if (!enabled) {return;}
down = true;
setNeedsRedraw();
}
void onTouchUp() override {
if (!enabled) {return;}
down = false;
setNeedsRedraw();
if (listener) {listener->onClick(this);}
}
};
#endif // UI_BUTTON_H