diff options
author | Gonzalo Odiard <godiard@gmail.com> | 2012-04-29 06:15:59 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2012-04-29 06:15:59 (GMT) |
commit | b5d1a3cb3a4a39e9852c0ed2d7a7da7ef653d7d4 (patch) | |
tree | cd6b82b624522e2e9b1706bb71714d890fe881e7 | |
parent | 02d55dafcc4676b2055cd906e3703e2967e640f6 (diff) |
Add first version of a object to display the state of the game
Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
-rw-r--r-- | stateview.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/stateview.py b/stateview.py new file mode 100644 index 0000000..a27ab8d --- /dev/null +++ b/stateview.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python +# Copyright (C) 2011, One Laptop Per Child +# Author, Gonzalo Odiard +# License: LGPLv2 +# +# The class StateView draw how many questions there are in the game, +# how many have been read, and how many have been replied. + +import gtk +import cairo +import rsvg +import model +import math + + +class StateView(): + + def __init__(self, model, x, y, width, height): + self.model = model + self._x = x + self._y = y + self._width = width + self._height = height + svg = rsvg.Handle(file='./icons/question.svg') + self._tmp_image = cairo.ImageSurface(cairo.FORMAT_ARGB32, + svg.props.width, svg.props.height) + self._tmp_ctx = cairo.Context(self._tmp_image) + svg.render_cairo(self._tmp_ctx) + self._svg_width = svg.props.width + + def draw(self, ctx): + # calculate cell size + cant_questions = len(self.model.data['questions']) + if cant_questions == 0: + return + icon_size = int(self._width / cant_questions) # margin + print "icon_size", icon_size + + state = self.model.data['state'] + displayed_questions = state['displayed_questions'] + replied_questions = state['replied_questions'] + + scale = float(self._svg_width) / float(icon_size) + print "scale", scale + ctx.translate(self._x, self._y) + for n in range(cant_questions): + if n < replied_questions: + radio = icon_size / 2.0 + ctx.arc(radio, radio, radio, 0., 2 * math.pi) + ctx.set_source_rgb(0.913, 0.733, 0.0) # eebb00 + ctx.fill() + + ctx.scale(1.0 / scale, 1.0 / scale) + ctx.set_source_surface(self._tmp_image) + if n < displayed_questions: + ctx.paint() + else: + ctx.paint_with_alpha(0.25) + ctx.scale(scale, scale) + ctx.translate(icon_size, 0) + + +def main(): + window = gtk.Window() + _model = model.GameModel() + state_view = StateView(_model, 10, 10, 150, 200) + area = gtk.DrawingArea() + + # add fake questions to test + _model.data['questions'].append('') + _model.data['questions'].append('') + _model.data['questions'].append('') + _model.data['questions'].append('') + _model.data['questions'].append('') + + # ...and a fake state + state = {'displayed_questions': 3, 'replied_questions': 2} + _model.data['state'] = state + + def expose(widget, event, state_view): + ctx = widget.window.cairo_create() + state_view.draw(ctx) + return False + + area.connect('expose_event', expose, state_view) + + window.add(area) + window.connect("destroy", gtk.main_quit) + window.show_all() + gtk.main() + +if __name__ == "__main__": + main() |