Office Space Out 2 Lua source code

--
-- Copyright (c) 2010 Tuomas Pelkonen, tuomaspelkonen.com
--
-- Permission is hereby granted, free of charge, to any person
-- obtaining a copy of this software and associated documentation
-- files (the "Software"), to deal in the Software without
-- restriction, including without limitation the rights to use,
-- copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the
-- Software is furnished to do so, subject to the following
-- conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-- OTHER DEALINGS IN THE SOFTWARE.
--

local main_page = {}
local keyboard_page = {}
local record_page = {}
local types = {"Quiet", "Normal", "Loud"}
local selected_type = 1

function record_page.stopped()
    ui.stop_recording(record_page.recorder)
    record_page.recorder = nil
end

function record_page.record(x)
    local function callback()
        record_page.recorder = ui.record_audio("temp" .. x)
        if not record_page.recorder then
            ui.alert_view("Could not record audio. Are you sure microphone" ..
                          " is activated?")
        else
            ui.alert_view("Recording", "Stop", record_page.stopped)
        end
    end

    return callback
end

function record_page.stop_playing()
    ui.delete_sound(record_page.sound)
    record_page.sound = nil
end

function record_page.play(x)
    local function callback()
        record_page.sound = ui.load_sound("temp" .. x, true)
        if record_page.sound then
            ui.play_sound(record_page.sound)
            ui.alert_view("Playing", "Stop", record_page.stop_playing)
        end
    end

    return callback
end

function record_page.cancel()
    hide(record_page)
    show(keyboard_page)
end

function record_page.save()
    local text = ui.get_text(record_page.text_field)

    if not text or text:len() == 0 then
        ui.alert_view("Enter a type for the keyboard sounds")
        return
    end

    if not common.copy_file("temp1", "fast_" .. text) then
        ui.alert_view("No fast typing recorded")
        return
    end

    if not common.copy_file("temp2", "slow_" .. text) then
        ui.alert_view("No slow typing recorded")
        return
    end

    types[#types + 1] = text
    hide(record_page)
    show(keyboard_page)

    local str = table.save(types)
    common.write_file("types.txt", str)
end

function record_page.load()
    record_page.title = ui.create_label({x = 0, width = 320,
                                         y = 10, size = 24,
                                         alignment = ui.CENTER,
                                         text = "Record keyboard sounds",
                                         font = "Arial-BoldMT"})

    record_page.label1 = ui.create_label({text = "Fast typing",
                                          x = 10, y = 110, size = 20.0,
                                          width = 300,
                                          alignment = ui.CENTER,
                                          font = "Arial-BoldMT"})

    record_page.label2 = ui.create_label({text = "Slow typing",
                                          x = 10, y = 210, size = 20.0,
                                          width = 300,
                                          alignment = ui.CENTER,
                                          font = "Arial-BoldMT"})

    for i = 1, 2 do
        record_page["record" .. i]  =
            ui.create_button({image = "oso2_record.png",
                              background_image = "oso2_box.png",
                              x = 20, height = 40, width = 130,
                              y = i * 100 + 50,
                              onclick = record_page.record(i)})
        record_page["play" .. i]  =
            ui.create_button({image = "oso2_play.png",
                              background_image = "oso2_box.png",
                              x = 170, height = 40, width = 130,
                              y = i * 100 + 50,
                              onclick = record_page.play(i)})
    end

    record_page.text_field = ui.create_textfield({x = 170,
                                                  y = 60,
                                                  width = 140,
                                                  height = 30})

    record_page.label3 = ui.create_label({text = "Keyboard type:",
                                          x = 10, y = 62,
                                          font = "Arial-BoldMT"})

    record_page.toolbar =
        ui.create_toolbar({buttons = {"Cancel", "", "Save"},
                           callbacks = {record_page.cancel, nil,
                                        record_page.save}})
end

function keyboard_page.activated()
    ui.reload_table_data(keyboard_page.table)
end

function keyboard_page.get_data(row)
    if row == 0 then
        return #types
    else
        return types[row]
    end
end

function keyboard_page.is_selected(row)
    return row == selected_type
end

function keyboard_page.change(row)
    selected_type = row
end

function keyboard_page.new()
    hide(keyboard_page)
    show(record_page)
end

function keyboard_page.back()
    hide(keyboard_page)
    show(main_page)
end

function keyboard_page.delete_answer(selected)
    if selected == 1 then
        for i = 4, #types do
            common.remove_file("slow_" .. types[i])
            common.remove_file("fast_" .. types[i])
        end
        types = {"Quiet", "Normal", "Loud"}
        if selected_type > 3 then
            selected_type = 1
        end

        local str = table.save(types)
        common.write_file("types.txt", str)

        ui.reload_table_data(keyboard_page.table)
    end
end

function keyboard_page.delete()
    ui.alert_view("Are you sure you want to delete all your recorded sounds?",
                  keyboard_page.delete_answer)
end

function keyboard_page.load()
    keyboard_page.title = ui.create_label({x = 0, width = 320,
                                           y = 10, size = 24,
                                           alignment = ui.CENTER,
                                           text = "Select keyboard",
                                           font = "Arial-BoldMT"})

    keyboard_page.new_type =
        ui.create_table({x = 0, width = 320,
                         y = 40,
                         data = {"Record new keyboard type"},
                         selected = keyboard_page.new})

    keyboard_page.table =
        ui.create_table({x = 0, width = 320,
                         y = 100, height = 300,
                         data = keyboard_page.get_data,
                         checkmark = keyboard_page.is_selected,
                         selected = keyboard_page.change})

    keyboard_page.toolbar =
        ui.create_toolbar({buttons = {"Back", "", "Delete user sounds"},
                           callbacks = {keyboard_page.back, nil,
                                        keyboard_page.delete}})
end

function main_page.select_keyboard()
    hide(main_page)
    show(keyboard_page)
end

local timer = nil

function main_page.next_sound()
    -- fast, slow, mouse, silence

    timer = nil

    if main_page.sound then
        ui.delete_sound(main_page.sound)
        main_page.sound = nil
    end

    main_page.step = main_page.step + 1
    if main_page.step == 5 then
        main_page.step = 1
    end

    local duration =
        ui.get_value(main_page["slider" .. main_page.step]) / 100 * 30

    if duration == 0 then
        main_page.next_sound()
        return
    end

    local sound_file = nil
    local recorded_sound = false

    if main_page.step == 1 then
        sound_file = "fast_"
    elseif main_page.step == 2 then
        sound_file = "slow_"
    elseif main_page.step == 3 then
        sound_file = "mouse"
    end

    if main_page.step == 1 or main_page.step == 2 then
        sound_file = sound_file .. types[selected_type]
        if selected_type >= 4 then
            recorded_sound = true
        end
    end

    if sound_file then
        print(string.format("Playing sound %s for %d seconds", sound_file,
                            duration))
    else
        print(string.format("Silence for %d seconds", duration))
    end

    if sound_file then
        main_page.sound = ui.load_sound(sound_file, recorded_sound)
        if not main_page.sound then
            ui.alert_view("Could not load sound " .. sound_file)
            return
        end
        ui.play_sound(main_page.sound, -1)
    else
        main_page.sound = nil
    end

    timer = ui.create_timer({repeats = FALSE,
                             interval = duration,
                             callback = main_page.next_sound})
end

function main_page.stop()
    if timer then
        ui.stop_timer(timer)
        timer = nil
    end

    if main_page.sound then
        ui.delete_sound(main_page.sound)
        main_page.sound = nil
    end
end

function main_page.clicked()

    local all_zeros = true
    for i = 1, 4 do
        if ui.get_value(main_page["slider" .. i]) ~= 0 then
            all_zeros = false
            break
        end
    end

    if all_zeros then
        ui.alert_view("All durations are zero")
        return
    end

    main_page.step = 0
    main_page.next_sound()
    ui.alert_view("Playing sounds in a loop", "Stop", main_page.stop)
end

function main_page.slider_changed(label)
    function callback(slider)
        local value = ui.get_value(slider)

        ui.set_text(label, string.format("%2d s", value / 100 * 30))

        print(value)
    end

    return callback
end

function main_page.activated()
    ui.set_text(main_page.type, 1, nil, types[selected_type])
end

function main_page.load()
    main_page.table = ui.create_table({x = 0, width = 320,
                                       y = 80, height = 200,
                                       data = {"Fast typing",
                                               "Slow typing",
                                               "Mouse usage",
                                               "Silence"}})

    main_page.extra_view = ui.create_view({x = 0, y = 0, width = 320,
                                           height = 460, color = "clear"})

    main_page.title = ui.create_label({x = 0, width = 320,
                                       y = 10, size = 24,
                                       alignment = ui.CENTER,
                                       text = "Office Space Out 2",
                                       font = "Arial-BoldMT"})

    main_page.label = ui.create_label({x = 0, width = 320,
                                       y = 60, size = 22,
                                       alignment = ui.CENTER,
                                       text = "Durations for sounds",
                                       font = "Arial-BoldMT"})

    main_page.type = ui.create_table({x = 0, y = 280,
                                      width = 320,
                                      data = {{"Keyboard type", "Normal"}},
                                      selected = main_page.select_keyboard})

    main_page.button = ui.create_button({y = 380,
                                         image = "oso2_play.png",
                                         background_image = "oso2_box.png",
                                         onclick = main_page.clicked})

    for i = 1, 4 do
        main_page["label" .. i] = ui.create_label({text = " 0 s",
                                                   x = 120,
                                                   width = 60,
                                                   height = 40,
                                                   y = 50 + i * 44,
                                                   alignment = ui.RIGHT,
                                                   font = "Arial-BoldMT",
                                                   size = 20.0})
        main_page["slider" .. i] =
            ui.create_slider({x = 190,
                              y = 59 + i * 44,
                              width = 110,
                              height = 20,
                              on_value_changed = main_page.slider_changed(
                                  main_page["label" .. i])})

    end
end

function main()
    print "RUNNING"

    local str = common.read_file("types.txt")
    if str then
        types = table.load(str)
    end

    main_page.load()
    keyboard_page.load()
    record_page.load()
    show(main_page)
end
  • Share/Bookmark

Trackbacks / Pingbacks

  1. Office Space Out 2 | Rants and Apps

Leave a Reply