hokusai-zero 0.2.5 → 0.2.6.pre.pinephone

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 (43) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -1
  3. data/Gemfile.lock +0 -2
  4. data/README.md +1 -1
  5. data/ast/src/core/input.c +85 -0
  6. data/ast/src/core/input.h +31 -0
  7. data/ast/src/core/util.c +23 -23
  8. data/ast/src/core/util.h +7 -7
  9. data/ext/extconf.rb +7 -3
  10. data/hokusai.gemspec +1 -2
  11. data/ui/examples/drag.rb +154 -0
  12. data/ui/examples/embedded.rb +59 -0
  13. data/ui/examples/game.rb +143 -0
  14. data/ui/examples/overlay.rb +233 -0
  15. data/ui/examples/provider.rb +56 -0
  16. data/ui/examples/shader/test.rb +145 -0
  17. data/ui/examples/shader.rb +100 -0
  18. data/ui/examples/wiki.rb +82 -0
  19. data/ui/lib/lib_hokusai.rb +29 -8
  20. data/ui/spec/spec_helper.rb +1 -1
  21. data/ui/src/hokusai/assets/arrow-down-line.png +0 -0
  22. data/ui/src/hokusai/assets/arrow-down-wide-line.png +0 -0
  23. data/ui/src/hokusai/automation/server.rb +2 -3
  24. data/ui/src/hokusai/backends/embedded/config.rb +47 -0
  25. data/ui/src/hokusai/backends/embedded/font.rb +112 -0
  26. data/ui/src/hokusai/backends/embedded/keys.rb +124 -0
  27. data/ui/src/hokusai/backends/embedded.rb +564 -0
  28. data/ui/src/hokusai/backends/raylib.rb +80 -5
  29. data/ui/src/hokusai/blocks/color_picker.rb +1080 -0
  30. data/ui/src/hokusai/blocks/shader_begin.rb +22 -0
  31. data/ui/src/hokusai/blocks/shader_end.rb +12 -0
  32. data/ui/src/hokusai/blocks/texture.rb +23 -0
  33. data/ui/src/hokusai/commands/rect.rb +10 -1
  34. data/ui/src/hokusai/commands/shader.rb +33 -0
  35. data/ui/src/hokusai/commands/texture.rb +26 -0
  36. data/ui/src/hokusai/commands.rb +22 -0
  37. data/ui/src/hokusai/event.rb +2 -1
  38. data/ui/src/hokusai/events/embedded.rb +66 -0
  39. data/ui/src/hokusai/painter.rb +22 -0
  40. data/ui/src/hokusai/types.rb +29 -0
  41. data/ui/src/hokusai.rb +4 -9
  42. metadata +24 -22
  43. data/ui/src/hokusai/assets/chevron-down.svg +0 -1
@@ -0,0 +1,82 @@
1
+ require "wikipedia"
2
+ require_relative "../src/hokusai"
3
+ require_relative "../src/hokusai/backends/raylib"
4
+
5
+ module Wiki
6
+ class Widget < Hokusai::Block
7
+ style <<-EOF
8
+ [style]
9
+ wikiStyle {
10
+ color: rgb(49, 49, 49);
11
+ size: 20;
12
+ markdown: true;
13
+ padding: padding(5.0, 10.0, 5.0, 10.0);
14
+ }
15
+ search {
16
+ height: 45;
17
+ background: rgb(239, 243, 255);
18
+ }
19
+ EOF
20
+ template <<-EOF
21
+ [template]
22
+ selectable
23
+ vblock
24
+ vblock {
25
+ ...search
26
+ }
27
+ label {
28
+ content="Search by Term"
29
+ }
30
+ input {
31
+ size="25"
32
+ @change="update_term"
33
+ }
34
+ [if="summary"]
35
+ panel
36
+ text {
37
+ @selected="handle_selected"
38
+ :content="summary"
39
+ ...wikiStyle
40
+ }
41
+ EOF
42
+
43
+ uses(
44
+ selectable: Hokusai::Blocks::Selectable,
45
+ vblock: Hokusai::Blocks::Vblock,
46
+ text: Hokusai::Blocks::Text,
47
+ label: Hokusai::Blocks::Label,
48
+ input: Hokusai::Blocks::Input,
49
+ panel: Hokusai::Blocks::Panel,
50
+ )
51
+
52
+ attr_reader :summary
53
+
54
+ def update_term(term)
55
+ page = Wikipedia.find(term)
56
+
57
+ @summary = <<~EOF
58
+ **[#{term}](#{page.fullurl})**
59
+
60
+ #{page.summary}
61
+ EOF
62
+
63
+ @summary
64
+ end
65
+
66
+ def handle_selected(start, stop)
67
+ pp [start, stop]
68
+ end
69
+ end
70
+ end
71
+
72
+ Hokusai::Backends::RaylibBackend.run(Wiki::Widget) do |config|
73
+ config.after_load do
74
+ font = Hokusai::Backends::RaylibBackend::Font.from_ext("#{__dir__}/assets/Inter-Regular.ttf", 160)
75
+ Hokusai.fonts.register "inter", font
76
+ Hokusai.fonts.activate "inter"
77
+ end
78
+
79
+ config.width = 500
80
+ config.height = 500
81
+ config.title = "Wiki Widget"
82
+ end
@@ -245,6 +245,22 @@ module LibHokusai
245
245
  :collecting, :bool
246
246
  end
247
247
 
248
+ enum :hml_drag_direction, [:up, :down, :left, :right]
249
+ enum :hml_pinch_direction, [:in, :out]
250
+
251
+ class HmlInputEmbedded < FFI::Struct
252
+ layout :hold, :bool,
253
+ :hold_duration, :int,
254
+ :drag, :bool,
255
+ :drag_direction, :hml_drag_direction,
256
+ :drag_pos, HmlVec2.ptr,
257
+ :drag_angle, :float,
258
+ :pinch, :bool,
259
+ :pinch_direction, :hml_pinch_direction,
260
+ :pinch_pos, HmlVec2.ptr,
261
+ :pinch_angle, :float
262
+ end
263
+
248
264
  class HmlInputMouseButton < FFI::Struct
249
265
  layout :down, :bool,
250
266
  :up, :bool,
@@ -293,7 +309,8 @@ module LibHokusai
293
309
 
294
310
  class HmlInput < FFI::Struct
295
311
  layout :keyboard, HmlInputKeyboard.ptr,
296
- :mouse, HmlInputMouse.ptr
312
+ :mouse, HmlInputMouse.ptr,
313
+ :embedded, HmlInputEmbedded.ptr
297
314
  end
298
315
 
299
316
  def LibHokusai.const_missing( sym )
@@ -308,6 +325,10 @@ module LibHokusai
308
325
  attach_function :hoku_input_set_mouse_position, [HmlInput.by_ref, HmlVec2.by_ref], :void
309
326
  attach_function :hoku_input_mouse_set_scroll, [HmlInput.by_ref, :float], :void
310
327
  attach_function :hoku_input_mouse_set_button, [HmlInput.by_ref, HmlInputMouseButton.by_ref, :int], :void
328
+ attach_function :hoku_input_embedded_set_hold, [HmlInput.by_ref, :bool, :int], :void
329
+ attach_function :hoku_input_embedded_set_drag, [HmlInput.by_ref, :bool, :hml_drag_direction, :float, :float, :float], :void
330
+ attach_function :hoku_input_embedded_set_pinch, [HmlInput.by_ref, :bool, :hml_pinch_direction, :float, :float, :float], :void
331
+ attach_function :hoku_input_attach_embedded, [HmlInput.by_ref], :int
311
332
 
312
333
  attach_function :hoku_input_is_clicked, [HmlInput.by_ref, HmlRect.by_ref], :bool
313
334
  attach_function :hoku_input_is_hovered, [HmlInput.by_ref, HmlRect.by_ref], :bool
@@ -352,16 +373,16 @@ module LibHokusai
352
373
 
353
374
  attach_function :hoku_selection_init, [:pointer], :int
354
375
  attach_function :hoku_selection_selected, [HokuSelection.by_ref, :float, :float, :float, :float], :bool
355
- attach_function :hoku_selection_cursor_set, [HokuSelection.by_ref, HokuCursorPosition.by_value], :int
376
+ attach_function :hoku_selection_cursor_set, [HokuSelection.by_ref, HokuCursorPosition.by_ref], :int
356
377
  attach_function :hoku_selection_cursor_free, [HokuSelection.by_ref], :void
357
378
  attach_function :hoku_selection_free, [HokuSelection.by_ref], :void
358
379
 
359
- attach_function :hoku_rect_includes_y, [HmlRect.by_value, :float], :bool
360
- attach_function :hoku_rect_includes_x, [HmlRect.by_value, :float], :bool
361
- attach_function :hoku_rect_x_left, [HmlRect.by_value, :int], :float
362
- attach_function :hoku_rect_x_right, [HmlRect.by_value, :int], :float
363
- attach_function :hoku_rect_y_up, [HmlRect.by_value, :int], :float
364
- attach_function :hoku_rect_y_down, [HmlRect.by_value, :int], :float
380
+ attach_function :hoku_rect_includes_y, [HmlRect.by_ref, :float], :bool
381
+ attach_function :hoku_rect_includes_x, [HmlRect.by_ref, :float], :bool
382
+ attach_function :hoku_rect_x_left, [HmlRect.by_ref, :int], :float
383
+ attach_function :hoku_rect_x_right, [HmlRect.by_ref, :int], :float
384
+ attach_function :hoku_rect_y_up, [HmlRect.by_ref, :int], :float
385
+ attach_function :hoku_rect_y_down, [HmlRect.by_ref, :int], :float
365
386
 
366
387
  class HokuChar < FFI::Struct
367
388
  layout :offset, :int,
@@ -44,7 +44,7 @@ def with_app(app, &block)
44
44
  client = nil
45
45
  error = nil
46
46
 
47
- Hokusai::ThreadPool.post do
47
+ Thread.new do
48
48
  client = Hokusai::Automation::Client.start
49
49
 
50
50
  yield client
@@ -99,15 +99,14 @@ module Hokusai
99
99
  app = App.new(driver)
100
100
 
101
101
  @socket = Thin::Server.new(*args, app)
102
- Hokusai::ThreadPool.post do
102
+
103
+ Thread.new do
103
104
  @socket.start
104
105
  end
105
106
  end
106
107
 
107
108
  def self.stop
108
109
  @socket.stop
109
-
110
- Hokusai::ThreadPool.prune_pool
111
110
  end
112
111
  end
113
112
  end
@@ -0,0 +1,47 @@
1
+ module Hokusai::Backends
2
+ class EmbeddedBackend::ConfigError < StandardError; end
3
+
4
+ class EmbeddedBackend::Config
5
+ attr_accessor :width, :height, :fps,
6
+ :title, :config_flags, :window_state_flags,
7
+ :automation_driver, :background, :after_load_cb,
8
+ :host, :port, :automated, :on_reload, :event_waiting
9
+
10
+ def initialize
11
+ @width = 500
12
+ @height = 500
13
+ @fps = 60
14
+ @title = "(Unknown Title)"
15
+ @config_flags = Raylib::FLAG_WINDOW_RESIZABLE | Raylib::FLAG_VSYNC_HINT
16
+ @window_state_flags = Raylib::FLAG_WINDOW_RESIZABLE
17
+ @automation_driver = nil
18
+ @background = Raylib::WHITE
19
+ @after_load_cb = nil
20
+ @host = "127.0.0.1"
21
+ @port = 4333
22
+ @automated = false
23
+ @on_reload = ->(_){}
24
+ @event_waiting = false
25
+ end
26
+
27
+ def start_automation_driver
28
+ raise ConfigError.new("Need a Hokusai::Driver in order to automate") if automation_driver.nil?
29
+
30
+ automation_driver.serve(host, port)
31
+ end
32
+
33
+ def automate(host, port)
34
+ self.host = host
35
+ self.port = port
36
+ self.automated = true
37
+ end
38
+
39
+ def after_load(&block)
40
+ self.after_load_cb = block
41
+ end
42
+
43
+ def on_reload(&block)
44
+ @on_reload = block
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,112 @@
1
+ module Hokusai
2
+ module Backends
3
+ class EmbeddedBackend
4
+ class DataForCb < FFI::Struct
5
+ layout :size, :int,
6
+ :spacing, :int,
7
+ :raw, :pointer
8
+
9
+ def self.create(size, spacing, raw)
10
+ obj = new.tap do |instance|
11
+ instance[:size] = size
12
+ instance[:spacing] = spacing
13
+ instance[:raw] = raw
14
+ end
15
+
16
+ yield obj.to_ptr
17
+ end
18
+ end
19
+
20
+ OnWidthCb = Proc.new do |char, ffi_pointer|
21
+ data = DataForCb.new ffi_pointer
22
+
23
+ Raylib.MeasureTextEx(data[:raw], "#{char.chr}", data[:size], data[:spacing]).x + 1.565#@ data[:spacing]
24
+ end
25
+
26
+ class Font < Hokusai::Font
27
+ attr_reader :raw
28
+
29
+ DEFAULT = "–—‘’“”…\r\n\t0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%%^&*(),.?/\"\\[]-_=+|~`{}<>;:'\0"
30
+
31
+ def self.default
32
+ font = Raylib.GetFontDefault
33
+ Raylib.SetTextureFilter(font.texture, Raylib::TEXTURE_FILTER_BILINEAR)
34
+ # Raylib.GenTextureMipmaps(font.texture)
35
+
36
+ new(font)
37
+ end
38
+
39
+ def self.from(file)
40
+ font = Raylib.LoadFont(file)
41
+ Raylib.SetTextureFilter(font.texture, Raylib::TEXTURE_FILTER_BILINEAR)
42
+ # Raylib.GenTextureMipmaps(font.texture)
43
+
44
+ new(font)
45
+ end
46
+
47
+ def self.from_ext(file, font_size, codepoint_string = DEFAULT)
48
+ ptr = FFI::MemoryPointer.new :int
49
+ codepoints = Raylib.LoadCodepoints(codepoint_string, ptr)
50
+ count = ptr.read_int
51
+
52
+ raylib_font = Raylib.LoadFontEx(file, font_size, codepoints, count)
53
+ # Raylib.GenTextureMipmaps(raylib_font.texture)
54
+ Raylib.SetTextureFilter(raylib_font.texture, Raylib::TEXTURE_FILTER_BILINEAR)
55
+
56
+ font = new(raylib_font)
57
+ Raylib.UnloadCodepoints(codepoints)
58
+
59
+ font
60
+ end
61
+
62
+ def initialize(raw, spacing = 1.0)
63
+ @raw = raw
64
+ @spacing = spacing
65
+ end
66
+
67
+ # returns the spacing for this font
68
+ # based on the text size
69
+ def spacing
70
+ ssize = raw.baseSize || 1
71
+ ssize = 10 if ssize < 10
72
+
73
+ (ssize / (raw.baseSize.zero? ? 1 : raw.baseSize)).to_f
74
+ end
75
+
76
+ def measure(string, size)
77
+ vec = Raylib.MeasureTextEx(raw, string, size, spacing)
78
+
79
+ [vec.x, vec.y]
80
+ end
81
+
82
+ def height
83
+ raw.baseSize
84
+ end
85
+
86
+ def clamp_markdown(text, size, width, initial_offset = 0.0)
87
+ clamping_pointer = FFI::MemoryPointer.new :pointer
88
+ RaylibBackend::DataForCb.create(size, spacing, raw) do |ptr|
89
+ ret = LibHokusai.hoku_text_md_clamp(clamping_pointer, text, width, initial_offset, ptr, RaylibBackend::OnWidthCb)
90
+ raise Hokusai::Error.new("Clamping failed") unless ret.zero?
91
+ end
92
+
93
+ clamping = LibHokusai::HokuClamping.new(FFI::AutoPointer.new(clamping_pointer.get_pointer(0), LibHokusai.method(:hoku_text_clamping_free)))
94
+
95
+ Hokusai::Clamping.new(clamping, markdown: true)
96
+ end
97
+
98
+ def clamp(text, size, width, initial_offset = 0.0)
99
+ clamping_pointer = FFI::MemoryPointer.new :pointer
100
+ EmbeddedBackend::DataForCb.create(size, spacing, raw) do |ptr|
101
+ ret = LibHokusai.hoku_text_clamp(clamping_pointer, text, width, initial_offset, ptr, EmbeddedBackend::OnWidthCb)
102
+ raise Hokusai::Error.new("Clamping failed") unless ret.zero?
103
+ end
104
+
105
+ clamping = LibHokusai::HokuClamping.new(FFI::AutoPointer.new(clamping_pointer.get_pointer(0), LibHokusai.method(:hoku_text_clamping_free)))
106
+
107
+ Hokusai::Clamping.new(clamping)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,124 @@
1
+ module Hokusai::Backends
2
+ class EmbeddedBackend
3
+ Keys = [
4
+ [:null, Raylib::KEY_NULL],
5
+ [:left_shift, Raylib::KEY_LEFT_SHIFT],
6
+ [:left_control, Raylib::KEY_LEFT_CONTROL],
7
+ [:left_alt, Raylib::KEY_LEFT_ALT],
8
+ [:left_super, Raylib::KEY_LEFT_SUPER],
9
+ [:right_shift, Raylib::KEY_RIGHT_SHIFT],
10
+ [:right_control, Raylib::KEY_RIGHT_CONTROL],
11
+ [:right_alt, Raylib::KEY_RIGHT_ALT],
12
+ [:right_super, Raylib::KEY_RIGHT_SUPER],
13
+ [:apostrophe, Raylib::KEY_APOSTROPHE],
14
+ [:comma, Raylib::KEY_COMMA],
15
+ [:minus, Raylib::KEY_MINUS],
16
+ [:period, Raylib::KEY_PERIOD],
17
+ [:slash, Raylib::KEY_SLASH],
18
+ [:zero, Raylib::KEY_ZERO],
19
+ [:one, Raylib::KEY_ONE],
20
+ [:two, Raylib::KEY_TWO],
21
+ [:three, Raylib::KEY_THREE],
22
+ [:four, Raylib::KEY_FOUR],
23
+ [:five, Raylib::KEY_FIVE],
24
+ [:six, Raylib::KEY_SIX],
25
+ [:seven, Raylib::KEY_SEVEN],
26
+ [:eight, Raylib::KEY_EIGHT],
27
+ [:nine, Raylib::KEY_NINE],
28
+ [:semicolon, Raylib::KEY_SEMICOLON],
29
+ [:equal, Raylib::KEY_EQUAL],
30
+ [:a, Raylib::KEY_A],
31
+ [:b, Raylib::KEY_B],
32
+ [:c, Raylib::KEY_C],
33
+ [:d, Raylib::KEY_D],
34
+ [:e, Raylib::KEY_E],
35
+ [:f, Raylib::KEY_F],
36
+ [:g, Raylib::KEY_G],
37
+ [:h, Raylib::KEY_H],
38
+ [:i, Raylib::KEY_I],
39
+ [:j, Raylib::KEY_J],
40
+ [:k, Raylib::KEY_K],
41
+ [:l, Raylib::KEY_L],
42
+ [:m, Raylib::KEY_M],
43
+ [:n, Raylib::KEY_N],
44
+ [:o, Raylib::KEY_O],
45
+ [:p, Raylib::KEY_P],
46
+ [:q, Raylib::KEY_Q],
47
+ [:r, Raylib::KEY_R],
48
+ [:s, Raylib::KEY_S],
49
+ [:t, Raylib::KEY_T],
50
+ [:u, Raylib::KEY_U],
51
+ [:v, Raylib::KEY_V],
52
+ [:w, Raylib::KEY_W],
53
+ [:x, Raylib::KEY_X],
54
+ [:y, Raylib::KEY_Y],
55
+ [:z, Raylib::KEY_Z],
56
+ [:left_bracket, Raylib::KEY_LEFT_BRACKET],
57
+ [:backslash, Raylib::KEY_BACKSLASH],
58
+ [:right_bracket, Raylib::KEY_RIGHT_BRACKET],
59
+ [:grave, Raylib::KEY_GRAVE],
60
+ [:space, Raylib::KEY_SPACE],
61
+ [:escape, Raylib::KEY_ESCAPE],
62
+ [:enter, Raylib::KEY_ENTER],
63
+ [:tab, Raylib::KEY_TAB],
64
+ [:backspace, Raylib::KEY_BACKSPACE],
65
+ [:insert, Raylib::KEY_INSERT],
66
+ [:delete, Raylib::KEY_DELETE],
67
+ [:right, Raylib::KEY_RIGHT],
68
+ [:left, Raylib::KEY_LEFT],
69
+ [:down, Raylib::KEY_DOWN],
70
+ [:up, Raylib::KEY_UP],
71
+ [:page_up, Raylib::KEY_PAGE_UP],
72
+ [:page_down, Raylib::KEY_PAGE_DOWN],
73
+ [:home, Raylib::KEY_HOME],
74
+ [:end, Raylib::KEY_END],
75
+ [:caps_lock, Raylib::KEY_CAPS_LOCK],
76
+ [:scroll_lock, Raylib::KEY_SCROLL_LOCK],
77
+ [:num_lock, Raylib::KEY_NUM_LOCK],
78
+ [:print_screen, Raylib::KEY_PRINT_SCREEN],
79
+ [:pause, Raylib::KEY_PAUSE],
80
+ [:f1, Raylib::KEY_F1],
81
+ [:f2, Raylib::KEY_F2],
82
+ [:f3, Raylib::KEY_F3],
83
+ [:f4, Raylib::KEY_F4],
84
+ [:f5, Raylib::KEY_F5],
85
+ [:f6, Raylib::KEY_F6],
86
+ [:f7, Raylib::KEY_F7],
87
+ [:f8, Raylib::KEY_F8],
88
+ [:f9, Raylib::KEY_F9],
89
+ [:f10, Raylib::KEY_F10],
90
+ [:f11, Raylib::KEY_F11],
91
+ [:f12, Raylib::KEY_F12],
92
+ [:left_shift, Raylib::KEY_LEFT_SHIFT],
93
+ [:left_control, Raylib::KEY_LEFT_CONTROL],
94
+ [:left_alt, Raylib::KEY_LEFT_ALT],
95
+ [:left_super, Raylib::KEY_LEFT_SUPER],
96
+ [:right_shift, Raylib::KEY_RIGHT_SHIFT],
97
+ [:right_control, Raylib::KEY_RIGHT_CONTROL],
98
+ [:right_alt, Raylib::KEY_RIGHT_ALT],
99
+ [:right_super, Raylib::KEY_RIGHT_SUPER],
100
+ [:kb_menu, Raylib::KEY_KB_MENU],
101
+ [:kp_0, Raylib::KEY_KP_0],
102
+ [:kp_1, Raylib::KEY_KP_1],
103
+ [:kp_2, Raylib::KEY_KP_2],
104
+ [:kp_3, Raylib::KEY_KP_3],
105
+ [:kp_4, Raylib::KEY_KP_4],
106
+ [:kp_5, Raylib::KEY_KP_5],
107
+ [:kp_6, Raylib::KEY_KP_6],
108
+ [:kp_7, Raylib::KEY_KP_7],
109
+ [:kp_8, Raylib::KEY_KP_8],
110
+ [:kp_9, Raylib::KEY_KP_9],
111
+ [:kp_decimal, Raylib::KEY_KP_DECIMAL],
112
+ [:kp_divide, Raylib::KEY_KP_DIVIDE],
113
+ [:kp_multiply, Raylib::KEY_KP_MULTIPLY],
114
+ [:kp_subtract, Raylib::KEY_KP_SUBTRACT],
115
+ [:kp_add, Raylib::KEY_KP_ADD],
116
+ [:kp_enter, Raylib::KEY_KP_ENTER],
117
+ [:kp_equal, Raylib::KEY_KP_EQUAL],
118
+ [:back, Raylib::KEY_BACK],
119
+ [:menu, Raylib::KEY_MENU],
120
+ [:volume_up, Raylib::KEY_VOLUME_UP],
121
+ [:volume_down, Raylib::KEY_VOLUME_DOWN]]
122
+ end
123
+ end
124
+