diff options
author | florent <florent.pigout@gmail.com> | 2011-04-26 20:47:46 (GMT) |
---|---|---|
committer | florent <florent.pigout@gmail.com> | 2011-04-26 20:47:46 (GMT) |
commit | 0bce2460d1a8baeddb8a751bc55b326934734c95 (patch) | |
tree | 323b5d1174a4081c48de4365defb2814988d0757 | |
parent | 79baf8c40594ca9f874994cf80508a2b5d44eeae (diff) |
udpate scene rendering way of doing to enhance perfomance issue
-rw-r--r-- | atoidejouer/tools/image.py | 29 | ||||
-rw-r--r-- | atoidejouer/ui/screens.py | 339 |
2 files changed, 152 insertions, 216 deletions
diff --git a/atoidejouer/tools/image.py b/atoidejouer/tools/image.py index 5f0ec2d..9b9d47c 100644 --- a/atoidejouer/tools/image.py +++ b/atoidejouer/tools/image.py @@ -1,12 +1,15 @@ # python import -import gtk, os, struct, StringIO +import gtk, logging, os, struct, StringIO # atoidejouer import from atoidejouer.tools import storage +# get application logger +logger = logging.getLogger('atoidejouer') -def get_pixbuf(file_path, max_width, max_height): + +def get_pixbuf(file_path, max_width, max_height, use_max=False): # little check if os.path.exists(file_path): pass @@ -18,10 +21,14 @@ def get_pixbuf(file_path, max_width, max_height): if _info in ['image/png']: # update size _width, _height = compute_width_height(_width, _height, - max_width, max_height) + max_width, max_height, use_max=use_max) # prepare preview _pixbuf = gtk.gdk.pixbuf_new_from_file(file_path) - # TODO resize it in nicer way + # DEBUG + logger.debug('[image] get_pixbuf - _width: %s' % _width) + logger.debug('[image] get_pixbuf - _height: %s' % _height) + # DEBUG + # .. return _pixbuf.scale_simple(_width, _height, gtk.gdk.INTERP_BILINEAR) else: @@ -47,7 +54,7 @@ def get_sequence_first_graphic(type_, sequence_name, size=None): return get_pixbuf(_path, *size) -def compute_width_height(width, height, max_width, max_height): +def compute_width_height(width, height, max_width, max_height, use_max=False): # compute ratio _ratio_scr = max_width / float(max_height) _ratio_img = width / float(height) @@ -58,15 +65,19 @@ def compute_width_height(width, height, max_width, max_height): width = max_width height = int(max_width / _ratio_img) elif _ratio_img < _ratio_scr: - width = int(max_height / _ratio_img) + width = int(max_height * _ratio_img) height = max_height else: width = max_width height = max_height + # .. + return width, height + # .. + elif use_max is True: + return max_width, max_height + # .. else: - pass - # return update - return width, height + return width, height def get_image_info(path): diff --git a/atoidejouer/ui/screens.py b/atoidejouer/ui/screens.py index e943ade..f7ad2e5 100644 --- a/atoidejouer/ui/screens.py +++ b/atoidejouer/ui/screens.py @@ -37,27 +37,21 @@ def _on_image_clicked(screen, widget, sequence_name): # .. do update screen.timeline.set_active_sequence(sequence_name) -class ScreenStory(gtk.Fixed): + +class ScreenStory(gtk.Frame): # gtk.Fixed def __init__(self, toolbar, height_offset=0, width_offset=0, - set_canvas=False, image_cb=None): + set_canvas=False, frame=None): # init parent - gtk.Fixed.__init__(self) + gtk.Frame.__init__(self) + self.set_shadow_type(gtk.SHADOW_NONE) # keep toolbar self.toolbar = toolbar # .. self._set_canvas = set_canvas - self._image_cb = image_cb - # set has window for bg color issue - self.set_has_window(True) - self.modify_bg(gtk.STATE_NORMAL, COLOR_WHITE) - # create dummy - self.__dummy = gtk.Fixed() - # do it a window - self.__dummy.set_has_window(True) - self.__dummy.modify_bg(gtk.STATE_NORMAL, COLOR_WHITE) + # init frame dict + self.__frame_dict = dict() # init image dicts - self.__graphics = dict() self.__sizes = dict() self.__sounds = dict() # init fullscreen flag @@ -74,6 +68,15 @@ class ScreenStory(gtk.Fixed): # and show self._show() + def __init_buffer(self): + # .. + _buffer = gtk.Fixed() + # set has window for bg color issue + _buffer.set_has_window(True) + _buffer.modify_bg(gtk.STATE_NORMAL, COLOR_WHITE) + # .. + return _buffer + def _get_keys(self, type_): # .. if type_ == 'graphics': @@ -81,30 +84,11 @@ class ScreenStory(gtk.Fixed): else: return self.toolbar.activity.sound_keys - def _hide_all(self): - # show dummy - self.__dummy.show() - # update canvas - if self._set_canvas is True: - self.toolbar.activity.set_canvas(self.__dummy) - else: - pass - # hide us - self.hide() - - def _show_all(self): - # show us - self._show() - # hide dummy - self.__dummy.hide() - def set_fullscreen(self, fullscreen): # update fullscreen flag self.fullscreen = fullscreen # has changed self._fullscreen_changed = True - # hide all - self._hide_all() # refresh self.refresh() @@ -113,55 +97,32 @@ class ScreenStory(gtk.Fixed): self._screen_height = gtk.gdk.screen_height() - self._height_offset self._screen_width = gtk.gdk.screen_width() - self._width_offset - def _clear_graphic(self, filecode): - # ... - if filecode in self.__graphics: - # .. - self.__graphics[filecode].destroy() - # .. - if filecode in self.__graphics: - del self.__graphics[filecode] - else: - pass - # .. - if filecode in self.__sizes: - del self.__sizes[filecode] - else: - pass - else: - pass - def _check_clear(self): # need clear if self._get_keys('graphics').get_clear(self.toolbar.name) is True: # destroy all - for _k in self.__graphics.keys()[:]: - self._clear_graphic(_k) + for _d_area in self.__frame_dict.values(): + _d_area.destroy() + # ... + self.__frame_dict = dict() + self.__sizes = dict() # done self._get_keys('graphics').set_clear(self.toolbar.name, False) + # .. + return True else: - pass + return False def refresh(self): - # clear ?? - self._check_clear() # get the current frame _frame = 0 if self.toolbar.activity._thread is None\ else self.toolbar.activity._thread._time - # screen size first - self._refresh_screen_size() - # for each images - for _n in self._get_keys('graphics').get_names(): - # do move - self.__refresh_image(_n, _frame) - # draw mask - self.__refresh_default('mask') - # show first - if self._fullscreen_changed is True: - # disable changed flag - self._fullscreen_changed = False - # show all - self._show_all() + # clear ?? + if self._check_clear() is True\ + or _frame not in self.__frame_dict: + self.__buffer_update(_frame) + elif _frame in self.__frame_dict: + self.__set_child(self.__frame_dict[_frame]) else: pass # refresh sounds if playing @@ -180,17 +141,72 @@ class ScreenStory(gtk.Fixed): else: _sound.pause() - def _update_w_h(self, filecode, filename): - if filecode in self.__sizes: + def __set_child(self, child): + # remove previous + _previous = self.get_child() + if _previous is None: + pass + else: + self.remove(_previous) + # do show + child.show() + # set new + self.add(child) + + def __expose(self, drawing_area, event, frame): + # .. + if drawing_area.window is None: + pass + else: + # .. + _context = drawing_area.window.cairo_create() + # init rec + _rec = self.get_allocation() + # paint it white + _context.rectangle(0, 0, _rec.width, _rec.height) + _context.set_source_rgb(1, 1, 1) + _context.paint() + # for each images + for _n in self._get_keys('graphics').get_names(): + # do move + self.__refresh_image(_n, frame, _context, _rec) + # draw mask + self.__refresh_default('mask', _context, _rec) + # clean + if frame in self.__frame_dict: + del self.__frame_dict[frame] + else: + pass + # update dict + self.__frame_dict[frame] = drawing_area + # show first + if self._fullscreen_changed is True: + # disable changed flag + self._fullscreen_changed = False + else: + pass + + def __buffer_update(self, frame): + # screen size first + self._refresh_screen_size() + # + _drawing_area = gtk.DrawingArea() + _drawing_area.connect("expose-event", self.__expose, frame) + # .. + self.__set_child(_drawing_area) + + def _update_w_h(self, filename): + if filename in self.__sizes: # get image size - return self.__sizes[filecode] + return self.__sizes[filename] else: # get graphics path _path = storage.get_image_path(filename) # get file size _c, _w, _h = image.get_image_info(_path) # .. - if (self.fullscreen is True or self._screen_width < 1024)\ + if (self.fullscreen is True\ + or self._screen_width < 1024)\ and _w != 0: _new_w = _w * self._screen_width / 1024 _new_h = _h * _new_w / _w @@ -198,7 +214,7 @@ class ScreenStory(gtk.Fixed): _new_w = _w _new_h = _h # update size - self.__sizes[filecode] = _new_w, _new_h + self.__sizes[filename] = _new_w, _new_h # return new values return _new_w, _new_h @@ -220,136 +236,47 @@ class ScreenStory(gtk.Fixed): # return it return _x, _y - def __hide_previous_images(self, seq_codes, current): - # hide previous - for _filename, _code in seq_codes.items(): - # ensure image - if _filename == current: - continue - elif _code in self.__graphics: - # get previous image - _image = self.__graphics[_code] - # hide it - _image.hide() - else: - continue - - def __refresh_default(self, default_name): - _code = '__%s' % default_name + def __refresh_default(self, default_name, context, rec): + # _code = '__%s' % default_name _filename = '%s_default' % default_name - # ensure current image - if _code in self.__graphics: - # get image - _image = self.__graphics[_code] - # redraw all or move only - if self._fullscreen_changed is True: - # remove previous image - _image.destroy() - # create new image - self._add_image(None, _filename, filecode=_code) - else: - # do show - _image.show() - else: - # create new image - self._add_image(None, _filename, filecode=_code) + # get/update width and height + _w, _h = self._update_w_h(_filename) + # update x, y + _x, _y = self._update_x_y(_w, _h, 0, 0) + # .. + self.__update_drawing_area(context, rec, _filename, + (_w, _h), (_x, _y)) - def __refresh_image(self, sequence_name, frame): - # get sequence file codes - _seq_codes = self._get_keys('graphics').get_sequence_codes( - self.toolbar.name, sequence_name) + def __refresh_image(self, sequence_name, frame, context, rec): # get sequence filename _current, _f_type, _k_type = self._get_keys('graphics').get_current( sequence_name, frame) - # .. - self.__hide_previous_images(_seq_codes, _current) # need current if _current is None: return else: - pass - # current file code - _filecode = _seq_codes[_current] if _current in _seq_codes else None - # ensure current image - if _filecode in self.__graphics: - # get previous image - _image = self.__graphics[_filecode] - # .. - _image.show() - else: - # .. - _image, _filecode = self._add_image(sequence_name, _current) - # little check - if _image is None: - return - else: - pass - # get align - _align = self._get_keys('graphics').get_align(sequence_name, frame, - _current) - # redraw all or move only - if self._fullscreen_changed is True: - # remove previous image - _image.destroy() - # create new image - _image, _filecode = self._add_image(sequence_name, _current, - align=_align) - else: + # get align + _p_align = self._get_keys('graphics').get_align(sequence_name, + frame, _current) # get/update width and height - _w, _h = self._update_w_h(_filecode, _current) + _w, _h = self._update_w_h(_current) # update x, y - _x, _y = self._update_x_y(_w, _h, *_align) - # do move - self.move(_image, _x, _y) - - def _add_image(self, sequence_name, current, align=None, filecode=None): - # get graphics path - _path = storage.get_image_path(current) - # little check - if os.path.exists(_path): - pass - else: - return None, None - # add a picture here - _image = gtk.Image() - # ensure/generate code - filecode = key.random_str() if filecode is None else filecode - # get/update width and height - _w, _h = self._update_w_h(filecode, current) - # get pixbuff - _pixbuf = gtk.gdk.pixbuf_new_from_file(_path) - # do resize and set image - _image.set_from_pixbuf( - _pixbuf.scale_simple(_w, _h, gtk.gdk.INTERP_BILINEAR)) - # set alignment - if align is None: - align = (0, 0) - else: - pass - # update story key object - self._get_keys('graphics').set_code(self.toolbar.name, sequence_name, - current, filecode) - # init positions - self._get_keys('graphics').set_align(self.toolbar.name, sequence_name, - current, align) - # show - _image.show() - # set cb - if self._image_cb is None: - _widget = _image - else: - _widget = gtk.Button() - _widget.show() - _widget.set_image(_image) - _widget.connect('clicked', self._image_cb, sequence_name) - # update graphic dict - self.__graphics[filecode] = _widget - # update x, y - _x, _y = self._update_x_y(_w, _h, *align) - # add - self.put(_widget, _x, _y) - # return image for futher use - return _widget, filecode + _x, _y = self._update_x_y(_w, _h, *_p_align) + # .. + self.__update_drawing_area(context, rec, _current, + (_w, _h), (_x, _y)) + + def __update_drawing_area(self, context, rec, filename, size, align): + # get path + _path = storage.get_image_path(filename) + # split size + _w, _h = size + # init pixbuf + _pixbuf = image.get_pixbuf(_path, _w, _h, use_max=True) + # add source + context.set_source_pixbuf(_pixbuf, *align) + # paint context + context.paint_with_alpha(1) def __stop_unused_sounds(self, sequence_codes, current_code): # hide previous @@ -373,10 +300,10 @@ class ScreenStory(gtk.Fixed): else: pass # current file code - _filecode = _seq_codes[_current] if _current in _seq_codes else None + _code = _seq_codes[_current] if _current in _seq_codes else None # ensure current image - if _filecode in self.__sounds: - _s = self.__sounds[_filecode] + if _code in self.__sounds: + _s = self.__sounds[_code] if _k_type is None: _s.pause() elif _k_type == 'key': @@ -392,11 +319,12 @@ class ScreenStory(gtk.Fixed): pass else: # ... TODO manage start time - _filecode = self._add_sound(sequence_name, _current) + _code, _sound = self._prepare_sound(sequence_name, _current) + _sound.play() # .. - self.__stop_unused_sounds(_seq_codes, _filecode) + self.__stop_unused_sounds(_seq_codes, _code) - def _add_sound(self, sequence_name, current): + def _prepare_sound(self, sequence_name, current): # get graphics path _path = storage.get_sound_path(current) # ensure/generate code @@ -407,19 +335,18 @@ class ScreenStory(gtk.Fixed): # ... _sound = sound.Player() _sound.load(_path) - _sound.play() # update graphic dict self.__sounds[_filecode] = _sound # return image for futher use - return _filecode + return _filecode, _sound def _show(self): - # .. + # first refresh self.refresh() + # .. + self.show() # update size request self.set_size_request(self._screen_width, self._screen_height) - # show self - self.show() # update canvas if self._set_canvas is True: self.toolbar.activity.set_canvas(self) @@ -457,10 +384,8 @@ class ScreenEdit(gtk.HBox): def add_scene(self, vbox): # get scene - # self.scene = ScreenStory(self.toolbar, height_offset=148, - # width_offset=300, image_cb=partial(_on_image_clicked, self)) self.scene = ScreenStory(self.toolbar, height_offset=148, - width_offset=300) + width_offset=300) # add it vbox.pack_start(self.scene, expand=True, fill=True) |