About Monkey 2 › Forums › Monkey 2 Programming Help › Adding two views to one view
This topic contains 8 replies, has 4 voices, and was last updated by 
 Hezkore
 2 years ago.
- 
		AuthorPosts
 - 
		
			
				
April 10, 2017 at 1:42 am #7832
I’m trying to make my own “view” (they’ll forever be called gadgets to me heh)
Basically, I want one main view, which does nothing more than house two other views in a DockingView you can resize.I can’t figure out how to do it though!
I’ve tried AddChildView but it never produces any result, it’s like it’s never added.
Something similar to this:Monkey12345678910Class MainView Extends ViewMethod New()Local d:DockingView=New DockingViewd.AddView(New View1,"left","",True)d.AddView(New View2,"right","",True)Self.AddChildView(d)EndEndHere’s a picture of what I want:

Main view, view 1 and view 2 would just be one single view to the end user.
When you make a new view, you’d just do blah=New MainView, and view 1 and 2 would always be part of it.Any examples of this?
April 10, 2017 at 4:08 am #7836Look at this example
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100Namespace CustomViews#Import "<std>"#Import "<mojo>"#Import "<mojox>"Using std..Using mojo..Using mojox..Class View1 Extends ViewMethod OnRender( canvas:Canvas ) Overridecanvas.Color=Color.Greencanvas.DrawText( "View 1",Frame.Width*.5,Frame.Height*.5,.5,.5 )EndEndClass View2 Extends ViewMethod New()RenderStyle.BackgroundColor=Color.SteelEndMethod OnRender( canvas:Canvas ) Overridecanvas.Color=Color.Whitecanvas.DrawText( "View 2",Frame.Width*.5,Frame.Height*.5,.5,.5 )EndEndClass GameView Extends ViewMethod New()RenderStyle.BackgroundColor=Color.DarkGreyEndMethod OnRender( canvas:Canvas ) Overridecanvas.Color=Color.Redcanvas.DrawText( "Game View",Frame.Width*.5,Frame.Height*.5,.5,.5 )EndEndClass MainView Extends DockingViewMethod New()Super.New()Local d:=New DockingViewd.AddView( New View1,"left","50%",True )d.AddView( New View2,"left","50%",True )AddView( d,"bottom","240",True )ContentView=New GameViewEndMethod OnRender( canvas:Canvas ) OverrideSuper.OnRender( canvas )'canvas.Color=Color.Red'canvas.DrawText( "MainView",Frame.Width*.5,Frame.Height*.5,.5,.5 )EndEndClass AppWindow Extends WindowMethod New()Super.New( "CustomView test",800,600,WindowFlags.Resizable|WindowFlags.Center )Local view:=New MainViewContentView=viewEndMethod OnRender( canvas:Canvas ) OverrideSuper.OnRender( canvas )RequestRender()EndEndFunction Main()New AppInstanceNew AppWindowApp.Run()EndInside of MainView you can see two different ways of AddView – one with percentage, other with pixels.
If you use percentage – views will resizable according to parent view.
If you use pixels – resizing beam became visible, so we can change it position.
April 10, 2017 at 1:59 pm #7840Super big thanks!
I’m having a super hard time understanding how “Views” work…
MaxGUI made sense to me, MojoX does not haha.With a bit of messing around, I finally managed to get it to act the way I want!
April 11, 2017 at 10:35 am #7851AFAIK, AdamStrange made his own layout system, but I didn’t look at that.
I like QT widgets, maxgui is like qt. But i don’t remember is it possible to use auto-stretching gadgets in maxgui.
The power of ui system based on flexible layout system. Let’s bring it into mx2 official repo at one point?
mojo[y]
April 11, 2017 at 11:44 am #7852Yep, I made my own one, Mojox just doesn’t work.
Ted21 uses this new layout system for the custom controls as wells as mt font editor, map editor, sprite editor, and palette editor.
Check them out here:
Both the font and palette editor have no mojox anywhere near them – apart from the internal stuff that Mark moved into Mojo.
In essence I took the Mojo view and started from scratch from their building a proper flexible control and layout system.
April 11, 2017 at 9:52 pm #7859Mojox just doesn’t work.
Works fine here.
Mojox could definitely do with more layout views, like GridView, FlowView etc. These’ll happen one day, but DockingView is a very flexible alternative for now.
Wish I could spend more time on mojox, but it’s still relatively low priority…
April 11, 2017 at 10:46 pm #7860Yeah, it’s not that MojoX doesn’t work for me.
It’s just kinda confusing, especially without any proper documentation or examples.
MojoX doesn’t come naturally to me, like MaxGUI did.
I mean, I’ll probably learn it eventually, but it’s hard figuring everything on my own.April 11, 2017 at 11:14 pm #7861It’s just kinda confusing, especially without any proper documentation or examples.
There’s a sample for each view type in the mojox/tests dir, but yeah, docs could be better.
Mojox is a little more complex than MaxGUI as it aims for an algorithmic layout approach instead of using ‘hard coded’ coords/sizes everywhere ala MaxGUI. This is mainly to make it easier to write GUIs that work on a wide range of devices/resolutions, but it also helps when dealing with resizable windows and dynamic themes.
Alas, there is not a great deal of variety when it comes to layout style views. It really needs something like a GridView for arranging simple tool palettes etc, a FlowView for quick ‘n’ nasty ‘add some buttons to a windows’ functionality, and likely a bunch more! DockingView is flexible, but it can be cumbersome to use in some situations.
I’ll probably be getting back into some mojox soon, but in the meantime, here’s a little GridView that might be useful for situations such as the above:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161#Import "<std>"#Import "<mojo>"#Import "<mojox>"Using std..Using mojo..Using mojox..Class GridView Extends ViewMethod New( width:Int,height:Int )_gridw=width_gridh=heightEndMethod Clear()For Local cell:=Eachin _cellsRemoveChildView( cell.view )Next_cells.Clear()RequestRender()EndMethod AddView( view:View,x:Int,y:Int,width:Int=1,height:Int=1 )_cells.Add( New Cell( view,x,y,width,height ) )AddChildView( view )RequestRender()EndMethod ReplaceView( view:View,with:View )For Local cell:=Eachin _cellsIf cell.view<>view ContinueRemoveChildView( view )AddChildView( with )cell.view=withRequestRender()ReturnNextEndMethod RemoveView( view:View )For Local cell:=Eachin _cellsIf cell.view<>view ContinueRemoveChildView( view )_cells.Remove( cell )RequestRender()ReturnNextEndProtectedMethod OnMeasure:Vec2i() OverrideLocal maxw:=0Local maxh:=0For Local cell:=Eachin _cellsLocal size:=cell.view.MeasuredSizemaxw=Max( maxw,Int( Ceil( size.x/cell.w ) ) )maxh=Max( maxh,Int( Ceil( size.y/cell.h ) ) )NextReturn New Vec2i( maxw * _gridw,maxh * _gridh )EndMethod OnLayout() OverrideFor Local cell:=Eachin _cellsLocal x0:=cell.x * Width / _gridwLocal x1:=(cell.x+cell.w) * Width / _gridwLocal y0:=cell.y * Height / _gridwLocal y1:=(cell.y+cell.h) * Height / _gridhcell.view.Frame=New Recti( x0,y0,x1,y1 )NextEndPrivateStruct CellField view:ViewField x:IntField y:IntField w:IntField h:IntMethod New( view:View,x:Int,y:Int,w:Int,h:Int )Self.view=viewSelf.x=xSelf.y=ySelf.w=wSelf.h=hEndEndField _cellw:IntField _cellh:IntField _gridw:IntField _gridh:IntField _cells:=New Stack<Cell>EndClass MyWindow Extends WindowField mainView:GridViewField gameView:TextViewField view1:TextViewField view2:TextViewMethod New()Super.New( "GridView",640,480,WindowFlags.Resizable )gameView=New TextViewgameView.Text="GameView"Local gameViewStyle:=gameView.Style.Copy()gameViewStyle.BackgroundColor=Color.RedgameView.Style=gameViewStyleview1=New TextViewview1.Text="View1"Local view1Style:=view1.Style.Copy()view1Style.BackgroundColor=Color.Greenview1.Style=view1Styleview2=New TextViewview2.Text="View2"Local view2Style:=view2.Style.Copy()view2Style.BackgroundColor=Color.Blueview2.Style=view2StylemainView=New GridView( 8,8 ) 'create 8x8 gridmainView.AddView( gameView,0,0,8,4 ) 'top half is gameViewmainView.AddView( view1,0,4,4,4 ) 'lower left quarter is view1mainView.AddView( view2,4,4,4,4 ) 'lower right quarter is view2ContentView=mainViewEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()End…that was pretty fun to write – think I’ll add this to mojox right now!
April 11, 2017 at 11:20 pm #7862Whoa!
You’re a machine Mark haha.Thanks a bunch, but focus on the 3D module! (Or ZIP module)
The 3D module is the only reason I check Twitter every day heh. x) - 
		AuthorPosts
 
You must be logged in to reply to this topic.