Changeset - r24325:ed063eed3f1c
[Not reviewed]
master
4 4 2
glx - 5 years ago 2019-03-05 14:52:41
glx@openttd.org
Change: rewrote generate_widget in CMake
10 files changed with 334 insertions and 3013 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -151,6 +151,7 @@ target_link_libraries(openttd
 
    openttd::languages
 
    openttd::settings
 
    openttd::basesets
 
    openttd::script_api
 
    Threads::Threads
 
)
 

	
cmake/scripts/GenerateWidget.cmake
Show inline comments
 
new file 100644
 
cmake_minimum_required(VERSION 3.5)
 

	
 
#
 
# CMake script to automatically generate the enums in script_window.hpp
 
#
 
# The file is scanned for @enum tokens, and the placeholder is filled with an enum from a different file.
 
#
 
# Example:
 
#   // @enum enumname filename@placeholder@
 
#   ... content here is replaced ...
 
#   // @endenum
 
#
 
# The parameter "enumname" specifies the enumeration to extract. This can also be a regular expression.
 
# The parameter "filename" specifies the relative path to the file, where the enumeration is extracted from. This can also be a glob expression.
 
#
 
#
 

	
 
if (NOT GENERATE_SOURCE_FILE)
 
    message(FATAL_ERROR "Script needs GENERATE_SOURCE_FILE defined")
 
endif (NOT GENERATE_SOURCE_FILE)
 
if (NOT GENERATE_BINARY_FILE)
 
    message(FATAL_ERROR "Script needs GENERATE_BINARY_FILE defined")
 
endif (NOT GENERATE_BINARY_FILE)
 

	
 
file(STRINGS ${GENERATE_SOURCE_FILE} ENUM_LINES REGEX "@enum")
 

	
 
foreach(ENUM IN LISTS ENUM_LINES)
 
    string(REGEX REPLACE "^(	)// @enum ([^ ]+) ([^ ]+)@([^ ]+)@" "\\4" PLACE_HOLDER "${ENUM}")
 
    set(ADD_INDENT "${CMAKE_MATCH_1}")
 
    set(ENUM_PATTERN "${CMAKE_MATCH_2}")
 

	
 
    file(GLOB FILENAMES "${CMAKE_MATCH_3}")
 
    list(SORT FILENAMES)
 

	
 
    foreach(FILE IN LISTS FILENAMES)
 
        unset(ACTIVE)
 
        unset(ACTIVE_COMMENT)
 
        unset(COMMENT)
 

	
 
        file(STRINGS ${FILE} SOURCE_LINES)
 

	
 
        string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" FILE ${FILE})
 
        string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}/* automatically generated from ${FILE} */")
 

	
 
        foreach(LINE IN LISTS SOURCE_LINES)
 
            string(REPLACE "${RM_INDENT}" "" LINE "${LINE}")
 

	
 
            # Remember possible doxygen comment before enum declaration
 
            if ((NOT ACTIVE) AND "${LINE}" MATCHES "/\\*\\*")
 
                set(COMMENT "${ADD_INDENT}${LINE}")
 
                set(ACTIVE_COMMENT 1)
 
            elseif (ACTIVE_COMMENT EQUAL 1)
 
                string(APPEND COMMENT "\n${ADD_INDENT}${LINE}")
 
            endif()
 

	
 
            # Check for enum match
 
            if ("${LINE}" MATCHES "^	*enum *${ENUM_PATTERN} *\{")
 
                # REGEX REPLACE does a REGEX MATCHALL and fails if an empty string is matched
 
                string(REGEX MATCH "[^	]*" RESULT "${LINE}")
 
                string(REPLACE "${RESULT}" "" RM_INDENT "${LINE}")
 

	
 
                set(ACTIVE 1)
 
                if (ACTIVE_COMMENT GREATER 0)
 
                     string(APPEND ${PLACE_HOLDER} "\n${COMMENT}")
 
                endif (ACTIVE_COMMENT GREATER 0)
 
                unset(ACTIVE_COMMENT)
 
                unset(COMMENT)
 
            endif ("${LINE}" MATCHES "^	*enum *${ENUM_PATTERN} *\{")
 

	
 
            # Forget doxygen comment, if no enum follows
 
            if (ACTIVE_COMMENT EQUAL 2 AND NOT "${LINE}" STREQUAL "")
 
                unset(ACTIVE_COMMENT)
 
                unset(COMMENT)
 
            endif (ACTIVE_COMMENT EQUAL 2 AND NOT "${LINE}" STREQUAL "")
 
            if (ACTIVE_COMMENT EQUAL 1 AND "${LINE}" MATCHES "\\*/")
 
                set(ACTIVE_COMMENT 2)
 
            endif (ACTIVE_COMMENT EQUAL 1 AND "${LINE}" MATCHES "\\*/")
 

	
 
            if (ACTIVE)
 
                if ("${LINE}" MATCHES "^	*[A-Za-z0-9_]* *[,=]")
 
                    # Transform enum values
 
                    # REGEX REPLACE does a REGEX MATCHALL and replaces too much
 
                    string(REGEX MATCH " *=[^,]*" RESULT "${LINE}")
 
                    string(REPLACE "${RESULT}" "" LINE "${LINE}")
 

	
 
                    string(REGEX REPLACE " *//" " //" LINE "${LINE}")
 

	
 
                    string(REGEX MATCH "^(	*)([A-Za-z0-9_]+),(.*)" RESULT "${LINE}")
 

	
 
                    string(LENGTH "${CMAKE_MATCH_2}" LEN)
 
                    math(EXPR LEN "43 - ${LEN}")
 
                    unset(SPACES)
 
                    foreach(i RANGE ${LEN})
 
                        string(APPEND SPACES " ")
 
                    endforeach(i)
 

	
 
                    if (CMAKE_MATCH_3)
 
                        string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${CMAKE_MATCH_1}${CMAKE_MATCH_2}${SPACES} = ::${CMAKE_MATCH_2},${SPACES}${CMAKE_MATCH_3}")
 
                    else (CMAKE_MATCH_3)
 
                        string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${CMAKE_MATCH_1}${CMAKE_MATCH_2}${SPACES} = ::${CMAKE_MATCH_2},")
 
                    endif (CMAKE_MATCH_3)
 
                elseif ("${LINE}" STREQUAL "")
 
                    string(APPEND ${PLACE_HOLDER} "\n")
 
                else ()
 
                    string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${LINE}")
 
                endif ()
 
            endif (ACTIVE)
 

	
 
            if ("${LINE}" MATCHES "^	*\};")
 
                if (ACTIVE)
 
                    string(APPEND ${PLACE_HOLDER} "\n")
 
                endif (ACTIVE)
 
                unset(ACTIVE)
 
            endif ("${LINE}" MATCHES "^	*\};")
 
         endforeach(LINE)
 
    endforeach(FILE)
 
 endforeach(ENUM)
 

	
 
configure_file(${GENERATE_SOURCE_FILE} ${GENERATE_BINARY_FILE})
src/script/api/CMakeLists.txt
Show inline comments
 
# Get script_window.hpp dependencies
 
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/script_window.hpp.in ENUM_LINES REGEX "@enum")
 
foreach(ENUM IN LISTS ENUM_LINES)
 
    string(REGEX REPLACE ".* ([^ @]+)@.*" "\\1" FILE_PATTERN "${ENUM}")
 
    file(GLOB FILENAMES "${FILE_PATTERN}")
 
    list(APPEND DEPENDENCIES ${FILENAMES})
 
endforeach(ENUM)
 
list(REMOVE_DUPLICATES DEPENDENCIES)
 

	
 
# Add a command to generate script_window.hpp
 
add_custom_command_timestamp(OUTPUT ${CMAKE_BINARY_DIR}/generated/script/api/script_window.hpp
 
        COMMAND ${CMAKE_COMMAND}
 
                -DGENERATE_SOURCE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/script_window.hpp.in
 
                -DGENERATE_BINARY_FILE=${CMAKE_BINARY_DIR}/generated/script/api/script_window.hpp
 
                -P ${CMAKE_SOURCE_DIR}/cmake/scripts/GenerateWidget.cmake
 
        COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/generated/script/api/dummy # dummy directory for #include "../script_window.hpp"
 
        MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/script_window.hpp.in
 
        DEPENDS ${CMAKE_SOURCE_DIR}/cmake/scripts/GenerateWidget.cmake ${DEPENDENCIES}
 
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
 
        COMMENT "Generating script_window.hpp"
 
)
 
add_custom_target_timestamp(script_window
 
        DEPENDS
 
        ${CMAKE_BINARY_DIR}/generated/script/api/script_window.hpp
 
)
 

	
 
add_library(script_api
 
        INTERFACE
 
)
 
target_include_directories(script_api
 
        INTERFACE
 
        ${CMAKE_BINARY_DIR}/generated/script/api/
 
        ${CMAKE_BINARY_DIR}/generated/script/api/dummy # dummy path so #include "../script_window.hpp" works
 
        ${CMAKE_CURRENT_SOURCE_DIR}
 
)
 
add_dependencies(script_api
 
        script_window
 
)
 
add_library(openttd::script_api ALIAS script_api)
 

	
 

	
 
add_files(
 
    ai_changelog.hpp
 
    game_changelog.hpp
 
@@ -69,7 +110,6 @@ add_files(
 
    script_viewport.hpp
 
    script_waypoint.hpp
 
    script_waypointlist.hpp
 
    script_window.hpp
 
    script_accounting.cpp
 
    script_admin.cpp
 
    script_airport.cpp
src/script/api/Doxyfile_AI
Show inline comments
 
@@ -91,7 +91,7 @@ WARN_LOGFILE           =
 
#---------------------------------------------------------------------------
 
# configuration options related to the input files
 
#---------------------------------------------------------------------------
 
INPUT                  = .
 
INPUT                  = . $(GENERATED_API_DIR)
 
INPUT_ENCODING         = UTF-8
 
FILE_PATTERNS          = script_*.hpp \
 
                         ai_*.hpp
src/script/api/Doxyfile_Game
Show inline comments
 
@@ -91,7 +91,7 @@ WARN_LOGFILE           =
 
#---------------------------------------------------------------------------
 
# configuration options related to the input files
 
#---------------------------------------------------------------------------
 
INPUT                  = .
 
INPUT                  = . $(GENERATED_API_DIR)
 
INPUT_ENCODING         = UTF-8
 
FILE_PATTERNS          = script_*.hpp \
 
                         game_*.hpp
src/script/api/generate_widget.awk
Show inline comments
 
deleted file
src/script/api/generate_widget.sh
Show inline comments
 
deleted file
src/script/api/generate_widget.vbs
Show inline comments
 
deleted file
src/script/api/script_window.hpp
Show inline comments
 
deleted file
src/script/api/script_window.hpp.in
Show inline comments
 
new file 100644
 
/*
 
 * This file is part of OpenTTD.
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file script_window.hpp Everything to handle window interaction. */
 

	
 
#ifndef SCRIPT_WINDOW_HPP
 
#define SCRIPT_WINDOW_HPP
 

	
 
#include "script_object.hpp"
 
#include "../../window_type.h"
 
#include "../../gfx_type.h"
 

	
 
#include "../../widgets/ai_widget.h"
 
#include "../../widgets/airport_widget.h"
 
#include "../../widgets/autoreplace_widget.h"
 
#include "../../widgets/bootstrap_widget.h"
 
#include "../../widgets/bridge_widget.h"
 
#include "../../widgets/build_vehicle_widget.h"
 
#include "../../widgets/cheat_widget.h"
 
#include "../../widgets/company_widget.h"
 
#include "../../widgets/console_widget.h"
 
#include "../../widgets/date_widget.h"
 
#include "../../widgets/depot_widget.h"
 
#include "../../widgets/dock_widget.h"
 
#include "../../widgets/dropdown_widget.h"
 
#include "../../widgets/engine_widget.h"
 
#include "../../widgets/error_widget.h"
 
#include "../../widgets/fios_widget.h"
 
#include "../../widgets/framerate_widget.h"
 
#include "../../widgets/genworld_widget.h"
 
#include "../../widgets/goal_widget.h"
 
#include "../../widgets/graph_widget.h"
 
#include "../../widgets/group_widget.h"
 
#include "../../widgets/highscore_widget.h"
 
#include "../../widgets/industry_widget.h"
 
#include "../../widgets/intro_widget.h"
 
#include "../../widgets/main_widget.h"
 
#include "../../widgets/misc_widget.h"
 
#include "../../widgets/music_widget.h"
 
#include "../../widgets/network_chat_widget.h"
 
#include "../../widgets/network_content_widget.h"
 
#include "../../widgets/network_widget.h"
 
#include "../../widgets/newgrf_debug_widget.h"
 
#include "../../widgets/newgrf_widget.h"
 
#include "../../widgets/news_widget.h"
 
#include "../../widgets/object_widget.h"
 
#include "../../widgets/order_widget.h"
 
#include "../../widgets/osk_widget.h"
 
#include "../../widgets/rail_widget.h"
 
#include "../../widgets/road_widget.h"
 
#include "../../widgets/screenshot_widget.h"
 
#include "../../widgets/settings_widget.h"
 
#include "../../widgets/sign_widget.h"
 
#include "../../widgets/smallmap_widget.h"
 
#include "../../widgets/station_widget.h"
 
#include "../../widgets/statusbar_widget.h"
 
#include "../../widgets/subsidy_widget.h"
 
#include "../../widgets/terraform_widget.h"
 
#include "../../widgets/timetable_widget.h"
 
#include "../../widgets/toolbar_widget.h"
 
#include "../../widgets/town_widget.h"
 
#include "../../widgets/transparency_widget.h"
 
#include "../../widgets/tree_widget.h"
 
#include "../../widgets/vehicle_widget.h"
 
#include "../../widgets/viewport_widget.h"
 
#include "../../widgets/waypoint_widget.h"
 
#include "../../widgets/link_graph_legend_widget.h"
 
#include "../../widgets/story_widget.h"
 

	
 
/**
 
 * Class that handles window interaction. A Window in OpenTTD has two imporant
 
 *  values. The WindowClass, and a Window number. The first indicates roughly
 
 *  which window it is. WC_TOWN_VIEW for example, is the view of a town.
 
 * The Window number is a bit more complex, as it depends mostly on the
 
 *  WindowClass. For example for WC_TOWN_VIEW it is the TownID. In general a
 
 *  good rule of thumb is: either the number is always 0, or the ID of the
 
 *  object in question.
 
 * In the comment at the widget enum, it is mentioned how the number is used.
 
 *
 
 * Note, that the detailed window layout is very version specific.
 
 * Enum values might be added, changed or removed in future versions without notice
 
 * in the changelog, and there won't be any means of compatibility.
 
 *
 
 * @api game
 
 */
 
class ScriptWindow : public ScriptObject {
 
public:
 
	// @enum WindowNumberEnum ../../window_type.h@ENUM_WINDOWNUMBERENUM@
 
	// @endenum
 

	
 
	// @enum WindowClass ../../window_type.h@ENUM_WINDOWCLASS@
 
	// @endenum
 

	
 
	/**
 
	 * The colours in the game which you can use for text and highlights.
 
	 */
 
	enum TextColour {
 
		/* Note: these values represent part of the in-game TextColour enum */
 
		TC_BLUE        = ::TC_BLUE,        ///< Blue colour.
 
		TC_SILVER      = ::TC_SILVER,      ///< Silver colour.
 
		TC_GOLD        = ::TC_GOLD,        ///< Gold colour.
 
		TC_RED         = ::TC_RED,         ///< Red colour.
 
		TC_PURPLE      = ::TC_PURPLE,      ///< Purple colour.
 
		TC_LIGHT_BROWN = ::TC_LIGHT_BROWN, ///< Light brown colour.
 
		TC_ORANGE      = ::TC_ORANGE,      ///< Orange colour.
 
		TC_GREEN       = ::TC_GREEN,       ///< Green colour.
 
		TC_YELLOW      = ::TC_YELLOW,      ///< Yellow colour.
 
		TC_DARK_GREEN  = ::TC_DARK_GREEN,  ///< Dark green colour.
 
		TC_CREAM       = ::TC_CREAM,       ///< Cream colour.
 
		TC_BROWN       = ::TC_BROWN,       ///< Brown colour.
 
		TC_WHITE       = ::TC_WHITE,       ///< White colour.
 
		TC_LIGHT_BLUE  = ::TC_LIGHT_BLUE,  ///< Light blue colour.
 
		TC_GREY        = ::TC_GREY,        ///< Grey colour.
 
		TC_DARK_BLUE   = ::TC_DARK_BLUE,   ///< Dark blue colour.
 
		TC_BLACK       = ::TC_BLACK,       ///< Black colour.
 
		TC_INVALID     = ::TC_INVALID,     ///< Invalid colour.
 
	};
 

	
 
	/**
 
	 * Special number values.
 
	 */
 
	enum NumberType {
 
		NUMBER_ALL = 0xFFFFFFFF, ///< Value to select all windows of a class.
 
	};
 

	
 
	/**
 
	 * Special widget values.
 
	 */
 
	enum WidgetType {
 
		WIDGET_ALL = 0xFF, ///< Value to select all widgets of a window.
 
	};
 

	
 
	/**
 
	 * Close a window.
 
	 * @param window The class of the window to close.
 
	 * @param number The number of the window to close, or NUMBER_ALL to close all of this class.
 
	 * @pre !ScriptGame::IsMultiplayer().
 
	 */
 
	static void Close(WindowClass window, uint32 number);
 

	
 
	/**
 
	 * Check if a window is open.
 
	 * @param window The class of the window to check for.
 
	 * @param number The number of the window to check for, or NUMBER_ALL to check for any in the class.
 
	 * @pre !ScriptGame::IsMultiplayer().
 
	 * @return True if the window is open.
 
	 */
 
	static bool IsOpen(WindowClass window, uint32 number);
 

	
 
	/**
 
	 * Highlight a widget in a window.
 
	 * @param window The class of the window to highlight a widget in.
 
	 * @param number The number of the window to highlight a widget in.
 
	 * @param widget The widget in the window to highlight, or WIDGET_ALL (in combination with TC_INVALID) to disable all widget highlighting on this window.
 
	 * @param colour The colour of the highlight, or TC_INVALID for disabling.
 
	 * @pre !ScriptGame::IsMultiplayer().
 
	 * @pre number != NUMBER_ALL.
 
	 * @pre colour < TC_END || (widget == WIDGET_ALL && colour == TC_INVALID).
 
	 * @pre IsOpen(window, number).
 
	 */
 
	static void Highlight(WindowClass window, uint32 number, uint8 widget, TextColour colour);
 

	
 
	// @enum .*Widgets ../../widgets/*_widget.h@ENUM_WIDGETS@
 
	// @endenum
 
};
 

	
 
#endif /* SCRIPT_WINDOW_HPP */
0 comments (0 inline, 0 general)