snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit bfb9795496c3a3cbaa6e77e9ac0ae9e291b76a2d
parent f50c6854e3ba7a2852252ed67f87f76c4dfdf218
Author: Josh Moore <jxm5210@rit.edu>
Date:   Thu, 28 May 2020 14:29:06 -0400

Change appmenu backend to dict structure

Diffstat:
Msnakewm/appmenu/appmenupanel.py | 67++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Msnakewm/wm.py | 21++++++++++++++-------
2 files changed, 70 insertions(+), 18 deletions(-)

diff --git a/snakewm/appmenu/appmenupanel.py b/snakewm/appmenu/appmenupanel.py @@ -4,6 +4,7 @@ current directory. """ import pygame +import pygame_gui from pygame_gui.elements import UIPanel from pygame_gui.elements import UIButton @@ -19,21 +20,20 @@ class AppMenuPanel(UIPanel): pos = None path = None - # the elements field contains a list of tuples, where each tuple - # represents a single element the current directory: - # (text, next) - # where text is the text to display, and next is the directory this - # element represents, or None if the element represents an app. + # the elements field is a dict object containing the structure of + # the current directory and all subdirectories. If a value is None, + # that means its corresponding key is represents an app. elements = None + # child panel created when a directory button is hovered + child = None + def __init__(self, manager, pos, path, elements): """ manager - UIManager to manage this panel pos - position indices to start drawing this panel at path - the directory this panel represents elements - list of elements in this directory - \TODO replace elements with a dict structure representing the - current directory """ super().__init__( pygame.Rect( @@ -43,23 +43,68 @@ class AppMenuPanel(UIPanel): ), ( BUTTON_DIMS[0] + 5, - BUTTON_DIMS[1] * len(elements) + 5 + BUTTON_DIMS[1] * len(elements.keys()) + 5 ) ), starting_layer_height=PANEL_LAYER, manager=manager ) + self.pos = pos self.path = path self.elements = elements # generate buttons - for i in range(len(elements)): + ekeys = list(elements.keys()) + for i in range(len(ekeys)): UIButton( pygame.Rect( (0, i * BUTTON_DIMS[1]), BUTTON_DIMS ), - text=elements[i][0], + text=ekeys[i], manager=manager, - container=self + container=self, + object_id = 'menu-' + self.path.replace('.', '-') ) + + def process_event(self, event): + if event.type != pygame.USEREVENT: + return + + if event.user_type == pygame_gui.UI_BUTTON_PRESSED and\ + event.ui_object_id == ('panel.menu-' + self.path.replace('.', '-')): + # open clicked app + uitext = event.ui_element.text + + if self.elements[uitext] == None: + #\TODO open app + print("opening app: " + self.path + '.' + uitext) + + if event.user_type == pygame_gui.UI_BUTTON_ON_HOVERED and\ + event.ui_object_id == ('panel.menu-' + self.path.replace('.', '-')): + uitext = event.ui_element.text + + if self.elements[uitext] != None: + # first destroy the active child panel + if self.child is not None: + self.child.destroy() + + # next open a new child panel + self.child = AppMenuPanel( + self.ui_manager, + ( + self.pos[0] + 1, + list(self.elements.keys()).index(uitext) + ), + self.path + '.' + uitext, + self.elements[uitext] + ) + + def destroy(self): + """ + Recursively kill this panel and all child panels. + """ + if self.child is not None: + self.child.destroy() + self.child = None + self.kill() diff --git a/snakewm/wm.py b/snakewm/wm.py @@ -49,13 +49,20 @@ class SnakeWM: self.MANAGER, (0, 0), 'apps', - [('test', None), ('games', None), ('aaa', None)] - ) - AppMenuPanel( - self.MANAGER, - (1, 2), - 'apps.games', - [('HelloWorld', None), ('asdf', None)] + { + 'test': { + 'HelloWorld': None, + 'asdf': { + 'cool': None, + 'wow': None + }, + 'a': None, + 'b': None + }, + 'games': { + 'pong': None + } + } ) ##