diff --git a/examples/simple/simple.ino b/examples/simple/simple.ino index b1f4590..859b1b9 100644 --- a/examples/simple/simple.ino +++ b/examples/simple/simple.ino @@ -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. diff --git a/library.json b/library.json index 13f2c05..af1c7c7 100644 --- a/library.json +++ b/library.json @@ -14,7 +14,7 @@ "dependencies": { "name": "M5Stack" }, - "version": "0.2.5", + "version": "0.3.0", "framework": "arduino", "platforms": "espressif32", "build": { diff --git a/library.properties b/library.properties index b41d7e8..622b9d6 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=M5OnScreenKeyboard -version=0.2.5 +version=0.3.0 author=lovyan03 maintainer=Lovyan <42724151+lovyan03@users.noreply.github.com> sentence=OnScreenKeyboard for M5Stack diff --git a/src/M5ButtonDrawer.cpp b/src/M5ButtonDrawer.cpp index b380f96..34cc8c2 100644 --- a/src/M5ButtonDrawer.cpp +++ b/src/M5ButtonDrawer.cpp @@ -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); @@ -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); } diff --git a/src/M5ButtonDrawer.h b/src/M5ButtonDrawer.h index 0310eb9..b0568a7 100644 --- a/src/M5ButtonDrawer.h +++ b/src/M5ButtonDrawer.h @@ -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} @@ -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; diff --git a/src/M5OnScreenKeyboard.cpp b/src/M5OnScreenKeyboard.cpp index cf4ca52..0293c3d 100644 --- a/src/M5OnScreenKeyboard.cpp +++ b/src/M5OnScreenKeyboard.cpp @@ -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; @@ -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; @@ -248,7 +250,7 @@ bool M5OnScreenKeyboard::loop() { } } #endif -//#ifndef _M5JOYSTICK_H_ +#ifdef _M5JOYSTICK_H_ if (useJoyStick && JoyStick.update()) { if (!JoyStick.isNeutral()) { press = true; @@ -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 @@ -298,6 +298,8 @@ bool M5OnScreenKeyboard::loop() { , M5.Lcd.fontHeight(font) , (_msec / 150) % 2 ? textboxBackColor : textboxFontColor); } + updateButton(); + _btnDrawer.draw(); return true; } void M5OnScreenKeyboard::close() { @@ -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) { diff --git a/src/M5OnScreenKeyboard.h b/src/M5OnScreenKeyboard.h index 061feec..5c0bdd3 100644 --- a/src/M5OnScreenKeyboard.h +++ b/src/M5OnScreenKeyboard.h @@ -20,7 +20,6 @@ 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; @@ -28,6 +27,9 @@ class M5OnScreenKeyboard 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(); @@ -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 @@ -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(); };