|
| 1 | +#!/usr/bin/env jruby |
| 2 | +require 'picrate' |
| 3 | +# Displays 4 sketchy rectangles with different hachuring styles. |
| 4 | +# Version for propane by Martin Prout |
| 5 | +# Author Jo Wood. |
| 6 | +class PresetStyleDemo < Processing::App |
| 7 | + load_libraries :handy, :gicentre_utils |
| 8 | + java_import 'org.gicentre.handy.HandyPresets' |
| 9 | + java_import 'org.gicentre.handy.HandyRenderer' |
| 10 | + java_import 'org.gicentre.utils.move.ZoomPan' |
| 11 | + #***************************************************************************************** |
| 12 | + # Simple sketch to show handy shape drawing in four different present styles. H key toggles |
| 13 | + # sketchy rendering on or off. Left and right arrows change the hachure angle. Image zoomed |
| 14 | + # and panned with mouse drag. |
| 15 | + # @author Jo Wood, giCentre, City University London. |
| 16 | + # @version JRubyArt translated by Martin Prout |
| 17 | + # |
| 18 | + # ***************************************************************************************** |
| 19 | + |
| 20 | + # self file is part of Handy sketchy drawing library. Handy is free software: you can |
| 21 | + # redistribute it and/or modify it under the terms of the GNU Lesser General Public License |
| 22 | + # as published by the Free Software Foundation, either version 3 of the License, or (at your |
| 23 | + # option) any later version. |
| 24 | + # |
| 25 | + # Handy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY |
| 26 | + # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 27 | + # See the GNU Lesser General Public License for more details. |
| 28 | + # |
| 29 | + # You should have received a copy of the GNU Lesser General Public License along with self |
| 30 | + # source code (see COPYING.LESSER included with self source code). If not, see |
| 31 | + # http://www.gnu.org/licenses/. |
| 32 | + # |
| 33 | + |
| 34 | + # ----------------------------- Object variables ------------------------------ |
| 35 | + |
| 36 | + attr_reader :pencil, :marker, :water, :cPencil, :border, :zoomer, :angle |
| 37 | + attr_reader :isHandy, :sketchyFont, :normalFont |
| 38 | + |
| 39 | + # ---------------------------- Processing methods ----------------------------- |
| 40 | + def settings |
| 41 | + size(1200, 800) |
| 42 | + # Should work with all Processing 3 renderers. |
| 43 | + # size(1200, 800, P2D) |
| 44 | + # size(1200, 800, P3D) |
| 45 | + # size(1200, 800, FX2D) |
| 46 | + pixelDensity(displayDensity) # Use platform's maximum display density. |
| 47 | + end |
| 48 | + |
| 49 | + def setup |
| 50 | + sketch_title 'Preset Styles Demo' |
| 51 | + @zoomer = ZoomPan.new(self) |
| 52 | + @angle = -42 |
| 53 | + @isHandy = true |
| 54 | + @pencil = HandyPresets.create_pencil(self) |
| 55 | + @water = HandyPresets.create_water_and_ink(self) |
| 56 | + @marker = HandyPresets.create_marker(self) |
| 57 | + @cPencil = HandyPresets.create_coloured_pencil(self) |
| 58 | + @border = HandyRenderer.new(self) |
| 59 | + pencil.set_hachure_angle(angle) |
| 60 | + water.set_hachure_angle(angle) |
| 61 | + marker.set_hachure_angle(angle) |
| 62 | + cPencil.set_hachure_angle(angle) |
| 63 | + @sketchyFont = load_font(data_path('HumorSans-32.vlw')) |
| 64 | + @normalFont = create_font('sans-serif', 32) |
| 65 | + end |
| 66 | + |
| 67 | + def draw |
| 68 | + background(255) |
| 69 | + zoomer.transform |
| 70 | + pencil.set_seed(1234) |
| 71 | + water.set_seed(1234) |
| 72 | + marker.set_seed(1234) |
| 73 | + cPencil.set_seed(1234) |
| 74 | + stroke(0) |
| 75 | + stroke_weight(1) |
| 76 | + textAlign(RIGHT,BOTTOM) |
| 77 | + if isHandy |
| 78 | + text_font(sketchyFont) |
| 79 | + else |
| 80 | + text_font(normalFont) |
| 81 | + end |
| 82 | + srand(10) |
| 83 | + no_fill |
| 84 | + border.rect(10, 10, width / 2 - 20, height / 2 - 20) |
| 85 | + draw_shapes(pencil, 0, 0, width / 2, height / 2) |
| 86 | + fill(60) |
| 87 | + text('Pencil', width / 2 - 20, height / 2-15) |
| 88 | + no_fill |
| 89 | + border.rect(width / 2 + 10, 10,width / 2 - 20, height / 2 - 20) |
| 90 | + draw_shapes(water, width / 2, 0, width / 2, height / 2) |
| 91 | + fill(60) |
| 92 | + text('Ink and watercolour', width - 20,height / 2-15) |
| 93 | + no_fill |
| 94 | + border.rect(10, height / 2 + 10, width / 2 - 20, height / 2 - 20) |
| 95 | + draw_shapes(marker, 0, height / 2, width / 2, height / 2) |
| 96 | + fill(60) |
| 97 | + text('Marker pen', width / 2 - 20, height - 15) |
| 98 | + no_fill |
| 99 | + border.rect(width / 2 + 10, height / 2 + 10, width / 2 - 20, height / 2 - 20) |
| 100 | + draw_shapes(cPencil,width/2,height / 2,width/2,height / 2) |
| 101 | + fill(60) |
| 102 | + text('Coloured pencil', width - 20,height - 15) |
| 103 | + no_loop |
| 104 | + end |
| 105 | + |
| 106 | + def key_pressed |
| 107 | + case key |
| 108 | + when 'h', 'H' |
| 109 | + @isHandy = !isHandy |
| 110 | + pencil.setIsHandy(isHandy) |
| 111 | + water.setIsHandy(isHandy) |
| 112 | + marker.setIsHandy(isHandy) |
| 113 | + cPencil.setIsHandy(isHandy) |
| 114 | + loop |
| 115 | + when 'r', 'R' |
| 116 | + zoomer.reset |
| 117 | + loop |
| 118 | + else |
| 119 | + return unless key == CODED |
| 120 | + end |
| 121 | + case key_code |
| 122 | + when LEFT |
| 123 | + @angle -= 1 |
| 124 | + pencil.set_hachure_angle(angle) |
| 125 | + water.set_hachure_angle(angle) |
| 126 | + marker.set_hachure_angle(angle) |
| 127 | + cPencil.set_hachure_angle(angle) |
| 128 | + loop |
| 129 | + when RIGHT |
| 130 | + @angle += 1 |
| 131 | + pencil.set_hachure_angle(angle) |
| 132 | + water.set_hachure_angle(angle) |
| 133 | + marker.set_hachure_angle(angle) |
| 134 | + cPencil.set_hachure_angle(angle) |
| 135 | + loop |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + def mouse_dragged |
| 140 | + loop |
| 141 | + end |
| 142 | + |
| 143 | + private |
| 144 | + |
| 145 | + def draw_shapes(handy, x, y, w, h) |
| 146 | + minSize = w / 10 |
| 147 | + maxSize = min(w, h) / 4 |
| 148 | + 30.times do |
| 149 | + colour = color(rand(100..200), rand(60..200), rand(100..200), 120) |
| 150 | + fill(colour) |
| 151 | + shape_choice = rand |
| 152 | + if (shape_choice < 0.33) |
| 153 | + handy.rect(x + rand(minSize..w - maxSize),y + rand(minSize..h - maxSize),rand(minSize..maxSize), rand(minSize..maxSize)) |
| 154 | + elsif shape_choice < 0.66 |
| 155 | + x1 = x + rand(minSize..w - maxSize) |
| 156 | + y1 = y + rand(minSize..h - maxSize) |
| 157 | + x2 = x1 + rand(50..maxSize) |
| 158 | + y2 = y1 + rand(-10..10) |
| 159 | + x3 = (x1 + x2) / 2 |
| 160 | + y3 = y1 - rand(minSize..maxSize) |
| 161 | + handy.triangle(x1, y1, x2, y2, x3, y3) |
| 162 | + else |
| 163 | + handy.ellipse(x + rand(minSize..w - maxSize), y + rand(minSize..h - maxSize),rand(minSize..maxSize), rand(minSize..maxSize)) |
| 164 | + end |
| 165 | + end |
| 166 | + end |
| 167 | +end |
| 168 | + |
| 169 | +PresetStyleDemo.new |
0 commit comments