summaryrefslogtreecommitdiff
path: root/cad/src/exprs/Center.py
blob: 50b7a1398720ab1bc550b172f46d93861c51b98a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details.
"""
Center.py - alignment primitives

$Id$

might be renamed or merged

note: transforms.py was split out of here, 061115
"""

from exprs.transforms import Translate

from exprs.Exprs import V_expr
from exprs.attr_decl_macros import Arg
from exprs.instance_helpers import DelegatingInstanceOrExpr
from exprs.widget2d import Widget2D
from exprs.__Symbols__ import _self

# There are 15 non-noop combos of dx & dy formulae, but only 13 have simple names, due to X/Y ambiguity of Center...
# even so, I spell them out like this, partly since the resulting classes are more efficient
# than the easy implems of a general version which takes args for what to do with x & y independently.
# Maybe the missing ones could be called CenterX and CenterY? (They do nothing to the not-named coordinate.) Done.
# See testexpr_21e for a table of all of them.

# BTW they need docstrings; here is an old one for Center:
"Center(thing) draws as thing (a Widget2D [#e should make work for 3D too]), but is centered on the origin"

class TopRight(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = - thing.bright
    dy = - thing.btop
    delegate = Translate(thing, V_expr(dx,dy,0))

class CenterRight(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = - thing.bright
    dy = (thing.bbottom - thing.btop)/2.0
    delegate = Translate(thing, V_expr(dx,dy,0))

class BottomRight(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = - thing.bright
    dy = + thing.bbottom
    delegate = Translate(thing, V_expr(dx,dy,0))


class TopLeft(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = + thing.bleft
    dy = - thing.btop
    delegate = Translate(thing, V_expr(dx,dy,0))

class CenterLeft(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = + thing.bleft
    dy = (thing.bbottom - thing.btop)/2.0
    delegate = Translate(thing, V_expr(dx,dy,0))

class BottomLeft(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = + thing.bleft
    dy = + thing.bbottom
    delegate = Translate(thing, V_expr(dx,dy,0))


class TopCenter(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = (thing.bleft - thing.bright)/2.0
    dy = - thing.btop
    delegate = Translate(thing, V_expr(dx,dy,0))

class Center(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = (thing.bleft - thing.bright)/2.0
    dy = (thing.bbottom - thing.btop)/2.0
    delegate = Translate(thing, V_expr(dx,dy,0))

class BottomCenter(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = (thing.bleft - thing.bright)/2.0
    dy = + thing.bbottom
    delegate = Translate(thing, V_expr(dx,dy,0))


class Left(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = + thing.bleft
    delegate = Translate(thing, V_expr(dx,0,0))

class Right(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = - thing.bright
    delegate = Translate(thing, V_expr(dx,0,0))

class Top(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dy = - thing.btop
    delegate = Translate(thing, V_expr(0,dy,0))

class Bottom(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dy = + thing.bbottom
    delegate = Translate(thing, V_expr(0,dy,0))


class CenterX(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dx = (thing.bleft - thing.bright)/2.0
    delegate = Translate(thing, V_expr(dx,0,0))

class CenterY(DelegatingInstanceOrExpr):
    thing = Arg(Widget2D)
    dy = (thing.bbottom - thing.btop)/2.0
    delegate = Translate(thing, V_expr(0,dy,0))

# == not yet working: a form with less duplicated code

class AlignmentExpr(DelegatingInstanceOrExpr): # doesn't work yet, see below
    thing = Arg(Widget2D)
    dx = 0 # override in some subclasses
    dy = 0
    delegate = Translate(thing, V_expr(dx,dy,0))
    pass

class TopRight_buggy(AlignmentExpr): # with AlignmentExpr -- doesn't work yet
    dx = - _self.thing.bright
    dy = - _self.thing.btop ###BUG [061211] -- for some reason these don't seem to be inherited into the formula for delegate.
        # guess: ExprsMeta stores it, then stores these new ones, but doesn't know it needs to "revisit" that one (for delegate)
        # and remake it for subclass with revised dx/dy. (Ideally it would remake it afresh for each subclass, I guess... how hard??)
        # conclusion: debug that, but for now, don't use the AlignmentExpr superclass, just spell out each subclass.
        # update, 080514: obvious in hindsight: when the delegate formula in AlignmentExpr is executed by Python,
        # dx is 0 so it might as well be defining delegate = Translate(thing, V_expr(0,0,0)).
        # So there is no fix without revising that definition or the defs of dx, dy.

# pre-061211 version of Center, also worked fine:
##class Center(InstanceMacro):
##    "Center(thing) draws as thing (a Widget2D [#e should make work for 3D too]), but is centered on the origin"
##    # args
##    thing = Arg(Widget2D)
##    # internal formulas - decide how much to translate thing
##    dx = (thing.bleft - thing.bright)/2.0
##    dy = (thing.bbottom - thing.btop)/2.0
##    # value
##    ###k for now, use V_expr instead of V when it has expr args... not sure we can get around this (maybe redef of V will be ok??)
##    _value = Translate(thing, V_expr(dx,dy,0)) # this translates both thing and its layout box ###k might not work with V(expr...)
##    pass

# end