Skip to content

Creating your own wallpapers

IgorTimofeev edited this page Jan 21, 2024 · 5 revisions

Desktop background in MineOS is simple GUI object that is managed by the system. Wallpapers are small applications which providing drawing method for this object and (if needed) a configuration method for the settings app.

preview

Creating your own wallpapers

If you want to make custom wallpaper for your beautiful system, start by creating a new directory with extension .wlp under the /Wallpapers/. For example, i will create /Wallpapers/Random.wlp.

The only required file under your directory is Main.lua. This script should return table that contains draw, and, optionally, configure functions. The draw function will receive wallpaper GUI.object instance where you can render your pixel data. Use Screen API for drawing. The configure function will receive layout instance where you can place some controls that will be shown under the wallpapers tab in MineOS settings.

Let's see example of main wallpaper script:

local screen = require("Screen")

local points = {}
local limit = 20
local backgroundColor = 0x0F0F0F
local pointColor = 0xFFFFFF

return {
  draw = function(wallpaper)
    -- Spawning points
    table.insert(points, {
      x = math.random(0, wallpaper.width  - 1),
      y = math.random(0, wallpaper.height - 1)
    })

    -- Remove out of limit points
    if #points > limit then
      table.remove(points, 1, #points - limit)
    end

    -- Clearing area
    screen.drawRectangle(wallpaper.x, wallpaper.y, wallpaper.width, wallpaper.height, backgroundColor, 0, " ")

    -- Drawing points
    for i = 1, #points do
      screen.set(wallpaper.x + points[i].x, wallpaper.y + points[i].y, pointColor, 0, " ")
    end
  end 
}

Random wallpaper preview

Configuration method

Now we want our wallpaper to be configurable in the settings. Let's improve the script to store config in file and make some controls for these settings.

local screen = require("Screen")
local system = require("System")
local filesystem = require("Filesystem")
local GUI = require("GUI")

-- Let's define a path to store the wallpaper config file
-- For convenience, we can do it directly with Main.lua script
local configPath = filesystem.path(system.getCurrentScript()) .. "Config.cfg"

-- Read config if it exists or create default one otherwise
local config =
  filesystem.exists(configPath) and
  filesystem.readTable(configPath) or
  {
    limit = 20,
    backgroundColor = 0x0F0F0F,
    pointColor = 0xFFFFFF
  }

-- Create a function to save config table
local function saveConfig()
  filesystem.writeTable(configPath, config)
end

local points = {}

return {
  draw = function(wallpaper)
    -- Spawning points
    table.insert(points, {
      x = math.random(0, wallpaper.width  - 1),
      y = math.random(0, wallpaper.height - 1)
    })

    -- Remove out of limit points
    if #points > config.limit then
      table.remove(points, 1, #points - config.limit)
    end

    -- Clearing area
    screen.drawRectangle(wallpaper.x, wallpaper.y, wallpaper.width, wallpaper.height, config.backgroundColor, 0, " ")

    -- Drawing points
    for i = 1, #points do
      screen.set(wallpaper.x + points[i].x, wallpaper.y + points[i].y, config.pointColor, 0, " ")
    end
  end,

  configure = function(layout)
    -- Add selector for the background color
    layout:addChild(GUI.colorSelector(1, 1, 36, 3, config.backgroundColor, "Background color")).onColorSelected = function(_, selector)
      config.backgroundColor = selector.color

      -- Save config to file when parameter has changed
      saveConfig()
    end

    -- Add selector for the point color
    layout:addChild(GUI.colorSelector(1, 1, 36, 3, config.pointColor, "Point color")).onColorSelected = function(_, selector)
      config.pointColor = selector.color
      saveConfig()
    end
  end
}

Configuration preview

Clone this wiki locally