From 3f00fb038aeed8875bb705acc72ba9c3fb7cc924 Mon Sep 17 00:00:00 2001 From: alexrazor Date: Sun, 22 Sep 2019 12:48:33 +0300 Subject: [PATCH 1/2] Screen size font adaptation --- chat2_client.lua | 218 ++++++++++++++++++++++++++--------------------- chat2_shared.lua | 7 ++ 2 files changed, 127 insertions(+), 98 deletions(-) diff --git a/chat2_client.lua b/chat2_client.lua index 6254df7..9da09d1 100644 --- a/chat2_client.lua +++ b/chat2_client.lua @@ -3,9 +3,9 @@ local chatInstanceLoading local chatInstanceLoaded local state = { - show = false, - activeInputKeyButton = nil, - activeScrollKeyButton = nil + show = false, + activeInputKeyButton = nil, + activeScrollKeyButton = nil } -- Define your local/global/team/etc input types here @@ -18,13 +18,13 @@ local state = { -- Do not forget to execute exports.chat2:useDefaultOutput(false) -- and then handle this messageTypes in "onPlayerChat" event handlers local inputKeyButtons = { - ["t"] = {"say", 0}, - ["y"] = {"teamsay", 2} + ["t"] = {"say", 0}, + ["y"] = {"teamsay", 2} } local scrollKeyButtons = { - ["pgup"] = "scrollup", - ["pgdn"] = "scrolldown" + ["pgup"] = "scrollup", + ["pgdn"] = "scrolldown" } addEvent("onChat2Loaded") @@ -34,162 +34,184 @@ addEvent("onChat2Clear", true) addEvent("onChat2Show", true) function create() - chatInstance = guiGetBrowser(guiCreateBrowser(0.01, 0.01, 0.25, 0.4, true, true, true)) - chatInstanceLoading = true - addEventHandler("onClientBrowserCreated", chatInstance, load) + chatInstance = guiGetBrowser(guiCreateBrowser(0.01, 0.01, 0.25, 0.4, true, true, true)) + chatInstanceLoading = true + addEventHandler("onClientBrowserCreated", chatInstance, load) end function load() - loadBrowserURL(chatInstance, "http://mta/local/index.html") + loadBrowserURL(chatInstance, "http://mta/local/index.html") end function output(message) - if not chatInstanceLoaded then - return setTimer(output, 250, 1, message) - end + if not chatInstanceLoaded then + return setTimer(output, 250, 1, message) + end - if not state.show then - return - end + if not state.show then + return + end - execute(string.format("addMessage(%s)", toJSON(message))) + execute(string.format("addMessage(%s)", toJSON(message))) end function clear() - execute("clear()") + execute("clear()") end function isChatVisible() - return state.show + return state.show end function show(bool) - if chatInstanceLoaded ~= true then - if chatInstanceLoading ~= true then - create() - return setTimer(show, 300, 1, bool) - else - return setTimer(show, 300, 1, bool) - end - end + if chatInstanceLoaded ~= true then + if chatInstanceLoading ~= true then + create() + return setTimer(show, 300, 1, bool) + else + return setTimer(show, 300, 1, bool) + end + end - execute(string.format("show(%s)", tostring(bool))) - state.show = bool + execute(string.format("show(%s)", tostring(bool))) + state.show = bool end function registerKeyButtons() - for keyButton, definition in pairs(inputKeyButtons) do - bindKey(keyButton, "down", onChatInputButton, keyButton, definition) - end + for keyButton, definition in pairs(inputKeyButtons) do + bindKey(keyButton, "down", onChatInputButton, keyButton, definition) + end - for keyButton, definition in pairs(scrollKeyButtons) do - bindKey(keyButton, "down", onChatScrollStartButton, keyButton, definition) - end + for keyButton, definition in pairs(scrollKeyButtons) do + bindKey(keyButton, "down", onChatScrollStartButton, keyButton, definition) + end - for keyButton, definition in pairs(scrollKeyButtons) do - bindKey(keyButton, "up", onChatScrollStopButton, keyButton, definition) - end + for keyButton, definition in pairs(scrollKeyButtons) do + bindKey(keyButton, "up", onChatScrollStopButton, keyButton, definition) + end end function execute(eval) - executeBrowserJavascript(chatInstance, eval) + executeBrowserJavascript(chatInstance, eval) end function onChatLoaded() - chatInstanceLoaded = true - focusBrowser(chatInstance) + chatInstanceLoaded = true + focusBrowser(chatInstance) + adoptFont() end function onChatInputButton(_, _, keyButton, definition) - if not state.show then - return - end + if not state.show then + return + end - if state.activeInputKeyButton then - return - end + if state.activeInputKeyButton then + return + end - execute(string.format("showInput(%s)", toJSON(definition[1]))) - focusBrowser(chatInstance) - guiSetInputEnabled(true) - state.activeInputKeyButton = keyButton + execute(string.format("showInput(%s)", toJSON(definition[1]))) + focusBrowser(chatInstance) + guiSetInputEnabled(true) + state.activeInputKeyButton = keyButton end function onChatEnterButton(message) - if not state.show then - return - end + if not state.show then + return + end - if not state.activeInputKeyButton then - return - end + if not state.activeInputKeyButton then + return + end - execute("hideInput()") - guiSetInputEnabled(false) - triggerServerEvent("onChat2Message", resourceRoot, message, inputKeyButtons[state.activeInputKeyButton][2]) - state.activeInputKeyButton = nil + execute("hideInput()") + guiSetInputEnabled(false) + triggerServerEvent("onChat2Message", resourceRoot, message, inputKeyButtons[state.activeInputKeyButton][2]) + state.activeInputKeyButton = nil end function onChatScrollStartButton(_, _, keyButton, definition) - if state.activeScrollKeyButton then - return - end + if state.activeScrollKeyButton then + return + end - execute(string.format("startScroll(%s)", toJSON(definition))) - state.activeScrollKeyButton = keyButton + execute(string.format("startScroll(%s)", toJSON(definition))) + state.activeScrollKeyButton = keyButton end function onChatScrollStopButton(_, _, _, definition) - if not state.activeScrollKeyButton then - return - end + if not state.activeScrollKeyButton then + return + end - execute("stopScroll()") - state.activeScrollKeyButton = nil + execute("stopScroll()") + state.activeScrollKeyButton = nil end function listenForOutputChatBox(_, _, _, _, _, message, r, g, b) - local hexColor = "" + local hexColor = "" - if (r and g and b) then - hexColor = RGBToHex(r, g, b) - end + if (r and g and b) then + hexColor = RGBToHex(r, g, b) + end - output(string.format("%s%s", hexColor, message)) - return "skip" + output(string.format("%s%s", hexColor, message)) + return "skip" end function listenForShowChat(_, _, _, _, _, bool) - show(bool) - return "skip" + show(bool) + return "skip" end function listenForClearChatBox() - clear() - return "skip" + clear() + return "skip" end function onClientResourceStart() - showChat(false) - addDebugHook("preFunction", listenForShowChat, {"showChat"}) - addDebugHook("preFunction", listenForOutputChatBox, {"outputChatBox"}) - addDebugHook("preFunction", listenForClearChatBox, {"clearChatBox"}) - showChat(true) + showChat(false) + addDebugHook("preFunction", listenForShowChat, {"showChat"}) + addDebugHook("preFunction", listenForOutputChatBox, {"outputChatBox"}) + addDebugHook("preFunction", listenForClearChatBox, {"clearChatBox"}) + showChat(true) - registerKeyButtons() + registerKeyButtons() end function onClientResourceStop() - showChat(false) - - if state.activeInputKeyButton then - guiSetInputEnabled(false) - end - - removeDebugHook("preFunction", listenForShowChat) - removeDebugHook("preFunction", listenForOutputChatBox) - removeDebugHook("preFunction", listenForClearChatBox) - showChat(true) + showChat(false) + + if state.activeInputKeyButton then + guiSetInputEnabled(false) + end + + removeDebugHook("preFunction", listenForShowChat) + removeDebugHook("preFunction", listenForOutputChatBox) + removeDebugHook("preFunction", listenForClearChatBox) + showChat(true) +end + +function getAspectRatio() + local dxResolution + local dw, dh = guiGetScreenSize() + local aspectRatio = math.round( dw / dh, 2 ) + local dxAspectRatio = "16:9" + if aspectRatio == 1.33 then dxAspectRatio = "4:3" + elseif aspectRatio == 1.25 then dxAspectRatio = "5:4" + elseif aspectRatio == 1.77 then dxAspectRatio = "16:9" + end + return dxAspectRatio +end + +function adoptFont() + if getAspectRatio() ~= "16:9" then + executeBrowserJavascript(chatInstance, + "let stylesElement = document.createElement('style');" .. + 'stylesElement.innerHTML = ".chat__message {font-size: 4vh} .chat__input {font-size: 4vh}";' .. + 'document.body.appendChild(stylesElement);') + end end addEventHandler("onChat2Loaded", resourceRoot, onChatLoaded) diff --git a/chat2_shared.lua b/chat2_shared.lua index 7e233f2..ab33239 100644 --- a/chat2_shared.lua +++ b/chat2_shared.lua @@ -5,3 +5,10 @@ function RGBToHex(red, green, blue) return string.format("#%.2X%.2X%.2X", red, green, blue) end + +function math.round(number, decimals, method) + decimals = decimals or 0 + local factor = 10 ^ decimals + if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor + else return tonumber(("%."..decimals.."f"):format(number)) end +end \ No newline at end of file From 4433f9beb3fd8cf4474489607b1a0f3c5ccf88c7 Mon Sep 17 00:00:00 2001 From: alexrazor Date: Sun, 22 Sep 2019 13:35:53 +0300 Subject: [PATCH 2/2] Convert to spaces --- chat2_client.lua | 224 +++++++++++++++++++++++------------------------ 1 file changed, 112 insertions(+), 112 deletions(-) diff --git a/chat2_client.lua b/chat2_client.lua index 9da09d1..07845cc 100644 --- a/chat2_client.lua +++ b/chat2_client.lua @@ -3,9 +3,9 @@ local chatInstanceLoading local chatInstanceLoaded local state = { - show = false, - activeInputKeyButton = nil, - activeScrollKeyButton = nil + show = false, + activeInputKeyButton = nil, + activeScrollKeyButton = nil } -- Define your local/global/team/etc input types here @@ -18,13 +18,13 @@ local state = { -- Do not forget to execute exports.chat2:useDefaultOutput(false) -- and then handle this messageTypes in "onPlayerChat" event handlers local inputKeyButtons = { - ["t"] = {"say", 0}, - ["y"] = {"teamsay", 2} + ["t"] = {"say", 0}, + ["y"] = {"teamsay", 2} } local scrollKeyButtons = { - ["pgup"] = "scrollup", - ["pgdn"] = "scrolldown" + ["pgup"] = "scrollup", + ["pgdn"] = "scrolldown" } addEvent("onChat2Loaded") @@ -34,184 +34,184 @@ addEvent("onChat2Clear", true) addEvent("onChat2Show", true) function create() - chatInstance = guiGetBrowser(guiCreateBrowser(0.01, 0.01, 0.25, 0.4, true, true, true)) - chatInstanceLoading = true - addEventHandler("onClientBrowserCreated", chatInstance, load) + chatInstance = guiGetBrowser(guiCreateBrowser(0.01, 0.01, 0.25, 0.4, true, true, true)) + chatInstanceLoading = true + addEventHandler("onClientBrowserCreated", chatInstance, load) end function load() - loadBrowserURL(chatInstance, "http://mta/local/index.html") + loadBrowserURL(chatInstance, "http://mta/local/index.html") end function output(message) - if not chatInstanceLoaded then - return setTimer(output, 250, 1, message) - end + if not chatInstanceLoaded then + return setTimer(output, 250, 1, message) + end - if not state.show then - return - end + if not state.show then + return + end - execute(string.format("addMessage(%s)", toJSON(message))) + execute(string.format("addMessage(%s)", toJSON(message))) end function clear() - execute("clear()") + execute("clear()") end function isChatVisible() - return state.show + return state.show end function show(bool) - if chatInstanceLoaded ~= true then - if chatInstanceLoading ~= true then - create() - return setTimer(show, 300, 1, bool) - else - return setTimer(show, 300, 1, bool) - end - end + if chatInstanceLoaded ~= true then + if chatInstanceLoading ~= true then + create() + return setTimer(show, 300, 1, bool) + else + return setTimer(show, 300, 1, bool) + end + end - execute(string.format("show(%s)", tostring(bool))) - state.show = bool + execute(string.format("show(%s)", tostring(bool))) + state.show = bool end function registerKeyButtons() - for keyButton, definition in pairs(inputKeyButtons) do - bindKey(keyButton, "down", onChatInputButton, keyButton, definition) - end + for keyButton, definition in pairs(inputKeyButtons) do + bindKey(keyButton, "down", onChatInputButton, keyButton, definition) + end - for keyButton, definition in pairs(scrollKeyButtons) do - bindKey(keyButton, "down", onChatScrollStartButton, keyButton, definition) - end + for keyButton, definition in pairs(scrollKeyButtons) do + bindKey(keyButton, "down", onChatScrollStartButton, keyButton, definition) + end - for keyButton, definition in pairs(scrollKeyButtons) do - bindKey(keyButton, "up", onChatScrollStopButton, keyButton, definition) - end + for keyButton, definition in pairs(scrollKeyButtons) do + bindKey(keyButton, "up", onChatScrollStopButton, keyButton, definition) + end end function execute(eval) - executeBrowserJavascript(chatInstance, eval) + executeBrowserJavascript(chatInstance, eval) end function onChatLoaded() - chatInstanceLoaded = true - focusBrowser(chatInstance) - adoptFont() + chatInstanceLoaded = true + focusBrowser(chatInstance) + adoptFont() end function onChatInputButton(_, _, keyButton, definition) - if not state.show then - return - end + if not state.show then + return + end - if state.activeInputKeyButton then - return - end + if state.activeInputKeyButton then + return + end - execute(string.format("showInput(%s)", toJSON(definition[1]))) - focusBrowser(chatInstance) - guiSetInputEnabled(true) - state.activeInputKeyButton = keyButton + execute(string.format("showInput(%s)", toJSON(definition[1]))) + focusBrowser(chatInstance) + guiSetInputEnabled(true) + state.activeInputKeyButton = keyButton end function onChatEnterButton(message) - if not state.show then - return - end + if not state.show then + return + end - if not state.activeInputKeyButton then - return - end + if not state.activeInputKeyButton then + return + end - execute("hideInput()") - guiSetInputEnabled(false) - triggerServerEvent("onChat2Message", resourceRoot, message, inputKeyButtons[state.activeInputKeyButton][2]) - state.activeInputKeyButton = nil + execute("hideInput()") + guiSetInputEnabled(false) + triggerServerEvent("onChat2Message", resourceRoot, message, inputKeyButtons[state.activeInputKeyButton][2]) + state.activeInputKeyButton = nil end function onChatScrollStartButton(_, _, keyButton, definition) - if state.activeScrollKeyButton then - return - end + if state.activeScrollKeyButton then + return + end - execute(string.format("startScroll(%s)", toJSON(definition))) - state.activeScrollKeyButton = keyButton + execute(string.format("startScroll(%s)", toJSON(definition))) + state.activeScrollKeyButton = keyButton end function onChatScrollStopButton(_, _, _, definition) - if not state.activeScrollKeyButton then - return - end + if not state.activeScrollKeyButton then + return + end - execute("stopScroll()") - state.activeScrollKeyButton = nil + execute("stopScroll()") + state.activeScrollKeyButton = nil end function listenForOutputChatBox(_, _, _, _, _, message, r, g, b) - local hexColor = "" + local hexColor = "" - if (r and g and b) then - hexColor = RGBToHex(r, g, b) - end + if (r and g and b) then + hexColor = RGBToHex(r, g, b) + end - output(string.format("%s%s", hexColor, message)) - return "skip" + output(string.format("%s%s", hexColor, message)) + return "skip" end function listenForShowChat(_, _, _, _, _, bool) - show(bool) - return "skip" + show(bool) + return "skip" end function listenForClearChatBox() - clear() - return "skip" + clear() + return "skip" end function onClientResourceStart() - showChat(false) - addDebugHook("preFunction", listenForShowChat, {"showChat"}) - addDebugHook("preFunction", listenForOutputChatBox, {"outputChatBox"}) - addDebugHook("preFunction", listenForClearChatBox, {"clearChatBox"}) - showChat(true) + showChat(false) + addDebugHook("preFunction", listenForShowChat, {"showChat"}) + addDebugHook("preFunction", listenForOutputChatBox, {"outputChatBox"}) + addDebugHook("preFunction", listenForClearChatBox, {"clearChatBox"}) + showChat(true) - registerKeyButtons() + registerKeyButtons() end function onClientResourceStop() - showChat(false) + showChat(false) - if state.activeInputKeyButton then - guiSetInputEnabled(false) - end + if state.activeInputKeyButton then + guiSetInputEnabled(false) + end - removeDebugHook("preFunction", listenForShowChat) - removeDebugHook("preFunction", listenForOutputChatBox) - removeDebugHook("preFunction", listenForClearChatBox) - showChat(true) + removeDebugHook("preFunction", listenForShowChat) + removeDebugHook("preFunction", listenForOutputChatBox) + removeDebugHook("preFunction", listenForClearChatBox) + showChat(true) end function getAspectRatio() - local dxResolution - local dw, dh = guiGetScreenSize() - local aspectRatio = math.round( dw / dh, 2 ) - local dxAspectRatio = "16:9" - if aspectRatio == 1.33 then dxAspectRatio = "4:3" - elseif aspectRatio == 1.25 then dxAspectRatio = "5:4" - elseif aspectRatio == 1.77 then dxAspectRatio = "16:9" - end - return dxAspectRatio + local dxResolution + local dw, dh = guiGetScreenSize() + local aspectRatio = math.round( dw / dh, 2 ) + local dxAspectRatio = "16:9" + if aspectRatio == 1.33 then dxAspectRatio = "4:3" + elseif aspectRatio == 1.25 then dxAspectRatio = "5:4" + elseif aspectRatio == 1.77 then dxAspectRatio = "16:9" + end + return dxAspectRatio end function adoptFont() - if getAspectRatio() ~= "16:9" then - executeBrowserJavascript(chatInstance, - "let stylesElement = document.createElement('style');" .. - 'stylesElement.innerHTML = ".chat__message {font-size: 4vh} .chat__input {font-size: 4vh}";' .. - 'document.body.appendChild(stylesElement);') - end + if getAspectRatio() ~= "16:9" then + executeBrowserJavascript(chatInstance, + "let stylesElement = document.createElement('style');" .. + 'stylesElement.innerHTML = ".chat__message {font-size: 4vh} .chat__input {font-size: 4vh}";' .. + 'document.body.appendChild(stylesElement);') + end end addEventHandler("onChat2Loaded", resourceRoot, onChatLoaded)