From dbb400ac08feee88ac636517949c1527f2b889df Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 6 Nov 2018 10:45:15 -0800 Subject: [PATCH] fix left view updates --- lib/base/list_view.rb | 4 ++++ lib/views/left_view.rb | 36 +++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/base/list_view.rb b/lib/base/list_view.rb index 85dfc94..0a7800f 100644 --- a/lib/base/list_view.rb +++ b/lib/base/list_view.rb @@ -16,6 +16,10 @@ class ListView < ElementView @elements = [] end + def length + @children.length + end + # create a root node acording to the tag given (default div) # The tag name will be passed to the div function, so class and id may be set as well (see there) # draw all children and keep the elements as @elements diff --git a/lib/views/left_view.rb b/lib/views/left_view.rb index adbb649..93e26ea 100644 --- a/lib/views/left_view.rb +++ b/lib/views/left_view.rb @@ -1,23 +1,22 @@ require_relative "classes_view" +# the whole of the left, ie selection, space and classes class LeftView < ListView def initialize( interpreter ) @interpreter = interpreter - init_space super([ SelectView.new(interpreter) , - @space, + ObjectView.new( Parfait.object_space , @interpreter , 26), ClassesView.new(interpreter) ]) interpreter.register_event(:state_changed, self) end - def init_space - @space = ObjectView.new( Parfait.object_space , @interpreter , 26) - end - + # need to re-init when we go to running, as the objects (and the actual space) change + # we replace space and class view with new instances def state_changed( old , new_s ) return unless new_s == :running - init_space - replace_at( 1 , @space ) + space = ObjectView.new( Parfait.object_space , @interpreter , 26) + replace_at( 1 , space ) + replace_at( 2 , ClassesView.new(@interpreter) ) end def draw @@ -26,6 +25,12 @@ class LeftView < ListView end +# view for the little code select, implemented as a normal expandable menu +# +# on click calls select method +# +# derive from element, meaning we draw +# # TODO: make into listview, so code can be the next level expansion class SelectView < ElementView def initialize( interpreter ) @@ -45,32 +50,33 @@ class SelectView < ElementView def selection_codes @codes = get_codes.keys list = div "ul" - @codes << @codes.first if @codes.length == 1 #otherwise unselectable @codes.each do |c| code = div("li") << div("a" , c ) code.style["z-index"] = 10 code.on("click"){ select(c) } - list << code + list << code end @element.at_css(".code_list") << list end - + # select method set up as click handler for the codes + # restart the interpreter after compiling def select( code ) puts "selecting #{code}" @interpreter.set_state :stopped @element.at_css(".selected").text = code - ruby = get_codes[code] - linker = RubyX::RubyXCompiler.new.ruby_to_binary(as_main(ruby), :interpreter) + ruby = as_main(get_codes[code]) + linker = RubyX::RubyXCompiler.new.ruby_to_binary(ruby, :interpreter) @interpreter.start_program(linker) end + def as_main(statements) "class Space ;def yielder; return yield ; end;def main(arg) ; #{statements}; end; end" end def get_codes - { while_with_calls: 'a = 0; while( 0 > a); a = 1 + a;end;return a', + { while_with_calls: 'a = 2; while( 0 < a); a = a - 1;end;return a', set_internal_byte: "return 'Hello'.set_internal_byte(1,75)" , - called_if: 'if( 10 ); return "then";else;return "else";end' , + basic_if: 'if( 10 ); return "then";else;return "else";end' , plus: 'return 5 + 7' , yield: "a = yielder {return 15} ; return a" , return: 'return 5' ,