summaryrefslogtreecommitdiffhomepage
path: root/samples/07_advanced_rendering/00_labels_with_wrapped_text/app/main.rb
blob: 71eefa9b7146686e0fac447261bce1c1056b5db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def tick args
  # defaults
  args.state.scroll_location  ||= 0
  args.state.textbox.messages ||= []
  args.state.textbox.scroll   ||= 0

  # render
  args.outputs.background_color = [0, 0, 0, 255]
  render_messages args
  render_instructions args

  # inputs
  if args.inputs.keyboard.key_down.one
    queue_message args, "Hello there neighbour! my name is mark, how is your day today?"
  end

  if args.inputs.keyboard.key_down.two
    queue_message args, "I'm doing great sir, actually I'm having a picnic today"
  end

  if args.inputs.keyboard.key_down.three
    queue_message args, "Well that sounds wonderful!"
  end

  if args.inputs.keyboard.key_down.home
    args.state.scroll_location = 1
  end

  if args.inputs.keyboard.key_down.delete
    clear_message_queue args
  end
end

def queue_message args, msg
  args.state.textbox.messages.concat msg.wrapped_lines 50
end

def clear_message_queue args
  args.state.textbox.messages = nil
  args.state.textbox.scroll = 0
end

def render_messages args
  args.outputs[:textbox].w = 400
  args.outputs[:textbox].h = 720

  args.outputs.primitives << args.state.textbox.messages.each_with_index.map do |s, idx|
    {
      x: 0,
      y: 20 * (args.state.textbox.messages.size - idx) + args.state.textbox.scroll * 20,
      text: s,
      size_enum: -3,
      alignment_enum: 0,
      r: 255, g:255, b: 255, a: 255
    }
  end

  args.outputs[:textbox].labels << args.state.textbox.messages.each_with_index.map do |s, idx|
    {
      x: 0,
      y: 20 * (args.state.textbox.messages.size - idx) + args.state.textbox.scroll * 20,
      text: s,
      size_enum: -3,
      alignment_enum: 0,
      r: 255, g:255, b: 255, a: 255
    }
  end

  args.outputs[:textbox].borders << [0, 0, args.outputs[:textbox].w, 720]

  args.state.textbox.scroll += args.inputs.mouse.wheel.y unless args.inputs.mouse.wheel.nil?

  if args.state.scroll_location > 0
    args.state.textbox.scroll = 0
    args.state.scroll_location = 0
  end

  args.outputs.sprites << [900, 0, args.outputs[:textbox].w, 720, :textbox]
end

def render_instructions args
  args.outputs.labels << [30,
                          30.from_top,
                          "press 1, 2, 3 to display messages, MOUSE WHEEL to scroll, HOME to go to top, BACKSPACE to delete.",
                          0, 255, 255]

  args.outputs.primitives << [0, 55.from_top, 1280, 30, :pixel, 0, 255, 0, 0, 0].sprite
end