Quellcode durchsuchen

added HiDPI support

Daniel Salvadori vor 7 Jahren
Ursprung
Commit
f367d868ed
5 geänderte Dateien mit 47 neuen und 17 gelöschten Zeilen
  1. 17 10
      gui/panel.go
  2. 11 0
      gui/root.go
  3. 12 0
      renderer/renderer.go
  4. 6 6
      window/glfw.go
  5. 1 1
      window/window.go

+ 17 - 10
gui/panel.go

@@ -44,6 +44,7 @@ type IPanel interface {
 	graphic.IGraphic
 	GetPanel() *Panel
 	SetRoot(*Root)
+	Root() *Root
 	LostKeyFocus()
 	TotalHeight() float32
 	TotalWidth() float32
@@ -204,8 +205,8 @@ func (p *Panel) GetPanel() *Panel {
 	return p
 }
 
-// SetRoot satisfies the IPanel interface
-// Sets the pointer to the root panel for this panel and all its children
+// SetRoot satisfies the IPanel interface.
+// Sets the pointer to the root panel for this panel and all its children.
 func (p *Panel) SetRoot(root *Root) {
 
 	p.root = root
@@ -215,6 +216,13 @@ func (p *Panel) SetRoot(root *Root) {
 	}
 }
 
+// Root satisfies the IPanel interface
+// Returns the pointer to the root panel for this panel's root.
+func (p *Panel) Root() *Root {
+
+	return p.root
+}
+
 // LostKeyFocus satisfies the IPanel interface and is called by gui root
 // container when the panel loses the key focus
 func (p *Panel) LostKeyFocus() {
@@ -241,12 +249,6 @@ func (p *Panel) Material() *material.Material {
 	return p.mat
 }
 
-// Root returns the pointer for this panel root panel
-func (p *Panel) Root() *Root {
-
-	return p.root
-}
-
 // SetTopChild sets the Z coordinate of the specified panel to
 // be on top of all other children of this panel.
 // The function does not check if the specified panel is a
@@ -965,10 +967,15 @@ func (p *Panel) RenderSetup(gl *gls.GLS, rinfo *core.RenderInfo) {
 // SetModelMatrix calculates and sets the specified matrix with the model matrix for this panel
 func (p *Panel) SetModelMatrix(gl *gls.GLS, mm *math32.Matrix4) {
 
+	// Get scale of window (for HiDPI support)
+	sX64, sY64 := p.Root().Window().Scale()
+	sX := float32(sX64)
+	sY := float32(sY64)
+
 	// Get the current viewport width and height
 	_, _, width, height := gl.GetViewport()
-	fwidth := float32(width)
-	fheight := float32(height)
+	fwidth := float32(width) / sX
+	fheight := float32(height) / sY
 
 	// Scale the quad for the viewport so it has fixed dimensions in pixels.
 	p.wclip = 2 * float32(p.width) / fwidth

+ 11 - 0
gui/root.go

@@ -77,6 +77,12 @@ func (r *Root) Add(ipan IPanel) {
 	r.Panel.Add(ipan)
 }
 
+// Window returns the associated IWindow.
+func (r *Root) Window() window.IWindow {
+
+	return r.win
+}
+
 // SetModal sets the modal panel.
 // If there is a modal panel, only events for this panel are dispatched
 // To remove the modal panel call this function with a nil panel.
@@ -249,6 +255,11 @@ func (r *Root) onCursor(evname string, ev interface{}) {
 // which contain the specified screen position
 func (r *Root) sendPanels(x, y float32, evname string, ev interface{}) {
 
+	// Apply scale of window (for HiDPI support)
+	sX64, sY64 := r.Window().Scale()
+	x /= float32(sX64)
+	y /= float32(sY64)
+
 	// If there is panel with MouseFocus send only to this panel
 	if r.mouseFocus != nil {
 		// Checks modal panel

+ 12 - 0
renderer/renderer.go

@@ -338,6 +338,18 @@ func (r *Renderer) renderScene(iscene core.INode, icam camera.ICamera) error {
 		if r.panel3D != nil {
 			pos := r.panel3D.GetPanel().Pospix()
 			width, height := r.panel3D.GetPanel().Size()
+
+			// Get scale of window (for HiDPI support)
+			sX64, sY64 := r.panel3D.Root().Window().Scale()
+			sX := float32(sX64)
+			sY := float32(sY64)
+
+			// Modify position and height of scissor according to window scale (for HiDPI support)
+			width *= sX
+			height *= sY
+			pos.X *= sX
+			pos.Y *= sY
+
 			_, _, _, viewheight := r.gs.GetViewport()
 			r.gs.Enable(gls.SCISSOR_TEST)
 			r.gs.Scissor(int32(pos.X), viewheight-int32(pos.Y)-int32(height), uint32(width), uint32(height))

+ 6 - 6
window/glfw.go

@@ -287,12 +287,6 @@ func (w *glfwWindow) SwapBuffers() {
 	w.win.SwapBuffers()
 }
 
-// Size returns this window size in screen coordinates
-func (w *glfwWindow) Size() (width int, height int) {
-
-	return w.win.GetSize()
-}
-
 // FramebufferSize returns framebuffer size of this window
 func (w *glfwWindow) FramebufferSize() (width int, height int) {
 
@@ -305,6 +299,12 @@ func (w *glfwWindow) Scale() (x float64, y float64) {
 	return w.scaleX, w.scaleY
 }
 
+// Size returns this window's size in screen coordinates
+func (w *glfwWindow) Size() (width int, height int) {
+
+	return w.win.GetSize()
+}
+
 // SetSize sets the size, in screen coordinates, of the client area of this window
 func (w *glfwWindow) SetSize(width int, height int) {
 

+ 1 - 1
window/window.go

@@ -25,8 +25,8 @@ type IWindow interface {
 	core.IDispatcher
 	MakeContextCurrent()
 	FramebufferSize() (width int, height int)
-	Size() (width int, height int)
 	Scale() (x float64, y float64)
+	Size() (width int, height int)
 	SetSize(width int, height int)
 	Pos() (xpos, ypos int)
 	SetPos(xpos, ypos int)