hokusai-zero 0.2.6.pre.pinephone4 → 0.2.6.pre.pinephone6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hokusai
2
4
  class KeyboardEvent < Event
3
5
  extend Forwardable
4
6
 
5
7
  def_delegators :@keyboard, :shift, :super, :ctrl,
6
- :alt, :pressed, :pressed_len, :released,
7
- :released_len
8
+ :alt, :pressed, :released, :char, :code
8
9
 
9
10
  attr_reader :input
10
11
 
@@ -35,48 +36,40 @@ module Hokusai
35
36
  class KeyUpEvent < KeyboardEvent
36
37
  name "keyup"
37
38
 
38
- def keyboard_key
39
- input.raw[:keyboard][:released]
40
- end
41
-
42
39
  def key
43
- keyboard_key[:key][:key]
40
+ released[0]&.[](:symbol)
44
41
  end
45
42
 
46
43
  def code
47
- keyboard_key[:char_code]
44
+ released[0]&.[](:code)
48
45
  end
49
46
 
50
47
  def char
51
- code.chr
48
+ released[0]&.[](:char)
52
49
  end
53
50
 
54
51
  def capture(block, _)
55
- captures << block if matches(block) && released_len > 0
52
+ captures << block if matches(block) && released.size > 0
56
53
  end
57
54
  end
58
55
 
59
56
  class KeyPressEvent < KeyboardEvent
60
57
  name "keypress"
61
58
 
62
- def keyboard_key
63
- input.raw[:keyboard][:pressed]
64
- end
65
-
66
59
  def key
67
- keyboard_key[:key][:key]
60
+ pressed[0]&.[](:symbol)
68
61
  end
69
62
 
70
63
  def code
71
- keyboard_key[:char_code]
64
+ pressed[0]&.[](:code)
72
65
  end
73
66
 
74
67
  def char
75
- code.chr
68
+ pressed[0]&.[](:char)
76
69
  end
77
70
 
78
71
  def capture(block, _)
79
- return unless matches(block) && pressed_len > 0
72
+ return unless matches(block) && pressed.size > 0
80
73
 
81
74
  captures << block
82
75
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hokusai
2
4
  class MouseEvent < Event
3
5
  extend Forwardable
@@ -67,7 +69,7 @@ module Hokusai
67
69
  protected
68
70
 
69
71
  def hovered(canvas)
70
- LibHokusai.hoku_input_is_hovered(input.raw, canvas.to_hoku_rect)
72
+ input.hovered?(canvas)
71
73
  end
72
74
  end
73
75
 
@@ -85,19 +87,19 @@ module Hokusai
85
87
  name "click"
86
88
 
87
89
  def capture(block, canvas)
88
- if left[:clicked] && clicked(canvas)
90
+ if left.clicked && clicked(canvas)
89
91
  block.node.meta.focus
90
92
 
91
93
  if matches(block)
92
94
  captures << block
93
95
  end
94
- elsif left[:clicked]
96
+ elsif left.clicked
95
97
  block.node.meta.blur
96
98
  end
97
99
  end
98
100
 
99
101
  def clicked(canvas)
100
- LibHokusai.hoku_input_is_clicked(input.raw, canvas.to_hoku_rect)
102
+ left.clicked && input.hovered?(canvas)
101
103
  end
102
104
  end
103
105
 
@@ -105,7 +107,7 @@ module Hokusai
105
107
  name "mouseup"
106
108
 
107
109
  def capture(block, _)
108
- if left[:up] && matches(block)
110
+ if left.up && matches(block)
109
111
  captures << block
110
112
  end
111
113
  end
@@ -115,7 +117,7 @@ module Hokusai
115
117
  name "mousedown"
116
118
 
117
119
  def capture(block, _)
118
- if left[:down] && matches(block)
120
+ if left.down && matches(block)
119
121
  captures << block
120
122
  end
121
123
  end
@@ -168,13 +170,13 @@ module Hokusai
168
170
  def capture(block, canvas)
169
171
  captures << block if matches(block)
170
172
 
171
- if left[:clicked] && !clicked(canvas)
173
+ if left.clicked && !clicked(canvas)
172
174
  block.node.meta.blur
173
175
  end
174
176
  end
175
177
 
176
178
  def clicked(canvas)
177
- LibHokusai.hoku_input_is_clicked(input.raw, canvas.to_hoku_rect)
179
+ left.clicked && input.hovered?(canvas)
178
180
  end
179
181
  end
180
182
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Hokusai
2
4
  class TouchEvent < Event
3
5
  extend Forwardable
@@ -96,7 +96,7 @@ module Hokusai
96
96
  root_entry = PainterEntry.new(root, canvas.x, canvas.y, canvas.width, canvas.height)
97
97
  groups << [root_entry, measure(root_children, canvas)]
98
98
 
99
- mouse_y = input.mouse.pos[:y]
99
+ mouse_y = input.mouse.pos.y
100
100
  can_capture = mouse_y >= (canvas.y || 0.0) && mouse_y <= (canvas.y || 0.0) + canvas.height
101
101
 
102
102
  hovered = false
@@ -127,7 +127,7 @@ module Hokusai
127
127
 
128
128
  canvas.reset(entry.x, entry.y, entry.w, entry.h)
129
129
 
130
- before_render&.call([group.block, group.parent], canvas, input.raw)
130
+ before_render&.call([group.block, group.parent], canvas, input)
131
131
 
132
132
  if capture
133
133
  capture_events(group.block, canvas, hovered: hovered)
@@ -267,10 +267,7 @@ module Hokusai
267
267
  return
268
268
  end
269
269
 
270
- rect = canvas.to_hoku_rect
271
- block_is_hovered = LibHokusai.hoku_input_is_hovered(input.raw, rect)
272
-
273
- if block_is_hovered
270
+ if input.hovered?(canvas)
274
271
  events[:hover].capture(block, canvas)
275
272
  events[:click].capture(block, canvas)
276
273
  events[:wheel].capture(block, canvas)
@@ -281,7 +278,7 @@ module Hokusai
281
278
  end
282
279
  events[:mousemove].capture(block, canvas)
283
280
 
284
- if block_is_hovered || block.node.meta.focused || input.keyboard_override
281
+ if input.hovered?(canvas) || block.node.meta.focused || input.keyboard_override
285
282
  events[:keyup].capture(block, canvas)
286
283
  events[:keypress].capture(block, canvas)
287
284
  end
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hokusai
4
+ Outline = Struct.new(:top, :right, :bottom, :left) do
5
+ def self.default
6
+ new(0.0, 0.0, 0.0, 0.0)
7
+ end
8
+
9
+ def hash
10
+ [self.class, top, right, bottom, left].hash
11
+ end
12
+
13
+ def self.convert(value)
14
+ case value
15
+ when String
16
+ if value =~ /,/
17
+ convert(value.split(",").map(&:to_f))
18
+ else
19
+ convert(value.to_f)
20
+ end
21
+ when Float
22
+ new(value, value, value, value)
23
+ when Array
24
+ new(value[0] || 0.0, value[1] || 0.0, value[2] || 0.0, value[3] || 0.0)
25
+ when Outline
26
+ value
27
+ end
28
+ end
29
+
30
+ def present?
31
+ top > 0.0 || right > 0.0 || bottom > 0.0 || left > 0.0
32
+ end
33
+
34
+ def uniform?
35
+ top == right && top == bottom && top == left
36
+ end
37
+ end
38
+
39
+ class Padding
40
+ attr_reader :top, :left, :right, :bottom
41
+ def initialize(top, right, bottom, left)
42
+ @top = top
43
+ @left = left
44
+ @right = right
45
+ @bottom = bottom
46
+ end
47
+
48
+ alias_method :t, :top
49
+ alias_method :l, :left
50
+ alias_method :r, :right
51
+ alias_method :b, :bottom
52
+
53
+ def width
54
+ right + left
55
+ end
56
+
57
+ def height
58
+ top + bottom
59
+ end
60
+
61
+ def self.convert(value)
62
+ case value
63
+ when String
64
+ if value =~ /,/
65
+ convert(value.split(",").map(&:to_f))
66
+ else
67
+ convert(value.to_i)
68
+ end
69
+ when Integer
70
+ new(value, value, value, value)
71
+ when Array
72
+ new(value[0], value[1], value[2], value[3])
73
+ when Padding
74
+ value
75
+ else
76
+ raise Hokusai::Error.new("Unsupported conversion type #{value.class} for Hokusai::Padding")
77
+ end
78
+ end
79
+
80
+ def hash
81
+ [self.class, top, right, bottom, left].hash
82
+ end
83
+ end
84
+
85
+ class Canvas
86
+ attr_accessor :width, :height, :x, :y, :vertical, :reverse
87
+ def initialize(width, height, x = 0.0, y = 0.0, vertical = true, reverse = false)
88
+ @width = width
89
+ @height = height
90
+ @x = x
91
+ @y = y
92
+ @vertical = vertical
93
+ @reverse = reverse
94
+ end
95
+
96
+ def reset(x, y, width, height, vertical: true, reverse: false)
97
+ self.x = x
98
+ self.y = y
99
+ self.width = width
100
+ self.height = height
101
+ self.vertical = vertical
102
+ self.reverse = reverse
103
+ end
104
+
105
+ def to_bounds
106
+ Hokusai::Rect.new(x, y, width, height)
107
+ end
108
+
109
+ def hovered?(input)
110
+ input.hovered?(self)
111
+ end
112
+
113
+ def reverse?
114
+ reverse
115
+ end
116
+ end
117
+
118
+ # Color = Struct.new(:red, :green, :blue, :alpha) do
119
+ class Color
120
+ attr_reader :red, :green, :blue, :alpha
121
+ def initialize(red, green, blue, alpha = 255)
122
+ @red = red.freeze
123
+ @green = green.freeze
124
+ @blue = blue.freeze
125
+ @alpha = alpha.freeze
126
+ end
127
+
128
+ alias_method :r, :red
129
+ alias_method :b, :blue
130
+ alias_method :g, :green
131
+ alias_method :a, :alpha
132
+
133
+ def self.convert(value)
134
+ case value
135
+ when String
136
+ value = value.split(",").map(&:to_i)
137
+ when Array
138
+ when Color
139
+ return value
140
+ else
141
+ raise Hokusai::Error.new("Unsupported conversion type #{value.class} for Hokusai::Color")
142
+ end
143
+
144
+ new(value[0], value[1], value[2], value[3] || 255)
145
+ end
146
+
147
+ def hash
148
+ [self.class, r, g, b, a].hash
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hokusai
4
+ KEY_CODES = {
5
+ null: 0, apostrophe: 39, comma: 44, minus: 45, period: 46,
6
+ slash: 47, zero: 48, one: 49, two: 50, three: 51, four: 52,
7
+ five: 53, six: 54, seven: 55, eight: 56, nine: 57, semicolon: 59,
8
+ equal: 61, a: 65, b: 66, c: 67, d: 68, e: 69, f: 70, g: 71, h: 72,
9
+ i: 73, j: 74, k: 75, l: 76, m: 77, n: 78, o: 79, p: 80, q: 81, r: 82,
10
+ s: 83, t: 84, u: 85, v: 86, w: 87, x: 88, y: 89, z: 90, left_bracket: 91,
11
+ backslash: 92, right_bracket: 93, grave: 96, space: 32, escape: 256,
12
+ enter: 257, tab: 258, backspace: 259, insert: 260, delete: 261, right: 262,
13
+ left: 263, down: 264, up: 265, page_up: 266, page_down: 267, home: 268, end: 269,
14
+ caps_lock: 280, scroll_lock: 281, num_lock: 282, print_screen: 283, pause: 284,
15
+ f1: 290, f2: 291, f3: 292, f4: 293, f5: 294, f6: 295, f7: 296, f8: 297,
16
+ f9: 298, f10: 299, f11: 300, f12: 301, left_shift: 340, left_control: 341,
17
+ left_alt: 342, left_super: 343, right_shift: 344, right_control: 345, right_alt: 346,
18
+ right_super: 347, kb_menu: 348, kp_0: 320, kp_1: 321, kp_2: 322, kp_3: 323,
19
+ kp_4: 324, kp_5: 325, kp_6: 326, kp_7: 327, kp_8: 328, kp_9: 329, kp_decimal: 330,
20
+ kp_divide: 331, kp_multiply: 332, kp_subtract: 333, kp_add: 334, kp_enter: 335,
21
+ kp_equal: 336, back: 4, menu: 5, volume_up: 24, volume_down: 25
22
+ }
23
+
24
+ class Keyboard
25
+ attr_accessor :shift, :control, :super, :alt
26
+ attr_reader :keys, :pressed, :released
27
+
28
+ def initialize
29
+ @shift = false
30
+ @control = false
31
+ @super = false
32
+ @alt = false
33
+
34
+ @keys = {}
35
+ @pressed = []
36
+ @released = []
37
+
38
+ # populate the key states
39
+ KEY_CODES.each do |symbol, code|
40
+ @keys[symbol] = { code: code, symbol: symbol, up: false, down: false, pressed: false, released: false }
41
+ end
42
+ end
43
+
44
+ def code
45
+ pressed[0]&.[](:code)
46
+ end
47
+
48
+ def char
49
+ pressed[0]&.[](:char)
50
+ end
51
+
52
+ def ctrl
53
+ @control
54
+ end
55
+
56
+ def reset
57
+ @pressed.clear
58
+ @released.clear
59
+
60
+ @shift = false
61
+ @control = false
62
+ @super = false
63
+ @alt = false
64
+ end
65
+
66
+
67
+ def key_is_letter?(symbol)
68
+ symbol == :a || symbol == :b || symbol == :c || symbol == :d ||
69
+ symbol == :e || symbol == :f || symbol == :g || symbol == :h ||
70
+ symbol == :i || symbol == :j || symbol == :k || symbol == :l ||
71
+ symbol == :m || symbol == :n || symbol == :o || symbol == :p ||
72
+ symbol == :q || symbol == :r || symbol == :s || symbol == :t ||
73
+ symbol == :u || symbol == :v || symbol == :w || symbol == :x ||
74
+ symbol == :y || symbol == :z
75
+ end
76
+
77
+ def char_code_from_key(key, shift)
78
+ code = keys[key][:code]
79
+
80
+ if !shift && key_is_letter?(key)
81
+ code += 32
82
+ elsif shift && key == :apostrophe
83
+ code = 34
84
+ elsif shift && key == :comma
85
+ code = 60
86
+ elsif shift && key == :minus
87
+ code = 95
88
+ elsif shift && key == :period
89
+ code = 62
90
+ elsif shift && key == :slash
91
+ code = 63
92
+ elsif shift && key == :zero
93
+ code = 41
94
+ elsif shift && key == :one
95
+ code = 33
96
+ elsif shift && key == :two
97
+ code = 64
98
+ elsif shift && key == :three
99
+ code = 35
100
+ elsif shift && key == :four
101
+ code = 36
102
+ elsif shift && key == :five
103
+ code = 37
104
+ elsif shift && key == :six
105
+ code = 94
106
+ elsif shift && key == :seven
107
+ code = 38
108
+ elsif shift && key == :eight
109
+ code = 42
110
+ elsif shift && key == :nine
111
+ code = 40
112
+ elsif shift && key == :semicolon
113
+ code = 58
114
+ elsif shift && key == :equal
115
+ code = 43
116
+ elsif shift && key == :left_bracket
117
+ code = 123
118
+ elsif shift && key == :backslash
119
+ code = 124
120
+ elsif shift && key == :right_bracket
121
+ code = 125
122
+ elsif shift && key == :grave
123
+ code = 126
124
+ end
125
+
126
+ return code if code <= 256
127
+ end
128
+
129
+ def set(key, down)
130
+ if down
131
+ case key
132
+ when :left_shift, :right_shift
133
+ @shift = true
134
+ when :left_control, :right_control
135
+ @control = true
136
+ when :left_super, :right_super
137
+ @super = true
138
+ when :left_alt, :right_alt
139
+ @alt = true
140
+ end
141
+ end
142
+
143
+ if down && keys[key][:up]
144
+ keys[key][:pressed] = true
145
+ keys[key][:released]= false
146
+
147
+ nkey = keys[key].dup
148
+ nkey.merge!({ char: char_code_from_key(key, shift)&.chr })
149
+
150
+ pressed << nkey
151
+ elsif !down && keys[key][:down]
152
+ keys[key][:pressed] = false
153
+ keys[key][:released] = true
154
+
155
+ nkey = keys[key].dup
156
+ nkey.merge!({ char: char_code_from_key(key, shift)&.chr })
157
+
158
+ released << nkey
159
+ else
160
+ keys[key][:pressed] = false
161
+ keys[key][:released] = false
162
+ end
163
+
164
+ keys[key][:down] = down
165
+ keys[key][:up] = !down
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hokusai
4
+ class MouseButton
5
+ attr_accessor :up, :down, :clicked, :released
6
+
7
+ def initialize
8
+ @up = false
9
+ @down = false
10
+ @clicked = false
11
+ @released = false
12
+ end
13
+ end
14
+
15
+ class Mouse
16
+ attr_reader :pos, :delta, :left, :right, :middle, :scroll
17
+ attr_accessor :scroll_delta
18
+
19
+ def initialize
20
+ @pos = Vec2.new(0.0, 0.0)
21
+ @delta = Vec2.new(0.0, 0.0)
22
+ @scroll = 0.0
23
+ @scroll_delta = 0.0
24
+ @left = MouseButton.new
25
+ @middle = MouseButton.new
26
+ @right = MouseButton.new
27
+ end
28
+
29
+ def scroll=(val)
30
+ last = scroll
31
+ new_y = (last >= val) ? last - val : val - last
32
+ self.scroll_delta = new_y
33
+ @scroll = val
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hokusai
4
+ class Vec2
5
+ attr_accessor :x, :y
6
+ def initialize(x, y)
7
+ @x = x
8
+ @y = y
9
+ end
10
+ end
11
+
12
+ class Rect
13
+ attr_accessor :x, :y, :width, :height
14
+
15
+ def initialize(x, y, width, height)
16
+ @x = x
17
+ @y = y
18
+ @width = width
19
+ @height = height
20
+ end
21
+ def to_hoku_rect
22
+ @hoku_rect ||= LibHokusai::HmlRect.create(x, y, width, height)
23
+ end
24
+
25
+ def includes_y?(y)
26
+ LibHokusai.hoku_rect_includes_y(to_hoku_rect, y)
27
+ end
28
+
29
+ def includes_x?(x)
30
+ LibHokusai.hoku_rect_includes_x(to_hoku_rect, x)
31
+ end
32
+
33
+ def move_x_left(times = 1)
34
+ LibHokusai.hoku_rect_x_left(to_hoku_rect, times)
35
+ end
36
+
37
+ def move_x_right(times = 1)
38
+ LibHokusai.hoku_rect_x_right(to_hoku_rect, times)
39
+ end
40
+
41
+ def move_y_up(times = 1)
42
+ LibHokusai.hoku_rect_y_up(to_hoku_rect, times)
43
+ end
44
+
45
+ def move_y_down(times = 1)
46
+ LibHokusai.hoku_rect_y_down(to_hoku_rect, times)
47
+ end
48
+
49
+ def self.from_hoku_rect(rect)
50
+ self.x = rect[:x]
51
+ self.y = rect[:y]
52
+ self.width = rect[:w]
53
+ self.height = rect[:h]
54
+ end
55
+ end
56
+ end