feat(admin_menu): add announcement presets and message length validation

- Add preset system for quick announcement creation with configurable presets
- Implement message length validation with visual feedback (red border, warning icon)
- Add character counter display and configurable max length limit
- Improve UI layout and spacing for better visual hierarchy
- Add status panel showing character count and overflow warnings
- Enhance send button with disabled state when message exceeds limit
- Fix ESC key handling to properly close menu without showing pause menu
- Add helper functions for UTF-8 character counting and warning triangle drawing
- Initialize BladW namespace for better code organization
- Send selected preset identifier to server with announcement data
This commit is contained in:
nocode committed 2026-07-20 13:35:56 +02:00
1 parent 9501bfa6f8
commit fa6435442b
1 file changed
+106 -25
@@ -1,14 +1,37 @@
local rndx = include("lib/rndx.lua")
local logo = Material("nocode/announcement/logo.png")
BladW = BladW or {}
BladW.Announcement = BladW.Announcement or {}
-- Palette identique au thème des annonces
local COL_BG = Color(0, 0, 0, 200)
local COL_PANEL = Color(0, 0, 0, 160)
local COL_ACCENT = Color(76, 119, 200)
local COL_BG = Color(0, 0, 0, 200)
local COL_PANEL = Color(0, 0, 0, 160)
local COL_ACCENT = Color(76, 119, 200)
local COL_ACCENT_DIM = Color(60, 90, 150)
local COL_TEXT = Color(255, 255, 255)
local COL_RED = Color(255, 80, 80)
local COL_GRAY = Color(54, 54, 54, 255)
local COL_TEXT = Color(255, 255, 255)
local COL_RED = Color(255, 80, 80)
local COL_GRAY = Color(54, 54, 54, 255)
local COL_MUTED = Color(160, 160, 160)
local COL_WARN = Color(255, 200, 50)
-- Longueur en caractères (UTF-8, pour compter les accents comme 1) d'un DTextEntry
local function EntryLen(entry)
local v = entry:GetValue()
return utf8.len(v) or #v
end
-- Petit triangle d'avertissement (emoji ⚠ dessiné -> fiable quelle que soit la police)
local function DrawWarning(x, y, size)
draw.NoTexture()
surface.SetDrawColor(COL_WARN)
surface.DrawPoly({
{ x = x + size * 0.5, y = y },
{ x = x + size, y = y + size },
{ x = x, y = y + size },
})
draw.SimpleText("!", "DermaDefaultBold", x + size * 0.5, y + size * 0.62, Color(30, 30, 30), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
-- Applique le style thème à un DTextEntry
local function StyleEntry(entry, placeholder, default)
@@ -30,9 +53,12 @@ local function OpenAAdminMenu()
-- Évite d'empiler plusieurs fenêtres si on rouvre le menu
if IsValid(BladW_AdminMenu) then BladW_AdminMenu:Remove() end
local selectedPreset = ""
local maxLen = (BladW.Announcement.Conf or {}).announcement_max_length or 120
local frame = vgui.Create("DFrame")
BladW_AdminMenu = frame
frame:SetSize(500, 380)
frame:SetSize(500, 460)
frame:Center()
frame:SetTitle("")
frame:ShowCloseButton(false)
@@ -45,15 +71,16 @@ local function OpenAAdminMenu()
rndx.DrawOutlined(20, 0, 0, w, h, COL_TEXT, 3, rndx.SHAPE_FIGMA)
-- Logo + titre
rndx.DrawMaterial(0, w * 0.5 - 32, 22, 64, 64, COL_TEXT, logo)
draw.SimpleText("Envoyer une annonce", "bladw_text", w * 0.5, 100, COL_TEXT, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
rndx.DrawMaterial(0, w * 0.5 - 30, 20, 60, 60, COL_TEXT, logo)
draw.SimpleText("Envoyer une annonce", "bladw_text", w * 0.5, 94, COL_TEXT, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
draw.SimpleText("Presets", "bladw_text_Medium", 40, 118, COL_TEXT, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
end
-- ⚠️ ESC est captée par le moteur (bind du menu pause) AVANT la lib input,
-- donc input.IsKeyDown(KEY_ESCAPE) est toujours false → indétectable ici.
-- On détecte plutôt l'ouverture du menu pause, que ESC provoque.
-- ESC ferme le menu admin sans laisser apparaître le menu pause.
-- Le moteur ouvre l'UI jeu sur ESC avant qu'on puisse la capter : on la re-masque aussitôt et on ferme.
frame.Think = function(self)
if gui.IsGameUIVisible() then
gui.HideGameUI()
self:Remove()
end
end
@@ -69,37 +96,91 @@ local function OpenAAdminMenu()
end
close.DoClick = function() frame:Remove() end
-- Champ : titre de l'annonce
-- Champs (créés avant les boutons preset pour être capturés par leurs closures)
local titleEntry = vgui.Create("DTextEntry", frame)
titleEntry:SetPos(40, 140)
titleEntry:SetSize(420, 42)
titleEntry:SetPos(40, 240)
titleEntry:SetSize(420, 40)
StyleEntry(titleEntry, "Titre de l'annonce", "Annonce")
-- Champ : message de l'annonce
local msgEntry = vgui.Create("DTextEntry", frame)
msgEntry:SetPos(40, 196)
msgEntry:SetSize(420, 42)
msgEntry:SetPos(40, 288)
msgEntry:SetSize(420, 40)
StyleEntry(msgEntry, "Message de l'annonce")
-- Bord rouge quand le message dépasse la limite de caractères
msgEntry.Paint = function(self, w, h)
local over = EntryLen(self) > maxLen
rndx.Draw(10, 0, 0, w, h, COL_PANEL, rndx.SHAPE_FIGMA)
local outline = over and COL_RED or (self:IsEditing() and COL_ACCENT or Color(255, 255, 255, 60))
rndx.DrawOutlined(10, 0, 0, w, h, outline, over and 3 or 2, rndx.SHAPE_FIGMA)
self:DrawTextEntryText(COL_TEXT, COL_ACCENT, COL_TEXT)
end
-- Bouton d'envoi
-- Ligne d'état : avertissement à gauche, compteur à droite
local status = vgui.Create("DPanel", frame)
status:SetPos(40, 332)
status:SetSize(420, 26)
status:SetPaintBackground(false)
status.Paint = function(self, w, h)
local len = EntryLen(msgEntry)
local over = len > maxLen
if over then
DrawWarning(0, h * 0.5 - 8, 16)
draw.SimpleText("Trop de caractères", "bladw_text_Medium", 24, h * 0.5, COL_RED, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER)
end
draw.SimpleText(len .. " / " .. maxLen, "bladw_text_Medium", w, h * 0.5,
over and COL_RED or COL_MUTED, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER)
end
-- Boutons de presets : remplissent les champs et mémorisent le preset choisi
local presets = vgui.Create("DIconLayout", frame)
presets:SetPos(40, 134)
presets:SetSize(420, 90)
presets:SetSpaceX(8)
presets:SetSpaceY(8)
for key, preset in SortedPairs(BladW.Announcement.Presets or {}) do
local b = presets:Add("DButton")
b:SetSize(134, 34)
b:SetText("")
b.Paint = function(self, w, h)
local on = (selectedPreset == key) or self:IsHovered()
rndx.Draw(8, 0, 0, w, h, on and COL_ACCENT or COL_GRAY, rndx.SHAPE_FIGMA)
draw.SimpleText(preset.title or key, "bladw_text_Medium", w * 0.5, h * 0.5, COL_TEXT, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
b.DoClick = function()
selectedPreset = key
titleEntry:SetText(preset.title or "")
msgEntry:SetText(preset.text or "")
end
end
-- Bouton d'envoi (grisé + bloqué tant que le message dépasse la limite)
local send = vgui.Create("DButton", frame)
send:SetPos(40, 300)
send:SetPos(40, 372)
send:SetSize(420, 50)
send:SetText("")
send.Paint = function(self, w, h)
rndx.Draw(12, 0, 0, w, h, self:IsHovered() and COL_ACCENT or COL_ACCENT_DIM, rndx.SHAPE_FIGMA)
draw.SimpleText("Envoyer", "bladw_text", w * 0.5, h * 0.5, COL_TEXT, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
local over = EntryLen(msgEntry) > maxLen
local col = over and COL_GRAY or (self:IsHovered() and COL_ACCENT or COL_ACCENT_DIM)
rndx.Draw(12, 0, 0, w, h, col, rndx.SHAPE_FIGMA)
draw.SimpleText("Envoyer", "bladw_text", w * 0.5, h * 0.5, over and COL_MUTED or COL_TEXT, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end
send.DoClick = function()
local text = msgEntry:GetValue()
if text == "" then
if EntryLen(msgEntry) > maxLen then
surface.PlaySound("buttons/button10.wav")
return
end
if msgEntry:GetValue() == "" and selectedPreset == "" then
surface.PlaySound("buttons/button10.wav")
return
end
net.Start("bladWSendAnnouncement")
net.WriteString(selectedPreset)
net.WriteString(titleEntry:GetValue())
net.WriteString(text)
net.WriteString(msgEntry:GetValue())
net.SendToServer()
frame:Remove()