Created
October 17, 2023 17:41
-
-
Save xord/fb20f784c8ed1bd49a59586a2fe3195a to your computer and use it in GitHub Desktop.
rubysketch gem で作ったパラパラ漫画アプリ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SAVE_PATH = 'ToonData' | |
| class Button < Sprite | |
| def initialize(w: 100, h: 44, label: nil, rgb: [200] * 3, &block) | |
| super 0, 0, w, h | |
| @label, @rgb, @select = label, rgb, false | |
| pressing = false | |
| includeMouse = ->x, y { (0...self.w).include?(x) && (0..self.h).include?(y) } | |
| self.draw do | |
| round, offset = 12, 8 | |
| ww, hh = self.w, self.h - offset | |
| yy = pressing ? 6 : 0 | |
| if @rgb | |
| fill *@rgb.map { _1 - 20 } | |
| rect 0, offset, ww, hh, round | |
| fill *@rgb | |
| rect 0, yy, ww, hh, round | |
| end | |
| if select | |
| strokeWeight 3 | |
| stroke *(@rgb == [255, 255, 255] ? [0, 0, 0] : [255, 255, 255]) | |
| noFill | |
| rect 0, yy, ww, hh, round | |
| end | |
| if label | |
| textAlign CENTER, CENTER | |
| fill 0 | |
| text @label, 0, yy, ww, hh | |
| end | |
| end | |
| self.mousePressed do | |
| pressing = true | |
| end | |
| self.mouseReleased do | |
| block.call self if block && includeMouse[self.mouseX, self.mouseY] | |
| pressing = false | |
| end | |
| self.mouseDragged do | |
| pressing = includeMouse[self.mouseX, self.mouseY] | |
| end | |
| end | |
| attr_accessor :label, :rgb, :select | |
| end # Button | |
| class Space < Sprite | |
| def initialize(w: 1, h: 1) | |
| super 0, 0, w, h | |
| hide | |
| end | |
| end | |
| class Canvas < Sprite | |
| CHECKER = createShader nil, <<~END | |
| varying vec4 vertPosition; | |
| void main() { | |
| float x = mod(vertPosition.x, 32.) / 16. - 1.; | |
| float y = mod(vertPosition.y, 32.) / 16. - 1.; | |
| float c = x * y >= 0. ? 0.8 : 0.7; | |
| gl_FragColor = vec4(c, c, c, 1.); | |
| } | |
| END | |
| def initialize(width, height, zoom: 1) | |
| super 0, 0, width, height | |
| @width, @height, @zoom = width, height, zoom | |
| clear | |
| self.brushSize = 1 | |
| self.brushColor = [0, 0, 0] | |
| self.draw do | |
| push do | |
| shader CHECKER | |
| rect 0, 0, self.w, self.h | |
| end | |
| scale @zoom, @zoom | |
| drawImage self.image, 0, 0, @width, @height | |
| tint 255, 10 | |
| self.image(offset: -1)&.tap { drawImage _1, 0, 0, @width, @height } | |
| self.image(offset: +1)&.tap { drawImage _1, 0, 0, @width, @height } | |
| end | |
| self.mousePressed { brushStarted self.mouseX / @zoom, self.mouseY / @zoom } | |
| self.mouseReleased { brushEnded self.mouseX / @zoom, self.mouseY / @zoom } | |
| self.mouseDragged { brushMoved self.mouseX / @zoom, self.mouseY / @zoom } | |
| end | |
| attr_accessor :brushSize, :brushColor | |
| attr_reader :zoom, :frame, :playing, :brushSize, :brushColor | |
| alias playing? playing | |
| def image(offset: 0) | |
| frame = @frame + offset | |
| return nil if frame < 0 | |
| @images.insert frame, createFrameImage(@width, @height) if offset == 0 && @images[frame] == nil | |
| @images[frame] | |
| end | |
| def size() | |
| @images.size | |
| end | |
| def clear() | |
| stop | |
| @images, @frame = [], 0 | |
| end | |
| def play() | |
| return if playing? | |
| setInterval 0.2, id: :play do | |
| @frame += 1 | |
| @frame = 0 if @frame >= size | |
| end | |
| @playing = true | |
| end | |
| def stop() | |
| clearInterval :play | |
| @playing = false | |
| end | |
| def nextFrame(append: true) | |
| @frame += 1 if append || @frame + 1 < @images.size | |
| end | |
| def prevFrame() | |
| @frame -= 1 | |
| @frame = 0 if @frame < 0 | |
| end | |
| def drawPoint(x, y) | |
| image.beginDraw do |g| | |
| g.noFill | |
| g.strokeWeight @brushSize | |
| g.stroke *@brushColor | |
| g.point x, y | |
| end | |
| end | |
| def drawLine(x1, y1, x2, y2) | |
| image.beginDraw do |g| | |
| g.noFill | |
| g.strokeWeight @brushSize | |
| g.stroke *@brushColor | |
| g.line x1, y1, x2, y2 | |
| end | |
| end | |
| def brushStarted(x, y) | |
| drawPoint x, y | |
| @prevPoint = [x, y] | |
| end | |
| def brushEnded(x, y) | |
| @prevPoint = nil | |
| end | |
| def brushMoved(x, y) | |
| return unless @prevPoint | |
| drawLine *@prevPoint, x, y | |
| @prevPoint = [x, y] | |
| end | |
| def save(dir) | |
| dir = File.expand_path dir | |
| Dir.glob("#{dir}/image_*.png").each do |path| | |
| File.delete path | |
| end | |
| FileUtils.mkdir_p dir | |
| @images.each.with_index do |img, i| | |
| img.save "#{dir}/image_#{i}.png" | |
| end | |
| end | |
| def load(dir) | |
| dir = File.expand_path dir | |
| images = Dir.glob("#{dir}/image_*.png") | |
| .sort { _1[/(\d+)\.png$/, 1].to_i <=> _2[/(\d+)\.png$/, 1].to_i } | |
| .map { loadFrameImage _1 } | |
| raise "Invalid images" if images.any? { _1.width != @width || _1.height != @height } | |
| clear | |
| @images = images | |
| end | |
| private | |
| def createFrameImage(w, h) | |
| createGraphics(w, h).tap do |g| | |
| g.beginDraw do | |
| g.background 255 | |
| end | |
| end | |
| end | |
| def loadFrameImage(path) | |
| img = loadImage path | |
| createFrameImage(img.width, img.height).tap do |g| | |
| g.beginDraw do | |
| g.image img, 0, 0 | |
| end | |
| end | |
| end | |
| end # Canvas | |
| class App | |
| MARGIN = 8 | |
| def initialize() | |
| setTitle 'Toon!' | |
| windowResize 800, 600 | |
| noStroke | |
| @canvas = Canvas.new 160, 120, zoom: 3 | |
| @actions = [ | |
| Button.new(label: 'Play', rgb: [240, 180, 180]) { playOrStop _1 }, | |
| Space.new(h: MARGIN * 2), | |
| Button.new(label: 'Append', rgb: [180, 240, 180]) { @canvas.nextFrame }, | |
| Button.new(label: 'Next', h: 66, rgb: [180, 240, 180]) { @canvas.nextFrame append: false }, | |
| Button.new(label: 'Previous', h: 66, rgb: [180, 240, 180]) { @canvas.prevFrame }, | |
| Space.new(h: MARGIN * 2), | |
| Button.new(label: 'New', rgb: [240, 240, 180]) { @canvas.clear }, | |
| Button.new(label: 'Load', rgb: [240, 240, 180]) { @canvas.load SAVE_PATH }, | |
| Button.new(label: 'Save', rgb: [240, 240, 180]) { @canvas.save SAVE_PATH }, | |
| ] | |
| @tools = [1, 2, 3, 5, 10].map { |n| | |
| Button.new(label: "#{n}px", w: 44) { | |
| @canvas.brushSize = n | |
| updateSelections | |
| } | |
| } | |
| @colors = [0, 127, 191, 223, 255].map { |n| | |
| Button.new(w: 44, rgb: [n, n, n]) { | |
| @canvas.brushColor = [n, n, n] | |
| updateSelections | |
| } | |
| } | |
| @status = createStatus | |
| @sprites = [@canvas, *@actions, *@tools, *@colors, @status] | |
| @sprites.each { addSprite _1 } | |
| updateSelections | |
| end | |
| attr_reader :canvas | |
| def playOrStop(button) | |
| @canvas.playing? ? @canvas.stop : @canvas.play | |
| button.label = @canvas.playing? ? 'Stop' : 'Play' | |
| end | |
| def updateSelections() | |
| @tools.each { _1.select = _1.label == "#{@canvas.brushSize}px" } | |
| @colors.each { _1.select = _1.rgb == @canvas.brushColor } | |
| end | |
| def draw() | |
| background 100 | |
| sprite *@sprites | |
| end | |
| def resized() | |
| @actions.first.pos = [MARGIN, MARGIN] | |
| @actions.each_cons(2) { _2.pos = [MARGIN, _1.bottom + MARGIN] } | |
| @tools.first.pos = [@actions.first.right + MARGIN, MARGIN] | |
| @tools.each_cons(2) { _2.pos = [_1.right + MARGIN, _1.y] } | |
| @colors.first.pos = [@actions.first.right + MARGIN, @tools.first.bottom + MARGIN] | |
| @colors.each_cons(2) { _2.pos = [_1.right + MARGIN, _1.y] } | |
| @status.pos = [0, height - 22] | |
| @status.right = width | |
| @status.bottom = height | |
| @canvas.pos = [@actions.first.right + MARGIN, @colors.first.bottom + MARGIN] | |
| @canvas.right = width - MARGIN | |
| @canvas.bottom = @status.top - MARGIN | |
| end | |
| def keyPressed(key) | |
| case key | |
| when :right then @canvas.nextFrame | |
| when :left then @canvas.prevFrame | |
| when :space then @canvas.playing? ? @canvas.stop : @canvas.play | |
| end | |
| end | |
| private | |
| def createStatus() | |
| Sprite.new.tap do |sp| | |
| sp.draw do | |
| fill 200 | |
| rect 0, 0, sp.w, sp.y | |
| fill 0 | |
| textAlign LEFT, CENTER | |
| text "[ #{canvas.frame + 1} / #{canvas.size} ]", MARGIN, 0, sp.w, sp.h | |
| end | |
| end | |
| end | |
| end # App | |
| setup { $app = App.new } | |
| draw { $app.draw } | |
| windowResized { $app.resized if $app } | |
| # keyPressed {$app.keyPressed keyCode} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment