From 68855130db47fbae0a37cf4874dc81116058b183 Mon Sep 17 00:00:00 2001 From: Azumgi <4zumgi@gmail.com> Date: Mon, 15 Apr 2019 14:21:25 +0300 Subject: [PATCH] [Commands GUI] Clean up + little style changes --- .../vmf/modules/ui/chat/commands_list_gui.lua | 238 +++++++++--------- 1 file changed, 114 insertions(+), 124 deletions(-) diff --git a/vmf/scripts/mods/vmf/modules/ui/chat/commands_list_gui.lua b/vmf/scripts/mods/vmf/modules/ui/chat/commands_list_gui.lua index db2a7d8..accb8c7 100644 --- a/vmf/scripts/mods/vmf/modules/ui/chat/commands_list_gui.lua +++ b/vmf/scripts/mods/vmf/modules/ui/chat/commands_list_gui.lua @@ -1,40 +1,27 @@ -local vmf = get_mod("VMF") --@TODO: remove it? +local vmf = get_mod("VMF") -local _gui +local MULTISTRING_INDICATOR_TEXT = "[...]" -if VT1 then - -- @TODO: I don't think I need the 2nd texture - _gui = World.create_screen_gui(Managers.world:world("top_ingame_view"), "immediate", - "material", "materials/fonts/gw_fonts", - "material", "materials/ui/ui_1080p_ingame_common") -else - _gui = World.create_screen_gui(Managers.world:world("top_ingame_view"), - "material", "materials/fonts/gw_fonts", "immediate") -end +local FONT_TYPE = "hell_shark_arial" +local FONT_SIZE = 22 --- DebugScreen.gui = World.create_screen_gui(world, "material", "materials/fonts/gw_fonts", --- "material", "materials/menu/debug_screen", "immediate") +local MAX_COMMANDS_VISIBLE = 5 +local STRING_HEIGHT = 25 +local STRING_Y_OFFSET = 7 +local STRING_X_MARGIN = 10 -local _FONT_TYPE = "hell_shark_arial" -local _FONT_SIZE = 22 +local OFFSET_X = 0 +local OFFSET_Y = 200 +local OFFSET_Z = 880 +local WIDTH = 550 -local _MULTISTRING_INDICATOR_TEXT = "[...]" +local _gui = World.create_screen_gui(Managers.world:world("top_ingame_view"), "immediate", + "material", "materials/fonts/gw_fonts") -local _MAX_COMMANDS_VISIBLE = 5 - -local _STRING_HEIGHT = 25 -local _STRING_Y_OFFSET = 7 -local _STRING_X_MARGIN = 10 - -local _OFFSET_X = 0 -local _OFFSET_Y = 200 -local _OFFSET_Z = 880 -local _WIDTH = 550 - --- #################################################################################################################### --- ##### Local functions ############################################################################################## --- #################################################################################################################### +-- ##################################################################################################################### +-- ##### Local functions ############################################################################################### +-- ##################################################################################################################### local function get_text_width(text, font_material, font_size) local text_extent_min, text_extent_max = Gui.text_extents(_gui, text, font_material, font_size) @@ -42,6 +29,7 @@ local function get_text_width(text, font_material, font_size) return text_height end + local function word_wrap(text, font_material, font_size, max_width) local whitespace = " " local soft_dividers = "-+&/*" @@ -53,127 +41,129 @@ local function word_wrap(text, font_material, font_size, max_width) soft_dividers, return_dividers, reuse_global_table) end + local function draw(commands_list, selected_command_index) -- VT2 requires applying additional HUD scaling if not VT1 then UPDATE_RESOLUTION_LOOKUP(true, UISettings.hud_scale * 0.01) end - --vmf:pcall(function() - local selected_command_new_index = 0 + local selected_command_new_index = 0 - -- pick displayed commands - local last_displayed_command = math.max(math.min(_MAX_COMMANDS_VISIBLE, #commands_list), selected_command_index) - local first_displayed_command = math.max(1, last_displayed_command - (_MAX_COMMANDS_VISIBLE - 1)) - local displayed_commands = {} - for i = first_displayed_command, last_displayed_command do - local new_entry = {} - new_entry.name = "/" .. commands_list[i].name - new_entry.description = commands_list[i].description - new_entry.full_text = new_entry.name .. " " .. new_entry.description - if i == selected_command_index then - new_entry.selected = true - selected_command_new_index = #displayed_commands + 1 - end - table.insert(displayed_commands, new_entry) + -- pick displayed commands + local last_displayed_command = math.max(math.min(MAX_COMMANDS_VISIBLE, #commands_list), selected_command_index) + local first_displayed_command = math.max(1, last_displayed_command - (MAX_COMMANDS_VISIBLE - 1)) + local displayed_commands = {} + for i = first_displayed_command, last_displayed_command do + local new_entry = {} + new_entry.name = "/" .. commands_list[i].name + new_entry.description = commands_list[i].description + new_entry.full_text = new_entry.name .. " " .. new_entry.description + if i == selected_command_index then + new_entry.selected = true + selected_command_new_index = #displayed_commands + 1 end + table.insert(displayed_commands, new_entry) + end - local scale = RESOLUTION_LOOKUP.scale + local scale = RESOLUTION_LOOKUP.scale - local selected_strings_number = 1 + local selected_strings_number = 1 - local font, font_size = UIFontByResolution({font_type = _FONT_TYPE, font_size = _FONT_SIZE}) - local font_material = font[1] - local font_name = font[3] + local font, font_size = UIFontByResolution({font_type = FONT_TYPE, font_size = FONT_SIZE}) + local font_material = font[1] + local font_name = font[3] - for i, command in ipairs(displayed_commands) do + for i, command in ipairs(displayed_commands) do - -- draw "/command_name" text - local scaled_offet_x = (_OFFSET_X + _STRING_X_MARGIN) * scale - local scaled_offset_y = (_OFFSET_Y - _STRING_HEIGHT * (i + selected_strings_number - 1) + _STRING_Y_OFFSET) * - scale + -- draw "/command_name" text + local scaled_offet_x = (OFFSET_X + STRING_X_MARGIN) * scale + local scaled_offset_y = (OFFSET_Y - STRING_HEIGHT * (i + selected_strings_number - 1) + STRING_Y_OFFSET) * scale - local string_position = Vector3(scaled_offet_x, scaled_offset_y, _OFFSET_Z + 2) - Gui.text(_gui, command.name, font_material, font_size, font_name, string_position, Color(255, 100, 255, 100)) + local string_position = Vector3(scaled_offet_x, scaled_offset_y, OFFSET_Z + 2) + Gui.text(_gui, command.name, font_material, font_size, font_name, string_position, Color(255, 100, 255, 100)) - local command_text_strings = word_wrap(command.full_text, font_material, font_size, _WIDTH - _STRING_X_MARGIN * 2) - local multistring = #command_text_strings > 1 - local first_description_string - if multistring then - if command.selected then - selected_strings_number = #command_text_strings - else - local multistring_indicator_width = get_text_width(_MULTISTRING_INDICATOR_TEXT, font_material, font_size) - local command_text_width = _WIDTH - _STRING_X_MARGIN * 2 - (multistring_indicator_width / scale) - command_text_strings = word_wrap(command.full_text, font_material, font_size, command_text_width) - - -- draw that [...] thing - local multistring_offset_x = (_OFFSET_X + _WIDTH) * scale - multistring_indicator_width - local multistring_indicator_position = Vector3(multistring_offset_x, string_position.y, string_position.z) - Gui.text(_gui, _MULTISTRING_INDICATOR_TEXT, font_material, font_size, font_name, - multistring_indicator_position, Color(255, 100, 100, 100)) - end - first_description_string = string.sub(command_text_strings[1], #command.name + 2) + local command_text_strings = word_wrap(command.full_text, font_material, font_size, WIDTH - STRING_X_MARGIN * 2) + local multistring = #command_text_strings > 1 + local first_description_string + if multistring then + if command.selected then + selected_strings_number = #command_text_strings else - first_description_string = command.description - end - - -- draw command description text (1st string) - local first_description_string_width = get_text_width(command.name, font_material, font_size) - - local first_description_pos_x = string_position.x + first_description_string_width - local first_description_string_position = Vector3(first_description_pos_x, string_position.y, string_position.z) - Gui.text(_gui, first_description_string, font_material, font_size, font_name, - first_description_string_position, Color(255, 255, 255, 255)) - - -- draw command description text (2+ strings) - if command.selected and multistring then - for j = 2, selected_strings_number do - string_position.y = string_position.y - _STRING_HEIGHT * scale - Gui.text(_gui, command_text_strings[j], font_material, font_size, font_name, - string_position, Color(255, 255, 255, 255)) - end + local multistring_indicator_width = get_text_width(MULTISTRING_INDICATOR_TEXT, font_material, font_size) + local command_text_width = WIDTH - STRING_X_MARGIN * 2 - (multistring_indicator_width / scale) + command_text_strings = word_wrap(command.full_text, font_material, font_size, command_text_width) + + -- draw that [...] thing + local multistring_offset_x = (OFFSET_X + WIDTH) * scale - multistring_indicator_width + local multistring_indicator_position = Vector3(multistring_offset_x, string_position.y, string_position.z) + Gui.text(_gui, MULTISTRING_INDICATOR_TEXT, font_material, font_size, font_name, + multistring_indicator_position, Color(255, 100, 100, 100)) end + first_description_string = string.sub(command_text_strings[1], #command.name + 2) + else + first_description_string = command.description end - -- background rectangle - local bg_height = _STRING_HEIGHT * (#displayed_commands + selected_strings_number - 1) - local bg_pos_y = _OFFSET_Y - bg_height + -- draw command description text (1st string) + local first_description_string_width = get_text_width(command.name, font_material, font_size) - local bg_position = Vector3(_OFFSET_X * scale, bg_pos_y * scale, _OFFSET_Z) - local bg_size = Vector2(_WIDTH * scale, bg_height * scale) - local bg_color = Color(200, 10, 10, 10) - Gui.rect(_gui, bg_position, bg_size, bg_color) + local first_description_pos_x = string_position.x + first_description_string_width + local first_description_string_position = Vector3(first_description_pos_x, string_position.y, string_position.z) + Gui.text(_gui, first_description_string, font_material, font_size, font_name, + first_description_string_position, Color(255, 255, 255, 255)) - -- selection rectangle - if selected_command_new_index > 0 then - local selection_height = _STRING_HEIGHT * selected_strings_number - local selection_pos_y = _OFFSET_Y - selection_height - _STRING_HEIGHT * (selected_command_new_index - 1) - - local selection_position = Vector3(_OFFSET_X * scale, selection_pos_y * scale, _OFFSET_Z + 1) - local selection_size = Vector2(_WIDTH * scale, selection_height * scale) - local selection_color = Color(100, 120, 120, 120) - Gui.rect(_gui, selection_position, selection_size, selection_color) - end - - -- "selected command number / total commands number" indicator - if not ((#commands_list == 1) and (selected_command_index > 0)) then - local total_number_indicator = tostring(#commands_list) - if selected_command_index > 0 then - total_number_indicator = selected_command_index .. "/" .. total_number_indicator + -- draw command description text (2+ strings) + if command.selected and multistring then + for j = 2, selected_strings_number do + string_position.y = string_position.y - STRING_HEIGHT * scale + Gui.text(_gui, command_text_strings[j], font_material, font_size, font_name, + string_position, Color(255, 255, 255, 255)) end - local total_number_indicator_width = get_text_width(total_number_indicator, font_material, font_size) - local total_number_indicator_x = (_WIDTH) * scale - total_number_indicator_width - local total_number_indicator_y = (_OFFSET_Y + _STRING_Y_OFFSET) * scale - local total_number_indicator_position = Vector3(total_number_indicator_x, total_number_indicator_y, _OFFSET_Z + 2) - Gui.text(_gui, total_number_indicator, font_material, font_size, font_name, - total_number_indicator_position, Color(255, 100, 100, 100)) end - --end) + end + + -- background rectangle + local bg_height = STRING_HEIGHT * (#displayed_commands + selected_strings_number - 1) + local bg_pos_y = OFFSET_Y - bg_height + + local bg_position = Vector3(OFFSET_X * scale, bg_pos_y * scale, OFFSET_Z) + local bg_size = Vector2(WIDTH * scale, bg_height * scale) + local bg_color = Color(200, 10, 10, 10) + Gui.rect(_gui, bg_position, bg_size, bg_color) + + -- selection rectangle + if selected_command_new_index > 0 then + local selection_height = STRING_HEIGHT * selected_strings_number + local selection_pos_y = OFFSET_Y - selection_height - STRING_HEIGHT * (selected_command_new_index - 1) + + local selection_position = Vector3(OFFSET_X * scale, selection_pos_y * scale, OFFSET_Z + 1) + local selection_size = Vector2(WIDTH * scale, selection_height * scale) + local selection_color = Color(100, 120, 120, 120) + Gui.rect(_gui, selection_position, selection_size, selection_color) + end + + -- "selected command number / total commands number" indicator + if not ((#commands_list == 1) and (selected_command_index > 0)) then + local total_number_indicator = tostring(#commands_list) + if selected_command_index > 0 then + total_number_indicator = selected_command_index .. "/" .. total_number_indicator + end + local total_number_indicator_width = get_text_width(total_number_indicator, font_material, font_size) + local total_number_indicator_x = (WIDTH) * scale - total_number_indicator_width + local total_number_indicator_y = (OFFSET_Y + STRING_Y_OFFSET) * scale + local total_number_indicator_position = Vector3(total_number_indicator_x, total_number_indicator_y, OFFSET_Z + 2) + Gui.text(_gui, total_number_indicator, font_material, font_size, font_name, + total_number_indicator_position, Color(255, 100, 100, 100)) + end if not VT1 then UPDATE_RESOLUTION_LOOKUP(true) end end +-- ##################################################################################################################### +-- ##### Return ######################################################################################################## +-- ##################################################################################################################### + return draw