class Tool(object): """Base class for a tool in the toolbar.""" def __init__(self): self.size = Vector(32, 32) self.outline = pyglet.graphics.vertex_list(4, ('v2f/static', self.size.to('r2f'))) self.highlight = pyglet.graphics.vertex_list_indexed(4, [0, 1, 2, 0, 2, 3], ('v2f/static', self.size.to('r2f')), ('c3f/static', (0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5))) self.active = False def draw(self): if self.active: self.highlight.draw(GL_TRIANGLE_STRIP) self.outline.draw(GL_LINE_LOOP) def drawCursor(self): self
class Toolbar(object): def __init__(self, engine): self.engine = engine engine.addDrawer(self, 'hud') engine.subscribeMouse(self, ['release']) self.size = Vector(engine.window.width, 32) self.pos = Vector(0, 0) #self.tools = [ToolObjectCursor(engine), ToolHand(engine), # ToolAddPoint(engine), ToolRemovePoint(engine)] self.tools = [ToolObjectCursor(), ToolVertexCursor()] self.outline = pyglet.graphics.vertex_list(4, ('v2f/static', self.size.to('r2f'))) def on_mouse_release(self, x, y, button, modifiers): if 0 <= y <= 32: idx = x // 32 if 0 <= idx < len(self.tools): self.activate(idx) def draw(self): self.outline.draw(GL_LINE_LOOP) glPushMatrix() for tool in self.tools: tool.draw() glTranslatef(32, 0, 0) glPopMatrix() def activate(self, idx): """Activate the tool at `idx`, and deactivate others.""" for i, tool in enumerate(self.tools): tool.active = (i == idx) self.active_tool = self.tools[idx]