显示 HN:将 GUI 作为脚本运行
Show HN: Run GUIs as Scripts

原始链接: https://github.com/skinnyjames/hokusai-pocket

## Hokusai Pocket:使用 Ruby 的便携应用和游戏 Hokusai Pocket 是一个正在进行中的项目,旨在利用 mruby、tree-sitter 和 raylib,使用 Ruby 创建便携式应用程序和游戏。它使用 Barista 进行引导和编译。 要开始,克隆仓库 ([https://github.com/skinnyjames/hokusai-pocket](https://github.com/skinnyjames/hokusai-pocket)) 并使用 `barista cli` 构建 `hokusai-pocket` 二进制文件。应用程序使用 `hokusai-pocket run:target=` 运行。构建适用于宿主系统的可分发二进制文件使用 `hokusai-pocket build:target=`,交叉编译(目前正在进行中)使用 `hokusai-pocket publish:target=`。 该项目包含一个示例“计数器”应用程序,演示了基本的 UI 元素和事件处理。它支持 `require_relative` 用于 Ruby 代码组织。 Hokusai Pocket 旨在简化使用 Ruby 进行游戏开发,其灵感来自 Taylor、Ruby 2D 和 DragonRuby 等引擎。项目结构包括源代码、语法和由 Brewfile 管理的构建文件。

对不起。
相关文章

原文

A project for making portable apps and games using hokusai

Work in progress, expect changes. ideas and contributions are welcome.

Hokusai pocket uses barista for bootstrapping itself.

Note: It is not recommended to build this project with mrbgems.

  • git clone https://github.com/skinnyjames/hokusai-pocket.git && cd hokusai-pocket
  • barista cli
    • this command will compile mruby/tree-sitter/raylib and produce a hokusai-pocket binary in bin/

To run apps using the binary

  • hokusai-pocket run:target=<somefile.rb>
    • where <somefile.rb> is a hokusai app

To package your app as a binary for the host system from this repo

  • hokusai-pocket build:target=<somefile.rb>
    • where <somefile.rb> is a hokusai app

To cross-compile your app for different platforms (wip, requires docker)

  • hokusai-pocket publish:target=<somefile.rb>
    • optional arguments include
      • assets_path=[assets folder]
      • platforms=osx (defaults to osx,linux,windows)
      • extras=[folders accessible to the build] (useful for including custom gems)
      • gem_config=[file declaring conf.gems] (useful for adding gems)

This will create a platforms/[platform]/[target]/[targetfile] for each included platform

An example app that can be run with hokusai-pocket run:target=counter.rb

# counter.rb
class Counter < Hokusai::Block
  style <<~EOF
  [style]
  additionStyles {
    background: rgb(214, 49, 24);
    cursor: "pointer";
  }

  additionLabel {
    size: 40;
    color: rgb(255,255,255);
  }

  subtractStyles {
    background: rgb(0, 85, 170);
    cursor: "pointer";
  }

  subtractLabel {
    size: 40;
    color: rgb(255, 255, 255);
  }
  EOF

  template <<-EOF
  [template]
    hblock { background="255,255,255" }
      label#count {
        :content="count.to_s"
        size="190" 
        :color="count_color"
      }
    hblock
      vblock#add { ...additionStyles @click="increment"}
        label { 
          content="Add"
          ...additionLabel 
        }
      vblock#subtract { ...subtractStyles @click="decrement" }
        label { 
          content="Subtract"
          ...subtractLabel 
        }
  EOF

  uses(
    vblock: Hokusai::Blocks::Vblock,
    hblock: Hokusai::Blocks::Hblock,
    label: Hokusai::Blocks::Text,
  )

  attr_accessor :count, :keys, :modal_open

  def count_positive
    count > 0
  end

  def increment(event)
    self.count += 1
  end

  def decrement(event)
    self.count -= 1
  end

  def count_color
    count.negative? ? [244, 0, 0] : [0, 0, 244]
  end

  def initialize(**args)
    @count = 0

    super
  end
end

Hokusai::Backend.run(Counter) do |config|
  config.title = "Counter"     # title
  config.fps = 60              # set frames per second
  config.width = 550
  config.height = 500

  config.after_load do         # register fonts
    Hokusai.fonts.register "default", Hokusai::Backend::Font.default
    Hokusai.fonts.activate "default"
  end
end

First, build or obtain a barista binary

barista cli will initially build the following archives in vendor in addition to the hokusai-pocket binary

* libtree-sitter.a [task: treesitter]
* libraylib.a      [task: raylib]
* libmruby.a       [task: mruby]
* libhokusai.a     [task: hokusai]

When updating any hokusai code, just run barista hokusai to update libhokusai.a.

To modify the build process for mruby, raylib, or tree-sitter, adjust the Brewfile and rebuild that task.

barista clean will remove the vendor library.

More soon.

The project structure is

/bin
  hokusai-pocket              (the binary)
/grammar                      (the tree sitter grammar for the templates)
/ruby                         (the hokusai ruby project)
/mrblib                       (the hokusai ruby project as a single file)
/src                          (supporting c files)
Brewfile                      (the build file for the project)

hokusai pocket supports a basic implementation of require_relative in the ruby code. It will perform basic subsitution and generate one large ruby file to be compiled using mrbc

  • Taylor - A simple game engine built using raylib and mruby
  • Ruby 2D - Make cross-platform 2D applications in Ruby
  • DragonRuby - DragonRuby is a Multilevel Cross-platform Runtime. The “multiple levels” within the runtime allows us to target platforms no other Ruby can target: PC, Mac, Linux, Raspberry Pi, WASM, iOS, Android, Nintendo Switch, PS4, Xbox, and Stadia.
联系我们 contact @ memedata.com