Code Monkey home page Code Monkey logo

Comments (2)

AndyObtiva avatar AndyObtiva commented on June 2, 2024 1

Thank you. That was very helpful! I get lost with regards to memory access calculations for arrays and structs in C, so deferring to your expertise alleviates this burden for me.

I amended examples/area_example.rb to utilize dashes as per your example.

Area Gallery

from libui.

kojix2 avatar kojix2 commented on June 2, 2024

Hi Andy

I wrote page6.c in Ruby.

Just pack the contents of arr into a binary sequence.

  dashes = [
    50.0, # ink
    10.0, # skip
    10.0, # ink
    10.0  # skip
  ].pack('d*')     # array -> string

Here is the correspondence table between types and strings.

image

require 'libui'

UI = LibUI

UI.init

handler = UI::FFI::AreaHandler.malloc
area    = UI.new_area(handler)

brush = UI::FFI::DrawBrush.malloc.tap do |b|
  b.Type = 0
  b.R = 0.3
  b.G = 0.6
  b.B = 0.9
  b.A = 1.0
end

stroke_params = UI::FFI::DrawStrokeParams.malloc.tap do |sp|
  dashes = [
    50.0, # ink
    10.0, # skip
    10.0, # ink
    10.0  # skip
  ].pack('d*')     # here!
  ndash = 4
  offset = -50.0

  sp.Cap = UI::DrawLineCapFlat
  sp.Join = UI::DrawLineJoinMiter
  sp.MiterLimit = 10
  sp.Dashes = dashes
  sp.NumDashes = ndash
  sp.DashPhase = offset
  sp.Thickness = 10.0
end

handler_draw_event = Fiddle::Closure::BlockCaller.new(0, [1, 1, 1]) do |_, _, area_draw_params|
  UI.draw_new_path(UI::DrawFillModeWinding).then do |path|
    UI.draw_path_new_figure(path, 128.0, 25.6)
    UI.draw_path_line_to(path, 230.4, 230.4)
    UI.draw_path_line_to(path, 230.4 - 102.4, 230.4 + 0.0)
    UI.draw_path_bezier_to(path,
                           51.2, 230.4,
                           51.2, 128.0,
                           128.0, 128.0)
    UI.draw_path_end(path)

    area_draw_params = UI::FFI::AreaDrawParams.new(area_draw_params)
    UI.draw_stroke(area_draw_params.Context, path, brush.to_ptr, stroke_params)
    UI.draw_free_path(path)
  end
end

handler.Draw         = handler_draw_event
# These callbacks are not protected from GC. It's really not very good.
handler.MouseEvent   = Fiddle::Closure::BlockCaller.new(0, [0]) {}
handler.MouseCrossed = Fiddle::Closure::BlockCaller.new(0, [0]) {}
handler.DragBroken   = Fiddle::Closure::BlockCaller.new(0, [0]) {}
handler.KeyEvent     = Fiddle::Closure::BlockCaller.new(0, [0]) {}

box = UI.new_vertical_box
UI.box_set_padded(box, 1)
UI.box_append(box, area, 1)

main_window = UI.new_window('Basic Draw Dash', 300, 300, 1)
UI.window_set_margined(main_window, 1)
UI.window_set_child(main_window, box)

UI.window_on_closing(main_window) do
  UI.control_destroy(main_window)
  UI.quit
  0
end
UI.control_show(main_window)

UI.main
UI.quit

from libui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.