(未完成)Pyglet实现SideBar
Posted howld
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(未完成)Pyglet实现SideBar相关的知识,希望对你有一定的参考价值。
import pyglet class Window(pyglet.window.Window): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def attach(self, widget): def __on_draw(): self.clear() widget.on_draw(self.width, self.height) def __on_mouse_press(x, y, button, modifiers): widget.on_mouse_press(x, self.height-y, button, modifiers) def __on_mouse_motion(x, y, dx, dy): widget.on_mouse_motion(x, self.height-y, dx, -dy) def __on_key_press(symbol, modifiers): widget.on_key_press(symbol, modifiers) self.push_handlers(on_draw = __on_draw) self.push_handlers(on_mouse_press = __on_mouse_press) self.push_handlers(on_mouse_motion = __on_mouse_motion) self.push_handlers(on_key_press = __on_key_press) class Color(): White = (255, 255, 255) Black = (0, 0, 0) Green = (0, 255, 0) Blue = (0, 0, 255) class BaseWidget(): def __init__(self): self.x = 0 self.y = 0 self.width = 0 self.height = 0 self.text = "" self.text_x = 0 self.visible = True self.active = True self.color = Color.White def __del__(self): pass def __draw(self, mode, window_width, window_height): ny = window_height - self.y points = (self.x, ny, self.x+self.width, ny, self.x+self.width, ny-self.height, self.x, ny-self.height) colors = [] for i in range(4): colors.extend(self.color) pyglet.graphics.draw(4, mode, ("v2i", points), ("c3B", colors)) # def draw_line_loop(self, ): # self.draw(pyglet.gl.GL_LINE_LOOP) def draw_rectangle(self, window_width, window_height): self.__draw(pyglet.gl.GL_QUADS, window_width, window_height) def is_within_area(self, x, y): if x >= self.x and x <= self.x+self.width: if y >= self.y and y <= self.y+self.height: return True return False def on_draw(self, window_width, window_height): pass def on_mouse_press(self, x, y, button, modifiers): pass def on_mouse_motion(self, x, y, dx, dy): pass def on_key_press(self, symbol, modifiers): pass class PaneWidget(BaseWidget): def __init__(self): super().__init__() self.widgets = [] # self.selected_widget = None def add(self, widget): self.widgets.append(widget) def on_draw(self, window_width, window_height): for widget in self.widgets: widget.on_draw(window_width, window_height) def on_mouse_press(self, x, y, button, modifiers): for widget in self.widgets: widget.on_mouse_press(x, y, button, modifiers) def on_mouse_motion(self, x, y, dx, dy): for widget in self.widgets: widget.on_mouse_motion(x, y, dx, dy) def on_key_press(self, symbol, modifiers): for widget in self.widgets: widget.on_key_press(symbol, modifiers) class Node(): def __init__(self): self.widget = None self.nodes = [] class TreeWidget(BaseWidget): def __init__(self): super().__init__() self.root = Node() self.selected_node = self.root def add(self, widget): node = Node() node.widget = widget self.selected_node.nodes.append(node) height = self.update() * widget.height if height > self.height: self.height = height def update(self): return self.__update(self.root, 0, 0) def on_draw(self, window_width, window_height): if self.selected_node != self.root: self.selected_node.widget.color = Color.Blue self.__on_draw(self.root, window_width, window_height) def on_mouse_press(self, x, y, button, modifiers): self.__on_mouse_press(self.root, x, y, button, modifiers) def on_mouse_motion(self, x, y, dx, dy): self.__on_mouse_motion(self.root, x, y, dx, dy) def __update(self, next_node, depth, height): for node in next_node.nodes: # node.widget.x = self.x + depth*20 node.widget.text_x = self.x + depth*20 node.widget.y = self.y + height*node.widget.height # node.widget.width = self.width # node.widget.height = 20 height = self.__update(node, depth + 1, height + 1) return height def __on_draw(self, next_node, window_width, window_height): for node in next_node.nodes: node.widget.on_draw(window_width, window_height) self.__on_draw(node, window_width, window_height) def __on_mouse_press(self, next_node, x, y, button, modifiers): for node in next_node.nodes: if node.widget.on_mouse_press(x, y, button, modifiers): if self.selected_node != self.root: self.selected_node.widget.color = Color.White self.selected_node = node self.__on_mouse_press(node, x, y, button, modifiers) def __on_mouse_motion(self, next_node, x, y, dx, dy): for node in next_node.nodes: node.widget.on_mouse_motion(x, y, dx, dy) self.__on_mouse_motion(node, x, y, dx, dy) class Item(BaseWidget): def __init__(self): super().__init__() def on_draw(self, window_width, window_height): self.draw_rectangle(window_width, window_height) def on_mouse_press(self, x, y, button, modifiers): if self.is_within_area(x, y): if button == pyglet.window.mouse.LEFT or button == pyglet.window.mouse.RIGHT: return True return False def on_mouse_motion(self, x, y, dx, dy): if self.is_within_area(x, y): self.color = Color.Green else: self.color = Color.White class Sidebar(TreeWidget): def __init__(self): super().__init__() def add(self, widget): widget.x = self.x widget.y = self.y widget.width = self.width widget.height = 20 super().add(widget) def on_draw(self, window_width, window_height): self.draw_rectangle(window_width, window_height) super().on_draw(window_width, window_height)
class Form(PaneWidget): def __init__(self): super().__init__() self.sidebar = None # self.menu = None self.init_component() self.test() def init_component(self): self.sidebar = Sidebar() self.sidebar.x = 10 self.sidebar.y = 10 self.sidebar.width = 100 self.sidebar.height = 200 self.add(self.sidebar) # self.menu = Menu() # self.menu.width = 80 # self.menu.active = False # self.add(self.menu) def on_mouse_press(self, x, y, button, modifiers): super().on_mouse_press(x, y, button, modifiers) # if button == pyglet.window.mouse.RIGHT: # widget = self.sidebar.selected_node.widget # if widget != None and widget.is_within_area(x, y): # if self.menu.active: # if self.menu.is_within_area(x, y) == False: # self.menu.x = x # self.menu.y = y # else: # self.menu.x = x # self.menu.y = y # self.menu.active = True def on_key_press(self, symbol, modifiers): super().on_key_press(symbol, modifiers) if symbol == pyglet.window.key.N: self.sidebar.add(Item()) def test(self): self.sidebar.add(Item()) self.sidebar.add(Item()) self.sidebar.add(Item())
from widget import * if __name__ == "__main__": window = Window() form = Form() window.attach(form) pyglet.app.run()
以上是关于(未完成)Pyglet实现SideBar的主要内容,如果未能解决你的问题,请参考以下文章
实现 pyglet 会破坏我曾经工作的帧缓冲区 OpenGL 代码