summaryrefslogtreecommitdiffhomepage
path: root/dragon/console_menu.rb
blob: d10ceeb592956bf2b0b5b0fb50082ae9c8853edb (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright 2019 DragonRuby LLC
# MIT License
# console_menu.rb has been released under MIT (*only this file*).

module GTK
  class Console
    class Menu
      attr_accessor :buttons

      def initialize console
        @console = console
      end

      def record_clicked
        $recording.start 100
      end

      def replay_clicked
        $replay.start 'replay.txt'
      end

      def reset_clicked
        $gtk.reset
      end

      def scroll_up_clicked
        @console.scroll_up_half
      end

      def scroll_down_clicked
        @console.scroll_down_half
      end

      def show_menu_clicked
        @menu_shown = :visible
      end

      def close_clicked
        @menu_shown = :hidden
        @console.hide
      end

      def hide_menu_clicked
        @menu_shown = :hidden
      end

      def framerate_diagnostics_clicked
        @console.scroll_to_bottom
        $gtk.framerate_diagnostics
      end

      def itch_wizard_clicked
        @console.scroll_to_bottom
        $wizards.itch.start
      end

      def docs_clicked
        @console.scroll_to_bottom
        log Kernel.docs_classes
      end

      def scroll_end_clicked
        @console.scroll_to_bottom
      end

      def custom_buttons
        []
      end

      def tick args
        return unless @console.visible?

        @menu_shown ||= :hidden

        if $gtk.production
          @buttons = [
            (button id: :record,      row: 0, col:   9, text: "record gameplay",       method: :record_clicked),
            (button id: :replay,      row: 0, col:  10, text: "start replay",          method: :replay_clicked),
          ]
        elsif @menu_shown == :hidden
          @buttons = [
            (button id: :show_menu,       row: 0, col: 10, text: "show menu", method: :show_menu_clicked),
          ]
        else
          @buttons = [
            (button id: :scroll_up,   row: 0, col:  6, text: "scroll up",             method: :scroll_up_clicked),
            (button id: :scroll_down, row: 0, col:  7, text: "scroll down",           method: :scroll_down_clicked),
            (button id: :scroll_down, row: 0, col:  8, text: "scroll end",            method: :scroll_end_clicked),
            (button id: :close,       row: 0, col:  9, text: "close console",         method: :close_clicked),
            (button id: :hide,        row: 0, col: 10, text: "hide menu",             method: :hide_menu_clicked),

            (button id: :record,      row: 1, col:  7, text: "record gameplay",       method: :record_clicked),
            (button id: :replay,      row: 1, col:  8, text: "start replay",          method: :replay_clicked),
            (button id: :record,      row: 1, col:  9, text: "framerate diagnostics", method: :framerate_diagnostics_clicked),
            (button id: :reset,       row: 1, col: 10, text: "reset game",            method: :reset_clicked),

            (button id: :reset,       row: 2, col: 10, text: "docs",                  method: :docs_clicked),
            (button id: :reset,       row: 2, col:  9, text: "itch wizard",           method: :itch_wizard_clicked),
            *custom_buttons
          ]
        end

        # render
        args.outputs.reserved << @buttons.map { |b| b[:primitives] }

        # inputs
        if args.inputs.mouse.click
          clicked = @buttons.find { |b| args.inputs.mouse.inside_rect? b[:rect] }
          if clicked
            args.inputs.mouse.click = nil
            send clicked[:method]
          end
        end
      end

      def rect_for_layout row, col
        col_width  = 100
        row_height = 50
        col_margin = 5
        row_margin = 5
        x = (col_margin + (col * col_width)  + (col * col_margin))
        y = (row_margin + (row * row_height) + (row * row_margin) + row_height).from_top
        { x: x, y: y, w: col_width, h: row_height }
      end

      def button args
        id, row, col, text, method = args[:id], args[:row], args[:col], args[:text], args[:method]

        font_height = @console.font_style.line_height_px.half
        {
          id: id,
          rect: (rect_for_layout row, col),
          text: text,
          method: method
        }.let do |entity|
          primitives = []
          primitives << entity[:rect].merge(a: 164).solid
          primitives << entity[:rect].merge(r: 255, g: 255, b: 255).border
          primitives << text.wrapped_lines(5)
                            .map_with_index do |l, i|
                              [
                                entity[:rect][:x] + entity[:rect][:w].half,
                                entity[:rect][:y] + entity[:rect][:h].half + font_height - (i * (font_height + 2)),
                                l, -3, 1, 255, 255, 255
                              ]
                            end.labels

          entity.merge(primitives: primitives)
        end
      end

      def serialize
        {
          not_supported: "#{self}"
        }
      end
    end
  end
end