cca
changeset 64:6f328d849ba8
+ mouse motion, save listbox focus
author | is_rusinov |
---|---|
date | Sat, 11 Dec 2010 14:02:45 +0300 |
parents | bbe2a8e64826 |
children | 766236a9c924 |
files | Interface.py |
diffstat | 1 files changed, 103 insertions(+), 78 deletions(-) [+] |
line diff
1.1 --- a/Interface.py Sat Dec 11 00:47:07 2010 +0300 1.2 +++ b/Interface.py Sat Dec 11 14:02:45 2010 +0300 1.3 @@ -1,3 +1,5 @@ 1.4 +import math 1.5 +import tkColorChooser 1.6 from Tkinter import * 1.7 1.8 from State import * 1.9 @@ -13,12 +15,11 @@ 1.10 self.offset_x = offset_x 1.11 self.offset_y = offset_y 1.12 self.after_id = 0 1.13 - self.enter1_bound = False 1.14 - self.enter3_bound = False 1.15 self.mouse_offset_x = 0 1.16 self.mouse_offset_y = 0 1.17 self.mouse_zoom = 0 1.18 self.zoom_divisor = 1 1.19 + self.selected_state = None 1.20 self.is_started = False 1.21 self.char = None 1.22 self.keys = dict() 1.23 @@ -113,53 +114,77 @@ 1.24 fill=color, outline="", tag="cell") 1.25 self.cells[row].append(cell) 1.26 1.27 + def draw_line(self, x1, y1, x2, y2, order=1): 1.28 + answer = [] 1.29 + if abs(x1 - x2) > abs(y1 - y2): 1.30 + dx = x2 - x1 1.31 + abs_dx = abs(dx) 1.32 + dy = float(y2 - y1) 1.33 + while x1 != x2: 1.34 + x1 = x1 + dx / abs_dx 1.35 + y1 = y1 + dy / abs_dx 1.36 + answer.append((self.rounding(y1), x1)) 1.37 + else: 1.38 + dx = float(x2 - x1) 1.39 + dy = y2 - y1 1.40 + abs_dy = abs(dy) 1.41 + while y1 != y2: 1.42 + x1 = x1 + dx / abs_dy 1.43 + y1 = y1 + dy / abs_dy 1.44 + answer.append((y1, self.rounding(x1))) 1.45 + self.new_state(answer, order) 1.46 + 1.47 + def rounding(self, num): 1.48 + return int(num + math.copysign(0.5, num)) 1.49 + 1.50 + def new_state(self, cells, order=1): 1.51 + num_states = len(automata.states) 1.52 + changed_cells = [] 1.53 + for row, col in cells: 1.54 + if col >= 0 and row >= 0: 1.55 + try: 1.56 + index = (automata.symbols[automata.field[row][col]] + num_states + order) % num_states 1.57 + if self.char != None and self.keys.has_key(self.char): 1.58 + index = self.keys[self.char] 1.59 + automata.field[row][col] = automata.states[index].symbol 1.60 + changed_cells.append((row, col)) 1.61 + except: 1.62 + pass 1.63 + self.draw_cell(changed_cells) 1.64 + 1.65 def press1(self, event):# drawer 1.66 - if self.enter1_bound: 1.67 - canvas.tag_unbind("cell", "<Enter>") 1.68 - self.enter1_bound = False 1.69 - else: 1.70 - column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) 1.71 - row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) 1.72 - index = (automata.symbols[automata.field[row][column]] + 1) % len(automata.states) 1.73 - if self.char != None and self.keys.has_key(self.char): 1.74 - index = self.keys[self.char] 1.75 - automata.field[row][column] = automata.states[index].symbol 1.76 - self.draw_cell([(row, column)]) 1.77 - 1.78 - def d_press1(self, event):# drawer 1.79 - canvas.tag_bind("cell", "<Enter>", self.enter1) 1.80 - self.enter1_bound = True 1.81 - 1.82 - def enter1(self, event): 1.83 - column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) 1.84 + self.col1 = event.x / (self.cell_size + self.line_width) - self.offset_x 1.85 + self.row1 = event.y / (self.cell_size + self.line_width) - self.offset_y 1.86 + if self.col1 >= 0 and self.row1 >= 0: 1.87 + self.new_state([(self.row1, self.col1)]) 1.88 + 1.89 + def motion1(self, event):# drawer 1.90 + col = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) 1.91 row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) 1.92 - index = (automata.symbols[automata.field[row][column]] + 1) % len(automata.states) 1.93 - if self.char != None and self.keys.has_key(self.char): 1.94 - index = self.keys[self.char] 1.95 - automata.field[row][column] = automata.states[index].symbol 1.96 - self.draw_cell([(row, column)]) 1.97 + if not (self.col1 == col and self.row1 == row): 1.98 + if abs(self.col1 - col) <= 1 and abs(self.row1 - row) <= 1: 1.99 + self.new_state([(row, col)]) 1.100 + else: 1.101 + self.draw_line(self.col1, self.row1, col, row) 1.102 + self.col1 = col 1.103 + self.row1 = row 1.104 1.105 def press3(self, event):# drawer 1.106 - if self.enter3_bound: 1.107 - canvas.tag_unbind("cell", "<Enter>") 1.108 - self.enter3_bound = False 1.109 - else: 1.110 - column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) 1.111 - row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) 1.112 - index = (automata.symbols[automata.field[row][column]] + len(automata.states) - 1) % len(automata.states) 1.113 - automata.field[row][column] = automata.states[index].symbol 1.114 - self.draw_cell([(row, column)]) 1.115 + self.col3 = event.x / (self.cell_size + self.line_width) - self.offset_x 1.116 + self.row3 = event.y / (self.cell_size + self.line_width) - self.offset_y 1.117 + if self.col1 >= 0 and self.row1 >= 0: 1.118 + self.new_state([(self.row3, self.col3)], -1) 1.119 1.120 - def d_press3(self, event):# drawer 1.121 - canvas.tag_bind("cell", "<Enter>", self.enter3) 1.122 - self.enter3_bound = True 1.123 - 1.124 - def enter3(self, event): 1.125 - column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) 1.126 + def motion3(self, event):# drawer 1.127 + col = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) 1.128 row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) 1.129 - index = (automata.symbols[automata.field[row][column]] - 1) % len(automata.states) 1.130 - automata.field[row][column] = automata.states[index].symbol 1.131 - self.draw_cell([(row, column)]) 1.132 + if not (self.col3 == col and self.row3 == row): 1.133 + if abs(self.col3 - col) <= 1 and abs(self.row3 - row) <= 1: 1.134 + self.new_state([(row, col)], -1) 1.135 + else: 1.136 + self.draw_line(self.col3, self.row3, col, row, -1) 1.137 + self.col3 = col 1.138 + self.row3 = row 1.139 1.140 def press_key(self, event):# drawer 1.141 self.char = event.char 1.142 @@ -194,73 +219,68 @@ 1.143 self.draw() 1.144 1.145 def to_top(self):# replace choosen state to top 1.146 - selected = state_list.curselection() 1.147 - if len(selected): 1.148 - index = int(selected[0]) 1.149 + selected = self.selected_state 1.150 + if selected != None: 1.151 + index = selected 1.152 state = automata.states[index] 1.153 print state 1.154 del automata.states[index] 1.155 automata.states.insert(0, state) 1.156 + self.selected_state = 0 1.157 self.refresh_list() 1.158 - state_list.selection_set(0) 1.159 - print automata.states 1.160 1.161 def to_bottom(self):# replace choosen state to botton 1.162 - selected = state_list.curselection() 1.163 - if len(selected): 1.164 - index = int(selected[0]) 1.165 + selected = self.selected_state 1.166 + if selected != None: 1.167 + index = selected 1.168 state = automata.states[index] 1.169 print state 1.170 del automata.states[index] 1.171 automata.states.append(state) 1.172 + self.selected_state = len(automata.states) - 1 1.173 self.refresh_list() 1.174 - state_list.selection_set("end") 1.175 - print automata.states 1.176 1.177 def upwards(self): 1.178 - selected = state_list.curselection() 1.179 - if len(selected): 1.180 - index = int(selected[0]) 1.181 + selected = self.selected_state 1.182 + if selected != None: 1.183 + index = selected 1.184 if index > 0: 1.185 state = automata.states[index] 1.186 print state 1.187 del automata.states[index] 1.188 automata.states.insert(index - 1, state) 1.189 + self.selected_state = index - 1 1.190 self.refresh_list() 1.191 - state_list.selection_set(index - 1) 1.192 - print automata.states 1.193 1.194 def downwards(self): 1.195 - selected = state_list.curselection() 1.196 - if len(selected): 1.197 - index = int(selected[0]) 1.198 + selected = self.selected_state 1.199 + if selected != None: 1.200 + index = selected 1.201 if index < state_list.size() - 1: 1.202 state = automata.states[index] 1.203 print state 1.204 del automata.states[index] 1.205 automata.states.insert(index + 1, state) 1.206 + self.selected_state = index + 1 1.207 self.refresh_list() 1.208 - state_list.selection_set(index + 1) 1.209 - print automata.states 1.210 1.211 def delete_state(self):# delete choosen state 1.212 - selected = state_list.curselection() 1.213 - if len(selected): 1.214 - index = int(selected[0]) 1.215 + selected = self.selected_state 1.216 + if selected != None: 1.217 + index = selected 1.218 print automata.states[index] 1.219 del automata.symbols[automata.states[index].symbol] 1.220 for key in self.keys.keys(): 1.221 if self.keys[key] == index: 1.222 del self.keys[key] 1.223 - del automata.states[index] 1.224 + self.selected_state = index 1.225 self.refresh_list() 1.226 - print automata.states 1.227 1.228 def add(self):# add new state 1.229 name = state_name.get() 1.230 symbol = state_symbol.get() 1.231 key = state_key.get().lower() 1.232 - color = state_color.get() 1.233 + color = state_color.cget("bg") 1.234 nums = [] 1.235 for i, value in enumerate(ckeckbox_nums): 1.236 if value: 1.237 @@ -286,13 +306,13 @@ 1.238 self.refresh_list() 1.239 1.240 def change(self):# change chosen state 1.241 - selected = state_list.curselection() 1.242 - if len(selected): 1.243 - index = int(selected[0]) 1.244 + selected = self.selected_state 1.245 + if selected != None: 1.246 + index = selected 1.247 name = state_name.get() 1.248 symbol = state_symbol.get() 1.249 key = state_key.get().lower() 1.250 - color = state_color.get() 1.251 + color = state_color.cget("bg") 1.252 nums = [] 1.253 for i, value in enumerate(ckeckbox_nums): 1.254 print i, value.get() 1.255 @@ -334,9 +354,11 @@ 1.256 state_list.delete(0, "end") 1.257 for state in automata.states: 1.258 state_list.insert("end", state) 1.259 + if self.selected_state != None: 1.260 + state_list.selection_set(self.selected_state) 1.261 def list_mouse_release(self, event): 1.262 - print 'hello' 1.263 selected = state_list.curselection() 1.264 + self.selected_state = int(selected[0]) 1.265 if len(selected): 1.266 index = int(selected[0]) 1.267 state = automata.states[index] 1.268 @@ -348,10 +370,12 @@ 1.269 if self.keys[key] == index: 1.270 state_key.delete(0, "end") 1.271 state_key.insert(0, key) 1.272 - state_color.delete(0, "end") 1.273 - state_color.insert(0, state.color) 1.274 + state_color.config(bg=state.color) 1.275 for i in range(9): 1.276 ckeckbox_nums[i].set(i in state.nums) 1.277 + 1.278 + def choose_color(self, event): 1.279 + state_color.config(bg=tkColorChooser.askcolor()[1]) 1.280 1.281 1.282 1.283 @@ -365,9 +389,9 @@ 1.284 handlers = Handlers() 1.285 1.286 canvas.tag_bind("cell", "<1>", handlers.press1) 1.287 -canvas.tag_bind("cell", "<Double-Button-1>", handlers.d_press1) 1.288 +canvas.tag_bind("cell", "<B1-Motion>", handlers.motion1) 1.289 canvas.tag_bind("cell", "<3>", handlers.press3) 1.290 -canvas.tag_bind("cell", "<Double-Button-3>", handlers.d_press3) 1.291 +canvas.tag_bind("cell", "<B3-Motion>", handlers.motion3) 1.292 canvas.bind_all("<KeyPress>", handlers.press_key) 1.293 canvas.bind_all("<KeyRelease>", handlers.release_key) 1.294 #canvas.bind("<Control-ButtonPress-1>", handlers.press1_ctrl) 1.295 @@ -432,6 +456,7 @@ 1.296 Label(info_frame, text="Color").grid(row=3, column=0) 1.297 state_color = Label(info_frame, background="white", cursor="plus") 1.298 state_color.grid(row=3, column=1, sticky="ew") 1.299 +state_color.bind('<1>', handlers.choose_color) 1.300 info_frame.pack(side="top") 1.301 1.302