Sfoglia il codice sorgente

standard app dev...

leonsal 8 anni fa
parent
commit
96c4a2bd47
2 ha cambiato i file con 144 aggiunte e 39 eliminazioni
  1. 14 2
      gui/vboxlayout.go
  2. 130 37
      util/app/app.go

+ 14 - 2
gui/vboxlayout.go

@@ -101,7 +101,7 @@ func (bl *VBoxLayout) Recalc(ipan IPanel) {
 		var totalHeight float32
 		for _, ichild := range parent.Children() {
 			child := ichild.(IPanel).GetPanel()
-			if !child.Visible() {
+			if !child.Visible() || !child.Bounded() {
 				continue
 			}
 			totalHeight += child.Height()
@@ -119,7 +119,7 @@ func (bl *VBoxLayout) Recalc(ipan IPanel) {
 		var maxWidth float32
 		for _, ichild := range parent.Children() {
 			child := ichild.(IPanel).GetPanel()
-			if !child.Visible() {
+			if !child.Visible() || !child.Bounded() {
 				continue
 			}
 			if child.Width() > maxWidth {
@@ -141,6 +141,9 @@ func (bl *VBoxLayout) Recalc(ipan IPanel) {
 	paramsDef := VBoxLayoutParams{Expand: 0, AlignH: AlignLeft}
 	for pos, obj := range parent.Children() {
 		pan := obj.(IPanel).GetPanel()
+		if !pan.Visible() || !pan.Bounded() {
+			continue
+		}
 		// Get item layout parameters or use default
 		params := paramsDef
 		if pan.layoutParams != nil {
@@ -177,6 +180,9 @@ func (bl *VBoxLayout) Recalc(ipan IPanel) {
 		if totalSpace > 0 {
 			for _, obj := range parent.Children() {
 				pan := obj.(IPanel).GetPanel()
+				if !pan.Visible() || !pan.Bounded() {
+					continue
+				}
 				// Get item layout parameters or use default
 				params := paramsDef
 				if pan.layoutParams != nil {
@@ -191,6 +197,9 @@ func (bl *VBoxLayout) Recalc(ipan IPanel) {
 		} else {
 			for _, obj := range parent.Children() {
 				pan := obj.(IPanel).GetPanel()
+				if !pan.Visible() || !pan.Bounded() {
+					continue
+				}
 				// Get item layout parameters or use default
 				params := paramsDef
 				if pan.layoutParams != nil {
@@ -232,6 +241,9 @@ func (bl *VBoxLayout) Recalc(ipan IPanel) {
 	width := parent.ContentWidth()
 	for pos, obj := range parent.Children() {
 		pan := obj.(IPanel).GetPanel()
+		if !pan.Visible() || !pan.Bounded() {
+			continue
+		}
 		// Get item layout parameters or use default
 		params := paramsDef
 		if pan.layoutParams != nil {

+ 130 - 37
util/app/app.go

@@ -9,8 +9,11 @@ import (
 	"github.com/g3n/engine/audio/ov"
 	"github.com/g3n/engine/audio/vorbis"
 	"github.com/g3n/engine/camera"
+	"github.com/g3n/engine/camera/control"
 	"github.com/g3n/engine/core"
 	"github.com/g3n/engine/gls"
+	"github.com/g3n/engine/gui"
+	"github.com/g3n/engine/light"
 	"github.com/g3n/engine/math32"
 	"github.com/g3n/engine/renderer"
 	"github.com/g3n/engine/util/logger"
@@ -18,20 +21,24 @@ import (
 )
 
 type App struct {
-	win         window.IWindow       // Application window
-	gl          *gls.GLS             // Application OpenGL state
-	log         *logger.Logger       // Application logger
-	renderer    *renderer.Renderer   // Renderer object
-	camPersp    *camera.Perspective  // Perspective camera
-	camOrtho    *camera.Orthographic // Orthographic camera
-	audio       bool                 // Audio available
-	vorbis      bool                 // Vorbis decoder available
-	audioEFX    bool                 // Audio effect extension support available
-	audioDev    *al.Device           // Audio player device
-	captureDev  *al.Device           // Audio capture device
-	frameRater  *FrameRater
+	win         window.IWindow        // Application window
+	gl          *gls.GLS              // OpenGL state
+	log         *logger.Logger        // Default application logger
+	renderer    *renderer.Renderer    // Renderer object
+	camPersp    *camera.Perspective   // Perspective camera
+	camOrtho    *camera.Orthographic  // Orthographic camera
+	camera      camera.ICamera        // Current camera
+	orbit       *control.OrbitControl // Camera orbit controller
+	ambLight    *light.Ambient        // Default ambient light
+	audio       bool                  // Audio available
+	vorbis      bool                  // Vorbis decoder available
+	audioEFX    bool                  // Audio effect extension support available
+	audioDev    *al.Device            // Audio player device
+	captureDev  *al.Device            // Audio capture device
+	frameRater  *FrameRater           // Render loop frame rater
+	scene       *core.Node            // Node container for 3D tests
+	guiroot     *gui.Root             // Gui root panel
 	oCpuProfile *string
-	scene       *core.Node // Node container for 3D tests
 }
 
 type Options struct {
@@ -44,7 +51,7 @@ type Options struct {
 	TargetFPS    uint // Desired FPS
 }
 
-// appInstance points to the single Application instance
+// appInstance contains the pointer to the single Application instance
 var appInstance *App
 
 // Creates creates and returns the application object using the specified name for
@@ -63,11 +70,11 @@ func Create(name string, ops Options) (*App, error) {
 	}
 
 	// Creates application logger
-	log := logger.New(name, nil)
-	log.AddWriter(logger.NewConsole(false))
-	log.SetFormat(logger.FTIME | logger.FMICROS)
-	log.SetLevel(ops.LogLevel)
-	log.Info("%s v%d.%d starting", name, ops.VersionMajor, ops.VersionMinor)
+	app.log = logger.New(name, nil)
+	app.log.AddWriter(logger.NewConsole(false))
+	app.log.SetFormat(logger.FTIME | logger.FMICROS)
+	app.log.SetLevel(ops.LogLevel)
+	app.log.Info("%s v%d.%d starting", name, ops.VersionMajor, ops.VersionMinor)
 
 	// Window event handling must run on the main OS thread
 	runtime.LockOSThread()
@@ -86,16 +93,17 @@ func Create(name string, ops Options) (*App, error) {
 		sheight = ops.WinHeight
 	}
 	win.SetSize(swidth, sheight)
+	app.win = win
 
 	// Create OpenGL state
 	gl, err := gls.New()
 	if err != nil {
 		return nil, err
 	}
-
-	// Creates application object
-	app.win = win
 	app.gl = gl
+	cc := math32.NewColor("gray")
+	app.gl.ClearColor(cc.R, cc.G, cc.B, 1)
+	app.gl.Clear(gls.DEPTH_BUFFER_BIT | gls.STENCIL_BUFFER_BIT | gls.COLOR_BUFFER_BIT)
 
 	// Creates perspective camera
 	width, height := app.win.GetSize()
@@ -108,15 +116,32 @@ func Create(name string, ops Options) (*App, error) {
 	app.camOrtho.LookAt(&math32.Vector3{0, 0, 0})
 	app.camOrtho.SetZoom(1.0)
 
+	// Default camera is perspective
+	app.camera = app.camPersp
+
+	// Creates orbit camera control
+	// It is important to do this after the root panel subscription
+	// to avoid GUI events being propagated to the orbit control.
+	app.orbit = control.NewOrbitControl(app.camera, app.win)
+
+	// Creates scene for 3D objects
+	app.scene = core.NewNode()
+
+	// Creates gui root panel
+	app.guiroot = gui.NewRoot(app.gl, app.win)
+
 	// Creates renderer
 	app.renderer = renderer.NewRenderer(gl)
 	err = app.renderer.AddDefaultShaders()
 	if err != nil {
 		return nil, fmt.Errorf("Error from AddDefaulShaders:%v", err)
 	}
+	app.renderer.SetScene(app.scene)
+	app.renderer.SetGui(app.guiroot)
 
-	// Creates scene for 3D objects
-	app.scene = core.NewNode()
+	// Adds white ambient light to the scene
+	app.ambLight = light.NewAmbient(&math32.Color{1.0, 1.0, 1.0}, 0.5)
+	app.scene.Add(app.ambLight)
 
 	// Create frame rater
 	app.frameRater = NewFrameRater(60)
@@ -127,50 +152,118 @@ func Create(name string, ops Options) (*App, error) {
 	})
 
 	return app, nil
-
 }
 
-// Returns the single application instance
+// App returns the application single instance or nil
+// if the application was not created yet
 func Get() *App {
 
 	return appInstance
 }
 
+// Log returns the application logger
+func (a *App) Log() *logger.Logger {
+
+	return a.log
+}
+
 // Window returns the application window
 func (a *App) Window() window.IWindow {
 
 	return a.win
 }
 
-func (a *App) Run() {
+// Gui returns the current application Gui root panel
+func (app *App) Gui() *gui.Root {
+
+	return app.guiroot
+}
+
+// Scene returns the current application 3D scene
+func (a *App) Scene() *core.Node {
+
+	return a.scene
+}
+
+// SetScene sets the 3D scene to be rendered
+func (a *App) SetScene(scene *core.Node) {
+
+	a.renderer.SetScene(scene)
+}
+
+// SetGui sets the root panel of th3 gui to be rendered
+func (app *App) SetGui(root *gui.Root) {
+
+	app.guiroot = root
+	app.renderer.SetGui(app.guiroot)
+}
+
+// CameraPersp returns the application perspective camera
+func (app *App) CameraPersp() *camera.Perspective {
+
+	return app.camPersp
+}
+
+// Camera returns the current application camera
+func (app *App) Camera() camera.ICamera {
+
+	return app.camera
+}
+
+func (app *App) Renderer() *renderer.Renderer {
 
-	for !a.win.ShouldClose() {
+	return app.renderer
+}
+
+// Runs runs the application render loop
+func (app *App) Run() error {
+
+	for !app.win.ShouldClose() {
 		// Starts measuring this frame
-		a.frameRater.Start()
+		app.frameRater.Start()
+
+		// Renders the current scene and/or gui
+		rendered, err := app.renderer.Render(app.camera)
+		if err != nil {
+			return err
+		}
+		app.log.Error("render stats:%+v", app.renderer.Stats())
 
 		// Poll input events and process them
-		a.win.PollEvents()
+		app.win.PollEvents()
 
-		a.win.SwapBuffers()
+		if rendered {
+			app.win.SwapBuffers()
+		}
 
 		// Controls the frame rate and updates the FPS for the user
-		a.frameRater.Wait()
+		app.frameRater.Wait()
 	}
+	return nil
+}
+
+// Quit ends the application
+func (app *App) Quit() {
+
+	app.win.SetShouldClose(true)
 }
 
 // OnWindowResize is called when the window resize event is received
 func (app *App) OnWindowResize() {
 
-	// Sets view port
+	// Get window size and sets the viewport to the same size
 	width, height := app.win.GetSize()
 	app.gl.Viewport(0, 0, int32(width), int32(height))
-	aspect := float32(width) / float32(height)
 
-	// Sets camera aspect ratio
+	// Sets perspective camera aspect ratio
+	aspect := float32(width) / float32(height)
 	app.camPersp.SetAspect(aspect)
+	app.log.Error("app window resize:%v", aspect)
 
-	// Sets GUI root panel size
-	//ctx.root.SetSize(float32(width), float32(height))
+	// Sets the size of GUI root panel size to the size of the screen
+	if app.guiroot != nil {
+		app.guiroot.SetSize(float32(width), float32(height))
+	}
 }
 
 // LoadAudioLibs try to load audio libraries