# Ledger System

{% embed url="<https://www.youtube.com/watch?v=phKSA8qp4j4>" %}

<figure><img src="https://580445893-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fzf2ZAqGbUPk9K8cVFSgd%2Fuploads%2F0LJDdY0wSHp0C4RmLxK0%2Fimage.png?alt=media&#x26;token=57dc6363-9668-4d07-b15d-fe0d4afb318b" alt=""><figcaption><p>qadr_ui / Ledger</p></figcaption></figure>

This system manages the display of a ledger in an interactive way within a game. The code allows adding pages, handling page items, and responding to user interactions such as selecting or hovering over items.

## **Note:** The code provided cannot be used as an "Export." Therefore, you can access all the necessary examples and code in the `client` folder, specifically in the `ledger.lua` file.

### Functions and Data Structures

#### 1. `pageitem`

The `pageitem` table represents a single item on a page in the ledger. This item includes properties such as visibility, title, notes, price, and event handlers for user interaction (select and hover events).

**Code Example:**

```lua
local pageitem = {
    visible = true,
    title = "HalfPageTop3",
    notes = "HalfPageTop_notes",
    sketchTexture = "ledger_camp_horse_station",
    crossedOut = false,
    price = {
        hovered = false,
        price = "$241",
        crossedOutTexture = "LEDGER_CAMP_LINE_04",
        highlighted = false,
    },
    onSelect = function(self)
        print("selected", self.title)
        print("selected", self.notes)
        self.crossed()
    end,
    onHovered = function(self)
        print("Hovered", self.title)
        print("Hovered", self.notes)
    end
}
```

**Description:**

* **visible**: Determines whether the item is visible on the page.
* **title**: The title of the page item.
* **notes**: Notes or additional information related to the item.
* **sketchTexture**: The texture used to display the sketch image on the page.
* **crossedOut**: Boolean to indicate if the item should appear as crossed out.
* **price**: Contains details about the item's price, including hover status and whether it's highlighted or crossed out.
  * **hovered**: Indicates if the price is currently hovered.
  * **price**: The actual price of the item, in this case, `$241`.
  * **crossedOutTexture**: The texture used when the item is crossed out.
  * **highlighted**: Indicates if the price is highlighted.

**Event Handlers:**

* **onSelect**: Function triggered when the item is selected by the user. It prints the title and notes, and calls the `crossed` function.
* **onHovered**: Function triggered when the item is hovered by the user. It prints the title and notes.

***

#### 2. `contribution`

The `contribution` table represents a contribution entry in the ledger. It includes the contributor’s name, visibility, price, and description.

**Code Example:**

```lua
local contribution = {
    name = "Wyatt Earp",
    visible = true,
    price = 81,
    description = "Sheriff's fee",
}
```

**Description:**

* **name**: The name of the contributor (in this case, Wyatt Earp).
* **visible**: Determines if the contribution entry is visible.
* **price**: The amount of the contribution ($81).
* **description**: A description of the contribution (Sheriff’s fee).

***

#### 3. `fullPageUpgrade`

This table represents an upgrade that can be displayed on a full page. It includes details such as visibility, title, crossed-out status, notes, and price information.

**Code Example:**

```lua
local fullPageUpgrade = {
    visible = true,
    title = "Upgrades 1 Title",
    crossedOut = false,
    crossedOutTexture = "ledger_camp_line_04",
    notes = "Upgrades Note 1",
    price = {
        hovered = true,
        price = "10",
        crossedOutTexture = "LEDGER_CAMP_LINE_04",
        highlighted = false,
    },
    onSelect = function(self)
        print("selected", self.title)
        print("selected", self.notes)
        self.crossed()
    end,
    onHovered = function(self)
        print("Hovered", self.title)
        print("Hovered", self.notes)
    end
}
```

**Description:**

* **visible**: Determines if the upgrade is visible.
* **title**: The title of the upgrade.
* **crossedOut**: Indicates whether the upgrade is crossed out.
* **crossedOutTexture**: The texture to use when the upgrade is crossed out.
* **notes**: Additional notes about the upgrade.
* **price**: Contains price information for the upgrade.
  * **hovered**: Indicates if the price is hovered.
  * **price**: The cost of the upgrade.
  * **crossedOutTexture**: The texture used if the price is crossed out.
  * **highlighted**: Whether the price is highlighted.

**Event Handlers:**

* **onSelect**: Prints the selected upgrade's title and notes, and calls the `crossed` function.
* **onHovered**: Prints the hovered upgrade's title and notes.

***

#### 4. `fullPageReStock`

The `fullPageReStock` table defines an item in the restock section of a full page. It handles whether the item is crossed out, hovered, and its price.

**Code Example:**

```lua
local fullPageReStock = {
    index = 1,
    crossedOut = false,
    hovered = false,
    price = "33",
    crossedOutTexture = "LEDGER_CAMP_LINE_04",
    highlighted = false,
    onSelect = function(self)
        print("index", self.index)
        print("price", self.price)
        self.crossed()
    end,
    onHovered = function(self)
        print("index", self.index)
        print("price", self.price)
    end
}
```

**Description:**

* **index**: The index of the restock item.
* **crossedOut**: Whether the item is crossed out.
* **hovered**: Whether the item is hovered.
* **price**: The price of the restock item.
* **crossedOutTexture**: The texture used if the item is crossed out.
* **highlighted**: Whether the price is highlighted.

**Event Handlers:**

* **onSelect**: Prints the selected restock item's index and price, and calls the `crossed` function.
* **onHovered**: Prints the hovered restock item's index and price.

***

### Ledger Setup

#### Adding Pages

The `ledger` function allows the creation of ledger pages and adding different types of pages to it. Pages can be of type `PageItems`, `ContPage`, or `FullPage`, each with its own structure.

**Code Example:**

```lua
RegisterCommand("ledger",function()
    local ledgers = ledger({
        {
            coords = vector3(-323.06, 803.4, 117.9306),
            rots = vector3(0, 0, 99.88),
            name = "Ledger Name",
        },
        {
            coords = vector3(-326.1, 797.38, 117.72),
            rots = vector3(0, 0, 99.9),
            name = "Ledger Name2",
        },
        {
            coords = vector3(-282.62, 779.85, 119.58),
            rots = vector3(0, 0, 180.0),
            name = "Ledger Name3",
        },
    })

    local PageItems = {
        pageType = "PageItems",
        items = {
            pageitem,
            pageitem,
        }
    }
    ledgers:addPage(PageItems)

    local ContPage = {
        pageType = "ContPage",
        visible = true,
        title = "Title 1",
        carried = {
            visible = true,
        },
        contributions = {
            contribution,
            contribution
        },
        total = {
            visible = true
        }
    }
    ledgers:addPage(ContPage)

    local FullPage = {
        pageType = "FullPage",
        visible = true,
        title = "Title 2",
        subTitle = "SubTitle 2",
        sketchTexture = "ledger_camp_munitions",
        upgrades = { fullPageUpgrade, fullPageUpgrade },
        restock = { fullPageReStock, fullPageReStock },
    }
    ledgers:addPage(FullPage)
end)
```

***

#### Event Handling

The script also registers event handlers for various actions such as selecting or hovering over ledger items, as well as entering or exiting a volume around a ledger. Below are the event handlers implemented in the code:

**Event Example:**

```lua
luaCopy codeAddEventHandler("qadr_ui:ledger_selectedItem", function(data)
    print("qadr_ui:ledger_selectedItem")
end)

AddEventHandler("qadr_ui:ledger_hovered", function(data)
    print("qadr_ui:ledger_hovered")
end)

AddEventHandler("qadr_ui:ledger_in_volume", function(data)
    print("qadr_ui:ledger_in_volume")
    tablePrint(data, "ledger_in_volume")
end)

AddEventHandler("qadr_ui:ledger_out_volume", function(data)
    print("qadr_ui:ledger_out_volume")
    tablePrint(data, "ledger_out_volume")
end)
```

**Descriptions:**

* **qadr\_ui:**&#x6C;edger\_selectedItem: Triggered when an item in the ledger is selected. The `data` returned contains information about the selected item.
* **qadr\_ui:**&#x6C;edger\_hovered: Triggered when an item in the ledger is hovered over. The `data` returned contains information about the hovered item.
* **qadr\_ui:**&#x6C;edger\_in\_volume: Triggered when the player enters a specific volume around the ledger. The `data` returned contains information about the volume, and the player can start interacting with the ledger. For example, you might display a prompt to the player when they enter the volume.
* **qadr\_ui:**&#x6C;edger\_out\_volume: Triggered when the player exits a volume around the ledger. The `data` returned contains information about the volume, and you can use this event to remove any prompts or interaction options when the player leaves the area.

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://abdulkadir-aktas.gitbook.io/qadr_docs/qadr_ui/ledger-system.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
