hokusai-zero 0.2.6.pre.pinephone6 → 0.2.6

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.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -0
  3. data/Gemfile.lock +2 -0
  4. data/README.md +1 -1
  5. data/ast/src/core/input.h +1 -0
  6. data/ast/src/core/util.c +23 -23
  7. data/ast/src/core/util.h +7 -7
  8. data/ext/extconf.rb +3 -3
  9. data/hokusai.gemspec +2 -1
  10. data/ui/examples/counter.rb +1 -2
  11. data/ui/examples/forum/file.rb +1 -1
  12. data/ui/examples/forum/post.rb +1 -0
  13. data/ui/examples/forum.rb +7 -7
  14. data/ui/examples/spreadsheet.rb +0 -1
  15. data/ui/examples/tic_tac_toe.rb +6 -6
  16. data/ui/lib/lib_hokusai.rb +24 -25
  17. data/ui/spec/spec_helper.rb +1 -1
  18. data/ui/src/hokusai/assets/chevron-down.svg +1 -0
  19. data/ui/src/hokusai/automation/driver_commands/base.rb +8 -2
  20. data/ui/src/hokusai/automation/driver_commands/trigger_keyboard.rb +6 -3
  21. data/ui/src/hokusai/automation/driver_commands/trigger_mouse.rb +5 -12
  22. data/ui/src/hokusai/automation/server.rb +3 -2
  23. data/ui/src/hokusai/backends/raylib/config.rb +1 -2
  24. data/ui/src/hokusai/backends/raylib/font.rb +3 -24
  25. data/ui/src/hokusai/backends/raylib.rb +36 -167
  26. data/ui/src/hokusai/backends/sdl2/config.rb +6 -9
  27. data/ui/src/hokusai/backends/sdl2/font.rb +1 -3
  28. data/ui/src/hokusai/backends/sdl2.rb +93 -188
  29. data/ui/src/hokusai/blocks/input.rb +2 -2
  30. data/ui/src/hokusai/commands/rect.rb +3 -12
  31. data/ui/src/hokusai/commands.rb +0 -22
  32. data/ui/src/hokusai/event.rb +1 -2
  33. data/ui/src/hokusai/events/keyboard.rb +18 -11
  34. data/ui/src/hokusai/events/mouse.rb +8 -10
  35. data/ui/src/hokusai/mounting/loop_entry.rb +1 -1
  36. data/ui/src/hokusai/painter.rb +8 -31
  37. data/ui/src/hokusai/types.rb +244 -20
  38. data/ui/src/hokusai.rb +35 -61
  39. data/xmake.lua +1 -1
  40. metadata +22 -32
  41. data/ui/examples/drag.rb +0 -154
  42. data/ui/examples/embedded.rb +0 -58
  43. data/ui/examples/game.rb +0 -143
  44. data/ui/examples/keyboard.rb +0 -47
  45. data/ui/examples/overlay.rb +0 -233
  46. data/ui/examples/provider.rb +0 -56
  47. data/ui/examples/shader/test.rb +0 -155
  48. data/ui/examples/shader.rb +0 -100
  49. data/ui/examples/wiki.rb +0 -82
  50. data/ui/spec/hokusai/e2e/keyboard_spec.rb +0 -52
  51. data/ui/src/hokusai/assets/arrow-down-line.png +0 -0
  52. data/ui/src/hokusai/assets/arrow-down-wide-line.png +0 -0
  53. data/ui/src/hokusai/assets/icons/outline/arrow-big-up.svg +0 -19
  54. data/ui/src/hokusai/assets/icons/outline/backspace.svg +0 -20
  55. data/ui/src/hokusai/blocks/color_picker.rb +0 -1080
  56. data/ui/src/hokusai/blocks/keyboard.rb +0 -249
  57. data/ui/src/hokusai/blocks/shader_begin.rb +0 -22
  58. data/ui/src/hokusai/blocks/shader_end.rb +0 -12
  59. data/ui/src/hokusai/blocks/slider.rb +0 -139
  60. data/ui/src/hokusai/blocks/texture.rb +0 -23
  61. data/ui/src/hokusai/commands/shader.rb +0 -33
  62. data/ui/src/hokusai/commands/texture.rb +0 -26
  63. data/ui/src/hokusai/events/touch.rb +0 -62
  64. data/ui/src/hokusai/types/display.rb +0 -151
  65. data/ui/src/hokusai/types/keyboard.rb +0 -168
  66. data/ui/src/hokusai/types/mouse.rb +0 -36
  67. data/ui/src/hokusai/types/primitives.rb +0 -56
  68. data/ui/src/hokusai/types/touch.rb +0 -181
  69. data/ui/src/hokusai/util/wrap_stream.rb +0 -193
@@ -5,34 +5,6 @@ require 'memory_profiler'
5
5
 
6
6
  module Hokusai::Backends
7
7
  class RaylibBackend
8
-
9
- SDF_SHADER = <<~EOF
10
- #version 100
11
-
12
- precision mediump float;
13
-
14
- // Input vertex attributes (from vertex shader)
15
- varying vec2 fragTexCoord;
16
- varying vec4 fragColor;
17
-
18
- // Input uniform values
19
- uniform sampler2D texture0;
20
- uniform vec4 colDiffuse;
21
-
22
- // NOTE: Add your custom variables here
23
- const float smoothing = 1.0/16.0;
24
-
25
- void main()
26
- {
27
- // Texel color fetching from texture sampler
28
- // NOTE: Calculate alpha using signed distance field (SDF)
29
- float distance = texture2D(texture0, fragTexCoord).a;
30
- float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);
31
-
32
- // Calculate final fragment color
33
- gl_FragColor = vec4(fragColor.rgb, fragColor.a*alpha);
34
- }
35
- EOF
36
8
 
37
9
  RAYLIB_PATH = ENV["RAYLIB_PATH"] || begin
38
10
  path = "#{__dir__}/../../../../vendor/lib"
@@ -56,10 +28,6 @@ module Hokusai::Backends
56
28
  @images ||= {}
57
29
  end
58
30
 
59
- def self.shaders
60
- @shaders ||= {}
61
- end
62
-
63
31
  def self.stopped?
64
32
  @stop = false if @stop.nil?
65
33
 
@@ -101,8 +69,8 @@ module Hokusai::Backends
101
69
  @hml_vec2
102
70
  end
103
71
 
104
- def color(hc, fresh = false)
105
- if @raylib_color.nil? || fresh
72
+ def color(hc)
73
+ if @raylib_color.nil?
106
74
  @raylib_color = Raylib::Color.from_u8(hc.red, hc.green, hc.blue, hc.alpha)
107
75
  else
108
76
  @raylib_color[:r] = hc.red
@@ -141,67 +109,31 @@ module Hokusai::Backends
141
109
  end
142
110
 
143
111
  def process_input(input)
144
- if !config.touch
145
- raylib_mouse_pos = Raylib.GetMousePosition
146
- raylib_mouse_delta = Raylib.GetMouseDelta
147
-
148
- input.mouse.pos.x = raylib_mouse_pos.x
149
- input.mouse.pos.y = raylib_mouse_pos.y
150
- input.mouse.delta.x = raylib_mouse_delta.x
151
- input.mouse.delta.y = raylib_mouse_delta.y
152
- input.mouse.scroll = Raylib.GetMouseWheelMove
153
- end
154
-
155
- if config.touch
156
- count = Raylib.GetTouchPointCount
157
- if count > 0
158
- vec = Raylib.GetTouchPosition(0)
159
- unless vec.x.zero? && vec.y.zero?
160
- input.touch.record(0, vec.x, vec.y)
161
- end
162
- else
163
- input.touch.clear
164
- end
165
- else
166
- {left: 0, middle: 1, right: 2}.each do |key, button_id|
112
+ raylib_mouse_pos = Raylib.GetMousePosition
113
+ raylib_mouse_delta = Raylib.GetMouseDelta
114
+ LibHokusai.hoku_input_mouse_set_scroll(input.raw, Raylib.GetMouseWheelMove)
115
+ LibHokusai.hoku_input_set_mouse_position(input.raw, hml_vec2(raylib_mouse_pos.x, raylib_mouse_pos.y))
167
116
 
168
- button = input.mouse.public_send(key)
169
- button.clicked = Raylib.IsMouseButtonPressed(button_id)
170
- button.down = Raylib.IsMouseButtonDown(button_id)
171
- button.released = Raylib.IsMouseButtonReleased(button_id)
172
- button.up = Raylib.IsMouseButtonUp(button_id)
173
- end
174
- end
117
+ input.raw[:mouse][:delta][:x] = raylib_mouse_delta.x
118
+ input.raw[:mouse][:delta][:y] = raylib_mouse_delta.y
175
119
 
176
- if config.touch
177
- if input.touch.touching?
178
- token = input.touch.token
120
+ [0,1,2].each do |button_id|
121
+ clicked = Raylib.IsMouseButtonPressed(button_id)
122
+ down = Raylib.IsMouseButtonDown(button_id)
123
+ released = Raylib.IsMouseButtonReleased(button_id)
124
+ up = Raylib.IsMouseButtonUp(button_id)
179
125
 
180
- input.mouse.pos.x = token[:x]
181
- input.mouse.pos.y = token[:y]
182
- end
183
-
184
- # translate taps to clicks
185
- if input.touch.tapped?
186
- input.mouse.left.clicked = true
187
- input.mouse.left.down = true
188
- input.mouse.left.released = false
189
- input.mouse.left.up = false
190
- else
191
- input.mouse.left.clicked = true
192
- input.mouse.left.down = true
193
- input.mouse.left.released = false
194
- input.mouse.left.up = false
195
- end
126
+ button = mouse_button(clicked: clicked, down: down, released: released, up: up)
127
+ LibHokusai.hoku_input_mouse_set_button(input.raw, button, button_id)
196
128
  end
197
129
 
198
- if !input.keyboard_override
199
- input.keyboard.reset
130
+ LibHokusai.hoku_input_keyboard_start(input.raw)
200
131
 
201
- Keys.each do |(hoku_key, raylib_key)|
202
- input.keyboard.set(hoku_key, Raylib.IsKeyDown(raylib_key))
203
- end
132
+ Keys.each do |(hoku_key, raylib_key)|
133
+ LibHokusai.hoku_input_keyboard_set_key(input.raw, hoku_key, Raylib.IsKeyDown(raylib_key))
204
134
  end
135
+
136
+ LibHokusai.hoku_input_keyboard_stop(input.raw)
205
137
  end
206
138
 
207
139
  def self.run(app)
@@ -217,6 +149,7 @@ module Hokusai::Backends
217
149
  self.class.reset
218
150
 
219
151
  Raylib.load_lib(RAYLIB_PATH)
152
+
220
153
  resize = false
221
154
  initial = true
222
155
  width = config.width
@@ -224,8 +157,11 @@ module Hokusai::Backends
224
157
 
225
158
  register_command_handlers
226
159
 
227
- input = Hokusai::Input.new
228
- input.support_touch! if config.touch
160
+ ptr = FFI::MemoryPointer.new :pointer
161
+ LibHokusai.hoku_input_init(ptr)
162
+ raw = LibHokusai::HmlInput.new(ptr.get_pointer(0))
163
+ input = Hokusai::Input.new(raw)
164
+ ptr.free
229
165
 
230
166
  Raylib.SetConfigFlags(config.config_flags)
231
167
  Raylib.InitWindow(config.width, config.height, config.title)
@@ -279,7 +215,7 @@ module Hokusai::Backends
279
215
  block.update
280
216
  block.public_send(:after_updated) if block.respond_to?(:after_updated)
281
217
 
282
- canvas.reset(0.0, 0.0, width.to_f, height.to_f)
218
+ canvas.reset(nil, nil, width.to_f, height.to_f)
283
219
 
284
220
  Raylib.BeginDrawing
285
221
  Raylib.ClearBackground(config.background)
@@ -303,6 +239,7 @@ module Hokusai::Backends
303
239
  Raylib.DrawFPS(10, 10) if ENV["PROFILE"] || ENV["FPS"]
304
240
  Raylib.EndDrawing
305
241
 
242
+
306
243
  break if self.class.stopped?
307
244
  end
308
245
 
@@ -314,9 +251,7 @@ module Hokusai::Backends
314
251
  Raylib.UnloadTexture(texture)
315
252
  end
316
253
 
317
- self.class.shaders.each do |k, shader|
318
- Raylib.UnloadShader(shader)
319
- end
254
+ LibHokusai.hoku_input_free(input.raw)
320
255
 
321
256
  if ENV["PROFILE"]
322
257
  report = MemoryProfiler.stop
@@ -328,26 +263,6 @@ module Hokusai::Backends
328
263
  config.automation_driver&.stop
329
264
  end
330
265
 
331
- def shader_value(value, type)
332
- case type
333
- when Hokusai::SHADER_UNIFORM_FLOAT
334
- ptr = FFI::MemoryPointer.new(:float)
335
- ptr.write_float(value)
336
- ptr
337
- when Hokusai::SHADER_UNIFORM_INT
338
- ptr = FFI::MemoryPointer.new(:int)
339
- ptr.write_int(value)
340
- ptr
341
- when Hokusai::SHADER_UNIFORM_IVEC2, Hokusai::SHADER_UNIFORM_UIVEC2, Hokusai::SHADER_UNIFORM_VEC2
342
- Raylib::Vector2.create(*value)
343
- when Hokusai::SHADER_UNIFORM_VEC3, Hokusai::SHADER_UNIFORM_IVEC3, Hokusai::SHADER_UNIFORM_UIVEC3
344
- Raylib::Vector3.create(*value)
345
- when Hokusai::SHADER_UNIFORM_VEC4, Hokusai::SHADER_UNIFORM_UIVEC4, Hokusai::SHADER_UNIFORM_IVEC4
346
- Raylib::Vector4.create(*value)
347
- end
348
- end
349
-
350
-
351
266
  def inside_scissor(x, y, h = 0)
352
267
  return true if @scissor.nil?
353
268
 
@@ -367,17 +282,11 @@ module Hokusai::Backends
367
282
  ibeam: Raylib::MOUSE_CURSOR_IBEAM,
368
283
  crosshair: Raylib::MOUSE_CURSOR_CROSSHAIR,
369
284
  pointer: Raylib::MOUSE_CURSOR_POINTING_HAND,
370
- none: -1,
371
285
  }[type]
372
286
 
373
287
  raise Hokusai::Error.new("Cursor #{type} not recognized") if raylib_type.nil?
374
288
 
375
- if raylib_type == -1
376
- Raylib.HideCursor
377
- else
378
- Raylib.ShowCursor
379
- Raylib::SetMouseCursor(raylib_type)
380
- end
289
+ Raylib::SetMouseCursor(raylib_type)
381
290
  end
382
291
 
383
292
  Hokusai.on_set_mouse_position do |mouse|
@@ -410,38 +319,6 @@ module Hokusai::Backends
410
319
  inside_scissor(canvas.x, canvas.y, canvas.height)
411
320
  end
412
321
 
413
- Hokusai::Commands::ShaderBegin.on_draw do |command|
414
- self.class.shaders[command.hash] ||= Raylib.LoadShaderFromMemory(command.vertex_shader, command.fragment_shader)
415
-
416
- unless command.uniforms.empty?
417
- command.uniforms.each do |key, value|
418
- location = Raylib.GetShaderLocation(self.class.shaders[command.hash], key.to_s)
419
-
420
- ptr = shader_value(value[0], value[1])
421
- Raylib.SetShaderValue(self.class.shaders[command.hash], location, ptr, value[1])
422
- ptr.free if ptr.is_a?(FFI::MemoryPointer)
423
- end
424
- end
425
-
426
- Raylib.BeginShaderMode(self.class.shaders[command.hash])
427
-
428
- @shader = self.class.shaders[command.hash]
429
- end
430
-
431
- Hokusai::Commands::ShaderEnd.on_draw do |_|
432
- Raylib.EndShaderMode
433
-
434
- @shader = nil
435
- end
436
-
437
- Hokusai::Commands::Texture.on_draw do |command|
438
- self.class.images[command.hash] ||= begin
439
- image = Raylib.GenImageColor(command.width, command.height, Raylib::BLANK)
440
- Raylib.LoadTextureFromImage(image)
441
- end
442
- Raylib.DrawTexture(self.class.images[command.hash], command.x, command.y, Raylib::WHITE)
443
- end
444
-
445
322
  Hokusai::Commands::ScissorBegin.on_draw do |command|
446
323
  Raylib.BeginScissorMode(command.x, command.y, command.width, command.height)
447
324
  @scissor = [command.x, command.y, command.width, command.height]
@@ -507,9 +384,9 @@ module Hokusai::Backends
507
384
  next unless inside_scissor(command.x, command.y, command.size)
508
385
 
509
386
  active_name = Hokusai.fonts.active_font_name
510
- font_name = command.font.nil? ? active_name : command.font
387
+ font = Hokusai.fonts.active
511
388
 
512
- Hokusai.fonts.activate font_name
389
+ Hokusai.fonts.activate command.font.nil? ? active_name : command.font
513
390
  font = Hokusai.fonts.active
514
391
 
515
392
  c = color(command.color)
@@ -517,16 +394,9 @@ module Hokusai::Backends
517
394
  y = command.y + command.padding.t
518
395
 
519
396
  if fnt = font
520
- if fnt.sdf
521
- self.class.shaders["font-sdf-#{font_name}"] ||= Raylib.LoadShaderFromMemory(nil, SDF_SHADER)
522
- Raylib.BeginShaderMode(self.class.shaders["font-sdf-#{font_name}"])
523
- end
524
-
397
+ # content = FFI::MemoryPointer.from_string(command.content)
525
398
  Raylib.DrawTextEx(fnt.raw, command.content.to_s, vec2(x, y), command.size, fnt.spacing, c)
526
-
527
- if fnt.sdf
528
- Raylib.EndShaderMode
529
- end
399
+ # content.free
530
400
  else
531
401
  Raylib.DrawText(command.content, x, y, command.size, c)
532
402
  end
@@ -583,9 +453,8 @@ module Hokusai::Backends
583
453
  end
584
454
  else
585
455
  rect = rect(command.background_boundary)
586
- if command.gradient
587
- Raylib.DrawRectangleGradientEx(rect, color(command.gradient[0], true), color(command.gradient[1], true), color(command.gradient[2], true), color(command.gradient[3], true))
588
- elsif command.rounding > 0
456
+
457
+ if command.rounding > 0
589
458
  Raylib.DrawRectangleRounded(rect, command.rounding, 50, background_color)
590
459
  else
591
460
  Raylib.DrawRectangleRec(rect, background_color)
@@ -3,8 +3,8 @@ module Hokusai::Backends
3
3
  class Config
4
4
  attr_accessor :width, :height, :fps, :init_flags,
5
5
  :title, :background, :automation_driver,
6
- :after_load_cb, :host, :port, :automated, :mobile,
7
- :window_config_flags, :touch
6
+ :after_load_cb, :host, :port, :automated,
7
+ :window_config_flags
8
8
 
9
9
  def after_load(&block)
10
10
  self.after_load_cb = block
@@ -14,19 +14,16 @@ module Hokusai::Backends
14
14
  @width = 500
15
15
  @height = 500
16
16
  @fps = 60
17
- @touch = false
18
17
 
19
18
  @init_flags = ::SDL::INIT_VIDEO | ::SDL::INIT_EVENTS
20
- @window_config_flags = SDL::WINDOW_RESIZABLE | SDL::WINDOW_ALLOW_HIGHDPI
19
+ @window_config_flags = SDL::WINDOW_RESIZABLE
21
20
  @title = "(Unknown Title)"
22
21
  @background = SDLBackend.color(255,255,255, 0)
23
22
 
24
23
  after_load do
25
- if Hokusai.fonts.get("default").nil?
26
- font = Hokusai::Backends::SDLBackend::Font.from "#{__dir__}/Monaco.ttf"
27
- Hokusai.fonts.register "default", font
28
- Hokusai.fonts.activate "default"
29
- end
24
+ font = Hokusai::Backends::SDLBackend::Font.from "#{__dir__}/Monaco.ttf"
25
+ Hokusai.fonts.register "default", font
26
+ Hokusai.fonts.activate "default"
30
27
  end
31
28
  end
32
29
  end
@@ -21,7 +21,7 @@ module Hokusai::Backends
21
21
  end
22
22
 
23
23
  class Font < Hokusai::Font
24
- def self.from(file = "#{__dir__}/Monaco.ttf", size=60)
24
+ def self.from(file = "#{__dir__}/Monaco.ttf", size=50)
25
25
  raw = SDL.TTF_OpenFont(file, size)
26
26
 
27
27
  raise Hokusai::Error.new("Cannot open font from #{file}: #{ttf_error}") if raw.null?
@@ -51,8 +51,6 @@ module Hokusai::Backends
51
51
  set_style(**args)
52
52
 
53
53
  render_color = SDLBackend.color(color.r, color.g, color.b, color.a)
54
- # bg = SDLBackend.color(0, 0, 0, 0)
55
-
56
54
  SDL.TTF_RenderUTF8_Blended(raw, text, render_color)
57
55
  end
58
56