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.
@@ -0,0 +1,181 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hokusai
4
+ class Touch
5
+ attr_accessor :stack, :archive
6
+
7
+ def initialize
8
+ @stack = []
9
+ @archive = []
10
+ @tapped = false
11
+ @swiped = false
12
+ @pinched = false
13
+ # @file = File.open("touch.log", "w")
14
+ end
15
+
16
+ def tapped?
17
+ @tapped
18
+ end
19
+
20
+ def swiped?
21
+ @swiped
22
+ end
23
+
24
+ def pinched?
25
+ @pinched
26
+ end
27
+
28
+ def longtapping?(stuff = "ok")
29
+ log("#{touching?} - #{elapsed(token)} - #{stuff}") if touching?
30
+ touching? && elapsed(token) > 5
31
+ end
32
+
33
+ def longtapped?
34
+ @longtapped
35
+ end
36
+
37
+ def touching?
38
+ type == :down || type == :move
39
+ end
40
+
41
+ def duration
42
+ if longtapping?
43
+ return elapsed(token)
44
+ end
45
+
46
+ first, last = archive[-2..-1]
47
+
48
+ last[:start] - first[:start]
49
+ end
50
+
51
+ def distance
52
+ raise Hokusai::Error.new("Archive is empty") if archive.empty?
53
+ first, last = archive[-2..-1]
54
+
55
+ x = last[:x] - first[:x]
56
+ y = last[:y] - first[:y]
57
+
58
+ [x, y]
59
+ end
60
+
61
+ def direction
62
+ raise Hokusai::Error.new("Archive is empty") if archive.empty?
63
+
64
+ first, last = archive[-2..-1]
65
+
66
+ x = last[:x] - first[:x]
67
+ y = last[:y] - first[:y]
68
+
69
+ if x.abs > y.abs
70
+ # swiping left/right
71
+ last[:x] > first[:x] ? :right : :left
72
+ else
73
+ # swiping up/down
74
+ last[:y] > first[:y] ? :down : :up
75
+ end
76
+ end
77
+
78
+ def angle
79
+ raise Hokusai::Error.new("Archive is empty") if archive.empty?
80
+
81
+ last, first = archive[-2..-1]
82
+
83
+ x = last[:x] - first[:x]
84
+ y = last[:y] - first[:y]
85
+
86
+ (Math.atan2(x, y) * (-180 / Math::PI)).round(0).to_i
87
+ end
88
+
89
+ def log(str)
90
+ # Thread.new do
91
+ # @file.write_nonblock("#{str}\n")
92
+ # end
93
+ end
94
+
95
+ def record(finger, x, y)
96
+ log("recording #{token}")
97
+ if type == :down
98
+ push(:move, finger, x, y)
99
+ log("state is move")
100
+ elsif type == :move
101
+ stack.last[:x] = x
102
+ stack.last[:y] = y
103
+
104
+ log("updated state move")
105
+ else
106
+ @longtapped = false
107
+ @swiped = false
108
+ @tapped = false
109
+ push(:down, finger, x, y)
110
+ log("state is down")
111
+ end
112
+ end
113
+
114
+ def clear
115
+ # log("clearing")
116
+ if type == :move
117
+ log("elapsed: #{elapsed(token)}")
118
+ if elapsed(token) > 300 && within(10.0)
119
+ @longtapped = true
120
+ log('longtap')
121
+ elsif within(10.0)
122
+ @tapped = true
123
+ else
124
+ @swiped = true
125
+ log('swipe')
126
+ end
127
+ elsif type == :down
128
+ @tapped = true
129
+ log('tap')
130
+ else
131
+ @longtapped = false
132
+ @swiped = false
133
+ @tapped = false
134
+ end
135
+
136
+ self.archive = stack.dup
137
+ stack.clear
138
+ end
139
+
140
+ def elapsed(token)
141
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) - token[:start]
142
+ end
143
+
144
+ def within(threshold)
145
+ move = stack.last
146
+ down = stack[-2]
147
+
148
+ t1 = (move[:x] - down[:x]).abs
149
+ t2 = (move[:y] - down[:y]).abs
150
+
151
+ t1 < threshold && t2 < threshold
152
+ end
153
+
154
+ def pop
155
+ stack.pop
156
+ end
157
+
158
+ def push(type, finger, x, y)
159
+ log("push: #{type}")
160
+ stack << {
161
+ type: type,
162
+ i: finger,
163
+ x: x,
164
+ y: y,
165
+ start: Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
166
+ }
167
+ end
168
+
169
+ def index
170
+ token&.[](:finger)
171
+ end
172
+
173
+ def type
174
+ token&.[](:type)
175
+ end
176
+
177
+ def token
178
+ @stack.last
179
+ end
180
+ end
181
+ end
@@ -1,435 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Hokusai
4
- # Rect = Struct.new(:x, :y, :width, :height) do
5
- class Rect
6
- attr_accessor :x, :y, :width, :height
7
-
8
- def initialize(x, y, width, height)
9
- @x = x
10
- @y = y
11
- @width = width
12
- @height = height
13
- end
14
- def to_hoku_rect
15
- @hoku_rect ||= LibHokusai::HmlRect.create(x, y, width, height)
16
- end
17
-
18
- def includes_y?(y)
19
- LibHokusai.hoku_rect_includes_y(to_hoku_rect, y)
20
- end
21
-
22
- def includes_x?(x)
23
- LibHokusai.hoku_rect_includes_x(to_hoku_rect, x)
24
- end
25
-
26
- def move_x_left(times = 1)
27
- LibHokusai.hoku_rect_x_left(to_hoku_rect, times)
28
- end
29
-
30
- def move_x_right(times = 1)
31
- LibHokusai.hoku_rect_x_right(to_hoku_rect, times)
32
- end
33
-
34
- def move_y_up(times = 1)
35
- LibHokusai.hoku_rect_y_up(to_hoku_rect, times)
36
- end
37
-
38
- def move_y_down(times = 1)
39
- LibHokusai.hoku_rect_y_down(to_hoku_rect, times)
40
- end
41
-
42
- def self.from_hoku_rect(rect)
43
- self.x = rect[:x]
44
- self.y = rect[:y]
45
- self.width = rect[:w]
46
- self.height = rect[:h]
47
- end
48
- end
49
-
50
- Outline = Struct.new(:top, :right, :bottom, :left) do
51
- def self.default
52
- new(0.0, 0.0, 0.0, 0.0)
53
- end
54
-
55
- def hash
56
- [self.class, top, right, bottom, left].hash
57
- end
58
-
59
- def self.convert(value)
60
- case value
61
- when String
62
- if value =~ /,/
63
- convert(value.split(",").map(&:to_f))
64
- else
65
- convert(value.to_f)
66
- end
67
- when Float
68
- new(value, value, value, value)
69
- when Array
70
- new(value[0] || 0.0, value[1] || 0.0, value[2] || 0.0, value[3] || 0.0)
71
- when Outline
72
- value
73
- end
74
- end
75
-
76
- def present?
77
- top > 0.0 || right > 0.0 || bottom > 0.0 || left > 0.0
78
- end
79
-
80
- def uniform?
81
- top == right && top == bottom && top == left
82
- end
83
- end
84
-
85
- class Padding
86
- attr_reader :top, :left, :right, :bottom
87
- def initialize(top, right, bottom, left)
88
- @top = top
89
- @left = left
90
- @right = right
91
- @bottom = bottom
92
- end
93
-
94
- alias_method :t, :top
95
- alias_method :l, :left
96
- alias_method :r, :right
97
- alias_method :b, :bottom
98
-
99
- def width
100
- right + left
101
- end
102
-
103
- def height
104
- top + bottom
105
- end
106
-
107
- def self.convert(value)
108
- case value
109
- when String
110
- if value =~ /,/
111
- convert(value.split(",").map(&:to_f))
112
- else
113
- convert(value.to_i)
114
- end
115
- when Integer
116
- new(value, value, value, value)
117
- when Array
118
- new(value[0], value[1], value[2], value[3])
119
- when Padding
120
- value
121
- else
122
- raise Hokusai::Error.new("Unsupported conversion type #{value.class} for Hokusai::Padding")
123
- end
124
- end
125
-
126
- def hash
127
- [self.class, top, right, bottom, left].hash
128
- end
129
- end
130
-
131
- class Canvas
132
- attr_accessor :width, :height, :x, :y, :vertical, :reverse
133
- def initialize(width, height, x = 0.0, y = 0.0, vertical = true, reverse = false)
134
- @width = width
135
- @height = height
136
- @x = x
137
- @y = y
138
- @vertical = vertical
139
- @reverse = reverse
140
- @hoku_rect = LibHokusai::HmlRect.create(x, y, width, height)
141
- end
142
-
143
- def reset(x, y, width, height, vertical: true, reverse: false)
144
- self.x = x
145
- self.y = y
146
- self.width = width
147
- self.height = height
148
- self.vertical = vertical
149
- self.reverse = reverse
150
- @hoku_rect[:x] = x
151
- @hoku_rect[:y] = y
152
- @hoku_rect[:w] = width
153
- @hoku_rect[:h] = height
154
- end
155
-
156
- def to_bounds
157
- Hokusai::Rect.new(x, y, width, height)
158
- end
159
-
160
- def reverse?
161
- reverse
162
- end
163
-
164
- def to_hoku_rect
165
- @hoku_rect
166
- end
167
- end
168
-
169
- # Color = Struct.new(:red, :green, :blue, :alpha) do
170
- class Color
171
- attr_reader :red, :green, :blue, :alpha
172
- def initialize(red, green, blue, alpha = 255)
173
- @red = red.freeze
174
- @green = green.freeze
175
- @blue = blue.freeze
176
- @alpha = alpha.freeze
177
- end
178
-
179
- alias_method :r, :red
180
- alias_method :b, :blue
181
- alias_method :g, :green
182
- alias_method :a, :alpha
183
-
184
- def self.convert(value)
185
- case value
186
- when String
187
- value = value.split(",").map(&:to_i)
188
- when Array
189
- when Color
190
- return value
191
- else
192
- raise Hokusai::Error.new("Unsupported conversion type #{value.class} for Hokusai::Color")
193
- end
194
-
195
- new(value[0], value[1], value[2], value[3] || 255)
196
- end
197
-
198
- def hash
199
- [self.class, r, g, b, a].hash
200
- end
201
- end
202
-
203
- class Keyboard
204
- attr_reader :raw
205
-
206
- [
207
- :shift, :super, :ctrl,
208
- :alt, :pressed, :pressed_len, :released,
209
- :released_len
210
- ].each do |name|
211
- define_method(name) do
212
- raw[name]
213
- end
214
- end
215
-
216
- def keyboard_key
217
- pressed.read_array_of_type(TYPE_UINT8, :read_uint8, pressed_len).first
218
- end
219
-
220
- def key
221
- keyboard_key[:key][:key]
222
- end
223
-
224
- def initialize(raw)
225
- @raw = raw
226
- end
227
- end
228
-
229
- class Mouse
230
- attr_reader :raw
231
-
232
- def initialize(raw)
233
- @raw = raw
234
- end
235
-
236
- [
237
- :pos, :delta, :scroll,
238
- :scroll_delta, :selection,
239
- :selection_type, :left,
240
- :middle, :right
241
- ].each do |name|
242
- define_method(name) do
243
- # instance_variable_set("@#{name}", raw[name]) if instance_variable_get("@#{name}").nil?
244
- #
245
- # instance_variable_get("@#{name}")
246
-
247
- raw[name]
248
- end
249
-
250
- define_method("#{name}=") do |val|
251
- raw[name] = val
252
- end
253
- end
254
- end
255
-
256
- class Touch
257
- attr_accessor :stack, :archive
258
- def initialize
259
- @stack = []
260
- @archive = []
261
- @tapped = false
262
- @swiped = false
263
- @pinched = false
264
- # @file = File.open("touch.log", "w")
265
- end
266
-
267
- def tapped?
268
- @tapped
269
- end
270
-
271
- def swiped?
272
- @swiped
273
- end
274
-
275
- def pinched?
276
- @pinched
277
- end
278
-
279
- def longtapping?(stuff = "ok")
280
- log("#{touching?} - #{elapsed(token)} - #{stuff}") if touching?
281
- touching? && elapsed(token) > 5
282
- end
283
-
284
- def longtapped?
285
- @longtapped
286
- end
287
-
288
- def touching?
289
- type == :down || type == :move
290
- end
291
-
292
- def duration
293
- if longtapping?
294
- return elapsed(token)
295
- end
296
-
297
- first, last = archive[-2..-1]
298
-
299
- last[:start] - first[:start]
300
- end
301
-
302
- def distance
303
- raise Hokusai::Error.new("Archive is empty") if archive.empty?
304
- first, last = archive[-2..-1]
305
-
306
- x = last[:x] - first[:x]
307
- y = last[:y] - first[:y]
308
-
309
- [x, y]
310
- end
311
-
312
- def direction
313
- raise Hokusai::Error.new("Archive is empty") if archive.empty?
314
-
315
- first, last = archive[-2..-1]
316
-
317
- x = last[:x] - first[:x]
318
- y = last[:y] - first[:y]
319
-
320
- if x.abs > y.abs
321
- # swiping left/right
322
- last[:x] > first[:x] ? :right : :left
323
- else
324
- # swiping up/down
325
- last[:y] > first[:y] ? :down : :up
326
- end
327
- end
328
-
329
- def angle
330
- raise Hokusai::Error.new("Archive is empty") if archive.empty?
331
-
332
- last, first = archive[-2..-1]
333
-
334
- x = last[:x] - first[:x]
335
- y = last[:y] - first[:y]
336
-
337
- (Math.atan2(x, y) * (-180 / Math::PI)).round(0).to_i
338
- end
339
-
340
- def log(str)
341
- # Thread.new do
342
- # @file.write_nonblock("#{str}\n")
343
- # end
344
- end
345
-
346
- def record(finger, x, y)
347
- log("recording #{token}")
348
- if type == :down
349
- push(:move, finger, x, y)
350
- log("state is move")
351
- elsif type == :move
352
- stack.last[:x] = x
353
- stack.last[:y] = y
354
-
355
- log("updated state move")
356
- else
357
- @longtapped = false
358
- @swiped = false
359
- @tapped = false
360
- push(:down, finger, x, y)
361
- log("state is down")
362
- end
363
- end
364
-
365
- def clear
366
- # log("clearing")
367
- if type == :move
368
- log("elapsed: #{elapsed(token)}")
369
- if elapsed(token) > 300 && within(10.0)
370
- @longtapped = true
371
- log('longtap')
372
- elsif within(10.0)
373
- @tapped = true
374
- else
375
- @swiped = true
376
- log('swipe')
377
- end
378
- elsif type == :down
379
- @tapped = true
380
- log('tap')
381
- else
382
- @longtapped = false
383
- @swiped = false
384
- @tapped = false
385
- end
386
-
387
- self.archive = stack.dup
388
- stack.clear
389
- end
390
-
391
- def elapsed(token)
392
- Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) - token[:start]
393
- end
394
-
395
- def within(threshold)
396
- move = stack.last
397
- down = stack[-2]
398
-
399
- t1 = (move[:x] - down[:x]).abs
400
- t2 = (move[:y] - down[:y]).abs
401
-
402
- t1 < threshold && t2 < threshold
403
- end
404
-
405
- def pop
406
- stack.pop
407
- end
408
-
409
- def push(type, finger, x, y)
410
- log("push: #{type}")
411
- stack << {
412
- type: type,
413
- i: finger,
414
- x: x,
415
- y: y,
416
- start: Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
417
- }
418
- end
419
-
420
- def index
421
- token&.[](:finger)
422
- end
423
-
424
- def type
425
- token&.[](:type)
426
- end
427
-
428
- def token
429
- @stack.last
430
- end
431
- end
3
+ require_relative "./types/primitives"
4
+ require_relative "./types/display"
5
+ require_relative "./types/touch"
6
+ require_relative "./types/mouse"
7
+ require_relative "./types/keyboard"
432
8
 
9
+ module Hokusai
433
10
  class Input
434
11
  attr_accessor :keyboard_override
435
12
  attr_reader :raw, :touch
@@ -438,8 +15,7 @@ module Hokusai
438
15
  [self.class, mouse.pos.x, mouse.pos.y, mouse.scroll, mouse.left.clicked, mouse.left.down, mouse.left.up].hash
439
16
  end
440
17
 
441
- def initialize(raw)
442
- @raw = raw
18
+ def initialize
443
19
  @touch = nil
444
20
  @keyboard_override = false
445
21
  end
@@ -451,15 +27,16 @@ module Hokusai
451
27
  end
452
28
 
453
29
  def keyboard
454
- Keyboard.new(@raw[:keyboard])
30
+ @keyboard ||= Keyboard.new
455
31
  end
456
32
 
457
33
  def mouse
458
- @mouse ||= Mouse.new(@raw[:mouse])
34
+ @mouse ||= Mouse.new
459
35
  end
460
36
 
461
37
  def hovered?(canvas)
462
- LibHokusai.hoku_input_is_hovered(raw, canvas.to_hoku_rect)
38
+ pos = mouse.pos
39
+ pos.x >= canvas.x && pos.x <= canvas.x + canvas.width && pos.y >= canvas.y && pos.y <= canvas.y + canvas.height
463
40
  end
464
41
  end
465
42
  end
@@ -1,7 +1,3 @@
1
- require "rouge"
2
- require "hokusai"
3
- require_relative "./lib/hokusai_code/formatter"
4
-
5
1
  module Hokusai::Util
6
2
  class Wrapped
7
3
  attr_accessor :text, :x, :y, :extra
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hokusai-zero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6.pre.pinephone4
4
+ version: 0.2.6.pre.pinephone6
5
5
  platform: ruby
6
6
  authors:
7
7
  - skinnyjames
@@ -292,6 +292,11 @@ files:
292
292
  - ui/src/hokusai/publisher.rb
293
293
  - ui/src/hokusai/style.rb
294
294
  - ui/src/hokusai/types.rb
295
+ - ui/src/hokusai/types/display.rb
296
+ - ui/src/hokusai/types/keyboard.rb
297
+ - ui/src/hokusai/types/mouse.rb
298
+ - ui/src/hokusai/types/primitives.rb
299
+ - ui/src/hokusai/types/touch.rb
295
300
  - ui/src/hokusai/util/clamping_iterator.rb
296
301
  - ui/src/hokusai/util/piece_table.rb
297
302
  - ui/src/hokusai/util/selection.rb