Skip to content

Commit

Permalink
Add: setTextFont , setFreeFont
Browse files Browse the repository at this point in the history
  • Loading branch information
lovyan03 committed Feb 20, 2019
1 parent 60b9744 commit cede74d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 22 deletions.
10 changes: 7 additions & 3 deletions examples/simple/simple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ void setup() {
m5osk.textboxFontColor = 0xFFFF;
m5osk.textboxBackColor = 0x8410;
m5osk.keyHeight = 20;
m5osk.font = 2;
m5osk.setTextFont(2);
//m5osk.keyHeight = 24;
//m5osk.setFreeFont(&FreeMono9pt7b);
//m5osk.setFreeFont(&FreeSans9pt7b);
M5ButtonDrawer::fontColor[0] = 0xFFFF;
M5ButtonDrawer::fontColor[1] = 0x0000;
M5ButtonDrawer::backColor[0] = 0x0010;
M5ButtonDrawer::backColor[1] = 0xF79E;
M5ButtonDrawer::width = 90;
M5ButtonDrawer::width = 100;
M5ButtonDrawer::height = 20;
M5ButtonDrawer::font = 2;
M5ButtonDrawer::setTextFont(2);
//*/

/* // response speed change example.
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dependencies": {
"name": "M5Stack"
},
"version": "0.2.5",
"version": "0.3.0",
"framework": "arduino",
"platforms": "espressif32",
"build": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=M5OnScreenKeyboard
version=0.2.5
version=0.3.0
author=lovyan03
maintainer=Lovyan <[email protected]>
sentence=OnScreenKeyboard for M5Stack
Expand Down
25 changes: 15 additions & 10 deletions src/M5ButtonDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ uint16_t M5ButtonDrawer::fontColor[2] = { 0xffff,0xffff };
int16_t M5ButtonDrawer::width = 64;
int16_t M5ButtonDrawer::height = 14;
int16_t M5ButtonDrawer::font = 1;
const GFXfont* M5ButtonDrawer::gfxFont = NULL;

void M5ButtonDrawer::setText(const String& btnA, const String& btnB, const String& btnC) {
setText(0, btnA);
Expand All @@ -29,28 +30,32 @@ void M5ButtonDrawer::draw(bool force)
void M5ButtonDrawer::draw(uint8_t idx, bool pressed)
{
_mod[idx] = false;
drawButton(idx * 96 + 64, pressed, _titles[idx]);
drawButton((idx - 1) * (96 + (84 < width ? (width - 84) / 2 : 0)) + 160, pressed, _titles[idx]);
}

void M5ButtonDrawer::drawButton(int x, bool pressed, const String& title) const
{
M5.Lcd.setTextSize(1);
if (gfxFont) {
M5.Lcd.setFreeFont(gfxFont);
} else {
M5.Lcd.setTextFont(0);
M5.Lcd.setTextFont(font);
}
M5.Lcd.setTextColor(fontColor[pressed]);
int16_t fh = M5.Lcd.fontHeight(font);
if (gfxFont && 12 < fh) fh = fh * 9 / 10;

int rx = x - width / 2;
int ry = M5.Lcd.height() - height;
int rw = width;
int rh = height;
int fy = ry + (rh - M5.Lcd.fontHeight(font))/2;
int fy = ry + (rh - fh)/2;
uint16_t color = frameColor[pressed];
M5.Lcd.drawRect(rx+1,ry ,rw-2,rh ,color);
M5.Lcd.drawRect(rx ,ry+1,rw ,rh-2 ,color);
rx+=2;
ry+=2;
rw-=4;
rh-=4;

M5.Lcd.setTextSize(1);
M5.Lcd.setTextColor(fontColor[pressed]);

M5.Lcd.fillRect(rx, ry, rw, rh, backColor[pressed]);
M5.Lcd.fillRect(rx+2, ry+2, rw-4, rh-4, backColor[pressed]);
M5.Lcd.drawCentreString(title, x, fy, font);
}

5 changes: 4 additions & 1 deletion src/M5ButtonDrawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class M5ButtonDrawer {
static uint16_t fontColor[2];
static int16_t width;
static int16_t height;
static int16_t font;
static void setTextFont(int f) { gfxFont = NULL; font = f; }
static void setFreeFont(const GFXfont* f) { gfxFont = f; font = 1; }
M5ButtonDrawer(){};
M5ButtonDrawer(const String& btnA, const String& btnB, const String& btnC)
: _titles{btnA,btnB,btnC}
Expand All @@ -22,6 +23,8 @@ class M5ButtonDrawer {
void draw(uint8_t idx, bool pressed);

private:
static int16_t font;
static const GFXfont* gfxFont;
String _titles[3];
bool _mod[3];
void drawButton(int x, bool pressed, const String& title) const;
Expand Down
22 changes: 17 additions & 5 deletions src/M5OnScreenKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ uint16_t M5OnScreenKeyboard::backColor[2] = {0x630C, 0x421F};
uint16_t M5OnScreenKeyboard::frameColor[2] = {0x0208, 0xFFFF};
uint16_t M5OnScreenKeyboard::textboxFontColor = 0x0000;
uint16_t M5OnScreenKeyboard::textboxBackColor = 0xFFFF;
int16_t M5OnScreenKeyboard::font = 1;
uint8_t M5OnScreenKeyboard::keyHeight = 14;
int16_t M5OnScreenKeyboard::font = 1;
const GFXfont* M5OnScreenKeyboard::gfxFont = NULL;

uint16_t M5OnScreenKeyboard::msecHold = 300;
uint16_t M5OnScreenKeyboard::msecRepeat= 150;
Expand Down Expand Up @@ -115,6 +116,7 @@ void M5OnScreenKeyboard::setup(const String& value) {
}

bool M5OnScreenKeyboard::loop() {
applyFont();
_keyCode = 0;
M5.Lcd.setTextSize(1);
if (_state == APPEAR && !appear()) return true;
Expand Down Expand Up @@ -248,7 +250,7 @@ bool M5OnScreenKeyboard::loop() {
}
}
#endif
//#ifndef _M5JOYSTICK_H_
#ifdef _M5JOYSTICK_H_
if (useJoyStick && JoyStick.update()) {
if (!JoyStick.isNeutral()) {
press = true;
Expand All @@ -263,9 +265,7 @@ bool M5OnScreenKeyboard::loop() {
if (JoyStick.wasClicked()) { ++_repeat; pressKey(); }
if (JoyStick.wasHold()) { switchTable(); }
}
//#endif
updateButton();
_btnDrawer.draw();
#endif
if (oldCol != _col
|| oldRow != _row
|| oldTbl != _tbl
Expand Down Expand Up @@ -298,6 +298,8 @@ bool M5OnScreenKeyboard::loop() {
, M5.Lcd.fontHeight(font)
, (_msec / 150) % 2 ? textboxBackColor : textboxFontColor);
}
updateButton();
_btnDrawer.draw();
return true;
}
void M5OnScreenKeyboard::close() {
Expand Down Expand Up @@ -530,6 +532,16 @@ void M5OnScreenKeyboard::drawColumn(int col, int x, int y, int h) {
}
}

void M5OnScreenKeyboard::applyFont()
{
if (gfxFont) {
M5.Lcd.setFreeFont(gfxFont);
} else {
M5.Lcd.setTextFont(0);
M5.Lcd.setTextFont(font);
}
}

bool M5OnScreenKeyboard::appear() {
int tmp = (millis() - _msec) / 2;
if (tmp < keyHeight * ROWCOUNT) {
Expand Down
8 changes: 7 additions & 1 deletion src/M5OnScreenKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ class M5OnScreenKeyboard
static uint16_t frameColor[2];
static uint16_t textboxFontColor;
static uint16_t textboxBackColor;
static int16_t font;
static uint8_t keyHeight;

static uint16_t msecHold;
static uint16_t msecRepeat;
static uint16_t msecMorseInput;
static uint8_t maxlength;

static void setTextFont(int f) { gfxFont = NULL; font = f; }
static void setFreeFont(const GFXfont* f) { gfxFont = f; font = 1; }

void setup(const String& value = "");
bool loop();
void close();
Expand All @@ -38,6 +40,9 @@ class M5OnScreenKeyboard
void setString(const String& value = "");
char getKeyCode() const { return _keyCode; }
private:
static int16_t font;
static const GFXfont* gfxFont;

enum eState
{ APPEAR
, LEFTRIGHT
Expand Down Expand Up @@ -76,6 +81,7 @@ class M5OnScreenKeyboard
void drawKeyboard(int h = -1);
void drawColumn(int col);
void drawColumn(int col, int x, int y, int h);
void applyFont();
bool appear();
};

Expand Down

0 comments on commit cede74d

Please sign in to comment.