Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 326 lines (277 sloc) 9.467 kb
e0ac753 @alloy Use NSString drawAtPoint:attributes: because CGContextShowTextAtPoint do...
authored
1 # encoding: UTF-8
2
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
3 # requires to be put in the same directory as https://github.com/drtoast/macruby_graphics
4
5 framework 'Cocoa'
6 here = File.expand_path(File.dirname(__FILE__))
7 require File.join(here, 'graphics')
8
1bd2892 @alloy Rewrite it properly to use the RTF file.
authored
9 SLIDES = File.expand_path('../slides.rtf', __FILE__)
f62acfa @alloy Get actual slide text from slides.md file.
authored
10
10ff448 @alloy Render text at the top and horizontally centered.
authored
11 MARGIN = 20
12
0741f4d @alloy Render headings with inverted color scheme.
authored
13 TEXT_ATTRS = {
a8d997a @alloy Hack in idea to support all glyphs of the font. Read RTF instead of plai...
authored
14 #NSFontAttributeName => NSFont.fontWithName("FedraSansDisStd HeavyCond", size:200) ||
15 #NSFont.fontWithName("Helvetica Neue Condensed Black", size:200),
10ff448 @alloy Render text at the top and horizontally centered.
authored
16 NSParagraphStyleAttributeName => NSMutableParagraphStyle.new.tap { |p| p.alignment = NSCenterTextAlignment },
a47e0ca @alloy Reset text matrix explicitely, as it's not part of the graphics state.
authored
17 NSStrokeWidthAttributeName => -2, # negative value means stroke and fill, i.e. bordered
18 NSStrokeColorAttributeName => NSColor.whiteColor,
19 NSForegroundColorAttributeName => NSColor.blackColor,
a8d997a @alloy Hack in idea to support all glyphs of the font. Read RTF instead of plai...
authored
20 NSShadowAttributeName => NSShadow.new.tap { |s| s.shadowOffset = NSMakeSize(0, -5); s.shadowBlurRadius = 5 }
a47e0ca @alloy Reset text matrix explicitely, as it's not part of the graphics state.
authored
21 }
22
0741f4d @alloy Render headings with inverted color scheme.
authored
23 HEADING_ATTRS = TEXT_ATTRS.merge({
24 NSStrokeColorAttributeName => TEXT_ATTRS[NSForegroundColorAttributeName],
25 NSForegroundColorAttributeName => TEXT_ATTRS[NSStrokeColorAttributeName]
26 })
27
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
28 # there are a few problems with constants resolutions
29 # on the last MacRuby anyway so just do it simple
30 include MRGraphics
31
32 class SlideManager
1bd2892 @alloy Rewrite it properly to use the RTF file.
authored
33 def initialize(slides_rtf_file)
34 @slides = NSMutableAttributedString.alloc.initWithPath(slides_rtf_file, documentAttributes:nil)
35 _find_slide_locations
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
36 @current_slide = 0
f62acfa @alloy Get actual slide text from slides.md file.
authored
37 @backgrounds = []
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
38 end
39
40 def next_slide
1bd2892 @alloy Rewrite it properly to use the RTF file.
authored
41 @current_slide += 1 if @current_slide < @slide_locations.length - 1
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
42 end
43
44 def previous_slide
45 @current_slide -= 1 if @current_slide > 0
46 end
47
48 def current_slide
1bd2892 @alloy Rewrite it properly to use the RTF file.
authored
49 text = _current_slide
50 # is it a heading?
51 attrs = text.string.start_with?('# ') ? HEADING_ATTRS : TEXT_ATTRS
52 # get actual content
53 text = text.attributedSubstringFromRange(NSMakeRange(2, text.length - 2))
54 text.addAttributes(attrs, range:NSMakeRange(0, text.length))
55 text
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
56 end
f62acfa @alloy Get actual slide text from slides.md file.
authored
57
58 def current_background
59 @backgrounds[@current_slide % @backgrounds.size]
60 end
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
61
f62acfa @alloy Get actual slide text from slides.md file.
authored
62 def add_background(&block)
63 @backgrounds << block
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
64 end
1bd2892 @alloy Rewrite it properly to use the RTF file.
authored
65
66 private
67
68 def _current_slide
69 @slides.attributedSubstringFromRange(@slide_locations[@current_slide])
70 end
71
72 def _find_slide_locations
73 pointer = 0
74 @slide_locations = @slides.string.split("\n").map do |line|
75 start, pointer = pointer, (pointer + line.size + 1)
76 NSMakeRange(start, line.size)
77 end
78 end
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
79 end
80
81 class SlideView < NSView
82 attr_accessor :slide_manager
83
69cd6a7 @alloy Make window resizable and add very naive resizing smoothing.
authored
84 def viewWillStartLiveResize
85 @boundsBeforeResize = bounds
86 super
87 end
88
89 def viewDidEndLiveResize
90 @boundsBeforeResize = nil
91 super
92 end
93
edad154 @alloy Import some kittehs
authored
94 KITTEH_PICTURES = Dir.glob(File.expand_path('../kittehs/*.*', __FILE__)).map { |path| Image.new(path) }
95 def kittehPicture
96 KITTEH_PICTURES[rand(KITTEH_PICTURES.size)]
97 end
98
69cd6a7 @alloy Make window resizable and add very naive resizing smoothing.
authored
99 # As a simple way to support smooth resizing we cache the result image and
100 # draw it scaled to the current bounds.
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
101 def drawRect(rect)
69cd6a7 @alloy Make window resizable and add very naive resizing smoothing.
authored
102 unless inLiveResize
103 Canvas.for_rendering(:size => bounds.size) do |c|
104 CGContextSetTextMatrix(c.ctx, CGAffineTransformIdentity)
f62acfa @alloy Get actual slide text from slides.md file.
authored
105 c.instance_eval(&slide_manager.current_background)
edad154 @alloy Import some kittehs
authored
106 c.reset
107 kittehPicture.draw(c.ctx, 50, 50, 200, 200)
108 kittehPicture.draw(c.ctx, bounds.size.width - 250, 50, 200, 200)
69cd6a7 @alloy Make window resizable and add very naive resizing smoothing.
authored
109 @renderCache = c.ciimage
110 end
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
111 end
69cd6a7 @alloy Make window resizable and add very naive resizing smoothing.
authored
112 cicontext = CIContext.contextWithCGContext(NSGraphicsContext.currentContext.graphicsPort, options:nil)
113 cicontext.drawImage(@renderCache, inRect:bounds, fromRect:@boundsBeforeResize || bounds)
e0ac753 @alloy Use NSString drawAtPoint:attributes: because CGContextShowTextAtPoint do...
authored
114
f62acfa @alloy Get actual slide text from slides.md file.
authored
115 text = slide_manager.current_slide
1bd2892 @alloy Rewrite it properly to use the RTF file.
authored
116 text.drawInRect(NSInsetRect(bounds, MARGIN, MARGIN), withAttributes:nil)
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
117 end
118
119 def keyDown(key)
120 case key.keyCode
121 when KVK_Escape
122 exit
123 when KVK_LeftArrow
124 slide_manager.previous_slide
a720c5e @alloy Get rid of constant size and use the full screen.
authored
125 self.needsDisplay = true
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
126 when KVK_RightArrow
127 slide_manager.next_slide
a720c5e @alloy Get rid of constant size and use the full screen.
authored
128 self.needsDisplay = true
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
129 else
130 case key.characters
131 when "f"
132 if @fullscreen
133 exitFullScreenModeWithOptions nil
134 window.makeFirstResponder self
135 else
136 enterFullScreenMode NSScreen.mainScreen, withOptions: {NSFullScreenModeAllScreens => false}
137 end
138 @fullscreen = !@fullscreen
139 when "r"
a720c5e @alloy Get rid of constant size and use the full screen.
authored
140 self.needsDisplay = true
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
141 end
142 end
143 end
144 end
145
f62acfa @alloy Get actual slide text from slides.md file.
authored
146 slide_manager = SlideManager.new(SLIDES)
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
147
f62acfa @alloy Get actual slide text from slides.md file.
authored
148 slide_manager.add_background do
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
149 # based on canvas_example.rb
255d85e @alloy Add border around text and a drop shadow.
authored
150 background Color.random
e0ac753 @alloy Use NSString drawAtPoint:attributes: because CGContextShowTextAtPoint do...
authored
151 foregroundColor = Color.random
152 fill foregroundColor
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
153 stroke 0.2
154 stroke_width 1
155 font "Zapfino"
156
157 80.times do
158 font_size rand(170)
e0ac753 @alloy Use NSString drawAtPoint:attributes: because CGContextShowTextAtPoint do...
authored
159 fill foregroundColor.copy.darken(rand(0.8))
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
160 letters = %W{ m a c r u b y }
a720c5e @alloy Get rid of constant size and use the full screen.
authored
161 text(letters[rand(letters.size)], rand(width), rand(height))
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
162 end
163 end
164
f62acfa @alloy Get actual slide text from slides.md file.
authored
165 slide_manager.add_background do
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
166 # based on drawing_iterate_example.rb
255d85e @alloy Add border around text and a drop shadow.
authored
167 background Color.random
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
168
169 # create a petal shape with base at (0,0), size 40×150, and bulge at 30px
170 shape = Path.new
171 shape.petal(0,0,40,150,30)
172 # add a circle
173 shape.oval(-10,20,20,20)
174 # color it red
255d85e @alloy Add border around text and a drop shadow.
authored
175 shape.fill Color.random
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
176
177 # increment shape parameters by the specified amount each iteration,
178 # or by a random value selected from the specified range
179 shape.increment(:rotation, 5.0)
180 shape.increment(:scale_x, 0.99)
181 shape.increment(:scale_y, 0.96)
182 shape.increment(:x, 10.0)
183 shape.increment(:y, 12.0)
184 shape.increment(:hue,-0.02..0.02)
185 shape.increment(:saturation, -0.1..0.1)
186 shape.increment(:brightness, -0.1..0.1)
187 shape.increment(:alpha, -0.1..0.1)
188
189 # draw 200 petals on the canvas
a720c5e @alloy Get rid of constant size and use the full screen.
authored
190 translate(width/2-150, height/2+20)
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
191 draw(shape,0,0,200)
192 end
193
f62acfa @alloy Get actual slide text from slides.md file.
authored
194 slide_manager.add_background do
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
195 # based on spirograph_example.rb
255d85e @alloy Add border around text and a drop shadow.
authored
196 background Color.random
197 fill Color.random
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
198 font 'Book Antiqua'
199 font_size 12
a720c5e @alloy Get rid of constant size and use the full screen.
authored
200 translate width/2, height/2
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
201
202 # rotate, draw text, repeat
203 180.times do |frame|
204 new_state do
205 rotate((frame*2) + 120)
206 translate(70,0)
207 text('going...', 80, 0)
208 rotate(frame * 6)
209 text('Around and', 20, 0)
210 end
211 end
212 end
213
badcb95 @vincentisambart Make the code is a bit more ugly but faster
vincentisambart authored
214 red_colors = nil
215 blue_colors = nil
8d1f3fc @alloy Add two more backgrounds
authored
216 slide_manager.add_background do
217 c = self
218 c.background(Color.random)
219
220 # create flower head shape
221 head = Path.new.oval(0,0,10,10,:center)
222 petals = 3
223 petals.times do
224 head.rotate(360/petals)
225 head.oval(0,10,5,5,:center)
226 head.oval(0,17,2,2,:center)
227 end
228 # randomize head attributes
229 head.randomize(:fill, red_colors)
230 head.randomize(:stroke, blue_colors)
231 head.randomize(:scale, 0.2..2.0)
232 head.randomize(:rotation, 0..360)
233
234 # create particles
235 numparticles = 200
236 numframes = 200
237 particles = []
238 numparticles.times do |i|
239 # start particle at random point at bottom of canvas
240 x = MRGraphics.random(c.width/2 - 50, c.width/2 + 50)
241 p = Particle.new(x,0)
242 p.velocity_x = MRGraphics.random(-0.5,0.5) # set initial x velocity
243 p.velocity_y = MRGraphics.random(1.0,3.0) # set initial y velocity
244 p.acceleration = 0.1 # set drag or acceleration
245 particles[i] = p # add particle to array
246 end
247
248 # animate particles
249 numframes.times do |frame|
250 numparticles.times do |i|
251 particles[i].move
252 end
253 end
254
255 # draw particle trails and heads
256 numparticles.times do |i|
257 c.push
258 # choose a stem color
259 color = MRGraphics.choose(blue_colors).a(0.7).analog(20,0.7)
260 c.stroke(color)
261 c.stroke_width(MRGraphics.random(0.5,2.0))
262
263 # draw the particle
264 particles[i].draw(c)
265
266 # go to the last particle position and draw the flower head
267 c.translate(particles[i].points[-1][0],particles[i].points[-1][1])
268 c.draw(head)
269 c.pop
270 end
271 end
272
273 slide_manager.add_background do
274 canvas = self
275
276 # choose a random color and set the background to a darker variant
277 clr = Color.random.a(0.5)
278 canvas.background(clr.copy.darken(0.6))
279
280 # create a new rope with 200 fibers
badcb95 @vincentisambart Make the code is a bit more ugly but faster
vincentisambart authored
281 rope = Rope.new(canvas, :width => 100, :fibers => 30, :stroke_width => 0.4, :roundness => 3.0)
8d1f3fc @alloy Add two more backgrounds
authored
282
283 # randomly rotate the canvas from its center
284 canvas.translate(canvas.width/2, canvas.height/2)
285 canvas.rotate(MRGraphics.random(0, 360))
286 canvas.translate(-canvas.width/2, -canvas.height/2)
287
288 # draw 20 ropes
289 ropes = 10
290 ropes.times do
291 canvas.stroke(clr.copy.analog(20, 0.8)) # rotate hue up to 20 deg left/right, vary brightness/saturation by up to 70%
292 rope.x0 = -100 # start rope off bottom left of canvas
293 rope.y0 = -100
294 rope.x1 = canvas.width + 100 # end rope off top right of canvas
295 rope.y1 = canvas.height + 100
296 rope.hair # draw rope in organic “hair” style
297 end
298 end
299
a720c5e @alloy Get rid of constant size and use the full screen.
authored
300 frame = NSScreen.mainScreen.visibleFrame
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
301 app = NSApplication.sharedApplication
302 app.setActivationPolicy NSApplicationActivationPolicyRegular
303 app.activateIgnoringOtherApps true
304 view = SlideView.alloc.initWithFrame(frame)
305 view.slide_manager = slide_manager
306
307 window = NSWindow.alloc.initWithContentRect(frame,
69cd6a7 @alloy Make window resizable and add very naive resizing smoothing.
authored
308 styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask,
309 backing:NSBackingStoreBuffered,
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
310 defer:false)
311 window.contentView = view
312 window.makeFirstResponder view
313 window.center
314 window.display
315 window.makeKeyAndOrderFront(nil)
316 window.orderFrontRegardless
317
badcb95 @vincentisambart Make the code is a bit more ugly but faster
vincentisambart authored
318 # load images and grab colors
319 HERE = File.expand_path("../examples", __FILE__)
320 img = Image.new(File.join(HERE, 'images', 'italy.jpg')).saturation(1.9)
321 red_colors = img.colors(100)
322 img = Image.new(File.join(HERE, 'images', 'v2.jpg')).saturation(1.9)
323 blue_colors = img.colors(100)
324
11c9862 @alloy Add Vincent's slides codem, add a blur to rendered background, and draw ...
authored
325 app.run
Something went wrong with that request. Please try again.