Skip to content

Commit 970fe7a

Browse files
author
Tom De Smedt
committed
Control.color property.
1 parent cbafc55 commit 970fe7a

File tree

1 file changed

+55
-47
lines changed

1 file changed

+55
-47
lines changed

nodebox/gui/controls.py

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self, path, **kwargs):
6666

6767
class Control(Layer):
6868

69-
def __init__(self, x=0, y=0, id=None, **kwargs):
69+
def __init__(self, x=0, y=0, id=None, color=(1,1,1,1), **kwargs):
7070
""" Base class for GUI controls.
7171
The Control class inherits from Layer so it must be appended to the canvas (or a container)
7272
to receive events and get drawn.
@@ -76,6 +76,7 @@ def __init__(self, x=0, y=0, id=None, **kwargs):
7676
Layer.__init__(self, x=x, y=y, **kwargs)
7777
self.id = id
7878
self.src = {} # Collection of source images.
79+
self.color = color # Color for source images.
7980
self.enabled = True # Enable event listener.
8081
self.duration = 0 # Disable tweening.
8182
self._controls = {} # Lazy index of (id, control) children, see nested().
@@ -288,7 +289,7 @@ def update(self):
288289
self[0].y = 0.5 * (self.height - self[0].height) - self.pressed
289290

290291
def draw(self):
291-
clr = self.pressed and (0.75, 0.75, 0.75) or (1.0, 1.0, 1.0)
292+
clr = self.pressed and [v*0.75 for v in self.color] or self.color
292293
im1, im2, im3 = self.src["cap1"], self.src["cap2"], self.src["face"]
293294
image(im1, 0, 0, height=self.height, color=clr)
294295
image(im2, x=self.width-im2.width, height=self.height, color=clr)
@@ -322,7 +323,7 @@ def _pack(self):
322323
self._set_height(self.src["face"].height)
323324

324325
def draw(self):
325-
clr = self.pressed and (0.75, 0.75, 0.75) or (1.0, 1.0, 1.0)
326+
clr = self.pressed and [v*0.75 for v in self.color] or self.color
326327
image(self.src["face"], 0, 0, color=clr)
327328

328329
def on_mouse_release(self, mouse):
@@ -364,7 +365,7 @@ def on_mouse_release(self, mouse):
364365
self.parent.on_mouse_release(mouse)
365366

366367
def draw(self):
367-
clr = self.parent.pressed | self.pressed and (0.75, 0.75, 0.75) or (1.0, 1.0, 1.0)
368+
clr = self.parent.pressed | self.pressed and [v*0.75 for v in self.color] or self.color
368369
image(self.parent.src["handle"], 0, 0, color=clr)
369370

370371
class Slider(Control):
@@ -421,10 +422,11 @@ def update(self):
421422
def draw(self):
422423
t = self._t * self.width
423424
im1, im2, im3, im4 = self.src["cap1"], self.src["cap2"], self.src["face1"], self.src["face2"]
424-
image(im1, x=0, y=0)
425-
image(im2, x=self.width-im2.width, y=0)
426-
image(im3, x=im1.width, y=0, width=t-im1.width)
427-
image(im4, x=t, y=0, width=self.width-t-im2.width+1)
425+
clr = self.color
426+
image(im1, x=0, y=0, color=clr)
427+
image(im2, x=self.width-im2.width, y=0, color=clr)
428+
image(im3, x=im1.width, y=0, width=t-im1.width, color=clr)
429+
image(im4, x=t, y=0, width=self.width-t-im2.width+1, color=clr)
428430

429431
def on_mouse_press(self, mouse):
430432
x0, y0 = self.absolute_position() # Can be nested in other layers.
@@ -476,11 +478,12 @@ def reset(self):
476478
self.value = self.default
477479

478480
def draw(self):
481+
clr1 = self.color
482+
clr2 = self.pressed and [v*0.85 for v in self.color] or self.color
479483
translate(self.width/2, self.height/2)
480-
image(self.src["socket"], -self.width/2, -self.height/2)
484+
image(self.src["socket"], -self.width/2, -self.height/2, color=clr1)
481485
rotate(360-self.value)
482-
clr = self.pressed and (0.85, 0.85, 0.85) or (1.0, 1.0, 1.0)
483-
image(self.src["face"], -self.width/2, -self.height/2, color=clr)
486+
image(self.src["face"], -self.width/2, -self.height/2, color=clr2)
484487

485488
def on_mouse_press(self, mouse):
486489
self.value += mouse.dy * (CTRL in mouse.modifiers and 1 or 5)
@@ -518,7 +521,8 @@ def reset(self):
518521
self.value = self.default
519522

520523
def draw(self):
521-
image(self.value and self.src["checked"] or self.src["face"])
524+
clr = self.color
525+
image(self.value and self.src["checked"] or self.src["face"], color=clr)
522526

523527
def on_mouse_release(self, mouse):
524528
Control.on_mouse_release(self, mouse)
@@ -769,10 +773,11 @@ def update(self):
769773
self[0].hidden = self.editing or self.value != ""
770774

771775
def draw(self):
776+
clr = self.color
772777
im1, im2, im3 = self.src["cap1"], self.src["cap2"], self.src["face"]
773-
image(im1, 0, 0, height=self.height)
774-
image(im2, x=self.width-im2.width, height=self.height)
775-
image(im3, x=im1.width, width=self.width-im1.width-im2.width, height=self.height)
778+
image(im1, 0, 0, height=self.height, color=clr)
779+
image(im2, x=self.width-im2.width, height=self.height, color=clr)
780+
image(im3, x=im1.width, width=self.width-im1.width-im2.width, height=self.height, color=clr)
776781
Editable.draw(self)
777782

778783
#=====================================================================================================
@@ -781,7 +786,7 @@ def draw(self):
781786

782787
class Rulers(Control):
783788

784-
def __init__(self, step=10, interval=5, crosshair=False, fill=(0,0,0,1)):
789+
def __init__(self, step=10, interval=5, crosshair=False, color=(0,0,0,1)):
785790
""" A horizontal and vertical ruler displaying the width/height of the parent at intervals.
786791
A measurement line is drawn at each step(e.g. at 10 20 30...)
787792
A label with the value is drawn at each interval (e.g. 50 | | | | 100 | | | | 150).
@@ -791,7 +796,7 @@ def __init__(self, step=10, interval=5, crosshair=False, fill=(0,0,0,1)):
791796
self.step = step
792797
self.interval = interval
793798
self.crosshair = crosshair
794-
self._fill = fill
799+
self.color = color
795800
self._dirty = False
796801
self._markers = {}
797802
self._pack()
@@ -827,7 +832,7 @@ def _pack(self):
827832
Text(str(int(round(i*self._step))),
828833
fontname = theme["fontname"],
829834
fontsize = theme["fontsize"] * 0.6,
830-
fill = self._fill))
835+
fill = self.color))
831836

832837
def update(self):
833838
self._pack()
@@ -838,26 +843,26 @@ def draw(self):
838843
for i in range(1, int(round(self.height / self._step))):
839844
v, mark = i*self._step, i%self.interval==0
840845
line(0, v, mark and length*3 or length, v,
841-
stroke = self._fill,
846+
stroke = self.color,
842847
strokewidth = 0.5)
843848
if mark:
844849
self._markers[v].draw(length*3-self._markers[v].metrics[0], v+2)
845850
# Draw the vertical ruler.
846851
for i in range(1, int(round(self.width / self._step))):
847852
v, mark = i*self._step, i%self.interval==0
848853
line(v, 0, v, mark and length*3 or length,
849-
stroke = self._fill,
854+
stroke = self.color,
850855
strokewidth = 0.5)
851856
if mark:
852857
self._markers[v].draw(v+2, length*3-self._markers[v].fontsize)
853858
# Draw the crosshair.
854859
if self.crosshair:
855860
line(0, self.canvas.mouse.y, self.width, self.canvas.mouse.y,
856-
stroke = self._fill,
861+
stroke = self.color,
857862
strokewidth = 0.5,
858863
strokestyle = DOTTED)
859864
line(self.canvas.mouse.x, 0, self.canvas.mouse.x, self.height,
860-
stroke = self._fill,
865+
stroke = self.color,
861866
strokewidth = 0.5,
862867
strokestyle = DOTTED)
863868

@@ -905,19 +910,19 @@ def controls(self):
905910
return iter(self[2:]) # self[0] is the Label,
906911
# self[1] is the Close action.
907912

908-
def insert(self, i, control):
913+
def insert(self, i, control, **kwargs):
909914
""" Inserts the control, or inserts all controls in the given Layout.
910915
"""
911916
if isinstance(control, Layout):
912917
# If the control is actually a Layout (e.g. ordered group of controls), apply it.
913-
control.apply()
918+
control.apply(**kwargs)
914919
Layer.insert(self, i, control)
915920

916-
def append(self, control):
917-
self.insert(len(self), control)
918-
def extend(self, controls):
921+
def append(self, control, **kwargs):
922+
self.insert(len(self), control, **kwargs)
923+
def extend(self, controls, **kwargs):
919924
for control in controls:
920-
self.append(control)
925+
self.append(control, **kwargs)
921926

922927
def _pack(self):
923928
# Center the caption in the label's header.
@@ -945,20 +950,22 @@ def _visit(control):
945950

946951
def update(self):
947952
self[1].hidden = self.modal
953+
self[1].color = self.color
948954

949955
def draw(self):
950956
im1, im2, im3 = self.src["cap1"], self.src["cap2"], self.src["top"]
951957
im4, im5, im6 = self.src["cap3"], self.src["cap4"], self.src["bottom"]
952958
im7, im8, im9 = self.src["left"], self.src["right"], self.src["face"]
953-
image(im1, 0, self.height-im1.height)
954-
image(im2, self.width-im2.width, self.height-im2.height)
955-
image(im3, im1.width, self.height-im3.height, width=self.width-im1.width-im2.width)
956-
image(im4, 0, 0)
957-
image(im5, self.width-im5.width, 0)
958-
image(im6, im4.width, 0, width=self.width-im4.width-im5.width)
959-
image(im7, 0, im4.height, height=self.height-im1.height-im4.height)
960-
image(im8, self.width-im8.width, im4.height, height=self.height-im2.height-im5.height)
961-
image(im9, im4.width, im6.height, width=self.width-im7.width-im8.width, height=self.height-im3.height-im6.height)
959+
clr = self.color
960+
image(im1, 0, self.height-im1.height, color=clr)
961+
image(im2, self.width-im2.width, self.height-im2.height, color=clr)
962+
image(im3, im1.width, self.height-im3.height, width=self.width-im1.width-im2.width, color=clr)
963+
image(im4, 0, 0, color=clr)
964+
image(im5, self.width-im5.width, 0, color=clr)
965+
image(im6, im4.width, 0, width=self.width-im4.width-im5.width, color=clr)
966+
image(im7, 0, im4.height, height=self.height-im1.height-im4.height, color=clr)
967+
image(im8, self.width-im8.width, im4.height, height=self.height-im2.height-im5.height, color=clr)
968+
image(im9, im4.width, im6.height, width=self.width-im7.width-im8.width, height=self.height-im3.height-im6.height, color=clr)
962969

963970
def on_mouse_enter(self, mouse):
964971
mouse.cursor = DEFAULT
@@ -1009,8 +1016,9 @@ def draw(self):
10091016
if self.canvas is not None and \
10101017
(self.anchor == LEFT and self.x == 0) or \
10111018
(self.anchor == RIGHT and self.x == self.canvas.width-self.width):
1012-
image(im1, 0, self.height-im1.height, width=self.width)
1013-
image(im2, 0, -self.canvas.height+self.height, width=self.width, height=self.canvas.height-im1.height)
1019+
clr = self.color
1020+
image(im1, 0, self.height-im1.height, width=self.width, color=clr)
1021+
image(im2, 0, -self.canvas.height+self.height, width=self.width, height=self.canvas.height-im1.height, color=clr)
10141022
else:
10151023
Panel.draw(self)
10161024

@@ -1026,15 +1034,15 @@ def __init__(self, x=0, y=0, **kwargs):
10261034
The layout will be applied when Layout.apply() is called.
10271035
This happens automatically if a layout is appended to a Panel.
10281036
"""
1029-
10301037
kwargs["width"] = 0
10311038
kwargs["height"] = 0
10321039
Layer.__init__(self, x=x, y=y, **kwargs)
10331040
self._controls = {} # Lazy cache of (id, control)-children, see nested().
10341041

10351042
def insert(self, i, control):
10361043
if isinstance(control, Layout):
1037-
control.apply() # If the control is actually a Layout, apply it.
1044+
# If the control is actually a Layout, apply it.
1045+
control.apply()
10381046
Layer.insert(self, i, control)
10391047

10401048
def append(self, control):
@@ -1076,10 +1084,10 @@ def __repr__(self):
10761084

10771085
class Labeled(Layout):
10781086

1079-
def __init__(self, controls=[], x=0, y=0):
1087+
def __init__(self, controls=[], x=0, y=0, **kwargs):
10801088
""" A layout where each control has an associated text label.
10811089
"""
1082-
Layout.__init__(self, x=x, y=y)
1090+
Layout.__init__(self, x=x, y=y, **kwargs)
10831091
self.controls = []
10841092
self.captions = []
10851093
self.extend(controls)
@@ -1111,12 +1119,12 @@ def pop(self, i):
11111119

11121120
class Rows(Labeled):
11131121

1114-
def __init__(self, controls=[], x=0, y=0, width=125):
1122+
def __init__(self, controls=[], x=0, y=0, width=125, **kwargs):
11151123
""" A layout where each control appears on a new line.
11161124
Each control has an associated text caption, displayed to the left of the control.
11171125
The given width defines the desired width for each control.
11181126
"""
1119-
Labeled.__init__(self, controls, x=x, y=y)
1127+
Labeled.__init__(self, controls, x=x, y=y, **kwargs)
11201128
self._maxwidth = width
11211129

11221130
def apply(self, spacing=10):
@@ -1154,12 +1162,12 @@ def apply(self, spacing=10):
11541162

11551163
class Row(Labeled):
11561164

1157-
def __init__(self, controls=[], x=0, y=0, width=125, align=CENTER):
1165+
def __init__(self, controls=[], x=0, y=0, width=125, align=CENTER, **kwargs):
11581166
""" A layout where each control appears in a new column.
11591167
Each control has an associated text caption, displayed on top of the control.
11601168
The given width defines the desired width for each control.
11611169
"""
1162-
Labeled.__init__(self, controls, x=x, y=y)
1170+
Labeled.__init__(self, controls, x=x, y=y, **kwargs)
11631171
self._maxwidth = width
11641172
self._align = align
11651173

0 commit comments

Comments
 (0)