def __init__(self, resource, player_data, renderer): """Constructor. :param resource: The ui resource :type resource: :class:`loaders.Resource` :param renderer: Renderer to use for UI rendering. :type renderer: :class:`renderer.Renderer` """ self.renderer = renderer self.w = renderer.width self.h = renderer.height self.scene = Scene() self.camera = OrthoCamera( -self.w / 2, +self.w / 2, +self.h / 2, -self.h / 2, 0, 1) self.font = Font(resource['font'], 14) self.shader = resource['shader'] self.color = Vec(0.7, 0.7, 0.7) # Mode node self.game_mode_node = self.scene.root.add_child(TextNode( self.font, self.shader, Context.GameMode.default.value, self.color)) self.transform(self.game_mode_node, self.w * 0.85, 20) # FPS counter self.fps_counter_node = self.scene.root.add_child(TextNode( self.font, self.shader, 'FPS', self.color)) self.transform(self.fps_counter_node, self.w * 0.85, 0) # clock self.clock = self.scene.root.add_child(TextNode( self.font, self.shader, '--:--', self.color)) self.transform(self.clock, self.w * 0.5, 0) avatar_res, avatar = player_data['avatar_res'], player_data['avatar'] # avatar self.avatar = Avatar(avatar_res, avatar) self.scene.root.add_child(self.avatar.node) self.transform(self.avatar.node, 0, 0) # healthbar self.health_bar = HealthBar( resource['health_bar'], avatar_res.data['width'], resource['health_bar'].data['height']) self.scene.root.add_child(self.health_bar.node) self.transform(self.health_bar.node, 0, avatar_res.data['width'] + 5)
from renderer.light import Light from renderer.camera import Camera from renderer.scene import Scene from renderer.material import Material from renderer.mathlib import Vector from renderer.raytracer import Raytracer red = Material('red', .8, .1, .1, .25) green = Material('green', .5, .9, .1, .25) blue = Material('blue', .1, .4, .8, .25) white = Material('white', 1.0, 1.0, 1.0, 0.9) shapes = [ Sphere(-.5, 0., -2., .75, red), Sphere(1., 0., -2., .5, blue), Sphere(-.5, -.25, -1.25, .8, green), Plane(Vector(0.0, -0.5, 0.0), Vector(0.0, 1.0, 0.0), white) ] lights = [Light(0., 2, -2., 0.75), Light(0., .5, -1., 0.75)] camera = Camera(0, 0, -.5, ray_depth=4, width=600, height=600) scene = Scene(shapes, lights, camera) raytracer = Raytracer(scene) image = raytracer.render(subsamples=0) imsave(image, 'render.png')
class UI: """User interface. This class encapsulates the user interface creation and management. """ def __init__(self, resource, player_data, renderer): """Constructor. :param resource: The ui resource :type resource: :class:`loaders.Resource` :param renderer: Renderer to use for UI rendering. :type renderer: :class:`renderer.Renderer` """ self.renderer = renderer self.w = renderer.width self.h = renderer.height self.scene = Scene() self.camera = OrthoCamera( -self.w / 2, +self.w / 2, +self.h / 2, -self.h / 2, 0, 1) self.font = Font(resource['font'], 14) self.shader = resource['shader'] self.color = Vec(0.7, 0.7, 0.7) # Mode node self.game_mode_node = self.scene.root.add_child(TextNode( self.font, self.shader, Context.GameMode.default.value, self.color)) self.transform(self.game_mode_node, self.w * 0.85, 20) # FPS counter self.fps_counter_node = self.scene.root.add_child(TextNode( self.font, self.shader, 'FPS', self.color)) self.transform(self.fps_counter_node, self.w * 0.85, 0) # clock self.clock = self.scene.root.add_child(TextNode( self.font, self.shader, '--:--', self.color)) self.transform(self.clock, self.w * 0.5, 0) avatar_res, avatar = player_data['avatar_res'], player_data['avatar'] # avatar self.avatar = Avatar(avatar_res, avatar) self.scene.root.add_child(self.avatar.node) self.transform(self.avatar.node, 0, 0) # healthbar self.health_bar = HealthBar( resource['health_bar'], avatar_res.data['width'], resource['health_bar'].data['height']) self.scene.root.add_child(self.health_bar.node) self.transform(self.health_bar.node, 0, avatar_res.data['width'] + 5) def transform(self, node, x, y): """Transform the UI scene node from screen space to scene space. :param node: Scene node to transform. :type node: :class:`renderer.scene.SceneNode` :param x: Screen X coordinate. :type x: float :param y: Screen Y coordinate. :type y: float """ tx = x - self.w / 2 ty = self.h / 2 - y node.transform.identity() node.transform.translate(Vec(tx, ty, -0.5)) node.transform.rotate(Vec(1, 0, 0), pi / 2) def set_fps(self, number): """Set the current frame rate in FPS widget. :param number: Number of frames per second to visualize. :type number: int """ self.fps_counter_node.text = 'FPS: {}'.format(number) def set_mode(self, mode=None): """Set the current game mode on the game mode widget. :param number: The game mode: None in case of default :type number: :enum:`context.Context.GameMode` """ self.game_mode_node.text = '{}'.format(mode.value) def set_clock(self, hour, minute): """Set the time in clock widget. :param hour: Hour. :type hour: int :param minute: Minute. :type minute: int """ self.clock.text = '{h:02d}:{m:02d}'.format(h=hour, m=minute) def render(self): """Render the user interface.""" self.scene.render(self.renderer, self.camera)