Change: rewrote generate_widget in CMake

This commit is contained in:
glx
2019-03-05 15:52:41 +01:00
committed by glx22
parent 26b91192a3
commit 8794c61f25
10 changed files with 334 additions and 3013 deletions

View File

@@ -1,3 +1,44 @@
# 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

View File

@@ -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

View File

@@ -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

View File

@@ -1,114 +0,0 @@
# 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/>.
#
# Awk script to automatically generate the enums in script_window.hpp
#
# The file is scanned for @enum and @endenum tokens, and the content between them is replaced by an enum from a different file.
#
# Example:
# // @enum enumname filename
# ... 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.
#
#
BEGIN {
skiptillend = 0;
}
{ CR = (match($0, "\\r$") > 0 ? "\r" : "") }
/@enum/ {
print;
add_indent = gensub("[^ ]*", "", "g");
sub(".*@enum *", "");
enum = $1;
pattern = $2;
files = "echo " pattern;
files | getline filelist;
close(files);
split(filelist, filearray);
count = asort(filearray);
for (i = 1; i <= count; i++) {
active = 0;
active_comment = 0;
comment = "";
file = filearray[i];
print add_indent "/* automatically generated from " file " */" CR
while ((getline < file) > 0) {
sub(rm_indent, "");
# Remember possible doxygen comment before enum declaration
if ((active == 0) && (match($0, "/\\*\\*") > 0)) {
comment = add_indent $0;
active_comment = 1;
} else if (active_comment == 1) {
comment = comment "\n" add_indent $0;
}
# Check for enum match
if (match($0, "^ *enum *" enum " *\\{") > 0) {
rm_indent = $0;
gsub("[^ ]*", "", rm_indent);
active = 1;
if (active_comment > 0) print comment;
active_comment = 0;
comment = "";
}
# Forget doxygen comment, if no enum follows
if (active_comment == 2 && $0 != "" CR) {
active_comment = 0;
comment = "";
}
if (active_comment == 1 && match($0, "\\*/") > 0) active_comment = 2;
if (active != 0) {
if (match($0, "^ *[A-Za-z0-9_]* *[,=]") > 0) {
# Transform enum values
sub(" *=[^,]*", "");
sub(" *//", " //");
match($0, "^( *)([A-Za-z0-9_]+),(.*)", parts);
if (parts[3] == "" CR) {
printf "%s%s%-45s= ::%s\n", add_indent, parts[1], parts[2], (parts[2] "," CR)
} else {
printf "%s%s%-45s= ::%-45s%s\n", add_indent, parts[1], parts[2], (parts[2] ","), (parts[3]);
}
} else if ($0 == "" CR) {
print "" CR;
} else {
print add_indent $0;
}
}
if (match($0, "^ *\\};") > 0) {
if (active != 0) print "" CR;
active = 0;
}
}
close(file);
}
skiptillend = 1;
next;
}
/@endenum/ {
print;
skiptillend = 0;
next;
}
{
if (skiptillend == 0) print;
}

View File

@@ -1,22 +0,0 @@
#!/bin/bash
# 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/>.
# Set neutral locale so sort behaves the same everywhere
LC_ALL=C
export LC_ALL
# We really need gawk for this!
AWK=gawk
${AWK} --version > /dev/null 2> /dev/null
if [ "$?" != "0" ]; then
echo "This script needs gawk to run properly"
exit 1
fi
${AWK} -v BINMODE=1 -f generate_widget.awk script_window.hpp > script_window.tmp
mv script_window.tmp script_window.hpp

View File

@@ -1,190 +0,0 @@
Option Explicit
' 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/>.
Dim FSO, filename, skiptillend, eof
Set FSO = CreateObject("Scripting.FileSystemObject")
filename = "script_window.hpp"
skiptillend = False
eof = vbCrLf
If Not FSO.FileExists(filename) Then
WScript.Echo filename & " not found"
WScript.Quit 1
End If
Function GetFiles(pattern)
Dim parent, re, files, f
Set files = CreateObject("Scripting.Dictionary")
Set re = New RegExp
parent = FSO.GetParentFolderName(pattern)
pattern = FSO.GetFileName(pattern)
' Convert pattern to a valid regex
re.Global = True
re.Pattern = "\."
pattern = re.Replace(pattern, "\.")
re.Pattern = "\*"
pattern = re.Replace(pattern, ".*")
re.Pattern = pattern
' Get the file list
For Each f In FSO.GetFolder(parent).Files
If re.Test(f.Path) Then
f = parent & "/" & f.Name
files.Add f, f
End If
Next
' Sort the file list
Set GetFiles = CreateObject("Scripting.Dictionary")
While files.Count <> 0
Dim first
first = ""
For Each f in files
If first = "" Or StrComp(first, f) = 1 Then first = f
Next
GetFiles.Add first, first
files.Remove(First)
Wend
End Function
Sub Generate(line, file)
Dim re, add_indent, enum_pattern, file_pattern, f, active, active_comment, comment, rm_indent
Set re = New RegExp
re.Global = True
re.Pattern = "[^ ]*"
add_indent = re.Replace(line, "")
re.Global = False
re.Pattern = ".*@enum *"
line = Split(re.Replace(line, ""))
enum_pattern = line(0)
file_pattern = line(1)
For Each f In GetFiles(file_pattern).Items
active = 0
active_comment = 0
comment = ""
file.Write add_indent & "/* automatically generated from " & f & " */" & eof
Set f = FSO.OpenTextFile(f, 1)
While Not f.AtEndOfStream
re.Pattern = rm_indent
line = re.Replace(f.ReadLine, "")
' Remember possible doxygen comment before enum declaration
re.Pattern = "/\*\*"
If active = 0 And re.Test(line) Then
comment = add_indent & line
active_comment = 1
ElseIf active_comment = 1 Then
comment = comment & vbCrLf & add_indent & line
End If
' Check for enum match
re.Pattern = "^ *enum *" & enum_pattern & " *\{"
If re.Test(line) Then
re.Global = True
re.Pattern = "[^ ]*"
rm_indent = re.Replace(line, "")
re.Global = False
active = 1
If active_comment > 0 Then file.Write comment & eof
active_comment = 0
comment = ""
End If
' Forget doxygen comment, if no enum follows
If active_comment = 2 And line <> "" Then
active_comment = 0
comment = ""
End If
re.Pattern = "\*/"
If active_comment = 1 And re.Test(line) Then active_comment = 2
If active <> 0 Then
re.Pattern = "^ *[A-Za-z0-9_]* *[,=]"
If re.Test(line) Then
Dim parts
' Transform enum values
re.Pattern = " *=[^,]*"
line = re.Replace(line, "")
re.Pattern = " *//"
line = re.Replace(line, " //")
re.Pattern = "^( *)([A-Za-z0-9_]+),(.*)"
Set parts = re.Execute(line)
With parts.Item(0).SubMatches
If .Item(2) = "" Then
file.Write add_indent & .Item(0) & .Item(1) & String(45 - Len(.Item(1)), " ") & "= ::" & .Item(1) & "," & eof
Else
file.Write add_indent & .Item(0) & .Item(1) & String(45 - Len(.Item(1)), " ") & "= ::" & .Item(1) & "," & String(44 - Len(.Item(1)), " ") & .Item(2) & eof
End If
End With
ElseIf line = "" Then
file.Write eof
Else
file.Write add_indent & line & eof
End If
End If
re.Pattern = "^ *\};"
If re.Test(line) Then
If active <> 0 Then file.Write eof
active = 0
End If
Wend
f.Close
Next
End Sub
Sub Parse(line, file)
Dim re
Set re = New RegExp
re.pattern = "@enum"
If re.Test(line) Then
file.Write line & eof
Generate line, file
skiptillend = True
Exit Sub
End If
re.pattern = "@endenum"
If re.Test(line) Then
file.Write line & eof
skiptillend = False
Exit Sub
End If
If Not skiptillend Then
file.Write line & eof
End If
End Sub
Dim file, source, lines, i
WScript.Echo "Starting to parse " & filename
Set file = FSO.OpenTextFile(filename, 1)
If Not file.AtEndOfStream Then
source = file.ReadAll
End IF
file.Close
lines = Split(source, eof)
If UBound(lines) = 0 Then
eof = vbLf
lines = Split(source, eof)
End If
Set file = FSO.OpenTextFile(filename, 2)
For i = LBound(lines) To UBound(lines) - 1 ' Split adds an extra line, we must ignore it
Parse lines(i), file
Next
file.Close
WScript.Echo "Done"

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,171 @@
/*
* 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 */