View Source DevJoy.Scene behaviour (DevJoy v1.0.0)

Scene defines DSL which stores scene parts within get_part/0 and get_part/1 function.

Examples

In order to use the DevJoy DSL simply add use DevJoy.Scene at the top of your module

defmodule MyApp.SceneName do
  use DevJoy.Scene

  # parts DSL goes here
end

The optional translation is done at runtime, so you can use it as same as any other module. To enable runtime translation all you need to do is to pass your gettext module as shown below.

defmodule MyApp.Gettext do
  use Gettext, otp_app: :my_app
end

defmodule MyApp.SceneWithGettext do
  use DevJoy.Scene, gettext_module: MyApp.Gettext

  # parts DSL goes here
end

Summary

Types

Fields to cast into struct

Module to which fields are casted

Callbacks

Returns a main part of scene

Returns a part of scene

Functions

Returns a quoted anonymous function to place it within get_part/1 function. Once called, function fetches character and passes it along with choices to the function defined in second argument. The function in 2nd argument should call an action for one of passed choices. This allows to follow a choice based on a runtime condition.

The same as goto/2, but defaults to current scene and :main part.

The same as goto/2, but defaults to current scene.

Returns a quoted anonymous function to place it within get_part/1 function. Once called, function returns a data of specified part. This allows to easily navigate to other part or even scene.

Types

@type struct_fields() :: Keyword.t()

Fields to cast into struct

@type struct_module() :: module()

Module to which fields are casted

Callbacks

@callback get_part() :: DevJoy.Scene.Part.t()

Returns a main part of scene

@callback get_part(DevJoy.Scene.Part.name()) :: DevJoy.Scene.Part.t()

Returns a part of scene

Functions

Link to this macro

asset(path, data \\ [])

View Source (macro)

A DSL for DevJoy.Scene.Asset struct

Link to this macro

challenge(type, data)

View Source (macro)

A DSL for DevJoy.Scene.Element.Challenge struct

Link to this macro

choice(content, action)

View Source (macro)

A DSL for DevJoy.Scene.Element.Choice struct

Link to this macro

condition(character_id, func, list)

View Source (macro)

Returns a quoted anonymous function to place it within get_part/1 function. Once called, function fetches character and passes it along with choices to the function defined in second argument. The function in 2nd argument should call an action for one of passed choices. This allows to follow a choice based on a runtime condition.

Unlike in menu/2 or question/4 the choices are not supposed to be displayed as buttons and therefore their content could be an atom and so since it does not need to be translated it should simplify the condition in passed function.

Example

defmodule MyApp do
  def some_func(_character, choices) do
    Enum.random(choices).action()
  end
end

defmodule MyApp.SceneWithCondition do
  use DevJoy.Scene

  part :condition do
    condition :john_doe, &MyApp.some_func/2 do
      choice :choiceA, goto(:partA)
      choice :choiceB, goto(:partB)
    end
  end

  part :partA do
    # elements for part A goes here
  end

  part :partB do
    # elements for part B goes here
  end
end
Link to this macro

dialog(character_id, side \\ :left, content)

View Source (macro)

A DSL for DevJoy.Scene.Element.Dialog struct

@spec goto() :: Macro.output()

The same as goto/2, but defaults to current scene and :main part.

Link to this macro

goto(part_name)

View Source (macro)

The same as goto/2, but defaults to current scene.

Link to this macro

goto(scene, part_name)

View Source (macro)

Returns a quoted anonymous function to place it within get_part/1 function. Once called, function returns a data of specified part. This allows to easily navigate to other part or even scene.

Example

defmodule MyApp.SceneName do
  use DevJoy.Scene

  part :partA do
    goto(:partB)
  end

  part :partB do
    goto(MyApp.AnotherSceneName, :partA)
  end
end
Link to this macro

part(name, list)

View Source (macro)
@spec part(DevJoy.Scene.Part.name(), [{:do, Macro.input()}]) :: Macro.output()

A DSL for DevJoy.Scene.Part struct

Link to this macro

question(character_id, side \\ :left, content, list)

View Source (macro)

A DSL for DevJoy.Scene.Element.Dialog struct with support for choices

Example

defmodule MyApp.SceneWithQuestion do
  use DevJoy.Scene

  part :question do
    question :john_doe, "Example question" do
      choice "Example answer", goto(:part_name)
    end
  end
end