Mercurial > MadButterfly
comparison pyink/FSM_window.py @ 1503:4d582306df85
Manage control points of transitions by _select_manager
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Sun, 08 May 2011 22:18:50 +0800 |
parents | 376a8cb7312e |
children | d5b677831671 |
comparison
equal
deleted
inserted
replaced
1502:376a8cb7312e | 1503:4d582306df85 |
---|---|
291 trn_cond = None | 291 trn_cond = None |
292 trn_g = None | 292 trn_g = None |
293 _arrow_node = None | 293 _arrow_node = None |
294 _path_node = None | 294 _path_node = None |
295 _control_points = None | 295 _control_points = None |
296 _selected_rect = None | |
296 | 297 |
297 def __init__(self, trn_cond): | 298 def __init__(self, trn_cond): |
298 self.trn_cond = trn_cond | 299 self.trn_cond = trn_cond |
299 pass | 300 pass |
300 | 301 |
304 self._state = state | 305 self._state = state |
305 self._states = states | 306 self._states = states |
306 self._fsm_layer = fsm_layer | 307 self._fsm_layer = fsm_layer |
307 self._control_layer = control_layer | 308 self._control_layer = control_layer |
308 pass | 309 pass |
310 | |
311 def _translate_page_xy(self, x, y): | |
312 return x, y | |
309 | 313 |
310 @staticmethod | 314 @staticmethod |
311 def _update_graph(path, arrow_node, path_node): | 315 def _update_graph(path, arrow_node, path_node): |
312 path_txt = 'M %f,%f C %f,%f %f,%f %f,%f' % tuple(path) | 316 path_txt = 'M %f,%f C %f,%f %f,%f %f,%f' % tuple(path) |
313 path_node.setAttribute('d', path_txt) | 317 path_node.setAttribute('d', path_txt) |
555 'fill: none') | 559 'fill: none') |
556 arrow_node.setAttribute('style', | 560 arrow_node.setAttribute('style', |
557 'stroke: #000000; stroke-width: 1; ' \ | 561 'stroke: #000000; stroke-width: 1; ' \ |
558 'fill: #000000') | 562 'fill: #000000') |
559 pass | 563 pass |
564 pass | |
565 | |
566 def show_selected(self): | |
567 if not self._selected_rect: | |
568 doc = self._doc | |
569 rect = doc.createElement('svg:rect') | |
570 control_layer = self._control_layer | |
571 rect.setAttribute('style', | |
572 'stroke: #404040; stroke-width: 1; ' | |
573 'stroke-dasharray: 6 4; fill: none') | |
574 control_layer.appendChild(rect) | |
575 self._selected_rect = rect | |
576 pass | |
577 | |
578 trn_g = self.trn_g | |
579 rect = self._selected_rect | |
580 | |
581 px, py, pw, ph = trn_g.getBBox() | |
582 x, y = self._translate_page_xy(px, py) | |
583 y = y - ph # px, py is left-bottom corner | |
584 | |
585 rect.setAttribute('x', str(x - 2)) | |
586 rect.setAttribute('y', str(y - 2)) | |
587 rect.setAttribute('width', str(pw + 4)) | |
588 rect.setAttribute('height', str(ph + 4)) | |
589 pass | |
590 | |
591 def hide_selected(self): | |
592 if not self._selected_rect: | |
593 return | |
594 | |
595 control_layer = self._control_layer | |
596 rect = self._selected_rect | |
597 control_layer.removeChild(rect) | |
598 self._selected_rect = None | |
560 pass | 599 pass |
561 pass | 600 pass |
562 | 601 |
563 class FSM_state(object): | 602 class FSM_state(object): |
564 _doc = None | 603 _doc = None |
852 | 891 |
853 | 892 |
854 class _select_manager(object): | 893 class _select_manager(object): |
855 selected_state = None | 894 selected_state = None |
856 selected_transition = None | 895 selected_transition = None |
896 controlled_transition = None | |
857 | 897 |
858 def deselect(self): | 898 def deselect(self): |
859 pass | 899 pass |
860 | 900 |
861 def select_state(self, state): | 901 def select_state(self, state): |
862 self.deselect() | 902 self.deselect() |
903 | |
863 self.selected_state = state | 904 self.selected_state = state |
864 state.show_selected() | 905 state.show_selected() |
906 | |
865 def hide(): | 907 def hide(): |
866 state.hide_selected() | 908 state.hide_selected() |
867 self.selected_state = None | 909 self.selected_state = None |
868 pass | 910 pass |
869 self.deselect = hide | 911 self.deselect = hide |
870 pass | 912 pass |
871 | 913 |
872 def select_transition(self, transition): | 914 def select_transition(self, transition): |
873 self.deselect() | 915 self.deselect() |
916 | |
874 self.selected_transition = transition | 917 self.selected_transition = transition |
918 transition.show_selected() | |
919 | |
920 def hide(): | |
921 transition.hide_selected() | |
922 self.selected_transition = None | |
923 pass | |
924 self.deselect = hide | |
925 pass | |
926 | |
927 def control_transition(self, transition): | |
928 self.deselect() | |
929 | |
930 self.controlled_transition = transition | |
875 transition.show_control_points() | 931 transition.show_control_points() |
932 | |
876 def hide(): | 933 def hide(): |
877 transition.hide_control_points() | 934 transition.hide_control_points() |
878 self.selected_transition = None | 935 self.controlled_transition = None |
879 pass | 936 pass |
880 self.deselect = hide | 937 self.deselect = hide |
881 pass | 938 pass |
882 pass | 939 pass |
883 | 940 |
934 # that is not handled by them. | 991 # that is not handled by them. |
935 # | 992 # |
936 def _handle_transition_mouse_events(self, trn, evtype, button, x, y): | 993 def _handle_transition_mouse_events(self, trn, evtype, button, x, y): |
937 if evtype == pybInkscape.PYSPItem.PYB_EVENT_BUTTON_PRESS and \ | 994 if evtype == pybInkscape.PYSPItem.PYB_EVENT_BUTTON_PRESS and \ |
938 button == 3: | 995 button == 3: |
996 self._select.select_transition(trn) | |
939 self._show_transition_menu(trn) | 997 self._show_transition_menu(trn) |
940 elif evtype == pybInkscape.PYSPItem.PYB_EVENT_MOUSE_ENTER: | 998 elif evtype == pybInkscape.PYSPItem.PYB_EVENT_MOUSE_ENTER: |
941 trn.start_hint() | 999 trn.start_hint() |
942 elif evtype == pybInkscape.PYSPItem.PYB_EVENT_MOUSE_LEAVE: | 1000 elif evtype == pybInkscape.PYSPItem.PYB_EVENT_MOUSE_LEAVE: |
943 trn.stop_hint() | 1001 trn.stop_hint() |
990 self._candidate_target = None | 1048 self._candidate_target = None |
991 pass | 1049 pass |
992 return | 1050 return |
993 self._select.deselect() | 1051 self._select.deselect() |
994 window.pop_grabs() | 1052 window.pop_grabs() |
1053 window.ungrab_bg() | |
995 pass | 1054 pass |
996 | 1055 |
997 window = self._window | 1056 window = self._window |
998 window.push_grabs() | 1057 window.push_grabs() |
999 window.ungrab_bg() | 1058 window.ungrab_bg() |
1071 | 1130 |
1072 _window = None | 1131 _window = None |
1073 _domview = None | 1132 _domview = None |
1074 | 1133 |
1075 _select = None | 1134 _select = None |
1135 | |
1136 _on_deactivate = None | |
1076 | 1137 |
1077 def __init__(self, window, domview_ui, select_man): | 1138 def __init__(self, window, domview_ui, select_man): |
1078 super(_FSM_move_state_mode, self).__init__() | 1139 super(_FSM_move_state_mode, self).__init__() |
1079 | 1140 |
1080 self._window = window | 1141 self._window = window |
1081 self._domview = domview_ui | 1142 self._domview = domview_ui |
1082 self._locker = domview_ui | 1143 self._locker = domview_ui |
1083 | 1144 |
1084 self._popup = _FSM_popup(window, domview_ui, select_man) | 1145 self._popup = _FSM_popup(window, domview_ui, select_man) |
1085 self._select = select_man | 1146 self._select = select_man |
1147 | |
1148 self._on_deactivate = [] | |
1086 pass | 1149 pass |
1087 | 1150 |
1088 def on_move_state_background(self, item, evtype, button, x, y): | 1151 def on_move_state_background(self, item, evtype, button, x, y): |
1089 if evtype == pybInkscape.PYSPItem.PYB_EVENT_BUTTON_RELEASE: | 1152 if evtype == pybInkscape.PYSPItem.PYB_EVENT_BUTTON_RELEASE: |
1090 self._select.deselect() | 1153 self._select.deselect() |
1139 target_name = trn.target | 1202 target_name = trn.target |
1140 states = trn._states | 1203 states = trn._states |
1141 state_target = states[target_name] | 1204 state_target = states[target_name] |
1142 domview = self._domview | 1205 domview = self._domview |
1143 window = self._window | 1206 window = self._window |
1207 select = self._select | |
1144 | 1208 |
1145 def c1_update(rx, ry): | 1209 def c1_update(rx, ry): |
1146 nc1x = c1x + rx | 1210 nc1x = c1x + rx |
1147 nc1y = c1y + ry | 1211 nc1y = c1y + ry |
1148 cx, cy = state_src.xy | 1212 cx, cy = state_src.xy |
1158 | 1222 |
1159 state_name = state_src.state_name | 1223 state_name = state_src.state_name |
1160 cond = trn.trn_cond | 1224 cond = trn.trn_cond |
1161 domview.set_transition_path(state_name, cond, path) | 1225 domview.set_transition_path(state_name, cond, path) |
1162 | 1226 |
1163 trn.show_control_points() | 1227 select.control_transition(trn) |
1164 trn.update() | |
1165 pass | 1228 pass |
1166 | 1229 |
1167 def c1_start(): | 1230 def c1_start(): |
1168 def relay_event(item, evtype, button, x, y): | 1231 def relay_event(item, evtype, button, x, y): |
1169 c1_dragger.mouse_event(evtype, button, x, y) | 1232 c1_dragger.mouse_event(evtype, button, x, y) |
1194 | 1257 |
1195 state_name = state_src.state_name | 1258 state_name = state_src.state_name |
1196 cond = trn.trn_cond | 1259 cond = trn.trn_cond |
1197 domview.set_transition_path(state_name, cond, path) | 1260 domview.set_transition_path(state_name, cond, path) |
1198 | 1261 |
1199 trn.show_control_points() | 1262 select.control_transition(trn) |
1200 trn.update() | |
1201 pass | 1263 pass |
1202 | 1264 |
1203 def c2_start(): | 1265 def c2_start(): |
1204 def relay_event(item, evtype, button, x, y): | 1266 def relay_event(item, evtype, button, x, y): |
1205 c2_dragger.mouse_event(evtype, button, x, y) | 1267 c2_dragger.mouse_event(evtype, button, x, y) |
1228 pass | 1290 pass |
1229 | 1291 |
1230 ## \brief A transition was selected. | 1292 ## \brief A transition was selected. |
1231 # | 1293 # |
1232 def _select_transition(self, trn): | 1294 def _select_transition(self, trn): |
1233 self._select.select_transition(trn) | 1295 def handle_bg(item, evtype, button, x, y): |
1234 | 1296 if evtype == pybInkscape.PYSPItem.PYB_EVENT_BUTTON_PRESS: |
1297 window.pop_grabs() | |
1298 window.ungrab_bg() | |
1299 | |
1300 self._on_deactivate.pop() | |
1301 self._on_deactivate.pop() | |
1302 | |
1303 select.deselect() | |
1304 del self._hint_transition | |
1305 pass | |
1306 pass | |
1307 | |
1308 select = self._select | |
1309 select.control_transition(trn) | |
1310 | |
1311 self._hint_transition = lambda trn: None | |
1235 trn.stop_hint() | 1312 trn.stop_hint() |
1236 window = self._window | 1313 window = self._window |
1314 window.push_grabs() | |
1237 window.ungrab_bg() | 1315 window.ungrab_bg() |
1316 window.grab_bg(handle_bg) | |
1238 | 1317 |
1239 self._install_trn_cps_mouse(trn) | 1318 self._install_trn_cps_mouse(trn) |
1319 self._on_deactivate.append(window.pop_grabs) | |
1320 def del_hint_transition(): | |
1321 del self._hint_transition | |
1322 pass | |
1323 self._on_deactivate.append(del_hint_transition) | |
1240 pass | 1324 pass |
1241 | 1325 |
1242 ## \brief Hint for mouse over a transition. | 1326 ## \brief Hint for mouse over a transition. |
1243 # | 1327 # |
1244 def _hint_transition(self, trn): | 1328 def _hint_transition(self, trn): |
1277 window = self._window | 1361 window = self._window |
1278 window._emit_leave_mode() | 1362 window._emit_leave_mode() |
1279 window._clear_leave_mode_cb() | 1363 window._clear_leave_mode_cb() |
1280 window.ungrab_all() | 1364 window.ungrab_all() |
1281 | 1365 |
1366 window.ungrab_bg() | |
1282 window.grab_bg(self.on_move_state_background) | 1367 window.grab_bg(self.on_move_state_background) |
1283 window.grab_state(self._handle_move_state_state) | 1368 window.grab_state(self._handle_move_state_state) |
1284 window.grab_transition(self._handle_transitoin_mouse_events) | 1369 window.grab_transition(self._handle_transitoin_mouse_events) |
1285 #window.grab_edit_transition(self._handle_edit_transition) | 1370 #window.grab_edit_transition(self._handle_edit_transition) |
1286 #window.grab_transition_apply(self._handle_transition_apply) | 1371 #window.grab_transition_apply(self._handle_transition_apply) |
1288 self._popup.popup_install_handler() | 1373 self._popup.popup_install_handler() |
1289 pass | 1374 pass |
1290 | 1375 |
1291 def deactivate(self): | 1376 def deactivate(self): |
1292 self._select.deselect() | 1377 self._select.deselect() |
1378 while self._on_deactivate: | |
1379 deactivate = self._on_deactivate.pop() | |
1380 deactivate() | |
1381 pass | |
1293 pass | 1382 pass |
1294 pass | 1383 pass |
1295 | 1384 |
1296 | 1385 |
1297 class _FSM_add_state_mode(object): | 1386 class _FSM_add_state_mode(object): |
1438 window = self._window | 1527 window = self._window |
1439 | 1528 |
1440 window._emit_leave_mode() | 1529 window._emit_leave_mode() |
1441 window.ungrab_all() | 1530 window.ungrab_all() |
1442 | 1531 |
1532 window.ungrab_bg() | |
1443 window.grab_bg(self.on_add_state_background) | 1533 window.grab_bg(self.on_add_state_background) |
1444 window.grab_state(self._handle_state_mouse_events) | 1534 window.grab_state(self._handle_state_mouse_events) |
1445 pass | 1535 pass |
1446 | 1536 |
1447 def deactivate(self): | 1537 def deactivate(self): |
1584 self._leave_mode_cb = None | 1674 self._leave_mode_cb = None |
1585 pass | 1675 pass |
1586 pass | 1676 pass |
1587 | 1677 |
1588 def ungrab_all(self): | 1678 def ungrab_all(self): |
1589 self.ungrab_bg() | |
1590 self.ungrab_mouse() | |
1591 self.ungrab_state() | 1679 self.ungrab_state() |
1592 self.ungrab_add_transition() | 1680 self.ungrab_add_transition() |
1593 self.ungrab_transition() | 1681 self.ungrab_transition() |
1594 self.ungrab_edit_transition() | 1682 self.ungrab_edit_transition() |
1595 self.ungrab_edit_state() | 1683 self.ungrab_edit_state() |
1596 self.ungrab_transition_apply() | 1684 self.ungrab_transition_apply() |
1597 pass | 1685 pass |
1598 | 1686 |
1599 def save_grabs(self): | 1687 def save_grabs(self): |
1600 save = (self._bg_hdl, | 1688 save = (self._state_mouse_event_handler, |
1601 self._grab_mouse_hdl, | 1689 self._transition_mouse_event_handler, |
1602 self._state_mouse_event_handler, | |
1603 self._add_transition_cb, | 1690 self._add_transition_cb, |
1604 self._edit_transition_cb, | 1691 self._edit_transition_cb, |
1605 self._transition_apply_cb, | 1692 self._transition_apply_cb, |
1606 self._edit_state_cb) | 1693 self._edit_state_cb) |
1607 return save | 1694 return save |
1608 | 1695 |
1609 def restore_grabs(self, save): | 1696 def restore_grabs(self, save): |
1610 self._bg_hdl, \ | 1697 self._state_mouse_event_handler, \ |
1611 self._grab_mouse_hdl, \ | 1698 self._transition_mouse_event_handler, \ |
1612 self._state_mouse_event_handler, \ | |
1613 self._add_transition_cb, \ | 1699 self._add_transition_cb, \ |
1614 self._edit_transition_cb, \ | 1700 self._edit_transition_cb, \ |
1615 self._transition_apply_cb, \ | 1701 self._transition_apply_cb, \ |
1616 self._edit_state_cb \ | 1702 self._edit_state_cb \ |
1617 = save | 1703 = save |