diff --git a/lua/Scripts/Twitch2DCSGameGUI.lua b/lua/Scripts/Twitch2DCSGameGUI.lua index f947e71..6b8b92c 100644 --- a/lua/Scripts/Twitch2DCSGameGUI.lua +++ b/lua/Scripts/Twitch2DCSGameGUI.lua @@ -13,6 +13,7 @@ local string = base.string local math = base.math local assert = base.assert local pairs = base.pairs +local ipairs = base.ipairs local lfs = require('lfs') local net = require('net') @@ -31,7 +32,7 @@ local TwitchClient = { joinPartSkin = nil, userSkins = {}, nextUserIndex = 1, - viewerCount = 0 + userNames = {} } local TwitchClient_mt = { __index = TwitchClient } local client = nil @@ -72,7 +73,7 @@ function TwitchClient:getSkinForUser(user) self.nextUserIndex = self.nextUserIndex + 1 - if self.nextUserIndex > table.getn(self.config.skins) then + if self.nextUserIndex > #self.config.skins.messageColors then self.nextUserIndex = 1 end @@ -87,6 +88,30 @@ function TwitchClient:canLogin() (self.config.oathToken ~= nil and self.config.oathToken ~= '') end +function TwitchClient:addViewer(user) + if user == self.config.username then + return + end + local function hasValue (tab, val) + for index, value in ipairs (tab) do + -- We grab the first index of our sub-table instead + if value == val then + return true + end + end + + return false + end + if not hasValue(client.userNames, user) then + table.insert(client.userNames,user) + end + client:updateTitle() +end + +function TwitchClient:removeViewer(user) + table.remove(client.userNames, user) +end + function TwitchClient.onUISendMessage(args) local username = client.config.username local skin = client:getSkinForUser(username) @@ -108,22 +133,26 @@ end function TwitchClient.onUserPart(cmd) client.ui:addMessage(client:getTimeStamp().." "..cmd.user.." left.", client.joinPartSkin) - client.viewerCount = client.viewerCount - 1 + client:removeViewer(cmd.user) end function TwitchClient.onUserJoin(cmd) + if cmd.user == client.config.username then + return + end client.ui:addMessage(client:getTimeStamp().." "..cmd.user.." joined.", client.joinPartSkin) - client.viewerCount = client.viewerCount + 1 - client:updateTitle() + client:addViewer(cmd.user) end function TwitchClient.onUserMessage(cmd) local skin = client:getSkinForUser(cmd.user) client.ui:addMessage(client:getTimeStamp().." "..cmd.user..": "..cmd.param2, skin) + client:addViewer(cmd.user) end function TwitchClient:updateTitle() - client.ui:setTitle("Twitch Chat | "..client.viewerCount.." viewers") + local viewerCount = #client.userNames + client.ui:setTitle("Twitch Chat | "..viewerCount.." viewers") end function TwitchClient:connect() diff --git a/lua/Scripts/twitch/ui.lua b/lua/Scripts/twitch/ui.lua index 3ff8fb5..134a116 100644 --- a/lua/Scripts/twitch/ui.lua +++ b/lua/Scripts/twitch/ui.lua @@ -22,7 +22,8 @@ local ListBoxItem = require('ListBoxItem') local Tools = require('tools') local MulChat = require('mul_chat') local tracer = require("twitch.tracer") -local utils = require('twitch.utils') +local utils = require('twitch.utils') +local Input = require('Input') local modes = { hidden = "hidden", @@ -35,10 +36,10 @@ local UI = { _currentWheelValue = 0, _listStatics = {}, _listMessages = {}, - _nextChatColorIndex = 1, _currentMode = modes.read, _x, _y, + _isKeyboardLocked = false, modes = modes } local UI_mt = { __index = UI } @@ -210,9 +211,9 @@ function UI:new(hotkey, defaultMode, x, y) ui:resizeEditMessage() if ui._currentMode == ui.modes.write then - ui:readMode() - else ui:writeMode() + else + ui:readMode() end ui.window:addPositionCallback(function() ui:positionCallback() end) @@ -248,7 +249,7 @@ function UI:writeMode() self.pDown:setVisible(true) self.eMessage:setFocused(true) - DCS.banKeyboard(true) + self:lockKeyboardInput(true) self:updateListM() end @@ -276,7 +277,7 @@ function UI:readMode() self.pDown:setVisible(false) self.eMessage:setFocused(false) - DCS.banKeyboard(false) + self:lockKeyboardInput(false) self:updateListM() end @@ -289,6 +290,43 @@ function UI:nextMode() end end +function UI:lockKeyboardInput(lock) + if lock then + if not self._isKeyboardLocked then + -- блокируем все кнопки клавиатуры, + -- кроме кнопок управления чатом + local keyboardEvents = Input.getDeviceKeys(Input.getKeyboardDeviceName()) + local inputActions = Input.getEnvTable().Actions + + local removeCommandEvents = function(commandEvents) + for i, commandEvent in ipairs(commandEvents) do + -- из массива удаляем элементы с конца + for j = #keyboardEvents, 1, -1 do + if keyboardEvents[j] == commandEvent then + table.remove(keyboardEvents, j) + + break + end + end + end + end + + removeCommandEvents(Input.getUiLayerCommandKeyboardKeys(inputActions.iCommandChat)) + removeCommandEvents(Input.getUiLayerCommandKeyboardKeys(inputActions.iCommandAllChat)) + removeCommandEvents(Input.getUiLayerCommandKeyboardKeys(inputActions.iCommandFriendlyChat)) + removeCommandEvents(Input.getUiLayerCommandKeyboardKeys(inputActions.iCommandChatShowHide)) + + DCS.lockKeyboardInput(keyboardEvents) + self._isKeyboardLocked = true + end + else + if self._isKeyboardLocked then + DCS.unlockKeyboardInput() + self._isKeyboardLocked = false + end + end +end + function UI:resize(w, h) self.window:setBounds(self._x, self._y, 360, 455) self.box:setBounds(0, 0, 360, 400)