예제 #1
0
    def __init__(self, app):
        super(NewsPanel, self).__init__(app)

        self._news_main_font = pygame.font.SysFont(self.default_font_name, 35)
        self._news_title_font = pygame.font.SysFont(self.default_font_name, 20)
        self.news_widget = Content(self,
                                   10,
                                   10,
                                   "News",
                                   font=self._news_main_font)
        self.title_widget = Content(self,
                                    90,
                                    10,
                                    "",
                                    font=self._news_title_font,
                                    max_width=self.screen_width - 100,
                                    max_height=30,
                                    borders=['left'],
                                    margin=(10, 0, 0, 0))
        self.newslist_widget = NewsList(self,
                                        10,
                                        45,
                                        max_width=self.screen_width - 20,
                                        max_height=self.screen_height - 60,
                                        title_widget=self.title_widget)
        self.widgets = [
            self.news_widget, self.title_widget, self.newslist_widget
        ]

        self.set_active_widget(self.newslist_widget)
예제 #2
0
    def __init__(self,
                 parent,
                 width,
                 height,
                 text,
                 actions={},
                 default_action=None):
        super(ConfirmPopup, self).__init__(parent, width, height)

        self.text = text
        self.actions = actions
        self.default_action = default_action

        self._text_padding = 10
        self._text_font = pygame.font.Font('fonts/FreeSans.ttf', 18)
        self._text_widget_max_width = self.width - 2 * self._text_padding
        self._text_widget_max_height = self.height - 2 * self._text_padding - self._header_height
        self._text_widget = Content(self.parent,
                                    0,
                                    0,
                                    self.text,
                                    max_width=self._text_widget_max_width,
                                    max_height=self._text_widget_max_height,
                                    font=self._text_font)
        self._text_widget.setup()
        text_x = self.x + (self.width - self._text_widget.get_width()) // 2
        text_y = self.y + (self.height - self._text_widget.get_height()) // 2
        self._text_widget.set_pos(text_x, text_y)
        self._subwidgets.append(self._text_widget)

        for key in actions:
            self.add_action_button(key,
                                   action=self._select_action,
                                   action_param=key)
        self.add_action_button('Cancel', action=self.close)
예제 #3
0
    def __init__(self, app):
        super(GamePanel, self).__init__(app)

        self.title_font = pygame.font.SysFont(self.default_font_name, 35)
        self.title_widget = Content(self, 10, 10, "Game", font=self.title_font)

        self.game_names = []
        self.menu_widget = List(self,
                                10,
                                45,
                                items=self.game_names,
                                max_width=400,
                                max_height=260,
                                selectable=True,
                                select_event=self.select_game,
                                reset_on_exit=False)
        self.widgets = [self.title_widget, self.menu_widget]

        self.games = {}
        self._add_game(
            "Snake",
            GameSnake(self, self.exit_game, total_rows=16, total_cols=16))
        self._add_game(
            "Tetris",
            GameTetris(self, self.exit_game, total_rows=16, total_cols=10))
        self._add_game(
            "Flip",
            GameFlip(self, self.exit_game, board_size=10, border_width=1))
예제 #4
0
    def __init__(self,
                 parent,
                 width,
                 height,
                 text='',
                 entries=[],
                 styles=[],
                 values=[],
                 required=None,
                 close_action=None,
                 input_width=100):
        super(InputPopup, self).__init__(parent, width, height)

        self.text = text
        self.entries = entries
        self.styles = styles
        self.required = required
        self.close_action = close_action
        self.values = values
        self.input_width = input_width

        self._text_padding = 10
        self._text_font = pygame.font.Font('fonts/FreeSans.ttf', 15)
        self._text_widget_max_width = self.width - 2 * self._text_padding
        self._text_widget_max_height = self.height - 2 * self._text_padding - self._header_height
        self._text_widget = Content(self.parent,
                                    0,
                                    0,
                                    self.text,
                                    max_width=self._text_widget_max_width,
                                    max_height=self._text_widget_max_height,
                                    font=self._text_font)
        self._text_widget.setup()
        self._text_x = self.x + 30
        self._text_y = self.y + self._header_height + 20
        self._text_widget.set_pos(self._text_x, self._text_y)
        self._subwidgets.append(self._text_widget)

        self._entry_font = pygame.font.Font('fonts/FreeSans.ttf', 15)
        self._input_widgets = []
        self._active_widget_ind = -1
        self._setup_input_widgets()

        self.add_action_button('OK', action=self._validate_close)
        self.add_action_button('Cancel', action=self.close)
예제 #5
0
class InfoPopup(Popup):
    def __init__(self, parent, width, height, text):
        super(InfoPopup, self).__init__(parent, width, height)

        self.text = text

        self._text_padding = 10
        self._text_font = pygame.font.Font('fonts/FreeSans.ttf', 18)
        self._text_widget_max_width = self.width - 2 * self._text_padding
        self._text_widget_max_height = self.height - 2 * self._text_padding - self._header_height
        self._text_widget = Content(self.parent,
                                    0,
                                    0,
                                    self.text,
                                    max_width=self._text_widget_max_width,
                                    max_height=self._text_widget_max_height,
                                    font=self._text_font)
        self._text_widget.setup()
        text_x = self.x + (self.width - self._text_widget.get_width()) // 2
        text_y = self.y + (self.height - self._text_widget.get_height()) // 2
        self._text_widget.set_pos(text_x, text_y)

        self._subwidgets.append(self._text_widget)

        self.add_action_button('OK', action=self.close)

    def _on_setup(self):
        self.set_title('Info')

    def _handle_popup_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                self.close()
예제 #6
0
    def _setup_input_widgets(self):
        if not self.entries:
            return

        x = self._text_x
        y = self._text_y + self._text_widget.get_height() + self._text_padding
        max_title = self._entry_font.render(max(self.entries, key=len), True,
                                            self._get_color('white'))
        max_title_width = max_title.get_width()

        for ind, entry in enumerate(self.entries):
            title_widget = Content(self.parent,
                                   x,
                                   y,
                                   entry + ': ',
                                   font=self._entry_font)
            title_widget.setup()
            self._subwidgets.append(title_widget)

            input_type = self.styles[ind] if self.styles else 'input'

            if input_type == 'input':
                input_widget = Input(self.parent,
                                     x + max_title_width + 15,
                                     y,
                                     font=self._entry_font,
                                     width=self.input_width,
                                     enter_key_event=self._validate_close)
                input_widget.setup()
                input_widget.bind_key(pygame.K_TAB, self._next_input_widget)
                input_widget.bind_key(pygame.K_TAB,
                                      self._prev_input_widget,
                                      shift=True)
                if self.values:
                    input_widget.set_text(self.values[ind])
                self._subwidgets.append(input_widget)
                self._input_widgets.append((entry, title_widget, input_widget))
            elif input_type == 'selector':
                selector_size = title_widget.get_height()
                input_widget = SelectorButton(
                    self.parent,
                    x + max_title_width + 15,
                    y,
                    width=selector_size,
                    height=selector_size,
                    border_color=self._get_color('white'),
                    focus_color=self._get_color('lightgray'),
                    background_color=self._get_color('black'),
                    border_width=1,
                    focus_width=2)
                self.buttons.append(input_widget)
                if self.values:
                    input_widget.set_selected(self.values[ind])
                self._input_widgets.append((entry, title_widget, input_widget))

            y += title_widget.get_height() + self._text_padding

        self._toggle_input_widget()
예제 #7
0
    def __init__(self, app):
        super(MapPanel, self).__init__(app)

        self._title_font = pygame.font.SysFont(self.default_font_name, 35)

        self.title_widget = Content(self, 10, 10, "Map", font=self._title_font)
        self.map_widget = Map(self,
                              10,
                              45,
                              map_width=400,
                              map_height=170,
                              map_padding=0.075,
                              background_alpha=100)
        self.widgets = [self.title_widget, self.map_widget]
예제 #8
0
    def __init__(self, parent, width, height, text):
        super(InfoPopup, self).__init__(parent, width, height)

        self.text = text

        self._text_padding = 10
        self._text_font = pygame.font.Font('fonts/FreeSans.ttf', 18)
        self._text_widget_max_width = self.width - 2 * self._text_padding
        self._text_widget_max_height = self.height - 2 * self._text_padding - self._header_height
        self._text_widget = Content(self.parent,
                                    0,
                                    0,
                                    self.text,
                                    max_width=self._text_widget_max_width,
                                    max_height=self._text_widget_max_height,
                                    font=self._text_font)
        self._text_widget.setup()
        text_x = self.x + (self.width - self._text_widget.get_width()) // 2
        text_y = self.y + (self.height - self._text_widget.get_height()) // 2
        self._text_widget.set_pos(text_x, text_y)

        self._subwidgets.append(self._text_widget)

        self.add_action_button('OK', action=self.close)
예제 #9
0
    def __init__(self, app):
        super(StockPanel, self).__init__(app)

        self._title_font = pygame.font.SysFont(self.default_font_name, 35)

        self.title_widget = Content(self,
                                    10,
                                    10,
                                    "Stock",
                                    font=self._title_font)
        self.stock_widget = Stock(self,
                                  25,
                                  40,
                                  chart=True,
                                  chart_width=self.screen_width - 80,
                                  chart_height=self.screen_height - 120)
        self.widgets = [self.title_widget, self.stock_widget]
예제 #10
0
class ConfirmPopup(Popup):
    def __init__(self,
                 parent,
                 width,
                 height,
                 text,
                 actions={},
                 default_action=None):
        super(ConfirmPopup, self).__init__(parent, width, height)

        self.text = text
        self.actions = actions
        self.default_action = default_action

        self._text_padding = 10
        self._text_font = pygame.font.Font('fonts/FreeSans.ttf', 18)
        self._text_widget_max_width = self.width - 2 * self._text_padding
        self._text_widget_max_height = self.height - 2 * self._text_padding - self._header_height
        self._text_widget = Content(self.parent,
                                    0,
                                    0,
                                    self.text,
                                    max_width=self._text_widget_max_width,
                                    max_height=self._text_widget_max_height,
                                    font=self._text_font)
        self._text_widget.setup()
        text_x = self.x + (self.width - self._text_widget.get_width()) // 2
        text_y = self.y + (self.height - self._text_widget.get_height()) // 2
        self._text_widget.set_pos(text_x, text_y)
        self._subwidgets.append(self._text_widget)

        for key in actions:
            self.add_action_button(key,
                                   action=self._select_action,
                                   action_param=key)
        self.add_action_button('Cancel', action=self.close)

    def _on_setup(self):
        self.set_title('Confirm')

    def _handle_popup_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                if self.default_action is not None:
                    self._select_action(self.default_action)
                self.close()

    def _select_action(self, key):
        if self.actions.get(key) is not None:
            self.actions[key]()
        self.close()
예제 #11
0
class InputPopup(Popup):
    def __init__(self,
                 parent,
                 width,
                 height,
                 text='',
                 entries=[],
                 styles=[],
                 values=[],
                 required=None,
                 close_action=None,
                 input_width=100):
        super(InputPopup, self).__init__(parent, width, height)

        self.text = text
        self.entries = entries
        self.styles = styles
        self.required = required
        self.close_action = close_action
        self.values = values
        self.input_width = input_width

        self._text_padding = 10
        self._text_font = pygame.font.Font('fonts/FreeSans.ttf', 15)
        self._text_widget_max_width = self.width - 2 * self._text_padding
        self._text_widget_max_height = self.height - 2 * self._text_padding - self._header_height
        self._text_widget = Content(self.parent,
                                    0,
                                    0,
                                    self.text,
                                    max_width=self._text_widget_max_width,
                                    max_height=self._text_widget_max_height,
                                    font=self._text_font)
        self._text_widget.setup()
        self._text_x = self.x + 30
        self._text_y = self.y + self._header_height + 20
        self._text_widget.set_pos(self._text_x, self._text_y)
        self._subwidgets.append(self._text_widget)

        self._entry_font = pygame.font.Font('fonts/FreeSans.ttf', 15)
        self._input_widgets = []
        self._active_widget_ind = -1
        self._setup_input_widgets()

        self.add_action_button('OK', action=self._validate_close)
        self.add_action_button('Cancel', action=self.close)

    def _setup_input_widgets(self):
        if not self.entries:
            return

        x = self._text_x
        y = self._text_y + self._text_widget.get_height() + self._text_padding
        max_title = self._entry_font.render(max(self.entries, key=len), True,
                                            self._get_color('white'))
        max_title_width = max_title.get_width()

        for ind, entry in enumerate(self.entries):
            title_widget = Content(self.parent,
                                   x,
                                   y,
                                   entry + ': ',
                                   font=self._entry_font)
            title_widget.setup()
            self._subwidgets.append(title_widget)

            input_type = self.styles[ind] if self.styles else 'input'

            if input_type == 'input':
                input_widget = Input(self.parent,
                                     x + max_title_width + 15,
                                     y,
                                     font=self._entry_font,
                                     width=self.input_width,
                                     enter_key_event=self._validate_close)
                input_widget.setup()
                input_widget.bind_key(pygame.K_TAB, self._next_input_widget)
                input_widget.bind_key(pygame.K_TAB,
                                      self._prev_input_widget,
                                      shift=True)
                if self.values:
                    input_widget.set_text(self.values[ind])
                self._subwidgets.append(input_widget)
                self._input_widgets.append((entry, title_widget, input_widget))
            elif input_type == 'selector':
                selector_size = title_widget.get_height()
                input_widget = SelectorButton(
                    self.parent,
                    x + max_title_width + 15,
                    y,
                    width=selector_size,
                    height=selector_size,
                    border_color=self._get_color('white'),
                    focus_color=self._get_color('lightgray'),
                    background_color=self._get_color('black'),
                    border_width=1,
                    focus_width=2)
                self.buttons.append(input_widget)
                if self.values:
                    input_widget.set_selected(self.values[ind])
                self._input_widgets.append((entry, title_widget, input_widget))

            y += title_widget.get_height() + self._text_padding

        self._toggle_input_widget()

    def _on_setup(self):
        self.set_title('Input')

    def _handle_popup_event(self, event):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_TAB:
                if shift_pressed():
                    self._prev_input_widget()
                else:
                    self._next_input_widget()
            elif event.key == pygame.K_RETURN:
                self._validate_close()
            elif event.key == pygame.K_SPACE:
                self._toggle_selector_status()

    def _toggle_selector_status(self):
        if not self._input_widgets:
            return

        curr_widget = self._input_widgets[self._active_widget_ind][2]
        if isinstance(curr_widget, SelectorButton):
            curr_widget.click()

    def _set_widget_status(self, widget, status):
        if isinstance(widget, Input):
            widget.set_active(status)
        elif isinstance(widget, SelectorButton):
            widget.set_always_focus(status)

    def _toggle_input_widget(self, reverse=False):
        if not self._input_widgets:
            return

        if self._active_widget_ind == -1:
            self._active_widget_ind = 0
        else:
            curr_widget = self._input_widgets[self._active_widget_ind][2]
            self._set_widget_status(curr_widget, False)

            if reverse:
                self._active_widget_ind -= 1
            else:
                self._active_widget_ind += 1

            total_widgets = len(self._input_widgets)
            if self._active_widget_ind == total_widgets:
                self._active_widget_ind = 0
            elif self._active_widget_ind < 0:
                self._active_widget_ind = total_widgets - 1
        curr_widget = self._input_widgets[self._active_widget_ind][2]
        self._set_widget_status(curr_widget, True)

    def _next_input_widget(self):
        self._toggle_input_widget(reverse=False)

    def _prev_input_widget(self):
        self._toggle_input_widget(reverse=True)

    def _validate_close(self):
        if not self.required:
            return True

        result = True
        for ind, t in enumerate(self._input_widgets):
            if not isinstance(t[2], Input):
                continue

            t[1].set_text_color(self._get_color('white'))

            req = self.required[ind]
            entry_valid = True
            entry_text = t[2].get_text()
            if isinstance(req, bool) and req and not entry_text:
                entry_valid = False
            elif isinstance(self.required[ind], str) \
                    and not re.search(self.required[ind], entry_text):
                entry_valid = False

            if not entry_valid:
                t[1].set_text_color(self._get_color('red'))
                result = False

        if result:
            if self.close_action:
                self.close_action()
            self.close()

        return result

    def get_input(self):
        result = {}
        for t in self._input_widgets:
            result[t[0]] = t[2].get_text()

        return result
예제 #12
0
    def __init__(self, app):
        super(SystemInfoPanel, self).__init__(app)

        self._title_font = pygame.font.SysFont(self.default_font_name, 35)
        self._caption_font = pygame.font.Font("fonts/FreeSans.ttf", 13)
        self._info_font = pygame.font.Font("fonts/FreeSans.ttf", 13)

        self._max_size = 120
        self._update_interval = 0.5
        self._cpu_info = queue.Queue(maxsize=self._max_size)
        self._last_cpu_info = 0
        self._memory_info = queue.Queue(maxsize=self._max_size)
        self._cputemp_info = queue.Queue(maxsize=self._max_size)
        self._system_info = {
            "CPU": self._cpu_info,
            "Memory": self._memory_info,
            "CPU Temp": self._cputemp_info
        }
        self._info_colors = {
            "CPU": "green",
            "Memory": "yellow",
            "CPU Temp": "orange"
        }

        self.title_widget = Content(self,
                                    10,
                                    10,
                                    "System Info",
                                    font=self._title_font)
        self.chart_widget = Chart(self,
                                  30,
                                  70,
                                  info=self._system_info,
                                  width=self.screen_width - 50,
                                  height=self.screen_height - 100,
                                  max_x=int(self._max_size *
                                            self._update_interval),
                                  info_colors=self._info_colors,
                                  line_width=2,
                                  x_unit=self._update_interval,
                                  y_unit=1,
                                  x_label_interval=10,
                                  y_label_interval=20,
                                  background=True,
                                  background_color=(75, 75, 75),
                                  background_alpha=180)
        self._info_widget = SystemInfo(self,
                                       160,
                                       10,
                                       font=self._info_font,
                                       cpu_info=False,
                                       memory_info=False,
                                       disk_info=False,
                                       percent_bar=False,
                                       ip_info=True)
        self.caption_widget = ChartCaption(self,
                                           357,
                                           10,
                                           self._info_colors,
                                           font=self._caption_font)
        self.statusbar_widget = StatusBar(self, 15, 40)
        self.widgets = [
            self.title_widget, self.chart_widget, self._info_widget,
            self.caption_widget, self.statusbar_widget
        ]

        self._last_update = time.time()