MX2- real progress bar

Well, this is my first experiment to create a new ‘widget’ in MX2-mojox…

It’s BAD, it’s UGLY … but it works (meaning that it’s quite the result I want)

ps: there will too much hacking (and considering mojox/mx2 still a wip) is not a very safe move.

In any case, PBAR.MONKEY2 should be placed in modules/mojox folder, while mojox.monkey2 should be changed adding #IMPORT <“pbar”> somewhere

The PBAR class (it’s a ‘clone’ of Label class – changed)

Namespace mojox

#rem monkeydoc The PBar class.
#end
Class PBar Extends View

	#rem monkeydoc Invoked when the label is clicked.
	#end
	Field Clicked:Void()

	#rem monkeydoc Invoked when the label is right clicked.
	#end
	Field RightClicked:Void()
	
	#rem monkeydoc Invoked when the label is double clicked.
	#end
	Field DoubleClicked:Void()
	
	#rem monkeydoc Creates a new label.
	#end
	Method New( value:Float=0.0)
		Style=GetStyle( "Button" )
		_value=value
		Layout="fill-x"
		Gravity=New Vec2f( 0,.5 )
	End

	Method Update:Void(v:Float=0.0)
			_value=v
			
			If _value<0 _value=0
			If _value>1 _value=1
			
			RequestRender()

	End 

	#rem monkeydoc Adds a view to the right of the label.
	#end
	Method AddView( view:View )
		
		AddChildView( view )
	
		_views.Push( view )
	End
	
	#rem monkeydoc Removes a view from the label.
	#end
	Method RemoveView( view:View )
	
		RemoveChildView( view )
		
		_views.Remove( view )
	End
	
	Protected
	
	Method OnMeasure:Vec2i() Override
		_viewsSize=New Vec2i( 0,0 )
		
		Local w:=0,h:=0

		w=100
		h=20
	
		
		For Local view:=Eachin _views
			_viewsSize.x+=view.LayoutSize.x
			_viewsSize.y=Max( _viewsSize.y,view.LayoutSize.y )
		Next
		
		w+=_viewsSize.x
		h=Max( h,_viewsSize.y )
		
		Return New Vec2i( w,h )
	End
	
	Method OnLayout() Override
	
		
	
		Local iy:=(Height-_iconSize.y)/2
		_iconRect=New Recti( 0,iy,_iconSize.x,iy+_iconSize.y )
	
		Local tx:=_iconSize.x,ty:=0
		Local tw:=_textSize.x,th:=Height
		_textRect=New Recti( tx,ty,tx+tw,ty+th )
		
		_textRect=New Recti(0,0,100,20)'the default size (for now!)
		
		Local x1:=Width
		For Local view:=Eachin _views.Backwards()
		
			Local x0:=Max( x1-view.LayoutSize.x,_textRect.Right )
			view.Frame=New Recti( x0,0,x1,Height )
			x1=x0
		Next
		
		Return
		
		Local x0:=_textRect.Right
		For Local view:=Eachin _views
		
			Local x1:=Min( x0+view.LayoutSize.x,Width )
			view.Frame=New Recti( x0,0,x1,Height )
			x0=x1
		Next
	
	End
	
	Method OnRender( canvas:Canvas ) Override
			Local s:=Self.Frame
			Local pp:Float
			
			pp=Float(100)*_value
			
			canvas.Color=Color.Grey
			canvas.DrawRect(s.X,s.Y,s.X+102,s.Y+15)
			canvas.Color=Color.Red
			canvas.DrawRect(s.X+1,s.Y+1,s.X+pp,s.Y+12)

	End
	
	Method OnMouseEvent( event:MouseEvent ) Override

		Select event.Type
		Case EventType.MouseDown,EventType.MouseWheel
		
			Return
			
		Case EventType.MouseClick
		
			Clicked()
			
		Case EventType.MouseRightClick
		
			RightClicked()
			
		Case EventType.MouseDoubleClick
		
			DoubleClicked()
		End

		event.Eat()
	End
	
	Private

	Field _value:float
	Field _views:=New Stack<View>
	
	Field _iconSize:Vec2i
	Field _iconRect:Recti
	Field _textSize:Vec2i
	Field _textRect:Recti
	Field _viewsSize:Vec2i

End

There are some thing not needed (for now) like _iconRect, _textSize etc

This is a working example

#import "<std>"
#import "<mojo>"
#import "<mojox>"

Using std..
Using mojo..
Using mojox..

Class MyWindow Extends Window


	Global pb:PBar,label:Label
	Global timer:Timer
	Global counter:Int
	Global bvalue:Float=0.1

	Method New()
		Super.New( "Pbar Demo",640,480,WindowFlags.Resizable )
		
		label=New Label( "Idle" )
		label.Gravity=New Vec2f( .5,0 )
			
		pb=New PBar(bvalue)
		pb.Layout="stretch"
		
		timer=New Timer(2,OnUpdate)
		
	
		Local dockingView:=New DockingView
		dockingView.AddView( label,"top" )
		dockingView.AddView( pb,"top" )
		ContentView=dockingView
	End
	
	Method OnUpdate()
		counter+=1
		Print "Tick: "+counter	
		bvalue=bvalue+.1
		pb.Update(bvalue)
		label.Text="Value "+bvalue
	End 
	
	
End

Function Main()

	New AppInstance
	
	New MyWindow
	
	App.Run()
End

I repeat, just a test to check if things work!

I still must understand the ‘render’ of the widgets

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close