Skip to content

Commit

Permalink
update: MenuItemWiFiClient ( WiFi icon and connect icon)
Browse files Browse the repository at this point in the history
update: MenuItem focusEnter supports setFocusItem in onEnter.
  • Loading branch information
lovyan03 committed Mar 22, 2019
1 parent 79f2047 commit 6d7c60d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
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.2.6",
"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=M5Stack_TreeView
version=0.2.5
version=0.2.6
author=lovyan03
maintainer=Lovyan <[email protected]>
sentence=TreeView Menu UI for M5Stack
Expand Down
31 changes: 20 additions & 11 deletions src/MenuItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ void MenuItem::drawTitle(bool selected, const String& text)

void MenuItem::drawText(String text, int16_t x, int16_t y)
{
int rp = getRightPadding();
while (text != "") {
int w = M5.Lcd.textWidth(text);
int over = (x + w) - rect.right();
int over = (x + w + rp) - rect.right();
if (0 < over) {
text = text.substring(0, text.length() - 1 - over / 12);
} else break;
Expand Down Expand Up @@ -199,21 +200,25 @@ void MenuItem::focusPrev() {
scrollTarget(focusItem);
}
bool MenuItem::focusEnter() {
MenuItem* oldFI = focusItem;
focusItem->onEnter();
if (!focusItem->Items.empty())
{ // Itemsがある場合はツリー展開
MenuItem* mi = focusItem;
setFocusItem(focusItem->Items[0]);
if (!oldFI->Items.empty())
{ // Expand the tree if the item has children.
if (oldFI == focusItem)
{ // If focus item isn't changed by onEnter, change focus to the first child.
MenuItem* mi = oldFI->Items[0];
setFocusItem(mi);
}
updateDest();
scrollSubitemArea(mi);
scrollTarget(mi);
scrollSubitemArea(oldFI);
scrollTarget(oldFI);
} else
{ // Itemsを持たない場合はコールバック呼び出し
MenuItem* mi = focusItem;
// コールバックが設定されているアイテムを検索(ツリーを遡る)
{ // Execute callback function if the item has no children.
MenuItem* mi = oldFI;
// Find the parent that holds the callback.
while (mi != this && !mi->callback) { mi = mi->parentItem(); }
if (mi->callback) {
mi->callback(focusItem);
mi->callback(oldFI);
_btnDrawer.draw(true);
return true;
}
Expand All @@ -228,6 +233,10 @@ void MenuItem::setFocusItem(MenuItem* newmi)
focusItem->onFocus();
}

MenuItem* MenuItem::getFocusItem() {
return focusItem;
}

void MenuItem::onAfterDraw()
{
if (!Items.empty()) {
Expand Down
6 changes: 5 additions & 1 deletion src/MenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class MenuItem {

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

MenuItem* const parentItem() const { return _parentItem; }
MenuItem* const topItem() { return _parentItem ? _parentItem->topItem() : this; }
Expand All @@ -57,7 +59,6 @@ class MenuItem {
void focusNext();
void focusPrev();
bool focusEnter();
void setFocusItem(MenuItem* newmi);
MenuItem* draw(bool force = true, const Rect16* forceRect = 0, const Rect16* prevforceRect = 0, MenuItem* nextmi = NULL);
MenuItem* erase(bool force = true, MenuItem* nextmi = NULL);

Expand All @@ -67,6 +68,9 @@ class MenuItem {
virtual void onDefocus() { }
virtual void onAfterDraw();

// title right padding
virtual int getRightPadding() const { return 0; }

int16_t updateDest();
int16_t updateDestRect(int16_t x = 0, int16_t y = 0);

Expand Down
40 changes: 30 additions & 10 deletions src/MenuItemWiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,40 @@ void MenuItemWiFiClient::onExit() {
MenuItem::onExit();
}

static void drawAuthIcon(const Rect16 r, uint16_t color) {
M5.Lcd.fillRect(r.x, r.y+3, r.w , 3, color);
M5.Lcd.drawFastVLine(r.x+1, r.y+1, 2, color);
M5.Lcd.drawFastVLine(r.x+4, r.y+1, 2, color);
M5.Lcd.drawFastHLine(r.x+2, r.y , 2, color);
}

static void drawWiFiIcon(const Rect16 r, uint8_t power, uint16_t color) {
switch (power) {
case 4: M5.Lcd.drawFastHLine(r.x , r.y , 7, color);
case 3: M5.Lcd.drawFastHLine(r.x+1, r.y+2, 5, color);
case 2: M5.Lcd.drawFastHLine(r.x+2, r.y+4, 3, color);
default: M5.Lcd.drawPixel(r.x+3, r.y+6, color);
}
}

void MenuItemWiFiClient::onAfterDraw() {
if (ssid.length() == 0) return;
Rect16 r( rect.x + 4
, rect.y + (rect.h - 6) / 2
, 6
, 6);
int y = rect.y + (rect.h - 6) / 2;

uint16_t color (fontColor[this == focusItem ? 1 : 0]);

if (WiFi.SSID() == ssid) {
M5.Lcd.drawPixel(rect.x + 3, y + 4, color);
M5.Lcd.drawLine(rect.x + 4, y + 5, rect.x + 8, y + 1, color);
}

if (auth != WIFI_AUTH_OPEN) {
uint16_t color = fontColor[this == focusItem ? 1 : 0];
M5.Lcd.fillRect(r.x, r.y+3, r.w , 3, color);
M5.Lcd.drawFastVLine(r.x+1, r.y+1, 2, color);
M5.Lcd.drawFastVLine(r.x+4, r.y+1, 2, color);
M5.Lcd.drawFastHLine(r.x+2, r.y , 2, color);
drawAuthIcon( Rect16 ( rect.right() - 18, y, 6, 6)
, color);
}
M5.Lcd.drawRightString(String(rssi, DEC) + "dBm", rect.right() - 10, rect.y + (rect.h - M5.Lcd.fontHeight(font)) / 2, font);
drawWiFiIcon( Rect16 ( rect.right() - 10, y, 8, 8)
, (rssi < -90 ? 1 : (rssi < -80 ? 2 : (rssi < -70 ? 3 : 4)))
, color);
}
void MenuItemWiFiClient::scanWiFi()
{
Expand Down
2 changes: 2 additions & 0 deletions src/MenuItemWiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class MenuItemWiFiClient : public MenuItem {
virtual void onExit();
virtual void onAfterDraw();

virtual int getRightPadding() const { return 18; }

String ssid;
int8_t rssi;
wifi_auth_mode_t auth;
Expand Down

0 comments on commit 6d7c60d

Please sign in to comment.