|
| 1 | +-------------------------------------------------------------------------------- |
| 2 | +-------------------------------------------------------------------------------- |
| 3 | + |
| 4 | +function widget:GetInfo() |
| 5 | + return { |
| 6 | + name = "Chobby Volume Panel", |
| 7 | + desc = "Ports the EpicMenu volume/clock panel to chobby", |
| 8 | + author = "Birdulon", |
| 9 | + date = "2020-10-07", |
| 10 | + license = "GNU GPL, v2 or later", |
| 11 | + layer = 0, |
| 12 | +-- handler = true, |
| 13 | + enabled = true, |
| 14 | + } |
| 15 | +end |
| 16 | + |
| 17 | +-------------------------------------------------------------------------------- |
| 18 | +-------------------------------------------------------------------------------- |
| 19 | +local IMAGE_DIR = LUA_DIRNAME .. "images/" |
| 20 | +local IMAGE_VOLUME = IMAGE_DIR .. "vol.png" |
| 21 | +local IMAGE_VOL_MUSIC = IMAGE_DIR .. "vol_music.png" |
| 22 | +local IMAGE_CLOCK = IMAGE_DIR .. "clock.png" |
| 23 | + |
| 24 | +local INVISIBLE_COLOR = {0, 0, 0, 0} |
| 25 | +local TEXT_COLOR = {1, 1, 1, 1} |
| 26 | + |
| 27 | +local spGetConfigInt = Spring.GetConfigInt |
| 28 | +local spSetConfigInt = Spring.SetConfigInt |
| 29 | +local spEcho = Spring.Echo |
| 30 | +local Configuration |
| 31 | + |
| 32 | +local VolumePanel = {} |
| 33 | +local trackbarVolume |
| 34 | +local trackbarMusic |
| 35 | +local lblClock |
| 36 | + |
| 37 | + |
| 38 | +local function InitializeControls(window) |
| 39 | + local sliderWidth = 96 |
| 40 | + |
| 41 | + -------------------------------------------------------------------------------- |
| 42 | + -- Make Widgets |
| 43 | + local imageVolume = Image:New{tooltip = 'Volume', file = IMAGE_VOLUME, width = 16, height = 16} |
| 44 | + local imageMusic = Image:New{tooltip = 'Music', file = IMAGE_VOL_MUSIC, width = 16, height = 16} |
| 45 | + local imageClock = Image:New{file = IMAGE_CLOCK, width = 20, height = 26} |
| 46 | + |
| 47 | + lblClock = Label:New{name = 'lblClock', caption = 'Clock', width = 45, height = 26, textColor = TEXT_COLOR} |
| 48 | + trackbarVolume = Trackbar:New{ |
| 49 | + tooltip = 'Volume', |
| 50 | + height = 16, |
| 51 | + width = sliderWidth, |
| 52 | + trackColor = TEXT_COLOR, |
| 53 | + value = spGetConfigInt("snd_volmaster", 50), |
| 54 | + OnChange = { |
| 55 | + function(self) |
| 56 | + spSetConfigInt("snd_volmaster", self.value) |
| 57 | + end |
| 58 | + }, |
| 59 | + } |
| 60 | + trackbarMusic = Trackbar:New{ |
| 61 | + tooltip = 'Music', |
| 62 | + height = 16, |
| 63 | + width = sliderWidth, |
| 64 | + min = 0, |
| 65 | + max = 1, |
| 66 | + step = 0.01, |
| 67 | + trackColor = TEXT_COLOR, |
| 68 | + value = WG.Chobby.Configuration.menuMusicVolume or 0.0, |
| 69 | + OnChange = { |
| 70 | + function(self) |
| 71 | + WG.Chobby.Configuration:SetConfigValue("menuMusicVolume", self.value) |
| 72 | + end |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + -------------------------------------------------------------------------------- |
| 77 | + -- Make Sublayouts |
| 78 | + local stackClock = StackPanel:New{ |
| 79 | + orientation = 'horizontal', |
| 80 | + width = 62, |
| 81 | + height = '100%', |
| 82 | + resizeItems = false, |
| 83 | + autoArrangeV = false, |
| 84 | + autoArrangeH = false, |
| 85 | + padding = {0, 2, 0, 0}, |
| 86 | + itemMargin = {1, 0, 0, 0}, |
| 87 | + children = {imageClock, lblClock}, |
| 88 | +-- parent = window, |
| 89 | + } |
| 90 | + local gridVolume = Grid:New{ |
| 91 | + height = '100%', |
| 92 | + width = sliderWidth + 25, |
| 93 | + columns = 2, |
| 94 | + rows = 2, |
| 95 | + resizeItems = false, |
| 96 | + margin = {0, 0, 0, 0}, |
| 97 | + padding = {0, 0, 0, 0}, |
| 98 | + itemPadding = {0, 0, 0, 0}, |
| 99 | + itemMargin = {0, 0, 0, 0}, |
| 100 | + children = {imageVolume, trackbarVolume, imageMusic, trackbarMusic}, |
| 101 | +-- parent = window, |
| 102 | + } |
| 103 | + |
| 104 | + -------------------------------------------------------------------------------- |
| 105 | + -- Make Layout |
| 106 | + local stackFull = StackPanel:New{ |
| 107 | + orientation = 'horizontal', |
| 108 | + width = '100%', |
| 109 | + height = 32, |
| 110 | + resizeItems = false, |
| 111 | + autoArrangeV = false, |
| 112 | + autoArrangeH = false, |
| 113 | + padding = {0, 0, 0, 0}, |
| 114 | + itemMargin = {0, 0, 0, 0}, |
| 115 | + children = {gridVolume, stackClock}, |
| 116 | + parent = window, |
| 117 | + } |
| 118 | +end |
| 119 | + |
| 120 | + |
| 121 | +function VolumePanel.GetControl() |
| 122 | + local window = Control:New { |
| 123 | + x = 0, |
| 124 | + y = 0, |
| 125 | + right = 0, |
| 126 | + bottom = 0, |
| 127 | + padding = {0, 0, 0, 0}, |
| 128 | + OnParent = { |
| 129 | + function(obj) |
| 130 | + if obj:IsEmpty() then |
| 131 | + InitializeControls(obj) |
| 132 | + end |
| 133 | + end |
| 134 | + }, |
| 135 | + } |
| 136 | + -- Other setup that we can only do once Chobby has loaded stuff |
| 137 | + --Configuration = WG.Chobby.Configuration |
| 138 | + --Configuration:AddListener("OnConfigurationChange", onConfigurationChange) |
| 139 | + return window |
| 140 | +end |
| 141 | + |
| 142 | +-------------------------------------------------------------------------------- |
| 143 | +-------------------------------------------------------------------------------- |
| 144 | +-- Widget Updates |
| 145 | +local function updateTrackbarVolume(value) |
| 146 | + if value then |
| 147 | + trackbarVolume.value = value |
| 148 | + trackbarVolume.tooltip = string.format("Volume (%d%%)", value) |
| 149 | + end |
| 150 | +end |
| 151 | + |
| 152 | +local function updateTrackbarMusic(value) |
| 153 | + if value then |
| 154 | + trackbarMusic.value = value |
| 155 | + trackbarMusic.tooltip = string.format("Music (%d%%)", math.floor(value*100)) |
| 156 | + end |
| 157 | +end |
| 158 | + |
| 159 | +-- local function onConfigurationChange(listener, key, value) |
| 160 | +-- if key == "menuMusicVolume" then |
| 161 | +-- updateTrackbarMusic(value) |
| 162 | +-- end |
| 163 | +-- end |
| 164 | + |
| 165 | +-------------------------------------------------------------------------------- |
| 166 | +-------------------------------------------------------------------------------- |
| 167 | +-- Widget Interface |
| 168 | +local dLastSync = 0 |
| 169 | +function widget:Update() |
| 170 | + dLastSync = dLastSync + 1 |
| 171 | + if dLastSync < 10 then -- Only run this stuff every 10 updates |
| 172 | + return |
| 173 | + end |
| 174 | + dLastSync = 0 |
| 175 | + |
| 176 | + if lblClock then |
| 177 | + lblClock:SetCaption(os.date("%H:%M")) |
| 178 | + end |
| 179 | + |
| 180 | + updateTrackbarVolume(spGetConfigInt("snd_volmaster")) |
| 181 | + updateTrackbarMusic(WG.Chobby.Configuration.menuMusicVolume) |
| 182 | +end |
| 183 | + |
| 184 | +function widget:Initialize() |
| 185 | + -- Very limited initialization as many things aren't available until GetControl() |
| 186 | + VFS.Include(LUA_DIRNAME .. "widgets/chobby/headers/exports.lua", nil, VFS.RAW_FIRST) |
| 187 | + WG.VolumePanel = VolumePanel |
| 188 | +end |
| 189 | + |
| 190 | +function widget:Shutdown() |
| 191 | +-- if Configuration then |
| 192 | +-- Configuration:RemoveListener("OnConfigurationChange", onConfigurationChange) |
| 193 | +-- end |
| 194 | +end |
| 195 | + |
| 196 | + |
| 197 | +-------------------------------------------------------------------------------- |
| 198 | +-------------------------------------------------------------------------------- |
0 commit comments