How to use mouse_up method in Airtest

Best Python code snippet using Airtest

gui.py

Source:gui.py Github

copy

Full Screen

1import pygame2import pygame.freetype3import improved_algorithm as improved4import naive_algorithm as naive5from pygame.sprite import Sprite6from enum import Enum7from multiprocessing import Pool89# ================ GUI Constants ================10WHITE = (255, 255, 255)11BLACK = (0, 0, 0)12KEEP_SELECT = False13LAST_NUMBER_SELECT = 214LAST_POP_SELECT = 115CANVAS = 01617# ================ Stat Constants ================18FLOOR_NUM = 019LIFE_STEPS = 020TOTAL_WAIT = 021TOTAL_IN_LIFT = 022POPULATION = 023AVG_WAIT = 024AVG_IN_LIFT = 0252627def create_text_surface(string: str, size, font_col, bg_col):28 """ Creates text of a certain font onto a surface. """29 font = pygame.freetype.SysFont("comicsansms", size)30 surface, _ = font.render(text=string, fgcolor=font_col, bgcolor=bg_col)31 return surface.convert_alpha()323334class ButtonElement(Sprite):35 """ A button sprite that returns a game state enum when clicked. """36 def __init__(self, centre_pos, string: str, size, font_col, bg_col, action=None):37 """ ButtonElement Constructor. A button sprite that can be drawn onto a canvas38 with specific size, colour, position and action."""39 super().__init__()4041 self.mouse_over = False4243 default_button = create_text_surface(string, size, font_col, bg_col)44 highlighted_button = create_text_surface(string, (size * 1.2), font_col, bg_col)45 self.buttons = [default_button, highlighted_button]46 self.rects = [default_button.get_rect(center=centre_pos), highlighted_button.get_rect(center=centre_pos)]4748 self.action = action4950 @property51 def button(self):52 """ Button property, there are two sizes of button. """53 if self.mouse_over:54 return self.buttons[1]55 else:56 return self.buttons[0]5758 @property59 def rect(self):60 """ Rectangle property, there are two sizes of rectangle. """61 if self.mouse_over:62 return self.rects[1]63 else:64 return self.rects[0]6566 def update(self, mouse_pos, mouse_up):67 """68 Checks mouse position and whether button has been clicked.6970 :param tuple mouse_pos: Where the mouse is on the canvas.71 :param bool mouse_up: Whether the mouse has been clicked and72 released.73 :return: The action of the button in the form of a state enum.74 """75 if self.rect.collidepoint(mouse_pos):76 self.mouse_over = True77 if mouse_up:78 return self.action79 else:80 self.mouse_over = False8182 def draw(self, canvas):83 canvas.blit(self.button, self.rect)848586class NumElement(Sprite):87 """ A button sprite that returns a number when clicked. """88 def __init__(self, centre_pos, string: str, size, font_col, bg_col, num: int = None):89 """ NumElement Constructor. A number button sprite that can be drawn onto a canvas90 with specific size, colour, position and numerical value."""91 super().__init__()9293 default_button = create_text_surface(string, size, font_col, bg_col)94 highlighted_button = create_text_surface(string, (size * 1.2), font_col, bg_col)95 self.buttons = [default_button, highlighted_button]96 self.rects = [default_button.get_rect(center=centre_pos), highlighted_button.get_rect(center=centre_pos)]9798 self.number = num99 self.mouse_over = False100101 @property102 def button(self):103 """104 The button properties of the button;105 there are two different size buttons.106 """107 if self.mouse_over:108 return self.buttons[1]109 else:110 return self.buttons[0]111112 @property113 def rect(self):114 """115 The rectangle properties of the button;116 there are two different size rectangles.117 """118 if self.mouse_over:119 return self.rects[1]120 else:121 return self.rects[0]122123 def update(self, mouse_pos, mouse_up):124 """125 Updates the button constantly, checking if126 mouse is over it or has clicked on it.127128 :param tuple mouse_pos: Where the mouse is on129 the canvas.130 :param bool mouse_up: Whether the mouse 1 button131 has been pressed and released.132133 :return: The number assigned to this button.134 :rtype: int135 """136 if self.rect.collidepoint(mouse_pos):137 self.mouse_over = True138 if mouse_up:139 return int(self.number)140 else:141 self.mouse_over = False142143 def draw(self, canvas):144 """ Blits the button and rectangle onto canvas. """145 canvas.blit(self.button, self.rect)146147148class LabelElement(Sprite):149 """ A Label sprite. """150 def __init__(self, centre_pos, string, size, font_col, bg_col):151 """ LabelElement Constructor. A label sprite that can be drawn onto a canvas152 with specific size, colour and position.153 """154 super().__init__()155156 default_label = create_text_surface(string, size, font_col, bg_col)157 default_rect = default_label.get_rect(center=centre_pos)158159 self.def_label = default_label160 self.def_rect = default_rect161162 @property163 def label(self):164 """165 The main label of this class.166 :return: The label167 """168 return self.def_label169170 @property171 def rect(self):172 """173 The main rectangle for drawing.174 :return: The rectangle175 """176 return self.def_rect177178 def draw(self, canvas):179 """180 Blits the label and rectangle.181 :param Canvas canvas: Canvas onto which label182 and rect are blitted.183 """184 canvas.blit(self.label, self.rect)185186187class GameState(Enum):188 """189 A class of enums that denote the190 states of the gui.191 """192 QUIT = -1193 MENU = 0194 FLOOR_ENTER = 1195 PEOPLE_ENTER = 2196 IMPROVED = 3197 NAIVE = 4198 ALGORITHM_ENTER = 5199 STATS = 6200201202def menu_screen(canvas):203 """204 The main menu for the system. Redirects to first page of205 selections or quits system. Quitting can only be done from206 here.207208 :param Canvas canvas: Canvas onto which drawing is done.209 :return: Gamestate enums.210 """211 start_button = ButtonElement(212 centre_pos=(400, 400),213 size=30,214 bg_col=WHITE,215 font_col=BLACK,216 string="Start Simulation",217 action=GameState.FLOOR_ENTER218 )219220 quit_button = ButtonElement(221 centre_pos=(400, 550),222 size=30,223 bg_col=WHITE,224 font_col=BLACK,225 string="Quit",226 action=GameState.QUIT227 )228229 title_label = LabelElement(230 centre_pos=(400, 200),231 size=40,232 bg_col=WHITE,233 font_col=BLACK,234 string="Lift Simulation System"235 )236237 author_label = LabelElement(238 centre_pos=(400, 250),239 size=25,240 bg_col=WHITE,241 font_col=BLACK,242 string="By 690037391"243 )244245 buttons = [start_button, quit_button]246247 while True:248 mouse_up = False249 for event in pygame.event.get():250 if event.type == pygame.MOUSEBUTTONUP and event.button == 1:251 mouse_up = True252253 canvas.fill(WHITE)254255 for button in buttons:256 action = button.update(pygame.mouse.get_pos(), mouse_up)257 if action is not None:258 return action259260 button.draw(canvas)261262 title_label.draw(canvas)263 author_label.draw(canvas)264265 pygame.display.flip()266267268def floor_select(canvas):269 """270 Main screen for selecting number of floors.271272 :param Canvas canvas: Canvas onto which floor number is drawn.273 :return: Game state enums274 """275 global KEEP_SELECT276 global LAST_NUMBER_SELECT277 global LAST_POP_SELECT278 return_button = ButtonElement(279 centre_pos=(140, 570),280 size=20,281 bg_col=WHITE,282 font_col=BLACK,283 string="Return to Menu",284 action=GameState.MENU285 )286287 people_button = ButtonElement(288 centre_pos=(660, 570),289 size=20,290 bg_col=WHITE,291 font_col=BLACK,292 string="Choose Population",293 action=GameState.PEOPLE_ENTER294 )295296 select_label = LabelElement(297 centre_pos=(400, 100),298 size=30,299 bg_col=WHITE,300 font_col=BLACK,301 string="Select the number of floors."302 )303304 one_button = NumElement(305 centre_pos=(460, 400),306 size=30,307 bg_col=WHITE,308 font_col=BLACK,309 string="+ 1",310 num=1311 )312313 five_button = NumElement(314 centre_pos=(520, 400),315 size=30,316 bg_col=WHITE,317 font_col=BLACK,318 string="+ 5",319 num=5320 )321322 ten_button = NumElement(323 centre_pos=(580, 400),324 size=30,325 bg_col=WHITE,326 font_col=BLACK,327 string="+ 10",328 num=10329 )330331 minus_one_button = NumElement(332 centre_pos=(340, 400),333 size=30,334 bg_col=WHITE,335 font_col=BLACK,336 string="- 1",337 num=1338 )339340 minus_five_button = NumElement(341 centre_pos=(280, 400),342 size=30,343 bg_col=WHITE,344 font_col=BLACK,345 string="- 5",346 num=5347 )348349 minus_ten_button = NumElement(350 centre_pos=(220, 400),351 size=30,352 bg_col=WHITE,353 font_col=BLACK,354 string="- 10",355 num=10356 )357358 if KEEP_SELECT:359 number_select = LAST_NUMBER_SELECT360 else:361 number_select = 2362363 while True:364 mouse_up = False365366 for event in pygame.event.get():367 if event.type == pygame.MOUSEBUTTONUP and event.button == 1:368 mouse_up = True369370 canvas.fill(WHITE)371372 plus_one = one_button.update(pygame.mouse.get_pos(), mouse_up)373 plus_five = five_button.update(pygame.mouse.get_pos(), mouse_up)374 plus_ten = ten_button.update(pygame.mouse.get_pos(), mouse_up)375376 minus_one = minus_one_button.update(pygame.mouse.get_pos(), mouse_up)377 minus_five = minus_five_button.update(pygame.mouse.get_pos(), mouse_up)378 minus_ten = minus_ten_button.update(pygame.mouse.get_pos(), mouse_up)379380 if plus_one is not None:381 if number_select < 1000:382 number_select += plus_one383384 if minus_one is not None:385 if number_select > 2:386 if number_select - minus_one < 2:387 number_select = 2388 else:389 number_select -= minus_one390391 if plus_five is not None:392 if number_select < 1000:393 number_select += plus_five394395 if minus_five is not None:396 if number_select > 2:397 if number_select - minus_five < 2:398 number_select = 2399 else:400 number_select -= minus_five401402 if plus_ten is not None:403 if number_select < 1000:404 number_select += plus_ten405406 if minus_ten is not None:407 if number_select > 2:408 if number_select - minus_ten < 2:409 number_select = 2410 else:411 number_select -= minus_ten412413 return_action = return_button.update(pygame.mouse.get_pos(), mouse_up)414 if return_action is not None:415 KEEP_SELECT = False416 LAST_POP_SELECT = 1417 return return_action418419 population = people_button.update(pygame.mouse.get_pos(), mouse_up)420 if population is not None:421 LAST_NUMBER_SELECT = number_select422 KEEP_SELECT = False423 return population424425 return_button.draw(canvas)426 people_button.draw(canvas)427428 one_button.draw(canvas)429 five_button.draw(canvas)430 ten_button.draw(canvas)431 minus_one_button.draw(canvas)432 minus_five_button.draw(canvas)433 minus_ten_button.draw(canvas)434435 select_label.draw(canvas)436437 number_select_label = LabelElement(438 centre_pos=(400, 300),439 size=40,440 bg_col=WHITE,441 font_col=BLACK,442 string=str(number_select),443 )444445 number_select_label.draw(canvas)446447 pygame.display.flip()448449450def people_select(canvas):451 """452 The main screen for selecting the453 population of the building.454455 :param Canvas canvas: Canvas onto which drawing is done.456 :return: Game state enums457 """458 global KEEP_SELECT459 global LAST_POP_SELECT460 global CANVAS461 return_button = ButtonElement(462 centre_pos=(140, 570),463 size=20,464 bg_col=WHITE,465 font_col=BLACK,466 string="Return to Menu",467 action=GameState.MENU468 )469470 back_button = ButtonElement(471 centre_pos=(400, 570),472 size=20,473 bg_col=WHITE,474 font_col=BLACK,475 string="Go Back",476 action=GameState.FLOOR_ENTER477 )478479 algorithm_button = ButtonElement(480 centre_pos=(660, 570),481 size=20,482 bg_col=WHITE,483 font_col=BLACK,484 string="Choose an algorithm",485 action=GameState.ALGORITHM_ENTER486 )487488 select_label = LabelElement(489 centre_pos=(400, 100),490 size=30,491 bg_col=WHITE,492 font_col=BLACK,493 string="Select the number of people."494 )495496 one_button = NumElement(497 centre_pos=(460, 400),498 size=30,499 bg_col=WHITE,500 font_col=BLACK,501 string="+ 1",502 num=1503 )504505 five_button = NumElement(506 centre_pos=(520, 400),507 size=30,508 bg_col=WHITE,509 font_col=BLACK,510 string="+ 5",511 num=5512 )513514 ten_button = NumElement(515 centre_pos=(580, 400),516 size=30,517 bg_col=WHITE,518 font_col=BLACK,519 string="+ 10",520 num=10521 )522523 minus_one_button = NumElement(524 centre_pos=(340, 400),525 size=30,526 bg_col=WHITE,527 font_col=BLACK,528 string="- 1",529 num=1530 )531532 minus_five_button = NumElement(533 centre_pos=(280, 400),534 size=30,535 bg_col=WHITE,536 font_col=BLACK,537 string="- 5",538 num=5539 )540541 minus_ten_button = NumElement(542 centre_pos=(220, 400),543 size=30,544 bg_col=WHITE,545 font_col=BLACK,546 string="- 10",547 num=10548 )549550 pop_select = LAST_POP_SELECT551552 while True:553 mouse_up = False554 for event in pygame.event.get():555 if event.type == pygame.MOUSEBUTTONUP and event.button == 1:556 mouse_up = True557558 canvas.fill(WHITE)559560 plus_one = one_button.update(pygame.mouse.get_pos(), mouse_up)561 plus_five = five_button.update(pygame.mouse.get_pos(), mouse_up)562 plus_ten = ten_button.update(pygame.mouse.get_pos(), mouse_up)563564 minus_one = minus_one_button.update(pygame.mouse.get_pos(), mouse_up)565 minus_five = minus_five_button.update(pygame.mouse.get_pos(), mouse_up)566 minus_ten = minus_ten_button.update(pygame.mouse.get_pos(), mouse_up)567568 if plus_one is not None:569 if pop_select < 1000:570 pop_select += plus_one571572 if minus_one is not None:573 if pop_select > 1:574 if pop_select - minus_one < 1:575 pop_select = 1576 else:577 pop_select -= minus_one578579 if plus_five is not None:580 if pop_select < 1000:581 pop_select += plus_five582583 if minus_five is not None:584 if pop_select > 1:585 if pop_select - minus_five < 1:586 pop_select = 1587 else:588 pop_select -= minus_five589590 if plus_ten is not None:591 if pop_select < 1000:592 pop_select += plus_ten593594 if minus_ten is not None:595 if pop_select > 1:596 if pop_select - minus_ten < 1:597 pop_select = 1598 else:599 pop_select -= minus_ten600601 action = return_button.update(pygame.mouse.get_pos(), mouse_up)602 if action is not None:603 KEEP_SELECT = False604 return action605606 algorithm = algorithm_button.update(pygame.mouse.get_pos(), mouse_up)607 if algorithm is not None:608 LAST_POP_SELECT = pop_select609 KEEP_SELECT = False610 CANVAS = canvas611 return algorithm612613 action = return_button.update(pygame.mouse.get_pos(), mouse_up)614 if action is not None:615 return action616617 back = back_button.update(pygame.mouse.get_pos(), mouse_up)618 if back is not None:619 KEEP_SELECT = True620 LAST_POP_SELECT = pop_select621 return back622623 one_button.draw(canvas)624 five_button.draw(canvas)625 ten_button.draw(canvas)626 minus_one_button.draw(canvas)627 minus_five_button.draw(canvas)628 minus_ten_button.draw(canvas)629630 return_button.draw(canvas)631 back_button.draw(canvas)632 select_label.draw(canvas)633 algorithm_button.draw(canvas)634635 pop_select_label = LabelElement(636 centre_pos=(400, 300),637 size=40,638 bg_col=WHITE,639 font_col=BLACK,640 string=str(pop_select),641 )642643 pop_select_label.draw(canvas)644645 pygame.display.flip()646647648def run_improved():649 """650 Runs the improved algorithm in a separate process pool,651 and returns any variables in a 3D array.652653 :return: An enum for the Stats screen state.654 :rtype: enum655 """656 global LAST_POP_SELECT657 global LAST_NUMBER_SELECT658 global KEEP_SELECT659 global FLOOR_NUM660 global LIFE_STEPS661 global TOTAL_WAIT662 global TOTAL_IN_LIFT663 global POPULATION664 global AVG_WAIT665 global AVG_IN_LIFT666667 people_list = improved.generate_people(LAST_NUMBER_SELECT, LAST_POP_SELECT)668 lift_job = Pool(processes=1)669670 result = lift_job.starmap(671 improved.better_lift_algorithm, [(LAST_NUMBER_SELECT, LAST_POP_SELECT, people_list)]672 )673 lift_job.close()674675 FLOOR_NUM = result[0][0]676 LIFE_STEPS = result[0][1]677 TOTAL_WAIT = result[0][2]678 TOTAL_IN_LIFT = result[0][3]679 POPULATION = result[0][4]680 AVG_WAIT = result[0][5]681 AVG_IN_LIFT = result[0][6]682683 LAST_NUMBER_SELECT = 2684 LAST_POP_SELECT = 1685 KEEP_SELECT = False686 return GameState.STATS687688689def run_naive():690 """691 Runs the Naïve algorithm in a separate process pool,692 and returns any variables in a 3D array.693694 :return: An enum for the Stats screen state.695 :rtype: enum696 """697 global LAST_POP_SELECT698 global LAST_NUMBER_SELECT699 global KEEP_SELECT700 global FLOOR_NUM701 global LIFE_STEPS702 global TOTAL_WAIT703 global TOTAL_IN_LIFT704 global POPULATION705 global AVG_WAIT706 global AVG_IN_LIFT707708 people_list = improved.generate_people(LAST_NUMBER_SELECT, LAST_POP_SELECT)709 lift_job = Pool(processes=1)710711 result = lift_job.starmap(712 naive.naive_lift_algorithm, [(LAST_NUMBER_SELECT, people_list, True)]713 )714 lift_job.close()715716 FLOOR_NUM = result[0][0]717 LIFE_STEPS = result[0][1]718 TOTAL_WAIT = result[0][2]719 TOTAL_IN_LIFT = result[0][3]720 POPULATION = result[0][4]721 AVG_WAIT = result[0][5]722 AVG_IN_LIFT = result[0][6]723724 LAST_NUMBER_SELECT = 2725 LAST_POP_SELECT = 1726 KEEP_SELECT = False727 return GameState.STATS728729730def show_stats(canvas):731 """732 Creates the stats display screen.733734 :param Canvas canvas: Canvas onto which stats are drawn.735 :return: A state enum736 :rtype: Enum737 """738 global LAST_POP_SELECT739 global LAST_NUMBER_SELECT740 global KEEP_SELECT741 global FLOOR_NUM742 global LIFE_STEPS743 global TOTAL_WAIT744 global TOTAL_IN_LIFT745 global POPULATION746 global AVG_WAIT747 global AVG_IN_LIFT748749 canvas.fill(WHITE)750751 return_button = ButtonElement(752 centre_pos=(140, 570),753 size=20,754 bg_col=WHITE,755 font_col=BLACK,756 string="Return to Menu",757 action=GameState.MENU758 )759760 title_label = LabelElement(761 centre_pos=(400, 100),762 size=40,763 bg_col=WHITE,764 font_col=BLACK,765 string="Statistics"766 )767768 floor_num_label = LabelElement(769 centre_pos=(300, 200),770 size=30,771 bg_col=WHITE,772 font_col=BLACK,773 string="Number of Floors"774 )775 floor_num_result = LabelElement(776 centre_pos=(600, 200),777 size=30,778 bg_col=WHITE,779 font_col=BLACK,780 string=str(FLOOR_NUM)781 )782783 life_steps_label = LabelElement(784 centre_pos=(300, 250),785 size=30,786 bg_col=WHITE,787 font_col=BLACK,788 string="Lifetime steps"789 )790 life_steps_result = LabelElement(791 centre_pos=(600, 250),792 size=30,793 bg_col=WHITE,794 font_col=BLACK,795 string=str(LIFE_STEPS)796 )797798 total_wait_label = LabelElement(799 centre_pos=(300, 300),800 size=30,801 bg_col=WHITE,802 font_col=BLACK,803 string="Total time waiting for lift"804 )805 total_wait_result = LabelElement(806 centre_pos=(600, 300),807 size=30,808 bg_col=WHITE,809 font_col=BLACK,810 string=str(TOTAL_WAIT)811 )812813 total_in_lift_label = LabelElement(814 centre_pos=(300, 350),815 size=30,816 bg_col=WHITE,817 font_col=BLACK,818 string="Total time spent in lift"819 )820 total_in_lift_result = LabelElement(821 centre_pos=(600, 350),822 size=30,823 bg_col=WHITE,824 font_col=BLACK,825 string=str(TOTAL_IN_LIFT)826 )827828 population_label = LabelElement(829 centre_pos=(300, 400),830 size=30,831 bg_col=WHITE,832 font_col=BLACK,833 string="Population"834 )835 population_result = LabelElement(836 centre_pos=(600, 400),837 size=30,838 bg_col=WHITE,839 font_col=BLACK,840 string=str(POPULATION)841 )842843 average_wait_label = LabelElement(844 centre_pos=(300, 450),845 size=30,846 bg_col=WHITE,847 font_col=BLACK,848 string="Average Waiting time"849 )850 average_wait_result = LabelElement(851 centre_pos=(600, 450),852 size=30,853 bg_col=WHITE,854 font_col=BLACK,855 string=str(AVG_WAIT)856 )857858 average_in_lift_label = LabelElement(859 centre_pos=(300, 500),860 size=30,861 bg_col=WHITE,862 font_col=BLACK,863 string="Average Time spent in Lift"864 )865 average_in_lift_result = LabelElement(866 centre_pos=(600, 500),867 size=30,868 bg_col=WHITE,869 font_col=BLACK,870 string=str(AVG_IN_LIFT)871 )872873 while True:874 mouse_up = False875 for event in pygame.event.get():876 if event.type == pygame.MOUSEBUTTONUP and event.button == 1:877 mouse_up = True878879 canvas.fill(WHITE)880881 return_action = return_button.update(pygame.mouse.get_pos(), mouse_up)882 if return_action is not None:883 LAST_POP_SELECT = 1884 LAST_NUMBER_SELECT = 2885 KEEP_SELECT = False886 FLOOR_NUM = 0887 LIFE_STEPS = 0888 TOTAL_WAIT = 0889 TOTAL_IN_LIFT = 0890 POPULATION = 0891 AVG_WAIT = 0892 AVG_IN_LIFT = 0893894 return return_action895896 return_button.draw(canvas)897 title_label.draw(canvas)898899 life_steps_label.draw(canvas)900 life_steps_result.draw(canvas)901902 total_in_lift_label.draw(canvas)903 total_in_lift_result.draw(canvas)904905 total_wait_label.draw(canvas)906 total_wait_result.draw(canvas)907908 population_label.draw(canvas)909 population_result.draw(canvas)910911 floor_num_label.draw(canvas)912 floor_num_result.draw(canvas)913914 average_in_lift_label.draw(canvas)915 average_in_lift_result.draw(canvas)916917 average_wait_label.draw(canvas)918 average_wait_result.draw(canvas)919920 pygame.display.flip()921922923def algorithm_select(canvas):924 """925 Is the main algorithm selection screen.926927 :param Canvas canvas: The pygame canvas onto which drawing is done.928 :return: An Enum corresponding to the state to move to.929 :rtype: Enum930 """931 global KEEP_SELECT932 global LAST_POP_SELECT933 global LAST_NUMBER_SELECT934935 return_button = ButtonElement(936 centre_pos=(140, 570),937 size=20,938 bg_col=WHITE,939 font_col=BLACK,940 string="Return to Menu",941 action=GameState.MENU942 )943944 back_button = ButtonElement(945 centre_pos=(400, 570),946 size=20,947 bg_col=WHITE,948 font_col=BLACK,949 string="Go Back",950 action=GameState.PEOPLE_ENTER951 )952953 improved_button = ButtonElement(954 centre_pos=(500, 300),955 size=20,956 bg_col=WHITE,957 font_col=BLACK,958 string="Improved",959 action=GameState.IMPROVED960 )961962 naive_button = ButtonElement(963 centre_pos=(300, 300),964 size=20,965 bg_col=WHITE,966 font_col=BLACK,967 string="Naive",968 action=GameState.NAIVE969 )970971 title_label = LabelElement(972 centre_pos=(400, 200),973 size=30,974 bg_col=WHITE,975 font_col=BLACK,976 string="Choose an algorithm"977 )978979 while True:980 mouse_up = False981 for event in pygame.event.get():982 if event.type == pygame.MOUSEBUTTONUP and event.button == 1:983 mouse_up = True984985 canvas.fill(WHITE)986987 return_action = return_button.update(pygame.mouse.get_pos(), mouse_up)988 if return_action is not None:989 return return_action990991 back = back_button.update(pygame.mouse.get_pos(), mouse_up)992 if back is not None:993 KEEP_SELECT = True994 return back995996 improved_action = improved_button.update(pygame.mouse.get_pos(), mouse_up)997 if improved_action is not None:998 return improved_action9991000 naive_action = naive_button.update(pygame.mouse.get_pos(), mouse_up)1001 if naive_action is not None:1002 return naive_action10031004 return_button.draw(canvas)1005 back_button.draw(canvas)1006 improved_button.draw(canvas)1007 naive_button.draw(canvas)1008 title_label.draw(canvas)10091010 pygame.display.flip()101110121013def main():1014 """1015 Controls which state the GUI is in and what to1016 do when changing states.1017 """1018 pygame.init()1019 is_done = False10201021 canvas = pygame.display.set_mode((800, 600))10221023 game_state = GameState.MENU10241025 while not is_done:1026 if game_state == GameState.MENU:1027 game_state = menu_screen(canvas)10281029 if game_state == GameState.ALGORITHM_ENTER:1030 game_state = algorithm_select(canvas)10311032 if game_state == GameState.FLOOR_ENTER:1033 game_state = floor_select(canvas)10341035 if game_state == GameState.PEOPLE_ENTER:1036 game_state = people_select(canvas)10371038 if game_state == GameState.IMPROVED:1039 game_state = run_improved()10401041 if game_state == GameState.NAIVE:1042 game_state = run_naive()10431044 if game_state == GameState.STATS:1045 canvas = pygame.display.set_mode((800, 600))1046 game_state = show_stats(canvas)10471048 if game_state == GameState.QUIT:1049 pygame.quit()1050 is_done = True105110521053if __name__ == '__main__':1054 main() ...

Full Screen

Full Screen

mouse.py

Source:mouse.py Github

copy

Full Screen

1from typing import TYPE_CHECKING, FrozenSet2from prompt_toolkit.data_structures import Point3from prompt_toolkit.key_binding.key_processor import KeyPress, KeyPressEvent4from prompt_toolkit.keys import Keys5from prompt_toolkit.mouse_events import (6 MouseButton,7 MouseEvent,8 MouseEventType,9 MouseModifier,10)11from prompt_toolkit.utils import is_windows12from ..key_bindings import KeyBindings13if TYPE_CHECKING:14 from prompt_toolkit.key_binding.key_bindings import NotImplementedOrNone15__all__ = [16 "load_mouse_bindings",17]18E = KeyPressEvent19# fmt: off20# flake8: noqa E20121SCROLL_UP = MouseEventType.SCROLL_UP22SCROLL_DOWN = MouseEventType.SCROLL_DOWN23MOUSE_DOWN = MouseEventType.MOUSE_DOWN24MOUSE_MOVE = MouseEventType.MOUSE_MOVE25MOUSE_UP = MouseEventType.MOUSE_UP26NO_MODIFIER : FrozenSet[MouseModifier] = frozenset()27SHIFT : FrozenSet[MouseModifier] = frozenset({MouseModifier.SHIFT})28ALT : FrozenSet[MouseModifier] = frozenset({MouseModifier.ALT})29SHIFT_ALT : FrozenSet[MouseModifier] = frozenset({MouseModifier.SHIFT, MouseModifier.ALT})30CONTROL : FrozenSet[MouseModifier] = frozenset({MouseModifier.CONTROL})31SHIFT_CONTROL : FrozenSet[MouseModifier] = frozenset({MouseModifier.SHIFT, MouseModifier.CONTROL})32ALT_CONTROL : FrozenSet[MouseModifier] = frozenset({MouseModifier.ALT, MouseModifier.CONTROL})33SHIFT_ALT_CONTROL: FrozenSet[MouseModifier] = frozenset({MouseModifier.SHIFT, MouseModifier.ALT, MouseModifier.CONTROL})34UNKNOWN_MODIFIER : FrozenSet[MouseModifier] = frozenset()35LEFT = MouseButton.LEFT36MIDDLE = MouseButton.MIDDLE37RIGHT = MouseButton.RIGHT38NO_BUTTON = MouseButton.NONE39UNKNOWN_BUTTON = MouseButton.UNKNOWN40xterm_sgr_mouse_events = {41 ( 0, 'm') : (LEFT, MOUSE_UP, NO_MODIFIER), # left_up 0+ + + =042 ( 4, 'm') : (LEFT, MOUSE_UP, SHIFT), # left_up Shift 0+4+ + =443 ( 8, 'm') : (LEFT, MOUSE_UP, ALT), # left_up Alt 0+ +8+ =844 (12, 'm') : (LEFT, MOUSE_UP, SHIFT_ALT), # left_up Shift Alt 0+4+8+ =1245 (16, 'm') : (LEFT, MOUSE_UP, CONTROL), # left_up Control 0+ + +16=1646 (20, 'm') : (LEFT, MOUSE_UP, SHIFT_CONTROL), # left_up Shift Control 0+4+ +16=2047 (24, 'm') : (LEFT, MOUSE_UP, ALT_CONTROL), # left_up Alt Control 0+ +8+16=2448 (28, 'm') : (LEFT, MOUSE_UP, SHIFT_ALT_CONTROL), # left_up Shift Alt Control 0+4+8+16=2849 ( 1, 'm') : (MIDDLE, MOUSE_UP, NO_MODIFIER), # middle_up 1+ + + =150 ( 5, 'm') : (MIDDLE, MOUSE_UP, SHIFT), # middle_up Shift 1+4+ + =551 ( 9, 'm') : (MIDDLE, MOUSE_UP, ALT), # middle_up Alt 1+ +8+ =952 (13, 'm') : (MIDDLE, MOUSE_UP, SHIFT_ALT), # middle_up Shift Alt 1+4+8+ =1353 (17, 'm') : (MIDDLE, MOUSE_UP, CONTROL), # middle_up Control 1+ + +16=1754 (21, 'm') : (MIDDLE, MOUSE_UP, SHIFT_CONTROL), # middle_up Shift Control 1+4+ +16=2155 (25, 'm') : (MIDDLE, MOUSE_UP, ALT_CONTROL), # middle_up Alt Control 1+ +8+16=2556 (29, 'm') : (MIDDLE, MOUSE_UP, SHIFT_ALT_CONTROL), # middle_up Shift Alt Control 1+4+8+16=2957 ( 2, 'm') : (RIGHT, MOUSE_UP, NO_MODIFIER), # right_up 2+ + + =258 ( 6, 'm') : (RIGHT, MOUSE_UP, SHIFT), # right_up Shift 2+4+ + =659 (10, 'm') : (RIGHT, MOUSE_UP, ALT), # right_up Alt 2+ +8+ =1060 (14, 'm') : (RIGHT, MOUSE_UP, SHIFT_ALT), # right_up Shift Alt 2+4+8+ =1461 (18, 'm') : (RIGHT, MOUSE_UP, CONTROL), # right_up Control 2+ + +16=1862 (22, 'm') : (RIGHT, MOUSE_UP, SHIFT_CONTROL), # right_up Shift Control 2+4+ +16=2263 (26, 'm') : (RIGHT, MOUSE_UP, ALT_CONTROL), # right_up Alt Control 2+ +8+16=2664 (30, 'm') : (RIGHT, MOUSE_UP, SHIFT_ALT_CONTROL), # right_up Shift Alt Control 2+4+8+16=3065 ( 0, 'M') : (LEFT, MOUSE_DOWN, NO_MODIFIER), # left_down 0+ + + =066 ( 4, 'M') : (LEFT, MOUSE_DOWN, SHIFT), # left_down Shift 0+4+ + =467 ( 8, 'M') : (LEFT, MOUSE_DOWN, ALT), # left_down Alt 0+ +8+ =868 (12, 'M') : (LEFT, MOUSE_DOWN, SHIFT_ALT), # left_down Shift Alt 0+4+8+ =1269 (16, 'M') : (LEFT, MOUSE_DOWN, CONTROL), # left_down Control 0+ + +16=1670 (20, 'M') : (LEFT, MOUSE_DOWN, SHIFT_CONTROL), # left_down Shift Control 0+4+ +16=2071 (24, 'M') : (LEFT, MOUSE_DOWN, ALT_CONTROL), # left_down Alt Control 0+ +8+16=2472 (28, 'M') : (LEFT, MOUSE_DOWN, SHIFT_ALT_CONTROL), # left_down Shift Alt Control 0+4+8+16=2873 ( 1, 'M') : (MIDDLE, MOUSE_DOWN, NO_MODIFIER), # middle_down 1+ + + =174 ( 5, 'M') : (MIDDLE, MOUSE_DOWN, SHIFT), # middle_down Shift 1+4+ + =575 ( 9, 'M') : (MIDDLE, MOUSE_DOWN, ALT), # middle_down Alt 1+ +8+ =976 (13, 'M') : (MIDDLE, MOUSE_DOWN, SHIFT_ALT), # middle_down Shift Alt 1+4+8+ =1377 (17, 'M') : (MIDDLE, MOUSE_DOWN, CONTROL), # middle_down Control 1+ + +16=1778 (21, 'M') : (MIDDLE, MOUSE_DOWN, SHIFT_CONTROL), # middle_down Shift Control 1+4+ +16=2179 (25, 'M') : (MIDDLE, MOUSE_DOWN, ALT_CONTROL), # middle_down Alt Control 1+ +8+16=2580 (29, 'M') : (MIDDLE, MOUSE_DOWN, SHIFT_ALT_CONTROL), # middle_down Shift Alt Control 1+4+8+16=2981 ( 2, 'M') : (RIGHT, MOUSE_DOWN, NO_MODIFIER), # right_down 2+ + + =282 ( 6, 'M') : (RIGHT, MOUSE_DOWN, SHIFT), # right_down Shift 2+4+ + =683 (10, 'M') : (RIGHT, MOUSE_DOWN, ALT), # right_down Alt 2+ +8+ =1084 (14, 'M') : (RIGHT, MOUSE_DOWN, SHIFT_ALT), # right_down Shift Alt 2+4+8+ =1485 (18, 'M') : (RIGHT, MOUSE_DOWN, CONTROL), # right_down Control 2+ + +16=1886 (22, 'M') : (RIGHT, MOUSE_DOWN, SHIFT_CONTROL), # right_down Shift Control 2+4+ +16=2287 (26, 'M') : (RIGHT, MOUSE_DOWN, ALT_CONTROL), # right_down Alt Control 2+ +8+16=2688 (30, 'M') : (RIGHT, MOUSE_DOWN, SHIFT_ALT_CONTROL), # right_down Shift Alt Control 2+4+8+16=3089 (32, 'M') : (LEFT, MOUSE_MOVE, NO_MODIFIER), # left_drag 32+ + + =3290 (36, 'M') : (LEFT, MOUSE_MOVE, SHIFT), # left_drag Shift 32+4+ + =3691 (40, 'M') : (LEFT, MOUSE_MOVE, ALT), # left_drag Alt 32+ +8+ =4092 (44, 'M') : (LEFT, MOUSE_MOVE, SHIFT_ALT), # left_drag Shift Alt 32+4+8+ =4493 (48, 'M') : (LEFT, MOUSE_MOVE, CONTROL), # left_drag Control 32+ + +16=4894 (52, 'M') : (LEFT, MOUSE_MOVE, SHIFT_CONTROL), # left_drag Shift Control 32+4+ +16=5295 (56, 'M') : (LEFT, MOUSE_MOVE, ALT_CONTROL), # left_drag Alt Control 32+ +8+16=5696 (60, 'M') : (LEFT, MOUSE_MOVE, SHIFT_ALT_CONTROL), # left_drag Shift Alt Control 32+4+8+16=6097 (33, 'M') : (MIDDLE, MOUSE_MOVE, NO_MODIFIER), # middle_drag 33+ + + =3398 (37, 'M') : (MIDDLE, MOUSE_MOVE, SHIFT), # middle_drag Shift 33+4+ + =3799 (41, 'M') : (MIDDLE, MOUSE_MOVE, ALT), # middle_drag Alt 33+ +8+ =41100 (45, 'M') : (MIDDLE, MOUSE_MOVE, SHIFT_ALT), # middle_drag Shift Alt 33+4+8+ =45101 (49, 'M') : (MIDDLE, MOUSE_MOVE, CONTROL), # middle_drag Control 33+ + +16=49102 (53, 'M') : (MIDDLE, MOUSE_MOVE, SHIFT_CONTROL), # middle_drag Shift Control 33+4+ +16=53103 (57, 'M') : (MIDDLE, MOUSE_MOVE, ALT_CONTROL), # middle_drag Alt Control 33+ +8+16=57104 (61, 'M') : (MIDDLE, MOUSE_MOVE, SHIFT_ALT_CONTROL), # middle_drag Shift Alt Control 33+4+8+16=61105 (34, 'M') : (RIGHT, MOUSE_MOVE, NO_MODIFIER), # right_drag 34+ + + =34106 (38, 'M') : (RIGHT, MOUSE_MOVE, SHIFT), # right_drag Shift 34+4+ + =38107 (42, 'M') : (RIGHT, MOUSE_MOVE, ALT), # right_drag Alt 34+ +8+ =42108 (46, 'M') : (RIGHT, MOUSE_MOVE, SHIFT_ALT), # right_drag Shift Alt 34+4+8+ =46109 (50, 'M') : (RIGHT, MOUSE_MOVE, CONTROL), # right_drag Control 34+ + +16=50110 (54, 'M') : (RIGHT, MOUSE_MOVE, SHIFT_CONTROL), # right_drag Shift Control 34+4+ +16=54111 (58, 'M') : (RIGHT, MOUSE_MOVE, ALT_CONTROL), # right_drag Alt Control 34+ +8+16=58112 (62, 'M') : (RIGHT, MOUSE_MOVE, SHIFT_ALT_CONTROL), # right_drag Shift Alt Control 34+4+8+16=62113 (35, 'M') : (NO_BUTTON, MOUSE_MOVE, NO_MODIFIER), # none_drag 35+ + + =35114 (39, 'M') : (NO_BUTTON, MOUSE_MOVE, SHIFT), # none_drag Shift 35+4+ + =39115 (43, 'M') : (NO_BUTTON, MOUSE_MOVE, ALT), # none_drag Alt 35+ +8+ =43116 (47, 'M') : (NO_BUTTON, MOUSE_MOVE, SHIFT_ALT), # none_drag Shift Alt 35+4+8+ =47117 (51, 'M') : (NO_BUTTON, MOUSE_MOVE, CONTROL), # none_drag Control 35+ + +16=51118 (55, 'M') : (NO_BUTTON, MOUSE_MOVE, SHIFT_CONTROL), # none_drag Shift Control 35+4+ +16=55119 (59, 'M') : (NO_BUTTON, MOUSE_MOVE, ALT_CONTROL), # none_drag Alt Control 35+ +8+16=59120 (63, 'M') : (NO_BUTTON, MOUSE_MOVE, SHIFT_ALT_CONTROL), # none_drag Shift Alt Control 35+4+8+16=63121 (64, 'M') : (NO_BUTTON, SCROLL_UP, NO_MODIFIER), # scroll_up 64+ + + =64122 (68, 'M') : (NO_BUTTON, SCROLL_UP, SHIFT), # scroll_up Shift 64+4+ + =68123 (72, 'M') : (NO_BUTTON, SCROLL_UP, ALT), # scroll_up Alt 64+ +8+ =72124 (76, 'M') : (NO_BUTTON, SCROLL_UP, SHIFT_ALT), # scroll_up Shift Alt 64+4+8+ =76125 (80, 'M') : (NO_BUTTON, SCROLL_UP, CONTROL), # scroll_up Control 64+ + +16=80126 (84, 'M') : (NO_BUTTON, SCROLL_UP, SHIFT_CONTROL), # scroll_up Shift Control 64+4+ +16=84127 (88, 'M') : (NO_BUTTON, SCROLL_UP, ALT_CONTROL), # scroll_up Alt Control 64+ +8+16=88128 (92, 'M') : (NO_BUTTON, SCROLL_UP, SHIFT_ALT_CONTROL), # scroll_up Shift Alt Control 64+4+8+16=92129 (65, 'M') : (NO_BUTTON, SCROLL_DOWN, NO_MODIFIER), # scroll_down 64+ + + =65130 (69, 'M') : (NO_BUTTON, SCROLL_DOWN, SHIFT), # scroll_down Shift 64+4+ + =69131 (73, 'M') : (NO_BUTTON, SCROLL_DOWN, ALT), # scroll_down Alt 64+ +8+ =73132 (77, 'M') : (NO_BUTTON, SCROLL_DOWN, SHIFT_ALT), # scroll_down Shift Alt 64+4+8+ =77133 (81, 'M') : (NO_BUTTON, SCROLL_DOWN, CONTROL), # scroll_down Control 64+ + +16=81134 (85, 'M') : (NO_BUTTON, SCROLL_DOWN, SHIFT_CONTROL), # scroll_down Shift Control 64+4+ +16=85135 (89, 'M') : (NO_BUTTON, SCROLL_DOWN, ALT_CONTROL), # scroll_down Alt Control 64+ +8+16=89136 (93, 'M') : (NO_BUTTON, SCROLL_DOWN, SHIFT_ALT_CONTROL), # scroll_down Shift Alt Control 64+4+8+16=93137}138typical_mouse_events = {139 32: (LEFT , MOUSE_DOWN , UNKNOWN_MODIFIER),140 33: (MIDDLE , MOUSE_DOWN , UNKNOWN_MODIFIER),141 34: (RIGHT , MOUSE_DOWN , UNKNOWN_MODIFIER),142 35: (UNKNOWN_BUTTON , MOUSE_UP , UNKNOWN_MODIFIER),143 64: (LEFT , MOUSE_MOVE , UNKNOWN_MODIFIER),144 65: (MIDDLE , MOUSE_MOVE , UNKNOWN_MODIFIER),145 66: (RIGHT , MOUSE_MOVE , UNKNOWN_MODIFIER),146 67: (NO_BUTTON , MOUSE_MOVE , UNKNOWN_MODIFIER),147 96: (NO_BUTTON , SCROLL_UP , UNKNOWN_MODIFIER),148 97: (NO_BUTTON , SCROLL_DOWN, UNKNOWN_MODIFIER),149}150urxvt_mouse_events={151 32: (UNKNOWN_BUTTON, MOUSE_DOWN , UNKNOWN_MODIFIER),152 35: (UNKNOWN_BUTTON, MOUSE_UP , UNKNOWN_MODIFIER),153 96: (NO_BUTTON , SCROLL_UP , UNKNOWN_MODIFIER),154 97: (NO_BUTTON , SCROLL_DOWN, UNKNOWN_MODIFIER),155}156# fmt:on157def load_mouse_bindings() -> KeyBindings:158 """159 Key bindings, required for mouse support.160 (Mouse events enter through the key binding system.)161 """162 key_bindings = KeyBindings()163 @key_bindings.add(Keys.Vt100MouseEvent)164 def _(event: E) -> "NotImplementedOrNone":165 """166 Handling of incoming mouse event.167 """168 # TypicaL: "eSC[MaB*"169 # Urxvt: "Esc[96;14;13M"170 # Xterm SGR: "Esc[<64;85;12M"171 # Parse incoming packet.172 if event.data[2] == "M":173 # Typical.174 mouse_event, x, y = map(ord, event.data[3:])175 # TODO: Is it possible to add modifiers here?176 mouse_button, mouse_event_type, mouse_modifier = typical_mouse_events[177 mouse_event178 ]179 # Handle situations where `PosixStdinReader` used surrogateescapes.180 if x >= 0xDC00:181 x -= 0xDC00182 if y >= 0xDC00:183 y -= 0xDC00184 x -= 32185 y -= 32186 else:187 # Urxvt and Xterm SGR.188 # When the '<' is not present, we are not using the Xterm SGR mode,189 # but Urxvt instead.190 data = event.data[2:]191 if data[:1] == "<":192 sgr = True193 data = data[1:]194 else:195 sgr = False196 # Extract coordinates.197 mouse_event, x, y = map(int, data[:-1].split(";"))198 m = data[-1]199 # Parse event type.200 if sgr:201 try:202 (203 mouse_button,204 mouse_event_type,205 mouse_modifiers,206 ) = xterm_sgr_mouse_events[mouse_event, m]207 except KeyError:208 return NotImplemented209 else:210 # Some other terminals, like urxvt, Hyper terminal, ...211 (212 mouse_button,213 mouse_event_type,214 mouse_modifiers,215 ) = urxvt_mouse_events.get(216 mouse_event, (UNKNOWN_BUTTON, MOUSE_MOVE, UNKNOWN_MODIFIER)217 )218 x -= 1219 y -= 1220 # Only handle mouse events when we know the window height.221 if event.app.renderer.height_is_known and mouse_event_type is not None:222 # Take region above the layout into account. The reported223 # coordinates are absolute to the visible part of the terminal.224 from prompt_toolkit.renderer import HeightIsUnknownError225 try:226 y -= event.app.renderer.rows_above_layout227 except HeightIsUnknownError:228 return NotImplemented229 # Call the mouse handler from the renderer.230 # Note: This can return `NotImplemented` if no mouse handler was231 # found for this position, or if no repainting needs to232 # happen. this way, we avoid excessive repaints during mouse233 # movements.234 handler = event.app.renderer.mouse_handlers.mouse_handlers[y][x]235 return handler(236 MouseEvent(237 position=Point(x=x, y=y),238 event_type=mouse_event_type,239 button=mouse_button,240 modifiers=mouse_modifiers,241 )242 )243 return NotImplemented244 @key_bindings.add(Keys.ScrollUp)245 def _scroll_up(event: E) -> None:246 """247 Scroll up event without cursor position.248 """249 # We don't receive a cursor position, so we don't know which window to250 # scroll. Just send an 'up' key press instead.251 event.key_processor.feed(KeyPress(Keys.Up), first=True)252 @key_bindings.add(Keys.ScrollDown)253 def _scroll_down(event: E) -> None:254 """255 Scroll down event without cursor position.256 """257 event.key_processor.feed(KeyPress(Keys.Down), first=True)258 @key_bindings.add(Keys.WindowsMouseEvent)259 def _mouse(event: E) -> "NotImplementedOrNone":260 """261 Handling of mouse events for Windows.262 """263 assert is_windows() # This key binding should only exist for Windows.264 # Parse data.265 pieces = event.data.split(";")266 button = MouseButton(pieces[0])267 event_type = MouseEventType(pieces[1])268 x = int(pieces[2])269 y = int(pieces[3])270 # Make coordinates absolute to the visible part of the terminal.271 output = event.app.renderer.output272 from prompt_toolkit.output.win32 import Win32Output273 from prompt_toolkit.output.windows10 import Windows10_Output274 if isinstance(output, (Win32Output, Windows10_Output)):275 screen_buffer_info = output.get_win32_screen_buffer_info()276 rows_above_cursor = (277 screen_buffer_info.dwCursorPosition.Y - event.app.renderer._cursor_pos.y278 )279 y -= rows_above_cursor280 # Call the mouse event handler.281 # (Can return `NotImplemented`.)282 handler = event.app.renderer.mouse_handlers.mouse_handlers[y][x]283 return handler(284 MouseEvent(285 position=Point(x=x, y=y),286 event_type=event_type,287 button=button,288 modifiers=UNKNOWN_MODIFIER,289 )290 )291 # No mouse handler found. Return `NotImplemented` so that we don't292 # invalidate the UI.293 return NotImplemented...

Full Screen

Full Screen

mouse_bindings.py

Source:mouse_bindings.py Github

copy

Full Screen

1from ..events import Mods, MouseEventType, MouseButton2MOUSE_UP = MouseEventType.MOUSE_UP3MOUSE_DOWN = MouseEventType.MOUSE_DOWN4MOUSE_MOVE = MouseEventType.MOUSE_MOVE5SCROLL_UP = MouseEventType.SCROLL_UP6SCROLL_DOWN = MouseEventType.SCROLL_DOWN7LEFT = MouseButton.LEFT8MIDDLE = MouseButton.MIDDLE9RIGHT = MouseButton.RIGHT10NO_BUTTON = MouseButton.NO_BUTTON11UNKNOWN_BUTTON = MouseButton.UNKNOWN_BUTTON12NO_MODS = Mods.NO_MODS13ALT = Mods(True , False, False)14CONTROL = Mods(False, True , False)15SHIFT = Mods(False, False, True )16ALT_CONTROL = Mods(True , True , False)17ALT_SHIFT = Mods(True , False, True )18CONTROL_SHIFT = Mods(False, True , True )19ALT_CONTROL_SHIFT = Mods(True , True , True )20TERM_SGR = {21 ( 0, "m"): (MOUSE_UP, LEFT, NO_MODS),22 ( 4, "m"): (MOUSE_UP, LEFT, SHIFT),23 ( 8, "m"): (MOUSE_UP, LEFT, ALT),24 (12, "m"): (MOUSE_UP, LEFT, ALT_SHIFT),25 (16, "m"): (MOUSE_UP, LEFT, CONTROL),26 (20, "m"): (MOUSE_UP, LEFT, CONTROL_SHIFT),27 (24, "m"): (MOUSE_UP, LEFT, ALT_CONTROL),28 (28, "m"): (MOUSE_UP, LEFT, ALT_CONTROL_SHIFT),29 ( 1, "m"): (MOUSE_UP, MIDDLE, NO_MODS),30 ( 5, "m"): (MOUSE_UP, MIDDLE, SHIFT),31 ( 9, "m"): (MOUSE_UP, MIDDLE, ALT),32 (13, "m"): (MOUSE_UP, MIDDLE, ALT_SHIFT),33 (17, "m"): (MOUSE_UP, MIDDLE, CONTROL),34 (21, "m"): (MOUSE_UP, MIDDLE, CONTROL_SHIFT),35 (25, "m"): (MOUSE_UP, MIDDLE, ALT_CONTROL),36 (29, "m"): (MOUSE_UP, MIDDLE, ALT_CONTROL_SHIFT),37 ( 2, "m"): (MOUSE_UP, RIGHT, NO_MODS),38 ( 6, "m"): (MOUSE_UP, RIGHT, SHIFT),39 (10, "m"): (MOUSE_UP, RIGHT, ALT),40 (14, "m"): (MOUSE_UP, RIGHT, ALT_SHIFT),41 (18, "m"): (MOUSE_UP, RIGHT, CONTROL),42 (22, "m"): (MOUSE_UP, RIGHT, CONTROL_SHIFT),43 (26, "m"): (MOUSE_UP, RIGHT, ALT_CONTROL),44 (30, "m"): (MOUSE_UP, RIGHT, ALT_CONTROL_SHIFT),45 ( 0, "M"): (MOUSE_DOWN, LEFT, NO_MODS),46 ( 4, "M"): (MOUSE_DOWN, LEFT, SHIFT),47 ( 8, "M"): (MOUSE_DOWN, LEFT, ALT),48 (12, "M"): (MOUSE_DOWN, LEFT, ALT_SHIFT),49 (16, "M"): (MOUSE_DOWN, LEFT, CONTROL),50 (20, "M"): (MOUSE_DOWN, LEFT, CONTROL_SHIFT),51 (24, "M"): (MOUSE_DOWN, LEFT, ALT_CONTROL),52 (28, "M"): (MOUSE_DOWN, LEFT, ALT_CONTROL_SHIFT),53 ( 1, "M"): (MOUSE_DOWN, MIDDLE, NO_MODS),54 ( 5, "M"): (MOUSE_DOWN, MIDDLE, SHIFT),55 ( 9, "M"): (MOUSE_DOWN, MIDDLE, ALT),56 (13, "M"): (MOUSE_DOWN, MIDDLE, ALT_SHIFT),57 (17, "M"): (MOUSE_DOWN, MIDDLE, CONTROL),58 (21, "M"): (MOUSE_DOWN, MIDDLE, CONTROL_SHIFT),59 (25, "M"): (MOUSE_DOWN, MIDDLE, ALT_CONTROL),60 (29, "M"): (MOUSE_DOWN, MIDDLE, ALT_CONTROL_SHIFT),61 ( 2, "M"): (MOUSE_DOWN, RIGHT, NO_MODS),62 ( 6, "M"): (MOUSE_DOWN, RIGHT, SHIFT),63 (10, "M"): (MOUSE_DOWN, RIGHT, ALT),64 (14, "M"): (MOUSE_DOWN, RIGHT, ALT_SHIFT),65 (18, "M"): (MOUSE_DOWN, RIGHT, CONTROL),66 (22, "M"): (MOUSE_DOWN, RIGHT, CONTROL_SHIFT),67 (26, "M"): (MOUSE_DOWN, RIGHT, ALT_CONTROL),68 (30, "M"): (MOUSE_DOWN, RIGHT, ALT_CONTROL_SHIFT),69 (32, "M"): (MOUSE_MOVE, LEFT, NO_MODS),70 (36, "M"): (MOUSE_MOVE, LEFT, SHIFT),71 (40, "M"): (MOUSE_MOVE, LEFT, ALT),72 (44, "M"): (MOUSE_MOVE, LEFT, ALT_SHIFT),73 (48, "M"): (MOUSE_MOVE, LEFT, CONTROL),74 (52, "M"): (MOUSE_MOVE, LEFT, CONTROL_SHIFT),75 (56, "M"): (MOUSE_MOVE, LEFT, ALT_CONTROL),76 (60, "M"): (MOUSE_MOVE, LEFT, ALT_CONTROL_SHIFT),77 (33, "M"): (MOUSE_MOVE, MIDDLE, NO_MODS),78 (37, "M"): (MOUSE_MOVE, MIDDLE, SHIFT),79 (41, "M"): (MOUSE_MOVE, MIDDLE, ALT),80 (45, "M"): (MOUSE_MOVE, MIDDLE, ALT_SHIFT),81 (49, "M"): (MOUSE_MOVE, MIDDLE, CONTROL),82 (53, "M"): (MOUSE_MOVE, MIDDLE, CONTROL_SHIFT),83 (57, "M"): (MOUSE_MOVE, MIDDLE, ALT_CONTROL),84 (61, "M"): (MOUSE_MOVE, MIDDLE, ALT_CONTROL_SHIFT),85 (34, "M"): (MOUSE_MOVE, RIGHT, NO_MODS),86 (38, "M"): (MOUSE_MOVE, RIGHT, SHIFT),87 (42, "M"): (MOUSE_MOVE, RIGHT, ALT),88 (46, "M"): (MOUSE_MOVE, RIGHT, ALT_SHIFT),89 (50, "M"): (MOUSE_MOVE, RIGHT, CONTROL),90 (54, "M"): (MOUSE_MOVE, RIGHT, CONTROL_SHIFT),91 (58, "M"): (MOUSE_MOVE, RIGHT, ALT_CONTROL),92 (62, "M"): (MOUSE_MOVE, RIGHT, ALT_CONTROL_SHIFT),93 (35, "M"): (MOUSE_MOVE, NO_BUTTON, NO_MODS),94 (39, "M"): (MOUSE_MOVE, NO_BUTTON, SHIFT),95 (43, "M"): (MOUSE_MOVE, NO_BUTTON, ALT),96 (47, "M"): (MOUSE_MOVE, NO_BUTTON, ALT_SHIFT),97 (51, "M"): (MOUSE_MOVE, NO_BUTTON, CONTROL),98 (55, "M"): (MOUSE_MOVE, NO_BUTTON, CONTROL_SHIFT),99 (59, "M"): (MOUSE_MOVE, NO_BUTTON, ALT_CONTROL),100 (63, "M"): (MOUSE_MOVE, NO_BUTTON, ALT_CONTROL_SHIFT),101 # This is duplicated from the block above with lowercase "m" for WSL.102 (35, "m"): (MOUSE_MOVE, NO_BUTTON, NO_MODS),103 (39, "m"): (MOUSE_MOVE, NO_BUTTON, SHIFT),104 (43, "m"): (MOUSE_MOVE, NO_BUTTON, ALT),105 (47, "m"): (MOUSE_MOVE, NO_BUTTON, ALT_SHIFT),106 (51, "m"): (MOUSE_MOVE, NO_BUTTON, CONTROL),107 (55, "m"): (MOUSE_MOVE, NO_BUTTON, CONTROL_SHIFT),108 (59, "m"): (MOUSE_MOVE, NO_BUTTON, ALT_CONTROL),109 (63, "m"): (MOUSE_MOVE, NO_BUTTON, ALT_CONTROL_SHIFT),110 (64, "M"): (SCROLL_UP, NO_BUTTON, NO_MODS),111 (68, "M"): (SCROLL_UP, NO_BUTTON, SHIFT),112 (72, "M"): (SCROLL_UP, NO_BUTTON, ALT),113 (76, "M"): (SCROLL_UP, NO_BUTTON, ALT_SHIFT),114 (80, "M"): (SCROLL_UP, NO_BUTTON, CONTROL),115 (84, "M"): (SCROLL_UP, NO_BUTTON, CONTROL_SHIFT),116 (88, "M"): (SCROLL_UP, NO_BUTTON, ALT_CONTROL),117 (92, "M"): (SCROLL_UP, NO_BUTTON, ALT_CONTROL_SHIFT),118 (65, "M"): (SCROLL_DOWN, NO_BUTTON, NO_MODS),119 (69, "M"): (SCROLL_DOWN, NO_BUTTON, SHIFT),120 (73, "M"): (SCROLL_DOWN, NO_BUTTON, ALT),121 (77, "M"): (SCROLL_DOWN, NO_BUTTON, ALT_SHIFT),122 (81, "M"): (SCROLL_DOWN, NO_BUTTON, CONTROL),123 (85, "M"): (SCROLL_DOWN, NO_BUTTON, CONTROL_SHIFT),124 (89, "M"): (SCROLL_DOWN, NO_BUTTON, ALT_CONTROL),125 (93, "M"): (SCROLL_DOWN, NO_BUTTON, ALT_CONTROL_SHIFT),126}127TYPICAL = {128 32: (MOUSE_DOWN, LEFT, NO_MODS),129 33: (MOUSE_DOWN, MIDDLE, NO_MODS),130 34: (MOUSE_DOWN, RIGHT, NO_MODS),131 35: (MOUSE_UP, UNKNOWN_BUTTON, NO_MODS),132 64: (MOUSE_MOVE, LEFT, NO_MODS),133 65: (MOUSE_MOVE, MIDDLE, NO_MODS),134 66: (MOUSE_MOVE, RIGHT, NO_MODS),135 67: (MOUSE_MOVE, NO_BUTTON, NO_MODS),136 96: (SCROLL_UP, NO_BUTTON, NO_MODS),137 97: (SCROLL_DOWN, NO_BUTTON, NO_MODS),138}139URXVT = {140 32: (MOUSE_DOWN, UNKNOWN_BUTTON, NO_MODS),141 35: (MOUSE_UP, UNKNOWN_BUTTON, NO_MODS),142 96: (SCROLL_UP, NO_BUTTON, NO_MODS),143 97: (SCROLL_DOWN, NO_BUTTON, NO_MODS),...

Full Screen

Full Screen

app.py

Source:app.py Github

copy

Full Screen

...3 @abstractmethod4 def mouse_down(self):5 pass6 @abstractmethod7 def mouse_up(self):8 pass9class SelectionTool(Tool):10 def mouse_down(self):11 print('Selection tool.')12 def mouse_up(self):13 print('Draw dashed triangle.')14 def __repr__(self) -> str:15 return 'Selection Tool'16 def __str__(self) -> str:17 return 'Selection Tool'18class BrushTool(Tool):19 def mouse_down(self):20 print('Brush tool.')21 def mouse_up(self):22 print('Draw a line.')23 def __repr__(self) -> str:24 return 'Brush Tool'25 def __str__(self) -> str:26 return 'Brush Tool'27class EraserTool(Tool):28 def mouse_down(self):29 print('Eraser tool.')30 def mouse_up(self):31 print('Erase something.')32 def __repr__(self) -> str:33 return 'Eraser Tool'34 def __str__(self) -> str:35 return 'Eraser Tool'36class Canvas:37 def __init__(self) -> None:38 self.current_tool = None39 def mouse_up(self):40 self.current_tool.mouse_up()41 def mouse_down(self):42 self.current_tool.mouse_down()43 def get_current_tool(self):44 return self.current_tool45 def set_current_tool(self, tool):46 self.current_tool = tool47canvas = Canvas()48canvas.set_current_tool(SelectionTool())49canvas.mouse_down()50canvas.mouse_up()51canvas.set_current_tool(BrushTool())52canvas.mouse_down()53canvas.mouse_up()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful