import gui

class Looping_btn:

    def __init__(self, x, y, width, height, options, font = 7, cb = None):
        self.options = options
        self.current = self.options[self.idx]
        self.idx = 0
        self.len = len(options) - 1
        self.text = [str(self.options[self.idx])]
        self.gui_l = [gui.CTRL_FLATBUTTON, x, y, width, height, font, self.text, self.btn_cb]
        self.cb = cb

    def btn_cb(self, btn):
        if self.idx == self.len:
            self.idx = 0
        else:
            self.idx += 1
        self.text[0] = str(self.options[self.idx])
        self.current = self.options[self.idx]
        if self.cb:
            self.cb(self.current)

    def set_btn(self, var = None, idx = None):
        if var != None:
            self.idx = self.options.index(var)
            self.text = [str(self.options[self.idx])]
        elif idx != None:
            self.idx = idx
            self.text = [str(self.options[self.idx])]
