Procházet zdrojové kódy

standard app dev...

leonsal před 8 roky
rodič
revize
7bcdfacb1c
2 změnil soubory, kde provedl 68 přidání a 60 odebrání
  1. 40 45
      core/dispatcher.go
  2. 28 15
      util/application/application.go

+ 40 - 45
core/dispatcher.go

@@ -4,13 +4,13 @@
 
 
 package core
 package core
 
 
-import ()
-
+// Dispatcher implements an event dispatcher
 type Dispatcher struct {
 type Dispatcher struct {
 	evmap  map[string][]subscription // maps event name to subcriptions list
 	evmap  map[string][]subscription // maps event name to subcriptions list
 	cancel bool                      // flag informing cancelled dispatch
 	cancel bool                      // flag informing cancelled dispatch
 }
 }
 
 
+// IDispatcher is the interface for dispatchers
 type IDispatcher interface {
 type IDispatcher interface {
 	Subscribe(evname string, cb Callback)
 	Subscribe(evname string, cb Callback)
 	SubscribeID(evname string, id interface{}, cb Callback)
 	SubscribeID(evname string, id interface{}, cb Callback)
@@ -20,6 +20,7 @@ type IDispatcher interface {
 	CancelDispatch()
 	CancelDispatch()
 }
 }
 
 
+// Callback is the type for the Dispatcher callbacks functions
 type Callback func(string, interface{})
 type Callback func(string, interface{})
 
 
 type subscription struct {
 type subscription struct {
@@ -27,44 +28,42 @@ type subscription struct {
 	cb func(string, interface{})
 	cb func(string, interface{})
 }
 }
 
 
-// NewEventDispatcher creates and returns a pointer to an Event Dispatcher
+// NewDispatcher creates and returns a new Event Dispatcher
 func NewDispatcher() *Dispatcher {
 func NewDispatcher() *Dispatcher {
 
 
-	ed := new(Dispatcher)
-	ed.Initialize()
-	return ed
+	d := new(Dispatcher)
+	d.Initialize()
+	return d
 }
 }
 
 
 // Initialize initializes this event dispatcher.
 // Initialize initializes this event dispatcher.
 // It is normally used by other types which embed an event dispatcher
 // It is normally used by other types which embed an event dispatcher
-func (ed *Dispatcher) Initialize() {
+func (d *Dispatcher) Initialize() {
 
 
-	ed.evmap = make(map[string][]subscription)
+	d.evmap = make(map[string][]subscription)
 }
 }
 
 
 // Subscribe subscribes to receive events with the given name.
 // Subscribe subscribes to receive events with the given name.
-// If it is necessary to unsubscribe the event, the function SubscribeID
-// should be used.
-func (ed *Dispatcher) Subscribe(evname string, cb Callback) {
+// If it is necessary to unsubscribe the event, the function SubscribeID should be used.
+func (d *Dispatcher) Subscribe(evname string, cb Callback) {
 
 
-	ed.SubscribeID(evname, nil, cb)
+	d.SubscribeID(evname, nil, cb)
 }
 }
 
 
-// Subscribe subscribes to receive events with the given name.
+// SubscribeID subscribes to receive events with the given name.
 // The function accepts a unique id to be use to unsubscribe this event
 // The function accepts a unique id to be use to unsubscribe this event
-func (ed *Dispatcher) SubscribeID(evname string, id interface{}, cb Callback) {
+func (d *Dispatcher) SubscribeID(evname string, id interface{}, cb Callback) {
 
 
-	//log.Debug("Dispatcher(%p).SubscribeID:%s (%v)", ed, evname, id)
-	ed.evmap[evname] = append(ed.evmap[evname], subscription{id, cb})
+	d.evmap[evname] = append(d.evmap[evname], subscription{id, cb})
 }
 }
 
 
-// Unsubscribe unsubscribes from the specified event and subscription id
+// UnsubscribeID unsubscribes from the specified event and subscription id
 // Returns the number of subscriptions found.
 // Returns the number of subscriptions found.
-func (ed *Dispatcher) UnsubscribeID(evname string, id interface{}) int {
+func (d *Dispatcher) UnsubscribeID(evname string, id interface{}) int {
 
 
 	// Get list of subscribers for this event
 	// Get list of subscribers for this event
 	// If not found, nothing to do
 	// If not found, nothing to do
-	subs, ok := ed.evmap[evname]
+	subs, ok := d.evmap[evname]
 	if !ok {
 	if !ok {
 		return 0
 		return 0
 	}
 	}
@@ -82,56 +81,52 @@ func (ed *Dispatcher) UnsubscribeID(evname string, id interface{}) int {
 			pos++
 			pos++
 		}
 		}
 	}
 	}
-	//log.Debug("Dispatcher(%p).UnsubscribeID:%s (%p): %v",ed, evname, id, found)
-	ed.evmap[evname] = subs
+	d.evmap[evname] = subs
 	return found
 	return found
 }
 }
 
 
+// UnsubscribeAllID unsubscribes from all events with the specified subscription id.
+// Returns the number of subscriptions found.
+func (d *Dispatcher) UnsubscribeAllID(id interface{}) int {
+
+	total := 0
+	for evname := range d.evmap {
+		found := d.UnsubscribeID(evname, id)
+		total += found
+	}
+	return total
+}
+
 // Dispatch dispatch the specified event and data to all registered subscribers.
 // Dispatch dispatch the specified event and data to all registered subscribers.
 // The function returns true if the propagation was cancelled by a subscriber.
 // The function returns true if the propagation was cancelled by a subscriber.
-func (ed *Dispatcher) Dispatch(evname string, ev interface{}) bool {
+func (d *Dispatcher) Dispatch(evname string, ev interface{}) bool {
 
 
 	// Get list of subscribers for this event
 	// Get list of subscribers for this event
-	subs := ed.evmap[evname]
+	subs := d.evmap[evname]
 	if subs == nil {
 	if subs == nil {
 		return false
 		return false
 	}
 	}
 
 
 	// Dispatch to all subscribers
 	// Dispatch to all subscribers
-	//log.Debug("Dispatcher(%p).Dispatch:%s", ed, evname)
-	ed.cancel = false
+	d.cancel = false
 	for i := 0; i < len(subs); i++ {
 	for i := 0; i < len(subs); i++ {
 		subs[i].cb(evname, ev)
 		subs[i].cb(evname, ev)
-		if ed.cancel {
+		if d.cancel {
 			break
 			break
 		}
 		}
 	}
 	}
-	return ed.cancel
+	return d.cancel
 }
 }
 
 
 // ClearSubscriptions clear all subscriptions from this dispatcher
 // ClearSubscriptions clear all subscriptions from this dispatcher
-func (ed *Dispatcher) ClearSubscriptions() {
+func (d *Dispatcher) ClearSubscriptions() {
 
 
-	ed.evmap = make(map[string][]subscription)
-	//log.Debug("Dispatcher(%p).ClearSubscriptions: %d", ed, len(ed.evmap))
+	d.evmap = make(map[string][]subscription)
 }
 }
 
 
 // CancelDispatch cancels the propagation of the current event.
 // CancelDispatch cancels the propagation of the current event.
 // No more subscribers will be called for this event dispatch.
 // No more subscribers will be called for this event dispatch.
-func (ed *Dispatcher) CancelDispatch() {
+func (d *Dispatcher) CancelDispatch() {
 
 
-	ed.cancel = true
+	d.cancel = true
 }
 }
-
-//// LogSubscriptions is used for debugging to log the current
-//// subscriptions of this dispatcher
-//func (ed *Dispatcher) LogSubscriptions() {
-//
-//    for evname, subs := range ed.evmap {
-//        log.Debug("event:%s", evname)
-//        for _, sub := range subs {
-//            log.Debug("   subscription:%v", sub)
-//        }
-//    }
-//    log.Debug("")
-//}

+ 28 - 15
util/application/application.go

@@ -22,9 +22,8 @@ import (
 	"github.com/g3n/engine/window"
 	"github.com/g3n/engine/window"
 )
 )
 
 
-// Application is a basic standard application object which can be extended.
-// It creates a Window, OpenGL state, default cameras, default scene and Gui and
-// has a method to run the render loop.
+// Application is a basic standard application object which can be used as a base for G3N applications.
+// It creates a Window, OpenGL state, default cameras, default scene and Gui and has a method to run the render loop.
 type Application struct {
 type Application struct {
 	core.Dispatcher                         // Embedded event dispatcher
 	core.Dispatcher                         // Embedded event dispatcher
 	core.TimerManager                       // Embedded timer manager
 	core.TimerManager                       // Embedded timer manager
@@ -56,7 +55,7 @@ type Application struct {
 
 
 }
 }
 
 
-// Options defines initial options passed to application creation function
+// Options defines initial options passed to the application creation function
 type Options struct {
 type Options struct {
 	Height      int  // Initial window height (default is screen width)
 	Height      int  // Initial window height (default is screen width)
 	Width       int  // Initial window width (default is screen height)
 	Width       int  // Initial window width (default is screen height)
@@ -66,15 +65,16 @@ type Options struct {
 	TargetFPS   uint // Desired frames per second rate (default = 60)
 	TargetFPS   uint // Desired frames per second rate (default = 60)
 }
 }
 
 
+// OnBeforeRender is the event generated by Application just before rendering the scene/gui
+const OnBeforeRender = "util.application.OnBeforeRender"
+
+// OnAfterRender is the event generated by Application just after rendering the scene/gui
+const OnAfterRender = "util.application.OnAfterRender"
+
 // appInstance contains the pointer to the single Application instance
 // appInstance contains the pointer to the single Application instance
 var appInstance *Application
 var appInstance *Application
 
 
-const (
-	OnBeforeRender = "util.application.OnBeforeRender"
-	OnAfterRender  = "util.application.OnAfterRender"
-)
-
-// Creates creates and returns the application object using the specified name for
+// Create creates and returns the application object using the specified name for
 // the window title and log messages
 // the window title and log messages
 // This function must be called only once.
 // This function must be called only once.
 func Create(name string, ops Options) (*Application, error) {
 func Create(name string, ops Options) (*Application, error) {
@@ -122,7 +122,7 @@ func Create(name string, ops Options) (*Application, error) {
 	runtime.LockOSThread()
 	runtime.LockOSThread()
 
 
 	// Creates window
 	// Creates window
-	win, err := window.New("glfw", 801, 600, name, *app.fullScreen)
+	win, err := window.New("glfw", 800, 600, name, *app.fullScreen)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -200,6 +200,12 @@ func Create(name string, ops Options) (*Application, error) {
 	// Create frame rater
 	// Create frame rater
 	app.frameRater = NewFrameRater(*app.targetFPS)
 	app.frameRater = NewFrameRater(*app.targetFPS)
 
 
+	// Sets the default window resize event handler
+	app.win.SubscribeID(window.OnWindowSize, app, func(evname string, ev interface{}) {
+		app.OnWindowResize()
+	})
+	app.OnWindowResize()
+
 	return app, nil
 	return app, nil
 }
 }
 
 
@@ -356,15 +362,22 @@ func (app *Application) VorbisSupport() bool {
 	return app.vorbis
 	return app.vorbis
 }
 }
 
 
-// SetCpuProfile must be called before Run() and sets the file name for cpu profiling.
+// SetCPUProfile must be called before Run() and sets the file name for cpu profiling.
 // If set the cpu profiling starts before running the render loop and continues
 // If set the cpu profiling starts before running the render loop and continues
 // till the end of the application.
 // till the end of the application.
-func (app *Application) SetCpuProfile(fname string) {
+func (app *Application) SetCPUProfile(fname string) {
 
 
 	*app.cpuProfile = fname
 	*app.cpuProfile = fname
 }
 }
 
 
-// Runs runs the application render loop
+// SetOnWindowResize replaces the default window resize handler with the specified one
+func (app *Application) SetOnWindowResize(f func(evname string, ev interface{})) {
+
+	app.win.UnsubscribeID(window.OnWindowSize, app)
+	app.win.SubscribeID(window.OnWindowSize, app, f)
+}
+
+// Run runs the application render loop
 func (app *Application) Run() error {
 func (app *Application) Run() error {
 
 
 	// Set swap interval
 	// Set swap interval
@@ -441,7 +454,7 @@ func (app *Application) Quit() {
 }
 }
 
 
 // OnWindowResize is default handler for window resize events.
 // OnWindowResize is default handler for window resize events.
-func (app *Application) OnWindowResize(evname string, ev interface{}) {
+func (app *Application) OnWindowResize() {
 
 
 	// Get window size and sets the viewport to the same size
 	// Get window size and sets the viewport to the same size
 	width, height := app.win.GetSize()
 	width, height := app.win.GetSize()