module Tkar::Primitives
Constants
- SHORTCUTS
Public Instance Methods
arc(args, key_args)
click to toggle source
arcX,Y,W,H,key:val…
(X,Y) is center, W is width, H is height. This differs from Tk because it's better! Rotation may look strange if width != height
extent angle start angle style sty (pieslice, chord, or arc)
# File lib/tkar/primitives.rb 76 def arc args, key_args 77 extent, start, style = key_args.values_at(*%w{extent start style}) 78 79 proc do |tkaroid, cos_r, sin_r| 80 x = tkaroid.x 81 y = tkaroid.y 82 params = tkaroid.params 83 84 cx, cy, width, height = args.map {|arg| arg[params] rescue arg} 85 86 rcx = x + cx * cos_r - cy * sin_r 87 rcy = y + cx * sin_r + cy * cos_r 88 89 coords = [ 90 rcx - width, rcy - height, 91 rcx + width, rcy + height ] 92 93 ## possible to skip below if no changes? 94 config = {} 95 handle_generic_config(config, params, key_args) 96 97 if extent 98 config[:extent] = extent[params] rescue extent 99 end 100 if start 101 config[:start] = start[params] rescue start 102 config[:start] -= tkaroid.r * RADIANS_TO_DEGREES 103 end 104 if style 105 config[:style] = style[params] rescue style 106 end 107 108 [TkcArc, coords, config] 109 end 110 end
color_str(col)
click to toggle source
# File lib/tkar/primitives.rb 19 def color_str col 20 @color_str[col] ||= 21 begin 22 s = col.to_s(16).rjust(6, "0") 23 "##{s}" 24 rescue 25 col # assume col is a color name 26 end 27 end
dash_val(val)
click to toggle source
# File lib/tkar/primitives.rb 31 def dash_val val 32 @dash_val[val] ||= val.is_a?(Integer) ? val.to_s.split("") : val 33 end
get_image(file_name)
click to toggle source
# File lib/tkar/primitives.rb 323 def get_image file_name 324 @@images ||= {} 325 @@images[file_name] ||= TkPhotoImage.new(:file => file_name) 326 end
handle_generic_config(config, params, key_args)
click to toggle source
handles the following options:
fill outline width dash stipple
# File lib/tkar/primitives.rb 42 def handle_generic_config(config, params, key_args) 43 fc, oc, wi, da, st = 44 key_args.values_at(*%w{fill outline width dash stipple}) 45 if fc 46 ## skip if we saw this one before? 47 val = fc[params] rescue fc 48 config[:fill] = color_str(val) 49 end 50 if oc 51 val = oc[params] rescue oc 52 config[:outline] = color_str(val) 53 end 54 if wi 55 config[:width] = wi[params] rescue wi 56 end 57 if da 58 val = da[params] rescue da 59 config[:dash] = dash_val val 60 end 61 if st 62 config[:stipple] = st[params] rescue st 63 ## how to use "@filename" to load file once and then use bitmap name? 64 end 65 end
handle_shortcuts(key)
click to toggle source
# File lib/tkar/primitives.rb 13 def handle_shortcuts key 14 SHORTCUTS[key] || key 15 end
image(args, key_args)
click to toggle source
imageX,Y,key:val…
anchor anchorPos (center, n, nw, ...) image imageFileName
# File lib/tkar/primitives.rb 297 def image args, key_args 298 anchor, image = key_args.values_at(*%w{anchor image}) 299 300 proc do |tkaroid, cos_r, sin_r| 301 x = tkaroid.x 302 y = tkaroid.y 303 params = tkaroid.params 304 305 xv, yv = args.map {|arg| arg[params] rescue arg} 306 coords = [ 307 x + xv * cos_r - yv * sin_r, 308 y + xv * sin_r + yv * cos_r 309 ] 310 311 config = {} 312 if anchor 313 config[:anchor] = anchor[params] rescue anchor 314 end 315 if image 316 config[:image] = get_image(image[params]) rescue get_image(image) 317 end 318 319 [TkcImage, coords, config] 320 end 321 end
line(args, key_args)
click to toggle source
lineX1,Y1,X2,Y2…,key:val…
arrow where (none, first, last, both) arrowshape shape capstyle style (butt, projecting, round) joinstyle style (miter, bevel, round) smooth smoothMethod (true or false) splinesteps number
# File lib/tkar/primitives.rb 120 def line args, key_args 121 arrow, arrowshape, capstyle, joinstyle, smooth, splinesteps = 122 key_args.values_at( 123 *%w{arrow arrowshape capstyle joinstyle smooth splinesteps}) 124 125 proc do |tkaroid, cos_r, sin_r| 126 x = tkaroid.x 127 y = tkaroid.y 128 params = tkaroid.params 129 130 coords = [] 131 points = args.map {|arg| arg[params] rescue arg} 132 points.each_slice(2) do |xv, yv| 133 coords << x + xv * cos_r - yv * sin_r 134 coords << y + xv * sin_r + yv * cos_r 135 end 136 137 config = {} 138 handle_generic_config(config, params, key_args) 139 140 if arrow 141 config[:arrow] = arrow[params] rescue arrow 142 end 143 if arrowshape 144 val = arrowshape[params] rescue arrowshape 145 val = val.split("+").map{|s| Integer(s)} 146 config[:arrowshape] = val 147 end 148 if capstyle 149 config[:capstyle] = capstyle[params] rescue capstyle 150 end 151 if joinstyle 152 config[:joinstyle] = joinstyle[params] rescue joinstyle 153 end 154 if smooth 155 config[:smooth] = smooth[params] rescue smooth 156 end 157 if splinesteps 158 config[:splinesteps] = splinesteps[params] rescue splinesteps 159 end 160 161 [TkcLine, coords, config] 162 end 163 end
oval(args, key_args)
click to toggle source
ovalX1,Y1,X2,Y2,key:val… standard keys
# File lib/tkar/primitives.rb 167 def oval args, key_args 168 proc do |tkaroid, cos_r, sin_r| 169 x = tkaroid.x 170 y = tkaroid.y 171 params = tkaroid.params 172 173 coords = [] 174 points = args.map {|arg| arg[params] rescue arg} 175 points.each_slice(2) do |xv, yv| 176 coords << x + xv * cos_r - yv * sin_r 177 coords << y + xv * sin_r + yv * cos_r 178 end 179 180 config = {} 181 handle_generic_config(config, params, key_args) 182 183 [TkcOval, coords, config] 184 end 185 end
poly(args, key_args)
click to toggle source
polyX1,Y1,X2,Y2,…key:val…
joinstyle style (miter, bevel, round) smooth smoothMethod (true or false) splinesteps number
# File lib/tkar/primitives.rb 214 def poly args, key_args 215 joinstyle, smooth, splinesteps = 216 key_args.values_at(*%w{joinstyle smooth splinesteps}) 217 218 proc do |tkaroid, cos_r, sin_r| 219 x = tkaroid.x 220 y = tkaroid.y 221 params = tkaroid.params 222 223 coords = [] 224 points = args.map {|arg| arg[params] rescue arg} 225 points.each_slice(2) do |xv, yv| 226 coords << x + xv * cos_r - yv * sin_r 227 coords << y + xv * sin_r + yv * cos_r 228 end 229 230 config = {} 231 handle_generic_config(config, params, key_args) 232 233 if joinstyle 234 config[:joinstyle] = joinstyle[params] rescue joinstyle 235 end 236 if smooth 237 config[:smooth] = smooth[params] rescue smooth 238 end 239 if splinesteps 240 config[:splinesteps] = splinesteps[params] rescue splinesteps 241 end 242 243 [TkcPolygon, coords, config] 244 end 245 end
polybox(args, key_args)
click to toggle source
just a very simple example!
# File lib/tkar/primitives.rb 344 def polybox args, key_args 345 dx, dy = args 346 347 # return a proc to make the info needed to instantiate/update 348 proc do |tkaroid, cos_r, sin_r| 349 x = tkaroid.x 350 y = tkaroid.y 351 params = tkaroid.params 352 353 ex = dx[params] rescue dx 354 ey = dy[params] rescue dy 355 356 points = 357 [ [ ex, ey], 358 [ ex, -ey], 359 [-ex, -ey], 360 [-ex, ey] ] 361 362 coords = [] 363 points.each do |xv, yv| 364 coords << x + xv * cos_r - yv * sin_r 365 coords << y + xv * sin_r + yv * cos_r 366 end 367 368 ## possible to skip below if no changes? 369 config = {} 370 handle_generic_config(config, params, key_args) 371 372 [TkcPolygon, coords, config] 373 end 374 end
rect(args, key_args)
click to toggle source
rectX1,Y1,X2,Y2,key:val… standard keys
# File lib/tkar/primitives.rb 189 def rect args, key_args 190 proc do |tkaroid, cos_r, sin_r| 191 x = tkaroid.x 192 y = tkaroid.y 193 params = tkaroid.params 194 195 coords = [] 196 points = args.map {|arg| arg[params] rescue arg} 197 points.each_slice(2) do |xv, yv| 198 coords << x + xv * cos_r - yv * sin_r 199 coords << y + xv * sin_r + yv * cos_r 200 end 201 202 config = {} 203 handle_generic_config(config, params, key_args) 204 205 [TkcRectangle, coords, config] 206 end 207 end
text(args, key_args)
click to toggle source
textX,Y,key:val…
anchor anchorPos (center, n, nw, ...) font fontName justify how (left, right, or center) text string (**) width lineLength
(**) text with embedded spaces can be specified only via a param command
# File lib/tkar/primitives.rb 256 def text args, key_args 257 anchor, font, justify, text = 258 key_args.values_at(*%w{anchor font justify text}) 259 # width handled by handle_generic_config 260 261 proc do |tkaroid, cos_r, sin_r| 262 x = tkaroid.x 263 y = tkaroid.y 264 params = tkaroid.params 265 266 xv, yv = args.map {|arg| arg[params] rescue arg} 267 coords = [ 268 x + xv * cos_r - yv * sin_r, 269 y + xv * sin_r + yv * cos_r 270 ] 271 272 config = {} 273 handle_generic_config(config, params, key_args) 274 275 if anchor 276 config[:anchor] = anchor[params] rescue anchor 277 end 278 if font 279 config[:font] = font[params] rescue font 280 end 281 if justify 282 config[:justify] = justify[params] rescue justify 283 end 284 if text 285 config[:text] = text[params] rescue text 286 end 287 288 [TkcText, coords, config] 289 end 290 end