def __init__(self, target): super().__init__(target) self.next_scene = None size = self.target.size view = sf.View(sf.Rectangle((0, 0), (size.x, size.y))) self.target.view = view font = sf.Font.from_file(assets.get_asset("/fonts/Amethyst.ttf")) # Title self.title = sf.Text() self.title.string = "Reversi" self.title.font = font self.title.color = sf.Color.WHITE title_bounds = self.title.global_bounds # Menu items self.menuitems = [] for item in [["Spela en spelare", self._switch_colormenu], ["Spela två spelare", self._switch_sizemenu], ["Avsluta", target.close]]: button = Button(target, item[0]) button.set_listener(item[1]) self.menuitems.append(button) self.add_component(button) self._setup_buttons()
def set_position(self, pos): rect = sf.Rectangle( sf.Vector2(pos.x * self.atlas.size.x, pos.y * self.atlas.size.y), self.atlas.size) self.vertex(0).position = sf.Vector2(rect.left, rect.top) self.vertex(1).position = sf.Vector2(rect.right, rect.top) self.vertex(2).position = sf.Vector2(rect.right, rect.bottom) self.vertex(3).position = sf.Vector2(rect.left, rect.bottom)
def event(self, events): super().event(events) for e in events: if type(e) == sf.ResizeEvent: size = self.target.size view = sf.View(sf.Rectangle((0, 0), (size.x, size.y))) self.target.view = view self._setup_components()
def event(self, events): super().event(events) for e in events: if type(e) == sf.ResizeEvent: # Window has been resized. Resize components. size = self.target.size view = sf.View(sf.Rectangle((0, 0), (size.x, size.y))) self.target.view = view self._setup_buttons()
def _hovering(self): """Returns whether the mouse is above the button.""" mpos = self.target.map_pixel_to_coords( sf.Mouse.get_position(self.target)) pos = self.get_position() bounds = self.get_bounds() rect = sf.Rectangle(pos, bounds) return rect.contains(mpos)
def add_icon(self, name): x, y, path = name.split(':') pos = sf.Vector2(int(x), int(y)) rect = sf.Rectangle( sf.Vector2(pos.x * self.size.x, pos.y * self.size.y), self.size) tex = self.tex_pool.get(path) spr = sf.Sprite(tex, rect) spr.position = self.get_pos(self.next_id) self.render.draw(spr) icon = self.next_id self.next_id += 1 return icon
def __call__(self, scrollSpeed=50): current_view = sf.View() current_view.reset( sf.Rectangle((0, 0), (self._mode.width, self._mode.width))) window = sf.RenderWindow(self._mode, "Fractal Application.", sf.Style.TITLEBAR + sf.Style.CLOSE) while window.is_open: for event in window.events: if type(event) is sf.CloseEvent: window.close() elif type(event) is sf.KeyEvent and event.pressed: if sf.Keyboard.is_key_pressed(sf.Keyboard.NUM1): self._fractal = MandelbrotSet(self._mode.width, self._mode.height) elif sf.Keyboard.is_key_pressed(sf.Keyboard.NUM2): self._fractal = KochSnowflake(5) elif sf.Keyboard.is_key_pressed(sf.Keyboard.NUM3): self._fractal = SerpinskiTriangle( 0, (320, 100), (100, 500), (540, 500)) elif type(event) is sf.MouseWheelEvent: if event.delta > 0: current_view.size = (current_view.size.x - scrollSpeed, current_view.size.y - scrollSpeed) elif event.delta < 0: current_view.size = (current_view.size.x + scrollSpeed, current_view.size.y + scrollSpeed) elif type(event) is sf.MouseMoveEvent: if sf.Mouse.is_button_pressed(sf.Mouse.LEFT): x, y = event.position current_view.center = self._mode.width - x, self._mode.height - y window.view = current_view window.clear(sf.Color.WHITE) window.draw(self._fractal) window.display()
def get_rect(self, cell): pos = self.get_pos(cell) return sf.Rectangle(pos, self.size)