Mercurial > MadButterfly
comparison pyink/MBScene.py @ 1230:f0864d7177e3
Add MBDOM_UI
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 10 Jan 2011 11:45:12 +0800 |
parents | cc55f3de9c0b |
children | d28b1b840bfc |
comparison
equal
deleted
inserted
replaced
1229:cc55f3de9c0b | 1230:f0864d7177e3 |
---|---|
464 | 464 |
465 def rm_scene_node(self, scene_node): | 465 def rm_scene_node(self, scene_node): |
466 self._scenes_node.removeChild(scene_node) | 466 self._scenes_node.removeChild(scene_node) |
467 pass | 467 pass |
468 | 468 |
469 def rm_scene_node_n_group(self, scene_node): | |
470 scene_group_id = scene_node.getAttribute('ref') | |
471 scene_group_node = self.get_node(scene_group_id) | |
472 scene_group_node.parent.removeChild(scene_group_node) | |
473 | |
474 self._scenes_node.removeChild(scene_node) | |
475 pass | |
476 | |
469 ## \brief Create and add a svg:g for a scene under a group for a layer. | 477 ## \brief Create and add a svg:g for a scene under a group for a layer. |
470 # | 478 # |
471 def add_scene_group(self, layer_idx): | 479 def add_scene_group(self, layer_idx): |
472 layer = self._layers[layer_idx] | 480 layer = self._layers[layer_idx] |
473 doc = self._doc | 481 doc = self._doc |
514 layers[idx].idx = idx | 522 layers[idx].idx = idx |
515 pass | 523 pass |
516 pass | 524 pass |
517 pass | 525 pass |
518 | 526 |
527 ## \brief Remove layer and associated scene nodes and scene groups. | |
528 # | |
519 def rm_layer(self, layer_idx): | 529 def rm_layer(self, layer_idx): |
520 layers = self._layers | 530 layers = self._layers |
521 | 531 |
532 for layer in layers: | |
533 for scene_node in layer.scenes: | |
534 scene_group_id = scene_node.getAttribute('ref') | |
535 scene_group_node = self.get_node(scene_group_id) | |
536 scene_group_node.parent.removeChild(scene_group_node) | |
537 | |
538 scene_node.parent.removeChild(scene_node) | |
539 pass | |
540 pass | |
541 | |
522 del layers[layer_idx] | 542 del layers[layer_idx] |
523 | 543 |
524 for idx in range(layer_idx, len(layers)): | 544 for idx in range(layer_idx, len(layers)): |
525 layers[idx].idx = idx | 545 layers[idx].idx = idx |
526 pass | 546 pass |
533 if scene_group_id == node_id: | 553 if scene_group_id == node_id: |
534 return layer_idx, scene_node | 554 return layer_idx, scene_node |
535 pass | 555 pass |
536 pass | 556 pass |
537 pass | 557 pass |
558 | |
559 def get_layer_group(self, layer_idx): | |
560 layer = self._layers[layer_idx] | |
561 return layer.group | |
562 | |
563 def get_all_scene_node_of_layer(self, layer_idx): | |
564 layer = self._layers[layer_idx] | |
565 return layer.scenes | |
538 pass | 566 pass |
539 | 567 |
540 ## \brief Maintain frameline list for MBScene. | 568 ## \brief Maintain frameline list for MBScene. |
541 # | 569 # |
542 class MBScene_framelines(object): | 570 class MBScene_framelines(object): |
604 position = len(self._framelines) - layer_idx + 1 | 632 position = len(self._framelines) - layer_idx + 1 |
605 vbox.reorder_child(hbox, position) | 633 vbox.reorder_child(hbox, position) |
606 | 634 |
607 self._framelines[layer_idx: layer_idx] = [line] | 635 self._framelines[layer_idx: layer_idx] = [line] |
608 | 636 |
637 for idx in range(layer_idx, len(self._framelines)): | |
638 self._framelines[idx].layer_idx = idx | |
639 pass | |
640 | |
609 line.label = label | 641 line.label = label |
610 line.layer_idx = layer_idx | |
611 # TODO: The following line of code should be moved to MBScene. | 642 # TODO: The following line of code should be moved to MBScene. |
612 line.connect(line.FRAME_BUT_PRESS, self.onCellClick) | 643 line.connect(line.FRAME_BUT_PRESS, self.onCellClick) |
613 line.connect('motion-notify-event', self._change_hover_frameline) | 644 line.connect('motion-notify-event', self._change_hover_frameline) |
614 line.connect(frameline.FRAME_BUT_PRESS, self._change_active_frame) | 645 line.connect(frameline.FRAME_BUT_PRESS, self._change_active_frame) |
615 pass | 646 pass |
621 line = self._framelines[layer_idx] | 652 line = self._framelines[layer_idx] |
622 | 653 |
623 hbox = line.parent | 654 hbox = line.parent |
624 vbox.remove(hbox) | 655 vbox.remove(hbox) |
625 del self._framelines[layer_idx] | 656 del self._framelines[layer_idx] |
657 | |
658 for idx in range(layer_idx, len(self._framelines)): | |
659 self._framelines[idx].layer_idx = idx | |
660 pass | |
626 pass | 661 pass |
627 | 662 |
628 def _init_framelines(self): | 663 def _init_framelines(self): |
629 self._framelines = [] | 664 self._framelines = [] |
630 | 665 |
659 # screen and drawing framelines. | 694 # screen and drawing framelines. |
660 def _show_framelines(self): | 695 def _show_framelines(self): |
661 self._frameline_vbox.show_all() | 696 self._frameline_vbox.show_all() |
662 pass | 697 pass |
663 | 698 |
699 ## \brief Make given frame as current active frame. | |
700 # | |
664 def active_frame(self, layer_idx, frame_idx): | 701 def active_frame(self, layer_idx, frame_idx): |
665 frameline = self._framelines[layer_idx] | 702 frameline = self._framelines[layer_idx] |
666 self._active_frameline(frameline) | 703 self._active_frameline(frameline) |
667 frameline.active_frame(frame_idx) | 704 frameline.active_frame(frame_idx) |
668 pass | 705 pass |
706 | |
707 ## \brief Get layer and frame index of current active frame. | |
708 # | |
709 # \return (-1, -1) for no active, (layer_idx, frame_idx) for current | |
710 # active. | |
711 def get_active_frame(self): | |
712 if self._active_frameline: | |
713 layer_idx = self._active_frameline.layer_idx | |
714 frame_idx = self._active_frameline.get_active_frame() | |
715 if frame_idx != -1: | |
716 return layer_idx, frame_idx | |
717 pass | |
718 return -1, -1 | |
719 | |
720 ## \brief Get information of the key frame at left-side. | |
721 # | |
722 # The key frame, returned, is at the place of the give frame or its | |
723 # left-side. | |
724 def get_left_key_tween(self, layer_idx, frame_idx): | |
725 frameline = self._framelines[layer_idx] | |
726 start, end, fl_tween_type = frameline.get_frame_block_floor(frame_idx) | |
727 tween_type = self._frameline_tween_types.index(fl_tween_type) | |
728 return start, end, tween_type | |
729 | |
730 ## \brief Return information of a key frame and its tweening. | |
731 # | |
732 # This method return the key frame that the given frame is, or is in its | |
733 # tween. | |
734 # | |
735 # \return (start, end, tween_type) | |
736 def get_key_tween(self, layer_idx, frame_idx): | |
737 frameline = self._framelines[layer_idx] | |
738 start, end, fl_tween_type = frameline.get_frame_block(frame_idx) | |
739 | |
740 tween_type = self._frameline_tween_types.index(fl_tween_type) | |
741 return start, end, tween_type | |
742 | |
743 def get_all_key_tween_of_layer(self, layer_idx): | |
744 frameline = self._framelines[layer_idx] | |
745 info = frameline.get_frame_blocks() | |
746 return info | |
747 | |
748 ## \brief Tweening key frame to a give size | |
749 # | |
750 # The tween can be changed by tweening it again. | |
751 def tween(self, layer_idx, key_frame_idx, tween_len, | |
752 tween_type=TweenObject.TWEEN_TYPE_NORMAL): | |
753 assert tween_len > 0 | |
754 frameline = self._framelines[layer_idx] | |
755 right_frame_idx = key_frame_idx + tween_len - 1 | |
756 fl_tween_type = self._frameline_tween_types[tween_type] | |
757 | |
758 start, end, fl_tween_type = frameline.get_frame_block(frame_idx) | |
759 if start != key_frame_idx: | |
760 ValueError, 'invalid key frame (%d)' % (key_frame_idx) | |
761 if start < end: | |
762 frameline.rm_keyframe(end) | |
763 pass | |
764 frameline.add_keyframe(right_frame_idx) | |
765 frameline.tween(start, fl_tween_type) | |
766 pass | |
767 | |
768 def rm_keyframe(self, layer_idx, frame_idx): | |
769 frameline = self._framelines[layer_idx] | |
770 start, end, fl_tween_type = frameline.get_frame_block(frame_idx) | |
771 if start != frame_idx: | |
772 raise ValueError, 'no such key (%d, %d)' % (layer_idx, frame_idx) | |
773 | |
774 frameline.rm_keyframe(frame_idx) | |
775 if start < end: | |
776 frameline.rm_keyframe(end) | |
777 pass | |
778 pass | |
779 | |
780 def add_keyframe(self, layer_idx, frame_idx): | |
781 frameline = self._framelines[layer_idx] | |
782 frameline.add_keyframe(frame_idx) | |
783 pass | |
784 | |
785 ## \brief Get data associated with the given key frame. | |
786 # | |
787 def get_keyframe_data(self, layer_idx, frame_idx): | |
788 frameline = self._framelines[layer_idx] | |
789 data = frameline.get_frame_data(frame_idx) | |
790 return data | |
791 | |
792 ## \brief Set/associate data with the given key frame. | |
793 # | |
794 def set_keyframe_data(self, layer_idx, frame_idx, data): | |
795 frameline = self._framelines[layer_idx] | |
796 frameline.set_frame_data(frame_idx, data) | |
797 pass | |
798 | |
799 def insert_frames(self, layer_idx, frame_idx, num): | |
800 assert num > 0 | |
801 assert frame_idx >= 0 | |
802 frameline = self._framelines[layer_idx] | |
803 for i in range(num): | |
804 frameline.insert_frame(frame_idx) | |
805 pass | |
806 pass | |
807 | |
808 ## \brief Set label of a layer. | |
809 # | |
810 def set_layer_label(self, layer_idx, txt): | |
811 frameline = self._framelines[layer_idx] | |
812 frameline.label.set_text(txt) | |
813 pass | |
814 pass | |
815 | |
816 ## \brief Gateway of DOM-tree to syncrhonize data-model and UI. | |
817 # | |
818 # This class is a wrapper | |
819 class MBDOM_UI(object): | |
820 _tween_type_names = ('normal', 'scale') | |
821 | |
822 def __init__(self): | |
823 super(MBDOM_UI, self).__init__() | |
824 self._fl_mgr = MBScene_framelines() | |
825 self._dom = MBScene_dom() | |
826 pass | |
827 | |
828 ## \brief Update content of a frameline from scenes of respective layer. | |
829 # | |
830 def _update_frameline_content(self, layer_idx): | |
831 fl_mgr = self._fl_mgr | |
832 scene_nodes = self._fl_mgr.get_all_scene_node_of_layer(layer_idx): | |
833 for scene_node in scene_nodes: | |
834 start, end, tween_name = self._parse_one_scene(scene_node) | |
835 | |
836 fl_mgr.add_keyframe(layer_idx, start) | |
837 fl_mgr.set_keyframe_data(layer_idx, start, scene_node) | |
838 if start != end: | |
839 tween_type = self._tween_type_names.index(tween_name) | |
840 fl_mgr.tween(start, end, tween_type) | |
841 pass | |
842 pass | |
843 pass | |
844 | |
845 ## \brief Add a frameline for every found layer. | |
846 # | |
847 # This method is called to create a frameline for every layer found when | |
848 # loading a document. | |
849 # | |
850 def _add_frameline_for_every_layer(self): | |
851 for layer_idx in range(len(self._dom._layers)): | |
852 layer_group_node = self._dom.get_layer_group(layer_idx) | |
853 label = layer_group_node.getAttribute('inkscape:label') | |
854 | |
855 self._fl_mgr._add_frameline(layer_idx) | |
856 self._fl_mgr.set_layer_label(layer_idx, label) | |
857 | |
858 self._update_frameline_content(layer_idx) | |
859 pass | |
860 pass | |
861 | |
862 def handle_doc_root(self, doc, root): | |
863 self._dom.handle_doc_root(doc, root) | |
864 self._fl_mgr._init_framelines() | |
865 self._add_frameline_for_every_layer() | |
866 self._fl_mgr._show_framelines() | |
867 pass | |
868 | |
869 def add_key(self, layer_idx, key_idx): | |
870 scene_group = self._dom.add_scene_group(layer_idx) | |
871 scene_group_id = scene_group.getAttribute('id') | |
872 | |
873 scene_node = self._dom.add_scene_node(key_idx, key_idx) | |
874 scene_node.chg_scene_node(scene_node, ref=scene_group_id) | |
875 | |
876 self._fl_mgr.add_keyframe(layer_idx, key_idx) | |
877 self._fl_mgr.set_keyframe_data(layer_idx, key_idx, scene_node) | |
878 pass | |
879 | |
880 ## \brief Tweening a key frame. | |
881 # | |
882 # The tween of a key frame can be changed by tweening it again. | |
883 # | |
884 def tween(self, layer_idx, key_frame_idx, tween_len, | |
885 tween_type=TweenObject.TWEEN_TYPE_NORMAL): | |
886 self._fl_mgr.tween(layer_idx, key_frame_idx, tween_len, tween_type) | |
887 | |
888 end_frame_idx = key_frame_idx + tween_len - 1 | |
889 scene_node = self._fl_mgr.get_keyframe_data(layer_idx, key_frame_idx) | |
890 tween_name = self._tween_type_names[tween_type] | |
891 self._dom.chg_scene_node(scene_node, end=end_frame_idx, | |
892 tween_type=tween_name) | |
893 pass | |
894 | |
895 ## \brief Change tween info of a key frame | |
896 # | |
897 def chg_tween(self, layer_idx, key_frame_idx, | |
898 tween_len=None, tween_type=None): | |
899 scene_node = self._fl_mgr.get_keyframe_data(layer_idx, key_frame_idx) | |
900 start, end, old_tween_type = \ | |
901 self._fl_mgr.get_key_tween(layer_idx, key_frame_idx) | |
902 | |
903 if tween_len is not None: | |
904 end = start + tween_len - 1 | |
905 self._dom.chg_scene_node(scene_node, end=end) | |
906 pass | |
907 if tween_type is not None: | |
908 tween_name = self._tween_type_names[tween_type] | |
909 self._dom.chg_scene_node(scene_node, tween_type=tween_name) | |
910 pass | |
911 | |
912 if tween_type is None: | |
913 tween_type = old_tween_type | |
914 pass | |
915 self._fl_mgr.tween(layer_idx, start, end, tween_type) | |
916 pass | |
917 | |
918 def rm_key_n_tween(self, layer_idx, key_frame_idx): | |
919 scene_node = self._fl_mgr.get_keyframe_data(layer_idx, key_frame_idx) | |
920 self._dom.rm_scene_node_n_group(scene_node) | |
921 | |
922 self._fl_mgr.rm_key_n_tween(layer_idx, key_frame_idx) | |
923 pass | |
924 | |
925 def insert_frames(self, layer_idx, frame_idx, num): | |
926 self._fl_mgr.insert_frames(layer_idx, frame_idx, num) | |
927 | |
928 key_tweens = self._fl_mgr.get_all_key_tween_of_layer(layer_idx) | |
929 for start, end, tween_type in key_tweens: | |
930 if frame_idx > end: | |
931 continue | |
932 | |
933 scene_node = self._fl_mgr.get_keyframe_data(start) | |
934 self._dom.chg_scene_node(scene_node, start=start, end=end) | |
935 pass | |
936 pass | |
937 | |
938 def add_layer(self, layer_idx): | |
939 self._dom.add_layer(layer_idx) | |
940 self._fl_mgr._add_frameline(layer_idx) | |
941 self._fl_mgr._show_framelines() | |
942 pass | |
943 | |
944 def set_active_layer_frame(self, layer_idx, frame_idx): | |
945 self._fl_mgr.active_frame(layer_idx, frame_idx) | |
946 pass | |
947 | |
948 ## \bref Return current active frame and its layer. | |
949 # | |
950 # \return (layer_idx, frame_idx) of active frame, or (-1, -1) when no | |
951 # active one. | |
952 def get_active_layer_frame(self): | |
953 layer_idx, frame_idx = self._fl_mgr.get_active_frame() | |
954 return layer_idx, frame_idx | |
955 | |
956 def rm_layer(self, layer_idx): | |
957 self._dom.rm_layer(layer_idx) | |
958 self._fl_mgr._remove_frameline(layer_idx) | |
959 self._fl_mgr._show_framelines() | |
960 pass | |
961 | |
962 ## \brief Return associated group node for a key frame. | |
963 # | |
964 def get_keyframe_group(self, layer_idx, frame_idx): | |
965 scene_node = self._fl_mgr.get_keyframe_data(frame_idx) | |
966 scene_group_id = scene_node.getAttribute('ref') | |
967 scene_group_node = self._dom.get_node(scene_group_id) | |
968 return scene_group_node | |
969 | |
970 ## \brief Find an associated key frame and tween info for a group ID. | |
971 # | |
972 def find_keyframe_from_group(self, scene_group_id): | |
973 scene_node = self._dom.get_scene(scene_group_id) | |
974 start, end, tween_name = self._dom._parse_one_scene(scene_node) | |
975 tween_type = self._tween_type_names.index(tween_name) | |
976 return start, end, tween_type | |
977 | |
978 def get_left_key(self, layer_idx, frame_idx): | |
979 start, end, tween_type = \ | |
980 self._fl_mgr.get_left_key_tween(layer_idx, frame_idx) | |
981 return start, end, tween_type | |
669 pass | 982 pass |
670 | 983 |
671 ## \brief MBScene connect GUI and DOM-tree | 984 ## \brief MBScene connect GUI and DOM-tree |
672 # | 985 # |
673 # This class connect behavior of GUI to the DOM-tree. All about GUI is | 986 # This class connect behavior of GUI to the DOM-tree. All about GUI is |