def selection(self, start, stop, base=0): if start <= base + len(self.source) and base <= stop: start = clamp(0, len(self.source), start - base) stop = clamp(0, len(self.source), stop - base) (x0, y0), (w0, h0) = self.surface.selection(start, stop) (x1, y1), (w1, h1) = self.box.value return rectangle(vec2(x0, y1), vec2(w0, h1))
def dispatch(emit, event): global screen if event.type == pygame.QUIT: if not emit('quit', []): sys.exit(0) if event.type == pygame.VIDEORESIZE: size = [event.w, event.h] emit('resize', size) screen = Surface(pygame.display.set_mode(size, pygame.RESIZABLE)) emit('frame', [screen]) pygame.display.flip() if event.type == pygame.KEYDOWN: key = keyboard.bindings.get(event.key) modifiers = frozenset(keyboard.parse_modifiers(event.mod)) emit('keydown', [key, modifiers, event.unicode]) if event.type == pygame.KEYUP: key = keyboard.bindings.get(event.key) modifiers = frozenset(keyboard.parse_modifiers(event.mod)) emit('keyup', [key, modifiers]) if event.type == pygame.MOUSEMOTION: emit('motion', [vec2(*event.pos), vec2(*event.rel), event.buttons]) if event.type == pygame.MOUSEBUTTONDOWN: emit('buttondown', [vec2(*event.pos), event.button]) if event.type == pygame.MOUSEBUTTONUP: emit('buttonup', [vec2(*event.pos), event.button])
def selection(self, head, tail): head = clamp(0, len(self.offsets) - 1, head) tail = clamp(0, len(self.offsets) - 1, tail) start = min(head, tail) stop = max(head, tail) base = self.geometry.base size = self.geometry.size x0 = self.offsets[start] x1 = self.offsets[stop] return rectangle(base + vec2(x0-1, -1), vec2(x1 - x0 + 1, size.y+1))
def _selection(inner, outer, direction, start, stop, base=0): if start <= base + 1 and base <= stop: start = clamp(0, 1, start - base) stop = clamp(0, 1, stop - base) if direction == 'x': x0 = (inner.left.value, inner.right.value)[start] x1 = (inner.left.value, inner.right.value)[stop] y0 = outer.top.value y1 = outer.bottom.value else: x0 = outer.left.value x1 = outer.right.value y0 = (inner.top.value, inner.bottom.value)[start] y1 = (inner.top.value, inner.bottom.value)[stop] return rectangle(vec2(x0,y0), vec2(x1-x0, y1-y0)).offset((1,1,1,1))
def _carets(inner, outer, direction): if direction == 'x': x0 = inner.left.value x1 = inner.right.value y0 = outer.top.value y1 = outer.bottom.value yield rectangle(vec2(x0,y0), vec2(0,y1-y0)).offset((1,1,1,1)) yield rectangle(vec2(x1,y0), vec2(0,y1-y0)).offset((1,1,1,1)) else: x0 = outer.left.value x1 = outer.right.value y0 = inner.top.value y1 = inner.bottom.value yield rectangle(vec2(x0,y0), vec2(x1-x0,0)).offset((1,1,1,1)) yield rectangle(vec2(x0,y1), vec2(x1-x0,0)).offset((1,1,1,1))
def __call__(self, sampler, geometry=None): if geometry == None and hasattr(sampler, "geometry"): geometry = sampler.geometry elif geometry == None: geometry = rectangle(vec2(0, 0), self.size) if hasattr(geometry, "valid") and not geometry.valid(): return if isinstance(sampler, rgba): (x, y), (w, h) = geometry self.pys.fill(tuple(sampler), (x, y, w, h)) elif hasattr(sampler, "blit_duck"): sampler.blit_duck(self.pys, geometry, 0) elif hasattr(sampler, "comm_duck"): sampler.comm_duck(self, geometry) else: raise Exception("unknown draw command %r, %r", (sampler, geometry)) return self
def value(self): left, top, right, bottom = self p0 = vec2(left.value, top.value) p1 = vec2(right.value, bottom.value) return rectangle(p0, p1-p0)
def size(self): return vec2(*self.pys.get_size())
def move(self, scroll): (w, h) = self.box.value.size self.scroll = vec2(clamp(self.viewport.x - w, 0, scroll.x), clamp(self.viewport.y - h, 0, scroll.y))
def __init__(self, config, element): EnclosingFrame.__init__(self, config, element) self.scroll = vec2(0, 0) self.viewport = vec2(100, 100) self.layout = None
def caret(self, index): index = clamp(0, len(self.offsets) - 1, index) base = self.geometry.base size = self.geometry.size y = max(size.y, 8) return rectangle(base + vec2(self.offsets[index]-1, -1), vec2(1, y+1))
def carets(self): base = self.geometry.base size = self.geometry.size for offset in self.offsets: yield rectangle(base + vec2(offset-1, -1), vec2(1, size.y+1))
def __init__(self, pys, offsets, baseline, mathline): Surface.__init__(self, pys) self.offsets = offsets self.baseline = baseline self.mathline = mathline self.geometry = rectangle(vec2(0, 0), self.size)
def label_carets(self): (x1, y1), (w1, h1) = self.outer.value for (x0, y0), (w0, h0) in self.surface.carets(): yield rectangle(vec2(x0, y1), vec2(w0, h1))
def draw(self, screen): self.surface.geometry.base = vec2( self.inner.left.value, self.inner.bottom.value - self.surface.baseline, ) screen(self.surface)