-
Notifications
You must be signed in to change notification settings - Fork 572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for 9-slice/9-patch image for various widgets #69
Changes from all commits
fe2f2fe
4948eb2
9e08590
daed6fc
f7dee86
561b1c0
4472388
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Github language settings | ||
*.h linguist-language=c | ||
*.c linguist-language=c | ||
*.c linguist-language=c |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,7 @@ def omit_includes(str, files): | |
return str | ||
|
||
def fix_comments(str): | ||
return re.sub(r"//(.*)(\n|$)", "/* \\1 */\\2", str) | ||
return re.sub(r"//(.*)(" + os.linesep + r"|$)", "/* \\1 */\\2", str) | ||
|
||
# Main start | ||
# ========== | ||
|
@@ -83,6 +83,12 @@ def fix_comments(str): | |
print_help() | ||
exit() | ||
|
||
# Python 2.x Windows fix for newline madness | ||
# ========== | ||
if sys.platform == "win32": | ||
import os, msvcrt | ||
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | ||
|
||
intro_files = [] | ||
pub_files = [] | ||
priv_files1 = [] | ||
|
@@ -129,39 +135,42 @@ def fix_comments(str): | |
|
||
# Print concatenated output | ||
# ------------------------- | ||
print("/*") | ||
sys.stdout.write("/*" + os.linesep) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to admit I still can't grasp using I understand that we have to fight some corner cases of Python behavior (I also accept patches regarding Python 2.x behavior even if it's already after final EOL), but basically using Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Hejsil ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I have no idea on this one. Windows and Python oddities are not something i deal with often. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is with print on Windows with Python 2.x (I have 2.7). For some reason, it always uses LF instead of CRLF. It is silly that we have to work around this kind of thing, which is (apparently) resolved in Python 3.x, but too many programs still rely on 2.x. Would you be willing to make the script only work with Python 3.x? Most developers should have 3.x on their systems nowadays anyway, and it's simple to install if they don't have it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hm, but that's exactly the behavior we require, don't we?
Yeah, that's also an option I'm totally OK with. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Hejsil sounds plausible - what about just replacing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that could work. As long as this file is generated with one consistent line ending then I think git will be happy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hejsil explained it a bit clearer than I did, it seems.
I'm not sure we should start doing this kind of thing, but if it works, it works. I still feel that simply requiring that the script be run with Py3 would be a better decision. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do agree we can restrict users to Python 3, but I still like the explicit replace (with a comment explaining "why") because that makes it fully explicit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should be a separate pull request? Then we could get this one merged ;) |
||
for f in intro_files: | ||
sys.stdout.write(open(f, 'r').read()) | ||
print("*/") | ||
sys.stdout.write(open(f, 'rb').read()) | ||
sys.stdout.write("*/" + os.linesep) | ||
|
||
# print(os.linesep + "#ifndef " + macro + "_SINGLE_HEADER"); | ||
# print("#define " + macro + "_SINGLE_HEADER"); | ||
print("#ifndef NK_SINGLE_FILE"); | ||
print(" #define NK_SINGLE_FILE"); | ||
print("#endif"); | ||
print(""); | ||
sys.stdout.write("#ifndef NK_SINGLE_FILE" + os.linesep); | ||
sys.stdout.write(" #define NK_SINGLE_FILE" + os.linesep); | ||
sys.stdout.write("#endif" + os.linesep); | ||
sys.stdout.write(os.linesep); | ||
|
||
for f in pub_files: | ||
sys.stdout.write(open(f, 'r').read()) | ||
sys.stdout.write(open(f, 'rb').read()) | ||
# print("#endif /* " + macro + "_SINGLE_HEADER */"); | ||
|
||
print(os.linesep + "#ifdef " + macro + "_IMPLEMENTATION"); | ||
print(""); | ||
sys.stdout.write(os.linesep + "#ifdef " + macro + "_IMPLEMENTATION" + os.linesep) | ||
|
||
for f in priv_files1: | ||
print(omit_includes(open(f, 'r').read(), | ||
sys.stdout.write(omit_includes(open(f, 'rb').read(), | ||
pub_files + priv_files1 + priv_files2 + extern_files)) | ||
sys.stdout.write(os.linesep) | ||
|
||
for f in extern_files: | ||
print(fix_comments(open(f, 'r').read())) | ||
sys.stdout.write(fix_comments(open(f, 'rb').read())) | ||
sys.stdout.write(os.linesep) | ||
|
||
for f in priv_files2: | ||
print(omit_includes(open(f, 'r').read(), | ||
sys.stdout.write(omit_includes(open(f, 'rb').read(), | ||
pub_files + priv_files1 + priv_files2 + extern_files)) | ||
sys.stdout.write(os.linesep) | ||
|
||
print("#endif /* " + macro + "_IMPLEMENTATION */"); | ||
sys.stdout.write("#endif /* " + macro + "_IMPLEMENTATION */" + os.linesep); | ||
|
||
print(os.linesep + "/*") | ||
sys.stdout.write(os.linesep + "/*" + os.linesep) | ||
for f in outro_files: | ||
sys.stdout.write(open(f, 'r').read()) | ||
print("*/" + os.linesep) | ||
sys.stdout.write(open(f, 'rb').read()) | ||
sys.stdout.write("*/" + os.linesep) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include "nuklear.h" | ||
#include "nuklear_internal.h" | ||
|
||
/* =============================================================== | ||
* | ||
* 9-SLICE | ||
* | ||
* ===============================================================*/ | ||
NK_API struct nk_9slice | ||
nk_sub9slice_ptr(void *ptr, nk_ushort w, nk_ushort h, struct nk_rect rgn, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b) | ||
{ | ||
struct nk_9slice s; | ||
nk_zero(&s, sizeof(s)); | ||
struct nk_image *i = (struct nk_image*)&s; | ||
i->handle.ptr = ptr; | ||
i->w = w; i->h = h; | ||
i->region[0] = (nk_ushort)rgn.x; | ||
i->region[1] = (nk_ushort)rgn.y; | ||
i->region[2] = (nk_ushort)rgn.w; | ||
i->region[3] = (nk_ushort)rgn.h; | ||
s.l = l; s.t = t; s.r = r; s.b = b; | ||
return s; | ||
} | ||
NK_API struct nk_9slice | ||
nk_sub9slice_id(int id, nk_ushort w, nk_ushort h, struct nk_rect rgn, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b) | ||
{ | ||
struct nk_9slice s; | ||
nk_zero(&s, sizeof(s)); | ||
struct nk_image *i = (struct nk_image*)&s; | ||
i->handle.id = id; | ||
i->w = w; i->h = h; | ||
i->region[0] = (nk_ushort)rgn.x; | ||
i->region[1] = (nk_ushort)rgn.y; | ||
i->region[2] = (nk_ushort)rgn.w; | ||
i->region[3] = (nk_ushort)rgn.h; | ||
s.l = l; s.t = t; s.r = r; s.b = b; | ||
return s; | ||
} | ||
NK_API struct nk_9slice | ||
nk_sub9slice_handle(nk_handle handle, nk_ushort w, nk_ushort h, struct nk_rect rgn, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b) | ||
{ | ||
struct nk_9slice s; | ||
nk_zero(&s, sizeof(s)); | ||
struct nk_image *i = (struct nk_image*)&s; | ||
i->handle = handle; | ||
i->w = w; i->h = h; | ||
i->region[0] = (nk_ushort)rgn.x; | ||
i->region[1] = (nk_ushort)rgn.y; | ||
i->region[2] = (nk_ushort)rgn.w; | ||
i->region[3] = (nk_ushort)rgn.h; | ||
s.l = l; s.t = t; s.r = r; s.b = b; | ||
return s; | ||
} | ||
NK_API struct nk_9slice | ||
nk_9slice_handle(nk_handle handle, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b) | ||
{ | ||
struct nk_9slice s; | ||
nk_zero(&s, sizeof(s)); | ||
struct nk_image *i = (struct nk_image*)&s; | ||
i->handle = handle; | ||
i->w = 0; i->h = 0; | ||
i->region[0] = 0; | ||
i->region[1] = 0; | ||
i->region[2] = 0; | ||
i->region[3] = 0; | ||
s.l = l; s.t = t; s.r = r; s.b = b; | ||
return s; | ||
} | ||
NK_API struct nk_9slice | ||
nk_9slice_ptr(void *ptr, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b) | ||
{ | ||
struct nk_9slice s; | ||
nk_zero(&s, sizeof(s)); | ||
struct nk_image *i = (struct nk_image*)&s; | ||
NK_ASSERT(ptr); | ||
i->handle.ptr = ptr; | ||
i->w = 0; i->h = 0; | ||
i->region[0] = 0; | ||
i->region[1] = 0; | ||
i->region[2] = 0; | ||
i->region[3] = 0; | ||
s.l = l; s.t = t; s.r = r; s.b = b; | ||
return s; | ||
} | ||
NK_API struct nk_9slice | ||
nk_9slice_id(int id, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b) | ||
{ | ||
struct nk_9slice s; | ||
nk_zero(&s, sizeof(s)); | ||
struct nk_image *i = (struct nk_image*)&s; | ||
i->handle.id = id; | ||
i->w = 0; i->h = 0; | ||
i->region[0] = 0; | ||
i->region[1] = 0; | ||
i->region[2] = 0; | ||
i->region[3] = 0; | ||
s.l = l; s.t = t; s.r = r; s.b = b; | ||
return s; | ||
} | ||
NK_API int | ||
nk_9slice_is_sub9slice(const struct nk_9slice* slice) | ||
{ | ||
NK_ASSERT(slice); | ||
return !(slice->img.w == 0 && slice->img.h == 0); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look correct to me (this shouldn't be dependent on the environment in which this script is being executed). I think this should actually be hard coded - i.e. something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will, for some bizzare reason, always give priority to \n. This results in each comment being replaced with
\r /* comment */ \n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently I need to learn Python regexes better 😢. I think the issue is with the
$
character, but I think the easiest thing would be to rewrite it to simplyre.sub( "//([^\r\n]*)\r?\n?", "/* \\1 */\n", s )
. Does this work with python 2.x on your system?Also if we're already at it, we should discourage the use of
str
as variable label as it's a keyword in Python.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with that one is that it appends LF instead of the system-specific newline. I think it might be best to just completely remove the comment replacing feature, as
//
is fully supported in C99 and later, and in C90 if strict mode is not used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my goal 😉.
We're trying to be strictly C89 compatible for good reasons (and this presumably won't change during the coming few years). That's why we introduced the "replacing feature".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But of course, I could write the replacement feature in POSIX shell and assume each Windows dev has git for windows installed allowing them to run
paq.sh
and removepaq.bat
completely. But I'd prefer leaving it up to thebuild.py
script.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my reply below as to why this isn't recommended.
I'm not sure if this would be a good assumption to make. Somehow we need to fix the regex so that it searches for the line-endings in the order that they are specified, instead of always giving precedence to
\n
.