Class: Hokusai::Backends::RaylibBackend

Inherits:
Object
  • Object
show all
Defined in:
ui/src/hokusai/backends/raylib.rb,
ui/src/hokusai/backends/raylib/font.rb,
ui/src/hokusai/backends/raylib/keys.rb

Defined Under Namespace

Classes: Config, ConfigError, DataForCb, Font

Constant Summary collapse

RAYLIB_PATH =
ENV["RAYLIB_PATH"] || "#{__dir__}/../../../../vendor/lib"
OnWidthCb =
Proc.new do |char, ffi_pointer|
  data = DataForCb.new ffi_pointer

  Raylib.MeasureTextEx(data[:raw], "#{char.chr}", data[:size], data[:spacing]).x + 1.565#@ data[:spacing]
end
Keys =
[
[:null, Raylib::KEY_NULL],
[:left_shift, Raylib::KEY_LEFT_SHIFT],
[:left_control, Raylib::KEY_LEFT_CONTROL],
[:left_alt, Raylib::KEY_LEFT_ALT],
[:left_super, Raylib::KEY_LEFT_SUPER],
[:right_shift, Raylib::KEY_RIGHT_SHIFT],
[:right_control, Raylib::KEY_RIGHT_CONTROL],
[:right_alt, Raylib::KEY_RIGHT_ALT],
[:right_super, Raylib::KEY_RIGHT_SUPER],
[:apostrophe, Raylib::KEY_APOSTROPHE],
[:comma, Raylib::KEY_COMMA],
[:minus, Raylib::KEY_MINUS],
[:period, Raylib::KEY_PERIOD],
[:slash, Raylib::KEY_SLASH],
[:zero, Raylib::KEY_ZERO],
[:one, Raylib::KEY_ONE],
[:two, Raylib::KEY_TWO],
[:three, Raylib::KEY_THREE],
[:four, Raylib::KEY_FOUR],
[:five, Raylib::KEY_FIVE],
[:six, Raylib::KEY_SIX],
[:seven, Raylib::KEY_SEVEN],
[:eight, Raylib::KEY_EIGHT],
[:nine, Raylib::KEY_NINE],
[:semicolon, Raylib::KEY_SEMICOLON],
[:equal, Raylib::KEY_EQUAL],
[:a, Raylib::KEY_A],
[:b, Raylib::KEY_B],
[:c, Raylib::KEY_C],
[:d, Raylib::KEY_D],
[:e, Raylib::KEY_E],
[:f, Raylib::KEY_F],
[:g, Raylib::KEY_G],
[:h, Raylib::KEY_H],
[:i, Raylib::KEY_I],
[:j, Raylib::KEY_J],
[:k, Raylib::KEY_K],
[:l, Raylib::KEY_L],
[:m, Raylib::KEY_M],
[:n, Raylib::KEY_N],
[:o, Raylib::KEY_O],
[:p, Raylib::KEY_P],
[:q, Raylib::KEY_Q],
[:r, Raylib::KEY_R],
[:s, Raylib::KEY_S],
[:t, Raylib::KEY_T],
[:u, Raylib::KEY_U],
[:v, Raylib::KEY_V],
[:w, Raylib::KEY_W],
[:x, Raylib::KEY_X],
[:y, Raylib::KEY_Y],
[:z, Raylib::KEY_Z],
[:left_bracket, Raylib::KEY_LEFT_BRACKET],
[:backslash, Raylib::KEY_BACKSLASH],
[:right_bracket, Raylib::KEY_RIGHT_BRACKET],
[:grave, Raylib::KEY_GRAVE],
[:space, Raylib::KEY_SPACE],
[:escape, Raylib::KEY_ESCAPE],
[:enter, Raylib::KEY_ENTER],
[:tab, Raylib::KEY_TAB],
[:backspace, Raylib::KEY_BACKSPACE],
[:insert, Raylib::KEY_INSERT],
[:delete, Raylib::KEY_DELETE],
[:right, Raylib::KEY_RIGHT],
[:left, Raylib::KEY_LEFT],
[:down, Raylib::KEY_DOWN],
[:up, Raylib::KEY_UP],
[:page_up, Raylib::KEY_PAGE_UP],
[:page_down, Raylib::KEY_PAGE_DOWN],
[:home, Raylib::KEY_HOME],
[:end, Raylib::KEY_END],
[:caps_lock, Raylib::KEY_CAPS_LOCK],
[:scroll_lock, Raylib::KEY_SCROLL_LOCK],
[:num_lock, Raylib::KEY_NUM_LOCK],
[:print_screen, Raylib::KEY_PRINT_SCREEN],
[:pause, Raylib::KEY_PAUSE],
[:f1, Raylib::KEY_F1],
[:f2, Raylib::KEY_F2],
[:f3, Raylib::KEY_F3],
[:f4, Raylib::KEY_F4],
[:f5, Raylib::KEY_F5],
[:f6, Raylib::KEY_F6],
[:f7, Raylib::KEY_F7],
[:f8, Raylib::KEY_F8],
[:f9, Raylib::KEY_F9],
[:f10, Raylib::KEY_F10],
[:f11, Raylib::KEY_F11],
[:f12, Raylib::KEY_F12],
[:left_shift, Raylib::KEY_LEFT_SHIFT],
[:left_control, Raylib::KEY_LEFT_CONTROL],
[:left_alt, Raylib::KEY_LEFT_ALT],
[:left_super, Raylib::KEY_LEFT_SUPER],
[:right_shift, Raylib::KEY_RIGHT_SHIFT],
[:right_control, Raylib::KEY_RIGHT_CONTROL],
[:right_alt, Raylib::KEY_RIGHT_ALT],
[:right_super, Raylib::KEY_RIGHT_SUPER],
[:kb_menu, Raylib::KEY_KB_MENU],
[:kp_0, Raylib::KEY_KP_0],
[:kp_1, Raylib::KEY_KP_1],
[:kp_2, Raylib::KEY_KP_2],
[:kp_3, Raylib::KEY_KP_3],
[:kp_4, Raylib::KEY_KP_4],
[:kp_5, Raylib::KEY_KP_5],
[:kp_6, Raylib::KEY_KP_6],
[:kp_7, Raylib::KEY_KP_7],
[:kp_8, Raylib::KEY_KP_8],
[:kp_9, Raylib::KEY_KP_9],
[:kp_decimal, Raylib::KEY_KP_DECIMAL],
[:kp_divide, Raylib::KEY_KP_DIVIDE],
[:kp_multiply, Raylib::KEY_KP_MULTIPLY],
[:kp_subtract, Raylib::KEY_KP_SUBTRACT],
[:kp_add, Raylib::KEY_KP_ADD],
[:kp_enter, Raylib::KEY_KP_ENTER],
[:kp_equal, Raylib::KEY_KP_EQUAL],
[:back, Raylib::KEY_BACK],
[:menu, Raylib::KEY_MENU],
[:volume_up, Raylib::KEY_VOLUME_UP],
[:volume_down, Raylib::KEY_VOLUME_DOWN]]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|config| ... } ⇒ RaylibBackend

Returns a new instance of RaylibBackend.

Yields:



34
35
36
37
38
# File 'ui/src/hokusai/backends/raylib.rb', line 34

def initialize
  @config = Config.new

  yield(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'ui/src/hokusai/backends/raylib.rb', line 10

def config
  @config
end

Class Method Details

.iconsObject



12
13
14
# File 'ui/src/hokusai/backends/raylib.rb', line 12

def self.icons
  @icons ||= {}
end

.imagesObject



16
17
18
# File 'ui/src/hokusai/backends/raylib.rb', line 16

def self.images
  @images ||= {}
end

.resetObject



30
31
32
# File 'ui/src/hokusai/backends/raylib.rb', line 30

def self.reset
  @stop = false
end

.run(app) ⇒ Object



128
129
130
131
132
133
134
135
# File 'ui/src/hokusai/backends/raylib.rb', line 128

def self.run(app)
  backend = new do |config|
    yield config
  end

  block = app.mount
  backend.run(block)
end

.stop!Object



26
27
28
# File 'ui/src/hokusai/backends/raylib.rb', line 26

def self.stop!
  @stop = true
end

.stopped?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'ui/src/hokusai/backends/raylib.rb', line 20

def self.stopped?
  @stop = false if @stop.nil?

  @stop
end

Instance Method Details

#color(hc) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'ui/src/hokusai/backends/raylib.rb', line 61

def color(hc)
  if @raylib_color.nil?
    @raylib_color = Raylib::Color.from_u8(hc.red, hc.green, hc.blue, hc.alpha)
  else
    @raylib_color[:r] = hc.red
    @raylib_color[:g] = hc.green
    @raylib_color[:b] = hc.blue
    @raylib_color[:a] = hc.alpha
  end

  @raylib_color
end

#hml_vec2(x, y) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'ui/src/hokusai/backends/raylib.rb', line 50

def hml_vec2(x, y)
  if @hml_vec2.nil?
    @hml_vec2 = LibHokusai::HmlVec2.create(x, y)
  else
    @hml_vec2[:x] = x
    @hml_vec2[:y] = y
  end

  @hml_vec2
end

#inside_scissor(x, y, h = 0) ⇒ Object



252
253
254
255
256
257
# File 'ui/src/hokusai/backends/raylib.rb', line 252

def inside_scissor(x, y, h = 0)
  return true if @scissor.nil?

  val = y + h >= @scissor[1] && y <= @scissor[1] + @scissor[3]
  val
end

#mouse_button(clicked: false, down: false, released: false, up: false) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'ui/src/hokusai/backends/raylib.rb', line 87

def mouse_button(clicked: false, down: false, released: false, up: false)
  if @mouse_button.nil?
    @mouse_button = LibHokusai::HmlInputMouseButton.create(clicked: clicked, down: down, released: released, up: up)
  else
    @mouse_button[:clicked] = clicked
    @mouse_button[:down] = down
    @mouse_button[:released] = released
    @mouse_button[:up] = up
  end

  @mouse_button
end

#process_input(input) ⇒ Object



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
# File 'ui/src/hokusai/backends/raylib.rb', line 100

def process_input(input)
  raylib_mouse_pos = Raylib.GetMousePosition
  raylib_mouse_delta = Raylib.GetMouseDelta
  LibHokusai.hoku_input_mouse_set_scroll(input.raw, Raylib.GetMouseWheelMove)
  LibHokusai.hoku_input_set_mouse_position(input.raw, hml_vec2(raylib_mouse_pos.x, raylib_mouse_pos.y))

  input.raw[:mouse][:delta][:x] = raylib_mouse_delta.x
  input.raw[:mouse][:delta][:y] = raylib_mouse_delta.y

  [0,1,2].each do |button_id|
    clicked = Raylib.IsMouseButtonPressed(button_id)
    down = Raylib.IsMouseButtonDown(button_id)
    released = Raylib.IsMouseButtonReleased(button_id)
    up = Raylib.IsMouseButtonUp(button_id)

    button = mouse_button(clicked: clicked, down: down, released: released, up: up)
    LibHokusai.hoku_input_mouse_set_button(input.raw, button, button_id)
  end

  LibHokusai.hoku_input_keyboard_start(input.raw)

  Keys.each do |(hoku_key, raylib_key)|
    LibHokusai.hoku_input_keyboard_set_key(input.raw, hoku_key, Raylib.IsKeyDown(raylib_key))
  end

  LibHokusai.hoku_input_keyboard_stop(input.raw)
end

#rect(arr) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'ui/src/hokusai/backends/raylib.rb', line 74

def rect(arr)
 if @raylib_rect.nil?
   @raylib_rect = Raylib::Rectangle.create(arr[0], arr[1], arr[2], arr[3])
 else
   @raylib_rect[:x] = arr[0]
   @raylib_rect[:y] = arr[1]
   @raylib_rect[:width] = arr[2]
   @raylib_rect[:height] = arr[3]
 end

 @raylib_rect
end

#register_command_handlersObject



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'ui/src/hokusai/backends/raylib.rb', line 259

def register_command_handlers
  Hokusai.on_close_window do
    self.class.stop!
  end

  Hokusai.on_set_mouse_cursor do |type|
    raylib_type = {
      default: Raylib::MOUSE_CURSOR_DEFAULT,
      arrow: Raylib::MOUSE_CURSOR_ARROW,
      ibeam: Raylib::MOUSE_CURSOR_IBEAM,
      crosshair: Raylib::MOUSE_CURSOR_CROSSHAIR,
      pointer: Raylib::MOUSE_CURSOR_POINTING_HAND,
    }[type]

    raise Hokusai::Error.new("Cursor #{type} not recognized") if raylib_type.nil?

    Raylib::SetMouseCursor(raylib_type)
  end

  Hokusai.on_set_mouse_position do |mouse|
    Raylib.SetMousePosition(mouse.pos.x, mouse.pos.y)
  end

  Hokusai.on_set_window_position do |(dx, dy)|
    wpos = Raylib.GetWindowPosition

    Raylib.SetWindowPosition(wpos.x + dx, wpos.y + dy)
  end

  Hokusai.on_restore_window do
    Raylib.RestoreWindow
  end

  Hokusai.on_maximize_window do
    if Raylib.IsWindowMaximized
      Raylib.RestoreWindow
    else
      Raylib.MaximizeWindow
    end
  end

  Hokusai.on_minimize_window do
    Raylib.MinimizeWindow
  end

  Hokusai.on_renderable do |canvas|
    inside_scissor(canvas.x, canvas.y, canvas.height)
  end

  Hokusai::Commands::ScissorBegin.on_draw do |command|
    Raylib.BeginScissorMode(command.x, command.y, command.width, command.height)
    @scissor = [command.x, command.y, command.width, command.height]
  end

  Hokusai::Commands::ScissorEnd.on_draw do |_|
    Raylib.EndScissorMode
    @scissor = nil
  end

  Hokusai::Commands::Circle.on_draw do |command|
    next unless inside_scissor(command.x, command.y)

    radius = command.radius - command.outline
    rc = command.color
    color = Raylib::Color.from_u8(rc.r, rc.g, rc.b, rc.a)
    Raylib.DrawCircleV(vec2(command.x, command.y), radius, color)

    if command.outline > 0
      Raylib.DrawCircleLines(command.x.ceil.to_i, command.y.ceil.to_i, command.radius, color(command.color))
    end
  end

  Hokusai::Commands::SVG.on_draw do |command|
    command = command.as(Hokusai::Commands::SVG)
    texture = begin
                if self.class.images[command.source]
                  self.class.images[command.source]
                else
                  img = Raylib.LoadImageSVG(command.source, command.width, command.height)
                  texture = Raylib.load_texture_from_image(img)
                  Raylib.UnloadImage(img)
                  self.class.images[command.source] = texture
                  Raylib.GenTextureMipmaps(texture)
                  texture
                end
              end

    Raylib.DrawTexture(texture, command.x, command.y, color(command.color))
  end

  Hokusai::Commands::Image.on_draw do |command|
    next unless inside_scissor(command.x, command.y, command.height)

    texture = begin
                if self.class.images[command.cache]
                  self.class.images[command.cache]
                else
                  img = Raylib.LoadImage(command.source)
                  Raylib.ImageResize(img.to_ptr, command.width, command.height)

                  texture = Raylib.LoadTextureFromImage(img)
                  Raylib.UnloadImage(img)
                  self.class.images[command.cache] = texture
                  Raylib.GenTextureMipmaps(texture)
                  texture
                end
              end

    Raylib.DrawTexture(texture, command.x, command.y, Raylib::WHITE)
  end

  Hokusai::Commands::Text.on_draw do |command|
    next unless inside_scissor(command.x, command.y, command.size)

    active_name = Hokusai.fonts.active_font_name
    font = Hokusai.fonts.active

    Hokusai.fonts.activate command.font.nil? ? active_name : command.font
    font = Hokusai.fonts.active

    c = color(command.color)
    x = command.x + command.padding.l
    y = command.y + command.padding.t

    if fnt = font
      # content = FFI::MemoryPointer.from_string(command.content)
      Raylib.DrawTextEx(fnt.raw, command.content.to_s, vec2(x, y), command.size, fnt.spacing, c)
      # content.free
    else
      Raylib.DrawText(command.content, x, y, command.size, c)
    end

    Hokusai.fonts.activate active_name
  end

  Hokusai::Commands::Rect.on_draw do |command|
    next unless inside_scissor(command.x, command.y, command.height)

    background_color = color(command.color)
    # the rect has an outline
    # so we want to render twice
    if command.outline?
      # for Raylib, the boundaries for background and outline are the same
      # rectangles render in from the boundary
      # and outlines render out from the boundary
      # background_rect = outline_rect
      outline_rect = rect(command.background_boundary)
      background_rect = outline_rect
      # rect is rounded so we need a rounded outline and
      # a regular one
      if command.rounding > 0
        Raylib.DrawRectangleRounded(background_rect, command.rounding, 50, background_color)
      else
        Raylib.DrawRectangleRec(background_rect, background_color)
      end

      outline_color = color(command.outline_color)

      # now draw the outlines
      if command.outline_uniform? && command.rounding > 0.0 && outline_color.a > 0
        Raylib.DrawRectangleRoundedLinesEx(outline_rect, command.rounding, 50, command.outline.top + 1, outline_color)
      elsif command.outline_uniform? && !(command.rounding <= 0.0) && outline_color.a > 0
        Raylib.DrawRectangleLinesEx(outline_rect, command.outline.top, outline_color)
      elsif !command.outline_uniform?
        ox, oy, ow, oh = command.background_boundary

        if command.outline.top > 0.0
          Raylib.DrawLineEx(vec2(ox, oy, true), vec2(ox + ow, oy, true), command.outline.top, outline_color)
        end

        if command.outline.left > 0.0
          Raylib.DrawLineEx(vec2(ox, oy, true), vec2(ox, oy + oh, true), command.outline.left, outline_color)
        end

        if command.outline.right > 0.0
          Raylib.DrawLineEx(vec2(ox + ow, oy, true), vec2(ox + ow, oy + oh, true), command.outline.right, outline_color)
        end

        if command.outline.bottom > 0.0
          Raylib.DrawLineEx(vec2(ox, oy + oh, true), vec2(ox + ow, oy + oh, true), command.outline.bottom, outline_color)
        end
      end
    else
      rect = rect(command.background_boundary)

      if command.rounding > 0
        Raylib.DrawRectangleRounded(rect, command.rounding, 50, background_color)
      else
        Raylib.DrawRectangleRec(rect, background_color)
      end
    end
  end
end

#run(block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'ui/src/hokusai/backends/raylib.rb', line 137

def run(block)
  self.class.reset

  case RbConfig::CONFIG['host_os']
  when /darwin/
    Raylib.load_lib("#{RAYLIB_PATH}/libraylib.dylib")
  when /mswin|msys|mingw/
    Raylib.load_lib("#{RAYLIB_PATH}/libraylib.dll")
  when /linux/
    Raylib.load_lib("#{RAYLIB_PATH}/libraylib.so")
  end

  resize = false
  initial = true
  width = config.width
  height = config.height

  register_command_handlers

  ptr = FFI::MemoryPointer.new :pointer
  LibHokusai.hoku_input_init(ptr)
  raw = LibHokusai::HmlInput.new(ptr.get_pointer(0))
  input = Hokusai::Input.new(raw)
  ptr.free

  Raylib.SetConfigFlags(config.config_flags)
  Raylib.InitWindow(config.width, config.height, config.title)
  Raylib.SetTargetFPS(config.fps)
  Raylib.SetWindowState(config.window_state_flags)

  unless Hokusai.fonts.get("default")
    Hokusai.fonts.register "default", Hokusai::Backends::RaylibBackend::Font.default
    Hokusai.fonts.activate "default"
  end

  config.after_load_cb&.call

  if config.automated
    config.start_automation_driver
  end
  
  canvas = Hokusai::Canvas.new(width.to_f, height.to_f, 0.0, 0.0)
  @texture = Raylib.LoadRenderTexture(width, height)
  Raylib.SetTextureFilter(@texture.texture, Raylib::TEXTURE_FILTER_POINT)

  width = nil
  height = nil

  MemoryProfiler.start if ENV["PROFILE"]

  Raylib.EnableEventWaiting if config.event_waiting
  last_input_hash = nil

  until Raylib.WindowShouldClose
    last_width = Raylib.GetScreenWidth
    last_height = Raylib.GetScreenHeight

    if last_width != width || last_height != height
      resize = true
    else
      resize = false
    end

    width = last_width
    height = last_height

    process_input(input)
    block.public_send(:before_updated) if block.respond_to?(:before_updated)
    block.update
    block.public_send(:after_updated) if block.respond_to?(:after_updated)

    canvas.reset(nil, nil, width.to_f, height.to_f)

    Raylib.BeginDrawing
    Raylib.ClearBackground(config.background)

    painter = Hokusai::Painter.new(block, input)

    if config.automation_driver
      painter.on_before_render do |blocks, canvas, input|
        config.automation_driver.process(blocks, canvas, input)
      end

      painter.on_after_render do
        config.automation_driver.complete
      end

      sleep 0.004
    end

    painter.render(canvas, resize)

    Raylib.DrawFPS(10, 10) if ENV["PROFILE"] || ENV["FPS"]
    Raylib.EndDrawing


    break if self.class.stopped?
  end

  Hokusai.fonts.fonts.each do |key, font|
    Raylib.UnloadFont(font.raw)
  end

  LibHokusai.hoku_input_free(input.raw)

  if ENV["PROFILE"]
    report = MemoryProfiler.stop
    report.pretty_print(scale_bytes: true)
  end

  Raylib.CloseWindow

  config.automation_driver&.stop
end

#vec2(x, y, fresh = false) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'ui/src/hokusai/backends/raylib.rb', line 40

def vec2(x, y, fresh = false)
  if @raylib_vec2.nil? || fresh
    @raylib_vec2 = Raylib::Vector2.new
  end
  @raylib_vec2[:x] = x
  @raylib_vec2[:y] = y

  @raylib_vec2
end