def __init__(self): super().__init__('background_images/restaurantScene.png', constants.RESTAURANT_SCENE) self._close_restaurant_button = Button( constants.CLOSE_RESTAURANT_BUTTON_TEXT, constants.RESTAURANT_SCENE_CLOSE_BUTTON_COORDS, constants.RESTAURANT_SCENE_CLOSE_BUTTON_DIMENSIONS)
def __init__(self, filename, **kwds): text = get_text(filename) text_pages = text.split("\nPAGE\n") pages = [] page_size = (0, 0) for text_page in text_pages: lines = text_page.strip().split("\n") page = Page(self, lines[0], lines[1:]) pages.append(page) page_size = maximum(page_size, page.size) self.pages = pages bf = self.button_font b1 = Button("Prev Page", font=bf, action=self.prev_page) b2 = Button("Menu", font=bf, action=self.go_back) b3 = Button("Next Page", font=bf, action=self.next_page) b = self.margin page_rect = Rect((b, b), page_size) gap = (0, 18) b1.rect.topleft = add(page_rect.bottomleft, gap) b2.rect.midtop = add(page_rect.midbottom, gap) b3.rect.topright = add(page_rect.bottomright, gap) Screen.__init__(self, Rect((0, 0), add(b3.rect.bottomright, (b, b))), **kwds) self.add(b1) self.add(b2) self.add(b3) self.prev_button = b1 self.next_button = b3 self.set_current_page(0)
def __init__(self): super().__init__('background_images/leavingWorkScene.png', constants.LEAVING_WORK_SCENE) self._skip_button = Button( constants.LEAVING_WORK_SCENE_SKIP_BUTTON_TEXT, constants.MAIN_MENU_EXIT_BUTTON_COORDS, constants.MAIN_MENU_EXIT_BUTTON_DIMENSIONS)
def __init__(self, controls=[], window_side=WindowSide.LEFT): super().__init__() self.is_expanded = True self.controls = controls self.window_side = window_side self.sidebar_bck = go.Rect((0, 0), (0, 0)) self.btn_expand = Button(self.get_btn_txt(), on_click=self.toggle_expand) self.min_w, self.min_h = self.min_size = self.btn_expand.size self.set_size(self.assay_size()) self.set_position(self.assay_position()) self.wire_events()
def __init__(self, items, keysColumn=None, buttonsColumn=None, item_spacing=None): if keysColumn is None: keysColumn = [] if buttonsColumn is None: buttonsColumn = [] Widget.__init__(self) for t in items: if len(t) == 3: (hotkey, title, action) = t tooltipText = None else: (hotkey, title, action, tooltipText) = t if isinstance(title, (str, unicode)): button = Button(title, action=action) else: button = ValueButton(ref=title, action=action, width=200) button.anchor = self.anchor label = Label(hotkey, width=75, margin=button.margin) label.anchor = "wh" label.height = button.height if tooltipText: button.tooltipText = tooltipText keysColumn.append(label) buttonsColumn.append(button) self.buttons = list(buttonsColumn) #.# if item_spacing == None: buttonsColumn = Column(buttonsColumn) else: buttonsColumn = Column(buttonsColumn, spacing=item_spacing) #.# buttonsColumn.anchor = self.anchor #.# if item_spacing == None: keysColumn = Column(keysColumn) else: keysColumn = Column(keysColumn, spacing=item_spacing) commandRow = Row((keysColumn, buttonsColumn)) self.add(commandRow) self.shrink_wrap()
def __init__(self): super().__init__('background_images/alarmClockScene.png', constants.ALARM_CLOCK_SCENE) root = utilities.DialogTreeParser.get_instance().parse( '../writing/test_dialog.json') self._dialog_box = DialogBox(constants.DIALOG_BOX_ORIENTATION_RIGHT, constants.DIALOG_BOX_COORDS, constants.DIALOG_BOX_DIMENSIONS, root, 'control_assets/temp_clock_speaker.png') self._skip_button = Button( constants.ALARM_CLOCK_SCENE_SKIP_BUTTON_TEXT, constants.ALARM_CLOCK_SCENE_SKIP_BUTTON_COORDS, constants.ALARM_CLOCK_SCENE_SKIP_BUTTON_DIMENSIONS)
def main(): pygame.init() screen = setup_screen() pygame.display.set_caption(Settings.APP_NAME) play_button = Button(Settings, screen, "Play") stats = GameStats(Settings) sb = Scoreboard(Settings, screen, stats) ship = Ship(screen, Settings.SHIP_SPEED) bullets = Group() aliens = Group() gf.create_fleet(Settings, screen, ship, aliens) while 1: gf.check_events(Settings, screen, sb, play_button, stats, ship, aliens, bullets) if stats._game_active: ship.update() gf.update_bullets(Settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(Settings, screen, sb, stats, ship, aliens, bullets) gf.update_screen(Settings, screen, sb, stats, ship, aliens, bullets, play_button)
def _create_tr_button(self): if self.has_ok: self.top_right_button = Button(self, 'Ok', action=lambda ev: self.onok()) else: CeFrame._create_tr_button(self)
def __init__(self, client=None, responses=None, default=0, cancel=-1, **kwds): Widget.__init__(self, **kwds) if client or responses: rows = [] w1 = 0 w2 = 0 if client: rows.append(client) w1 = client.width if responses: buttons = Row([ Button(text, action=lambda t=text: self.dismiss(t)) for text in responses]) rows.append(buttons) w2 = buttons.width if w1 < w2: a = 'l' else: a = 'r' contents = Column(rows, align=a) m = self.margin contents.topleft = (m, m) self.add(contents) self.shrink_wrap() if responses and default is not None: self.enter_response = responses[default] if responses and cancel is not None: self.cancel_response = responses[cancel]
def ask(mess, responses=["OK", "Cancel"], default=0, cancel=-1, wrap_width=60, **kwds): box = Dialog(**kwds) d = box.margin lb = wrapped_label(mess, wrap_width) lb.topleft = (d, d) buts = [] for caption in responses: but = Button(caption, action=lambda x=caption: box.dismiss(x)) buts.append(but) brow = Row(buts, spacing=d) lb.width = max(lb.width, brow.width) col = Column([lb, brow], spacing=d, align='r') col.topleft = (d, d) if default is not None: box.enter_response = responses[default] buts[default].is_default = True else: box.enter_response = None if cancel is not None: box.cancel_response = responses[cancel] else: box.cancel_response = None box.add(col) box.shrink_wrap() return box.present()
class RestaurantScene(Scene): def __init__(self): super().__init__('background_images/restaurantScene.png', constants.RESTAURANT_SCENE) self._close_restaurant_button = Button( constants.CLOSE_RESTAURANT_BUTTON_TEXT, constants.RESTAURANT_SCENE_CLOSE_BUTTON_COORDS, constants.RESTAURANT_SCENE_CLOSE_BUTTON_DIMENSIONS) def handle_key_down(self, key): pass def handle_mouse_motion(self, coords): self._close_restaurant_button.handle_mouse_motion(coords) def handle_mouse_button_up(self, coords): if self._close_restaurant_button.handle_mouse_button_up(coords): raise TransitionScene def handle_mouse_button_down(self, coords): self._close_restaurant_button.handle_mouse_button_down(coords) def draw(self, screen): super().draw(screen) # TODO sprite = pygame.image.load( utilities.relative_path('../sprites/defaultSprite.png', __file__)) screen.blit(sprite, constants.ORIGIN) self._close_restaurant_button.draw(screen)
class LeavingWorkScene(Scene): def __init__(self): super().__init__('background_images/leavingWorkScene.png', constants.LEAVING_WORK_SCENE) self._skip_button = Button( constants.LEAVING_WORK_SCENE_SKIP_BUTTON_TEXT, constants.MAIN_MENU_EXIT_BUTTON_COORDS, constants.MAIN_MENU_EXIT_BUTTON_DIMENSIONS) def handle_key_down(self, key): pass def handle_mouse_motion(self, coords): self._skip_button.handle_mouse_motion(coords) def handle_mouse_button_up(self, coords): if self._skip_button.handle_mouse_button_up(coords): raise TransitionScene def handle_mouse_button_down(self, coords): self._skip_button.handle_mouse_button_down(coords) def draw(self, screen): super().draw(screen) self._skip_button.draw(screen)
class AlarmClockScene(Scene): def __init__(self): super().__init__('background_images/alarmClockScene.png', constants.ALARM_CLOCK_SCENE) root = utilities.DialogTreeParser.get_instance().parse( '../writing/test_dialog.json') self._dialog_box = DialogBox(constants.DIALOG_BOX_ORIENTATION_RIGHT, constants.DIALOG_BOX_COORDS, constants.DIALOG_BOX_DIMENSIONS, root, 'control_assets/temp_clock_speaker.png') self._skip_button = Button( constants.ALARM_CLOCK_SCENE_SKIP_BUTTON_TEXT, constants.ALARM_CLOCK_SCENE_SKIP_BUTTON_COORDS, constants.ALARM_CLOCK_SCENE_SKIP_BUTTON_DIMENSIONS) def handle_key_down(self, key): self._dialog_box.handle_key_down(key) def handle_mouse_motion(self, coords): self._skip_button.handle_mouse_motion(coords) def handle_mouse_button_up(self, coords): if self._skip_button.handle_mouse_button_up(coords): raise TransitionScene def handle_mouse_button_down(self, coords): self._skip_button.handle_mouse_button_down(coords) def draw(self, screen): super().draw(screen) self._dialog_box.draw(screen) self._skip_button.draw(screen)
def ask(mess, responses=["OK", "Cancel"], default=0, cancel=-1, wrap_width=60, **kwds): # If height is specified as a keyword, the Dialog object will have this haight, and the inner massage will # be displayed in a scrolling widget colLbl = kwds.pop('colLabel', "") box = Dialog(**kwds) d = box.margin lb = wrapped_label(mess, wrap_width) buts = [] for caption in responses: but = Button(caption, action=lambda x=caption: box.dismiss(x)) buts.append(but) brow = Row(buts, spacing=d) lb.width = max(lb.width, brow.width) height = kwds.get('height', None) if height and lb.height > height - (2 * brow.height) - ScrollPanel.column_margin - box.margin: lines = mess.split('\n') # ScrolledPanel refuses to render properly for now, so Tableview is used instead. w = TableView().font.size(_(colLbl))[0] lb = TableView(columns=[TableColumn(colLbl, max(lb.width, w)),], nrows=(height - (2 * brow.height) - box.margin - box.font.get_linesize()) / box.font.get_linesize(), height=height - (2 * brow.height) - (box.font.get_linesize() * 2) - box.margin) #, width=w) def num_rows(): return len(lines) lb.num_rows = num_rows lb.row_data = lambda x: (lines[x],) lb.topleft = (d, d) lb.width = max(lb.width, brow.width) col = Column([lb, brow], spacing=d, align='r') col.topleft = (d, d) if default is not None: box.enter_response = responses[default] buts[default].is_default = True else: box.enter_response = None if cancel is not None: box.cancel_response = responses[cancel] else: box.cancel_response = None box.add(col) box.shrink_wrap() def dispatchKeyForAsk(name, evt): if name == "key_down": if box.root.getKey(evt) == "Return": if default is not None: box.dismiss(responses[default]) box.dispatch_key = dispatchKeyForAsk return box.present()
def on_draw(self, display: SH1106) -> None: # clear display.fill(0) # Logo self.draw_logo(display, 56, 59) display.line(3, 60, 52, 60, 1) display.line(75, 60, 124, 60, 1) # Delay display.text("Delay", 8, 8, 1) self.draw_arrow(display, 51, 8) delay_len: int = int( (math.log10(self.delay) if self.delay > 0 else 0) + 1) display.fill_rect(62, 8, delay_len * 8 + 2, 10, 1) display.text(str(self.delay), 63, 9, 0) display.text("ms", 63 + delay_len * 8 + 2, 9, 1) # Blob display.text("Blob", 8, 20, 1) self.draw_arrow(display, 51, 20) blob_len: int = int((math.log10(self.blob) if self.blob > 0 else 0) + 1) display.fill_rect(62, 20, blob_len * 8 + 2, 10, 1) display.text(str(self.blob), 63, 21, 0) display.text("ms", 63 + blob_len * 8 + 2, 21, 1) # Test test_btn: Button = Button(8, 39, "TEST", width=38, height=13) test_btn.hover = self.test test_btn.draw(display) # Active active_btn: Button = Button(82, 39, "ACT", width=38, height=13) active_btn.hover = self.active active_btn.draw(display) # Camera self.draw_camera(display, 56, 39) display.show()
class MainMenuScene(Scene): def __init__(self): super().__init__('background_images/mainMenuScene.png', constants.MAIN_MENU_SCENE) self._exit_button = Button(constants.MAIN_MENU_EXIT_BUTTON_TEXT, constants.MAIN_MENU_EXIT_BUTTON_COORDS, constants.MAIN_MENU_EXIT_BUTTON_DIMENSIONS) self._settings_button = Button( constants.MAIN_MENU_SETTINGS_BUTTON_TEXT, constants.MAIN_MENU_SETTINGS_BUTTON_COORDS, constants.MAIN_MENU_SETTINGS_BUTTON_DIMENSIONS) self._play_button = Button(constants.MAIN_MENU_PLAY_BUTTON_TEXT, constants.MAIN_MENU_PLAY_BUTTON_COORDS, constants.MAIN_MENU_PLAY_BUTTON_DIMENSIONS) self._buttons = [ self._exit_button, self._settings_button, self._play_button ] def handle_key_down(self, key): pass def handle_mouse_motion(self, coords): for button in self._buttons: button.handle_mouse_motion(coords) def handle_mouse_button_up(self, coords): if self._exit_button.handle_mouse_button_up(coords): raise Terminate if self._settings_button.handle_mouse_button_up(coords): print('Settings button clicked.') if self._play_button.handle_mouse_button_up(coords): raise StartGameLoop def handle_mouse_button_down(self, coords): for button in self._buttons: button.handle_mouse_button_down(coords) def draw(self, screen): super().draw(screen) for button in self._buttons: button.draw(screen)
def input_text_buttons(prompt, width, initial=None, allowed_chars=None, **kwds): box = Dialog(**kwds) d = box.margin def ok(): box.dismiss(True) def cancel(): box.dismiss(False) buts = [Button("OK", action=ok), Button("Cancel", action=cancel)] brow = Row(buts, spacing=d) lb = Label(prompt) lb.topleft = (d, d) tf = TextFieldWrapped(width, allowed_chars=allowed_chars) if initial: tf.set_text(initial) tf.enter_action = ok tf.escape_action = cancel tf.top = lb.top tf.left = lb.right + 5 trow = Row([lb, tf], spacing=d) col = Column([trow, brow], spacing=d, align='c') col.topleft = (d, d) box.add(col) tf.focus() box.shrink_wrap() if box.present(): return tf.get_text() else: return None
def __init__(self, shell, filename, **kwds): text = get_text(filename) text_pages = text.split("\nPAGE\n") pages = [] page_size = (0, 0) for text_page in text_pages: lines = text_page.strip().split("\n") page = Page(self, lines[0], lines[1:]) pages.append(page) page_size = maximum(page_size, page.size) self.pages = pages bf = self.button_font b1 = Button("Prev Page", font=bf, action=self.prev_page) b2 = Button("Menu", font=bf, action=self.go_back) b3 = Button("Next Page", font=bf, action=self.next_page) b = self.margin page_rect = Rect((b, b), page_size) gap = (0, 18) b1.topleft = add(page_rect.bottomleft, gap) b2.midtop = add(page_rect.midbottom, gap) b3.topright = add(page_rect.bottomright, gap) Screen.__init__(self, shell, **kwds) self.size = add(b3.bottomright, (b, b)) self.add(b1) self.add(b2) self.add(b3) self.prev_button = b1 self.next_button = b3 self.set_current_page(0)
def __init__(self): BaseWindow.__init__(self, width=400, height=140, caption='Configuracion') # eventos self.on_configured = None # controles self.player_name_label = pyglet.text.Label('Jugador', x=10, y=100, anchor_y='bottom', color=(255, 255, 255, 255), batch=self.batch) self.server_address_label = pyglet.text.Label('Servidor', x=10, y=60, anchor_y='bottom', color=(255, 255, 255, 255), batch=self.batch) self.message_label = pyglet.text.Label('', x=self.width / 2, y=140, anchor_x='center', anchor_y='bottom', color=(255, 255, 255, 255), batch=self.batch) self.player_name_textbox = TextBox(self, 200, 100, self.width - 210, 20, 'Jugador1', (255, 255, 255, 255), (0, 0, 0, 255)) self.controls.append(self.player_name_textbox) self.server_address_textbox = TextBox(self, 200, 60, self.width - 210, 20, 'localhost', (255, 255, 255, 255), (0, 0, 0, 255)) self.controls.append(self.server_address_textbox) self.join_button = Button(self, (self.width - 30) / 2 + 20, 20, (self.width - 30) / 2, 20, 'Conectar a Servidor', (204, 204, 204, 255), (0, 0, 0, 255)) self.controls.append(self.join_button) self.join_button.click = self.join_button_click
def __init__(self): super().__init__('background_images/mainMenuScene.png', constants.MAIN_MENU_SCENE) self._exit_button = Button(constants.MAIN_MENU_EXIT_BUTTON_TEXT, constants.MAIN_MENU_EXIT_BUTTON_COORDS, constants.MAIN_MENU_EXIT_BUTTON_DIMENSIONS) self._settings_button = Button( constants.MAIN_MENU_SETTINGS_BUTTON_TEXT, constants.MAIN_MENU_SETTINGS_BUTTON_COORDS, constants.MAIN_MENU_SETTINGS_BUTTON_DIMENSIONS) self._play_button = Button(constants.MAIN_MENU_PLAY_BUTTON_TEXT, constants.MAIN_MENU_PLAY_BUTTON_COORDS, constants.MAIN_MENU_PLAY_BUTTON_DIMENSIONS) self._buttons = [ self._exit_button, self._settings_button, self._play_button ]
def buildWidgets(self): keysColumn = self.keysColumn buttonsColumn = self.buttonsColumn items = self.items item_spacing = self.item_spacing if keysColumn is None or True: keysColumn = [] if buttonsColumn is None or True: buttonsColumn = [] labels = [] for w in self.subwidgets: for _w in w.subwidgets: w.remove(_w) self.remove(w) for i, t in enumerate(items): if type(self.translateButtons) is bool: trn = not self.translateButtons elif type(self.translateButtons) in (list, tuple): trn = not i in self.translateButtons if len(t) == 3: (hotkey, title, action) = t tooltipText = None else: (hotkey, title, action, tooltipText) = t if isinstance(title, (str, unicode)): button = Button(title, action=action, doNotTranslate=trn) else: button = ValueButton(ref=title, action=action, width=200, doNotTranslate=trn) button.anchor = self.anchor label = Label(hotkey, width=100, margin=button.margin) label.anchor = "wh" label.height = button.height labels.append(label) if tooltipText: button.tooltipText = tooltipText keysColumn.append(label) buttonsColumn.append(button) self.buttons = list(buttonsColumn) #.# if item_spacing == None: buttonsColumn = Column(buttonsColumn) else: buttonsColumn = Column(buttonsColumn, spacing=item_spacing) #.# buttonsColumn.anchor = self.anchor #.# if item_spacing == None: keysColumn = Column(keysColumn) else: keysColumn = Column(keysColumn, spacing=item_spacing) commandRow = Row((keysColumn, buttonsColumn)) self.labels = labels self.add(commandRow) self.shrink_wrap() self.invalidate()
def showProgress(progressText, progressIterator, cancel=False): """Show the progress for a long-running synchronous operation. progressIterator should be a generator-like object that can return either None, for an indeterminate indicator, A float value between 0.0 and 1.0 for a determinate indicator, A string, to update the progress info label or a tuple of (float value, string) to set the progress and update the label""" class ProgressWidget(Dialog): progressFraction = 0.0 firstDraw = False root = None def draw(self, surface): if self.root is None: self.root = self.get_root() Widget.draw(self, surface) frameStart = datetime.now() frameInterval = timedelta(0, 1, 0) / 2 amount = None try: while datetime.now() < frameStart + frameInterval: amount = progressIterator.next() if self.firstDraw is False: self.firstDraw = True break except StopIteration: self.dismiss() infoText = "" if amount is not None: if isinstance(amount, tuple): if len(amount) > 2: infoText = ": " + amount[2] amount, max = amount[:2] else: max = amount maxwidth = (self.width - self.margin * 2) if amount is None: self.progressBar.width = maxwidth self.progressBar.bg_color = (255, 255, 25, 255) elif isinstance(amount, basestring): self.statusText = amount else: self.progressAmount = amount if isinstance(amount, (int, float)): self.progressFraction = float(amount) / (float(max) or 1) self.progressBar.width = maxwidth * self.progressFraction self.statusText = str("{0} / {1}".format(amount, max)) else: self.statusText = str(amount) if infoText: self.statusText += infoText @property def estimateText(self): delta = (datetime.now() - self.startTime) progressPercent = (int(self.progressFraction * 10000)) left = delta * (10000 - progressPercent) / (progressPercent or 1) return _("Time left: {0}").format(left) def cancel(self): if cancel: self.dismiss(False) def idleevent(self, evt): self.invalidate() def key_down(self, event): pass def key_up(self, event): pass def mouse_up(self, event): try: if "SelectionTool" in str(self.root.editor.currentTool): if self.root.get_nudge_block().count > 0: self.root.get_nudge_block().mouse_up(event) except: pass widget = ProgressWidget() widget.progressText = _(progressText) widget.statusText = "" widget.progressAmount = 0.0 progressLabel = ValueDisplay(ref=AttrRef(widget, 'progressText'), width=550) statusLabel = ValueDisplay(ref=AttrRef(widget, 'statusText'), width=550) estimateLabel = ValueDisplay(ref=AttrRef(widget, 'estimateText'), width=550) progressBar = Widget(size=(550, 20), bg_color=(150, 150, 150, 255)) widget.progressBar = progressBar col = (progressLabel, statusLabel, estimateLabel, progressBar) if cancel: cancelButton = Button("Cancel", action=widget.cancel, fg_color=(255, 0, 0, 255)) col += (Column((cancelButton, ), align="r"), ) widget.add(Column(col)) widget.shrink_wrap() widget.startTime = datetime.now() if widget.present(): return widget.progressAmount else: return "Canceled"
def __init__(self, title, choices, **kw): Button.__init__(self, title, **kw) self.choices = choices # self.menu = Menu(title, ((c, c) for c in choices)) self.menu = Menu(title, ((c, None) for c in choices))
def bind(self, clicked=None, **kw): Button.bind(self, clicked=clicked, **kw)
def __init__(self, parent, name, action): Button.__init__(self, parent, name, action)
def __init__(self, parent, title, menu=None): Button.__init__(self, parent, title, action=self.on_click) self.menu = menu
class SideBar(Control): def __init__(self, controls=[], window_side=WindowSide.LEFT): super().__init__() self.is_expanded = True self.controls = controls self.window_side = window_side self.sidebar_bck = go.Rect((0, 0), (0, 0)) self.btn_expand = Button(self.get_btn_txt(), on_click=self.toggle_expand) self.min_w, self.min_h = self.min_size = self.btn_expand.size self.set_size(self.assay_size()) self.set_position(self.assay_position()) self.wire_events() def wire_events(self): imp.IMP().add_listener( events.UserEvent(CustomEvent.REFRESH_UI).create( self.on_refresh_sidebar)) imp.IMP().add_listener(events.WindowResizedEvent().create( self.on_resize)) def on_resize(self, event): self.on_refresh_sidebar(event) def on_refresh_sidebar(self, event): self.set_size(self.assay_size()) self.set_position(self.assay_position()) def set_enabled(self, is_enabled): super().set_enabled(is_enabled) self.btn_expand.set_enabled(is_enabled) for control in self.controls: control.set_enabled(is_enabled) def set_btn_txt(self, btn_txt): self.btn_expand.set_text(btn_txt) self.min_w, self.min_h = self.min_size = self.btn_expand.size def set_window_side(self, window_side): self.window_side = window_side self.set_btn_txt(self.get_btn_txt()) self.set_size(self.assay_size()) self.set_position(self.assay_position()) def set_size(self, size): super().set_size(size) self.sidebar_bck.set_size(size) def set_position(self, left_top): super().set_position(left_top) self.btn_expand.set_position(self.get_btn_pos()) self.sidebar_bck.set_position(left_top) self.position_controls() def assay_size(self): return (self.get_w(), self.get_h()) def assay_position(self): window_w, window_h = imp.IMP().screen.size #WindowSide.LEFT and WindowSide.TOP left, top = 0, 0 if self.window_side == WindowSide.RIGHT: left = window_w - self.w elif self.window_side == WindowSide.BOTTOM: top = window_h - self.h return (left, top) def get_btn_pos(self): if self.window_side == WindowSide.LEFT: return (self.right - self.btn_expand.w, self.top) elif self.window_side == WindowSide.RIGHT: return self.left_top elif self.window_side == WindowSide.TOP: return (self.left, self.bottom - self.btn_expand.h) else: #self.window_side == WindowSide.BOTTOM: return self.left_top def get_btn_txt(self): left_btn_text = '<<<' right_btn_text = '>>>' top_btn_text = '/\\' bottom_btn_text = '\\/' if not self.is_expanded: left_btn_text, right_btn_text = right_btn_text, left_btn_text top_btn_text, bottom_btn_text = bottom_btn_text, top_btn_text if self.window_side == WindowSide.LEFT: return left_btn_text elif self.window_side == WindowSide.RIGHT: return right_btn_text elif self.window_side == WindowSide.TOP: return top_btn_text else: #self.window_side == WindowSide.BOTTOM: return bottom_btn_text def toggle_expand(self, event): self.is_expanded = not self.is_expanded if self.is_expanded: self.show_controls() else: self.hide_controls() self.set_btn_txt(self.get_btn_txt()) self.set_size(self.assay_size()) self.set_position(self.assay_position()) def hide_controls(self): for control in self.controls: control.set_visibility(False) def show_controls(self): for control in self.controls: control.set_visibility(True) def is_horizontal(self): return self.window_side in [WindowSide.TOP, WindowSide.BOTTOM] def is_vertical(self): return self.window_side in [WindowSide.LEFT, WindowSide.RIGHT] def position_controls(self): if self.is_vertical(): x = self.x y = self.top + self.btn_expand.h + self.mh for control in self.controls: control.center_on((x, y + control.h // 2)) y += control.h + self.mh else: x = self.left + self.btn_expand.w + self.mw y = self.top for control in self.controls: control.center_on((x + control.w // 2, y + 3 * control.h // 4)) x += control.w + self.mw def get_w(self): if not self.is_vertical(): return imp.IMP().screen.w if not self.is_expanded or not any(self.controls): return self.min_w return self.get_max_width() def get_max_width(self): max_width = 0 for control in self.controls: width = control.w if width > max_width: max_width = width return max_width + self.mw def get_h(self): if not self.is_horizontal(): return imp.IMP().screen.h if not self.is_expanded or not any(self.controls): return self.min_h return self.get_max_height() def get_max_height(self): max_height = 0 for control in self.controls: height = control.h if height > max_height: max_height = height return max_height + self.mh def update(self): for control in self.controls: control.update() def draw(self, surface): if self.is_visible: self.sidebar_bck.draw(surface, self.get_style('sidebar').color) self.btn_expand.draw(surface) for control in self.controls: control.draw(surface)
def ask(mess, responses=("OK", "Cancel"), default=0, cancel=-1, wrap_width=60, **kwds): # If height is specified as a keyword, the Dialog object will have this haight, and the inner massage will # be displayed in a scrolling widget # If 'responses_tooltips' it present in kwds, it must be dict with 'responses' as keys and the tooltips as values. # If 'tie_widget_to' is in kwds keys and the value is True, the global 'ask_tied_to' will be set up to the 'box'. # When called again with 'tie_widget_to', if 'ask_tied_to' is not None, the function returns a dummy answer. global ask_tied_to responses_tooltips = kwds.pop('responses_tooltips', {}) tie_widget_to = kwds.pop('tie_widget_to', None) # Return a dummy answer if tie_windget_to is True and the global 'ask_tied_to' is not None if tie_widget_to and ask_tied_to: return "_OK" colLbl = kwds.pop('colLabel', "") box = Dialog(**kwds) d = box.margin lb = wrapped_label(mess, wrap_width) buts = [] for caption in responses: but = Button(caption, action=lambda x=caption: box.dismiss(x)) if caption in responses_tooltips.keys(): but.tooltipText = responses_tooltips[caption] buts.append(but) brow = Row(buts, spacing=d) lb.width = max(lb.width, brow.width) height = kwds.get('height', None) if height and lb.height > height - ( 2 * brow.height) - ScrollPanel.column_margin - box.margin: lines = mess.split('\n') # ScrolledPanel refuses to render properly for now, so Tableview is used instead. w = TableView().font.size(_(colLbl))[0] lb = TableView( columns=[ TableColumn(colLbl, max(lb.width, w)), ], nrows=(height - (2 * brow.height) - box.margin - box.font.get_linesize()) / box.font.get_linesize(), height=height - (2 * brow.height) - (box.font.get_linesize() * 2) - box.margin) #, width=w) def num_rows(): return len(lines) lb.num_rows = num_rows lb.row_data = lambda x: (lines[x], ) lb.topleft = (d, d) lb.width = max(lb.width, brow.width) col = Column([lb, brow], spacing=d, align='r') col.topleft = (d, d) if default is not None: box.enter_response = responses[default] buts[default].is_default = True else: box.enter_response = None if cancel is not None: box.cancel_response = responses[cancel] else: box.cancel_response = None box.add(col) box.shrink_wrap() def dispatchKeyForAsk(name, evt): if name == "key_down" and box.root.getKey( evt) == "Return" and default is not None: box.dismiss(responses[default]) else: Dialog.dispatch_key(box, name, evt) box.dispatch_key = dispatchKeyForAsk if tie_widget_to: ask_tied_to = box ask_tied_tos.append(box) return box.present()
def ask(mess, responses=("OK", "Cancel"), default=0, cancel=-1, wrap_width=60, **kwds): # If height is specified as a keyword, the Dialog object will have this haight, and the inner massage will # be displayed in a scrolling widget # If 'responses_tooltips' it present in kwds, it must be dict with 'responses' as keys and the tooltips as values. # If 'tie_widget_to' is in kwds keys and the value is True, the global 'ask_tied_to' will be set up to the 'box'. # When called again with 'tie_widget_to', if 'ask_tied_to' is not None, the function returns a dummy answer. global ask_tied_to responses_tooltips = kwds.pop('responses_tooltips', {}) tie_widget_to = kwds.pop('tie_widget_to', None) # Return a dummy answer if tie_windget_to is True and the global 'ask_tied_to' is not None if tie_widget_to and ask_tied_to: return "_OK" colLbl = kwds.pop('colLabel', "") box = Dialog(**kwds) d = box.margin lb = wrapped_label(mess, wrap_width) buts = [] for caption in responses: but = Button(caption, action=lambda x=caption: box.dismiss(x)) if caption in responses_tooltips.keys(): but.tooltipText = responses_tooltips[caption] buts.append(but) brow = Row(buts, spacing=d) lb.width = max(lb.width, brow.width) height = kwds.get('height', None) if height and lb.height > height - (2 * brow.height) - ScrollPanel.column_margin - box.margin: lines = mess.split('\n') # ScrolledPanel refuses to render properly for now, so Tableview is used instead. w = TableView().font.size(_(colLbl))[0] lb = TableView(columns=[TableColumn(colLbl, max(lb.width, w)),], nrows=(height - (2 * brow.height) - box.margin - box.font.get_linesize()) / box.font.get_linesize(), height=height - (2 * brow.height) - (box.font.get_linesize() * 2) - box.margin) #, width=w) def num_rows(): return len(lines) lb.num_rows = num_rows lb.row_data = lambda x: (lines[x],) lb.topleft = (d, d) lb.width = max(lb.width, brow.width) col = Column([lb, brow], spacing=d, align='r') col.topleft = (d, d) if default is not None: box.enter_response = responses[default] buts[default].is_default = True else: box.enter_response = None if cancel is not None: box.cancel_response = responses[cancel] else: box.cancel_response = None box.add(col) box.shrink_wrap() def dispatchKeyForAsk(name, evt): if name == "key_down": if box.root.getKey(evt) == "Return": if default is not None: box.dismiss(responses[default]) box.dispatch_key = dispatchKeyForAsk if tie_widget_to: ask_tied_to = box ask_tied_tos.append(box) return box.present()
def buildWidgets(self): keysColumn = self.keysColumn buttonsColumn = self.buttonsColumn items = self.items item_spacing = self.item_spacing if keysColumn is None or True: keysColumn = [] if buttonsColumn is None or True: buttonsColumn = [] labels = [] for w in self.subwidgets: for _w in w.subwidgets: w.remove(_w) self.remove(w) for t in items: if len(t) == 3: (hotkey, title, action) = t tooltipText = None else: (hotkey, title, action, tooltipText) = t if isinstance(title, (str, unicode)): button = Button(title, action=action) else: button = ValueButton(ref=title, action=action, width=200) button.anchor = self.anchor label = Label(hotkey, width=100, margin=button.margin) label.anchor = "wh" label.height = button.height labels.append(label) if tooltipText: button.tooltipText = tooltipText keysColumn.append(label) buttonsColumn.append(button) self.buttons = list(buttonsColumn) #.# if item_spacing == None: buttonsColumn = Column(buttonsColumn) else: buttonsColumn = Column(buttonsColumn, spacing=item_spacing) #.# buttonsColumn.anchor = self.anchor #.# if item_spacing == None: keysColumn = Column(keysColumn) else: keysColumn = Column(keysColumn, spacing=item_spacing) commandRow = Row((keysColumn, buttonsColumn)) self.labels = labels self.add(commandRow) self.shrink_wrap() self.invalidate()
def main(): host = "localhost" port = 9777 addr = (host, port) sock = socket(AF_INET,SOCK_DGRAM) cf = CFunctions(True, sock, addr) # Setup a rotaryEncoder with switch for volume control R1 = RotaryEnc( PinA=3, PinB=7, button=11, rotaryCallback=cf.volumeControl, buttonCallback=cf.buttonControl ) # Setup a rotaryEncoder with switch for menu control R2 = RotaryEnc( PinA=23, PinB=29, button=31, rotaryCallback=cf.menuControl, buttonCallback=cf.menuSelect ) # Setup play/pause button B1 = Button(pin=15,callback=cf.playPause) # Setup play next button B2 = Button(pin=13,callback=cf.nextTrack) # Setup play prev button B3 = Button(pin=21,callback=cf.prevTrack) # Setup one level back button B4 = Button(pin=19,callback=cf.levelBack) # First packet must be HELO and can contain an icon packet = PacketHELO("BreadBoard Control", ICON_NONE) packet.send(sock, addr) while True: #CODE TO CATCH BUTTON PRESS HERE R1.read() R1.read_button() B1.read() B2.read() B3.read() B4.read() R2.read() R2.read_button()
def __init__(self, title, choices, **kw): Button.__init__(self, title, **kw) self.choices = choices self.menu = Menu(title, ((c, c) for c in choices))
def _create_tr_button(self): self.top_right_button = Button(self, 'X', action=lambda ev: self.close())