Mercurial > boomslang
comparison pyntnclick/tools/rect_drawer.py @ 701:9c920deb28c2 pyntnclick
Recreate the palette selection
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Tue, 14 Feb 2012 17:55:07 +0200 |
parents | 22e1893152c5 |
children | cbf6d4573785 |
comparison
equal
deleted
inserted
replaced
700:22e1893152c5 | 701:9c920deb28c2 |
---|---|
11 MOUSEBUTTONUP, KEYDOWN) | 11 MOUSEBUTTONUP, KEYDOWN) |
12 import pygame | 12 import pygame |
13 | 13 |
14 import pyntnclick.constants | 14 import pyntnclick.constants |
15 from pyntnclick.widgets.text import LabelWidget, TextButton | 15 from pyntnclick.widgets.text import LabelWidget, TextButton |
16 from pyntnclick.widgets.base import Container | 16 from pyntnclick.widgets.base import Container, Button |
17 from pyntnclick.tools.utils import draw_rect_image | 17 from pyntnclick.tools.utils import draw_rect_image |
18 | 18 |
19 | 19 |
20 class RectDrawerError(Exception): | 20 class RectDrawerError(Exception): |
21 """Raised when initilaization failed""" | 21 """Raised when initilaization failed""" |
29 zoom_step = 100 | 29 zoom_step = 100 |
30 | 30 |
31 constants = RectDrawerConstants() | 31 constants = RectDrawerConstants() |
32 | 32 |
33 | 33 |
34 class AppPalette(object): | 34 class ColourButton(Button): |
35 | 35 """Button for selecting a colour""" |
36 sel_width = 5 | 36 |
37 sel_colour = pygame.color.Color(255, 255, 255) | |
38 unsel_colour = pygame.color.Color(128, 128, 128) | |
39 | |
40 padding = 2 | |
41 border = 3 | |
42 | |
43 def __init__(self, rect, gd, colour, palette): | |
44 super(ColourButton, self).__init__(rect, gd) | |
45 self._colour = pygame.color.Color(colour) | |
46 self._button_rect = self.rect.inflate(-self.padding, -self.padding) | |
47 self._colour_rect = self._button_rect.inflate(-self.border, | |
48 -self.border) | |
49 self.selected = False | |
50 self._palette = palette | |
51 self.add_callback('clicked', self.fix_selection) | |
52 | |
53 def fix_selection(self, ev, widget): | |
54 self._palette.cur_selection.selected = False | |
55 self.selected = True | |
56 self._palette.cur_selection = self | |
57 | |
58 def draw(self, surface): | |
59 surface.fill(pygame.color.Color(0, 0, 0), self.rect) | |
60 if self.selected: | |
61 surface.fill(self.sel_colour, self._button_rect) | |
62 else: | |
63 surface.fill(self.unsel_colour, self._button_rect) | |
64 surface.fill(self._colour, self._colour_rect) | |
65 | |
66 | |
67 class AppPalette(Container): | |
68 | |
69 but_size = 35 | |
37 | 70 |
38 colors = [ | 71 colors = [ |
39 'red', 'maroon1', 'palevioletred1', 'moccasin', 'orange', | 72 'red', 'maroon1', 'palevioletred1', 'moccasin', 'orange', |
40 'honeydew', 'yellow', 'gold', 'goldenrod', 'brown', | 73 'honeydew', 'yellow', 'gold', 'goldenrod', 'brown', |
41 'blue', 'purple', 'darkorchid4', 'thistle', 'skyblue1', | 74 'blue', 'purple', 'darkorchid4', 'thistle', 'skyblue1', |
42 'green', 'palegreen1', 'darkgreen', 'aquamarine', 'darkolivegreen', | 75 'green', 'palegreen1', 'darkgreen', 'aquamarine', 'darkolivegreen', |
43 ] | 76 ] |
44 | 77 |
45 def __init__(self, app_image): | 78 def __init__(self, rect, gd, app_image): |
46 self.image = app_image | 79 self.image = app_image |
47 #super(AppPalette, self).__init__((35, 35), 4, 5, margin=2) | 80 super(AppPalette, self).__init__(rect, gd) |
48 #self.selection = 0 | 81 self.selection = 0 |
49 #self.image.rect_color = pygame.color.Color(self.colors[self.selection]) | 82 self.image.rect_color = pygame.color.Color(self.colors[self.selection]) |
50 | 83 |
51 def num_items(self): | 84 x = rect.left |
52 return len(self.colors) | 85 y = rect.top |
53 | 86 for num, col in enumerate(self.colors): |
54 def draw_item(self, surface, item_no, rect): | 87 if (x - rect.left + self.but_size) >= rect.width: |
55 d = -2 * self.sel_width | 88 x = rect.left |
56 r = rect.inflate(d, d) | 89 y += self.but_size |
57 surface.fill(pygame.color.Color(self.colors[item_no]), r) | 90 button = ColourButton( |
58 | 91 pygame.Rect((x, y), (self.but_size, self.but_size)), |
59 def click_item(self, item_no, event): | 92 gd, col, self) |
60 self.selection = item_no | 93 x += self.but_size |
61 self.image.rect_color = pygame.color.Color(self.colors[item_no]) | 94 if num == 0: |
62 | 95 self.cur_selection = button |
63 def item_is_selected(self, item_no): | 96 button.fix_selection(None, None) |
64 return self.selection == item_no | 97 button.add_callback('clicked', self.click_item, num) |
98 self.add(button) | |
99 # Fix height | |
100 self.rect.height = y + self.but_size - self.rect.top | |
101 | |
102 def click_item(self, ev, widget, number): | |
103 self.selection = number | |
104 self.image.rect_color = pygame.color.Color(self.colors[number]) | |
65 | 105 |
66 | 106 |
67 class AppImage(Container): | 107 class AppImage(Container): |
68 | 108 |
69 rect_thick = 3 | 109 rect_thick = 3 |
526 self.app_image = app_image | 566 self.app_image = app_image |
527 super(ModeLabel, self).__init__(rect, | 567 super(ModeLabel, self).__init__(rect, |
528 gd, 'Mode : ', fontname=constants.bold_font, | 568 gd, 'Mode : ', fontname=constants.bold_font, |
529 fontsize=15, color=pygame.color.Color(128, 0, 255)) | 569 fontsize=15, color=pygame.color.Color(128, 0, 255)) |
530 | 570 |
531 | |
532 def draw(self, surface): | 571 def draw(self, surface): |
533 text = 'Mode : %s' % self.app_image.mode | 572 text = 'Mode : %s' % self.app_image.mode |
534 if self.text != text: | 573 if self.text != text: |
535 self.text = text | 574 self.text = text |
536 self.prepare() | 575 self.prepare() |
587 self.add(cycle) | 626 self.add(cycle) |
588 y += cycle.rect.height | 627 y += cycle.rect.height |
589 delete = make_button("Delete Objects", gd, self.image.del_mode, y) | 628 delete = make_button("Delete Objects", gd, self.image.del_mode, y) |
590 self.add(delete) | 629 self.add(delete) |
591 y += delete.rect.height | 630 y += delete.rect.height |
592 #palette = AppPalette(self.image) | 631 palette = AppPalette(pygame.Rect((810, y), (200, 0)), gd, self.image) |
593 #palette.rect.move_ip(810, y) | 632 self.add(palette) |
594 #self.add(palette) | 633 y += palette.rect.height |
595 #y += palette.rect.height | |
596 print_rects = make_button("Print objects", gd, | 634 print_rects = make_button("Print objects", gd, |
597 self.image.print_objs, y) | 635 self.image.print_objs, y) |
598 self.add(print_rects) | 636 self.add(print_rects) |
599 y += print_rects.rect.height | 637 y += print_rects.rect.height |
600 toggle_things = make_button("Show Things (t)", gd, | 638 toggle_things = make_button("Show Things (t)", gd, |