Last active
October 5, 2023 10:30
-
-
Save owenbutler/e3f6a39d4132350777092504aa135470 to your computer and use it in GitHub Desktop.
render target labels with motion
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
| class Text | |
| def initialize label_hash | |
| @l = label_hash.dup | |
| if !@l.key?(:vertical_alignment_enum) | |
| @l.vertical_alignment_enum = 0 | |
| end | |
| if !@l.key?(:blendmode_enum) | |
| @l.blendmode_enum = 0 | |
| end | |
| if !@l.key?(:font) | |
| @l.font = "font.ttf" | |
| end | |
| @x = @l.x | |
| @y = @l.y | |
| @l.x = 0 | |
| @l.y = 0 | |
| @width, @height = $gtk.args.gtk.calcstringbox(@l.text, @l.size_enum, @l.font) | |
| @id = "#{@l.text}-#{@l.size_enum}-#{@l.font}" | |
| @created_at = $gtk.args.tick_count | |
| # render the label into a render target at id | |
| $gtk.args.outputs[@id].w = @width | |
| $gtk.args.outputs[@id].h = @height | |
| $gtk.args.outputs[@id].transient! | |
| $gtk.args.outputs[@id].labels << @l | |
| end | |
| def slide_v(num: 50, dist: 50, diff: 5, duration: 60, repeat: false, easer: :smooth_stop_quart) | |
| slices = [] | |
| slice_width = @width / num | |
| if repeat && $gtk.args.tick_count > @created_at + (duration + diff * num) | |
| @created_at = $gtk.args.tick_count | |
| end | |
| num.times do |index| | |
| ease = $gtk.args.easing.ease(@created_at, $gtk.args.tick_count - index * diff, duration, easer) | |
| r = ease.remap(0, 1, -dist, 0) | |
| a = ease.remap(0, 1, 0, 255) | |
| slices << { | |
| x: @x + (slice_width * index), y: @y + r, | |
| w: slice_width, h: @height, | |
| path: @id, | |
| source_x: slice_width * index, source_w: slice_width, a: a | |
| } | |
| index += 1 | |
| end | |
| return slices | |
| end | |
| def slide_h(num: 10, dist: 100, diff: 5, duration: 60, repeat: false, easer: :smooth_stop_quart) | |
| slices = [] | |
| slice_height = @height / num | |
| if repeat && $gtk.args.tick_count > @created_at + (duration + diff * num) | |
| @created_at = $gtk.args.tick_count | |
| end | |
| num.times do |index| | |
| ease = $gtk.args.easing.ease(@created_at, $gtk.args.tick_count - index * diff, duration, easer) | |
| r = ease.remap(0, 1, -dist, 0) | |
| a = ease.remap(0, 1, 0, 255) | |
| slices << { | |
| x: @x + r, y: @y + (slice_height * index), | |
| w: @width, h: slice_height, | |
| path: @id, | |
| source_y: slice_height * index, source_h: slice_height, a: a | |
| } | |
| index += 1 | |
| end | |
| return slices | |
| end | |
| def wave_h(num: 10, amp: 5, freq: 10, diff: 5) | |
| slices = [] | |
| slice_height = @height / num | |
| num.times do |index| | |
| r = Math::sin(($gtk.args.tick_count + index * diff) / freq).remap(-1, 1, -amp, amp) | |
| slices << { | |
| x: @x + r, y: @y + (slice_height * index), | |
| w: @width, h: slice_height, | |
| path: @id, | |
| source_y: slice_height * index, source_h: slice_height, | |
| } | |
| index += 1 | |
| end | |
| return slices | |
| end | |
| def wave_v(num: 50, amp: 5, freq: 10, diff: 5) | |
| slices = [] | |
| slice_width = @width / num | |
| num.times do |index| | |
| r = Math::sin(($gtk.args.tick_count + index * diff) / freq).remap(-1, 1, -amp, amp) | |
| slice_index = slice_width * index | |
| slices << { | |
| x: @x + slice_index, y: @y + r, | |
| w: slice_width, h: @height, | |
| path: @id, | |
| source_x: slice_index, source_w: slice_width, | |
| } | |
| index += 1 | |
| end | |
| return slices | |
| end | |
| end | |
| def tick args | |
| if args.tick_count == 0 | |
| init args | |
| end | |
| args.outputs.background_color = [242, 243, 245] | |
| args.outputs.sprites << args.state.wavy_h.wave_h | |
| args.outputs.sprites << args.state.wavy_v.wave_v | |
| args.outputs.sprites << args.state.slide_h.slide_h(repeat: true) | |
| args.outputs.sprites << args.state.slide_h2.slide_h(dist: -100, repeat: true) | |
| args.outputs.sprites << args.state.slide_v.slide_v(dist: -50, repeat: true, diff: 2) | |
| args.outputs.sprites << args.state.slide_v2.slide_v(dist: 50, repeat: true, diff: 2) | |
| if args.inputs.keyboard.key_held.backspace | |
| args.outputs.primitives << args.gtk.framerate_diagnostics_primitives | |
| end | |
| end | |
| def init args | |
| args.state.wavy_h = Text.new( { | |
| x: 150, y: 600, text: "The quick brown wavy text", size_enum: 30, r: 0, g: 0, b: 0, | |
| } ) | |
| args.state.wavy_v = Text.new( { | |
| x: 150, y: 500, text: "The quick brown wavy vtext", size_enum: 30, r: 0, g: 0, b: 0, | |
| } ) | |
| args.state.slide_h = Text.new( { | |
| x: 150, y: 400, text: "The quick brown slidey text", size_enum: 30, r: 0, g: 0, b: 0, | |
| } ) | |
| args.state.slide_h2 = Text.new( { | |
| x: 150, y: 300, text: "The quick brown slidey text", size_enum: 30, r: 0, g: 0, b: 0, | |
| } ) | |
| args.state.slide_v = Text.new( { | |
| x: 150, y: 200, text: "The quick brown slidev text", size_enum: 30, r: 0, g: 0, b: 0, | |
| } ) | |
| args.state.slide_v2 = Text.new( { | |
| x: 150, y: 100, text: "The quick brown slidev text", size_enum: 30, r: 0, g: 0, b: 0, | |
| } ) | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @Vagab . Yeah, that definitely looks more readable. Thanks for the suggestion, I appreciate it!