Skip to main content

Screen Vorlage

-- Version: 0.0
-- ####################################################
-- Globale statische Listen/Variablen
-- ####################################################
local LT = ic.enums.LogicType
local SUM = ic.enums.LogicBatchMethod.Sum
local AVG = ic.enums.LogicBatchMethod.Average
local LST = ic.enums.LogicSlotType
local SI = ic.enums.SorterInstruction

local Statics = {
    FONT_DEFAULT = 20,
}

local Colors = {
    SCREEN_BG = "#000000",
    TITLE_BG = "#44DD44",
    TITLE_TEXT = "#FFFFFF",
    TEXT_DEFAULT = "#ffffff",
    BTN_TEXT = "#ffffff",
    BTN_GREEN = "#33FF55",
    BTN_BLUE = "#0000ff",
    BTN_RED = "#ff0000",
    SELECT_BG = "#0F172A"
}


-- ####################################################
-- Einstellungen
-- ####################################################
local Settings = {


    last_train_stop = 0,
    move_to = 0,
    route_set = false,


    Devices = {
        DEMO              = { prefab = ic.hash("DEMO"),        name = ic.hash("DEMO"),               desc = "DEMO",           allowed_types = {} },
    }
}


function loadSettings()
    if ic.persist.has("Settings") then
        Settings = util.json.decode(ic.persist.get("Settings"))
    end
end
loadSettings()


function saveSettings()
    ic.persist.set("Settings", util.json.encode(Settings))
end


-- ####################################################
-- Hilfsvariablen
-- ####################################################


-- ####################################################
-- Screens
-- ####################################################


local screens = {
    main = { surface = ss.ui.surface("main"), name = "main", loaded = false },
    settings = { surface = ss.ui.surface("settings"), name = "settings", loaded = false },
    devices = { surface = ss.ui.surface("devices"), name = "devices", loaded = false }
}


local size = screens.main.surface:size()
local W, H = size.w, size.h


-- ####################################################
-- Funktionen
-- ####################################################


function initDevices()


    print("Call: initDevices()")
    local devs = Settings.Devices


end


function getDeviceList(sel_prefab, sel_name, allowed)
    local devices = ic.device_list()
    local device_ids = {}
    local device_options = {}
    local selected = 0


    for i, dev in ipairs(devices) do        
        --if dev.prefab_hash ~= Settings.hash_hd then
        device_ids[i] = { display = dev.display_name, name = dev.name_hash, prefab = dev.prefab_hash }
        device_options[i] = dev.display_name
       
        if dev.name_hash == sel_name and dev.prefab_name == sel_prefab then
            selected = i
        end
        --end
    end


    return device_options, device_ids, selected


end


function in_array(array, value)
    for _, v in ipairs(array) do
        if v == value then
            return true
        end
    end
    return false
end


function getDeviceByRefId(ref_id)
    local devices = ic.device_list()
    for _, dev in ipairs(devices) do
        if dev.ref_id == tonumber(ref_id) then
            return dev
        end
    end
    return nil
end


function getDeviceList(sel_prefab, sel_name, allowed)
    local allowed_device_types = allowed or {}
    local devices = ic.device_list()
    local option_list = {}
    local selected = 0
    local help_i = 1


    for i, dev in ipairs(devices) do
        if #allowed_device_types > 0 then
            if in_array(allowed_device_types, dev.prefab_hash) then
                option_list[help_i] = dev.display_name .. " (ID: ".. dev.ref_id .. ")"
           
                if dev.name_hash == sel_name and dev.prefab_hash == sel_prefab then
                    selected = help_i-1
                end
                help_i = help_i + 1
            end
        else
            option_list[i] = dev.display_name .. " (ID: ".. dev.ref_id .. ")"
            if dev.name_hash == sel_name and dev.prefab_hash == sel_prefab then
                selected = i-1
            end
        end
    end


    return option_list, selected


end


function printScreenTitel(surface, text)


    -- Background
    surface:element({ id = "bg", type = "panel",
        rect = { unit = "px", x = 0, y = 0, w = W, h = H },
        style = { bg = Colors.SCREEN_BG }
    })
   
    -- Titel
    surface:element({ id = "head_title_text", type = "panel",
        rect = { unit = "px", x = 0, y = 0, w = W, h = 40 },
        style = { bg = Colors.TITLE_BG }
    })
    surface:element({ id = "head_title_panel", type = "label",
        rect = { unit = "px", x = 20, y = 0, w = W - 40, h = 40 },
        props = { text = text },
        style = { font_size = Statics.FONT_DEFAULT * 1.5, color = Colors.TITLE_TEXT, align="center" }
    })
   
    return 45
end


-- ####################################################
-- START: MENÜ Geräteauswahl
-- ####################################################
function buildDevices()
   
    local devs = Settings.Devices
    local screen_surface = screens.devices.surface
    local pos_x = 20
    local pos_y = 30
   
    print("Call: buildDevices()")
    screen_surface:clear()


    pos_y = printScreenTitel(screen_surface, "Geräteauswahl")


    for key, dev in pairs(devs) do
        local ol, sel = getDeviceList(dev.prefab, dev.name, dev.allowed_types)
        screen_surface:element({ id = "lbl_settings_temp1" .. key, type = "label",
            rect = { unit = "px", x = pos_x, y = pos_y, w = 200, h = 20 },
            props = { text = dev.desc },
            style = { font_size = Statics.FONT_DEFAULT, color = Colors.TEXT_DEFAULT }
        })
        screen_surface:element({
            id = "select" .. key, type = "select",
            rect = { unit = "px", x = pos_x + 200, y = pos_y, w = W - pos_x - 210, h = 20 },
            props = { options = ol, selected = sel },
            style = { bg = Colors.SELECT_BG, text = Colors.TEXT_DEFAULT, font_size = Statics.FONT_DEFAULT },
            on_change = function(value, player)
                local id = ol[tonumber(value)+1]:match("ID:%s*(%d+)")
                local ref_dev = getDeviceByRefId(id)
                if ref_dev == nil then
                    print("Gerät mit ID " .. id .. " nicht gefunden")
                else
                    dev.prefab = ref_dev.prefab_hash
                    dev.name = ref_dev.name_hash
                    screen_surface:get("select" .. key):set_props({selected = tonumber(value)})
                end
            end,
        })
        pos_y = pos_y + 22
    end


    screen_surface:element({
        id = "btn_settings_ok", type = "button",
        rect = { unit = "px", x = pos_x, y = pos_y, w = W - pos_x - 20, h = 30 },
        style = { font_size = Statics.FONT_DEFAULT, bg = Colors.BTN_GREEN, text = Colors.TEXT_DEFAULT },
        props = { text = "Fertig", z_index = 101 },
        on_click = function(playerName)
            saveSettings()
            ss.ui.activate("settings")
           
        end
    })


    screen_surface:commit()
end


-- ####################################################
-- START: MENÜ EINSTELLUNGEN
-- ####################################################
function buildSettings()


    print("Call:buildSettings()")
    local devs = Settings.Devices
    local screen_surface = screens.settings.surface
    local pos_x = 20
    local pos_y = 30


    screen_surface:clear()


    pos_y = printScreenTitel(screen_surface, "Einstellungen")


    screen_surface:element({
        id = "btn_settings_ok", type = "button",
        rect = { unit = "px", x = pos_x, y = pos_y, w = W - pos_x - 20, h = 30 },
        style = { font_size = Statics.FONT_DEFAULT, bg = Colors.BTN_GREEN, text = Colors.BTN_TEXT },
        props = { text = "Fertig", z_index = 101 },
        on_click = function(playerName)
            saveSettings()
            ss.ui.activate("main")
           
        end
    })
    pos_y = pos_y + 35


    -- Button zu Geräteauswahl
    screen_surface:element({ id = "btn_settings_dev", type = "button",
        rect = { unit = "px", x = pos_x, y = pos_y, w = W - pos_x - 20, h = 30 },
        props = { text = "Geräteliste", z_index = 101 },
        style = { font_size = Statics.FONT_DEFAULT, bg = Colors.BTN_BLUE,  color = Colors.BTN_TEXT },
        on_click = function(playerName)
            initDevices()
            buildDevices()
            ss.ui.activate("devices")
        end
    })


    screen_surface:commit()
end


-- ####################################################
-- START: MENÜ MAIN
-- ####################################################
function buildMain()


    --print("Call:buildMain()")
    local devs = Settings.Devices
    local screen_surface = screens.main.surface
    local pos_x = 20
    local pos_y = 30
    screen_surface:clear()


    pos_y = printScreenTitel(screen_surface, "DEMO")


    screen_surface:commit()
end


-- ####################################################
-- MAIN-TICK
-- ####################################################


--local accum = 0
function tick(dt)
    local devs = Settings.Devices
   
    for _, s in ipairs(screens) do
        if not s.loaded then
            return
        end
    end


    sleep(0.2)
end


-- ####################################################
-- Abschluss
-- ####################################################


-- Menüs bauen
buildDevices()
buildSettings()
buildMain()


-- Timer starten
initDevices()


-- Hauptmenü aktivieren
ss.ui.activate("main")