Display-O-Tron menu.cancel not working?


#1

Hi !
(Should i say : Hi Phil ? ;) )

Continuing my RPG journey, i came to an odd issue.
The goal is to change back the color of the backlight to violet when i leave the dices to return to the main menu)

It works fine using the ‘select’ button but not with ‘cancel’ (the code is copy/paste from one to the other).
Any idea ?

#!/usr/bin/env python3

from random import randint as rng
from dot3k.menu import MenuOption
from dothat import backlight
import dothat.touch as nav
import buttonshim

class Dices(MenuOption):

def __init__(self, dice):
    self.which_dice = dice
    self.x = 0
    MenuOption.__init__(self)


def begin(self):
    self.score = self.roll()


def cancel(self):
    backlight.set_graph(0)
    backlight.rgb(255, 42, 255)
    buttonshim.set_pixel(0, 0, 0)
    return True


def select(self):
    backlight.set_graph(0)
    backlight.rgb(255, 42, 255)
    buttonshim.set_pixel(0, 0, 0)
    return True


def roll(self):
    result = rng(1, self.which_dice)
    return result


def redraw(self, menu):
    if self.which_dice == 100 and self.score >95:
        backlight.rgb(255, 42, 42)
        buttonshim.set_pixel(255, 42, 42)
        menu.write_row(0, "     D{} :".format(self.which_dice))
        menu.write_row(1, "     FAIL !")
        menu.write_row(2, "      {}".format(self.score))
    
    elif self.which_dice == 100 and self.score <=5:
        self.x += 3
        self.x %= 360
        backlight.sweep((360.0 - self.x) / 360.0)
        buttonshim.set_pixel(42, 255, 42)
        menu.write_row(0, "     D{} :".format(self.which_dice))
        menu.write_row(1, "    EPIC !")
        menu.write_row(2, "       {}".format(self.score))
        
    else:
        backlight.rgb(255, 42, 255)
        buttonshim.set_pixel(255, 42, 255)
        menu.write_row(0, "     D{} :".format(self.which_dice))
        menu.clear_row(1)
        menu.write_row(2, "       {}".format(self.score))

#2

The cancel option is hard-wired in the menu class to exit the current menu option, and can’t be overridden to perform any other function.

    def cancel(self):
        self.last_action = self.millis()
        if self.idle:
            self.idle = False
            self.idle_handler.cleanup()
            self.idle_handler.idling = False
            return True

        if self.mode == _MODE_NAV:
            self.exit_option()
        if self.mode == _MODE_ADJ:
            self.current_value().cleanup() # <-- this is what's called when you're in a plugin
            self.mode = _MODE_NAV

Whereas this is the handler for select():

    def select(self):
        """
        Handle "select" action
        """
        self.last_action = self.millis()
        if self.idle:
            self.idle = False
            self.idle_handler.cleanup()
            self.idle_handler.idling = False
            return True

        if self.mode == _MODE_NAV:
            self.select_option()
        elif self.mode == _MODE_ADJ:
            # The "select" call must return true to exit the adjust
            if self.current_value().select() is True:  # <-- the select method is called on the current plugin
                self.mode = _MODE_NAV
        elif self.mode == _MODE_TXT:
            if self.input_handler.select():
                self.finish_input()

#3

Ok, thanks for the detailed answer. :)