From f61b72728e27ab3821511d8cc1aaa5de927ab5bd Mon Sep 17 00:00:00 2001 From: Seth Kingsley Date: Wed, 14 Nov 2018 01:51:12 -0800 Subject: [PATCH] More thoroughly parse arguments and defaults --- misc/cpp/extract_raii.rb | 191 +++++++++++++++++++++++++++++---------- misc/cpp/imgui_raii.h | 57 ++++++++++-- 2 files changed, 193 insertions(+), 55 deletions(-) diff --git a/misc/cpp/extract_raii.rb b/misc/cpp/extract_raii.rb index 666f1b070..48a7277c7 100644 --- a/misc/cpp/extract_raii.rb +++ b/misc/cpp/extract_raii.rb @@ -40,7 +40,7 @@ class WrapperClass when 'Begin', 'BeginChild' 'IsContentVisible' else - 'IsOpen' if m[:type] == 'bool' + 'IsOpen' if m[:type] == %w{bool} end puts <[\w\s]+\w)\s+(?\w*)\((?[^)]+)\)(?[^;]*);(?.*)$/) do |m| - next unless m[:name].match(/^(Begin|Push|Tree)/) + m = parse_method(line) + next unless m - argnames = m[:args].split(/,\s*/).map do |arg| - arg = arg.split(/\s*=\s*/, 2).first - arg.split(/[ \*]/).last - end + next unless m[:name].match(/^(Begin|Push|Tree)/) - # Check for obsolete - if m[:name] == 'Begin' && - (argnames == %w{name p_open size_on_first_use bg_alpha_override flags} || - argnames == %w{items_count items_height}) - next - end + $stderr.puts m.inspect if $-d - $stderr.puts m.inspect if $-d - - fail "Return value #{m[:type]} is not bool or void" unless %w{bool void}.include?(m[:type]) - - if !current_class || current_class.name != m[:name] - if current_class - current_class.close - puts - end - - current_class = WrapperClass.new(m) - end - - attrs = m[:attrs].gsub(/IM_FMT(ARGS|LIST)\(\d+\)/) do |a| - a.sub(/\d+/) { |index| (index.to_i + 1).to_s } - end - - print "#{INDENT * 2}#{current_class.class_name}(#{m[:args]})#{attrs} { " - - use_varargs = false - if argnames.last == '...' - argnames[-1] = 'ap' - use_varargs = true - print "va_list ap; va_start(ap, fmt); " - end - - print "#{current_class.state_var} = " if current_class.state_var - - print "ImGui::#{m[:name]}" - print 'V' if use_varargs - print "(#{argnames.join(', ')}); " - print 'va_end(ap); ' if use_varargs - - puts '}' + # Check for obsolete + if m[:name] == 'Begin' && + (m[:argnames] == %w{name p_open size_on_first_use bg_alpha_override flags} || + m[:argnames] == %w{items_count items_height}) + next end + + fail "Return value #{m[:type]} is not bool or void" unless [%w{bool}, %w{void}].include?(m[:type]) + + if !current_class || current_class.name != m[:name] + if current_class + current_class.close + puts + end + + current_class = WrapperClass.new(m) + end + + shift_index = false + attrs = m[:attrs].map do |attr| + case attr + when /^IM_FMT(ARGS|LIST)$/ + shift_index = true + when /^\d+/ + if shift_index + attr = (attr.to_i + 1).to_s + shift_index = false + end + end + attr + end.join('') + + args = m[:args].map { |argparts| argparts.join('') }.join(',') + print "#{INDENT * 2}#{current_class.class_name}(#{args})#{attrs} { " + + use_varargs = false + if m[:argnames].last == '...' + m[:argnames][-1] = 'ap' + use_varargs = true + print "va_list ap; va_start(ap, fmt); " + end + + print "#{current_class.state_var} = " if current_class.state_var + + print "ImGui::#{m[:name]}" + print 'V' if use_varargs + print "(#{m[:argnames].join(', ')}); " + print 'va_end(ap); ' if use_varargs + + puts '}' end current_class.close if current_class diff --git a/misc/cpp/imgui_raii.h b/misc/cpp/imgui_raii.h index 42caf63a9..4e7eb6cea 100644 --- a/misc/cpp/imgui_raii.h +++ b/misc/cpp/imgui_raii.h @@ -26,8 +26,8 @@ namespace ImScoped { bool IsContentVisible; - Child(const char* str_id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0) { IsContentVisible = ImGui::BeginChild(str_id, size, 0); } - Child(ImGuiID id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0) { IsContentVisible = ImGui::BeginChild(id, size, 0); } + Child(const char* str_id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0) { IsContentVisible = ImGui::BeginChild(str_id, size, border, flags); } + Child(ImGuiID id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0) { IsContentVisible = ImGui::BeginChild(id, size, border, flags); } ~Child() { ImGui::EndChild(); } explicit operator bool() const { return IsContentVisible; } @@ -93,6 +93,14 @@ namespace ImScoped IMGUI_DELETE_MOVE_COPY(ButtonRepeat); }; + struct Group + { + Group() { ImGui::BeginGroup(); } + ~Group() { if () ImGui::EndGroup(); } + + IMGUI_DELETE_MOVE_COPY(Group); + }; + struct ID { ID(const char* str_id) { ImGui::PushID(str_id); } @@ -170,13 +178,28 @@ namespace ImScoped IMGUI_DELETE_MOVE_COPY(TreeNodeExV); }; - struct TreePush + struct MainMenuBar { - TreePush(const char* str_id) { ImGui::TreePush(str_id); } - TreePush(const void* ptr_id = NULL) { ImGui::TreePush(ptr_id); } - ~TreePush() { ImGui::TreePop(); } + bool IsOpen; - IMGUI_DELETE_MOVE_COPY(TreePush); + MainMenuBar() { IsOpen = ImGui::BeginMainMenuBar(); } + ~MainMenuBar() { if (IsOpen) ImGui::EndMainMenuBar(); } + + explicit operator bool() const { return IsOpen; } + + IMGUI_DELETE_MOVE_COPY(MainMenuBar); + }; + + struct MenuBar + { + bool IsOpen; + + MenuBar() { IsOpen = ImGui::BeginMenuBar(); } + ~MenuBar() { if (IsOpen) ImGui::EndMenuBar(); } + + explicit operator bool() const { return IsOpen; } + + IMGUI_DELETE_MOVE_COPY(MenuBar); }; struct Menu @@ -191,6 +214,14 @@ namespace ImScoped IMGUI_DELETE_MOVE_COPY(Menu); }; + struct Tooltip + { + Tooltip() { ImGui::BeginTooltip(); } + ~Tooltip() { if () ImGui::EndTooltip(); } + + IMGUI_DELETE_MOVE_COPY(Tooltip); + }; + struct Popup { bool IsOpen; @@ -263,6 +294,18 @@ namespace ImScoped IMGUI_DELETE_MOVE_COPY(DragDropSource); }; + struct DragDropTarget + { + bool IsOpen; + + DragDropTarget() { IsOpen = ImGui::BeginDragDropTarget(); } + ~DragDropTarget() { if (IsOpen) ImGui::EndDragDropTarget(); } + + explicit operator bool() const { return IsOpen; } + + IMGUI_DELETE_MOVE_COPY(DragDropTarget); + }; + struct ClipRect { ClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect) { ImGui::PushClipRect(clip_rect_min, clip_rect_max, intersect_with_current_clip_rect); }