Source code for joy.vui.viewer

# -*- coding: utf-8 -*-
#
#    Copyright © 2019 Simon Forman
#
#    This file is part of joy.py
#
#    joy.py is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    joy.py is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with joy.py.  If not see <http://www.gnu.org/licenses/>.
#
'''

Viewer
=================

'''
import pygame
from joy.vui.core import BACKGROUND, FOREGROUND


[docs]class Viewer(object): ''' Base Viewer class ''' MINIMUM_HEIGHT = 11 def __init__(self, surface): self.resurface(surface) self.last_touch = 0, 0 def resurface(self, surface): self.w, self.h = surface.get_width(), surface.get_height() self.surface = surface
[docs] def split(self, y): ''' Split the viewer at the y coordinate (which is relative to the viewer's surface and must be inside it somewhere) and return the remaining height. The upper part of the viewer remains (and gets redrawn on a new surface) and the lower space is now available for e.g. a new viewer. ''' assert y >= self.MINIMUM_HEIGHT new_viewer_h = self.h - y self.resurface(self.surface.subsurface((0, 0, self.w, y))) if y <= self.last_touch[1]: self.last_touch = 0, 0 self.draw() return new_viewer_h
def handle(self, message): assert self is not message.sender pass
[docs] def draw(self): '''Draw the viewer onto its surface.''' self.surface.fill(BACKGROUND) x, y, h = self.w - 1, self.MINIMUM_HEIGHT, self.h - 1 # Right-hand side. pygame.draw.line(self.surface, FOREGROUND, (x, 0), (x, h)) # Between header and body. pygame.draw.line(self.surface, FOREGROUND, (0, y), (x, y)) # Bottom. pygame.draw.line(self.surface, FOREGROUND, (0, h), (x, h))
[docs] def close(self): '''Close the viewer and release any resources, etc...'''
def focus(self, display): pass def unfocus(self): pass # Event handling. def mouse_down(self, display, x, y, button): self.last_touch = x, y def mouse_up(self, display, x, y, button): pass def mouse_motion(self, display, x, y, dx, dy, button0, button1, button2): pass def key_up(self, display, key, mod): if key == pygame.K_q and mod & pygame.KMOD_CTRL: # Ctrl-q display.close_viewer(self) return True if key == pygame.K_g and mod & pygame.KMOD_CTRL: # Ctrl-g display.grow_viewer(self) return True def key_down(self, display, uch, key, mod): pass
[docs]class SomeViewer(MenuViewer): def __init__(self, surface): MenuViewer.__init__(self, surface) def resurface(self, surface): MenuViewer.resurface(self, surface) def draw_menu(self): MenuViewer.draw_menu(self) def draw_body(self): pass def body_click(self, display, x, y, button): pass def menu_click(self, display, x, y, button): if MenuViewer.menu_click(self, display, x, y, button): return True def mouse_up(self, display, x, y, button): if MenuViewer.mouse_up(self, display, x, y, button): return True def mouse_motion(self, display, x, y, rel_x, rel_y, button0, button1, button2): if MenuViewer.mouse_motion(self, display, x, y, rel_x, rel_y, button0, button1, button2): return True def key_down(self, display, uch, key, mod): try: print chr(key), except ValueError: pass
# Note that Oberon book says that if you split at the exact top of a viewer # it should close, and I think this implies the new viewer gets the old # viewer's whole height. I haven't implemented that yet, so the edge-case # in the code is broken by "intent" for now.. def draw_a(surface, color=FOREGROUND, blend=False): w, h = surface.get_width() - 2, surface.get_height() - 2 pygame.draw.aalines(surface, color, False, ( (1, h), (w / 2, 1), (w, h), (1, h / 2) ), blend)