DevJoy.Scene.Choice (DevJoy v2.0.0)

View Source

Choices are visually presented as buttons that allow the user to perform an action. They can be used as simple menu items, as answers to character questions or as a possible condition choice. For example, the action of the chosen option can be used to navigate to other parts or scenes.

Condition

The choice with an atom content in condition

defmodule MyApp.SceneName do
  use DevJoy.Scene

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

Menu

The choice with a string content in menu

defmodule MyApp.SceneName do
  use DevJoy.Scene

  part :menu do
    menu "Menu title" do
      choice "Menu item content", goto(:part_name)
    end
  end
end

Question

The choice with a string content in question

defmodule MyApp.SceneName do
  use DevJoy.Scene

  part :dialog do
    question :john_doe, "Question content" do
      choice "Answer content", goto(:part_name)
    end

    # or
    question :john_doe, "Question content", some: :data do
      choice "Answer content", goto(:part_name)
    end
  end
end

Summary

Field types

The type representing the action of the choice. The action is a 1-arity function which is called when the user chooses it's choice.

The type representing the choice's content. When it's an atom, it won't be translated. This is useful in conditions as we only want to compare it with a content returned by the action function.

Main

The choice structure contains the following keys

t()

The type representing the choice structure.

The type representing the choice structure with a given content type. A content can be a string or an atom.

Field types

action()

@type action() :: (Keyword.t() -> any())

The type representing the action of the choice. The action is a 1-arity function which is called when the user chooses it's choice.

content()

@type content() :: atom() | String.t()

The type representing the choice's content. When it's an atom, it won't be translated. This is useful in conditions as we only want to compare it with a content returned by the action function.

Main

%DevJoy.Scene.Choice{}

(struct)

The choice structure contains the following keys:

  • action - a function to be called when the user chooses this option
  • content - the content of the choice

t()

@type t() :: t(String.t() | atom())

The type representing the choice structure.

t(content)

@type t(content) :: %DevJoy.Scene.Choice{action: action(), content: content}

The type representing the choice structure with a given content type. A content can be a string or an atom.