Beispiel #1
0
def create_state_from_node(node: dict, node_type: str, min_x: int, min_y: int, states: [State],
                           player_signal: List[str], functions: List[str]) -> Tuple[State, List[str]]:
    """
    creates state from mode with node_type type (state or group)
    :param functions: list with functions
    :param node: dict with node data
    :param node_type: state or group
    :param min_x - min x coordinate to add to state coordinate to excluse negative coordinates
    :param min_y - min y coordinate to add to state coordinate to excluse negative coordinates
    :param states - list of created states
    :param player_signal - list of triggers
    :return State
    """
    name: str = get_state_label(node) if node_type == 'state' else get_group_label(node)
    actions: str = get_state_actions(node) if node_type == 'state' else get_group_actions(node)
    node_id = node['id']
    (triggers, player_signal) = create_actions(actions, node_id, player_signal, functions)
    state_entry: List[str] = [trig.action for trig in triggers if trig.name == 'entry']
    state_exit: List[str] = [trig.action for trig in triggers if trig.name == 'exit']
    state_entry_str: str = '#ifdef DESKTOP\n    printf("Entered state %s");\n#endif /* def DESKTOP */' % name
    state_entry += state_entry[0] if state_entry else ""
    state_exit_str: str = '#ifdef DESKTOP\n    printf("Exited state %s");\n#endif /* def DESKTOP */' % name
    state_exit_str += state_exit[0] if state_exit else ""
    triggers: List[Trigger] = [trig for trig in triggers if trig.name != 'entry' and trig.name != 'exit']
    x, y, width, height = get_coordinates(node)
    x = x // divider - min_x // divider + 2
    y = y // divider - min_y // divider + 2
    width: int = width // divider
    height: int = height // divider
    parent: State = get_parent_by_label(node_id, states)
    new_id: List[str] = [(parent.new_id[0] + "/" + str(len(parent.childs) + len(parent.trigs)))]
    state: State = State(name=name, type=node_type, id=node_id, new_id=new_id, actions=actions,
                         entry=state_entry_str, exit=state_exit_str, trigs=triggers, x=x,
                         y=y, width=width, height=height, parent=parent, childs=list())
    return state, player_signal
Beispiel #2
0
def create_global_state(w: int, h: int) -> State:
    """
    creates global parent state of all states
    :param w: width between states
    :param h: height of all states
    :return: global parent state
    """
    state = State(name="global", type="group", id="", new_id=["1"], actions="",
                  entry="", exit="", trigs=[],
                  x=1, y=1, width=w // divider + global_w_delta, height=h // divider + global_h_delta, parent=None,
                  childs=[])
    return state
Beispiel #3
0
def create_terminate_state(states: [State]) -> State:
    """
    creates state for terminate state machine on desktop
    :param states: list of states
    :return: global parent state
    """
    global_state: State = get_state_by_id(states, '', 'old')
    state: State = State(name="final?def DESKTOP", type="state", id="last", new_id=["2"], actions="",
                         entry="""printf("\nBye! Bye!\n"); exit(0);""", exit="", trigs=[],
                         x=global_state.x + global_state.width // 2 - terminal_w // 2,
                         y=states[0].y + states[0].height + 5, width=terminal_w, height=terminal_h, parent=None,
                         childs=[])
    return state
Beispiel #4
0
def create_choice_from_node(node: dict, min_x: int, min_y: int, states: [State]) -> State:
    """
    creates choice state from node
    :param node: dict with node data
    :param min_x - min x coordinate to add to state coordinate to excluse negative coordinates
    :param min_y - min y coordinate to add to state coordinate to excluse negative coordinates
    :param states - list of already creates states
    :return State
    """
    node_id = node['id']
    x, y, width, height = get_coordinates(node)
    x: int = x // divider - min_x // divider + 1
    y: int = y // divider - min_y // divider + 1
    width: int = width // divider
    height: int = height // divider
    parent: State = get_parent_by_label(node_id, states)
    new_id: List[str] = [parent.new_id[0] + "/" + str(len(parent.childs) + len(parent.trigs))]
    state: State = State(name=get_state_label(node), type="choice", id=node_id, new_id=new_id, actions="",
                         entry="", exit="", trigs=[], x=x, y=y,
                         width=width, height=height, parent=parent, childs=list())
    return state